diff --git a/mysql-test/r/func_group.result b/mysql-test/r/func_group.result index 318248459a5..dc3a0b60ad3 100644 --- a/mysql-test/r/func_group.result +++ b/mysql-test/r/func_group.result @@ -2379,5 +2379,23 @@ companynr AVG(fld1) avg1 avg2 37 9223372036854775805.0000 9223372036854775805 9223372036854775805 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 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 <`test`.`t10`.`a`,`test`.`t10`.`b`>((`test`.`t10`.`a`,(select `test`.`t12`.`c` from `test`.`t12` where ((`test`.`t10`.`a`) = `test`.`t12`.`c`) union select max(`test`.`t10`.`b`) from `test`.`t11` group by `test`.`t11`.`c` having ((`test`.`t10`.`a`) = (max(`test`.`t10`.`b`))))))) +drop table t10,t11,t12; +# # End of 10.1 tests # diff --git a/mysql-test/r/subselect3.result b/mysql-test/r/subselect3.result index 0c03959a96a..31e0f66f961 100644 --- a/mysql-test/r/subselect3.result +++ b/mysql-test/r/subselect3.result @@ -890,6 +890,7 @@ ERROR 42S22: Unknown column 'c' in 'field list' SHOW WARNINGS; Level Code Message 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 Error 1054 Unknown column 'c' in 'field list' DROP TABLE t1; diff --git a/mysql-test/r/subselect3_jcl6.result b/mysql-test/r/subselect3_jcl6.result index 415963af882..8d976f8ddf8 100644 --- a/mysql-test/r/subselect3_jcl6.result +++ b/mysql-test/r/subselect3_jcl6.result @@ -900,6 +900,7 @@ ERROR 42S22: Unknown column 'c' in 'field list' SHOW WARNINGS; Level Code Message 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 Error 1054 Unknown column 'c' in 'field list' DROP TABLE t1; diff --git a/mysql-test/r/subselect_mat.result b/mysql-test/r/subselect_mat.result index a34b7d28e2e..af3df9ec811 100644 --- a/mysql-test/r/subselect_mat.result +++ b/mysql-test/r/subselect_mat.result @@ -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 Warnings: 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 <`test`.`t1`.`a`,`test`.`t1`.`b`,max(`test`.`t1`.`b`),max(`test`.`t1`.`b`)>((`test`.`t1`.`a`,(select `test`.`t2`.`c` from `test`.`t2` where ((<`test`.`t2`.`d`,`test`.`t1`.`b`,max(`test`.`t1`.`b`),max(`test`.`t1`.`b`)>((`test`.`t2`.`d`,(select `test`.`t3`.`e` from `test`.`t3` where (max(`test`.`t1`.`b`) = `test`.`t3`.`e`) having ((`test`.`t2`.`d`) >= (`test`.`t3`.`e`)))))) and ((`test`.`t1`.`a`) = `test`.`t2`.`c`))))) 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)); diff --git a/mysql-test/r/subselect_sj_mat.result b/mysql-test/r/subselect_sj_mat.result index 93fdcf1ccca..9aa223b83f2 100644 --- a/mysql-test/r/subselect_sj_mat.result +++ b/mysql-test/r/subselect_sj_mat.result @@ -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 Warnings: 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 <`test`.`t1`.`a`,`test`.`t1`.`b`,max(`test`.`t1`.`b`),max(`test`.`t1`.`b`)>((`test`.`t1`.`a`,(select `test`.`t2`.`c` from `test`.`t2` where ((<`test`.`t2`.`d`,`test`.`t1`.`b`,max(`test`.`t1`.`b`),max(`test`.`t1`.`b`)>((`test`.`t2`.`d`,(select `test`.`t3`.`e` from `test`.`t3` where (max(`test`.`t1`.`b`) = `test`.`t3`.`e`) having ((`test`.`t2`.`d`) >= (`test`.`t3`.`e`)))))) and ((`test`.`t1`.`a`) = `test`.`t2`.`c`))))) 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)); diff --git a/mysql-test/t/func_group.test b/mysql-test/t/func_group.test index 07a99f1b827..69a4dc1fddc 100644 --- a/mysql-test/t/func_group.test +++ b/mysql-test/t/func_group.test @@ -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; 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 # End of 10.1 tests --echo # diff --git a/sql/item_sum.cc b/sql/item_sum.cc index 5220521b95a..540eefcc79a 100644 --- a/sql/item_sum.cc +++ b/sql/item_sum.cc @@ -380,6 +380,16 @@ bool Item_sum::register_sum_func(THD *thd, Item **ref) sl->master_unit()->item->with_sum_func= 1; } 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; } diff --git a/sql/share/errmsg-utf8.txt b/sql/share/errmsg-utf8.txt index 709ac55b28c..a6eaf2b27bd 100644 --- a/sql/share/errmsg-utf8.txt +++ b/sql/share/errmsg-utf8.txt @@ -7139,3 +7139,6 @@ ER_KILL_QUERY_DENIED_ERROR ER_NO_EIS_FOR_FIELD eng "Engine-independent statistics are not collected for column '%s'" ukr "Незалежна від типу таблиці статистика не збирається для стовбця '%s'" +ER_WARN_AGGFUNC_DEPENDENCE + eng "Aggregate function '%-.192s)' of SELECT #%d belongs to SELECT #%d" + ukr "Агрегатна функція '%-.192s)' з SELECTу #%d належить до SELECTу #%d"