Commit 8b047ac5 authored by Michael Widenius's avatar Michael Widenius

Patch by Hartmut Holzgraefe

STRAIGHT_JOIN couldn't be combined with NATURAL or USING(), 
INNER JOIN not with NATURAL (MDEV-4271, MySQL Bug #35268)

Separate rules existed for "natural" (non-outer) joins and
for STRAIGHT_JOIN, with the only difference code wise being 
that with STRAIGHT_JOIN the "straight" property of the right
side table was set before calling the appropriate add_...()
function.

The "natural_join" parser rule has now been extended to also
accept STRAIGHT_JOIN, and the rule result value is set to
1 for straight joins, 0 otherwise, so acting as a "straight"
flag that can directly be assigned to the "straight" property
of the right side table.

The rule parsing NATURAL JOIN was hard coded to accept just
this keyword combination, without support for either 
STRAIGHT_JOIN or the optional INNER.

The "natural_join" rule has now been split up in an inner
"inner_join" rule that matches the JOIN, INNER JOIN and
STRAIGHT_JOIN cases while "natural_join" also matches
CROSS JOIN. 

The NATURAL rule has been changed to accept "inner_join"
instead of just JOIN, so now NATURAL STRAIGHT_JOIN and
NATURAL INNER JOIN also work as expected.

As a side effect the removal of the duplciated rules
for STRAIGHT_JOIN handling has reduced the shift/reduce
conflict count by one.

mysql-test/r/join.result:
  Added new test cases
mysql-test/t/join.test:
  Added new test cases
sql/sql_yacc.yy:
  The "natural_join" parser rule was extended to also accept STRAIGHT_JOIN
  NATURAL STRAIGHT_JOIN and NATURAL INNER JOIN also now work as expected
parent deeed041
......@@ -1473,3 +1473,44 @@ dog_id dog_id birthday dog_id t_id birthday dog_id t_id birthday a_id dog_id
SET optimizer_switch=@tmp_optimizer_switch;
DROP TABLE t1,t2,t3,t4,t5;
SET optimizer_switch=@save_optimizer_switch;
#
# Bug #35268: Parser can't handle STRAIGHT_JOIN with USING
#
CREATE TABLE t1 (a int);
INSERT INTO t1 (a) VALUES (1),(2),(3),(4),(5),(6),(7),(8);
CREATE TABLE t2 (a int);
INSERT INTO t2 (a) VALUES (1),(2),(3),(4);
EXPLAIN
SELECT t1.a FROM t1 NATURAL INNER JOIN t2 ORDER BY t1.a;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 4 Using temporary; Using filesort
1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where; Using join buffer (flat, BNL join)
SELECT t1.a FROM t1 NATURAL INNER JOIN t2 ORDER BY t1.a;
a
1
2
3
4
EXPLAIN
SELECT t1.a FROM t1 STRAIGHT_JOIN t2 USING(a) ORDER BY t1.a;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using temporary; Using filesort
1 SIMPLE t2 ALL NULL NULL NULL NULL 4 Using where; Using join buffer (flat, BNL join)
SELECT t1.a FROM t1 STRAIGHT_JOIN t2 USING(a) ORDER BY t1.a;
a
1
2
3
4
EXPLAIN
SELECT t1.a FROM t1 NATURAL STRAIGHT_JOIN t2 ORDER BY t1.a;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using temporary; Using filesort
1 SIMPLE t2 ALL NULL NULL NULL NULL 4 Using where; Using join buffer (flat, BNL join)
SELECT t1.a FROM t1 NATURAL STRAIGHT_JOIN t2 ORDER BY t1.a;
a
1
2
3
4
DROP TABLE t1,t2;
......@@ -1136,3 +1136,29 @@ SET optimizer_switch=@tmp_optimizer_switch;
DROP TABLE t1,t2,t3,t4,t5;
SET optimizer_switch=@save_optimizer_switch;
--echo #
--echo # Bug #35268: Parser can't handle STRAIGHT_JOIN with USING
--echo #
CREATE TABLE t1 (a int);
INSERT INTO t1 (a) VALUES (1),(2),(3),(4),(5),(6),(7),(8);
CREATE TABLE t2 (a int);
INSERT INTO t2 (a) VALUES (1),(2),(3),(4);
EXPLAIN
SELECT t1.a FROM t1 NATURAL INNER JOIN t2 ORDER BY t1.a;
SELECT t1.a FROM t1 NATURAL INNER JOIN t2 ORDER BY t1.a;
EXPLAIN
SELECT t1.a FROM t1 STRAIGHT_JOIN t2 USING(a) ORDER BY t1.a;
SELECT t1.a FROM t1 STRAIGHT_JOIN t2 USING(a) ORDER BY t1.a;
EXPLAIN
SELECT t1.a FROM t1 NATURAL STRAIGHT_JOIN t2 ORDER BY t1.a;
SELECT t1.a FROM t1 NATURAL STRAIGHT_JOIN t2 ORDER BY t1.a;
DROP TABLE t1,t2;
......@@ -789,10 +789,10 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
%pure_parser /* We have threads */
/*
Currently there are 174 shift/reduce conflicts.
Currently there are 173 shift/reduce conflicts.
We should not introduce new conflicts any more.
*/
%expect 174
%expect 173
/*
Comments for TOKENS.
......@@ -1623,7 +1623,7 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
clear_privileges flush_options flush_option
opt_with_read_lock flush_options_list
equal optional_braces
opt_mi_check_type opt_to mi_check_types normal_join
opt_mi_check_type opt_to mi_check_types
table_to_table_list table_to_table opt_table_list opt_as
handler_rkey_function handler_read_or_scan
single_multi table_wild_list table_wild_one opt_wild
......@@ -1680,8 +1680,12 @@ END_OF_INPUT
'-' '+' '*' '/' '%' '(' ')'
',' '!' '{' '}' '&' '|' AND_SYM OR_SYM OR_OR_SYM BETWEEN_SYM CASE_SYM
THEN_SYM WHEN_SYM DIV_SYM MOD_SYM OR2_SYM AND_AND_SYM DELETE_SYM
%type <num> normal_join inner_join
%%
/*
Indentation of grammar rules:
......@@ -9752,9 +9756,7 @@ join_table:
left-associative joins.
*/
table_ref normal_join table_ref %prec TABLE_REF_PRIORITY
{ MYSQL_YYABORT_UNLESS($1 && ($$=$3)); }
| table_ref STRAIGHT_JOIN table_factor
{ MYSQL_YYABORT_UNLESS($1 && ($$=$3)); $3->straight=1; }
{ MYSQL_YYABORT_UNLESS($1 && ($$=$3)); $3->straight=$2; }
| table_ref normal_join table_ref
ON
{
......@@ -9766,22 +9768,7 @@ join_table:
}
expr
{
add_join_on($3,$6);
Lex->pop_context();
Select->parsing_place= NO_MATTER;
}
| table_ref STRAIGHT_JOIN table_factor
ON
{
MYSQL_YYABORT_UNLESS($1 && $3);
/* Change the current name resolution context to a local context. */
if (push_new_name_resolution_context(YYTHD, $1, $3))
MYSQL_YYABORT;
Select->parsing_place= IN_ON;
}
expr
{
$3->straight=1;
$3->straight=$2;
add_join_on($3,$6);
Lex->pop_context();
Select->parsing_place= NO_MATTER;
......@@ -9792,10 +9779,15 @@ join_table:
MYSQL_YYABORT_UNLESS($1 && $3);
}
'(' using_list ')'
{ add_join_natural($1,$3,$7,Select); $$=$3; }
| table_ref NATURAL JOIN_SYM table_factor
{
$3->straight=$2;
add_join_natural($1,$3,$7,Select);
$$=$3;
}
| table_ref NATURAL inner_join table_factor
{
MYSQL_YYABORT_UNLESS($1 && ($$=$4));
$4->straight=$3;
add_join_natural($1,$4,NULL,Select);
}
......@@ -9875,10 +9867,16 @@ join_table:
}
;
inner_join: /* $$ set if using STRAIGHT_JOIN, false otherwise */
JOIN_SYM { $$ = 0; }
| INNER_SYM JOIN_SYM { $$ = 0; }
| STRAIGHT_JOIN { $$ = 1; }
;
normal_join:
JOIN_SYM {}
| INNER_SYM JOIN_SYM {}
| CROSS JOIN_SYM {}
inner_join { $$ = $1; }
| CROSS JOIN_SYM { $$ = 0; }
;
/*
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment