Skip to content

Commit 599942c

Browse files
committed
Improve the error message given for modifying a window with frame clause.
For rather inscrutable reasons, SQL:2008 disallows copying-and-modifying a window definition that has any explicit framing clause. The error message we gave for this only made sense if the referencing window definition itself contains an explicit framing clause, which it might well not. Moreover, in the context of an OVER clause it's not exactly obvious that "OVER (windowname)" implies copy-and-modify while "OVER windowname" does not. This has led to multiple complaints, eg bug #5199 from Iliya Krapchatov. Change to a hopefully more intelligible error message, and in the case where we have just "OVER (windowname)", add a HINT suggesting that omitting the parentheses will fix it. Also improve the related documentation. Back-patch to all supported branches.
1 parent 85de126 commit 599942c

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed

doc/src/sgml/syntax.sgml

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,10 +1709,10 @@ SELECT string_agg(a ORDER BY a, ',') FROM table; -- incorrect
17091709
The syntax of a window function call is one of the following:
17101710

17111711
<synopsis>
1712-
<replaceable>function_name</replaceable> (<optional><replaceable>expression</replaceable> <optional>, <replaceable>expression</replaceable> ... </optional></optional>) OVER ( <replaceable class="parameter">window_definition</replaceable> )
17131712
<replaceable>function_name</replaceable> (<optional><replaceable>expression</replaceable> <optional>, <replaceable>expression</replaceable> ... </optional></optional>) OVER <replaceable>window_name</replaceable>
1714-
<replaceable>function_name</replaceable> ( * ) OVER ( <replaceable class="parameter">window_definition</replaceable> )
1713+
<replaceable>function_name</replaceable> (<optional><replaceable>expression</replaceable> <optional>, <replaceable>expression</replaceable> ... </optional></optional>) OVER ( <replaceable class="parameter">window_definition</replaceable> )
17151714
<replaceable>function_name</replaceable> ( * ) OVER <replaceable>window_name</replaceable>
1715+
<replaceable>function_name</replaceable> ( * ) OVER ( <replaceable class="parameter">window_definition</replaceable> )
17161716
</synopsis>
17171717
where <replaceable class="parameter">window_definition</replaceable>
17181718
has the syntax
@@ -1749,15 +1749,14 @@ UNBOUNDED FOLLOWING
17491749
names or numbers.
17501750
<replaceable>window_name</replaceable> is a reference to a named window
17511751
specification defined in the query's <literal>WINDOW</literal> clause.
1752-
Named window specifications are usually referenced with just
1753-
<literal>OVER</> <replaceable>window_name</replaceable>, but it is
1754-
also possible to write a window name inside the parentheses and then
1755-
optionally supply an ordering clause and/or frame clause (the referenced
1756-
window must lack these clauses, if they are supplied here).
1757-
This latter syntax follows the same rules as modifying an existing
1758-
window name within the <literal>WINDOW</literal> clause; see the
1759-
<xref linkend="sql-select"> reference
1760-
page for details.
1752+
Alternatively, a full <replaceable>window_definition</replaceable> can
1753+
be given within parentheses, using the same syntax as for defining a
1754+
named window in the <literal>WINDOW</literal> clause; see the
1755+
<xref linkend="sql-select"> reference page for details. It's worth
1756+
pointing out that <literal>OVER wname</> is not exactly equivalent to
1757+
<literal>OVER (wname)</>; the latter implies copying and modifying the
1758+
window definition, and will be rejected if the referenced window
1759+
specification includes a frame clause.
17611760
</para>
17621761

17631762
<para>

src/backend/parser/parse_clause.c

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,11 +1658,16 @@ transformWindowDefinitions(ParseState *pstate,
16581658
/*
16591659
* Per spec, a windowdef that references a previous one copies the
16601660
* previous partition clause (and mustn't specify its own). It can
1661-
* specify its own ordering clause. but only if the previous one had
1661+
* specify its own ordering clause, but only if the previous one had
16621662
* none. It always specifies its own frame clause, and the previous
1663-
* one must not have a frame clause. (Yeah, it's bizarre that each of
1663+
* one must not have a frame clause. Yeah, it's bizarre that each of
16641664
* these cases works differently, but SQL:2008 says so; see 7.11
1665-
* <window clause> syntax rule 10 and general rule 1.)
1665+
* <window clause> syntax rule 10 and general rule 1. The frame
1666+
* clause rule is especially bizarre because it makes "OVER foo"
1667+
* different from "OVER (foo)", and requires the latter to throw an
1668+
* error if foo has a nondefault frame clause. Well, ours not to
1669+
* reason why, but we do go out of our way to throw a useful error
1670+
* message for such cases.
16661671
*/
16671672
if (refwc)
16681673
{
@@ -1701,11 +1706,27 @@ transformWindowDefinitions(ParseState *pstate,
17011706
wc->copiedOrder = false;
17021707
}
17031708
if (refwc && refwc->frameOptions != FRAMEOPTION_DEFAULTS)
1709+
{
1710+
/*
1711+
* Use this message if this is a WINDOW clause, or if it's an OVER
1712+
* clause that includes ORDER BY or framing clauses. (We already
1713+
* rejected PARTITION BY above, so no need to check that.)
1714+
*/
1715+
if (windef->name ||
1716+
orderClause || windef->frameOptions != FRAMEOPTION_DEFAULTS)
1717+
ereport(ERROR,
1718+
(errcode(ERRCODE_WINDOWING_ERROR),
1719+
errmsg("cannot copy window \"%s\" because it has a frame clause",
1720+
windef->refname),
1721+
parser_errposition(pstate, windef->location)));
1722+
/* Else this clause is just OVER (foo), so say this: */
17041723
ereport(ERROR,
17051724
(errcode(ERRCODE_WINDOWING_ERROR),
1706-
errmsg("cannot override frame clause of window \"%s\"",
1707-
windef->refname),
1725+
errmsg("cannot copy window \"%s\" because it has a frame clause",
1726+
windef->refname),
1727+
errhint("Omit the parentheses in this OVER clause."),
17081728
parser_errposition(pstate, windef->location)));
1729+
}
17091730
wc->frameOptions = windef->frameOptions;
17101731
/* Process frame offset expressions */
17111732
wc->startOffset = transformFrameOffset(pstate, wc->frameOptions,

0 commit comments

Comments
 (0)