Return-Path: X-Original-To: apmail-db-derby-commits-archive@www.apache.org Delivered-To: apmail-db-derby-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id D3844D1DA for ; Tue, 25 Sep 2012 22:56:45 +0000 (UTC) Received: (qmail 88542 invoked by uid 500); 25 Sep 2012 22:56:45 -0000 Delivered-To: apmail-db-derby-commits-archive@db.apache.org Received: (qmail 88517 invoked by uid 500); 25 Sep 2012 22:56:45 -0000 Mailing-List: contact derby-commits-help@db.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: Reply-To: "Derby Development" List-Id: Delivered-To: mailing list derby-commits@db.apache.org Received: (qmail 88510 invoked by uid 99); 25 Sep 2012 22:56:45 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 25 Sep 2012 22:56:45 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 25 Sep 2012 22:56:44 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 2FAB323888E3; Tue, 25 Sep 2012 22:56:01 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1390205 - in /db/derby/code/trunk/java: engine/org/apache/derby/impl/sql/compile/ColumnReference.java testing/org/apache/derbyTesting/functionTests/tests/lang/JoinTest.java Date: Tue, 25 Sep 2012 22:56:01 -0000 To: derby-commits@db.apache.org From: dag@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120925225601.2FAB323888E3@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: dag Date: Tue Sep 25 22:56:00 2012 New Revision: 1390205 URL: http://svn.apache.org/viewvc?rev=1390205&view=rev Log: DERBY-5933 SQL sorting error Patch "d5933-remap+test", which fixes a problem in column reference remapping in connection with flattening of a left outer join with a base table. See the Javadoc for the new test case JoinTest#testDerby_5933 for details. Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ColumnReference.java db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/JoinTest.java Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ColumnReference.java URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ColumnReference.java?rev=1390205&r1=1390204&r2=1390205&view=diff ============================================================================== --- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ColumnReference.java (original) +++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ColumnReference.java Tue Sep 25 22:56:00 2012 @@ -718,7 +718,10 @@ public class ColumnReference extends Val /* Find the matching ResultColumn */ source = getSourceResultColumn(); columnName = source.getName(); - columnNumber = source.getColumnPosition(); + // Use the virtual column id if the ResultColumn's expression + // is a virtual column (DERBY-5933). + columnNumber = source.getExpression() instanceof VirtualColumnNode ? + source.getVirtualColumnId() : source.getColumnPosition(); if (source.getExpression() instanceof ColumnReference) { Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/JoinTest.java URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/JoinTest.java?rev=1390205&r1=1390204&r2=1390205&view=diff ============================================================================== --- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/JoinTest.java (original) +++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/JoinTest.java Tue Sep 25 22:56:00 2012 @@ -2055,4 +2055,50 @@ public class JoinTest extends BaseJDBCTe rollback(); } + + /** + * DERBY-5933. Error in column reference remapping in connection + * with flattening of a left outer join with a base table "d", cf + * test case below. In the example, the predicate b1=1 is pushed + * into the LOJ node, and in that connection the column reference + * to "b1" is remapped. The generated table number of the LOJ node + * is 4, and the correct column number of "b1" should be 5, i.e. + * [a1: 1, a2: 2, a3: 3, a4: 4, b1:5, c1:6]. However, the + * remapping logic erroneously picked b1's column number from its + * base table, 1, which really is the position of a1. Now, since + * b1 is constant, the column reference b1 "alias" a1 gets marked + * as such. Since we are ordering on a1, the sort avoidance logic + * is led to believe a1 is constant, and hence sorting is skipped, + * hence the wrong result. For related issues, see DERBY-4679, + * DERBY-4695, DERBY-3023, DERBY-2526. + */ + + public void testDerby_5933() throws SQLException { + setAutoCommit(false); + + Statement s = createStatement(); + + s.executeUpdate("create table a (a1 int, a2 int, a3 int, a4 int)"); + s.executeUpdate("create table b (b1 int)"); + s.executeUpdate("create table c (c1 int)"); + s.executeUpdate("create table d (d1 int)"); + s.executeUpdate("insert into a values (1,2,1,2), (2,3,1,3), (1,4,1,4)"); + s.executeUpdate("insert into b values 1"); + s.executeUpdate("insert into d values 2,3,4"); + + ResultSet rs = s.executeQuery("select a1 from " + + "a inner join b on a3 = b1 " + + " left outer join c on a4 = c1 " + + " inner join d on a2 = d1 " + + "where b1 = 1 " + + "order by a1"); + + JDBC.assertFullResultSet(rs, new String[][] { + {"1"}, + {"1"}, + {"2"}}); + + rollback(); + } + }