db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From abr...@apache.org
Subject svn commit: r544974 - in /db/derby/code/trunk/java: engine/org/apache/derby/impl/sql/compile/ testing/org/apache/derbyTesting/functionTests/tests/lang/
Date Wed, 06 Jun 2007 22:26:18 GMT
Author: abrown
Date: Wed Jun  6 15:26:11 2007
New Revision: 544974

URL: http://svn.apache.org/viewvc?view=rev&rev=544974
Log:
DERBY-1620: Minor cleanup to follow svn # 520173, plus a new JUnit test to
verify the behavior fixed with DERBY-1620.

Contributed by: John Peterson (john.peterson@pega.com)

Added:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/CaseExpressionTest.java
  (with props)
Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ConditionalNode.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ConditionalNode.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ConditionalNode.java?view=diff&rev=544974&r1=544973&r2=544974
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ConditionalNode.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ConditionalNode.java
Wed Jun  6 15:26:11 2007
@@ -113,6 +113,12 @@
 		}
 	}
 
+	/**
+	 * Checks if the provided node is a CastNode.
+	 *
+	 * @param node	The node to check.
+	 * @return 		True if this node is a CastNode, false otherwise.
+	 */
 	private boolean isCastNode(ValueNode node) {
 		if (node.getNodeType() == C_NodeTypes.CAST_NODE)
 			return true;
@@ -120,6 +126,13 @@
 			return false;
 	}
 
+	/**
+	 * Checks if the provided CastNode is cast to a SQL CHAR type.
+	 *
+	 * @param node	The CastNode to check.
+	 * @return		True if this CastNode's target type is CHAR,
+	 *              false otherwise.
+	 */
 	private boolean isCastToChar(CastNode node) {
 		if (node.castTarget.getTypeName().equals(TypeId.CHAR_NAME))
 			return true;
@@ -156,24 +169,6 @@
 			return false;
 	}
 
