You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by da...@apache.org on 2010/06/23 18:17:42 UTC

svn commit: r957260 - in /db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile: ColumnReference.java ResultColumnList.java

Author: dag
Date: Wed Jun 23 16:17:41 2010
New Revision: 957260

URL: http://svn.apache.org/viewvc?rev=957260&view=rev
Log:
DERBY-4679 Several left outer joins causes unstable query with incorrect results

Follow-up patch derby-4679-followup, which makes the original patch
safer by also matching the column name once a candidate result column
has been located using the table number and column number pair to
match an RC. This is to safe-guard against false matches, since
DERBY-4595 shows that the column number can be wrong in certain
situations.


Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ColumnReference.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ResultColumnList.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=957260&r1=957259&r2=957260&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 Wed Jun 23 16:17:41 2010
@@ -887,7 +887,8 @@ public class ColumnReference extends Val
                 // the wrong column. DERBY-4679.
                 ftRC = rcl.getResultColumn(
                     tableNumberBeforeFlattening,
-                    columnNumberBeforeFlattening);
+                    columnNumberBeforeFlattening,
+                    columnName);
 
                 if (ftRC == null) {
                     // The above lookup won't work for references to a base

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ResultColumnList.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ResultColumnList.java?rev=957260&r1=957259&r2=957260&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ResultColumnList.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ResultColumnList.java Wed Jun 23 16:17:41 2010
@@ -321,11 +321,19 @@ public class ResultColumnList extends Qu
      * table may appear multiple times in the queries with separate correlation
      * names, and/or column names from different tables may be the same (hence
      * looking up by column name will not always work), cf DERBY-4679.
+     * <p/>
+     * {@code columnName} is used to assert that we find the right column.
+     * If we found a match on (tn, cn) but columnName is wrong, return null.
+     * Once we trust table numbers and column numbers to always be correct,
+     * cf. DERBY-4695, we can remove this parameter.
      *
      * @param tableNumber the table number to look for
      * @param columnNumber the column number to look for
+     * @param columnName name of the desired column
      */
-    public ResultColumn getResultColumn(int tableNumber, int columnNumber)
+    public ResultColumn getResultColumn(int tableNumber,
+                                        int columnNumber,
+                                        String columnName)
     {
         int size = size();
 
@@ -347,10 +355,28 @@ public class ResultColumnList extends Qu
                         if (ft.getTableNumber() == tableNumber &&
                                 rc.getColumnPosition() == columnNumber) {
 
-                            // Found matching (t,c) within this top resultColumn
-                            resultColumn.setReferenced();
-                            return resultColumn;
-
+                            // Found matching (t,c) within this top
+                            // resultColumn. Now do sanity check that column
+                            // name is correct. Remove when DERBY-4695 is
+                            // fixed.
+                            if (columnName.equals(
+                                        vcn.getSourceColumn().getName())) {
+                                resultColumn.setReferenced();
+                                return resultColumn;
+                            } else {
+                                if (SanityManager.DEBUG) {
+                                    SanityManager.ASSERT(
+                                        false,
+                                        "wrong (tn,cn) for column " +
+                                        columnName +
+                                        " found: this pair points to " +
+                                        vcn.getSourceColumn().getName());
+                                }
+                                // Fall back on column name based lookup,
+                                // cf. DERBY-4679. See ColumnReference#
+                                // remapColumnReferencesToExpressions
+                                return null;
+                            }
                         } else {
                             rc = vcn.getSourceColumn();
                         }