Commit a02642b6 authored by Oleksandr Byelkin's avatar Oleksandr Byelkin

MDEV-10017: Get unexpected `Empty Set` for correlated subquery with aggregate functions (part 1)

Make aggregate function dependency visible.
parent 00d84ead
...@@ -2379,5 +2379,23 @@ companynr AVG(fld1) avg1 avg2 ...@@ -2379,5 +2379,23 @@ companynr AVG(fld1) avg1 avg2
37 9223372036854775805.0000 9223372036854775805 9223372036854775805 37 9223372036854775805.0000 9223372036854775805 9223372036854775805
DROP TABLE t1; DROP TABLE t1;
# #
# case where aggregate resolved in the local SELECT
# but outer ones are checked
#
create table t10 (a int , b int, c int);
insert into t10 values (0,0,0),(1,1,1);
create table t11 as select * from t10;
create table t12 as select * from t10;
explain extended select a from t10 where c<3 or a in (select c from t12 union select max(t10.b) from t11 group by t11.c);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t10 ALL NULL NULL NULL NULL 2 100.00 Using where
2 DEPENDENT SUBQUERY t12 ALL NULL NULL NULL NULL 2 100.00 Using where
3 DEPENDENT UNION t11 ALL NULL NULL NULL NULL 2 100.00 Using temporary
NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL
Warnings:
Note 1276 Field or reference 'test.t10.b' of SELECT #3 was resolved in SELECT #1
Note 1003 select `test`.`t10`.`a` AS `a` from `test`.`t10` where ((`test`.`t10`.`c` < 3) or <expr_cache><`test`.`t10`.`a`,`test`.`t10`.`b`>(<in_optimizer>(`test`.`t10`.`a`,<exists>(select `test`.`t12`.`c` from `test`.`t12` where (<cache>(`test`.`t10`.`a`) = `test`.`t12`.`c`) union select max(`test`.`t10`.`b`) from `test`.`t11` group by `test`.`t11`.`c` having (<cache>(`test`.`t10`.`a`) = <ref_null_helper>(max(`test`.`t10`.`b`)))))))
drop table t10,t11,t12;
#
# End of 10.1 tests # End of 10.1 tests
# #
...@@ -890,6 +890,7 @@ ERROR 42S22: Unknown column 'c' in 'field list' ...@@ -890,6 +890,7 @@ ERROR 42S22: Unknown column 'c' in 'field list'
SHOW WARNINGS; SHOW WARNINGS;
Level Code Message Level Code Message
Note 1276 Field or reference 'test.t1.a' of SELECT #3 was resolved in SELECT #2 Note 1276 Field or reference 'test.t1.a' of SELECT #3 was resolved in SELECT #2
Note 1981 Aggregate function 'count()' of SELECT #3 belongs to SELECT #2
Note 1276 Field or reference 'test.t1.c' of SELECT #3 was resolved in SELECT #2 Note 1276 Field or reference 'test.t1.c' of SELECT #3 was resolved in SELECT #2
Error 1054 Unknown column 'c' in 'field list' Error 1054 Unknown column 'c' in 'field list'
DROP TABLE t1; DROP TABLE t1;
......
...@@ -900,6 +900,7 @@ ERROR 42S22: Unknown column 'c' in 'field list' ...@@ -900,6 +900,7 @@ ERROR 42S22: Unknown column 'c' in 'field list'
SHOW WARNINGS; SHOW WARNINGS;
Level Code Message Level Code Message
Note 1276 Field or reference 'test.t1.a' of SELECT #3 was resolved in SELECT #2 Note 1276 Field or reference 'test.t1.a' of SELECT #3 was resolved in SELECT #2
Note 1981 Aggregate function 'count()' of SELECT #3 belongs to SELECT #2
Note 1276 Field or reference 'test.t1.c' of SELECT #3 was resolved in SELECT #2 Note 1276 Field or reference 'test.t1.c' of SELECT #3 was resolved in SELECT #2
Error 1054 Unknown column 'c' in 'field list' Error 1054 Unknown column 'c' in 'field list'
DROP TABLE t1; DROP TABLE t1;
......
...@@ -1139,6 +1139,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra ...@@ -1139,6 +1139,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
3 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 4 100.00 Using where 3 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 4 100.00 Using where
Warnings: Warnings:
Note 1276 Field or reference 'test.t1.b' of SELECT #3 was resolved in SELECT #1 Note 1276 Field or reference 'test.t1.b' of SELECT #3 was resolved in SELECT #1
Note 1981 Aggregate function 'max()' of SELECT #3 belongs to SELECT #1
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` group by `test`.`t1`.`a` having <expr_cache><`test`.`t1`.`a`,`test`.`t1`.`b`,max(`test`.`t1`.`b`),max(`test`.`t1`.`b`)>(<in_optimizer>(`test`.`t1`.`a`,<exists>(select `test`.`t2`.`c` from `test`.`t2` where (<nop>(<expr_cache><`test`.`t2`.`d`,`test`.`t1`.`b`,max(`test`.`t1`.`b`),max(`test`.`t1`.`b`)>(<in_optimizer>(`test`.`t2`.`d`,<exists>(select `test`.`t3`.`e` from `test`.`t3` where (max(`test`.`t1`.`b`) = `test`.`t3`.`e`) having (<cache>(`test`.`t2`.`d`) >= <ref_null_helper>(`test`.`t3`.`e`)))))) and (<cache>(`test`.`t1`.`a`) = `test`.`t2`.`c`))))) Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` group by `test`.`t1`.`a` having <expr_cache><`test`.`t1`.`a`,`test`.`t1`.`b`,max(`test`.`t1`.`b`),max(`test`.`t1`.`b`)>(<in_optimizer>(`test`.`t1`.`a`,<exists>(select `test`.`t2`.`c` from `test`.`t2` where (<nop>(<expr_cache><`test`.`t2`.`d`,`test`.`t1`.`b`,max(`test`.`t1`.`b`),max(`test`.`t1`.`b`)>(<in_optimizer>(`test`.`t2`.`d`,<exists>(select `test`.`t3`.`e` from `test`.`t3` where (max(`test`.`t1`.`b`) = `test`.`t3`.`e`) having (<cache>(`test`.`t2`.`d`) >= <ref_null_helper>(`test`.`t3`.`e`)))))) and (<cache>(`test`.`t1`.`a`) = `test`.`t2`.`c`)))))
select a from t1 group by a select a from t1 group by a
having a in (select c from t2 where d >= some(select e from t3 where max(b)=e)); having a in (select c from t2 where d >= some(select e from t3 where max(b)=e));
......
...@@ -1173,6 +1173,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra ...@@ -1173,6 +1173,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
3 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 4 100.00 Using where 3 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 4 100.00 Using where
Warnings: Warnings:
Note 1276 Field or reference 'test.t1.b' of SELECT #3 was resolved in SELECT #1 Note 1276 Field or reference 'test.t1.b' of SELECT #3 was resolved in SELECT #1
Note 1981 Aggregate function 'max()' of SELECT #3 belongs to SELECT #1
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` group by `test`.`t1`.`a` having <expr_cache><`test`.`t1`.`a`,`test`.`t1`.`b`,max(`test`.`t1`.`b`),max(`test`.`t1`.`b`)>(<in_optimizer>(`test`.`t1`.`a`,<exists>(select `test`.`t2`.`c` from `test`.`t2` where (<nop>(<expr_cache><`test`.`t2`.`d`,`test`.`t1`.`b`,max(`test`.`t1`.`b`),max(`test`.`t1`.`b`)>(<in_optimizer>(`test`.`t2`.`d`,<exists>(select `test`.`t3`.`e` from `test`.`t3` where (max(`test`.`t1`.`b`) = `test`.`t3`.`e`) having (<cache>(`test`.`t2`.`d`) >= <ref_null_helper>(`test`.`t3`.`e`)))))) and (<cache>(`test`.`t1`.`a`) = `test`.`t2`.`c`))))) Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` group by `test`.`t1`.`a` having <expr_cache><`test`.`t1`.`a`,`test`.`t1`.`b`,max(`test`.`t1`.`b`),max(`test`.`t1`.`b`)>(<in_optimizer>(`test`.`t1`.`a`,<exists>(select `test`.`t2`.`c` from `test`.`t2` where (<nop>(<expr_cache><`test`.`t2`.`d`,`test`.`t1`.`b`,max(`test`.`t1`.`b`),max(`test`.`t1`.`b`)>(<in_optimizer>(`test`.`t2`.`d`,<exists>(select `test`.`t3`.`e` from `test`.`t3` where (max(`test`.`t1`.`b`) = `test`.`t3`.`e`) having (<cache>(`test`.`t2`.`d`) >= <ref_null_helper>(`test`.`t3`.`e`)))))) and (<cache>(`test`.`t1`.`a`) = `test`.`t2`.`c`)))))
select a from t1 group by a select a from t1 group by a
having a in (select c from t2 where d >= some(select e from t3 where max(b)=e)); having a in (select c from t2 where d >= some(select e from t3 where max(b)=e));
......
...@@ -1647,6 +1647,16 @@ INSERT INTO t1 VALUES (3,0x7FFFFFFFFFFFFFFC,37); ...@@ -1647,6 +1647,16 @@ INSERT INTO t1 VALUES (3,0x7FFFFFFFFFFFFFFC,37);
SELECT companynr, AVG(fld1), AVG(fld1)<<0 AS avg1, CAST(AVG(fld1) AS UNSIGNED)<<0 AS avg2 FROM t1 GROUP BY companynr; SELECT companynr, AVG(fld1), AVG(fld1)<<0 AS avg1, CAST(AVG(fld1) AS UNSIGNED)<<0 AS avg2 FROM t1 GROUP BY companynr;
DROP TABLE t1; DROP TABLE t1;
--echo #
--echo # case where aggregate resolved in the local SELECT
--echo # but outer ones are checked
--echo #
create table t10 (a int , b int, c int);
insert into t10 values (0,0,0),(1,1,1);
create table t11 as select * from t10;
create table t12 as select * from t10;
explain extended select a from t10 where c<3 or a in (select c from t12 union select max(t10.b) from t11 group by t11.c);
drop table t10,t11,t12;
--echo # --echo #
--echo # End of 10.1 tests --echo # End of 10.1 tests
--echo # --echo #
...@@ -380,6 +380,16 @@ bool Item_sum::register_sum_func(THD *thd, Item **ref) ...@@ -380,6 +380,16 @@ bool Item_sum::register_sum_func(THD *thd, Item **ref)
sl->master_unit()->item->with_sum_func= 1; sl->master_unit()->item->with_sum_func= 1;
} }
thd->lex->current_select->mark_as_dependent(thd, aggr_sel, NULL); thd->lex->current_select->mark_as_dependent(thd, aggr_sel, NULL);
if ((thd->lex->describe & DESCRIBE_EXTENDED) && aggr_sel)
{
push_warning_printf(thd, Sql_condition::WARN_LEVEL_NOTE,
ER_WARN_AGGFUNC_DEPENDENCE,
ER_THD(thd, ER_WARN_AGGFUNC_DEPENDENCE),
func_name(),
thd->lex->current_select->select_number,
aggr_sel->select_number);
}
return FALSE; return FALSE;
} }
......
...@@ -7139,3 +7139,6 @@ ER_KILL_QUERY_DENIED_ERROR ...@@ -7139,3 +7139,6 @@ ER_KILL_QUERY_DENIED_ERROR
ER_NO_EIS_FOR_FIELD ER_NO_EIS_FOR_FIELD
eng "Engine-independent statistics are not collected for column '%s'" eng "Engine-independent statistics are not collected for column '%s'"
ukr "Незалежна від типу таблиці статистика не збирається для стовбця '%s'" ukr "Незалежна від типу таблиці статистика не збирається для стовбця '%s'"
ER_WARN_AGGFUNC_DEPENDENCE
eng "Aggregate function '%-.192s)' of SELECT #%d belongs to SELECT #%d"
ukr "Агрегатна функція '%-.192s)' з SELECTу #%d належить до SELECTу #%d"
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