summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Barkov <bar@mariadb.org>2016-05-24 18:05:38 +0400
committerAlexander Barkov <bar@mariadb.org>2016-05-24 18:05:38 +0400
commitc80c3f6759e456b88211f8e207648ee5d3964dac (patch)
tree371a553ff4b77722df92536ea6da4a6f71d09f28
parentea9a393a868dda808bd0c7a3f03b28208eb6570f (diff)
downloadmariadb-git-c80c3f6759e456b88211f8e207648ee5d3964dac.tar.gz
MDEV-10109 Disallow syntactically INSERT .. SELECT .. {ORDER BY ..| LIMIT ..} .. UNION ..
-rw-r--r--mysql-test/r/parser.result11
-rw-r--r--mysql-test/r/union.result2
-rw-r--r--mysql-test/t/parser.test13
-rw-r--r--mysql-test/t/union.test2
-rw-r--r--sql/sql_yacc.yy56
5 files changed, 57 insertions, 27 deletions
diff --git a/mysql-test/r/parser.result b/mysql-test/r/parser.result
index f0cfde65b57..efb9ff07565 100644
--- a/mysql-test/r/parser.result
+++ b/mysql-test/r/parser.result
@@ -886,3 +886,14 @@ INSERT INTO t1 VALUES (1),(2),(3);
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
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
diff --git a/mysql-test/r/union.result b/mysql-test/r/union.result
index 6a52bc023e1..74eb3e069df 100644
--- a/mysql-test/r/union.result
+++ b/mysql-test/r/union.result
@@ -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;
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;
-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;
ERROR 21000: The used SELECT statements have a different number of columns
select a,b from t1 union select a from t2;
diff --git a/mysql-test/t/parser.test b/mysql-test/t/parser.test
index aae6b2c34be..0450e3c6fb0 100644
--- a/mysql-test/t/parser.test
+++ b/mysql-test/t/parser.test
@@ -1030,3 +1030,16 @@ INSERT INTO t1 VALUES (1),(2),(3);
--error ER_PARSE_ERROR
SELECT * FROM (SELECT * FROM t1 LIMIT 1 LIMIT 2) 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;
diff --git a/mysql-test/t/union.test b/mysql-test/t/union.test
index d7c67444015..151512515b9 100644
--- a/mysql-test/t/union.test
+++ b/mysql-test/t/union.test
@@ -56,7 +56,7 @@ select a,b from t1 into outfile 'skr' union select a,b from t2;
--error ER_PARSE_ERROR
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;
--error 1222
diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy
index 260f570ffd2..b116202d366 100644
--- a/sql/sql_yacc.yy
+++ b/sql/sql_yacc.yy
@@ -4944,7 +4944,8 @@ create_body:
conflict that prevents the rule above from parsing a syntax like
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
{
@@ -4965,12 +4966,18 @@ create_like:
opt_create_select:
/* 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);}
- union_clause {}
- | opt_duplicate opt_as '(' create_select ')'
- { Select->set_braces(1);}
- union_opt {}
+ union_clause
+ | SELECT_SYM create_select_part2 create_select_part3_union_not_ready
+ create_select_part4
+ | '(' create_select_query_specification ')'
+ { Select->set_braces(1);} union_opt {}
;
opt_create_partitioning:
@@ -5679,8 +5686,11 @@ opt_part_option:
End of partition parser part
*/
-create_select:
- SELECT_SYM
+create_select_query_specification:
+ SELECT_SYM create_select_part2 create_select_part3 create_select_part4
+ ;
+
+create_select_part2:
{
LEX *lex=Lex;
if (lex->sql_command == SQLCOM_INSERT)
@@ -5699,18 +5709,19 @@ create_select:
{
Select->parsing_place= NO_MATTER;
}
- /*
- TODO:
- The following sequence repeats a few times:
- opt_table_expression
- opt_order_clause
- opt_limit_clause
- opt_select_lock_type
- Perhaps they can be grouped into a dedicated rule.
- */
+ ;
+
+create_select_part3:
opt_table_expression
- opt_order_clause
- opt_limit_clause
+ | create_select_part3_union_not_ready
+ ;
+
+create_select_part3_union_not_ready:
+ table_expression order_or_limit
+ | order_or_limit
+ ;
+
+create_select_part4:
opt_select_lock_type
{
/*
@@ -12536,12 +12547,7 @@ fields:
insert_values:
VALUES values_list {}
| VALUE_SYM values_list {}
- | create_select
- { Select->set_braces(0);}
- union_clause {}
- | '(' create_select ')'
- { Select->set_braces(1);}
- union_opt {}
+ | create_select_query_expression_body {}
;
values_list: