Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Fix failure to detect some cases of improperly-nested aggregates.
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 13 Mar 2023 16:40:28 +0000 (12:40 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 13 Mar 2023 16:40:28 +0000 (12:40 -0400)
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

src/backend/parser/parse_agg.c
src/test/regress/expected/aggregates.out
src/test/regress/sql/aggregates.sql

index 00c0c00a00b6f0f5a3aad8f09c92c9ed9c15f39b..828cd99bc1e958298bc46a9712c8de9005aacecb 100644 (file)
@@ -728,8 +728,7 @@ check_agg_arguments_walker(Node *node,
                context->min_agglevel > agglevelsup)
                context->min_agglevel = agglevelsup;
        }
-       /* no need to examine args of the inner aggregate */
-       return false;
+       /* Continue and descend into subtree */
    }
    if (IsA(node, GroupingFunc))
    {
index 5949996ebc02a6116436b5f3190aa10271d84060..ffdfed8fef96fdbf7cc89d606441094acc513908 100644 (file)
@@ -1248,6 +1248,12 @@ select (select max(min(unique1)) from int8_tbl) from tenk1;
 ERROR:  aggregate function calls cannot be nested
 LINE 1: select (select max(min(unique1)) from int8_tbl) from tenk1;
                            ^
+select avg((select avg(a1.col1 order by (select avg(a2.col2) from tenk1 a3))
+            from tenk1 a1(col1)))
+from tenk1 a2(col2);
+ERROR:  aggregate function calls cannot be nested
+LINE 1: select avg((select avg(a1.col1 order by (select avg(a2.col2)...
+                                                        ^
 --
 -- Test removal of redundant GROUP BY columns
 --
index 7cf86465e947a72be395547659e76d1c3c083d80..3d5df2e469e83ea3253e135237355e171f12f839 100644 (file)
@@ -419,6 +419,9 @@ drop table minmaxtest cascade;
 -- check for correct detection of nested-aggregate errors
 select max(min(unique1)) from tenk1;
 select (select max(min(unique1)) from int8_tbl) from tenk1;
+select avg((select avg(a1.col1 order by (select avg(a2.col2) from tenk1 a3))
+            from tenk1 a1(col1)))
+from tenk1 a2(col2);
 
 --
 -- Test removal of redundant GROUP BY columns