-	private boolean shouldCast(ValueNode newNode, ValueNode oldNode)
-	throws StandardException
-	{
-		return shouldCast(newNode.getTypeServices(), oldNode.getTypeServices());
-	}
-
-	private boolean shouldCast(DataTypeDescriptor newType, ValueNode oldNode)
-	throws StandardException
-	{
-		return shouldCast(newType, oldNode.getTypeServices());
-	}
-
-	private boolean shouldCast(ValueNode newNode, DataTypeDescriptor oldType)
-	throws StandardException
-	{
-		return shouldCast(newNode.getTypeServices(), oldType);
-	}
-
 	/**
 	 * Checks to see if oldType should be casted to the newType.
 	 * Returns TRUE if the two DataTypeDescriptors have different
@@ -296,19 +291,26 @@
 		ValueNode thenNode = (ValueNode)thenElseList.elementAt(0);
 		ValueNode elseNode = (ValueNode)thenElseList.elementAt(1);
 
-		// check if the "then" node is NULL
-		if (isNullNode(thenNode) && shouldCast(castType, thenNode)) {
+		// first check if the "then" node is NULL
+		if (isNullNode(thenNode) &&
+		    shouldCast(castType, thenNode.getTypeServices()))
+		{
 			thenElseList.setElementAt(recastNullNode(thenNode, castType), 0);
-		// otherwise recurse if it's a conditional
+		// otherwise recurse on thenNode, but only if it's a conditional
 		} else if (isConditionalNode(thenNode)) {
-			recastNullNodes(((ConditionalNode)thenNode).thenElseList, castType);
+			recastNullNodes(((ConditionalNode)thenNode).thenElseList,
+			                castType);
 		}
-		// check if the "else node is NULL
-		if (isNullNode(elseNode) && shouldCast(castType, elseNode)) {
+
+		// lastly, check if the "else" node is NULL
+		if (isNullNode(elseNode) &&
+		    shouldCast(castType, elseNode.getTypeServices()))
+		{
 			thenElseList.setElementAt(recastNullNode(elseNode, castType), 1);
-		// otherwise recurse if it's a conditional
+		// otherwise recurse on elseNode, but only if it's a conditional
 		} else if (isConditionalNode(elseNode)) {
-			recastNullNodes(((ConditionalNode)elseNode).thenElseList, castType);
+			recastNullNodes(((ConditionalNode)elseNode).thenElseList,
+			                castType);
 		}
 	}
 

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/CaseExpressionTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/CaseExpressionTest.java?view=auto&rev=544974
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/CaseExpressionTest.java
(added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/CaseExpressionTest.java
Wed Jun  6 15:26:11 2007
@@ -0,0 +1,218 @@
+/**
+ *  Derby - Class org.apache.derbyTesting.functionTests.tests.lang.CaseExpressionTest
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.derbyTesting.functionTests.tests.lang;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.sql.ResultSet;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.derbyTesting.junit.BaseJDBCTestCase;
+import org.apache.derbyTesting.junit.CleanDatabaseTestSetup;
+import org.apache.derbyTesting.junit.SQLUtilities;
+import org.apache.derbyTesting.junit.TestConfiguration;
+
+import org.apache.derbyTesting.functionTests.tests.lang.CastingTest;
+
+public class CaseExpressionTest extends BaseJDBCTestCase {
+
+        // Results if the Case Expression evaluates to a column reference :
+        //
+        // 1. SELECT CASE WHEN 1 = 1 THEN <column reference> ELSE NULL END
+        // 2. SELECT CASE WHEN 1 = 1 THEN
+        //       (CASE WHEN 1 = 1 THEN <column reference> ELSE NULL END)
+        //       ELSE NULL END
+        //
+        private static String[][] columnReferenceResults = {
+            /*SMALLINT*/ {null,"0","1","2"},
+            /*INTEGER*/ {null,"0","1","21"},
+            /*BIGINT*/ {null,"0","1","22"},
+            /*DECIMAL(10,5)*/ {null,"0.00000","1.00000","23.00000"},
+            /*REAL*/ {null,"0.0","1.0","24.0"},
+            /*DOUBLE*/ {null,"0.0","1.0","25.0"},
+            /*CHAR(60)*/ {
+                null,
+                "0                                                           ",
+                "aa                                                          ",
+                "2.0                                                         "},
+            /*VARCHAR(60)*/ {null,"0","aa","15:30:20"},
+            /*LONG VARCHAR*/ {null,"0","aa","2000-01-01 15:30:20"},
+            /*CHAR(60) FOR BIT DATA*/ {
+                null,
+                "10aa20202020202020202020202020202020202020202020202020202020" +
+                "202020202020202020202020202020202020202020202020202020202020",
+                null,
+                "10aaaa202020202020202020202020202020202020202020202020202020" +
+                "202020202020202020202020202020202020202020202020202020202020"},
+            /*VARCHAR(60) FOR BIT DATA*/ {null,"10aa",null,"10aaba"},
+            /*LONG VARCHAR FOR BIT DATA*/ {null,"10aa",null,"10aaca"},
+            /*CLOB(1k)*/ {null,"13","14",null},
+            /*DATE*/ {null,"2000-01-01","2000-01-01",null},
+            /*TIME*/ {null,"15:30:20","15:30:20","15:30:20"},
+            /*TIMESTAMP*/ {
+                null,
+                "2000-01-01 15:30:20.0",
+                "2000-01-01 15:30:20.0",
+                "2000-01-01 15:30:20.0"},
+            /*BLOB(1k)*/ {null,null,null,null},
+        };
+
+        // Results if the Case Expression evaluates to a NULL value :
+        //
+        // 3. SELECT CASE WHEN 1 = 1 THEN NULL ELSE <column reference> END
+        // 4. SELECT CASE WHEN 1 = 1 THEN
+        //       (CASE WHEN 1 = 1 THEN NULL ELSE <column reference> END)
+        //         ELSE NULL END
+        // 5. SELECT CASE WHEN 1 = 1 THEN NULL ELSE
+        //         (CASE WHEN 1 = 1 THEN <column reference> ELSE NULL END) END
+        // 6. SELECT CASE WHEN 1 = 1 THEN NULL ELSE
+        //         (CASE WHEN 1 = 1 THEN NULL ELSE <column reference> END) END
+        //
+        private static String[][] nullValueResults  = {
+            /*SMALLINT*/ {null,null,null,null},
+            /*INTEGER*/ {null,null,null,null},
+            /*BIGINT*/ {null,null,null,null},
+            /*DECIMAL(10,5)*/ {null,null,null,null},
+            /*REAL*/ {null,null,null,null},
+            /*DOUBLE*/ {null,null,null,null},
+            /*CHAR(60)*/ {null,null,null,null},
+            /*VARCHAR(60)*/ {null,null,null,null},
+            /*LONG VARCHAR*/ {null,null,null,null},
+            /*CHAR(60) FOR BIT DATA*/ {null,null,null,null},
+            /*VARCHAR(60) FOR BIT DATA*/ {null,null,null,null},
+            /*LONG VARCHAR FOR BIT DATA*/ {null,null,null,null},
+            /*CLOB(1k)*/ {null,null,null,null},
+            /*DATE*/ {null,null,null,null},
+            /*TIME*/ {null,null,null,null},
+            /*TIMESTAMP*/ {null,null,null,null},
+            /*BLOB(1k)*/ {null,null,null,null},
+        };
+
+    public CaseExpressionTest(String name) {
+        super(name);
+    }
+
+    public void testAllDatatypesCombinationsForCaseExpressions()
+    throws SQLException
+    {
+        Statement s = createStatement();
+
+        /* 1. Column Reference in the THEN node, and NULL in
+         * the ELSE node.
+         */
+        testCaseExpressionQuery(s, columnReferenceResults,
+            "SELECT CASE WHEN 1 = 1 THEN ",
+            " ELSE NULL END from AllDataTypesTable");
+
+        /* 2. Test Column Reference nested in the THEN's node THEN node,
+         * NULL's elsewhere.
+         */
+        testCaseExpressionQuery(s, columnReferenceResults,
+            "SELECT CASE WHEN 1 = 1 THEN (CASE WHEN 1 = 1 THEN ",
+            " ELSE NULL END) ELSE NULL END from AllDataTypesTable");
+
+        /* 3. NULL in the THEN node, and a Column Reference in
+         * the ELSE node.
+         */
+        testCaseExpressionQuery(s, nullValueResults,
+            "SELECT CASE WHEN 1 = 1 THEN NULL ELSE ",
+            " END from AllDataTypesTable");
+
+        /* 4. Test Column Reference nested in the THEN's node ELSE node,
+         * NULL's elsewhere.
+         */
+        testCaseExpressionQuery(s, nullValueResults,
+            "SELECT CASE WHEN 1 = 1 THEN (CASE WHEN 1 = 1 THEN NULL ELSE ",
+            " END) ELSE NULL END from AllDataTypesTable");
+
+        /* 5. Test Column Reference nested in the ELSE's node THEN node,
+         * NULL's elsewhere.
+         */
+        testCaseExpressionQuery(s, nullValueResults,
+            "SELECT CASE WHEN 1 = 1 THEN NULL ELSE (CASE WHEN 1 = 1 THEN ",
+            " ELSE NULL END) END from AllDataTypesTable");
+
+        /* 6. Test Column Reference nested in the ELSE's node ELSE node,
+         * NULL's elsewhere.
+         */
+        testCaseExpressionQuery(s, nullValueResults,
+            "SELECT CASE WHEN 1 = 1 THEN NULL " +
+            "ELSE (CASE WHEN 1 = 1 THEN NULL ELSE ",
+            " END) END from AllDataTypesTable");
+
+        s.close();
+    }
+
+    /**
+     * Runs the test fixtures in embedded.
+     *
+     * @return test suite
+     */
+    public static Test suite()
+    {
+        TestSuite suite = (TestSuite)
+            TestConfiguration.embeddedSuite(CaseExpressionTest.class);
+
+        return new CleanDatabaseTestSetup(suite) {
+            /**
+             * Creates the table used in the test cases.
+             */
+            protected void decorateSQL(Statement s) throws SQLException {
+                SQLUtilities.createAndPopulateAllDataTypesTable(s);
+            }
+        };
+    }
+
+    /**
+     * Execute the received caseExpression on the received Statement
+     * and check the results against the receieved expected array.
+     */
+    private void testCaseExpressionQuery(Statement st,
+        String [][] expRS, String caseExprBegin, String caseExprEnd)
+        throws SQLException
+    {
+        ResultSet rs;
+        int colType;
+        int row;
+
+        for (colType = 0;
+            colType < CastingTest.SQLTypes.length;
+            colType++)
+        {
+            rs = st.executeQuery(
+                caseExprBegin +
+                SQLUtilities.allDataTypesColumnNames[colType] +
+                caseExprEnd);
+
+            row = 0;
+            while (rs.next()) {
+                String val = rs.getString(1);
+                assertEquals(expRS[colType][row], val);
+                row++;
+            }
+            rs.close();
+        }
+    }
+}

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/CaseExpressionTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java?view=diff&rev=544974&r1=544973&r2=544974
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java
Wed Jun  6 15:26:11 2007
@@ -108,6 +108,7 @@
         suite.addTest(ErrorCodeTest.suite());
         suite.addTest(TimestampArithTest.suite());
         suite.addTest(SpillHashTest.suite());
+        suite.addTest(CaseExpressionTest.suite());
 
         // Add the XML tests, which exist as a separate suite
         // so that users can "run all XML tests" easily.



Mime
View raw message