db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rhille...@apache.org
Subject svn commit: r1559183 - in /db/derby/code/trunk/java: engine/org/apache/derby/impl/sql/compile/MatchingClauseNode.java testing/org/apache/derbyTesting/functionTests/tests/lang/MergeStatementTest.java
Date Fri, 17 Jan 2014 16:54:43 GMT
Author: rhillegas
Date: Fri Jan 17 16:54:43 2014
New Revision: 1559183

URL: http://svn.apache.org/r1559183
Log:
DERBY-3155: Replace correlation names on the left side of the SET clauses in MERGE statements:
commit derby-3155-14-aa-replaceCorrelationNamesOnLeftSideOfSETclauses.diff.

Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/MatchingClauseNode.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=1559183&r1=1559182&r2=1559183&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
Fri Jan 17 16:54:43 2014
@@ -203,11 +203,41 @@ public class MatchingClauseNode extends 
         throws StandardException
     {
         parent.replaceCorrelationName( correlationName, newTableName, _matchingRefinement
);
-        parent.replaceCorrelationName( correlationName, newTableName, _updateColumns );
+        replaceCorrelationNameInSetClauses( parent, correlationName, newTableName );
         parent.replaceCorrelationName( correlationName, newTableName, _insertColumns );
         parent.replaceCorrelationName( correlationName, newTableName, _insertValues );
     }
     
+    /**
+     * <p>
+     * Replace references to the correlation name with the underlying table name
+     * in the SET clauses of WHEN MATCHED ... THEN UPDATE clauses. This replacement is
+     * done before the ColumnReferences are bound.
+     * </p>
+     */
+    public  void    replaceCorrelationNameInSetClauses
+        (
+         MergeNode  parent,
+         String correlationName,
+         TableName  newTableName
+         )
+        throws StandardException
+    {
+        if ( _updateColumns == null ) { return; }
+        
+        // this handles the right side of the SET clauses
+        parent.replaceCorrelationName( correlationName, newTableName, _updateColumns );
+
+        // we have to hand-process the left side because the Visitor
+        // logic for ResultColumns does not process the ColumnReference
+        for ( int i = 0; i < _updateColumns.size(); i++ )
+        {
+            ResultColumn    rc = _updateColumns.elementAt( i );
+
+            parent.replaceCorrelationName( correlationName, newTableName, rc.getReference()
);
+        }
+    }
+    
     /** Bind this WHEN [ NOT ] MATCHED clause against the parent JoinNode */
     void    bind
         (

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=1559183&r1=1559182&r2=1559183&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
Fri Jan 17 16:54:43 2014
@@ -4501,6 +4501,70 @@ public class MergeStatementTest extends 
         goodStatement( dboConnection, "drop table t1_026" );
     }
     
+    /**
+     * <p>
+     * Verify that correlation names on the left side of SET clauses are replaced properly.
+     * </p>
+     */
+    public  void    test_027_correlationNamesInSetClauses()
+        throws Exception
+    {
+        Connection  dboConnection = openUserConnection( TEST_DBO );
+
+        //
+        // create schema
+        //
+        goodStatement
+            (
+             dboConnection,
+             "create table t1_027( a int, b int )"
+             );
+        goodStatement
+            (
+             dboConnection,
+             "create table t2_027( c int, d int )"
+             );
+        goodStatement
+            (
+             dboConnection,
+             "insert into t1_027 values ( 1, 100 ), ( 2, 200 )"
+             );
+        goodStatement
+            (
+             dboConnection,
+             "insert into t2_027 values ( 1, 1000 ), (2, 2000), ( 3, 3000 ), ( 4, 4000 )"
+             );
+
+        // test that correlation names are replaced properly
+        goodUpdate
+            (
+             dboConnection,
+             "merge into t1_027 x\n" +
+             "using t2_027 y on x.a = y.c\n" +
+             "when matched and x.b > 100 then update set x.b = y.d\n" +
+             "when matched and x.b <= 100 then delete\n" +
+             "when not matched and y.d > 3000 then insert values ( y.c, y.d )\n",
+             3
+             );
+        assertResults
+            (
+             dboConnection,
+             "select * from t1_027 order by a",
+             new String[][]
+             {
+                 { "2", "2000" },
+                 { "4", "4000" },
+             },
+             false
+             );
+
+        //
+        // drop schema
+        //
+        goodStatement( dboConnection, "drop table t2_027" );
+        goodStatement( dboConnection, "drop table t1_027" );
+    }
+    
     ///////////////////////////////////////////////////////////////////////////////////
     //
     // ROUTINES



Mime
View raw message