diff --git a/mysql-test/r/subselect.result b/mysql-test/r/subselect.result index 7f6ebb14c3e..7fbaad3133a 100644 --- a/mysql-test/r/subselect.result +++ b/mysql-test/r/subselect.result @@ -4361,6 +4361,28 @@ id select_type table type possible_keys key key_len ref rows filtered Extra Warnings: Note 1003 select 1 AS `1` from `test`.`t1` where (1,(select 1 AS `1` from `test`.`t1` where (`test`.`t1`.`a` > 3) group by `test`.`t1`.`a` having ((1) = (1)))) DROP TABLE t1; +# +# Bug#45061: Incorrectly market field caused wrong result. +# +CREATE TABLE `C` ( +`int_nokey` int(11) NOT NULL, +`int_key` int(11) NOT NULL, +KEY `int_key` (`int_key`) +); +INSERT INTO `C` VALUES (9,9), (0,0), (8,6), (3,6), (7,6), (0,4), +(1,7), (9,4), (0,8), (9,4), (0,7), (5,5), (0,0), (8,5), (8,7), +(5,2), (1,8), (7,0), (0,9), (9,5); +SELECT * FROM C WHERE `int_key` IN (SELECT `int_nokey`); +int_nokey int_key +9 9 +0 0 +5 5 +0 0 +EXPLAIN EXTENDED SELECT * FROM C WHERE `int_key` IN (SELECT `int_nokey`); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 PRIMARY C ALL NULL NULL NULL NULL 20 100.00 Using where +DROP TABLE C; +# End of test for bug#45061. End of 5.0 tests. CREATE TABLE t1 (a INT, b INT); INSERT INTO t1 VALUES (2,22),(1,11),(2,22); diff --git a/mysql-test/t/subselect.test b/mysql-test/t/subselect.test index e82764d6ac4..3504e9e3733 100644 --- a/mysql-test/t/subselect.test +++ b/mysql-test/t/subselect.test @@ -3315,6 +3315,27 @@ EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1 IN (SELECT 1 FROM t1 GROUP BY a); EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1 IN (SELECT 1 FROM t1 WHERE a > 3 GROUP BY a); DROP TABLE t1; +--echo # +--echo # Bug#45061: Incorrectly market field caused wrong result. +--echo # +CREATE TABLE `C` ( + `int_nokey` int(11) NOT NULL, + `int_key` int(11) NOT NULL, + KEY `int_key` (`int_key`) +); + +INSERT INTO `C` VALUES (9,9), (0,0), (8,6), (3,6), (7,6), (0,4), +(1,7), (9,4), (0,8), (9,4), (0,7), (5,5), (0,0), (8,5), (8,7), +(5,2), (1,8), (7,0), (0,9), (9,5); + +--disable_warnings +SELECT * FROM C WHERE `int_key` IN (SELECT `int_nokey`); +EXPLAIN EXTENDED SELECT * FROM C WHERE `int_key` IN (SELECT `int_nokey`); +--enable_warnings + +DROP TABLE C; +--echo # End of test for bug#45061. + --echo End of 5.0 tests. # diff --git a/sql/item.cc b/sql/item.cc index 4d9004fff26..2640b74851b 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -599,6 +599,7 @@ bool Item_ident::remove_dependence_processor(uchar * arg) DBUG_ENTER("Item_ident::remove_dependence_processor"); if (depended_from == (st_select_lex *) arg) depended_from= 0; + context= &((st_select_lex *) arg)->context; DBUG_RETURN(0); } diff --git a/sql/item_subselect.cc b/sql/item_subselect.cc index 00c09679737..cdb091fa07e 100644 --- a/sql/item_subselect.cc +++ b/sql/item_subselect.cc @@ -1227,6 +1227,10 @@ Item_in_subselect::single_value_transformer(JOIN *join, else { // it is single select without tables => possible optimization + // remove the dependence mark since the item is moved to upper + // select and is not outer anymore. + item->walk(&Item::remove_dependence_processor, 0, + (uchar *) select_lex->outer_select()); item= func->create(left_expr, item); // fix_field of item will be done in time of substituting substitution= item;