Skip to content

Commit

Permalink
MDEV-10095 Fix derived tables to return a syntax error instead of "Il…
Browse files Browse the repository at this point in the history
…legal usage of UNION and LIMIT"
  • Loading branch information
Alexander Barkov committed May 21, 2016
1 parent 349da1d commit a999acf
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 48 deletions.
2 changes: 1 addition & 1 deletion mysql-test/r/join.result
Original file line number Diff line number Diff line change
Expand Up @@ -1476,7 +1476,7 @@ DROP TABLE t1,t2,t3,t4,t5;
# MDEV-4752: Segfault during parsing of illegal query
#
SELECT * FROM t5 JOIN (t1 JOIN t2 UNION SELECT * FROM t3 JOIN t4);
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT * FROM t3 JOIN t4)' at line 1
#
# MDEV-4959: join of const table with NULL fields
#
Expand Down
14 changes: 7 additions & 7 deletions mysql-test/r/parser.result
Original file line number Diff line number Diff line change
Expand Up @@ -822,19 +822,19 @@ ERROR 42000: You have an error in your SQL syntax; check the manual that corresp
SELECT (SELECT 1 FROM t1 ORDER BY 1 UNION SELECT 1 FROM t1);
ERROR HY000: Incorrect usage of UNION and ORDER BY
SELECT 1 FROM (SELECT 1 FROM t1 ORDER BY 1 UNION SELECT 1 FROM t1) a;
ERROR HY000: Incorrect usage of UNION and ORDER BY
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 FROM t1) a' at line 1
SELECT 1 FROM t1 LIMIT 1 UNION SELECT 1 FROM t1;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 FROM t1' at line 1
SELECT (SELECT 1 FROM t1 LIMIT 1 UNION SELECT 1 FROM t1);
ERROR HY000: Incorrect usage of UNION and LIMIT
SELECT 1 FROM (SELECT 1 FROM t1 LIMIT 1 UNION SELECT 1 FROM t1) a;
ERROR HY000: Incorrect usage of UNION and LIMIT
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 FROM t1) a' at line 1
SELECT 1 FROM t1 ORDER BY 1 LIMIT 1 UNION SELECT 1 FROM t1;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 FROM t1' at line 1
SELECT (SELECT 1 FROM t1 ORDER BY 1 LIMIT 1 UNION SELECT 1 FROM t1);
ERROR HY000: Incorrect usage of UNION and ORDER BY
SELECT 1 FROM (SELECT 1 FROM t1 ORDER BY 1 LIMIT 1 UNION SELECT 1 FROM t1) a;
ERROR HY000: Incorrect usage of UNION and ORDER BY
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 FROM t1) a' at line 1
SELECT 1 FROM t1 LIMIT 1 ORDER BY 1 UNION SELECT 1 FROM t1;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ORDER BY 1 UNION SELECT 1 FROM t1' at line 1
SELECT (SELECT 1 FROM t1 LIMIT 1 ORDER BY 1 UNION SELECT 1 FROM t1);
Expand All @@ -846,25 +846,25 @@ ERROR 42000: You have an error in your SQL syntax; check the manual that corresp
SELECT (SELECT 1 FROM t1 ORDER BY 1 UNION SELECT 1 FROM t1 ORDER BY 1);
ERROR HY000: Incorrect usage of UNION and ORDER BY
SELECT 1 FROM (SELECT 1 FROM t1 ORDER BY 1 UNION SELECT 1 FROM t1 ORDER BY 1) a;
ERROR HY000: Incorrect usage of UNION and ORDER BY
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 FROM t1 ORDER BY 1) a' at line 1
SELECT 1 FROM t1 LIMIT 1 UNION SELECT 1 FROM t1 LIMIT 1;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 FROM t1 LIMIT 1' at line 1
SELECT (SELECT 1 FROM t1 LIMIT 1 UNION SELECT 1 FROM t1 LIMIT 1);
ERROR HY000: Incorrect usage of UNION and LIMIT
SELECT 1 FROM (SELECT 1 FROM t1 LIMIT 1 UNION SELECT 1 FROM t1 LIMIT 1) a;
ERROR HY000: Incorrect usage of UNION and LIMIT
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 FROM t1 LIMIT 1) a' at line 1
SELECT 1 FROM t1 LIMIT 1 UNION SELECT 1 FROM t1 ORDER BY 1;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 FROM t1 ORDER BY 1' at line 1
SELECT (SELECT 1 FROM t1 LIMIT 1 UNION SELECT 1 FROM t1 ORDER BY 1);
ERROR HY000: Incorrect usage of UNION and LIMIT
SELECT 1 FROM (SELECT 1 FROM t1 LIMIT 1 UNION SELECT 1 FROM t1 ORDER BY 1) a;
ERROR HY000: Incorrect usage of UNION and LIMIT
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 FROM t1 ORDER BY 1) a' at line 1
SELECT 1 FROM t1 ORDER BY 1 UNION SELECT 1 FROM t1 LIMIT 1;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 FROM t1 LIMIT 1' at line 1
SELECT (SELECT 1 FROM t1 ORDER BY 1 UNION SELECT 1 FROM t1 LIMIT 1);
ERROR HY000: Incorrect usage of UNION and ORDER BY
SELECT 1 FROM (SELECT 1 FROM t1 ORDER BY 1 UNION SELECT 1 FROM t1 LIMIT 1) a;
ERROR HY000: Incorrect usage of UNION and ORDER BY
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 FROM t1 LIMIT 1) a' at line 1
DROP TABLE t1;
#
# MDEV-8380: Subquery parse error
Expand Down
4 changes: 2 additions & 2 deletions mysql-test/r/subselect.result
Original file line number Diff line number Diff line change
Expand Up @@ -5188,9 +5188,9 @@ a 1
SELECT * FROM t1 JOIN ((SELECT 1 UNION SELECT 1)) ON 1;
ERROR 42000: Every derived table must have its own alias
SELECT * FROM t1 JOIN (t1 t1a UNION SELECT 1) ON 1;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ') ON 1' at line 1
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT 1) ON 1' at line 1
SELECT * FROM t1 JOIN ((t1 t1a UNION SELECT 1)) ON 1;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')) ON 1' at line 1
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT 1)) ON 1' at line 1
SELECT * FROM t1 JOIN (t1 t1a) t1a ON 1;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 't1a ON 1' at line 1
SELECT * FROM t1 JOIN ((t1 t1a)) t1a ON 1;
Expand Down
4 changes: 2 additions & 2 deletions mysql-test/r/subselect_no_exists_to_in.result
Original file line number Diff line number Diff line change
Expand Up @@ -5190,9 +5190,9 @@ a 1
SELECT * FROM t1 JOIN ((SELECT 1 UNION SELECT 1)) ON 1;
ERROR 42000: Every derived table must have its own alias
SELECT * FROM t1 JOIN (t1 t1a UNION SELECT 1) ON 1;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ') ON 1' at line 1
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT 1) ON 1' at line 1
SELECT * FROM t1 JOIN ((t1 t1a UNION SELECT 1)) ON 1;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')) ON 1' at line 1
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT 1)) ON 1' at line 1
SELECT * FROM t1 JOIN (t1 t1a) t1a ON 1;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 't1a ON 1' at line 1
SELECT * FROM t1 JOIN ((t1 t1a)) t1a ON 1;
Expand Down
4 changes: 2 additions & 2 deletions mysql-test/r/subselect_no_mat.result
Original file line number Diff line number Diff line change
Expand Up @@ -5188,9 +5188,9 @@ a 1
SELECT * FROM t1 JOIN ((SELECT 1 UNION SELECT 1)) ON 1;
ERROR 42000: Every derived table must have its own alias
SELECT * FROM t1 JOIN (t1 t1a UNION SELECT 1) ON 1;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ') ON 1' at line 1
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT 1) ON 1' at line 1
SELECT * FROM t1 JOIN ((t1 t1a UNION SELECT 1)) ON 1;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')) ON 1' at line 1
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT 1)) ON 1' at line 1
SELECT * FROM t1 JOIN (t1 t1a) t1a ON 1;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 't1a ON 1' at line 1
SELECT * FROM t1 JOIN ((t1 t1a)) t1a ON 1;
Expand Down
4 changes: 2 additions & 2 deletions mysql-test/r/subselect_no_opts.result
Original file line number Diff line number Diff line change
Expand Up @@ -5184,9 +5184,9 @@ a 1
SELECT * FROM t1 JOIN ((SELECT 1 UNION SELECT 1)) ON 1;
ERROR 42000: Every derived table must have its own alias
SELECT * FROM t1 JOIN (t1 t1a UNION SELECT 1) ON 1;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ') ON 1' at line 1
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT 1) ON 1' at line 1
SELECT * FROM t1 JOIN ((t1 t1a UNION SELECT 1)) ON 1;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')) ON 1' at line 1
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT 1)) ON 1' at line 1
SELECT * FROM t1 JOIN (t1 t1a) t1a ON 1;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 't1a ON 1' at line 1
SELECT * FROM t1 JOIN ((t1 t1a)) t1a ON 1;
Expand Down
4 changes: 2 additions & 2 deletions mysql-test/r/subselect_no_scache.result
Original file line number Diff line number Diff line change
Expand Up @@ -5194,9 +5194,9 @@ a 1
SELECT * FROM t1 JOIN ((SELECT 1 UNION SELECT 1)) ON 1;
ERROR 42000: Every derived table must have its own alias
SELECT * FROM t1 JOIN (t1 t1a UNION SELECT 1) ON 1;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ') ON 1' at line 1
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT 1) ON 1' at line 1
SELECT * FROM t1 JOIN ((t1 t1a UNION SELECT 1)) ON 1;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')) ON 1' at line 1
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT 1)) ON 1' at line 1
SELECT * FROM t1 JOIN (t1 t1a) t1a ON 1;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 't1a ON 1' at line 1
SELECT * FROM t1 JOIN ((t1 t1a)) t1a ON 1;
Expand Down
4 changes: 2 additions & 2 deletions mysql-test/r/subselect_no_semijoin.result
Original file line number Diff line number Diff line change
Expand Up @@ -5184,9 +5184,9 @@ a 1
SELECT * FROM t1 JOIN ((SELECT 1 UNION SELECT 1)) ON 1;
ERROR 42000: Every derived table must have its own alias
SELECT * FROM t1 JOIN (t1 t1a UNION SELECT 1) ON 1;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ') ON 1' at line 1
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT 1) ON 1' at line 1
SELECT * FROM t1 JOIN ((t1 t1a UNION SELECT 1)) ON 1;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')) ON 1' at line 1
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT 1)) ON 1' at line 1
SELECT * FROM t1 JOIN (t1 t1a) t1a ON 1;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 't1a ON 1' at line 1
SELECT * FROM t1 JOIN ((t1 t1a)) t1a ON 1;
Expand Down
14 changes: 7 additions & 7 deletions mysql-test/t/parser.test
Original file line number Diff line number Diff line change
Expand Up @@ -951,23 +951,23 @@ let $q=SELECT 1 FROM t1 ORDER BY 1 UNION SELECT 1 FROM t1;
eval $q;
--error ER_WRONG_USAGE
eval SELECT ($q);
--error ER_WRONG_USAGE
--error ER_PARSE_ERROR
eval SELECT 1 FROM ($q) a;

