Skip to content

Commit ab578bd

Browse files
author
Varun Gupta
committed
MDEV-9513: Assertion `join->group_list || !join->is_in_subquery()' failed in create_sort_index
Removing the ORDER BY clause from the UNION when UNION is inside an IN/ALL/ANY/EXISTS subquery. The rewrites are done for subqueries but this rewrite is not done for the fake_select of the UNION.
1 parent 0e80f5a commit ab578bd

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

mysql-test/r/subselect4.result

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2608,3 +2608,44 @@ region area population
26082608
Central America and the Caribbean 442 66422
26092609
SET @@optimizer_switch= @save_optimizer_switch;
26102610
DROP TABLE t1;
2611+
#
2612+
# MDEV-9513: Assertion `join->group_list || !join->is_in_subquery()' failed in create_sort_index
2613+
#
2614+
CREATE TABLE t1 (a INT);
2615+
INSERT INTO t1 VALUES (1),(2);
2616+
CREATE TABLE t2 (a INT);
2617+
INSERT INTO t2 VALUES (2),(3);
2618+
EXPLAIN
2619+
SELECT t1.a FROM t1 WHERE t1.a IN ( SELECT A.a FROM t1 A UNION SELECT B.a FROM t2 B ORDER BY 1);
2620+
id select_type table type possible_keys key key_len ref rows Extra
2621+
1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where
2622+
2 DEPENDENT SUBQUERY A ALL NULL NULL NULL NULL 2 Using where
2623+
3 DEPENDENT UNION B ALL NULL NULL NULL NULL 2 Using where
2624+
NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
2625+
SELECT t1.a FROM t1 WHERE t1.a IN ( SELECT A.a FROM t1 A UNION SELECT B.a FROM t2 B ORDER BY 1);
2626+
a
2627+
1
2628+
2
2629+
EXPLAIN
2630+
SELECT t1.a FROM t1 WHERE EXISTS (SELECT A.a FROM t1 A UNION SELECT B.a FROM t2 B ORDER BY 1);
2631+
id select_type table type possible_keys key key_len ref rows Extra
2632+
1 PRIMARY t1 ALL NULL NULL NULL NULL 2
2633+
2 SUBQUERY A ALL NULL NULL NULL NULL 2
2634+
3 UNION B ALL NULL NULL NULL NULL 2
2635+
NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
2636+
SELECT t1.a FROM t1 WHERE EXISTS (SELECT A.a FROM t1 A UNION SELECT B.a FROM t2 B ORDER BY 1);
2637+
a
2638+
1
2639+
2
2640+
EXPLAIN
2641+
SELECT t1.a FROM t1 WHERE t1.a IN ( SELECT A.a FROM t1 A UNION ALL SELECT B.a FROM t2 B ORDER BY 1);
2642+
id select_type table type possible_keys key key_len ref rows Extra
2643+
1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where
2644+
2 DEPENDENT SUBQUERY A ALL NULL NULL NULL NULL 2 Using where
2645+
3 DEPENDENT UNION B ALL NULL NULL NULL NULL 2 Using where
2646+
SELECT t1.a FROM t1 WHERE t1.a IN ( SELECT A.a FROM t1 A UNION ALL SELECT B.a FROM t2 B ORDER BY 1);
2647+
a
2648+
1
2649+
2
2650+
DROP TABLE t1,t2;
2651+
# end of 10.1 tests

mysql-test/t/subselect4.test

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2138,3 +2138,28 @@ WHERE population/area = (SELECT MAX(population/area) from t1 B where A.region =
21382138
SET @@optimizer_switch= @save_optimizer_switch;
21392139

21402140
DROP TABLE t1;
2141+
2142+
--echo #
2143+
--echo # MDEV-9513: Assertion `join->group_list || !join->is_in_subquery()' failed in create_sort_index
2144+
--echo #
2145+
2146+
CREATE TABLE t1 (a INT);
2147+
INSERT INTO t1 VALUES (1),(2);
2148+
2149+
CREATE TABLE t2 (a INT);
2150+
INSERT INTO t2 VALUES (2),(3);
2151+
EXPLAIN
2152+
SELECT t1.a FROM t1 WHERE t1.a IN ( SELECT A.a FROM t1 A UNION SELECT B.a FROM t2 B ORDER BY 1);
2153+
SELECT t1.a FROM t1 WHERE t1.a IN ( SELECT A.a FROM t1 A UNION SELECT B.a FROM t2 B ORDER BY 1);
2154+
2155+
EXPLAIN
2156+
SELECT t1.a FROM t1 WHERE EXISTS (SELECT A.a FROM t1 A UNION SELECT B.a FROM t2 B ORDER BY 1);
2157+
SELECT t1.a FROM t1 WHERE EXISTS (SELECT A.a FROM t1 A UNION SELECT B.a FROM t2 B ORDER BY 1);
2158+
2159+
EXPLAIN
2160+
SELECT t1.a FROM t1 WHERE t1.a IN ( SELECT A.a FROM t1 A UNION ALL SELECT B.a FROM t2 B ORDER BY 1);
2161+
SELECT t1.a FROM t1 WHERE t1.a IN ( SELECT A.a FROM t1 A UNION ALL SELECT B.a FROM t2 B ORDER BY 1);
2162+
2163+
DROP TABLE t1,t2;
2164+
2165+
--echo # end of 10.1 tests

sql/item_subselect.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ class Item_subselect :public Item_result_field,
131131
Item_subselect(THD *thd);
132132

133133
virtual subs_type substype() { return UNKNOWN_SUBS; }
134+
bool is_exists_predicate()
135+
{
136+
return substype() == Item_subselect::EXISTS_SUBS;
137+
}
134138
bool is_in_predicate()
135139
{
136140
return (substype() == Item_subselect::IN_SUBS ||

sql/sql_union.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,25 @@ bool st_select_lex_unit::prepare(THD *thd_arg, select_result *sel_result,
388388
found_rows_for_union= first_sl->options & OPTION_FOUND_ROWS;
389389
is_union_select= is_union() || fake_select_lex;
390390

391+
/*
392+
If we are reading UNION output and the UNION is in the
393+
IN/ANY/ALL/EXISTS subquery, then ORDER BY is redundant and hence should
394+
be removed.
395+
Example:
396+
select ... col IN (select col2 FROM t1 union select col3 from t2 ORDER BY 1)
397+
398+
(as for ORDER BY ... LIMIT, it currently not supported inside
399+
IN/ALL/ANY subqueries)
400+
(For non-UNION this removal of ORDER BY clause is done in
401+
check_and_do_in_subquery_rewrites())
402+
*/
403+
if (is_union() && item &&
404+
(item->is_in_predicate() || item->is_exists_predicate()))
405+
{
406+
global_parameters()->order_list.first= NULL;
407+
global_parameters()->order_list.elements= 0;
408+
}
409+
391410
/* Global option */
392411

393412
if (is_union_select)

0 commit comments

Comments
 (0)