Fix failure to set correct operator in window run condition
authorDavid Rowley <drowley@postgresql.org>
Thu, 4 Aug 2022 22:14:00 +0000 (10:14 +1200)
committerDavid Rowley <drowley@postgresql.org>
Thu, 4 Aug 2022 22:14:00 +0000 (10:14 +1200)
This was a simple omission in 9d9c02ccd 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 9d9c02ccd was added

src/backend/optimizer/path/allpaths.c
src/test/regress/expected/window.out
src/test/regress/sql/window.sql

index b23cc8ee82ddb1000e91378a4e7955202b9b14c3..8fc28007f541d5295f8f7169ebb720f2453c0e2d 100644 (file)
@@ -2306,6 +2306,7 @@ find_window_run_conditions(Query *subquery, RangeTblEntry *rte, Index rti,
                        {
                                *keep_original = false;
                                runopexpr = opexpr;
+                               runoperator = opexpr->opno;
                                break;
                        }
 
index 433a0bb025973bd85d6f35a15fbff39afe641fac..55dcd668c94ec09c1c1c8fef62d5500e7563c059 100644 (file)
@@ -3646,6 +3646,23 @@ WHERE c <= 3;
      3 | sales     |   4800 | 3
 (8 rows)
 
+-- Ensure we get the correct run condition when the window function is both
+-- monotonically increasing and decreasing.
+EXPLAIN (COSTS OFF)
+SELECT * FROM
+  (SELECT empno,
+          depname,
+          salary,
+          count(empno) OVER () c
+   FROM empsalary) emp
+WHERE c = 1;
+                       QUERY PLAN                       
+--------------------------------------------------------
+ WindowAgg
+   Run Condition: (count(empsalary.empno) OVER (?) = 1)
+   ->  Seq Scan on empsalary
+(3 rows)
+
 -- Some more complex cases with multiple window clauses
 EXPLAIN (COSTS OFF)
 SELECT * FROM
index a504e46e40304fc43871abdcca0d8fac8dedab81..57c39e796c1810817c1b508d8cbeeb11f835c919 100644 (file)
@@ -1150,6 +1150,17 @@ SELECT * FROM
    FROM empsalary) emp
 WHERE c <= 3;
 
+-- Ensure we get the correct run condition when the window function is both
+-- monotonically increasing and decreasing.
+EXPLAIN (COSTS OFF)
+SELECT * FROM
+  (SELECT empno,
+          depname,
+          salary,
+          count(empno) OVER () c
+   FROM empsalary) emp
+WHERE c = 1;
+
 -- Some more complex cases with multiple window clauses
 EXPLAIN (COSTS OFF)
 SELECT * FROM