Skip to content

Commit ee95532

Browse files
committed
Handle WindowClause.runCondition in tree walker/mutator functions.
Commit 9d9c02c, which added the notion of a "run condition" for window functions, neglected to teach nodeFuncs.c to process the new field. Remarkably, that doesn't seem to have had any ill effects before we invented Var.varnullingrels, but now it can cause visible failures in join-removal scenarios. I have no faith that there's not reachable problems in v15 too, so back-patch the code change to v15 where 9d9c02c came in. The test case seems irrelevant to v15, though. Per bug #18277 from Zuming Jiang. Diagnosis and patch by Richard Guo. Discussion: https://postgr.es/m/18277-089ead83b329a2fd@postgresql.org
1 parent 65ee2ed commit ee95532

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

src/backend/nodes/nodeFuncs.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2283,6 +2283,8 @@ expression_tree_walker_impl(Node *node,
22832283
return true;
22842284
if (WALK(wc->endOffset))
22852285
return true;
2286+
if (WALK(wc->runCondition))
2287+
return true;
22862288
}
22872289
break;
22882290
case T_CTECycleClause:
@@ -2627,6 +2629,8 @@ query_tree_walker_impl(Query *query,
26272629
return true;
26282630
if (WALK(wc->endOffset))
26292631
return true;
2632+
if (WALK(wc->runCondition))
2633+
return true;
26302634
}
26312635
}
26322636

@@ -3312,6 +3316,7 @@ expression_tree_mutator_impl(Node *node,
33123316
MUTATE(newnode->orderClause, wc->orderClause, List *);
33133317
MUTATE(newnode->startOffset, wc->startOffset, Node *);
33143318
MUTATE(newnode->endOffset, wc->endOffset, Node *);
3319+
MUTATE(newnode->runCondition, wc->runCondition, List *);
33153320
return (Node *) newnode;
33163321
}
33173322
break;
@@ -3641,6 +3646,7 @@ query_tree_mutator_impl(Query *query,
36413646
FLATCOPY(newnode, wc, WindowClause);
36423647
MUTATE(newnode->startOffset, wc->startOffset, Node *);
36433648
MUTATE(newnode->endOffset, wc->endOffset, Node *);
3649+
MUTATE(newnode->runCondition, wc->runCondition, List *);
36443650

36453651
resultlist = lappend(resultlist, (Node *) newnode);
36463652
}

src/test/regress/expected/window.out

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3804,6 +3804,32 @@ SELECT * FROM
38043804
sales | 3 | 4800 | 08-01-2007 | 3 | 1 | 3 | 3 | 1
38053805
(2 rows)
38063806

3807+
-- Ensure we remove references to reduced outer joins as nulling rels in run
3808+
-- conditions
3809+
EXPLAIN (COSTS OFF)
3810+
SELECT 1 FROM
3811+
(SELECT ntile(e2.salary) OVER (PARTITION BY e1.depname) AS c
3812+
FROM empsalary e1 LEFT JOIN empsalary e2 ON TRUE
3813+
WHERE e1.empno = e2.empno) s
3814+
WHERE s.c = 1;
3815+
QUERY PLAN
3816+
---------------------------------------------------------
3817+
Subquery Scan on s
3818+
Filter: (s.c = 1)
3819+
-> WindowAgg
3820+
Run Condition: (ntile(e2.salary) OVER (?) <= 1)
3821+
-> Sort
3822+
Sort Key: e1.depname
3823+
-> Merge Join
3824+
Merge Cond: (e1.empno = e2.empno)
3825+
-> Sort
3826+
Sort Key: e1.empno
3827+
-> Seq Scan on empsalary e1
3828+
-> Sort
3829+
Sort Key: e2.empno
3830+
-> Seq Scan on empsalary e2
3831+
(14 rows)
3832+
38073833
-- Tests to ensure we don't push down the run condition when it's not valid to
38083834
-- do so.
38093835
-- Ensure we don't push down when the frame options show that the window

src/test/regress/sql/window.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,15 @@ SELECT * FROM
12361236
FROM empsalary
12371237
) e WHERE rn <= 1 AND c1 <= 3 AND nt < 2;
12381238

1239+
-- Ensure we remove references to reduced outer joins as nulling rels in run
1240+
-- conditions
1241+
EXPLAIN (COSTS OFF)
1242+
SELECT 1 FROM
1243+
(SELECT ntile(e2.salary) OVER (PARTITION BY e1.depname) AS c
1244+
FROM empsalary e1 LEFT JOIN empsalary e2 ON TRUE
1245+
WHERE e1.empno = e2.empno) s
1246+
WHERE s.c = 1;
1247+
12391248
-- Tests to ensure we don't push down the run condition when it's not valid to
12401249
-- do so.
12411250

0 commit comments

Comments
 (0)