Skip to content

Commit 53823a0

Browse files
committed
Fix failure to set correct operator in window run condition
This was a simple omission in 9d9c02c where the code didn't correctly set the operator to use in the run condition OpExpr when the window function was both monotonically increasing and decreasing. Bug discovered by Julien Roze, although he did not report it. Reported-by: Phil Florent Discussion: https://postgr.es/m/PA4P191MB160009A09B9D0624359278CFBA9F9@PA4P191MB1600.EURP191.PROD.OUTLOOK.COM Backpatch-through: 15, where 9d9c02c was added
1 parent cf112c1 commit 53823a0

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

src/backend/optimizer/path/allpaths.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2306,6 +2306,7 @@ find_window_run_conditions(Query *subquery, RangeTblEntry *rte, Index rti,
23062306
{
23072307
*keep_original = false;
23082308
runopexpr = opexpr;
2309+
runoperator = opexpr->opno;
23092310
break;
23102311
}
23112312

src/test/regress/expected/window.out

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3646,6 +3646,23 @@ WHERE c <= 3;
36463646
3 | sales | 4800 | 3
36473647
(8 rows)
36483648

3649+
-- Ensure we get the correct run condition when the window function is both
3650+
-- monotonically increasing and decreasing.
3651+
EXPLAIN (COSTS OFF)
3652+
SELECT * FROM
3653+
(SELECT empno,
3654+
depname,
3655+
salary,
3656+
count(empno) OVER () c
3657+
FROM empsalary) emp
3658+
WHERE c = 1;
3659+
QUERY PLAN
3660+
--------------------------------------------------------
3661+
WindowAgg
3662+
Run Condition: (count(empsalary.empno) OVER (?) = 1)
3663+
-> Seq Scan on empsalary
3664+
(3 rows)
3665+
36493666
-- Some more complex cases with multiple window clauses
36503667
EXPLAIN (COSTS OFF)
36513668
SELECT * FROM

src/test/regress/sql/window.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,17 @@ SELECT * FROM
11501150
FROM empsalary) emp
11511151
WHERE c <= 3;
11521152

1153+
-- Ensure we get the correct run condition when the window function is both
1154+
-- monotonically increasing and decreasing.
1155+
EXPLAIN (COSTS OFF)
1156+
SELECT * FROM
1157+
(SELECT empno,
1158+
depname,
1159+
salary,
1160+
count(empno) OVER () c
1161+
FROM empsalary) emp
1162+
WHERE c = 1;
1163+
11531164
-- Some more complex cases with multiple window clauses
11541165
EXPLAIN (COSTS OFF)
11551166
SELECT * FROM

0 commit comments

Comments
 (0)