diff --git a/mysql-test/r/subselect_mat.result b/mysql-test/r/subselect_mat.result index 81c8b2e1e84..b770cb8afd5 100644 --- a/mysql-test/r/subselect_mat.result +++ b/mysql-test/r/subselect_mat.result @@ -1764,6 +1764,22 @@ SELECT MIN(a) FROM t1, t2 WHERE b IN (SELECT c FROM t3 GROUP BY c); MIN(a) 1 DROP TABLE t1,t2,t3; +# +# BUG#902632: Crash or invalid read at st_join_table::cleanup, st_table::disable_keyread +# +CREATE TABLE t1 ( a INT ); +INSERT INTO t1 VALUES (1), (2); +CREATE TABLE t2 ( b INT ); +INSERT INTO t2 VALUES (3), (4); +CREATE TABLE t3 ( c INT ); +INSERT INTO t3 VALUES (5), (6); +SELECT * FROM t1 WHERE EXISTS ( +SELECT DISTINCT b FROM t2 +WHERE b <= a +AND b IN ( SELECT c FROM t3 GROUP BY c ) +); +a +DROP TABLE t1,t2,t3; # This must be at the end: set optimizer_switch=@subselect_sj_mat_tmp; set @subselect_mat_test_optimizer_switch_value=null; diff --git a/mysql-test/r/subselect_sj_mat.result b/mysql-test/r/subselect_sj_mat.result index 4845616018c..50e2f047005 100644 --- a/mysql-test/r/subselect_sj_mat.result +++ b/mysql-test/r/subselect_sj_mat.result @@ -1800,5 +1800,21 @@ SELECT MIN(a) FROM t1, t2 WHERE b IN (SELECT c FROM t3 GROUP BY c); MIN(a) 1 DROP TABLE t1,t2,t3; +# +# BUG#902632: Crash or invalid read at st_join_table::cleanup, st_table::disable_keyread +# +CREATE TABLE t1 ( a INT ); +INSERT INTO t1 VALUES (1), (2); +CREATE TABLE t2 ( b INT ); +INSERT INTO t2 VALUES (3), (4); +CREATE TABLE t3 ( c INT ); +INSERT INTO t3 VALUES (5), (6); +SELECT * FROM t1 WHERE EXISTS ( +SELECT DISTINCT b FROM t2 +WHERE b <= a +AND b IN ( SELECT c FROM t3 GROUP BY c ) +); +a +DROP TABLE t1,t2,t3; # This must be at the end: set optimizer_switch=@subselect_sj_mat_tmp; diff --git a/mysql-test/t/subselect_sj_mat.test b/mysql-test/t/subselect_sj_mat.test index a8dbeded84a..9614a41767e 100644 --- a/mysql-test/t/subselect_sj_mat.test +++ b/mysql-test/t/subselect_sj_mat.test @@ -1465,6 +1465,23 @@ SELECT MIN(a) FROM t1, t2 WHERE b IN (SELECT c FROM t3 GROUP BY c); DROP TABLE t1,t2,t3; +--echo # +--echo # BUG#902632: Crash or invalid read at st_join_table::cleanup, st_table::disable_keyread +--echo # +CREATE TABLE t1 ( a INT ); +INSERT INTO t1 VALUES (1), (2); +CREATE TABLE t2 ( b INT ); +INSERT INTO t2 VALUES (3), (4); +CREATE TABLE t3 ( c INT ); +INSERT INTO t3 VALUES (5), (6); + +SELECT * FROM t1 WHERE EXISTS ( + SELECT DISTINCT b FROM t2 + WHERE b <= a + AND b IN ( SELECT c FROM t3 GROUP BY c ) + ); +DROP TABLE t1,t2,t3; + --echo # This must be at the end: set optimizer_switch=@subselect_sj_mat_tmp; diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 87d3ee77f78..c95bd6cbcda 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -9738,6 +9738,12 @@ void JOIN_TAB::cleanup() { end_read_record(&read_record); table->pos_in_table_list->jtbm_subselect->cleanup(); + /* + The above call freed the materializedd temptable. Set it to NULL so + that we don't attempt to touch it if JOIN_TAB::cleanup() is invoked + multiple times (it may be) + */ + table=NULL; } DBUG_VOID_RETURN; }