diff --git a/src/backend/optimizer/path/allpaths.c b/src/backend/optimizer/path/allpaths.c index d70d88d..7e93402 100644 --- a/src/backend/optimizer/path/allpaths.c +++ b/src/backend/optimizer/path/allpaths.c @@ -1692,7 +1692,7 @@ subquery_is_pushdown_safe(Query *subquery, Query *topquery, { SetOperationStmt *topop; - /* Check point 1 */ + /* Pushdown is unsafe if we have a LIMIT/OFFSET clause */ if (subquery->limitOffset != NULL || subquery->limitCount != NULL) return false; @@ -1794,9 +1794,9 @@ recurse_pushdown_safe(Node *setOp, Query *topquery, * * 4. If the subquery has windowing functions we are able to push down any * quals that are referenced in all of the subquery's window PARTITION BY - * clauses. This is permitted as window partitions are completed isolated + * clauses. This is permitted as window partitions are completely isolated * from each other and removing records from unneeded partitions early has - * no affect on the query results. + * no effect on the query results. */ static void check_output_expressions(Query *subquery, bool *unsafeColumns) @@ -1837,7 +1837,7 @@ check_output_expressions(Query *subquery, bool *unsafeColumns) continue; } - /* Check if all partition by clauses, check point 4 */ + /* Check if all PARTITION BY clauses, check point 4 */ if (subquery->hasWindowFuncs && !targetExistsInAllQueryPartitionByClauses(tle, subquery)) { diff --git a/src/backend/parser/parse_clause.c b/src/backend/parser/parse_clause.c index c2c8f52..6d9c027 100644 --- a/src/backend/parser/parse_clause.c +++ b/src/backend/parser/parse_clause.c @@ -2450,8 +2450,8 @@ targetIsInSortList(TargetEntry *tle, Oid sortop, List *sortList) /* * targetExistsInAllQueryPartitionByClauses - * returns true if the given Var exists in the PARTITION BY clause in every - * WINDOW defined in the subquery + * returns true if the given TargetEntry exists in the PARTITION BY clause + * in every WINDOW defined in the query. */ bool targetExistsInAllQueryPartitionByClauses(TargetEntry *tle, Query *query) diff --git a/src/test/regress/expected/window.out b/src/test/regress/expected/window.out index 9f93593..f0c3d0e 100644 --- a/src/test/regress/expected/window.out +++ b/src/test/regress/expected/window.out @@ -80,44 +80,6 @@ WHERE depname = 'sales'; Filter: ((depname)::text = 'sales'::text) (7 rows) --- all sub queries contain depname, so it can be pushed down. (similar to above but columns in different order) -EXPLAIN (COSTS OFF) -SELECT * FROM (SELECT depname, - sum(salary) OVER (PARTITION BY depname) depsalary, - min(salary) OVER (PARTITION BY depname || 'A',depname || 'B',depname) depminsalary - FROM empsalary) emp -WHERE depname = 'sales'; - QUERY PLAN ----------------------------------------------------------------------------------------------------------------- - Subquery Scan on emp - -> WindowAgg - -> Sort - Sort Key: (((empsalary.depname)::text || 'A'::text)), (((empsalary.depname)::text || 'B'::text)) - -> WindowAgg - -> Seq Scan on empsalary - Filter: ((depname)::text = 'sales'::text) -(7 rows) - --- one sub query does not contain depname, so it cannot be pushed down. -EXPLAIN (COSTS OFF) -SELECT * FROM (SELECT depname, - sum(salary) OVER (PARTITION BY depname) depsalary, - min(salary) OVER (PARTITION BY depname || 'A',depname || 'B') depminsalary - FROM empsalary) emp -WHERE depname = 'sales'; - QUERY PLAN ----------------------------------------------------------------------------------------------------------------- - Subquery Scan on emp - Filter: ((emp.depname)::text = 'sales'::text) - -> WindowAgg - -> Sort - Sort Key: (((empsalary.depname)::text || 'A'::text)), (((empsalary.depname)::text || 'B'::text)) - -> WindowAgg - -> Sort - Sort Key: empsalary.depname - -> Seq Scan on empsalary -(9 rows) - -- empty window clause, so cannot push outer where into subquery. EXPLAIN (COSTS OFF) SELECT * FROM (SELECT depname,sum(salary) OVER () depsalary FROM empsalary) emp WHERE depname = 'sales'; @@ -147,23 +109,6 @@ WHERE depname = 'sales'; Filter: ((depname)::text = 'sales'::text) (7 rows) --- an unused window currently stops the pushdown from happening. (This could probably be optimized). -EXPLAIN (COSTS OFF) -SELECT depname,depsalary FROM (SELECT depname, - sum(salary) OVER (PARTITION BY depname) depsalary, - rank() OVER (ORDER BY enroll_date) emprank - FROM empsalary) emp -WHERE depname = 'sales'; - QUERY PLAN -------------------------------------------------- - Subquery Scan on emp - Filter: ((emp.depname)::text = 'sales'::text) - -> WindowAgg - -> Sort - Sort Key: empsalary.depname - -> Seq Scan on empsalary -(6 rows) - SELECT depname, empno, salary, sum(salary) OVER (PARTITION BY depname) FROM empsalary ORDER BY depname, salary; depname | empno | salary | sum -----------+-------+--------+------- diff --git a/src/test/regress/sql/window.sql b/src/test/regress/sql/window.sql index c2d1c60..89cac32 100644 --- a/src/test/regress/sql/window.sql +++ b/src/test/regress/sql/window.sql @@ -47,22 +47,6 @@ SELECT * FROM (SELECT depname, FROM empsalary) emp WHERE depname = 'sales'; --- all sub queries contain depname, so it can be pushed down. (similar to above but columns in different order) -EXPLAIN (COSTS OFF) -SELECT * FROM (SELECT depname, - sum(salary) OVER (PARTITION BY depname) depsalary, - min(salary) OVER (PARTITION BY depname || 'A',depname || 'B',depname) depminsalary - FROM empsalary) emp -WHERE depname = 'sales'; - --- one sub query does not contain depname, so it cannot be pushed down. -EXPLAIN (COSTS OFF) -SELECT * FROM (SELECT depname, - sum(salary) OVER (PARTITION BY depname) depsalary, - min(salary) OVER (PARTITION BY depname || 'A',depname || 'B') depminsalary - FROM empsalary) emp -WHERE depname = 'sales'; - -- empty window clause, so cannot push outer where into subquery. EXPLAIN (COSTS OFF) SELECT * FROM (SELECT depname,sum(salary) OVER () depsalary FROM empsalary) emp WHERE depname = 'sales'; @@ -75,14 +59,6 @@ SELECT * FROM (SELECT depname, FROM empsalary) emp WHERE depname = 'sales'; --- an unused window currently stops the pushdown from happening. (This could probably be optimized). -EXPLAIN (COSTS OFF) -SELECT depname,depsalary FROM (SELECT depname, - sum(salary) OVER (PARTITION BY depname) depsalary, - rank() OVER (ORDER BY enroll_date) emprank - FROM empsalary) emp -WHERE depname = 'sales'; - SELECT depname, empno, salary, sum(salary) OVER (PARTITION BY depname) FROM empsalary ORDER BY depname, salary; SELECT depname, empno, salary, rank() OVER (PARTITION BY depname ORDER BY salary) FROM empsalary;