let $q=SELECT 1 FROM t1 LIMIT 1 UNION SELECT 1 FROM t1;
--error ER_PARSE_ERROR
eval $q;
--error ER_WRONG_USAGE
eval SELECT ($q);
--error ER_WRONG_USAGE
--error ER_PARSE_ERROR
eval SELECT 1 FROM ($q) a;

let $q=SELECT 1 FROM t1 ORDER BY 1 LIMIT 1 UNION SELECT 1 FROM t1;
--error ER_PARSE_ERROR
eval $q;
--error ER_WRONG_USAGE
eval SELECT ($q);
--error ER_WRONG_USAGE
--error ER_PARSE_ERROR
eval SELECT 1 FROM ($q) a;

let $q=SELECT 1 FROM t1 LIMIT 1 ORDER BY 1 UNION SELECT 1 FROM t1;
Expand All @@ -983,31 +983,31 @@ let $q=SELECT 1 FROM t1 ORDER BY 1 UNION SELECT 1 FROM t1 ORDER BY 1;
eval $q;
--error ER_WRONG_USAGE
eval SELECT ($q);
--error ER_WRONG_USAGE
--error ER_PARSE_ERROR
eval SELECT 1 FROM ($q) a;

let $q=SELECT 1 FROM t1 LIMIT 1 UNION SELECT 1 FROM t1 LIMIT 1;
--error ER_PARSE_ERROR
eval $q;
--error ER_WRONG_USAGE
eval SELECT ($q);
--error ER_WRONG_USAGE
--error ER_PARSE_ERROR
eval SELECT 1 FROM ($q) a;

