8000 Fix failure to detect some cases of improperly-nested aggregates. · postgres/postgres@0736b11 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0736b11

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 b183274 commit 0736b11

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
@@ -695,8 +695,7 @@ check_agg_arguments_walker(Node *node,
695695
context->min_agglevel > agglevelsup)
696696
context->min_agglevel = agglevelsup;
697697
}
698-
/* no need to examine args of the inner aggregate */
699-
return false;
698+
/* Continue and descend into subtree */
700699
}
701700
if (IsA(node, GroupingFunc))
702701
{

src/test/regress/expected/aggregates.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,12 @@ select (select max(min(unique1)) from int8_tbl) from tenk1;
954954
ERROR: aggregate function calls cannot be nested
955955
LINE 1: select (select max(min(unique1)) from int8_tbl) from tenk1;
956956
^
957+
select avg((select avg(a1.col1 order by (select avg(a2.col2) from tenk1 a3))
958+
from tenk1 a1(col1)))
959+
from tenk1 a2(col2);
960+
ERROR: aggregate function calls cannot be nested
961+
LINE 1: select avg((select avg(a1.col1 order by (select avg(a2.col2)...
962+
^
957963
--
958964
-- Test removal of redundant GROUP BY columns
959965
--

src/test/regress/sql/aggregates.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,9 @@ drop table minmaxtest cascade;
334334
-- check for correct detection of nested-aggregate errors
335335
select max(min(unique1)) from tenk1;
336336
select (select max(min(unique1)) from int8_tbl) from tenk1;
337+
select avg((select avg(a1.col1 order by (select avg(a2.col2) from tenk1 a3))
338+
from tenk1 a1(col1)))
339+
from tenk1 a2(col2);
337340

338341
--
339342
-- Test removal of redundant GROUP BY columns

0 commit comments

Comments
 (0)
0