From c05e5b9c65f76ba2d3a6844add88c03076b2cb5d Mon Sep 17 00:00:00 2001 From: Igor Babaev Date: Wed, 16 Nov 2011 06:11:25 -0800 Subject: [PATCH] Fixed LP bug #887479. The function setup_semijoin_dups_elimination erroneously assumed that if join_cache_level is set to 3 or 4 then the type of the access to a table cannot be JT_REF or JT_EQ_REF. This could lead to wrong query result sets. --- mysql-test/r/join_cache.result | 33 +++++++++++++++++++++++++++++++++ mysql-test/t/join_cache.test | 29 +++++++++++++++++++++++++++++ sql/opt_subselect.cc | 2 +- 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/mysql-test/r/join_cache.result b/mysql-test/r/join_cache.result index 1f9ef1dfeda..af3d238c1fd 100644 --- a/mysql-test/r/join_cache.result +++ b/mysql-test/r/join_cache.result @@ -5146,4 +5146,37 @@ a 0 SET SESSION join_cache_level = DEFAULT; DROP TABLE t1, t2; +# +# Bug #887479: join_cache_level=3 + semijoin=on +# +CREATE TABLE t1 (a int, b int); +INSERT INTO t1 VALUES (3914,17), (3710,5), (3888,20); +CREATE TABLE t2 (c int, KEY (c)); +INSERT INTO t2 VALUES (27), (17), (33), (20), (3), (7), (18), (2); +SET @tmp887479_optimizer_switch=@@optimizer_switch; +SET SESSION optimizer_switch='semijoin=on'; +SET SESSION optimizer_switch='semijoin_with_cache=on'; +SET SESSION join_cache_level=1; +EXPLAIN +SELECT * FROM t1 WHERE (t1.b) IN (SELECT c FROM t2); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where +1 PRIMARY t2 ref c c 5 test.t1.b 2 Using index; Start temporary; End temporary +SELECT * FROM t1 WHERE (t1.b) IN (SELECT c FROM t2); +a b +3914 17 +3888 20 +SET SESSION join_cache_level=3; +EXPLAIN +SELECT * FROM t1 WHERE (t1.b) IN (SELECT c FROM t2); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where; Start temporary +1 PRIMARY t2 hash_index c #hash#c:c 5:5 test.t1.b 8 End temporary; Using join buffer (flat, BNLH join) +SELECT * FROM t1 WHERE (t1.b) IN (SELECT c FROM t2); +a b +3914 17 +3888 20 +SET SESSION join_cache_level = DEFAULT; +SET optimizer_switch=@tmp887479_optimizer_switch; +DROP TABLE t1,t2; set @@optimizer_switch=@save_optimizer_switch; diff --git a/mysql-test/t/join_cache.test b/mysql-test/t/join_cache.test index 116f19b55eb..530528ead2c 100644 --- a/mysql-test/t/join_cache.test +++ b/mysql-test/t/join_cache.test @@ -3225,5 +3225,34 @@ SET SESSION join_cache_level = DEFAULT; DROP TABLE t1, t2; +--echo # +--echo # Bug #887479: join_cache_level=3 + semijoin=on +--echo # + +CREATE TABLE t1 (a int, b int); +INSERT INTO t1 VALUES (3914,17), (3710,5), (3888,20); + +CREATE TABLE t2 (c int, KEY (c)); +INSERT INTO t2 VALUES (27), (17), (33), (20), (3), (7), (18), (2); + +SET @tmp887479_optimizer_switch=@@optimizer_switch; +SET SESSION optimizer_switch='semijoin=on'; +SET SESSION optimizer_switch='semijoin_with_cache=on'; + +SET SESSION join_cache_level=1; +EXPLAIN +SELECT * FROM t1 WHERE (t1.b) IN (SELECT c FROM t2); +SELECT * FROM t1 WHERE (t1.b) IN (SELECT c FROM t2); + +SET SESSION join_cache_level=3; +EXPLAIN +SELECT * FROM t1 WHERE (t1.b) IN (SELECT c FROM t2); +SELECT * FROM t1 WHERE (t1.b) IN (SELECT c FROM t2); + +SET SESSION join_cache_level = DEFAULT; +SET optimizer_switch=@tmp887479_optimizer_switch; + +DROP TABLE t1,t2; + # this must be the last command in the file set @@optimizer_switch=@save_optimizer_switch; diff --git a/sql/opt_subselect.cc b/sql/opt_subselect.cc index 56815a624e2..a954811bdfa 100644 --- a/sql/opt_subselect.cc +++ b/sql/opt_subselect.cc @@ -3921,7 +3921,7 @@ int setup_semijoin_dups_elimination(JOIN *join, ulonglong options, if (j != join->const_tables && js_tab->use_quick != 2 && j <= no_jbuf_after && ((js_tab->type == JT_ALL && join_cache_level != 0) || - (join_cache_level > 4 && (tab->type == JT_REF || + (join_cache_level > 2 && (tab->type == JT_REF || tab->type == JT_EQ_REF)))) { /* Looks like we'll be using join buffer */