summaryrefslogtreecommitdiff
path: root/mysql-test/main/brackets.test
diff options
context:
space:
mode:
authorIgor Babaev <igor@askmonty.org>2019-04-26 17:55:12 -0700
committerIgor Babaev <igor@askmonty.org>2019-04-26 17:56:26 -0700
commit5dee4a99d9bb30e5dfd5168384f8805047e403e4 (patch)
treeaaf962b1a1512586d377a59c2d9dfde628e99a2b /mysql-test/main/brackets.test
parentd1a43973ef2bb6f8e9de2196cbbfdd682d1c9139 (diff)
downloadmariadb-git-5dee4a99d9bb30e5dfd5168384f8805047e403e4.tar.gz
MDEV-19324 Wrong results from query, using brackets with ORDER BY ..LIMIT
If a select query was of the form (SELECT ... ORDER BY ...) LIMIT ... then in most cases it returned incorrect result. It happened because SELECT ... ORDER BY ... was wrapped into a select with materialized derived table: SELECT ... ORDER BY ... => SELECT * FROM (SELECT ... ORDER BY ...) dt. Yet for any materialized derived table ORDER BY without LIMIT is ignored. This patch resolves the problem by the conversion (SELECT ... ORDER BY ...) LIMIT ... => SELECT ... ORDER BY ... LIMIT ... at the parser stage. Similarly ((SELECT ... UNION ...) ORDER BY ...) LIMIT ... is converted to (SELECT ... UNION ...) ORDER BY ... LIMIT ... This conversion optimizes execution of the query because the result of (SELECT ... UNION ...) ORDER BY ... is not materialized into a temporary table anymore.
Diffstat (limited to 'mysql-test/main/brackets.test')
-rw-r--r--mysql-test/main/brackets.test33
1 files changed, 33 insertions, 0 deletions
diff --git a/mysql-test/main/brackets.test b/mysql-test/main/brackets.test
index 0eaa3bfc0a2..54f7d2714a1 100644
--- a/mysql-test/main/brackets.test
+++ b/mysql-test/main/brackets.test
@@ -106,5 +106,38 @@ INSERT INTO t2 VALUES (4),(5),(6),(7);
DROP TABLE t1,t2;
+--echo #
+--echo # MDEV-19324: ((SELECT ...) ORDER BY col ) LIMIT n
+--echo #
+
+create table t1 (a int);
+insert into t1 values (10),(20),(30);
+
+let $q1=
+select a from t1 order by a desc limit 1;
+eval $q1;
+eval explain extended $q1;
+eval explain format=json $q1;
+
+let $q2=
+(select a from t1 order by a desc) limit 1;
+eval $q2;
+eval explain extended $q2;
+eval explain format=json $q2;
+
+let $q1=
+(select a from t1 where a=20 union select a from t1) order by a desc limit 1;
+eval $q1;
+eval explain extended $q1;
+eval explain format=json $q1;
+
+let $q2=
+((select a from t1 where a=20 union select a from t1) order by a desc) limit 1;
+eval $q2;
+eval explain extended $q2;
+eval explain format=json $q2;
+
+drop table t1;
+
--echo # End of 10.4 tests