mirror of
https://github.com/MariaDB/server.git
synced 2025-07-30 16:24:05 +03:00
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.
This commit is contained in:
@ -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
|
||||
|
||||
|
Reference in New Issue
Block a user