Commit 5b73a17b authored by Sergey Petrunya's avatar Sergey Petrunya

BUG#1002630: Valgrind warnings 'Invalid read' in subselect_engine::calc_const_tables with SELECT

- In JOIN::exec(), make the having->update_used_tables() call before we've
  made the JOIN::cleanup(full=true) call. The latter frees SJ-Materialization
  structures, which correlated subquery predicate items attempt to walk afterwards.
parent a834af8c
...@@ -2743,4 +2743,18 @@ AND ( alias1.a1, alias2.a1 ) IN ( SELECT c1, c1 FROM t3 ) ...@@ -2743,4 +2743,18 @@ AND ( alias1.a1, alias2.a1 ) IN ( SELECT c1, c1 FROM t3 )
GROUP BY field1; GROUP BY field1;
field1 field1
DROP TABLE t1,t3,t2; 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; set optimizer_switch=@subselect_sj_tmp;
...@@ -2757,6 +2757,20 @@ AND ( alias1.a1, alias2.a1 ) IN ( SELECT c1, c1 FROM t3 ) ...@@ -2757,6 +2757,20 @@ AND ( alias1.a1, alias2.a1 ) IN ( SELECT c1, c1 FROM t3 )
GROUP BY field1; GROUP BY field1;
field1 field1
DROP TABLE t1,t3,t2; 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; set optimizer_switch=@subselect_sj_tmp;
# #
# BUG#49129: Wrong result with IN-subquery with join_cache_level=6 and firstmatch=off # BUG#49129: Wrong result with IN-subquery with join_cache_level=6 and firstmatch=off
......
...@@ -2445,5 +2445,22 @@ GROUP BY field1; ...@@ -2445,5 +2445,22 @@ GROUP BY field1;
DROP TABLE t1,t3,t2; 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 # The following command must be the last one the file
set optimizer_switch=@subselect_sj_tmp; set optimizer_switch=@subselect_sj_tmp;
...@@ -2264,6 +2264,8 @@ JOIN::exec() ...@@ -2264,6 +2264,8 @@ JOIN::exec()
List<Item> *curr_all_fields= &all_fields; List<Item> *curr_all_fields= &all_fields;
List<Item> *curr_fields_list= &fields_list; List<Item> *curr_fields_list= &fields_list;
TABLE *curr_tmp_table= 0; TABLE *curr_tmp_table= 0;
bool tmp_having_used_tables_updated= FALSE;
/* /*
Initialize examined rows here because the values from all join parts Initialize examined rows here because the values from all join parts
must be accumulated in examined_row_count. Hence every join must be accumulated in examined_row_count. Hence every join
...@@ -2511,12 +2513,22 @@ JOIN::exec() ...@@ -2511,12 +2513,22 @@ JOIN::exec()
if (curr_tmp_table->distinct) if (curr_tmp_table->distinct)
curr_join->select_distinct=0; /* Each row is unique */ 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 */ curr_join->join_free(); /* Free quick selects */
if (curr_join->select_distinct && ! curr_join->group_list) if (curr_join->select_distinct && ! curr_join->group_list)
{ {
thd_proc_info(thd, "Removing duplicates"); 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, if (remove_duplicates(curr_join, curr_tmp_table,
*curr_fields_list, curr_join->tmp_having)) *curr_fields_list, curr_join->tmp_having))
DBUG_VOID_RETURN; DBUG_VOID_RETURN;
...@@ -2589,7 +2601,8 @@ JOIN::exec() ...@@ -2589,7 +2601,8 @@ JOIN::exec()
! curr_join->sort_and_group) ! curr_join->sort_and_group)
{ {
// Some tables may have been const // 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]; JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables];
table_map used_tables= (curr_join->const_table_map | table_map used_tables= (curr_join->const_table_map |
curr_table->table->map); curr_table->table->map);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment