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 rh...@apache.org on 2014/02/18 21:33:20 UTC

svn commit: r1569521 - in /db/derby/code/trunk/java: engine/org/apache/derby/impl/sql/compile/MatchingClauseNode.java engine/org/apache/derby/impl/sql/compile/MergeNode.java testing/org/apache/derbyTesting/functionTests/tests/lang/MergeStatementTest.java

Author: rhillegas
Date: Tue Feb 18 20:33:20 2014
New Revision: 1569521

URL: http://svn.apache.org/r1569521
Log:
DERBY-3155: Point matching refinement clauses into the row returned by the driving left join of the MERGE statement; commit derby-3155-27-aa-adjustMatchingRefinements.diff.

Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/MatchingClauseNode.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/MergeNode.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/MergeStatementTest.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/MatchingClauseNode.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/MatchingClauseNode.java?rev=1569521&r1=1569520&r2=1569521&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/MatchingClauseNode.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/MatchingClauseNode.java Tue Feb 18 20:33:20 2014
@@ -234,12 +234,6 @@ public class MatchingClauseNode extends 
         }
     }
 
-    /** Re-bind various clauses and lists once we have ResultSet numbers for the driving left join */
-    void    bindResultSetNumbers( MergeNode mergeNode, FromList fullFromList ) throws StandardException
-    {
-        bindRefinement( mergeNode, fullFromList );
-    }
-
     /** Collect the columns mentioned by expressions in this MATCHED clause */
     void    getColumnsInExpressions
         (
@@ -1283,6 +1277,8 @@ public class MatchingClauseNode extends 
     {
         _clauseNumber = clauseNumber;
 
+        adjustMatchingRefinement( selectList, generatedScan );
+        
         if ( isInsertClause() || isUpdateClause() ) { generateInsertUpdateRow( acb, selectList, generatedScan, hojn ); }
         
         _actionMethodName = "mergeActionMethod_" + _clauseNumber;
@@ -1369,7 +1365,7 @@ public class MatchingClauseNode extends 
      * The method returns the stuffed row.
      * </p>
      */
-    void    generateInsertUpdateRow
+    private void    generateInsertUpdateRow
         (
          ActivationClassBuilder acb,
          ResultColumnList selectList,
@@ -1397,11 +1393,30 @@ public class MatchingClauseNode extends 
 
     /**
      * <p>
+     * Point the column references in the matching refinement at the corresponding
+     * columns returned by the driving left join.
+     * </p>
+     */
+    private void    adjustMatchingRefinement
+        (
+         ResultColumnList selectList,
+         ResultSetNode  generatedScan
+         )
+        throws StandardException
+    {
+        if ( _matchingRefinement != null )
+        {
+            useGeneratedScan( selectList, generatedScan, _matchingRefinement );
+        }
+    }
+    
+    /**
+     * <p>
      * Point the column references in the temporary row at the corresponding
      * columns returned by the driving left join.
      * </p>
      */
-    void    adjustThenColumns
+    private void    adjustThenColumns
         (
          ResultColumnList selectList,
          ResultSetNode  generatedScan,
@@ -1410,15 +1425,8 @@ public class MatchingClauseNode extends 
         throws StandardException
     {
         ResultColumnList    leftJoinResult = generatedScan.getResultColumns();
-        CollectNodesVisitor<ColumnReference> getCRs =
-            new CollectNodesVisitor<ColumnReference>( ColumnReference.class );
-        _thenColumns.accept( getCRs );
 
-        for ( ColumnReference cr : getCRs.getList() )
-        {
-            ResultColumn    leftJoinRC = leftJoinResult.elementAt( getSelectListOffset( selectList, cr ) - 1 );
-            cr.setSource( leftJoinRC );
-        }
+        useGeneratedScan( selectList, generatedScan, _thenColumns );
 
         //
         // For an UPDATE action, the final column in the temporary row is the
@@ -1444,6 +1452,31 @@ public class MatchingClauseNode extends 
         }
     }
 
+    /**
+     * <p>
+     * Point a node's ColumnReferences into the row returned by the driving left join.
+     * </p>
+     */
+    private void    useGeneratedScan
+        (
+         ResultColumnList selectList,
+         ResultSetNode  generatedScan,
+         QueryTreeNode  node
+         )
+        throws StandardException
+    {
+        ResultColumnList    leftJoinResult = generatedScan.getResultColumns();
+        CollectNodesVisitor<ColumnReference> getCRs =
+            new CollectNodesVisitor<ColumnReference>( ColumnReference.class );
+        node.accept( getCRs );
+
+        for ( ColumnReference cr : getCRs.getList() )
+        {
+            ResultColumn    leftJoinRC = leftJoinResult.elementAt( getSelectListOffset( selectList, cr ) - 1 );
+            cr.setSource( leftJoinRC );
+        }
+    }
+    
     /** Return true if the ResultColumn represents a RowLocation */
     private boolean isRowLocation( ResultColumn rc ) throws StandardException
     {

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/MergeNode.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/MergeNode.java?rev=1569521&r1=1569520&r2=1569521&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/MergeNode.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/MergeNode.java Tue Feb 18 20:33:20 2014
@@ -238,13 +238,6 @@ public final class MergeNode extends DML
         }
         
         bindLeftJoin( dd );
-
-        // re-bind the matchingRefinement clauses now that we have result set numbers
-        // from the driving left join.
-        for ( MatchingClauseNode mcn : _matchingClauses )
-        {
-            mcn.bindResultSetNumbers( this, _leftJoinFromList );
-        }
 	}
 
     /** Create a FromList for binding a WHEN [ NOT ] MATCHED clause */
@@ -702,7 +695,7 @@ public final class MergeNode extends DML
     {
         CompilerContext cc = getCompilerContext();
         final int previousReliability = cc.getReliability();
-        
+
         try {
             cc.setReliability( previousReliability | CompilerContext.SQL_IN_ROUTINES_ILLEGAL );
             

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/MergeStatementTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/MergeStatementTest.java?rev=1569521&r1=1569520&r2=1569521&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/MergeStatementTest.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/MergeStatementTest.java Tue Feb 18 20:33:20 2014
@@ -5416,6 +5416,98 @@ public class MergeStatementTest extends 
         goodStatement( dboConnection, "drop table t1_039" );
     }
     
+   /**
+     * <p>
+     * Verify correct behavior when source table is a values clause wrapped in a view.
+     * </p>
+     */
+    public  void    test_040_valuesView()
+        throws Exception
+    {
+        Connection  dboConnection = openUserConnection( TEST_DBO );
+
+        //
+        // create schema
+        //
+        goodStatement
+            (
+             dboConnection,
+             "create view sr_040( i ) as values 1"
+             );
+        goodStatement
+            (
+             dboConnection,
+             "create table t1_040( x int, y int, z int )"
+             );
+        goodStatement
+            (
+             dboConnection,
+             "create unique index idx on t1_040( x, y )"
+             );
+        goodStatement
+            (
+             dboConnection,
+             "insert into t1_040 values\n" +
+             "( 1, 100, 1000 ), ( 1, 101, 1000 ), ( 1, 102, 1000 ), ( 1, 103, 1000 ), ( 2, 200, 2000 )\n"
+             );
+
+        // verify the behavior
+        goodUpdate
+            (
+             dboConnection,
+             "merge into t1_040\n" +
+             "using sr_040 on ( x = 1 )\n" +
+             "when matched and y = 101 then delete\n" +
+             "when matched and y = 102 then update set z = -1000\n" +
+             "when not matched and i > 1 then insert values ( -1, i, 0 )\n",
+             2
+             );
+        assertResults
+            (
+             dboConnection,
+             "select * from t1_040 order by x, y, z",
+             new String[][]
+             {
+                 { "1", "100", "1000" },
+                 { "1", "102", "-1000" },
+                 { "1", "103", "1000" },
+                 { "2", "200", "2000" },
+             },
+             false
+             );
+
+        goodUpdate
+            (
+             dboConnection,
+             "merge into t1_040\n" +
+             "using sr_040 on ( x = 3 )\n" +
+             "when matched and y = 103 then delete\n" +
+             "when matched and y = 102 then update set z = -10000\n" +
+             "when not matched and i = 1 then insert values ( -1, i, 0 )\n",
+             1
+             );
+        assertResults
+            (
+             dboConnection,
+             "select * from t1_040 order by x, y, z",
+             new String[][]
+             {
+                 { "-1", "1", "0" },
+                 { "1", "100", "1000" },
+                 { "1", "102", "-1000" },
+                 { "1", "103", "1000" },
+                 { "2", "200", "2000" },
+             },
+             false
+             );
+
+        //
+        // drop schema
+        //
+        goodStatement( dboConnection, "drop view sr_040" );
+        goodStatement( dboConnection, "drop table t1_040" );
+    }
+    
     ///////////////////////////////////////////////////////////////////////////////////
     //
     // ROUTINES