From 1f2ef5740360148961151a71603faae497919aa6 Mon Sep 17 00:00:00 2001 From: Igor Babaev Date: Thu, 6 Mar 2014 13:56:34 -0800 Subject: [PATCH] Fixed bug mdev-5686. The calls of the function remove_eq_conds() may change the and/or structure of the where conditions. So JOIN::equal_cond should be updated for non-recursive calls of remove_eq_conds(). --- mysql-test/r/subselect4.result | 22 ++++++++++++++++++++++ mysql-test/t/subselect4.test | 21 +++++++++++++++++++++ sql/sql_select.cc | 8 +++++++- 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/mysql-test/r/subselect4.result b/mysql-test/r/subselect4.result index 6c314c448cb..6ab62c801f7 100644 --- a/mysql-test/r/subselect4.result +++ b/mysql-test/r/subselect4.result @@ -2375,5 +2375,27 @@ ORDER BY v2.id; id a2 a3 id a2 a3 DROP VIEW v2; DROP TABLE t1,t2; +# +# MDEV-5686: degenerate disjunct in NOT IN subquery +# +CREATE TABLE t1 (a int, b int, c varchar(3)) ENGINE=MyISAM; +INSERT INTO t1 VALUES (1,1,'CAN'),(2,2,'AUS'); +CREATE TABLE t2 (f int) ENGINE=MyISAM; +INSERT INTO t2 VALUES (3); +EXPLAIN EXTENDED +SELECT * FROM t2 +WHERE f NOT IN (SELECT b FROM t1 +WHERE 0 OR (c IN ('USA') OR c NOT IN ('USA')) AND a = b); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY t2 system NULL NULL NULL NULL 1 100.00 +2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 100.00 Using where +Warnings: +Note 1003 select 3 AS `f` from `test`.`t2` where (not(<3>((3,(select `test`.`t1`.`b` from `test`.`t1` where (((`test`.`t1`.`c` = 'USA') or (`test`.`t1`.`c` <> 'USA')) and trigcond((((3) = `test`.`t1`.`b`) or isnull(`test`.`t1`.`b`))) and (`test`.`t1`.`b` = `test`.`t1`.`a`)) having trigcond((`test`.`t1`.`b`))))))) +SELECT * FROM t2 +WHERE f NOT IN (SELECT b FROM t1 +WHERE 0 OR (c IN ('USA') OR c NOT IN ('USA')) AND a = b); +f +3 +DROP TABLE t1,t2; SET optimizer_switch= @@global.optimizer_switch; set @@tmp_table_size= @@global.tmp_table_size; diff --git a/mysql-test/t/subselect4.test b/mysql-test/t/subselect4.test index 6aa1fd51859..0d029e1bf8f 100644 --- a/mysql-test/t/subselect4.test +++ b/mysql-test/t/subselect4.test @@ -1970,5 +1970,26 @@ ORDER BY v2.id; DROP VIEW v2; DROP TABLE t1,t2; +--echo # +--echo # MDEV-5686: degenerate disjunct in NOT IN subquery +--echo # + +CREATE TABLE t1 (a int, b int, c varchar(3)) ENGINE=MyISAM; +INSERT INTO t1 VALUES (1,1,'CAN'),(2,2,'AUS'); + +CREATE TABLE t2 (f int) ENGINE=MyISAM; +INSERT INTO t2 VALUES (3); + +EXPLAIN EXTENDED +SELECT * FROM t2 + WHERE f NOT IN (SELECT b FROM t1 + WHERE 0 OR (c IN ('USA') OR c NOT IN ('USA')) AND a = b); + +SELECT * FROM t2 + WHERE f NOT IN (SELECT b FROM t1 + WHERE 0 OR (c IN ('USA') OR c NOT IN ('USA')) AND a = b); + +DROP TABLE t1,t2; + SET optimizer_switch= @@global.optimizer_switch; set @@tmp_table_size= @@global.tmp_table_size; diff --git a/sql/sql_select.cc b/sql/sql_select.cc index c7f31a9c956..ec71cb3b1b2 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -3530,6 +3530,9 @@ make_join_statistics(JOIN *join, List &tables_list, { conds->update_used_tables(); conds= remove_eq_conds(join->thd, conds, &join->cond_value); + if (conds && conds->type() == Item::COND_ITEM && + ((Item_cond*) conds)->functype() == Item_func::COND_AND_FUNC) + join->cond_equal= &((Item_cond_and*) conds)->cond_equal; join->select_lex->where= conds; if (join->cond_value == Item::COND_FALSE) { @@ -13228,7 +13231,10 @@ optimize_cond(JOIN *join, COND *conds, Remove all and-levels where CONST item != CONST item */ DBUG_EXECUTE("where",print_where(conds,"after const change", QT_ORDINARY);); - conds= remove_eq_conds(thd, conds, cond_value) ; + conds= remove_eq_conds(thd, conds, cond_value); + if (conds && conds->type() == Item::COND_ITEM && + ((Item_cond*) conds)->functype() == Item_func::COND_AND_FUNC) + join->cond_equal= &((Item_cond_and*) conds)->cond_equal; DBUG_EXECUTE("info",print_where(conds,"after remove", QT_ORDINARY);); } DBUG_RETURN(conds);