[ 
https://issues.apache.org/jira/browse/CALCITE-5863?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17746031#comment-17746031
 ] 

Itiel Sadeh commented on CALCITE-5863:
--------------------------------------

I've tried to figure it out, which is not a simple task.. I've found link to 
the last draft of SQL 2003 standard in PostgreSQL's wiki ([here 
title|[https://wiki.postgresql.org/wiki/Developer_FAQ#Where_can_I_get_a_copy_of_the_SQL_standards.3F],]
 and in there in section `7.1` "window clause", 
syntax rules 11 (page 333 in SQL/foundation) it states: 
{quote}If WDEF's <window frame clause> specifies <window frame preceding> or 
<window frame following>,
then let UVS be the <unsigned value specification> simply contained in the 
<window frame preceding> or
<window frame following>.
Case:
a) If RANGE is specified, then WDEF's <window order clause> shall contain a 
single <sort key> SK.
The declared type of SK shall be numeric, datetime, or interval. The declared 
type of UVS shall be
numeric if the declared type of SK is numeric; otherwise, it shall be an 
interval type that may be added
to or subtracted from the declared type of SK according to the Syntax Rules of 
Subclause 6.30,
“<datetime value expression>”, and Subclause 6.32, “<interval value 
expression>”, in this part of
ISO/IEC 9075.
b) If ROWS is specified, then the declared type of UVS shall be exact numeric 
with scale 0 (zero).
{quote}

Which seems to indicate that the `ORDER BY` should contain a single key only if 
there's a `preceding` or `following` call.


> Calcite rejects valid query with multiple ORDER BY columns and constant RANGE 
> bounds in window functions
> --------------------------------------------------------------------------------------------------------
>
>                 Key: CALCITE-5863
>                 URL: https://issues.apache.org/jira/browse/CALCITE-5863
>             Project: Calcite
>          Issue Type: Bug
>    Affects Versions: 1.34.0
>            Reporter: Itiel Sadeh
>            Priority: Major
>              Labels: pull-request-available
>
> Usually, it is not valid to specify multiple ORDER BY columns with RANGE 
> bounds in window functions:
> {code:java}
> SELECT sum(x) OVER (ORDER BY x,y RANGE BETWEEN 3 preceding and 4 following) 
> from t;{code}
> However, it is valid if both bounds are "constant" bound (CURRENT ROW, 
> UNBOUNDED PRECEDING and UNBOUNDED FOLLOWING), i.e:
> {code:java}
> SELECT sum(x) OVER (ORDER BY x,y RANGE BETWEEN UNBOUNDED PRECEDING and 
> CURRENT ROW) from t;{code}
> (tested on PostgreSQL and SQL Server)
> Calcite will incorrectly reject it. 



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to