]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Add subquery pullup handling for WindowClause runCondition
authorDavid Rowley <drowley@postgresql.org>
Sat, 10 Dec 2022 06:27:53 +0000 (19:27 +1300)
committerDavid Rowley <drowley@postgresql.org>
Sat, 10 Dec 2022 06:27:53 +0000 (19:27 +1300)
9d9c02ccd added code to allow WindowAgg to take some shortcuts when a
monotonic WindowFunc reached some value that it could never come back
from due to the function's monotonic nature.  That commit added a
runCondition field to WindowClause to store the condition which, when it
becomes false we can start taking shortcuts in nodeWindowAgg.c.

Here we fix an issue where subquery pullups didn't properly update the
runCondition to update the Vars to properly reference the new query level.

Here we also add a missing call to preprocess_expression() for the
WindowClause's runCondtion.  The WindowFuncs in the targetlist will have
had this process done, so we must also do it for the WindowFuncs in the
runCondition so that they can be correctly found in the targetlist
during setrefs.c

Bug: #17709
Reported-by: Alexey Makhmutov
Author: Richard Guo, David Rowley
Discussion: https://postgr.es/m/17709-4f557160e3e8ee9a@postgresql.org
Backpatch-through: 15, where 9d9c02ccd was introduced

src/backend/optimizer/plan/planner.c
src/backend/optimizer/prep/prepjointree.c

index 453a598cad48a601a7d95b4a4a49f2f587f7ef1d..c1535cbb3f29b1ce3a3ca7602443b22620e11ed2 100644 (file)
@@ -826,6 +826,9 @@ subquery_planner(PlannerGlobal *glob, Query *parse,
                                                                                                EXPRKIND_LIMIT);
                wc->endOffset = preprocess_expression(root, wc->endOffset,
                                                                                          EXPRKIND_LIMIT);
+               wc->runCondition = (List *) preprocess_expression(root,
+                                                                                                                 (Node *) wc->runCondition,
+                                                                                                                 EXPRKIND_TARGET);
        }
 
        parse->limitOffset = preprocess_expression(root, parse->limitOffset,
index cc7af394aef42f6b4ea9e5954564615719606c93..5a946cdfe9e8539ebb4481aca779fe2aa98985f3 100644 (file)
@@ -2124,6 +2124,15 @@ perform_pullup_replace_vars(PlannerInfo *root,
                pullup_replace_vars((Node *) parse->targetList, rvcontext);
        parse->returningList = (List *)
                pullup_replace_vars((Node *) parse->returningList, rvcontext);
+
+       foreach(lc, parse->windowClause)
+       {
+               WindowClause *wc = lfirst_node(WindowClause, lc);
+
+               if (wc->runCondition != NIL)
+                       wc->runCondition = (List *)
+                               pullup_replace_vars((Node *) wc->runCondition, rvcontext);
+       }
        if (parse->onConflict)
        {
                parse->onConflict->onConflictSet = (List *)