Skip to content

Commit

Permalink
Fixed the bug mdev-12855.
Browse files Browse the repository at this point in the history
This is actually a legacy bug:
SQL_SELECT::test_quick_select() was called
with SQL_SELECT::head not set.
It looks like that this problem can be
reproduced only on queries with ORDER BY
that use IN predicates converted to semi-joins.
  • Loading branch information
igorbabaev committed Jun 8, 2017
1 parent 151f4e9 commit b850fc6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
22 changes: 22 additions & 0 deletions mysql-test/r/subselect_sj2_mat.result
Original file line number Diff line number Diff line change
Expand Up @@ -1579,3 +1579,25 @@ Warnings:
Note 1003 select `test`.`t1`.`i` AS `i` from `test`.`t1` semi join (`test`.`t2`) where ((rand() < 0))
drop table t1,t2;
set optimizer_switch=@save_optimizer_switch;
#
# mdev-12855: materialization of a semi-join subquery + ORDER BY
#
CREATE TABLE t1 (f1 varchar(8), KEY(f1)) ENGINE=InnoDB;
INSERT INTO t1 VALUES ('qux'),('foo');
CREATE TABLE t2 (f2 varchar(8)) ENGINE=InnoDB;
INSERT INTO t2 VALUES ('bar'),('foo'),('qux');
SELECT f1 FROM t1
WHERE f1 IN ( SELECT f2 FROM t2 WHERE f2 > 'bar' )
HAVING f1 != 'foo'
ORDER BY f1;
f1
qux
explain SELECT f1 FROM t1
WHERE f1 IN ( SELECT f2 FROM t2 WHERE f2 > 'bar' )
HAVING f1 != 'foo'
ORDER BY f1;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 index f1 f1 11 NULL 2 Using index
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 11 func 1
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 3 Using where
DROP TABLE t1,t2;
20 changes: 20 additions & 0 deletions mysql-test/t/subselect_sj2_mat.test
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,23 @@ select * from t1 where (rand() < 0) and i in (select i from t2);

drop table t1,t2;
set optimizer_switch=@save_optimizer_switch;

--echo #
--echo # mdev-12855: materialization of a semi-join subquery + ORDER BY
--echo #

CREATE TABLE t1 (f1 varchar(8), KEY(f1)) ENGINE=InnoDB;
INSERT INTO t1 VALUES ('qux'),('foo');
CREATE TABLE t2 (f2 varchar(8)) ENGINE=InnoDB;
INSERT INTO t2 VALUES ('bar'),('foo'),('qux');

let $q=
SELECT f1 FROM t1
WHERE f1 IN ( SELECT f2 FROM t2 WHERE f2 > 'bar' )
HAVING f1 != 'foo'
ORDER BY f1;

eval $q;
eval explain $q;

DROP TABLE t1,t2;
5 changes: 4 additions & 1 deletion sql/sql_select.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2759,8 +2759,11 @@ JOIN::exec()
if (sort_table_cond)
{
if (!curr_table->select)
{
if (!(curr_table->select= new SQL_SELECT))
DBUG_VOID_RETURN;
curr_table->select->head= curr_table->table;
}
if (!curr_table->select->cond)
curr_table->select->cond= sort_table_cond;
else
Expand Down Expand Up @@ -2846,7 +2849,7 @@ JOIN::exec()
curr_join->select_limit,
(select_options & OPTION_FOUND_ROWS ?
HA_POS_ERROR : unit->select_limit_cnt),
curr_join->group_list ? TRUE : FALSE))
curr_join->group_list ? FALSE : TRUE))
DBUG_VOID_RETURN;
sortorder= curr_join->sortorder;
if (curr_join->const_tables != curr_join->table_count &&
Expand Down

0 comments on commit b850fc6

Please sign in to comment.