Issue Details (XML | Word | Printable)

Key: DERBY-2351
Type: Bug Bug
Status: Resolved Resolved
Resolution: Fixed
Priority: Major Major
Assignee: Bryan Pendleton
Reporter: Yip Ng
Votes: 1
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
Derby

ORDER BY with expression with distinct in the select list returns incorrect result

Created: 17/Feb/07 12:36 AM   Updated: 30/Jun/09 04:12 PM
Return to search
Component/s: SQL
Affects Version/s: 10.2.1.6, 10.2.2.0, 10.3.1.4
Fix Version/s: 10.3.3.0, 10.4.1.3, 10.5.1.1

Time Tracking:
Not Specified

File Attachments:
  Size
File Licensed for inclusion in ASF works d2351_aliasing.diff 2008-03-04 04:34 AM Bryan Pendleton 9 kB
File Licensed for inclusion in ASF works d2351_aliasing.diff 2008-03-02 10:42 PM Bryan Pendleton 7 kB
File Licensed for inclusion in ASF works d2351_aliasing_checkQualifiedName.diff 2008-03-06 04:29 AM Bryan Pendleton 11 kB
File Licensed for inclusion in ASF works derby_2351.diff 2007-06-17 03:30 AM Bryan Pendleton 9 kB
File Licensed for inclusion in ASF works derby_2351_v2.diff 2007-06-17 07:42 PM Bryan Pendleton 10 kB
File Licensed for inclusion in ASF works modifySynonymResults.diff 2008-03-08 08:45 PM Bryan Pendleton 12 kB
HTML File Licensed for inclusion in ASF works releaseNote.html 2008-04-01 01:29 PM Rick Hillegas 6 kB
HTML File Licensed for inclusion in ASF works releaseNote.html 2008-03-20 02:42 PM Bryan Pendleton 6 kB
HTML File Licensed for inclusion in ASF works releaseNote.html 2008-03-20 05:20 AM Bryan Pendleton 4 kB
File Licensed for inclusion in ASF works reproTests.diff 2007-06-10 09:45 PM Bryan Pendleton 5 kB
Environment: Any
Issue Links:
Reference

Issue & fix info: Release Note Needed
Resolution Date: 16/Mar/08 03:51 AM


 Description  « Hide
When distinct is in the select list and the query has order by with expression, the resultset produced contains an additional column.

ij> create table t1 (c1 int, c2 varchar(10))
0 rows inserted/updated/deleted
ij> insert into t1 values (1,'a'),(2,'b'),(3,'c');
3 rows inserted/updated/deleted
select distinct c1, c2 from t1 order by c1;
C1 |C2
----------------------
1 |a
2 |b
3 |c

3 rows selected
ij> select distinct c1, c2 from t1 order by c1+1;
C1 |C2 |3 <=====returns 3 columns, incorrect result returned
----------------------------------
1 |a |2
2 |b |3
3 |c |4

3 rows selected


 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Repository Revision Date User Message
ASF #555096 Tue Jul 10 22:06:02 UTC 2007 bpendleton DERBY-2351: Certain ORDER BY clauses should be rejected as invalid

This change modifies the ORDER BY clause so that it rejects certain
queries as invalid: specifically, queries which:
a) specify the set quantifier DISTINCT,
b) and also contain an ORDER BY clause which refers to a column
or expression which is not in the query result.

The problem with such queries is that we are told to return only
a single instance of the DISTINCT columns, but since the ORDER BY
clause refers to columns which are not in the DISTINCT set, if there
should be multiple candidate rows from which we choose the DISTINCT
result, we don't know which of those rows to use for the ORDER BY
processing.

When the DISTINCT and ORDER BY clauses are in conflict, Derby should
reject the query. This change modifies Derby to do so.
Files Changed
MODIFY /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/orderby.sql
MODIFY /db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/SQLState.java
MODIFY /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/master/orderby.out
MODIFY /db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/OrderByColumn.java
MODIFY /db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml

Repository Revision Date User Message
ASF #565184 Sun Aug 12 22:24:44 UTC 2007 bpendleton DERBY-2351: Some ambiguous ORDER BY queries are not rejected as invalid.

Merged the change from the trunk via svn merge -r 555095:555096
Files Changed
MODIFY /db/derby/code/branches/10.3/java/testing/org/apache/derbyTesting/functionTests/tests/lang/orderby.sql
MODIFY /db/derby/code/branches/10.3/java/shared/org/apache/derby/shared/common/reference/SQLState.java
MODIFY /db/derby/code/branches/10.3/java/testing/org/apache/derbyTesting/functionTests/master/orderby.out
MODIFY /db/derby/code/branches/10.3/java/engine/org/apache/derby/impl/sql/compile/OrderByColumn.java
MODIFY /db/derby/code/branches/10.3/java/engine/org/apache/derby/loc/messages.xml

