summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--mysql-test/r/subselect_sj.result14
-rw-r--r--mysql-test/r/subselect_sj_jcl6.result14
-rw-r--r--mysql-test/t/subselect_sj.test17
-rw-r--r--sql/sql_select.cc19
4 files changed, 61 insertions, 3 deletions
diff --git a/mysql-test/r/subselect_sj.result b/mysql-test/r/subselect_sj.result
index 3933100a7aa..ad2d5f760e8 100644
--- a/mysql-test/r/subselect_sj.result
+++ b/mysql-test/r/subselect_sj.result
@@ -2743,4 +2743,18 @@ AND ( alias1.a1, alias2.a1 ) IN ( SELECT c1, c1 FROM t3 )
GROUP BY field1;
field1
DROP TABLE t1,t3,t2;
+#
+# BUG#1002630: Valgrind warnings 'Invalid read' in subselect_engine::calc_const_tables with SELECT
+#
+CREATE TABLE t1 (a INT);
+INSERT INTO t1 VALUES (1),(7);
+CREATE TABLE t2 (b INT);
+INSERT INTO t2 VALUES (4),(6);
+SELECT ( SELECT SUM(a) FROM t1 ) AS t1sum, b
+FROM t2
+WHERE (1,1) IN ( SELECT MAX(a), MIN(a) FROM t1 )
+GROUP BY b
+HAVING t1sum <> 1;
+t1sum b
+DROP TABLE t1, t2;
set optimizer_switch=@subselect_sj_tmp;
diff --git a/mysql-test/r/subselect_sj_jcl6.result b/mysql-test/r/subselect_sj_jcl6.result
index 8cc9352bfa3..34388db5c3b 100644
--- a/mysql-test/r/subselect_sj_jcl6.result
+++ b/mysql-test/r/subselect_sj_jcl6.result
@@ -2757,6 +2757,20 @@ AND ( alias1.a1, alias2.a1 ) IN ( SELECT c1, c1 FROM t3 )
GROUP BY field1;
field1
DROP TABLE t1,t3,t2;
+#
+# BUG#1002630: Valgrind warnings 'Invalid read' in subselect_engine::calc_const_tables with SELECT
+#
+CREATE TABLE t1 (a INT);
+INSERT INTO t1 VALUES (1),(7);
+CREATE TABLE t2 (b INT);
+INSERT INTO t2 VALUES (4),(6);
+SELECT ( SELECT SUM(a) FROM t1 ) AS t1sum, b
+FROM t2
+WHERE (1,1) IN ( SELECT MAX(a), MIN(a) FROM t1 )
+GROUP BY b
+HAVING t1sum <> 1;
+t1sum b
+DROP TABLE t1, t2;
set optimizer_switch=@subselect_sj_tmp;
#
# 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 63bb29a2e13..2facb089718 100644
--- a/mysql-test/t/subselect_sj.test
+++ b/mysql-test/t/subselect_sj.test
@@ -2445,5 +2445,22 @@ GROUP BY field1;
DROP TABLE t1,t3,t2;
+--echo #
+--echo # BUG#1002630: Valgrind warnings 'Invalid read' in subselect_engine::calc_const_tables with SELECT
+--echo #
+CREATE TABLE t1 (a INT);
+INSERT INTO t1 VALUES (1),(7);
+
+CREATE TABLE t2 (b INT);
+INSERT INTO t2 VALUES (4),(6);
+
+SELECT ( SELECT SUM(a) FROM t1 ) AS t1sum, b
+FROM t2
+WHERE (1,1) IN ( SELECT MAX(a), MIN(a) FROM t1 )
+GROUP BY b
+HAVING t1sum <> 1;
+
+DROP TABLE t1, t2;
+
# The following command must be the last one the file
set optimizer_switch=@subselect_sj_tmp;
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index 4022b47613b..bf5ed800a1d 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -2264,6 +2264,8 @@ JOIN::exec()
List<Item> *curr_all_fields= &all_fields;
List<Item> *curr_fields_list= &fields_list;
TABLE *curr_tmp_table= 0;
+ bool tmp_having_used_tables_updated= FALSE;
+
/*
Initialize examined rows here because the values from all join parts
must be accumulated in examined_row_count. Hence every join
@@ -2511,12 +2513,22 @@ JOIN::exec()
if (curr_tmp_table->distinct)
curr_join->select_distinct=0; /* Each row is unique */
+
+ /*
+ curr_join->join_free() will call JOIN::cleanup(full=TRUE). It will not
+ be safe to call update_used_tables() after that.
+ */
+ if (curr_join->tmp_having)
+ {
+ curr_join->tmp_having->update_used_tables();
+ tmp_having_used_tables_updated= TRUE;
+ }
+
curr_join->join_free(); /* Free quick selects */
+
if (curr_join->select_distinct && ! curr_join->group_list)
{
thd_proc_info(thd, "Removing duplicates");
- if (curr_join->tmp_having)
- curr_join->tmp_having->update_used_tables();
if (remove_duplicates(curr_join, curr_tmp_table,
*curr_fields_list, curr_join->tmp_having))
DBUG_VOID_RETURN;
@@ -2589,7 +2601,8 @@ JOIN::exec()
! curr_join->sort_and_group)
{
// Some tables may have been const
- curr_join->tmp_having->update_used_tables();
+ if (!tmp_having_used_tables_updated)
+ curr_join->tmp_having->update_used_tables();
JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables];
table_map used_tables= (curr_join->const_table_map |
curr_table->table->map);