let $q=SELECT 1 FROM t1 LIMIT 1 UNION SELECT 1 FROM t1 ORDER BY 1;
--error ER_PARSE_ERROR
eval $q;
--error ER_WRONG_USAGE
eval SELECT ($q);
--error ER_WRONG_USAGE
--error ER_PARSE_ERROR
eval SELECT 1 FROM ($q) a;

let $q=SELECT 1 FROM t1 ORDER BY 1 UNION SELECT 1 FROM t1 LIMIT 1;
--error ER_PARSE_ERROR
eval $q;
--error ER_WRONG_USAGE
eval SELECT ($q);
--error ER_WRONG_USAGE
--error ER_PARSE_ERROR
eval SELECT 1 FROM ($q) a;

DROP TABLE t1;
Expand Down
55 changes: 34 additions & 21 deletions sql/sql_yacc.yy
Original file line number Diff line number Diff line change
Expand Up @@ -1882,6 +1882,7 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
table_primary_ident table_primary_derived
select_derived derived_table_list
select_derived_union
derived_query_specification

%type <date_time_type> date_time_type;
%type <interval> interval
Expand Down Expand Up @@ -11113,34 +11114,39 @@ table_primary_derived:
subqueries have their own union rules.
*/
select_derived_union:
select_derived opt_union_order_or_limit
select_derived
| select_derived union_order_or_limit
{
if ($1 && $2)
if ($1)
{
my_parse_error(thd, ER_SYNTAX_ERROR);
MYSQL_YYABORT;
}
}
| derived_query_specification
opt_order_clause
opt_limit_clause
opt_select_lock_type
{ $$= NULL; }
| select_derived_union union_head_non_top query_term
| select_derived union_head_non_top
{
/*
Remove from the name resolution context stack the context of the
last select in the union.
*/
Lex->pop_context();

if ($1 != NULL)
if ($1)
{
my_parse_error(thd, ER_SYNTAX_ERROR);
MYSQL_YYABORT;
}
}
;
union_list_derived_part2
| derived_query_specification opt_select_lock_type
| derived_query_specification order_or_limit opt_select_lock_type
| derived_query_specification opt_select_lock_type union_list_derived
;

union_list_derived_part2:
query_term_union_not_ready { Lex->pop_context(); }
| query_term_union_ready { Lex->pop_context(); }
| query_term_union_ready { Lex->pop_context(); } union_list_derived
;

union_list_derived:
union_head_non_top union_list_derived_part2
;


/* The equivalent of select_init2 for nested queries. */
select_init2_derived:
Expand Down Expand Up @@ -11206,6 +11212,7 @@ derived_query_specification:
MYSQL_YYABORT;
}
}
$$= NULL;
}
;

Expand Down Expand Up @@ -16398,12 +16405,18 @@ query_specification:
;

query_term:
query_specification
opt_order_clause
opt_limit_clause
opt_select_lock_type
query_term_union_ready
| query_term_union_not_ready
;

query_term_union_not_ready:
query_specification order_or_limit opt_select_lock_type
| '(' select_paren_derived ')' union_order_or_limit
;

query_term_union_ready:
query_specification opt_select_lock_type
| '(' select_paren_derived ')'
opt_union_order_or_limit
;

query_expression_body:
Expand Down

0 comments on commit a999acf

Please sign in to comment.