diff --git a/mysql-test/main/brackets.result b/mysql-test/main/brackets.result index 3cf3468320f..869afe56c9c 100644 --- a/mysql-test/main/brackets.result +++ b/mysql-test/main/brackets.result @@ -393,4 +393,63 @@ EXPLAIN } } drop table t1; +# +# MDEV-19363: ((SELECT ...) ORDER BY col ) LIMIT n UNION ... +# +create table t1 (pk int); +insert into t1 values (5),(4),(1),(2),(3); +((select * from t1 order by pk) limit 2) union (select * from t1 where pk > 4); +pk +1 +2 +5 +explain extended ((select * from t1 order by pk) limit 2) union (select * from t1 where pk > 4); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 5 100.00 Using filesort +2 UNION t1 ALL NULL NULL NULL NULL 5 100.00 Using where +NULL UNION RESULT ALL NULL NULL NULL NULL NULL NULL +Warnings: +Note 1003 (/* select#1 */ select `test`.`t1`.`pk` AS `pk` from `test`.`t1` order by `test`.`t1`.`pk` limit 2) union (/* select#2 */ select `test`.`t1`.`pk` AS `pk` from `test`.`t1` where `test`.`t1`.`pk` > 4) +explain format=json ((select * from t1 order by pk) limit 2) union (select * from t1 where pk > 4); +EXPLAIN +{ + "query_block": { + "union_result": { + "table_name": "", + "access_type": "ALL", + "query_specifications": [ + { + "query_block": { + "select_id": 1, + "read_sorted_file": { + "filesort": { + "sort_key": "t1.pk", + "table": { + "table_name": "t1", + "access_type": "ALL", + "rows": 5, + "filtered": 100 + } + } + } + } + }, + { + "query_block": { + "select_id": 2, + "operation": "UNION", + "table": { + "table_name": "t1", + "access_type": "ALL", + "rows": 5, + "filtered": 100, + "attached_condition": "t1.pk > 4" + } + } + } + ] + } + } +} +drop table t1; # End of 10.4 tests diff --git a/mysql-test/main/brackets.test b/mysql-test/main/brackets.test index 54f7d2714a1..cf1dcc56acc 100644 --- a/mysql-test/main/brackets.test +++ b/mysql-test/main/brackets.test @@ -139,5 +139,20 @@ eval explain format=json $q2; drop table t1; +--echo # +--echo # MDEV-19363: ((SELECT ...) ORDER BY col ) LIMIT n UNION ... +--echo # + +create table t1 (pk int); +insert into t1 values (5),(4),(1),(2),(3); + +let $q= +((select * from t1 order by pk) limit 2) union (select * from t1 where pk > 4); +eval $q; +eval explain extended $q; +eval explain format=json $q; + +drop table t1; + --echo # End of 10.4 tests diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index 66ac69fcc46..e1a6420846d 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -9147,13 +9147,15 @@ SELECT_LEX *LEX::parsed_select(SELECT_LEX *sel, Lex_order_limit_lock * l) } else { - SELECT_LEX_UNIT *unit= create_unit(sel); - if (!unit) - return NULL; if (!l->order_list && !sel->explicit_limit) l->order_list= &sel->order_list; else + { + SELECT_LEX_UNIT *unit= create_unit(sel); + if (!unit) + return NULL; sel= wrap_unit_into_derived(unit); + } if (!sel) return NULL; l->set_to(sel);