From 3f2fa308d14635a3936c0b62f8f05631d6362a27 Mon Sep 17 00:00:00 2001
From: Tom Lane
Date: Thu, 27 Aug 2009 20:08:12 +0000
Subject: Modify the definition of window-function PARTITION BY and ORDER BY
clauses so that their elements are always taken as simple expressions over
the query's input columns. It originally seemed like a good idea to make
them act exactly like GROUP BY and ORDER BY, right down to the SQL92-era
behavior of accepting output column names or numbers. However, that was not
such a great idea, for two reasons:
1. It permits circular references, as exhibited in bug #5018: the output
column could be the one containing the window function itself. (We actually
had a regression test case illustrating this, but nobody thought twice about
how confusing that would be.)
2. It doesn't seem like a good idea for, eg, "lead(foo) OVER (ORDER BY foo)"
to potentially use two completely different meanings for "foo".
Accordingly, narrow down the behavior of window clauses to use only the
SQL99-compliant interpretation that the expressions are simple expressions.
---
doc/src/sgml/ref/select.sgml | 21 ++++++++++++++-------
doc/src/sgml/syntax.sgml | 6 ++++--
2 files changed, 18 insertions(+), 9 deletions(-)
(limited to 'doc/src')
diff --git a/doc/src/sgml/ref/select.sgml b/doc/src/sgml/ref/select.sgml
index 9675adcbf86..96442a93084 100644
--- a/doc/src/sgml/ref/select.sgml
+++ b/doc/src/sgml/ref/select.sgml
@@ -1,5 +1,5 @@
@@ -595,17 +595,24 @@ WINDOW window_name AS (
The elements of the PARTITION BY> list are interpreted in
- the same fashion as elements of a
- , and
- the elements of the ORDER BY> list are interpreted in the
- same fashion as elements of an
- .
- The only difference is that these expressions can contain aggregate
+ much the same fashion as elements of a
+ , except that
+ they are always simple expressions and never the name or number of an
+ output column.
+ Another difference is that these expressions can contain aggregate
function calls, which are not allowed in a regular GROUP BY>
clause. They are allowed here because windowing occurs after grouping
and aggregation.
+
+ Similarly, the elements of the ORDER BY> list are interpreted
+ in much the same fashion as elements of an
+ , except that
+ the expressions are always taken as simple expressions and never the name
+ or number of an output column.
+
+
The optional frame_clause> defines
the window frame> for window functions that depend on the
diff --git a/doc/src/sgml/syntax.sgml b/doc/src/sgml/syntax.sgml
index bc562e1f97b..db3d9982d8e 100644
--- a/doc/src/sgml/syntax.sgml
+++ b/doc/src/sgml/syntax.sgml
@@ -1,4 +1,4 @@
-
+
SQL Syntax
@@ -1619,7 +1619,9 @@ ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
expression that does not itself contain window function calls.
The PARTITION BY> and ORDER BY> lists have
essentially the same syntax and semantics as GROUP BY>
- and ORDER BY> clauses of the whole query.
+ and ORDER BY> clauses of the whole query, except that their
+ expressions are always just expressions and cannot be output-column
+ names or numbers.
window_name is a reference to a named window
specification defined in the query's WINDOW clause.
Named window specifications are usually referenced with just
--
cgit v1.2.3