Commit bed4e847 authored by Oleksandr Byelkin's avatar Oleksandr Byelkin

MDEV-8380: Subquery parse error

backport mysql parser fixes
0034963fbf199696792491bcb79d5f0731c98804
5948561812bc691bd0c13cf518a3fe77d9daf920
parent d6371d3a
......@@ -650,3 +650,230 @@ CREATE TABLE t1(a INT);
SELECT * FROM t1 JOIN ((SELECT 1 AS b) UNION ALL (SELECT 2 AS b) ORDER BY b DESC) s1 WHERE a=1;
a b
DROP TABLE t1;
#
# Test of collective fix for three parser bugs:
#
# Bug #17727401, Bug #17426017, Bug #17473479:
# The server accepts wrong syntax and then fails in different ways
#
CREATE TABLE t1 (i INT);
# bug #17426017
SELECT (SELECT EXISTS(SELECT * LIMIT 1 ORDER BY VALUES (c00)));
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 VALUES (c00)))' at line 1
# bug#17473479
CREATE TABLE a(a int);
CREATE TABLE b(a int);
DELETE FROM b ORDER BY(SELECT 1 FROM a ORDER BY a ORDER BY a);
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 a)' at line 1
DROP TABLE a, b;
# bug #17727401
SELECT '' IN (SELECT '1' c FROM t1 ORDER BY '' ORDER BY '') 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 '') FROM t1' at line 1
# regression & coverage tests
# uniform syntax for FROM DUAL clause:
SELECT 1 FROM DUAL WHERE 1 GROUP BY 1 HAVING 1 ORDER BY 1
FOR UPDATE;
1
1
SELECT 1 FROM DUAL WHERE 1 GROUP BY 1 HAVING 1 ORDER BY 1
PROCEDURE ANALYSE() FOR UPDATE;
ERROR HY000: Can't use ORDER clause with this procedure
SELECT 1 FROM
(SELECT 1 FROM DUAL WHERE 1 GROUP BY 1 HAVING 1 ORDER BY 1
FOR UPDATE) a;
1
1
SELECT 1 FROM
(SELECT 1 FROM DUAL WHERE 1 GROUP BY 1 HAVING 1 ORDER BY 1
PROCEDURE ANALYSE() FOR UPDATE) a;
ERROR HY000: Incorrect usage of PROCEDURE and subquery
SELECT 1 FROM t1
WHERE EXISTS(SELECT 1 FROM DUAL WHERE 1 GROUP BY 1 HAVING 1 ORDER BY 1
FOR UPDATE);
1
SELECT 1 FROM t1
WHERE EXISTS(SELECT 1 FROM DUAL WHERE 1 GROUP BY 1 HAVING 1 ORDER BY 1
PROCEDURE ANALYSE() FOR UPDATE);
ERROR HY000: Incorrect usage of PROCEDURE and subquery
SELECT 1 FROM t1
UNION
SELECT 1 FROM DUAL WHERE 1 GROUP BY 1 HAVING 1 ORDER BY 1
FOR UPDATE;
1
1
SELECT 1 FROM t1
UNION
SELECT 1 FROM DUAL WHERE 1 GROUP BY 1 HAVING 1 ORDER BY 1
PROCEDURE ANALYSE() FOR UPDATE;
ERROR HY000: Incorrect usage of PROCEDURE and subquery
SELECT 1 FROM DUAL PROCEDURE ANALYSE()
UNION
SELECT 1 FROM t1;
ERROR HY000: Incorrect usage of UNION and SELECT ... PROCEDURE ANALYSE()
(SELECT 1 FROM t1)
UNION
(SELECT 1 FROM DUAL WHERE 1 GROUP BY 1 HAVING 1 ORDER BY 1
FOR UPDATE);
1
1
(SELECT 1 FROM t1)
UNION
(SELECT 1 FROM DUAL WHERE 1 GROUP BY 1 HAVING 1 ORDER BY 1
PROCEDURE ANALYSE() FOR UPDATE);
ERROR HY000: Incorrect usage of PROCEDURE and subquery
# "FOR UPDATE" tests
SELECT 1 FROM t1 UNION SELECT 1 FROM t1 ORDER BY 1 LIMIT 1;
1
SELECT 1 FROM t1 FOR UPDATE UNION SELECT 1 FROM t1 ORDER BY 1 LIMIT 1;
1
SELECT 1 FROM t1 UNION SELECT 1 FROM t1 ORDER BY 1 LIMIT 1 FOR UPDATE;
1
# "INTO" clause tests
SELECT 1 FROM t1 INTO @var17727401;
Warnings:
Warning 1329 No data - zero rows fetched, selected, or processed
SELECT 1 FROM DUAL INTO @var17727401;
SELECT 1 INTO @var17727401;
SELECT 1 INTO @var17727401 FROM t1;
Warnings:
Warning 1329 No data - zero rows fetched, selected, or processed
SELECT 1 INTO @var17727401 FROM DUAL;
SELECT 1 INTO @var17727401_1 FROM t1 INTO @var17727401_2;
ERROR HY000: Incorrect usage of INTO and INTO
SELECT 1 INTO @var17727401_1 FROM DUAL
INTO @var17727401_2;
ERROR HY000: Incorrect usage of INTO and INTO
SELECT 1 INTO @var17727401 FROM t1 WHERE 1 GROUP BY 1 HAVING 1 ORDER BY 1 LIMIT 1;
Warnings:
Warning 1329 No data - zero rows fetched, selected, or processed
SELECT 1 FROM t1 WHERE 1 GROUP BY 1 HAVING 1 ORDER BY 1 LIMIT 1 INTO @var17727401;
Warnings:
Warning 1329 No data - zero rows fetched, selected, or processed
SELECT 1 FROM t1 WHERE 1 INTO @var17727401 GROUP BY 1 HAVING 1 ORDER BY 1 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 'GROUP BY 1 HAVING 1 ORDER BY 1 LIMIT 1' at line 1
SELECT 1 INTO @var17727401_1
FROM t1 WHERE 1 GROUP BY 1 HAVING 1 ORDER BY 1 LIMIT 1
INTO @var17727401_2;
ERROR HY000: Incorrect usage of INTO and INTO
SELECT (SELECT 1 FROM t1 INTO @var17727401);
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 'INTO @var17727401)' at line 1
SELECT 1 FROM (SELECT 1 FROM t1 INTO @var17727401) a;
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 'INTO @var17727401) a' at line 1
SELECT EXISTS(SELECT 1 FROM t1 INTO @var17727401);
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 'INTO @var17727401)' at line 1
SELECT 1 FROM t1 INTO @var17727401 UNION SELECT 1 FROM t1 INTO t1;
ERROR HY000: Incorrect usage of UNION and INTO
(SELECT 1 FROM t1 INTO @var17727401) UNION (SELECT 1 FROM t1 INTO t1);
ERROR HY000: Incorrect usage of UNION and INTO
SELECT 1 FROM t1 UNION SELECT 1 FROM t1 INTO @var17727401;
Warnings:
Warning 1329 No data - zero rows fetched, selected, or processed
SELECT 1 INTO @var17727401 FROM t1 PROCEDURE ANALYSE();
ERROR HY000: Incorrect usage of PROCEDURE and INTO
SELECT 1 FROM t1 PROCEDURE ANALYSE() INTO @var17727401;
ERROR HY000: Incorrect usage of PROCEDURE and INTO
# ORDER and LIMIT clause combinations
(SELECT 1 FROM t1 ORDER BY 1) ORDER BY 1;
1
(SELECT 1 FROM t1 LIMIT 1) LIMIT 1;
1
((SELECT 1 FROM t1 ORDER BY 1) ORDER BY 1) 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 'ORDER BY 1) ORDER BY 1' at line 1
((SELECT 1 FROM t1 LIMIT 1) LIMIT 1) 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 'LIMIT 1) LIMIT 1' at line 1
(SELECT 1 FROM t1 ORDER BY 1) LIMIT 1;
1
(SELECT 1 FROM t1 LIMIT 1) ORDER BY 1;
1
((SELECT 1 FROM t1 ORDER BY 1) LIMIT 1) 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 'LIMIT 1) ORDER BY 1)' at line 1
((SELECT 1 FROM t1 LIMIT 1) ORDER BY 1) 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 'ORDER BY 1) LIMIT 1)' at line 1
SELECT 1 FROM t1 UNION SELECT 1 FROM t1 ORDER BY 1;
1
SELECT (SELECT 1 FROM t1 UNION SELECT 1 FROM t1 ORDER BY 1);
(SELECT 1 FROM t1 UNION SELECT 1 FROM t1 ORDER BY 1)
NULL
SELECT 1 FROM (SELECT 1 FROM t1 UNION SELECT 1 FROM t1 ORDER BY 1) a;
1
SELECT 1 FROM t1 UNION SELECT 1 FROM t1 LIMIT 1;
1
SELECT (SELECT 1 FROM t1 UNION SELECT 1 FROM t1 LIMIT 1);
(SELECT 1 FROM t1 UNION SELECT 1 FROM t1 LIMIT 1)
NULL
SELECT 1 FROM (SELECT 1 FROM t1 UNION SELECT 1 FROM t1 LIMIT 1) a;
1
SELECT 1 FROM t1 UNION SELECT 1 FROM t1 ORDER BY 1 LIMIT 1;
1
SELECT (SELECT 1 FROM t1 UNION SELECT 1 FROM t1 ORDER BY 1 LIMIT 1);
(SELECT 1 FROM t1 UNION SELECT 1 FROM t1 ORDER BY 1 LIMIT 1)
NULL
SELECT 1 FROM (SELECT 1 FROM t1 UNION SELECT 1 FROM t1 ORDER BY 1 LIMIT 1) a;
1
SELECT 1 FROM t1 UNION SELECT 1 FROM t1 LIMIT 1 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 'ORDER BY 1' at line 1
SELECT (SELECT 1 FROM t1 UNION SELECT 1 FROM t1 LIMIT 1 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 'ORDER BY 1)' at line 1
SELECT 1 FROM (SELECT 1 FROM t1 UNION SELECT 1 FROM t1 LIMIT 1 ORDER BY 1) a;
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) a' at line 1
SELECT 1 FROM t1 ORDER BY 1 UNION SELECT 1 FROM t1;
ERROR HY000: Incorrect usage of UNION and ORDER BY
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
SELECT 1 FROM t1 LIMIT 1 UNION SELECT 1 FROM t1;
ERROR HY000: Incorrect usage of UNION and LIMIT
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
SELECT 1 FROM t1 ORDER BY 1 LIMIT 1 UNION SELECT 1 FROM t1;
ERROR HY000: Incorrect usage of UNION and ORDER BY
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
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);
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 1 FROM (SELECT 1 FROM t1 LIMIT 1 ORDER BY 1 UNION SELECT 1 FROM t1) a;
ERROR HY000: Incorrect usage of UNION and ORDER BY
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 (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
SELECT 1 FROM t1 LIMIT 1 UNION SELECT 1 FROM t1 LIMIT 1;
ERROR HY000: Incorrect usage of UNION and LIMIT
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
SELECT 1 FROM t1 LIMIT 1 UNION SELECT 1 FROM t1 ORDER BY 1;
ERROR HY000: Incorrect usage of UNION and LIMIT
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
SELECT 1 FROM t1 ORDER BY 1 UNION SELECT 1 FROM t1 LIMIT 1;
ERROR HY000: Incorrect usage of UNION and ORDER BY
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
DROP TABLE t1;
#
# MDEV-8380: Subquery parse error
#
CREATE TABLE t1 ( a INT);
INSERT INTO t1 VALUES ( 2 );
SELECT *
FROM ( (SELECT a FROM t1 ORDER BY a) UNION (SELECT 1 as b ORDER BY b ) ) AS a1
WHERE a1.a = 1 OR a1.a = 2;
a
2
1
DROP TABLE t1;
......@@ -5138,8 +5138,9 @@ SELECT a FROM t1 WHERE a = 2
a
1
2
SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a);
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
SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a) q;
a
1
SELECT * FROM (SELECT 1 a UNION (SELECT 1 a)) alias;
a
1
......@@ -5181,7 +5182,7 @@ a 1
1 1
2 1
SELECT * FROM t1 JOIN ((SELECT 1 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: 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
SELECT * FROM t1 JOIN ((t1 t1a UNION SELECT 1)) ON 1;
......@@ -5300,7 +5301,10 @@ SELECT ((SELECT 1 UNION SELECT 1 UNION SELECT 1));
SELECT * FROM ( SELECT 1 UNION ( SELECT 1 UNION SELECT 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 ) )' at line 1
SELECT * FROM ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 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 )' at line 1
ERROR 42000: Every derived table must have its own alias
SELECT * FROM ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 ) a;
1
1
SELECT * FROM ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ) a;
1
1
......
......@@ -5140,8 +5140,9 @@ SELECT a FROM t1 WHERE a = 2
a
1
2
SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a);
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
SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a) q;
a
1
SELECT * FROM (SELECT 1 a UNION (SELECT 1 a)) alias;
a
1
......@@ -5183,7 +5184,7 @@ a 1
1 1
2 1
SELECT * FROM t1 JOIN ((SELECT 1 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: 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
SELECT * FROM t1 JOIN ((t1 t1a UNION SELECT 1)) ON 1;
......@@ -5302,7 +5303,10 @@ SELECT ((SELECT 1 UNION SELECT 1 UNION SELECT 1));
SELECT * FROM ( SELECT 1 UNION ( SELECT 1 UNION SELECT 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 ) )' at line 1
SELECT * FROM ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 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 )' at line 1
ERROR 42000: Every derived table must have its own alias
SELECT * FROM ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 ) a;
1
1
SELECT * FROM ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ) a;
1
1
......
......@@ -5138,8 +5138,9 @@ SELECT a FROM t1 WHERE a = 2
a
1
2
SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a);
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
SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a) q;
a
1
SELECT * FROM (SELECT 1 a UNION (SELECT 1 a)) alias;
a
1
......@@ -5181,7 +5182,7 @@ a 1
1 1
2 1
SELECT * FROM t1 JOIN ((SELECT 1 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: 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
SELECT * FROM t1 JOIN ((t1 t1a UNION SELECT 1)) ON 1;
......@@ -5300,7 +5301,10 @@ SELECT ((SELECT 1 UNION SELECT 1 UNION SELECT 1));
SELECT * FROM ( SELECT 1 UNION ( SELECT 1 UNION SELECT 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 ) )' at line 1
SELECT * FROM ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 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 )' at line 1
ERROR 42000: Every derived table must have its own alias
SELECT * FROM ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 ) a;
1
1
SELECT * FROM ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ) a;
1
1
......
......@@ -5134,8 +5134,9 @@ SELECT a FROM t1 WHERE a = 2
a
1
2
SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a);
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
SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a) q;
a
1
SELECT * FROM (SELECT 1 a UNION (SELECT 1 a)) alias;
a
1
......@@ -5177,7 +5178,7 @@ a 1
1 1
2 1
SELECT * FROM t1 JOIN ((SELECT 1 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: 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
SELECT * FROM t1 JOIN ((t1 t1a UNION SELECT 1)) ON 1;
......@@ -5296,7 +5297,10 @@ SELECT ((SELECT 1 UNION SELECT 1 UNION SELECT 1));
SELECT * FROM ( SELECT 1 UNION ( SELECT 1 UNION SELECT 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 ) )' at line 1
SELECT * FROM ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 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 )' at line 1
ERROR 42000: Every derived table must have its own alias
SELECT * FROM ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 ) a;
1
1
SELECT * FROM ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ) a;
1
1
......
......@@ -5144,8 +5144,9 @@ SELECT a FROM t1 WHERE a = 2
a
1
2
SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a);
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
SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a) q;
a
1
SELECT * FROM (SELECT 1 a UNION (SELECT 1 a)) alias;
a
1
......@@ -5187,7 +5188,7 @@ a 1
1 1
2 1
SELECT * FROM t1 JOIN ((SELECT 1 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: 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
SELECT * FROM t1 JOIN ((t1 t1a UNION SELECT 1)) ON 1;
......@@ -5306,7 +5307,10 @@ SELECT ((SELECT 1 UNION SELECT 1 UNION SELECT 1));
SELECT * FROM ( SELECT 1 UNION ( SELECT 1 UNION SELECT 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 ) )' at line 1
SELECT * FROM ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 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 )' at line 1
ERROR 42000: Every derived table must have its own alias
SELECT * FROM ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 ) a;
1
1
SELECT * FROM ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ) a;
1
1
......
......@@ -5134,8 +5134,9 @@ SELECT a FROM t1 WHERE a = 2
a
1
2
SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a);
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
SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a) q;
a
1
SELECT * FROM (SELECT 1 a UNION (SELECT 1 a)) alias;
a
1
......@@ -5177,7 +5178,7 @@ a 1
1 1
2 1
SELECT * FROM t1 JOIN ((SELECT 1 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: 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
SELECT * FROM t1 JOIN ((t1 t1a UNION SELECT 1)) ON 1;
......@@ -5296,7 +5297,10 @@ SELECT ((SELECT 1 UNION SELECT 1 UNION SELECT 1));
SELECT * FROM ( SELECT 1 UNION ( SELECT 1 UNION SELECT 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 ) )' at line 1
SELECT * FROM ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 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 )' at line 1
ERROR 42000: Every derived table must have its own alias
SELECT * FROM ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 ) a;
1
1
SELECT * FROM ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ) a;
1
1
......
......@@ -89,6 +89,10 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
NULL UNION RESULT <union1,2> ALL NULL NULL NULL NULL NULL NULL Using filesort
Warnings:
Note 1003 (select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t1` limit 2) union all (select `test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b` from `test`.`t2` order by `test`.`t2`.`a` limit 1) order by `b` desc
select count(*) from (
(select a,b from t1 limit 2) union all (select a,b from t2 order by a)) q;
count(*)
6
(select sql_calc_found_rows a,b from t1 limit 2) union all (select a,b from t2 order by a) limit 2;
a b
1 a
......@@ -96,6 +100,10 @@ a b
select found_rows();
found_rows()
6
select count(*) from (
select a,b from t1 union all select a,b from t2) q;
count(*)
8
select sql_calc_found_rows a,b from t1 union all select a,b from t2 limit 2;
a b
1 a
......@@ -308,12 +316,20 @@ create table t1 (a int);
insert into t1 values (1),(2),(3);
create table t2 (a int);
insert into t2 values (3),(4),(5);
SELECT COUNT(*) FROM (
(SELECT * FROM t1) UNION all (SELECT * FROM t2)) q;
COUNT(*)
6
(SELECT SQL_CALC_FOUND_ROWS * FROM t1) UNION all (SELECT * FROM t2) LIMIT 1;
a
1
select found_rows();
found_rows()
6
SELECT COUNT(*) FROM (
(SELECT * FROM t1 LIMIT 1) UNION all (SELECT * FROM t2)) q;
COUNT(*)
4
(SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1) UNION all (SELECT * FROM t2) LIMIT 2;
a
1
......@@ -321,6 +337,10 @@ a
select found_rows();
found_rows()
4
SELECT COUNT(*) FROM (
(SELECT * FROM t1 LIMIT 1) UNION all (SELECT * FROM t2)) q;
COUNT(*)
4
(SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1) UNION all (SELECT * FROM t2);
a
1
......@@ -330,6 +350,10 @@ a
select found_rows();
found_rows()
4
SELECT COUNT(*) FROM (
(SELECT * FROM t1) UNION all (SELECT * FROM t2 LIMIT 1)) q;
COUNT(*)
4
(SELECT SQL_CALC_FOUND_ROWS * FROM t1) UNION all (SELECT * FROM t2 LIMIT 1);
a
1
......@@ -345,6 +369,16 @@ a
select found_rows();
found_rows()
4
SELECT COUNT(*) FROM (
(SELECT * FROM t1 LIMIT 1) UNION SELECT * FROM t2) q;
COUNT(*)
4
(SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1) UNION SELECT * FROM t2 LIMIT 1;
a
1
select found_rows();
found_rows()
4
SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1 UNION all SELECT * FROM t2 LIMIT 2;
ERROR HY000: Incorrect usage of UNION and LIMIT
SELECT SQL_CALC_FOUND_ROWS * FROM t1 UNION all SELECT * FROM t2 LIMIT 2;
......@@ -354,13 +388,29 @@ a
select found_rows();
found_rows()
6
SELECT COUNT(*) FROM (
SELECT * FROM t1 UNION SELECT * FROM t2) q;
COUNT(*)
5
SELECT SQL_CALC_FOUND_ROWS * FROM t1 UNION SELECT * FROM t2 LIMIT 2;
a
1
2
SELECT COUNT(*) FROM (
(SELECT * FROM t1 LIMIT 1) UNION all SELECT * FROM t2) q;
COUNT(*)
4
(SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1) UNION all SELECT * FROM t2 LIMIT 2;
a
1
3
select found_rows();
found_rows()
5
4
SELECT COUNT(*) FROM (
SELECT * FROM t1 UNION all SELECT * FROM t2) q;
COUNT(*)
6
SELECT SQL_CALC_FOUND_ROWS * FROM t1 UNION SELECT * FROM t2 LIMIT 100;
a
1
......@@ -373,10 +423,39 @@ found_rows()
5
SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 100 UNION SELECT * FROM t2;
ERROR HY000: Incorrect usage of UNION and LIMIT
SELECT COUNT(*) FROM (
(SELECT * FROM t1 LIMIT 100) UNION SELECT * FROM t2) q;
COUNT(*)
5
(SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 100) UNION SELECT * FROM t2;
a
1
2
3
4
5
SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1 UNION SELECT * FROM t2;
ERROR HY000: Incorrect usage of UNION and LIMIT
SELECT COUNT(*) FROM (
(SELECT * FROM t1 LIMIT 1) UNION SELECT * FROM t2) q;
COUNT(*)
4
(SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1) UNION SELECT * FROM t2;
a
1
3
4
5
SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1 UNION SELECT * FROM t2 LIMIT 2;
ERROR HY000: Incorrect usage of UNION and LIMIT
SELECT COUNT(*) FROM (
(SELECT * FROM t1 LIMIT 1) UNION SELECT * FROM t2) q;
COUNT(*)
4
(SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1) UNION SELECT * FROM t2 LIMIT 2;
a
1
3
SELECT SQL_CALC_FOUND_ROWS * FROM t1 UNION SELECT * FROM t2 LIMIT 2,2;
a
3
......@@ -384,8 +463,21 @@ a
select found_rows();
found_rows()
5
SELECT COUNT(*) FROM (
SELECT * FROM t1 UNION SELECT * FROM t2) q;
COUNT(*)
5
SELECT SQL_CALC_FOUND_ROWS * FROM t1 limit 2,2 UNION SELECT * FROM t2;
ERROR HY000: Incorrect usage of UNION and LIMIT
SELECT COUNT(*) FROM (
(SELECT * FROM t1 limit 2,2) UNION SELECT * FROM t2) q;
COUNT(*)
3
(SELECT SQL_CALC_FOUND_ROWS * FROM t1 limit 2,2) UNION SELECT * FROM t2;
a
3
4
5
SELECT * FROM t1 UNION SELECT * FROM t2 ORDER BY a desc LIMIT 1;
a
5
......
......@@ -765,3 +765,259 @@ DROP TABLE t1;
CREATE TABLE t1(a INT);
SELECT * FROM t1 JOIN ((SELECT 1 AS b) UNION ALL (SELECT 2 AS b) ORDER BY b DESC) s1 WHERE a=1;
DROP TABLE t1;
--echo #
--echo # Test of collective fix for three parser bugs:
--echo #
--echo # Bug #17727401, Bug #17426017, Bug #17473479:
--echo # The server accepts wrong syntax and then fails in different ways
--echo #
CREATE TABLE t1 (i INT);
--echo # bug #17426017
--error ER_PARSE_ERROR
SELECT (SELECT EXISTS(SELECT * LIMIT 1 ORDER BY VALUES (c00)));
--echo # bug#17473479
CREATE TABLE a(a int);
CREATE TABLE b(a int);
--error ER_PARSE_ERROR
DELETE FROM b ORDER BY(SELECT 1 FROM a ORDER BY a ORDER BY a);
DROP TABLE a, b;
--echo # bug #17727401
--error ER_PARSE_ERROR
SELECT '' IN (SELECT '1' c FROM t1 ORDER BY '' ORDER BY '') FROM t1;
--echo # regression & coverage tests
--echo # uniform syntax for FROM DUAL clause:
SELECT 1 FROM DUAL WHERE 1 GROUP BY 1 HAVING 1 ORDER BY 1
FOR UPDATE;
--error ER_ORDER_WITH_PROC
SELECT 1 FROM DUAL WHERE 1 GROUP BY 1 HAVING 1 ORDER BY 1
PROCEDURE ANALYSE() FOR UPDATE;
SELECT 1 FROM
(SELECT 1 FROM DUAL WHERE 1 GROUP BY 1 HAVING 1 ORDER BY 1
FOR UPDATE) a;
--error ER_WRONG_USAGE
SELECT 1 FROM
(SELECT 1 FROM DUAL WHERE 1 GROUP BY 1 HAVING 1 ORDER BY 1
PROCEDURE ANALYSE() FOR UPDATE) a;
SELECT 1 FROM t1
WHERE EXISTS(SELECT 1 FROM DUAL WHERE 1 GROUP BY 1 HAVING 1 ORDER BY 1
FOR UPDATE);
--error ER_WRONG_USAGE
SELECT 1 FROM t1
WHERE EXISTS(SELECT 1 FROM DUAL WHERE 1 GROUP BY 1 HAVING 1 ORDER BY 1
PROCEDURE ANALYSE() FOR UPDATE);
SELECT 1 FROM t1
UNION
SELECT 1 FROM DUAL WHERE 1 GROUP BY 1 HAVING 1 ORDER BY 1
FOR UPDATE;
--error ER_WRONG_USAGE
SELECT 1 FROM t1
UNION
SELECT 1 FROM DUAL WHERE 1 GROUP BY 1 HAVING 1 ORDER BY 1
PROCEDURE ANALYSE() FOR UPDATE;
--error ER_WRONG_USAGE
SELECT 1 FROM DUAL PROCEDURE ANALYSE()
UNION
SELECT 1 FROM t1;
(SELECT 1 FROM t1)
UNION
(SELECT 1 FROM DUAL WHERE 1 GROUP BY 1 HAVING 1 ORDER BY 1
FOR UPDATE);
--error ER_WRONG_USAGE
(SELECT 1 FROM t1)
UNION
(SELECT 1 FROM DUAL WHERE 1 GROUP BY 1 HAVING 1 ORDER BY 1
PROCEDURE ANALYSE() FOR UPDATE);
--echo # "FOR UPDATE" tests
SELECT 1 FROM t1 UNION SELECT 1 FROM t1 ORDER BY 1 LIMIT 1;
SELECT 1 FROM t1 FOR UPDATE UNION SELECT 1 FROM t1 ORDER BY 1 LIMIT 1;
SELECT 1 FROM t1 UNION SELECT 1 FROM t1 ORDER BY 1 LIMIT 1 FOR UPDATE;
--echo # "INTO" clause tests
SELECT 1 FROM t1 INTO @var17727401;
SELECT 1 FROM DUAL INTO @var17727401;
SELECT 1 INTO @var17727401;
SELECT 1 INTO @var17727401 FROM t1;
SELECT 1 INTO @var17727401 FROM DUAL;
--error ER_WRONG_USAGE
SELECT 1 INTO @var17727401_1 FROM t1 INTO @var17727401_2;
--error ER_WRONG_USAGE
SELECT 1 INTO @var17727401_1 FROM DUAL
INTO @var17727401_2;
SELECT 1 INTO @var17727401 FROM t1 WHERE 1 GROUP BY 1 HAVING 1 ORDER BY 1 LIMIT 1;
SELECT 1 FROM t1 WHERE 1 GROUP BY 1 HAVING 1 ORDER BY 1 LIMIT 1 INTO @var17727401;
--error ER_PARSE_ERROR
SELECT 1 FROM t1 WHERE 1 INTO @var17727401 GROUP BY 1 HAVING 1 ORDER BY 1 LIMIT 1;
--error ER_WRONG_USAGE
SELECT 1 INTO @var17727401_1
FROM t1 WHERE 1 GROUP BY 1 HAVING 1 ORDER BY 1 LIMIT 1
INTO @var17727401_2;
--error ER_PARSE_ERROR
SELECT (SELECT 1 FROM t1 INTO @var17727401);
--error ER_PARSE_ERROR
SELECT 1 FROM (SELECT 1 FROM t1 INTO @var17727401) a;
--error ER_PARSE_ERROR
SELECT EXISTS(SELECT 1 FROM t1 INTO @var17727401);
--error ER_WRONG_USAGE
SELECT 1 FROM t1 INTO @var17727401 UNION SELECT 1 FROM t1 INTO t1;
--error ER_WRONG_USAGE
(SELECT 1 FROM t1 INTO @var17727401) UNION (SELECT 1 FROM t1 INTO t1);
SELECT 1 FROM t1 UNION SELECT 1 FROM t1 INTO @var17727401;
--error ER_WRONG_USAGE
SELECT 1 INTO @var17727401 FROM t1 PROCEDURE ANALYSE();
--error ER_WRONG_USAGE
SELECT 1 FROM t1 PROCEDURE ANALYSE() INTO @var17727401;
--echo # ORDER and LIMIT clause combinations
# Limited support for (SELECT ...) ORDER/LIMIT:
(SELECT 1 FROM t1 ORDER BY 1) ORDER BY 1;
(SELECT 1 FROM t1 LIMIT 1) LIMIT 1;
--error ER_PARSE_ERROR
((SELECT 1 FROM t1 ORDER BY 1) ORDER BY 1) ORDER BY 1;
--error ER_PARSE_ERROR
((SELECT 1 FROM t1 LIMIT 1) LIMIT 1) LIMIT 1;
(SELECT 1 FROM t1 ORDER BY 1) LIMIT 1;
(SELECT 1 FROM t1 LIMIT 1) ORDER BY 1;
--error ER_PARSE_ERROR
((SELECT 1 FROM t1 ORDER BY 1) LIMIT 1) ORDER BY 1);
--error ER_PARSE_ERROR
((SELECT 1 FROM t1 LIMIT 1) ORDER BY 1) LIMIT 1);
# ORDER/LIMIT and UNION:
let $q=SELECT 1 FROM t1 UNION SELECT 1 FROM t1 ORDER BY 1;
eval $q;
eval SELECT ($q);
eval SELECT 1 FROM ($q) a;
let $q=SELECT 1 FROM t1 UNION SELECT 1 FROM t1 LIMIT 1;
eval $q;
eval SELECT ($q);
eval SELECT 1 FROM ($q) a;
let $q=SELECT 1 FROM t1 UNION SELECT 1 FROM t1 ORDER BY 1 LIMIT 1;
eval $q;
eval SELECT ($q);
eval SELECT 1 FROM ($q) a;
let $q=SELECT 1 FROM t1 UNION SELECT 1 FROM t1 LIMIT 1 ORDER BY 1;
--error ER_PARSE_ERROR
eval $q;
--error ER_PARSE_ERROR
eval SELECT ($q);
--error ER_PARSE_ERROR
eval SELECT 1 FROM ($q) a;
let $q=SELECT 1 FROM t1 ORDER BY 1 UNION SELECT 1 FROM t1;
--error ER_WRONG_USAGE
eval $q;
--error ER_WRONG_USAGE
eval SELECT ($q);
--error ER_WRONG_USAGE
eval SELECT 1 FROM ($q) a;
let $q=SELECT 1 FROM t1 LIMIT 1 UNION SELECT 1 FROM t1;
--error ER_WRONG_USAGE
eval $q;
--error ER_WRONG_USAGE
eval SELECT ($q);
--error ER_WRONG_USAGE
eval SELECT 1 FROM ($q) a;
let $q=SELECT 1 FROM t1 ORDER BY 1 LIMIT 1 UNION SELECT 1 FROM t1;
--error ER_WRONG_USAGE
eval $q;
--error ER_WRONG_USAGE
eval SELECT ($q);
--error ER_WRONG_USAGE
eval SELECT 1 FROM ($q) a;
let $q=SELECT 1 FROM t1 LIMIT 1 ORDER BY 1 UNION SELECT 1 FROM t1;
--error ER_PARSE_ERROR
eval $q;
--error ER_PARSE_ERROR
eval SELECT ($q);
--error ER_WRONG_USAGE
eval SELECT 1 FROM ($q) a;
let $q=SELECT 1 FROM t1 ORDER BY 1 UNION SELECT 1 FROM t1 ORDER BY 1;
--error ER_WRONG_USAGE
eval $q;
--error ER_WRONG_USAGE
eval SELECT ($q);
--error ER_WRONG_USAGE
eval SELECT 1 FROM ($q) a;
let $q=SELECT 1 FROM t1 LIMIT 1 UNION SELECT 1 FROM t1 LIMIT 1;
--error ER_WRONG_USAGE
eval $q;
--error ER_WRONG_USAGE
eval SELECT ($q);
--error ER_WRONG_USAGE
eval SELECT 1 FROM ($q) a;
let $q=SELECT 1 FROM t1 LIMIT 1 UNION SELECT 1 FROM t1 ORDER BY 1;
--error ER_WRONG_USAGE
eval $q;
--error ER_WRONG_USAGE
eval SELECT ($q);
--error ER_WRONG_USAGE
eval SELECT 1 FROM ($q) a;
let $q=SELECT 1 FROM t1 ORDER BY 1 UNION SELECT 1 FROM t1 LIMIT 1;
--error ER_WRONG_USAGE
eval $q;
--error ER_WRONG_USAGE
eval SELECT ($q);
--error ER_WRONG_USAGE
eval SELECT 1 FROM ($q) a;
DROP TABLE t1;
--echo #
--echo # MDEV-8380: Subquery parse error
--echo #
CREATE TABLE t1 ( a INT);
INSERT INTO t1 VALUES ( 2 );
SELECT *
FROM ( (SELECT a FROM t1 ORDER BY a) UNION (SELECT 1 as b ORDER BY b ) ) AS a1
WHERE a1.a = 1 OR a1.a = 2;
DROP TABLE t1;
......@@ -4274,8 +4274,7 @@ SELECT * FROM (
# This was not allowed previously. Possibly, it should be allowed on the future.
# For now, the intent is to keep the fix as non-intrusive as possible.
--error ER_PARSE_ERROR
SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a);
SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a) q;
SELECT * FROM (SELECT 1 a UNION (SELECT 1 a)) alias;
SELECT * FROM (SELECT 1 UNION SELECT 1) t1a;
--error ER_PARSE_ERROR
......@@ -4310,7 +4309,7 @@ SELECT * FROM (SELECT 1 a UNION SELECT 1 a ORDER BY a LIMIT 1) t1a;
# aliases after.
#
SELECT * FROM t1 JOIN (SELECT 1 UNION SELECT 1) alias ON 1;
--error ER_PARSE_ERROR
--error ER_DERIVED_MUST_HAVE_ALIAS
SELECT * FROM t1 JOIN ((SELECT 1 UNION SELECT 1)) ON 1;
--error ER_PARSE_ERROR
SELECT * FROM t1 JOIN (t1 t1a UNION SELECT 1) ON 1;
......@@ -4402,8 +4401,9 @@ SELECT ((SELECT 1 UNION SELECT 1 UNION SELECT 1));
--error ER_PARSE_ERROR
SELECT * FROM ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
--error ER_PARSE_ERROR
--error ER_DERIVED_MUST_HAVE_ALIAS
SELECT * FROM ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 );
SELECT * FROM ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 ) a;
SELECT * FROM ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ) a;
--error ER_PARSE_ERROR
......
......@@ -26,8 +26,12 @@ select 't1',b,count(*) from t1 group by b UNION select 't2',b,count(*) from t2 g
--error 1250
(select a,b from t1 limit 2) union all (select a,b from t2 order by a limit 1) order by t1.b;
explain extended (select a,b from t1 limit 2) union all (select a,b from t2 order by a limit 1) order by b desc;
select count(*) from (
(select a,b from t1 limit 2) union all (select a,b from t2 order by a)) q;
(select sql_calc_found_rows a,b from t1 limit 2) union all (select a,b from t2 order by a) limit 2;
select found_rows();
select count(*) from (
select a,b from t1 union all select a,b from t2) q;
select sql_calc_found_rows a,b from t1 union all select a,b from t2 limit 2;
select found_rows();
......@@ -206,18 +210,30 @@ create table t2 (a int);
insert into t2 values (3),(4),(5);
# Test global limits
SELECT COUNT(*) FROM (
(SELECT * FROM t1) UNION all (SELECT * FROM t2)) q;
(SELECT SQL_CALC_FOUND_ROWS * FROM t1) UNION all (SELECT * FROM t2) LIMIT 1;
select found_rows();
SELECT COUNT(*) FROM (
(SELECT * FROM t1 LIMIT 1) UNION all (SELECT * FROM t2)) q;
(SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1) UNION all (SELECT * FROM t2) LIMIT 2;
select found_rows();
# Test cases where found_rows() should return number of returned rows
SELECT COUNT(*) FROM (
(SELECT * FROM t1 LIMIT 1) UNION all (SELECT * FROM t2)) q;
(SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1) UNION all (SELECT * FROM t2);
select found_rows();
SELECT COUNT(*) FROM (
(SELECT * FROM t1) UNION all (SELECT * FROM t2 LIMIT 1)) q;
(SELECT SQL_CALC_FOUND_ROWS * FROM t1) UNION all (SELECT * FROM t2 LIMIT 1);
select found_rows();
(SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1) UNION SELECT * FROM t2 LIMIT 1;
select found_rows();
SELECT COUNT(*) FROM (
(SELECT * FROM t1 LIMIT 1) UNION SELECT * FROM t2) q;
(SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1) UNION SELECT * FROM t2 LIMIT 1;
select found_rows();
# In these case found_rows() should work
--error ER_WRONG_USAGE
......@@ -226,20 +242,41 @@ SELECT SQL_CALC_FOUND_ROWS * FROM t1 UNION all SELECT * FROM t2 LIMIT 2;
select found_rows();
# The following examples will not be exact
SELECT COUNT(*) FROM (
SELECT * FROM t1 UNION SELECT * FROM t2) q;
SELECT SQL_CALC_FOUND_ROWS * FROM t1 UNION SELECT * FROM t2 LIMIT 2;
SELECT COUNT(*) FROM (
(SELECT * FROM t1 LIMIT 1) UNION all SELECT * FROM t2) q;
(SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1) UNION all SELECT * FROM t2 LIMIT 2;
select found_rows();
SELECT COUNT(*) FROM (
SELECT * FROM t1 UNION all SELECT * FROM t2) q;
SELECT SQL_CALC_FOUND_ROWS * FROM t1 UNION SELECT * FROM t2 LIMIT 100;
select found_rows();
--error ER_WRONG_USAGE
SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 100 UNION SELECT * FROM t2;
SELECT COUNT(*) FROM (
(SELECT * FROM t1 LIMIT 100) UNION SELECT * FROM t2) q;
(SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 100) UNION SELECT * FROM t2;
--error ER_WRONG_USAGE
SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1 UNION SELECT * FROM t2;
SELECT COUNT(*) FROM (
(SELECT * FROM t1 LIMIT 1) UNION SELECT * FROM t2) q;
(SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1) UNION SELECT * FROM t2;
--error ER_WRONG_USAGE
SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1 UNION SELECT * FROM t2 LIMIT 2;
SELECT COUNT(*) FROM (
(SELECT * FROM t1 LIMIT 1) UNION SELECT * FROM t2) q;
(SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1) UNION SELECT * FROM t2 LIMIT 2;
SELECT SQL_CALC_FOUND_ROWS * FROM t1 UNION SELECT * FROM t2 LIMIT 2,2;
select found_rows();
SELECT COUNT(*) FROM (
SELECT * FROM t1 UNION SELECT * FROM t2) q;
--error ER_WRONG_USAGE
SELECT SQL_CALC_FOUND_ROWS * FROM t1 limit 2,2 UNION SELECT * FROM t2;
SELECT COUNT(*) FROM (
(SELECT * FROM t1 limit 2,2) UNION SELECT * FROM t2) q;
(SELECT SQL_CALC_FOUND_ROWS * FROM t1 limit 2,2) UNION SELECT * FROM t2;
# Test some limits with ORDER BY
SELECT * FROM t1 UNION SELECT * FROM t2 ORDER BY a desc LIMIT 1;
......
......@@ -546,6 +546,7 @@ void lex_start(THD *thd)
lex->limit_rows_examined_cnt= ULONGLONG_MAX;
lex->var_list.empty();
lex->stmt_var_list.empty();
lex->proc_list.elements=0;
lex->is_lex_started= TRUE;
DBUG_VOID_RETURN;
......
......@@ -6959,11 +6959,28 @@ mysql_new_select(LEX *lex, bool move_down)
}
else
{
bool const outer_most= (lex->current_select->master_unit() == &lex->unit);
if (outer_most && lex->result)
{
my_error(ER_WRONG_USAGE, MYF(0), "UNION", "INTO");
DBUG_RETURN(TRUE);
}
if (lex->proc_list.elements!=0)
{
my_error(ER_WRONG_USAGE, MYF(0), "UNION",
"SELECT ... PROCEDURE ANALYSE()");
DBUG_RETURN(TRUE);
}
if (lex->current_select->order_list.first && !lex->current_select->braces)
{
my_error(ER_WRONG_USAGE, MYF(0), "UNION", "ORDER BY");
DBUG_RETURN(1);
}
if (lex->current_select->explicit_limit && !lex->current_select->braces)
{
my_error(ER_WRONG_USAGE, MYF(0), "UNION", "LIMIT");
DBUG_RETURN(1);
}
select_lex->include_neighbour(lex->current_select);
SELECT_LEX_UNIT *unit= select_lex->master_unit();
if (!unit->fake_select_lex && unit->add_fake_select_lex(lex->thd))
......
......@@ -1018,10 +1018,10 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
%parse-param { THD *thd }
%lex-param { THD *thd }
/*
Currently there are 164 shift/reduce conflicts.
Currently there are 160 shift/reduce conflicts.
We should not introduce new conflicts any more.
*/
%expect 164
%expect 160
/*
Comments for TOKENS.
......@@ -1748,6 +1748,7 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
opt_default_time_precision
case_stmt_body opt_bin_mod
opt_if_exists_table_element opt_if_not_exists_table_element
opt_into opt_procedure_clause
%type <object_ddl_options>
create_or_replace
......@@ -5588,7 +5589,7 @@ create_select:
{
Select->parsing_place= NO_MATTER;
}
opt_select_from
table_expression
{
/*
The following work only with the local list, the global list
......@@ -5983,11 +5984,6 @@ merge_insert_types:
| LAST_SYM { $$= MERGE_INSERT_TO_LAST; }
;
opt_select_from:
opt_limit_clause {}
| select_from select_lock_type
;
udf_type:
STRING_SYM {$$ = (int) STRING_RESULT; }
| REAL {$$ = (int) REAL_RESULT; }
......@@ -8453,6 +8449,7 @@ select_paren_derived:
Lex->current_select->set_braces(true);
}
SELECT_SYM select_part2_derived
table_expression
{
if (setup_select_in_parentheses(Lex))
MYSQL_YYABORT;
......@@ -8470,7 +8467,45 @@ select_init2:
union_clause
;
/*
Theoretically we can merge all 3 right hand sides of the select_part2
rule into one, however such a transformation adds one shift/reduce
conflict more.
*/
select_part2:
select_options_and_item_list
opt_order_clause
opt_limit_clause
opt_select_lock_type
| select_options_and_item_list into opt_select_lock_type
| select_options_and_item_list
opt_into
from_clause
opt_where_clause
opt_group_clause
opt_having_clause
opt_order_clause
opt_limit_clause
opt_procedure_clause
opt_into
opt_select_lock_type
{
if ($2 && $10)
{
/* double "INTO" clause */
my_error(ER_WRONG_USAGE, MYF(0), "INTO", "INTO");
MYSQL_YYABORT;
}
if ($9 && ($2 || $10))
{
/* "INTO" with "PROCEDURE ANALYSE" */
my_error(ER_WRONG_USAGE, MYF(0), "PROCEDURE", "INTO");
MYSQL_YYABORT;
}
}
;
select_options_and_item_list:
{
LEX *lex= Lex;
SELECT_LEX *sel= lex->current_select;
......@@ -8482,27 +8517,36 @@ select_part2:
{
Select->parsing_place= NO_MATTER;
}
select_into select_lock_type
;
select_into:
opt_order_clause opt_limit_clause {}
| into
| select_from
| into select_from
| select_from into
table_expression:
opt_from_clause
opt_where_clause
opt_group_clause
opt_having_clause
opt_order_clause
opt_limit_clause
opt_procedure_clause
opt_select_lock_type
;
select_from:
FROM join_table_list
from_clause:
FROM table_reference_list
;
opt_from_clause:
/* empty */
| from_clause
;
table_reference_list:
join_table_list
{
Select->context.table_list=
Select->context.first_name_resolution_table=
Select->table_list.first;
}
where_clause group_clause having_clause
opt_order_clause opt_limit_clause procedure_clause
| FROM DUAL_SYM where_clause opt_limit_clause
| DUAL_SYM
/* oracle compatibility: oracle always requires FROM clause,
and DUAL is system table without fields.
Is "SELECT 1 FROM DUAL" any better than "SELECT 1" ?
......@@ -8586,7 +8630,7 @@ select_option:
}
;
select_lock_type:
opt_select_lock_type:
/* empty */
| FOR_SYM UPDATE_SYM
{
......@@ -10953,9 +10997,7 @@ select_derived_union:
last select in the union.
*/
Lex->pop_context();
}
opt_union_order_or_limit
{
if ($1 != NULL)
{
my_parse_error(thd, ER_SYNTAX_ERROR);
......@@ -10969,18 +11011,11 @@ select_init2_derived:
select_part2_derived
{
LEX *lex= Lex;
SELECT_LEX * sel= lex->current_select;
if (lex->current_select->set_braces(0))
{
my_parse_error(thd, ER_SYNTAX_ERROR);
MYSQL_YYABORT;
}
if (sel->linkage == UNION_TYPE &&
sel->master_unit()->first_select()->braces)
{
my_parse_error(thd, ER_SYNTAX_ERROR);
MYSQL_YYABORT;
}
}
;
......@@ -10997,7 +11032,6 @@ select_part2_derived:
{
Select->parsing_place= NO_MATTER;
}
opt_select_from select_lock_type
;
/* handle contents of parentheses in join expression */
......@@ -11045,7 +11079,7 @@ select_derived2:
{
Select->parsing_place= NO_MATTER;
}
opt_select_from
table_expression
;
get_select_lex:
......@@ -11222,7 +11256,7 @@ opt_all:
| ALL
;
where_clause:
opt_where_clause:
/* empty */ { Select->where= 0; }
| WHERE
{
......@@ -11238,7 +11272,7 @@ where_clause:
}
;
having_clause:
opt_having_clause:
/* empty */
| HAVING
{
......@@ -11275,7 +11309,7 @@ opt_escape:
group by statement in select
*/
group_clause:
opt_group_clause:
/* empty */
| GROUP_SYM BY group_list olap_opt
;
......@@ -11423,25 +11457,6 @@ order_dir:
| DESC { $$ =0; }
;
opt_limit_clause_init:
/* empty */
{
LEX *lex= Lex;
SELECT_LEX *sel= lex->current_select;
if (sel->master_unit()->is_union() && !sel->braces)
{
/* Move LIMIT that belongs to UNION to fake_select_lex */
Lex->current_select= sel->master_unit()->fake_select_lex;
DBUG_ASSERT(Select);
}
sel= lex->current_select;
sel->offset_limit= 0;
sel->select_limit= 0;
lex->limit_rows_examined= 0;
}
| limit_clause {}
;
opt_limit_clause:
/* empty */ {}
| limit_clause {}
......@@ -11639,8 +11654,8 @@ choice:
| DEFAULT { $$= HA_CHOICE_UNDEF; }
;
procedure_clause:
/* empty */
opt_procedure_clause:
/* empty */ { $$= false; }
| PROCEDURE_SYM ident /* Procedure name */
{
LEX *lex=Lex;
......@@ -11669,6 +11684,9 @@ procedure_clause:
Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
}
'(' procedure_list ')'
{
$$= true;
}
;
procedure_list:
......@@ -11747,6 +11765,11 @@ select_outvar:
}
;
opt_into:
/* empty */ { $$= false; }
| into { $$= true; }
;
into:
INTO
{
......@@ -12241,7 +12264,7 @@ update:
*/
Select->set_lock_for_tables($3);
}
where_clause opt_order_clause delete_limit_clause {}
opt_where_clause opt_order_clause delete_limit_clause {}
;
update_list:
......@@ -12306,7 +12329,7 @@ single_multi:
YYPS->m_lock_type= TL_READ_DEFAULT;
YYPS->m_mdl_type= MDL_SHARED_READ;
}
where_clause opt_order_clause
opt_where_clause opt_order_clause
delete_limit_clause {}
opt_select_expressions {}
| table_wild_list
......@@ -12315,7 +12338,7 @@ single_multi:
YYPS->m_lock_type= TL_READ_DEFAULT;
YYPS->m_mdl_type= MDL_SHARED_READ;
}
FROM join_table_list where_clause
FROM join_table_list opt_where_clause
{
if (multi_delete_set_locks_and_link_aux_tables(Lex))
MYSQL_YYABORT;
......@@ -12326,7 +12349,7 @@ single_multi:
YYPS->m_lock_type= TL_READ_DEFAULT;
YYPS->m_mdl_type= MDL_SHARED_READ;
}
USING join_table_list where_clause
USING join_table_list opt_where_clause
{
if (multi_delete_set_locks_and_link_aux_tables(Lex))
MYSQL_YYABORT;
......@@ -12584,13 +12607,14 @@ show_param:
{
LEX *lex= Lex;
lex->sql_command= SQLCOM_SHOW_BINLOG_EVENTS;
} opt_limit_clause_init
}
opt_limit_clause
| RELAYLOG_SYM optional_connection_name EVENTS_SYM binlog_in binlog_from
{
LEX *lex= Lex;
lex->sql_command= SQLCOM_SHOW_RELAYLOG_EVENTS;
} opt_limit_clause_init
| keys_or_index from_or_in table_ident opt_db where_clause
} opt_limit_clause
| keys_or_index from_or_in table_ident opt_db opt_where_clause
{
LEX *lex= Lex;
lex->sql_command= SQLCOM_SHOW_KEYS;
......@@ -12625,13 +12649,13 @@ show_param:
{ (void) create_select_for_variable("warning_count"); }
| COUNT_SYM '(' '*' ')' ERRORS
{ (void) create_select_for_variable("error_count"); }
| WARNINGS opt_limit_clause_init
| WARNINGS opt_limit_clause
{ Lex->sql_command = SQLCOM_SHOW_WARNS;}
| ERRORS opt_limit_clause_init
| ERRORS opt_limit_clause
{ Lex->sql_command = SQLCOM_SHOW_ERRORS;}
| PROFILES_SYM
{ Lex->sql_command = SQLCOM_SHOW_PROFILES; }
| PROFILE_SYM opt_profile_defs opt_profile_args opt_limit_clause_init
| PROFILE_SYM opt_profile_defs opt_profile_args opt_limit_clause
{
LEX *lex= Lex;
lex->sql_command= SQLCOM_SHOW_PROFILE;
......@@ -15206,7 +15230,7 @@ handler:
if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
MYSQL_YYABORT;
}
handler_read_or_scan where_clause opt_limit_clause
handler_read_or_scan opt_where_clause opt_limit_clause
{
Lex->expr_allows_subselect= TRUE;
/* Stored functions are not supported for HANDLER READ. */
......@@ -15926,7 +15950,7 @@ union_order_or_limit:
;
order_or_limit:
order_clause opt_limit_clause_init
order_clause opt_limit_clause
| limit_clause
;
......@@ -15938,17 +15962,19 @@ union_option:
query_specification:
SELECT_SYM select_init2_derived
table_expression
{
$$= Lex->current_select->master_unit()->first_select();
}
| '(' select_paren_derived ')'
opt_union_order_or_limit
{
$$= Lex->current_select->master_unit()->first_select();
}
;
query_expression_body:
query_specification opt_union_order_or_limit
query_specification
| query_expression_body
UNION_SYM union_option
{
......@@ -15956,7 +15982,6 @@ query_expression_body:
MYSQL_YYABORT;
}
query_specification
opt_union_order_or_limit
{
Lex->pop_context();
$$= $1;
......
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