Repository Revision Date User Message
ASF #636608 Thu Mar 13 03:01:14 UTC 2008 bpendleton DERBY-2351: ORDER BY with expression with distinct in SELECT list.

This is a follow-on patch for DERBY-2351, which addresses a number of
problems involving column aliasing that occurred with the initial
DERBY-2351 patch. After the initial DERBY-2351 patch, queries such as
the following one failed:

   select distinct c1 as a1 from t order by c1

This patch modifies the ORDER BY column matching algorithm to
match column references using multiple rules, depending on
whether or not the ORDER BY reference is qualified or not:
 - unqualified references are matched first against the alias name,
   then against the underlying column name from the source table
 - qualified references are matched only against the underlying
   specified table

A number of additional test cases are added as part of this patch to verify
that the above query, and other similar queries, now work as intended.

This patch rejects at least one query known to have worked before:

    select t1.id as idc1, t1.id as idc2 from t1 order by t1.idc1, t1.idc2;

After discussion with Derby developers, this query was felt to
be invalid, as there is no such column "idc1" in table "t1". The query
should *not* qualify the ORDER BY column references with table "t1":

    select t1.id as idc1, t1.id as idc2 from t1 order by idc1, idc2;
Files Changed
MODIFY /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/orderby.sql
MODIFY /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/master/orderby.out
MODIFY /db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ResultColumn.java
MODIFY /db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ResultColumnList.java
MODIFY /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/master/synonym.out

Repository Revision Date User Message
ASF #637526 Sun Mar 16 03:50:31 UTC 2008 bpendleton DERBY-2351: ORDER BY with expression with distinct in SELECT list.

Merged by svn merge -r 636607:636608 ../trunk/
Files Changed
MODIFY /db/derby/code/branches/10.4/java/testing/org/apache/derbyTesting/functionTests/tests/lang/orderby.sql
MODIFY /db/derby/code/branches/10.4/java/testing/org/apache/derbyTesting/functionTests/master/orderby.out
MODIFY /db/derby/code/branches/10.4/java/engine/org/apache/derby/impl/sql/compile/ResultColumn.java
MODIFY /db/derby/code/branches/10.4/java/engine/org/apache/derby/impl/sql/compile/ResultColumnList.java
MODIFY /db/derby/code/branches/10.4/java/testing/org/apache/derbyTesting/functionTests/master/synonym.out

Repository Revision Date User Message
ASF #637529 Sun Mar 16 04:00:43 UTC 2008 bpendleton DERBY-3373: SQL distinct and order by needed together.

Derby was issuing the error message "The ORDER BY clause may not specify
an expression, since the query specifies DISTINCT" in situations where
the message was not appropriate. An example of a query that should have
been accepted, but was being rejected, is:

   select name from person order by lower(name)

This patch backs out part of the changes that went in with DERBY-2351;
specifically, this patch reverts the "if" test which was rejecting any
DISTINCT query which had a pulled-up ORDER BY expression. Not all such
expressions are invalid, as the above example shows. Since the "if"
statement cannot distinguish between valid expressions and invalid ones,
it is better to revert to the pre-DERBY-2351 behavior.

A few new tests are also added to demonstrate the behavior.
Files Changed
MODIFY /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/orderby.sql
MODIFY /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/master/orderby.out
MODIFY /db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/OrderByColumn.java

Repository Revision Date User Message
ASF #639696 Fri Mar 21 16:19:11 UTC 2008 bpendleton DERBY-2351: ORDER BY with expression with distinct in SELECT list.

Merged revision 637526 from the 10.4 branch. orderby.out had a conflict
and was hand-merged.
Files Changed
MODIFY /db/derby/code/branches/10.3/java/testing/org/apache/derbyTesting/functionTests/tests/lang/orderby.sql
MODIFY /db/derby/code/branches/10.3/java/testing/org/apache/derbyTesting/functionTests/master/orderby.out
MODIFY /db/derby/code/branches/10.3/java/engine/org/apache/derby/impl/sql/compile/ResultColumn.java
MODIFY /db/derby/code/branches/10.3/java/engine/org/apache/derby/impl/sql/compile/ResultColumnList.java
MODIFY /db/derby/code/branches/10.3/java/testing/org/apache/derbyTesting/functionTests/master/synonym.out

Repository Revision Date User Message
ASF #650995 Wed Apr 23 18:40:41 UTC 2008 kmarsden Update master for DERBY-2351
Files Changed
MODIFY /db/derby/code/branches/10.3.2.1_test/java/testing/org/apache/derbyTesting/functionTests/master/synonym.out