Commit bed4e847 authored by Oleksandr Byelkin's avatar Oleksandr Byelkin

MDEV-8380: Subquery parse error

backport mysql parser fixes
0034963fbf199696792491bcb79d5f0731c98804
5948561812bc691bd0c13cf518a3fe77d9daf920
parent d6371d3a
This diff is collapsed.
...@@ -5138,8 +5138,9 @@ SELECT a FROM t1 WHERE a = 2 ...@@ -5138,8 +5138,9 @@ SELECT a FROM t1 WHERE a = 2
a a
1 1
2 2
SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a); SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a) q;
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 a
1
SELECT * FROM (SELECT 1 a UNION (SELECT 1 a)) alias; SELECT * FROM (SELECT 1 a UNION (SELECT 1 a)) alias;
a a
1 1
...@@ -5181,7 +5182,7 @@ a 1 ...@@ -5181,7 +5182,7 @@ a 1
1 1 1 1
2 1 2 1
SELECT * FROM t1 JOIN ((SELECT 1 UNION SELECT 1)) ON 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; 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 ') ON 1' at line 1
SELECT * FROM t1 JOIN ((t1 t1a UNION SELECT 1)) ON 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)); ...@@ -5300,7 +5301,10 @@ SELECT ((SELECT 1 UNION SELECT 1 UNION SELECT 1));
SELECT * FROM ( 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 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 ); 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; SELECT * FROM ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ) a;
1 1
1 1
......
...@@ -5140,8 +5140,9 @@ SELECT a FROM t1 WHERE a = 2 ...@@ -5140,8 +5140,9 @@ SELECT a FROM t1 WHERE a = 2
a a
1 1
2 2
SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a); SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a) q;
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 a
1
SELECT * FROM (SELECT 1 a UNION (SELECT 1 a)) alias; SELECT * FROM (SELECT 1 a UNION (SELECT 1 a)) alias;
a a
1 1
...@@ -5183,7 +5184,7 @@ a 1 ...@@ -5183,7 +5184,7 @@ a 1
1 1 1 1
2 1 2 1
SELECT * FROM t1 JOIN ((SELECT 1 UNION SELECT 1)) ON 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; 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 ') ON 1' at line 1
SELECT * FROM t1 JOIN ((t1 t1a UNION SELECT 1)) ON 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)); ...@@ -5302,7 +5303,10 @@ SELECT ((SELECT 1 UNION SELECT 1 UNION SELECT 1));
SELECT * FROM ( 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 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 ); 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; SELECT * FROM ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ) a;
1 1
1 1
......
...@@ -5138,8 +5138,9 @@ SELECT a FROM t1 WHERE a = 2 ...@@ -5138,8 +5138,9 @@ SELECT a FROM t1 WHERE a = 2
a a
1 1
2 2
SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a); SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a) q;
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 a
1
SELECT * FROM (SELECT 1 a UNION (SELECT 1 a)) alias; SELECT * FROM (SELECT 1 a UNION (SELECT 1 a)) alias;
a a
1 1
...@@ -5181,7 +5182,7 @@ a 1 ...@@ -5181,7 +5182,7 @@ a 1
1 1 1 1
2 1 2 1
SELECT * FROM t1 JOIN ((SELECT 1 UNION SELECT 1)) ON 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; 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 ') ON 1' at line 1
SELECT * FROM t1 JOIN ((t1 t1a UNION SELECT 1)) ON 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)); ...@@ -5300,7 +5301,10 @@ SELECT ((SELECT 1 UNION SELECT 1 UNION SELECT 1));
SELECT * FROM ( 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 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 ); 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; SELECT * FROM ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ) a;
1 1
1 1
......
...@@ -5134,8 +5134,9 @@ SELECT a FROM t1 WHERE a = 2 ...@@ -5134,8 +5134,9 @@ SELECT a FROM t1 WHERE a = 2
a a
1 1
2 2
SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a); SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a) q;
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 a
1
SELECT * FROM (SELECT 1 a UNION (SELECT 1 a)) alias; SELECT * FROM (SELECT 1 a UNION (SELECT 1 a)) alias;
a a
1 1
...@@ -5177,7 +5178,7 @@ a 1 ...@@ -5177,7 +5178,7 @@ a 1
1 1 1 1
2 1 2 1
SELECT * FROM t1 JOIN ((SELECT 1 UNION SELECT 1)) ON 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; 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 ') ON 1' at line 1
SELECT * FROM t1 JOIN ((t1 t1a UNION SELECT 1)) ON 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)); ...@@ -5296,7 +5297,10 @@ SELECT ((SELECT 1 UNION SELECT 1 UNION SELECT 1));
SELECT * FROM ( 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 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 ); 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; SELECT * FROM ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ) a;
1 1
1 1
......
...@@ -5144,8 +5144,9 @@ SELECT a FROM t1 WHERE a = 2 ...@@ -5144,8 +5144,9 @@ SELECT a FROM t1 WHERE a = 2
a a
1 1
2 2
SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a); SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a) q;
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 a
1
SELECT * FROM (SELECT 1 a UNION (SELECT 1 a)) alias; SELECT * FROM (SELECT 1 a UNION (SELECT 1 a)) alias;
a a
1 1
...@@ -5187,7 +5188,7 @@ a 1 ...@@ -5187,7 +5188,7 @@ a 1
1 1 1 1
2 1 2 1
SELECT * FROM t1 JOIN ((SELECT 1 UNION SELECT 1)) ON 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; 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 ') ON 1' at line 1
SELECT * FROM t1 JOIN ((t1 t1a UNION SELECT 1)) ON 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)); ...@@ -5306,7 +5307,10 @@ SELECT ((SELECT 1 UNION SELECT 1 UNION SELECT 1));
SELECT * FROM ( 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 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 ); 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; SELECT * FROM ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ) a;
1 1
1 1
......
...@@ -5134,8 +5134,9 @@ SELECT a FROM t1 WHERE a = 2 ...@@ -5134,8 +5134,9 @@ SELECT a FROM t1 WHERE a = 2
a a
1 1
2 2
SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a); SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a) q;
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 a
1
SELECT * FROM (SELECT 1 a UNION (SELECT 1 a)) alias; SELECT * FROM (SELECT 1 a UNION (SELECT 1 a)) alias;
a a
1 1
...@@ -5177,7 +5178,7 @@ a 1 ...@@ -5177,7 +5178,7 @@ a 1
1 1 1 1
2 1 2 1
SELECT * FROM t1 JOIN ((SELECT 1 UNION SELECT 1)) ON 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; 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 ') ON 1' at line 1
SELECT * FROM t1 JOIN ((t1 t1a UNION SELECT 1)) ON 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)); ...@@ -5296,7 +5297,10 @@ SELECT ((SELECT 1 UNION SELECT 1 UNION SELECT 1));
SELECT * FROM ( 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 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 ); 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; SELECT * FROM ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ) a;
1 1
1 1
......
...@@ -89,6 +89,10 @@ id select_type table type possible_keys key key_len ref rows filtered Extra ...@@ -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 NULL UNION RESULT <union1,2> ALL NULL NULL NULL NULL NULL NULL Using filesort
Warnings: 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 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; (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 a b
1 a 1 a
...@@ -96,6 +100,10 @@ a b ...@@ -96,6 +100,10 @@ a b
select found_rows(); select found_rows();
found_rows() found_rows()
6 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; select sql_calc_found_rows a,b from t1 union all select a,b from t2 limit 2;
a b a b
1 a 1 a
...@@ -308,12 +316,20 @@ create table t1 (a int); ...@@ -308,12 +316,20 @@ create table t1 (a int);
insert into t1 values (1),(2),(3); insert into t1 values (1),(2),(3);
create table t2 (a int); create table t2 (a int);
insert into t2 values (3),(4),(5); 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; (SELECT SQL_CALC_FOUND_ROWS * FROM t1) UNION all (SELECT * FROM t2) LIMIT 1;
a a
1 1
select found_rows(); select found_rows();
found_rows() found_rows()
6 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; (SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1) UNION all (SELECT * FROM t2) LIMIT 2;
a a
1 1
...@@ -321,6 +337,10 @@ a ...@@ -321,6 +337,10 @@ a
select found_rows(); select found_rows();
found_rows() found_rows()
4 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); (SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1) UNION all (SELECT * FROM t2);
a a
1 1
...@@ -330,6 +350,10 @@ a ...@@ -330,6 +350,10 @@ a
select found_rows(); select found_rows();
found_rows() found_rows()
4 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); (SELECT SQL_CALC_FOUND_ROWS * FROM t1) UNION all (SELECT * FROM t2 LIMIT 1);
a a
1 1
...@@ -345,6 +369,16 @@ a ...@@ -345,6 +369,16 @@ a
select found_rows(); select found_rows();
found_rows() found_rows()
4 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; SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1 UNION all SELECT * FROM t2 LIMIT 2;
ERROR HY000: Incorrect usage of UNION and LIMIT ERROR HY000: Incorrect usage of UNION and LIMIT
SELECT SQL_CALC_FOUND_ROWS * FROM t1 UNION all SELECT * FROM t2 LIMIT 2; SELECT SQL_CALC_FOUND_ROWS * FROM t1 UNION all SELECT * FROM t2 LIMIT 2;
...@@ -354,13 +388,29 @@ a ...@@ -354,13 +388,29 @@ a
select found_rows(); select found_rows();
found_rows() found_rows()
6 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; SELECT SQL_CALC_FOUND_ROWS * FROM t1 UNION SELECT * FROM t2 LIMIT 2;
a a
1 1
2 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(); select found_rows();
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; SELECT SQL_CALC_FOUND_ROWS * FROM t1 UNION SELECT * FROM t2 LIMIT 100;
a a
1 1
...@@ -373,10 +423,39 @@ found_rows() ...@@ -373,10 +423,39 @@ found_rows()
5 5
SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 100 UNION SELECT * FROM t2; SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 100 UNION SELECT * FROM t2;
ERROR HY000: Incorrect usage of UNION and LIMIT 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; SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1 UNION SELECT * FROM t2;
ERROR HY000: Incorrect usage of UNION and LIMIT 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; SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1 UNION SELECT * FROM t2 LIMIT 2;
ERROR HY000: Incorrect usage of UNION and LIMIT 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; SELECT SQL_CALC_FOUND_ROWS * FROM t1 UNION SELECT * FROM t2 LIMIT 2,2;
a a
3 3
...@@ -384,8 +463,21 @@ a ...@@ -384,8 +463,21 @@ a
select found_rows(); select found_rows();
found_rows() found_rows()
5 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; SELECT SQL_CALC_FOUND_ROWS * FROM t1 limit 2,2 UNION SELECT * FROM t2;
ERROR HY000: Incorrect usage of UNION and LIMIT 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; SELECT * FROM t1 UNION SELECT * FROM t2 ORDER BY a desc LIMIT 1;
a a
5 5
......
...@@ -765,3 +765,259 @@ DROP TABLE t1; ...@@ -765,3 +765,259 @@ DROP TABLE t1;
CREATE TABLE t1(a INT); 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; 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; 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 ( ...@@ -4274,8 +4274,7 @@ SELECT * FROM (
# This was not allowed previously. Possibly, it should be allowed on the future. # 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. # 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) q;
SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a);
SELECT * FROM (SELECT 1 a UNION (SELECT 1 a)) alias; SELECT * FROM (SELECT 1 a UNION (SELECT 1 a)) alias;
SELECT * FROM (SELECT 1 UNION SELECT 1) t1a; SELECT * FROM (SELECT 1 UNION SELECT 1) t1a;
--error ER_PARSE_ERROR --error ER_PARSE_ERROR
...@@ -4310,7 +4309,7 @@ SELECT * FROM (SELECT 1 a UNION SELECT 1 a ORDER BY a LIMIT 1) t1a; ...@@ -4310,7 +4309,7 @@ SELECT * FROM (SELECT 1 a UNION SELECT 1 a ORDER BY a LIMIT 1) t1a;
# aliases after. # aliases after.
# #
SELECT * FROM t1 JOIN (SELECT 1 UNION SELECT 1) alias ON 1; 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; SELECT * FROM t1 JOIN ((SELECT 1 UNION SELECT 1)) ON 1;
--error ER_PARSE_ERROR --error ER_PARSE_ERROR
SELECT * FROM t1 JOIN (t1 t1a UNION SELECT 1) ON 1; SELECT * FROM t1 JOIN (t1 t1a UNION SELECT 1) ON 1;
...@@ -4402,8 +4401,9 @@ SELECT ((SELECT 1 UNION SELECT 1 UNION SELECT 1)); ...@@ -4402,8 +4401,9 @@ SELECT ((SELECT 1 UNION SELECT 1 UNION SELECT 1));
--error ER_PARSE_ERROR --error ER_PARSE_ERROR
SELECT * FROM ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) ); 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 );
SELECT * FROM ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 ) a;
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 --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 ...@@ -26,8 +26,12 @@ select 't1',b,count(*) from t1 group by b UNION select 't2',b,count(*) from t2 g
--error 1250 --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; (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; 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 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 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 sql_calc_found_rows a,b from t1 union all select a,b from t2 limit 2;
select found_rows(); select found_rows();
...@@ -206,18 +210,30 @@ create table t2 (a int); ...@@ -206,18 +210,30 @@ create table t2 (a int);
insert into t2 values (3),(4),(5); insert into t2 values (3),(4),(5);
# Test global limits # 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 SQL_CALC_FOUND_ROWS * FROM t1) UNION all (SELECT * FROM t2) LIMIT 1;
select found_rows(); 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 SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1) UNION all (SELECT * FROM t2) LIMIT 2;
select found_rows(); select found_rows();
# Test cases where found_rows() should return number of returned 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 SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1) UNION all (SELECT * FROM t2);
select found_rows(); 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 SQL_CALC_FOUND_ROWS * FROM t1) UNION all (SELECT * FROM t2 LIMIT 1);
select found_rows(); select found_rows();
(SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1) UNION SELECT * FROM t2 LIMIT 1; (SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1) UNION SELECT * FROM t2 LIMIT 1;
select found_rows(); 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 # In these case found_rows() should work
--error ER_WRONG_USAGE --error ER_WRONG_USAGE
...@@ -226,20 +242,41 @@ SELECT SQL_CALC_FOUND_ROWS * FROM t1 UNION all SELECT * FROM t2 LIMIT 2; ...@@ -226,20 +242,41 @@ SELECT SQL_CALC_FOUND_ROWS * FROM t1 UNION all SELECT * FROM t2 LIMIT 2;
select found_rows(); select found_rows();
# The following examples will not be exact # 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 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 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 SQL_CALC_FOUND_ROWS * FROM t1 UNION SELECT * FROM t2 LIMIT 100;
select found_rows(); select found_rows();
--error ER_WRONG_USAGE --error ER_WRONG_USAGE
SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 100 UNION SELECT * FROM t2; 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 --error ER_WRONG_USAGE
SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1 UNION SELECT * FROM t2; 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 --error ER_WRONG_USAGE
SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1 UNION SELECT * FROM t2 LIMIT 2; 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 SQL_CALC_FOUND_ROWS * FROM t1 UNION SELECT * FROM t2 LIMIT 2,2;
select found_rows(); select found_rows();
SELECT COUNT(*) FROM (
SELECT * FROM t1 UNION SELECT * FROM t2) q;
--error ER_WRONG_USAGE --error ER_WRONG_USAGE
SELECT SQL_CALC_FOUND_ROWS * FROM t1 limit 2,2 UNION SELECT * FROM t2; 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 # Test some limits with ORDER BY
SELECT * FROM t1 UNION SELECT * FROM t2 ORDER BY a desc LIMIT 1; SELECT * FROM t1 UNION SELECT * FROM t2 ORDER BY a desc LIMIT 1;
......
...@@ -546,6 +546,7 @@ void lex_start(THD *thd) ...@@ -546,6 +546,7 @@ void lex_start(THD *thd)
lex->limit_rows_examined_cnt= ULONGLONG_MAX; lex->limit_rows_examined_cnt= ULONGLONG_MAX;
lex->var_list.empty(); lex->var_list.empty();
lex->stmt_var_list.empty(); lex->stmt_var_list.empty();
lex->proc_list.elements=0;
lex->is_lex_started= TRUE; lex->is_lex_started= TRUE;
DBUG_VOID_RETURN; DBUG_VOID_RETURN;
......
...@@ -6959,11 +6959,28 @@ mysql_new_select(LEX *lex, bool move_down) ...@@ -6959,11 +6959,28 @@ mysql_new_select(LEX *lex, bool move_down)
} }
else 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) if (lex->current_select->order_list.first && !lex->current_select->braces)
{ {
my_error(ER_WRONG_USAGE, MYF(0), "UNION", "ORDER BY"); my_error(ER_WRONG_USAGE, MYF(0), "UNION", "ORDER BY");
DBUG_RETURN(1); 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->include_neighbour(lex->current_select);
SELECT_LEX_UNIT *unit= select_lex->master_unit(); SELECT_LEX_UNIT *unit= select_lex->master_unit();
if (!unit->fake_select_lex && unit->add_fake_select_lex(lex->thd)) if (!unit->fake_select_lex && unit->add_fake_select_lex(lex->thd))
......
This diff is collapsed.
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