Re: BUG #17835: Two assertions failed in nodeAgg.c and execExprInterp.c with the same SQL - Mailing list pgsql-bugs

From Tom Lane
Subject Re: BUG #17835: Two assertions failed in nodeAgg.c and execExprInterp.c with the same SQL
Date
Msg-id 1772189.1678721174@sss.pgh.pa.us
Whole thread Raw
In response to BUG #17835: Two assertions failed in nodeAgg.c and execExprInterp.c with the same SQL  (PG Bug reporting form <noreply@postgresql.org>)
Responses Re: BUG #17835: Two assertions failed in nodeAgg.c and execExprInterp.c with the same SQL  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-bugs
PG Bug reporting form <noreply@postgresql.org> writes:
> When executing the following query with CTE:

> WITH table1 ( column25 ) AS ( SELECT 1 ) SELECT FROM ( SELECT column25
> column12 FROM table1 ) AS alias0 GROUP BY column12 HAVING AVG ( ( SELECT 1
> FROM table1 JOIN ( SELECT AVG ( column25 ORDER BY CASE 1 WHEN column12 THEN
> ( SELECT AVG ( column12 ) FROM table1 ) END ) column14 FROM table1 ) AS
> alias3 ON alias3 . column14 = 1 ) ) = 1 ;

> I get a failed assertion with the following stacktrace:

Simplifying a bit, we get

WITH table1 ( col1 ) AS ( SELECT 1 )
SELECT AVG((
  SELECT AVG ( a1.col1 ORDER BY ( SELECT AVG ( a2.col2 ) FROM table1 ) )
  FROM table1 a1
))
FROM table1 AS a2(col2);

PG v10 says
    ERROR:  aggregate function calls cannot be nested
which seems correct: per spec, the innermost AVG actually should belong
to the outer query level that sources a2.col2.  Later versions fail to
detect that the query is nonsensical and end up with nonsensical
executor state instead.  I bisected this to

69c3936a1499b772a749ae629fc59b2d72722332 is the first bad commit
commit 69c3936a1499b772a749ae629fc59b2d72722332
Author: Andres Freund <andres@anarazel.de>
Date:   Tue Jan 9 13:25:38 2018 -0800

    Expression evaluation based aggregate transition invocation.

So that commit broke something about the nested-aggregate detection
logic.  It's not completely gone: if we simplify this to

WITH table1 ( col1 ) AS ( SELECT 1 )
SELECT AVG((
  SELECT AVG ( ( SELECT AVG ( a2.col2 ) FROM table1 ) )
  FROM table1 a1
))
FROM table1 AS a2(col2);

we still get

ERROR:  aggregate function calls cannot be nested
LINE 3:   SELECT AVG ( ( SELECT AVG ( a2.col2 ) FROM table1 ) )
                                ^

v10 detected this in ExecInitAgg, while later versions are trying to
do it in the parser, but evidently there's some gap there ...

            regards, tom lane



pgsql-bugs by date:

Previous
From: PG Bug reporting form
Date:
Subject: BUG #17837: The potential risks associated with executing "commit" in a procedure.
Next
From: "Daniel Verite"
Date:
Subject: Re: disable pipeline mode