db-derby-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Knut Anders Hatlen (JIRA)" <j...@apache.org>
Subject [jira] Commented: (DERBY-3634) Cannot use row_number() in ORDER BY clause
Date Thu, 15 Oct 2009 21:01:32 GMT

    [ https://issues.apache.org/jira/browse/DERBY-3634?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12766247#action_12766247

Knut Anders Hatlen commented on DERBY-3634:

Logged as DERBY-4411.

By the way, the syntax rules in SQL:2003, part 2, 14.1 <declare cursor> say

18) If an <order by clause> is specified, then:
a) Let OBC be the <order by clause>. Let NSK be the number of <sort specification>s
in OBC. For each
   i between 1 (one) and NSK, let Ki be the <sort key> contained in the i-th <sort
specification> in OBC.
b) Each Ki shall contain a <column reference> and shall not contain a <subquery>
or a <set function

What exactly does this mean? ROW_NUMBER() OVER () doesn't contain a <column reference>,
so should ORDER BY ROW_NUMBER() OVER () be rejected because of this? On the other hand, (ROW_NUMBER()
OVER () + COL1) does contain a <column reference>, so ORDER BY (ROW_NUMBER() OVER ()
+ COL1) would be fine? I'm sure I'm missing something, but what?

> Cannot use row_number() in ORDER BY clause
> ------------------------------------------
>                 Key: DERBY-3634
>                 URL: https://issues.apache.org/jira/browse/DERBY-3634
>             Project: Derby
>          Issue Type: Bug
>          Components: SQL
>    Affects Versions:
>            Reporter: Rick Hillegas
>            Assignee: Dag H. Wanvik
>         Attachments: derby-3634-a.diff, derby-3634-a.stat, derby-3634-a.txt, derby-3634-b.diff,
derby-3634-b.stat, derby-3634-c.diff, derby-3634-c.stat, derby-3634-newimpl-1.diff, derby-3634-newimpl-1.stat,
derby-3634-newimpl-1.txt, derby-3634-newimpl-2.diff, derby-3634-newimpl-2.stat, derby-3634-newimpl-3.diff,
derby-3634-newimpl-3.stat, derby-3634-newimpl-4.diff, derby-3634-newimpl-4.stat, derby-3634-remove-2.diff,
derby-3634-remove-2.stat, derby-3634-remove.diff, derby-3634-remove.stat
> The following query works correctly:
> select abs(a), row_number() over ()
> from t
> where a > 100 and a < 111
> order by abs(a)
> I expected the following query to also work, but it raised an exception:
> select abs(a), row_number() over ()
> from t
> where a > 100 and a < 111
> order by row_number() over ()
> This is the error I saw: "ERROR 42X01: Syntax error: Encountered "over" at line 5, column
> Here are the reasons why I think that this syntax is supposed to be supported:
> According to my reading of the 2003 SQL spec, the ORDER BY clause should be able to sort
on any expression in the SELECT list. That includes OLAP expressions. I believe this is so
because, according to part 2, section 10.10 (<sort specification>), a <sort key>
can be any <value expression> and if you follow the grammar for <value expression>,
it can resolve to be a <value expression primary> (see section 6.3), which can in turn
resolve to be a <window function>. This reasoning is supported by tracing the hotlinks
on the following page which lays out the SQL 2003 BNF: http://savage.net.au/SQL/sql-2003-2.bnf.html
This interpretation is further supported by the example of an ORDER BY clause referencing
an OLAP expression which is provided on page 23 of the introduction to OLAP written by Fred
Zemke, Krishna Kulkarni, Andy Witkowski, and Bob Lyle: www.cse.iitb.ac.in/dbms/Data/Papers-Other/SQL1999/OLAP-99-154r2.pdf

This message is automatically generated by JIRA.
You can reply to this email to add a comment to the issue online.

View raw message