From 0a2a470b08c508481da0ac60b50921b1f03a85fb Mon Sep 17 00:00:00 2001 From: Magne Mahre Date: Mon, 10 Jan 2011 13:43:12 +0100 Subject: [PATCH] Bug#58970 Problem Subquery (without referencing a table) and Order By When having a UNION statement in a subquery, with no referenced tables (or only a reference to the virtual table 'dual'), the UNION did not allow an ORDER BY clause. i.e: SELECT(SELECT 1 AS a UNION SELECT 0 AS a ORDER BY a) AS b or SELECT(SELECT 1 AS a FROM dual UNION SELECT 0 as a ORDER BY a) AS b In addition, an ORDER BY / LIMIT clause was not accepted in subqueries even for single SELECT statements with no referenced tables (or with 'dual' as table reference) i.e: SELECT(SELECT 1 AS a ORDER BY a) AS b or SELECT(SELECT 1 AS a FROM dual ORDER BY a) AS b The fix was to allow an optional ORDER BY/LIMIT clause to the grammar for these cases. See also: Bug#57986 --- mysql-test/r/union.result | 25 +++++++++++++++++++++++++ mysql-test/t/union.test | 13 +++++++++++++ sql/sql_yacc.yy | 3 ++- 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/mysql-test/r/union.result b/mysql-test/r/union.result index 7f0ec2275d7..1f3422c1767 100644 --- a/mysql-test/r/union.result +++ b/mysql-test/r/union.result @@ -1744,3 +1744,28 @@ ORDER BY c2 DESC, c1 ASC LIMIT 1 c1 c2 t1a 3 DROP TABLE t1, t2; +# +# Bug #58970 Problem Subquery (without referencing a table) +# and Order By +# +SELECT(SELECT 0 AS a UNION SELECT 1 AS a ORDER BY a ASC LIMIT 1) AS dev; +dev +0 +SELECT(SELECT 0 AS a UNION SELECT 1 AS a ORDER BY a DESC LIMIT 1) AS dev; +dev +1 +SELECT(SELECT 0 AS a FROM dual UNION SELECT 1 AS a FROM dual ORDER BY a ASC LIMIT 1) AS dev; +dev +0 +SELECT(SELECT 0 AS a FROM dual UNION SELECT 1 AS a FROM dual ORDER BY a DESC LIMIT 1) AS dev; +dev +1 +SELECT(SELECT 1 AS a ORDER BY a) AS dev; +dev +1 +SELECT(SELECT 1 AS a LIMIT 1) AS dev; +dev +1 +SELECT(SELECT 1 AS a FROM dual ORDER BY a DESC LIMIT 1) AS dev; +dev +1 diff --git a/mysql-test/t/union.test b/mysql-test/t/union.test index 694f1bab15c..c6599517e90 100644 --- a/mysql-test/t/union.test +++ b/mysql-test/t/union.test @@ -1171,3 +1171,16 @@ SELECT c1, c2 FROM ( ) AS res; DROP TABLE t1, t2; + +--echo # +--echo # Bug #58970 Problem Subquery (without referencing a table) +--echo # and Order By +--echo # + +SELECT(SELECT 0 AS a UNION SELECT 1 AS a ORDER BY a ASC LIMIT 1) AS dev; +SELECT(SELECT 0 AS a UNION SELECT 1 AS a ORDER BY a DESC LIMIT 1) AS dev; +SELECT(SELECT 0 AS a FROM dual UNION SELECT 1 AS a FROM dual ORDER BY a ASC LIMIT 1) AS dev; +SELECT(SELECT 0 AS a FROM dual UNION SELECT 1 AS a FROM dual ORDER BY a DESC LIMIT 1) AS dev; +SELECT(SELECT 1 AS a ORDER BY a) AS dev; +SELECT(SELECT 1 AS a LIMIT 1) AS dev; +SELECT(SELECT 1 AS a FROM dual ORDER BY a DESC LIMIT 1) AS dev; diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index c283747156a..397afd26d8d 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -13914,7 +13914,7 @@ query_specification: ; query_expression_body: - query_specification + query_specification opt_union_order_or_limit | query_expression_body UNION_SYM union_option { @@ -13922,6 +13922,7 @@ query_expression_body: MYSQL_YYABORT; } query_specification + opt_union_order_or_limit { Lex->pop_context(); $$= $1;