Skip to content

Commit 096e708

Browse files
committed
Fix failure to detect some cases of improperly-nested aggregates.
check_agg_arguments_walker() supposed that it needn't descend into the arguments of a lower-level aggregate function, but this is just wrong in the presence of multiple levels of sub-select. The oversight would lead to executor failures on queries that should be rejected. (Prior to v11, they actually were rejected, thanks to a "redundant" execution-time check.) Per bug #17835 from Anban Company. Back-patch to all supported branches. Discussion: https://postgr.es/m/17835-4f29f3098b2d0ba4@postgresql.org
1 parent 7e31923 commit 096e708

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

src/backend/parser/parse_agg.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -728,8 +728,7 @@ check_agg_arguments_walker(Node *node,
728728
context->min_agglevel > agglevelsup)
729729
context->min_agglevel = agglevelsup;
730730
}
731-
/* no need to examine args of the inner aggregate */
732-
return false;
731+
/* Continue and descend into subtree */
733732
}
734733
if (IsA(node, GroupingFunc))
735734
{

src/test/regress/expected/aggregates.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,12 @@ select (select max(min(unique1)) from int8_tbl) from tenk1;
12481248
ERROR: aggregate function calls cannot be nested
12491249
LINE 1: select (select max(min(unique1)) from int8_tbl) from tenk1;
12501250
^
1251+
select avg((select avg(a1.col1 order by (select avg(a2.col2) from tenk1 a3))
1252+
from tenk1 a1(col1)))
1253+
from tenk1 a2(col2);
1254+
ERROR: aggregate function calls cannot be nested
1255+
LINE 1: select avg((select avg(a1.col1 order by (select avg(a2.col2)...
1256+
^
12511257
--
12521258
-- Test removal of redundant GROUP BY columns
12531259
--

src/test/regress/sql/aggregates.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,9 @@ drop table minmaxtest cascade;
419419
-- check for correct detection of nested-aggregate errors
420420
select max(min(unique1)) from tenk1;
421421
select (select max(min(unique1)) from int8_tbl) from tenk1;
422+
select avg((select avg(a1.col1 order by (select avg(a2.col2) from tenk1 a3))
423+
from tenk1 a1(col1)))
424+
from tenk1 a2(col2);
422425

423426
--
424427
-- Test removal of redundant GROUP BY columns

0 commit comments

Comments
 (0)