Re: BUG #17709: Regression in PG15 with window functions - "WindowFunc not found in subplan target lists"

From: David Rowley <dgrowleyml(at)gmail(dot)com>
To: Richard Guo <guofenglinux(at)gmail(dot)com>
Cc: makhmutov(at)gmail(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #17709: Regression in PG15 with window functions - "WindowFunc not found in subplan target lists"
Date: 2022-12-09 11:52:40
Message-ID: CAApHDvqCT4E0A1pt86sbGwWLRhdiWcuUHx8oAq1T+LqpOMskQg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Sat, 10 Dec 2022 at 00:00, Richard Guo <guofenglinux(at)gmail(dot)com> wrote:
> Thanks for the report! I can reproduce this issue.
>
> The WindowFunc within runCondition comes from the query's targetList,
> before we pull up subquery 't1'. Then when it comes to pulling up
> subquery 't1', we perform pullup variable replacement for the query's
> targetList but not for runCondition in the query's windowClause. I
> believe that's how this error is triggered.
>
> Below is how we can fix this issue.
>
> --- a/src/backend/optimizer/prep/prepjointree.c
> +++ b/src/backend/optimizer/prep/prepjointree.c
> @@ -2134,6 +2134,16 @@ perform_pullup_replace_vars(PlannerInfo *root,
> * can't contain any references to a subquery.
> */
> }
> + if (parse->windowClause)
> + {
> + foreach(lc, parse->windowClause)
> + {
> + WindowClause *wclause = (WindowClause *) lfirst(lc);
> +
> + wclause->runCondition = (List *)
> + pullup_replace_vars((Node *) wclause->runCondition, rvcontext);
> + }
> + }

Thanks for having a look at this.

I think what you have above fixes the bulk of the issue, but there's
still a bit more to do to properly fix the reported case.

The additional thing that seems to cause the reported error is that
once the subquery is pulled up, the run condition also needs a round
of constant folding done. See subquery_planner() around line 827. The
problem is that the target list's WindowFunc ends up with count(1)
over .., but the run condition's one is left as count(case 1 when 1
then 1 else null end), which preprocess_expression() will fold into
the same as what's in the target list.

I'm now wondering if WindowClause.runCondition should be of type Node
* instead of List *. I'd have imagined I should be passing the type of
EXPRKIND_QUAL to preprocess_expression's type, but canonicalize_qual()
does not like Lists.

David

Attachment Content-Type Size
runcondition_fix_v2.patch text/plain 1.2 KB

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Richard Guo 2022-12-09 14:01:21 Re: BUG #17709: Regression in PG15 with window functions - "WindowFunc not found in subplan target lists"
Previous Message Richard Guo 2022-12-09 11:00:19 Re: BUG #17709: Regression in PG15 with window functions - "WindowFunc not found in subplan target lists"