Commit b05158cc authored by Sergei Petrunia's avatar Sergei Petrunia

MDEV-8988: Apparently valid SQL query gives wrong result (nested WHERE)

- "Early NULLs filtering" optimization used to "peel off" Item_ref and
  Item_direct_ref wrappers from an outside column reference before
  adding "outer_table_col IS NOT NULL" into JOIN::outer_ref_cond.
- When this happened in a subquery that was evaluated in a post-GROUP-BY
  context, attempt to evaluate JOIN::outer_ref_cond would fetch an
  incorrect value of outer_table_col.
parent d044507d
......@@ -2640,3 +2640,41 @@ field1 field2
DROP TABLE t1;
DROP TABLE where_subselect;
# End of Bug #58782
#
# MDEV-8988: Apparently valid SQL query gives wrong result (nested WHERE)
#
create table t0(a int);
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
create table t1 (a int, b int, c int);
insert into t1 select A.a + 10*B.a, A.a, A.a + 10*B.a from t0 A, t0 B;
insert into t1 values (NULL, NULL, NULL);
create table t2 (c int, col1 int, key(c));
insert into t2 select t1.a, 100000 from t1;
analyze table t2;
Table Op Msg_type Msg_text
test.t2 analyze status Table is already up to date
explain
select
max(a)+ (select col1 from t2 where t2.c=t1.c)
from t1
group by t1.b;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 101 Using temporary; Using filesort
2 DEPENDENT SUBQUERY t2 ref c c 5 func 1
select
max(a) + (select col1 from t2 where t2.c=t1.c)
from t1
group by t1.b;
max(a) + (select col1 from t2 where t2.c=t1.c)
NULL
100090
100091
100092
100093
100094
100095
100096
100097
100098
100099
drop table t0,t1,t2;
......@@ -1767,3 +1767,29 @@ DROP TABLE where_subselect;
--echo # End of Bug #58782
--echo #
--echo # MDEV-8988: Apparently valid SQL query gives wrong result (nested WHERE)
--echo #
create table t0(a int);
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
create table t1 (a int, b int, c int);
insert into t1 select A.a + 10*B.a, A.a, A.a + 10*B.a from t0 A, t0 B;
insert into t1 values (NULL, NULL, NULL);
create table t2 (c int, col1 int, key(c));
insert into t2 select t1.a, 100000 from t1;
analyze table t2;
explain
select
max(a)+ (select col1 from t2 where t2.c=t1.c)
from t1
group by t1.b;
select
max(a) + (select col1 from t2 where t2.c=t1.c)
from t1
group by t1.b;
drop table t0,t1,t2;
......@@ -9359,7 +9359,7 @@ static void add_not_null_conds(JOIN *join)
if (!referred_tab)
continue;
if (!(notnull= new (join->thd->mem_root)
Item_func_isnotnull(join->thd, not_null_item)))
Item_func_isnotnull(join->thd, item)))
DBUG_VOID_RETURN;
/*
We need to do full fix_fields() call here in order to have correct
......
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