From a20993608a65b9896b4c05cb7061bc60d6f1840a Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Thu, 13 Sep 2012 11:31:40 -0400 Subject: Fix case of window function + aggregate + GROUP BY expression. In commit 1bc16a946008a7cbb33a9a06a7c6765a807d7f59 I added a minor optimization to drop the component variables of a GROUP BY expression from the target list computed at the aggregation level of a query, if those Vars weren't referenced elsewhere in the tlist. However, I overlooked that the window-function planning code would deconstruct such expressions and thus need to have access to their component variables. Fix it to not do that. While at it, I removed the distinction between volatile and nonvolatile window partition/order expressions: the code now computes all of them at the aggregation level. This saves a relatively expensive check for volatility, and it's unclear that the resulting plan isn't better anyway. Per bug #7535 from Louis-David Mitterrand. Back-patch to 9.2. --- src/test/regress/expected/window.out | 12 ++++++++++++ src/test/regress/sql/window.sql | 6 ++++++ 2 files changed, 18 insertions(+) (limited to 'src/test') diff --git a/src/test/regress/expected/window.out b/src/test/regress/expected/window.out index 77786266255..49252c82ae3 100644 --- a/src/test/regress/expected/window.out +++ b/src/test/regress/expected/window.out @@ -614,6 +614,18 @@ group by ten order by ten; 9 | 10040184 | 7 (10 rows) +-- window and aggregate with GROUP BY expression (9.2 bug) +explain (costs off) +select first_value(max(x)) over (), y + from (select unique1 as x, ten+four as y from tenk1) ss + group by y; + QUERY PLAN +------------------------------- + WindowAgg + -> HashAggregate + -> Seq Scan on tenk1 +(3 rows) + -- test non-default frame specifications SELECT four, ten, sum(ten) over (partition by four order by ten), diff --git a/src/test/regress/sql/window.sql b/src/test/regress/sql/window.sql index d8e9e7e3b1c..769be0fdc61 100644 --- a/src/test/regress/sql/window.sql +++ b/src/test/regress/sql/window.sql @@ -145,6 +145,12 @@ select ten, from tenk1 group by ten order by ten; +-- window and aggregate with GROUP BY expression (9.2 bug) +explain (costs off) +select first_value(max(x)) over (), y + from (select unique1 as x, ten+four as y from tenk1) ss + group by y; + -- test non-default frame specifications SELECT four, ten, sum(ten) over (partition by four order by ten), -- cgit v1.2.3