Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 30dbdbe

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 9f8377f commit 30dbdbe

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

src/backend/parser/parse_agg.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -736,8 +736,7 @@ check_agg_arguments_walker(Node *node,
736736
context->min_agglevel > agglevelsup)
737737
context->min_agglevel = agglevelsup;
738738
}
739-
/* no need to examine args of the inner aggregate */
740-
return false;
739+
/* Continue and descend into subtree */
741740
}
742741
if (IsA(node, GroupingFunc))
743742
{

src/test/regress/expected/aggregates.out

+6
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,12 @@ select (select max(min(unique1)) from int8_tbl) from tenk1;
12821282
ERROR: aggregate function calls cannot be nested
12831283
LINE 1: select (select max(min(unique1)) from int8_tbl) from tenk1;
12841284
^
1285+
select avg((select avg(a1.col1 order by (select avg(a2.col2) from tenk1 a3))
1286+
from tenk1 a1(col1)))
1287+
from tenk1 a2(col2);
1288+
ERROR: aggregate function calls cannot be nested
1289+
LINE 1: select avg((select avg(a1.col1 order by (select avg(a2.col2)...
1290+
^
12851291
--
12861292
-- Test removal of redundant GROUP BY columns
12871293
--

src/test/regress/sql/aggregates.sql

+3
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,9 @@ drop table minmaxtest cascade;
439439
-- check for correct detection of nested-aggregate errors
440440
select max(min(unique1)) from tenk1;
441441
select (select max(min(unique1)) from int8_tbl) from tenk1;
442+
select avg((select avg(a1.col1 order by (select avg(a2.col2) from tenk1 a3))
443+
from tenk1 a1(col1)))
444+
from tenk1 a2(col2);
442445

443446
--
444447
-- Test removal of redundant GROUP BY columns

0 commit comments

Comments
 (0)