diff --git a/mysql-test/r/subselect_sj.result b/mysql-test/r/subselect_sj.result index 30a42b0c8e7..3d8a1f1d741 100644 --- a/mysql-test/r/subselect_sj.result +++ b/mysql-test/r/subselect_sj.result @@ -1218,4 +1218,31 @@ IN (SELECT t3.pk, t3.pk FROM t2 LEFT JOIN t3 ON t3.varchar_key) AND pk = 9; datetime_key DROP TABLE t1, t2, t3; +# +# BUG#784723: Wrong result with semijoin + nested subqueries in maria-5.3 +# +CREATE TABLE t1 ( t1field integer, primary key (t1field)); +CREATE TABLE t2 ( t2field integer, primary key (t2field)); +INSERT INTO t1 VALUES (1),(2),(3); +INSERT INTO t2 VALUES (2),(3),(4); +explain +SELECT * FROM t1 A +WHERE +A.t1field IN (SELECT A.t1field FROM t2 B) AND +A.t1field IN (SELECT C.t2field FROM t2 C +WHERE C.t2field IN (SELECT D.t2field FROM t2 D)); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY A index PRIMARY PRIMARY 4 NULL 3 Using index; Start temporary +1 PRIMARY B index NULL PRIMARY 4 NULL 3 Using index; End temporary; Using join buffer (flat, BNL join) +1 PRIMARY C eq_ref PRIMARY PRIMARY 4 test.A.t1field 1 Using index +1 PRIMARY D eq_ref PRIMARY PRIMARY 4 test.A.t1field 1 Using index +SELECT * FROM t1 A +WHERE +A.t1field IN (SELECT A.t1field FROM t2 B) AND +A.t1field IN (SELECT C.t2field FROM t2 C +WHERE C.t2field IN (SELECT D.t2field FROM t2 D)); +t1field +2 +3 +drop table t1,t2; set @@optimizer_switch=@save_optimizer_switch; diff --git a/mysql-test/r/subselect_sj2.result b/mysql-test/r/subselect_sj2.result index 064ec3f6dfc..325252493f3 100644 --- a/mysql-test/r/subselect_sj2.result +++ b/mysql-test/r/subselect_sj2.result @@ -303,9 +303,10 @@ t2.Code IN (SELECT Country FROM t3 WHERE Language='English' AND Percentage > 10 AND t2.Population > 100000); id select_type table type possible_keys key key_len ref rows Extra -1 PRIMARY t1 range Population,Country Population 4 NULL 1 Using index condition; Rowid-ordered scan -1 PRIMARY t3 eq_ref PRIMARY,Percentage PRIMARY 33 test.t1.Country,const 1 Using index condition; Using where +1 PRIMARY subselect2 ALL unique_key NULL NULL NULL 1 1 PRIMARY t2 eq_ref PRIMARY,Population PRIMARY 3 test.t1.Country 1 Using where +1 PRIMARY t3 eq_ref PRIMARY,Percentage PRIMARY 33 test.t1.Country,const 1 Using index condition; Using where +2 SUBQUERY t1 range Population,Country Population 4 NULL 1 Using index condition; Rowid-ordered scan DROP TABLE t1,t2,t3; CREATE TABLE t1 ( Code char(3) NOT NULL DEFAULT '', diff --git a/mysql-test/r/subselect_sj2_jcl6.result b/mysql-test/r/subselect_sj2_jcl6.result index 7b3b31b66fc..ce88a5aa44a 100644 --- a/mysql-test/r/subselect_sj2_jcl6.result +++ b/mysql-test/r/subselect_sj2_jcl6.result @@ -310,9 +310,10 @@ t2.Code IN (SELECT Country FROM t3 WHERE Language='English' AND Percentage > 10 AND t2.Population > 100000); id select_type table type possible_keys key key_len ref rows Extra -1 PRIMARY t1 range Population,Country Population 4 NULL 1 Using index condition; Rowid-ordered scan -1 PRIMARY t3 eq_ref PRIMARY,Percentage PRIMARY 33 test.t1.Country,const 1 Using index condition; Using where; Using join buffer (flat, BKA join); Key-ordered Rowid-ordered scan -1 PRIMARY t2 eq_ref PRIMARY,Population PRIMARY 3 test.t1.Country 1 Using where; Using join buffer (incremental, BKA join); Key-ordered Rowid-ordered scan +1 PRIMARY subselect2 ALL unique_key NULL NULL NULL 1 +1 PRIMARY t2 eq_ref PRIMARY,Population PRIMARY 3 test.t1.Country 1 Using where; Using join buffer (flat, BKA join); Key-ordered Rowid-ordered scan +1 PRIMARY t3 eq_ref PRIMARY,Percentage PRIMARY 33 test.t1.Country,const 1 Using index condition; Using where; Using join buffer (incremental, BKA join); Key-ordered Rowid-ordered scan +2 SUBQUERY t1 range Population,Country Population 4 NULL 1 Using index condition; Rowid-ordered scan DROP TABLE t1,t2,t3; CREATE TABLE t1 ( Code char(3) NOT NULL DEFAULT '', diff --git a/mysql-test/r/subselect_sj_jcl6.result b/mysql-test/r/subselect_sj_jcl6.result index 0b3e9c6984e..c804cfb8892 100644 --- a/mysql-test/r/subselect_sj_jcl6.result +++ b/mysql-test/r/subselect_sj_jcl6.result @@ -1226,6 +1226,33 @@ IN (SELECT t3.pk, t3.pk FROM t2 LEFT JOIN t3 ON t3.varchar_key) AND pk = 9; datetime_key DROP TABLE t1, t2, t3; +# +# BUG#784723: Wrong result with semijoin + nested subqueries in maria-5.3 +# +CREATE TABLE t1 ( t1field integer, primary key (t1field)); +CREATE TABLE t2 ( t2field integer, primary key (t2field)); +INSERT INTO t1 VALUES (1),(2),(3); +INSERT INTO t2 VALUES (2),(3),(4); +explain +SELECT * FROM t1 A +WHERE +A.t1field IN (SELECT A.t1field FROM t2 B) AND +A.t1field IN (SELECT C.t2field FROM t2 C +WHERE C.t2field IN (SELECT D.t2field FROM t2 D)); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY A index PRIMARY PRIMARY 4 NULL 3 Using index; Start temporary +1 PRIMARY B index NULL PRIMARY 4 NULL 3 Using index; End temporary; Using join buffer (flat, BNL join) +1 PRIMARY C eq_ref PRIMARY PRIMARY 4 test.A.t1field 1 Using index +1 PRIMARY D eq_ref PRIMARY PRIMARY 4 test.A.t1field 1 Using index +SELECT * FROM t1 A +WHERE +A.t1field IN (SELECT A.t1field FROM t2 B) AND +A.t1field IN (SELECT C.t2field FROM t2 C +WHERE C.t2field IN (SELECT D.t2field FROM t2 D)); +t1field +2 +3 +drop table t1,t2; set @@optimizer_switch=@save_optimizer_switch; # # BUG#49129: Wrong result with IN-subquery with join_cache_level=6 and firstmatch=off diff --git a/mysql-test/t/subselect_sj.test b/mysql-test/t/subselect_sj.test index 81f92b479e0..eeca05985b8 100644 --- a/mysql-test/t/subselect_sj.test +++ b/mysql-test/t/subselect_sj.test @@ -1117,6 +1117,25 @@ WHERE (int_nokey, pk) DROP TABLE t1, t2, t3; +--echo # +--echo # BUG#784723: Wrong result with semijoin + nested subqueries in maria-5.3 +--echo # +CREATE TABLE t1 ( t1field integer, primary key (t1field)); +CREATE TABLE t2 ( t2field integer, primary key (t2field)); +INSERT INTO t1 VALUES (1),(2),(3); +INSERT INTO t2 VALUES (2),(3),(4); +explain +SELECT * FROM t1 A +WHERE + A.t1field IN (SELECT A.t1field FROM t2 B) AND + A.t1field IN (SELECT C.t2field FROM t2 C + WHERE C.t2field IN (SELECT D.t2field FROM t2 D)); +SELECT * FROM t1 A +WHERE + A.t1field IN (SELECT A.t1field FROM t2 B) AND + A.t1field IN (SELECT C.t2field FROM t2 C + WHERE C.t2field IN (SELECT D.t2field FROM t2 D)); +drop table t1,t2; # The following command must be the last one the file set @@optimizer_switch=@save_optimizer_switch; diff --git a/sql/opt_subselect.cc b/sql/opt_subselect.cc index 505f336006b..dfb7e8abfa1 100644 --- a/sql/opt_subselect.cc +++ b/sql/opt_subselect.cc @@ -1551,6 +1551,7 @@ void advance_sj_state(JOIN *join, table_map remaining_tables, pos->prefix_record_count= *current_record_count; pos->sj_strategy= SJ_OPT_NONE; + pos->prefix_dups_producing_tables= join->cur_dups_producing_tables; /* Initialize the state or copy it from prev. tables */ if (idx == join->const_tables) { @@ -1983,7 +1984,6 @@ void advance_sj_state(JOIN *join, table_map remaining_tables, } } } - pos->prefix_dups_producing_tables= join->cur_dups_producing_tables; }