Skip to content

Commit a322651

Browse files
committed
MDEV-10017: Get unexpected Empty Set for correlated subquery with aggregate functions
take into account all arguments of aggregate function
1 parent f6e47c0 commit a322651

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

mysql-test/r/func_group.result

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2397,5 +2397,40 @@ Note 1276 Field or reference 'test.t10.b' of SELECT #3 was resolved in SELECT #1
23972397
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`)))))))
23982398
drop table t10,t11,t12;
23992399
#
2400+
# MDEV-10017: Get unexpected `Empty Set` for correlated subquery
2401+
# with aggregate functions
2402+
#
2403+
create table t1(c1 int, c2 int, c3 int);
2404+
insert into t1 values(1,1,1),(2,2,2),(3,3,3);
2405+
select * from t1;
2406+
c1 c2 c3
2407+
1 1 1
2408+
2 2 2
2409+
3 3 3
2410+
create table t2(c1 int, c2 int);
2411+
insert into t2 values(2,2);
2412+
select * from t2;
2413+
c1 c2
2414+
2 2
2415+
explain extended
2416+
select c1 from t1 having c1 >= (select t.c1 as c from t2 t order by (select min(t1.c1+c) from t2 tt));
2417+
ERROR HY000: Invalid use of group function
2418+
select c1 from t1 having c1 >= (select t.c1 as c from t2 t order by (select min(t1.c1+c) from t2 tt));
2419+
ERROR HY000: Invalid use of group function
2420+
explain extended
2421+
select c1 from t1 having c1 >= (select t.c1 as c from t2 t order by (select min(t1.c1+tt.c1) from t2 tt));
2422+
id select_type table type possible_keys key key_len ref rows filtered Extra
2423+
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00
2424+
2 DEPENDENT SUBQUERY t system NULL NULL NULL NULL 1 100.00
2425+
3 DEPENDENT SUBQUERY tt system NULL NULL NULL NULL 1 100.00
2426+
Warnings:
2427+
Note 1276 Field or reference 'test.t1.c1' of SELECT #3 was resolved in SELECT #1
2428+
Note 1003 select `test`.`t1`.`c1` AS `c1` from `test`.`t1` having (`test`.`t1`.`c1` >= <expr_cache><`test`.`t1`.`c1`>((select 2 AS `c` from dual order by (select min((`test`.`t1`.`c1` + 2)) from dual))))
2429+
select c1 from t1 having c1 >= (select t.c1 as c from t2 t order by (select min(t1.c1+tt.c1) from t2 tt));
2430+
c1
2431+
2
2432+
3
2433+
drop table t1,t2;
2434+
#
24002435
# End of 10.1 tests
24012436
#

mysql-test/t/func_group.test

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,28 @@ create table t11 as select * from t10;
16571657
create table t12 as select * from t10;
16581658
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);
16591659
drop table t10,t11,t12;
1660+
--echo #
1661+
--echo # MDEV-10017: Get unexpected `Empty Set` for correlated subquery
1662+
--echo # with aggregate functions
1663+
--echo #
1664+
1665+
create table t1(c1 int, c2 int, c3 int);
1666+
insert into t1 values(1,1,1),(2,2,2),(3,3,3);
1667+
select * from t1;
1668+
create table t2(c1 int, c2 int);
1669+
insert into t2 values(2,2);
1670+
select * from t2;
1671+
--error ER_INVALID_GROUP_FUNC_USE
1672+
explain extended
1673+
select c1 from t1 having c1 >= (select t.c1 as c from t2 t order by (select min(t1.c1+c) from t2 tt));
1674+
--error ER_INVALID_GROUP_FUNC_USE
1675+
select c1 from t1 having c1 >= (select t.c1 as c from t2 t order by (select min(t1.c1+c) from t2 tt));
1676+
1677+
explain extended
1678+
select c1 from t1 having c1 >= (select t.c1 as c from t2 t order by (select min(t1.c1+tt.c1) from t2 tt));
1679+
select c1 from t1 having c1 >= (select t.c1 as c from t2 t order by (select min(t1.c1+tt.c1) from t2 tt));
1680+
drop table t1,t2;
1681+
16601682
--echo #
16611683
--echo # End of 10.1 tests
16621684
--echo #

sql/item.cc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ bool cmp_items(Item *a, Item *b)
5757
}
5858

5959

60+
/**
61+
Set max_sum_func_level if it is needed
62+
*/
63+
inline void set_max_sum_func_level(THD *thd, SELECT_LEX *select)
64+
{
65+
if (thd->lex->in_sum_func &&
66+
thd->lex->in_sum_func->nest_level >= select->nest_level)
67+
set_if_bigger(thd->lex->in_sum_func->max_sum_func_level,
68+
select->nest_level - 1);
69+
}
70+
6071
/*****************************************************************************
6172
** Item functions
6273
*****************************************************************************/
@@ -4885,6 +4896,11 @@ Item_field::fix_outer_field(THD *thd, Field **from_field, Item **reference)
48854896
if (rf->fix_fields(thd, reference) || rf->check_cols(1))
48864897
return -1;
48874898

4899+
/*
4900+
We can not "move" aggregate function in the place where
4901+
its arguments are not defined.
4902+
*/
4903+
set_max_sum_func_level(thd, select);
48884904
mark_as_dependent(thd, last_checked_context->select_lex,
48894905
context->select_lex, rf,
48904906
rf);
@@ -4893,6 +4909,11 @@ Item_field::fix_outer_field(THD *thd, Field **from_field, Item **reference)
48934909
}
48944910
else
48954911
{
4912+
/*
4913+
We can not "move" aggregate function in the place where
4914+
its arguments are not defined.
4915+
*/
4916+
set_max_sum_func_level(thd, select);
48964917
mark_as_dependent(thd, last_checked_context->select_lex,
48974918
context->select_lex,
48984919
this, (Item_ident*)*reference);
@@ -5024,6 +5045,11 @@ bool Item_field::fix_fields(THD *thd, Item **reference)
50245045
return(1);
50255046
}
50265047

5048+
/*
5049+
We can not "move" aggregate function in the place where
5050+
its arguments are not defined.
5051+
*/
5052+
set_max_sum_func_level(thd, thd->lex->current_select);
50275053
set_field(new_field);
50285054
return 0;
50295055
}
@@ -5048,6 +5074,11 @@ bool Item_field::fix_fields(THD *thd, Item **reference)
50485074
select->parsing_place == IN_GROUP_BY &&
50495075
alias_name_used ? *rf->ref : rf);
50505076

5077+
/*
5078+
We can not "move" aggregate function in the place where
5079+
its arguments are not defined.
5080+
*/
5081+
set_max_sum_func_level(thd, thd->lex->current_select);
50515082
return FALSE;
50525083
}
50535084
}

0 commit comments

Comments
 (0)