Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d641b82

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 5b2da24 commit d641b82

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

src/backend/nodes/nodeFuncs.c

+6
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

+26
Original file line numberDiff line numberDiff line change
@@ -4181,6 +4181,32 @@ SELECT * FROM
41814181
sales | 3 | 4800 | 08-01-2007 | 3 | 1 | 3 | 3 | 1
41824182
(2 rows)
41834183

4184+
-- Ensure we remove references to reduced outer joins as nulling rels in run
4185+
-- conditions
4186+
EXPLAIN (COSTS OFF)
4187+
SELECT 1 FROM
4188+
(SELECT ntile(e2.salary) OVER (PARTITION BY e1.depname) AS c
4189+
FROM empsalary e1 LEFT JOIN empsalary e2 ON TRUE
4190+
WHERE e1.empno = e2.empno) s
4191+
WHERE s.c = 1;
4192+
QUERY PLAN
4193+
---------------------------------------------------------
4194+
Subquery Scan on s
4195+
Filter: (s.c = 1)
4196+
-> WindowAgg
4197+
Run Condition: (ntile(e2.salary) OVER (?) <= 1)
4198+
-> Sort
4199+
Sort Key: e1.depname
4200+
-> Merge Join
4201+
Merge Cond: (e1.empno = e2.empno)
4202+
-> Sort
4203+
Sort Key: e1.empno
4204+
-> Seq Scan on empsalary e1
4205+
-> Sort
4206+
Sort Key: e2.empno
4207+
-> Seq Scan on empsalary e2
4208+
(14 rows)
4209+
41844210
-- Tests to ensure we don't push down the run condition when it's not valid to
41854211
-- do so.
41864212
-- Ensure we don't push down when the frame options show that the window

src/test/regress/sql/window.sql

+9
Original file line numberDiff line numberDiff line change
@@ -1368,6 +1368,15 @@ SELECT * FROM
13681368
FROM empsalary
13691369
) e WHERE rn <= 1 AND c1 <= 3 AND nt < 2;
13701370

1371+
-- Ensure we remove references to reduced outer joins as nulling rels in run
1372+
-- conditions
1373+
EXPLAIN (COSTS OFF)
1374+
SELECT 1 FROM
1375+
(SELECT ntile(e2.salary) OVER (PARTITION BY e1.depname) AS c
1376+
FROM empsalary e1 LEFT JOIN empsalary e2 ON TRUE
1377+
WHERE e1.empno = e2.empno) s
1378+
WHERE s.c = 1;
1379+
13711380
-- Tests to ensure we don't push down the run condition when it's not valid to
13721381
-- do so.
13731382

0 commit comments

Comments
 (0)