Commit c80c3f67 authored by Alexander Barkov's avatar Alexander Barkov

MDEV-10109 Disallow syntactically INSERT .. SELECT .. {ORDER BY ..| LIMIT ..} .. UNION ..

parent ea9a393a
...@@ -886,3 +886,14 @@ INSERT INTO t1 VALUES (1),(2),(3); ...@@ -886,3 +886,14 @@ INSERT INTO t1 VALUES (1),(2),(3);
SELECT * FROM (SELECT * FROM t1 LIMIT 1 LIMIT 2) t1; SELECT * FROM (SELECT * FROM t1 LIMIT 1 LIMIT 2) 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 'LIMIT 2) t1' 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 'LIMIT 2) t1' at line 1
DROP TABLE t1; DROP TABLE t1;
#
# MDEV-10109 Disallow syntactically INSERT .. SELECT .. {ORDER BY ..| LIMIT ..} .. UNION ..
#
INSERT INTO t1 SELECT 1 ORDER BY 1 UNION SELECT 2;
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 2' at line 1
INSERT INTO t1 SELECT 1 LIMIT 1 UNION SELECT 2;
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 2' at line 1
CREATE TABLE t1 AS SELECT 1 ORDER BY 1 UNION SELECT 2;
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 2' at line 1
CREATE TABLE t1 AS SELECT 1 LIMIT 1 UNION SELECT 2;
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 2' at line 1
...@@ -128,7 +128,7 @@ ERROR 42000: You have an error in your SQL syntax; check the manual that corresp ...@@ -128,7 +128,7 @@ ERROR 42000: You have an error in your SQL syntax; check the manual that corresp
select a,b from t1 order by a union select a,b from t2; select a,b from t1 order by a union select a,b from t2;
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 a,b from t2' 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 'union select a,b from t2' at line 1
insert into t3 select a from t1 order by a union select a from t2; insert into t3 select a from t1 order by a union select a from t2;
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 a from t2' at line 1
create table t3 select a,b from t1 union select a from t2; create table t3 select a,b from t1 union select a from t2;
ERROR 21000: The used SELECT statements have a different number of columns ERROR 21000: The used SELECT statements have a different number of columns
select a,b from t1 union select a from t2; select a,b from t1 union select a from t2;
......
...@@ -1030,3 +1030,16 @@ INSERT INTO t1 VALUES (1),(2),(3); ...@@ -1030,3 +1030,16 @@ INSERT INTO t1 VALUES (1),(2),(3);
--error ER_PARSE_ERROR --error ER_PARSE_ERROR
SELECT * FROM (SELECT * FROM t1 LIMIT 1 LIMIT 2) t1; SELECT * FROM (SELECT * FROM t1 LIMIT 1 LIMIT 2) t1;
DROP TABLE t1; DROP TABLE t1;
--echo #
--echo # MDEV-10109 Disallow syntactically INSERT .. SELECT .. {ORDER BY ..| LIMIT ..} .. UNION ..
--echo #
--error ER_PARSE_ERROR
INSERT INTO t1 SELECT 1 ORDER BY 1 UNION SELECT 2;
--error ER_PARSE_ERROR
INSERT INTO t1 SELECT 1 LIMIT 1 UNION SELECT 2;
--error ER_PARSE_ERROR
CREATE TABLE t1 AS SELECT 1 ORDER BY 1 UNION SELECT 2;
--error ER_PARSE_ERROR
CREATE TABLE t1 AS SELECT 1 LIMIT 1 UNION SELECT 2;
...@@ -56,7 +56,7 @@ select a,b from t1 into outfile 'skr' union select a,b from t2; ...@@ -56,7 +56,7 @@ select a,b from t1 into outfile 'skr' union select a,b from t2;
--error ER_PARSE_ERROR --error ER_PARSE_ERROR
select a,b from t1 order by a union select a,b from t2; select a,b from t1 order by a union select a,b from t2;
--error 1221 --error ER_PARSE_ERROR
insert into t3 select a from t1 order by a union select a from t2; insert into t3 select a from t1 order by a union select a from t2;
--error 1222 --error 1222
......
...@@ -4944,7 +4944,8 @@ create_body: ...@@ -4944,7 +4944,8 @@ create_body:
conflict that prevents the rule above from parsing a syntax like conflict that prevents the rule above from parsing a syntax like
CREATE TABLE t1 (SELECT 1); CREATE TABLE t1 (SELECT 1);
*/ */
| '(' create_select ')' { Select->set_braces(1);} union_opt {} | '(' create_select_query_specification ')'
{ Select->set_braces(1);} union_opt {}
| create_like | create_like
{ {
...@@ -4965,12 +4966,18 @@ create_like: ...@@ -4965,12 +4966,18 @@ create_like:
opt_create_select: opt_create_select:
/* empty */ {} /* empty */ {}
| opt_duplicate opt_as create_select | opt_duplicate opt_as create_select_query_expression_body
;
create_select_query_expression_body:
SELECT_SYM create_select_part2 opt_table_expression
create_select_part4
{ Select->set_braces(0);} { Select->set_braces(0);}
union_clause {} union_clause
| opt_duplicate opt_as '(' create_select ')' | SELECT_SYM create_select_part2 create_select_part3_union_not_ready
{ Select->set_braces(1);} create_select_part4
union_opt {} | '(' create_select_query_specification ')'
{ Select->set_braces(1);} union_opt {}
; ;
opt_create_partitioning: opt_create_partitioning:
...@@ -5679,8 +5686,11 @@ opt_part_option: ...@@ -5679,8 +5686,11 @@ opt_part_option:
End of partition parser part End of partition parser part
*/ */
create_select: create_select_query_specification:
SELECT_SYM SELECT_SYM create_select_part2 create_select_part3 create_select_part4
;
create_select_part2:
{ {
LEX *lex=Lex; LEX *lex=Lex;
if (lex->sql_command == SQLCOM_INSERT) if (lex->sql_command == SQLCOM_INSERT)
...@@ -5699,18 +5709,19 @@ create_select: ...@@ -5699,18 +5709,19 @@ create_select:
{ {
Select->parsing_place= NO_MATTER; Select->parsing_place= NO_MATTER;
} }
/* ;
TODO:
The following sequence repeats a few times: create_select_part3:
opt_table_expression
opt_order_clause
opt_limit_clause
opt_select_lock_type
Perhaps they can be grouped into a dedicated rule.
*/
opt_table_expression opt_table_expression
opt_order_clause | create_select_part3_union_not_ready
opt_limit_clause ;
create_select_part3_union_not_ready:
table_expression order_or_limit
| order_or_limit
;
create_select_part4:
opt_select_lock_type opt_select_lock_type
{ {
/* /*
...@@ -12536,12 +12547,7 @@ fields: ...@@ -12536,12 +12547,7 @@ fields:
insert_values: insert_values:
VALUES values_list {} VALUES values_list {}
| VALUE_SYM values_list {} | VALUE_SYM values_list {}
| create_select | create_select_query_expression_body {}
{ Select->set_braces(0);}
union_clause {}
| '(' create_select ')'
{ Select->set_braces(1);}
union_opt {}
; ;
values_list: values_list:
......
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