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 2010/06/18 17:26:59 UTC
svn commit: r956025 - in /db/derby/code/trunk/java:
engine/org/apache/derby/impl/sql/compile/CastNode.java
testing/org/apache/derbyTesting/functionTests/tests/lang/BooleanValuesTest.java
Author: rhillegas
Date: Fri Jun 18 15:26:59 2010
New Revision: 956025
URL: http://svn.apache.org/viewvc?rev=956025&view=rev
Log:
DERBY-4704: Make casts of strings to booleans always nullable.
Modified:
db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/CastNode.java
db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/BooleanValuesTest.java
Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/CastNode.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/CastNode.java?rev=956025&r1=956024&r2=956025&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/CastNode.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/CastNode.java Fri Jun 18 15:26:59 2010
@@ -414,10 +414,19 @@ public class CastNode extends ValueNode
getTypeId().getSQLTypeName());
}
}
-
+
+ //
// Preserve the nullability of the operand since a CAST
- // of a non-NULL value is also non-NULL.
- setNullability(castOperand.getTypeServices().isNullable());
+ // of a non-NULL value is also non-NULL. However, if the source type is
+ // a non-nullable string type and the target type is a boolean, then the result
+ // still must be nullable because the string "unknown" casts to boolean NULL.
+ //
+ if (
+ castOperand.getTypeServices().getTypeId().isStringTypeId() &&
+ getTypeId().isBooleanTypeId()
+ )
+ { setNullability( true ); }
+ else { setNullability(castOperand.getTypeServices().isNullable()); }
}
/**
Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/BooleanValuesTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/BooleanValuesTest.java?rev=956025&r1=956024&r2=956025&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/BooleanValuesTest.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/BooleanValuesTest.java Fri Jun 18 15:26:59 2010
@@ -323,6 +323,24 @@ public class BooleanValuesTest extends
);
}
+ if ( !tableExists( conn, "T_4704" ) )
+ {
+ //
+ // create table
+ //
+ goodStatement( conn, "create table t_4704( keyCol int, stringCol varchar( 20 ) not null )" );
+
+ //
+ // populate it
+ //
+ goodStatement
+ (
+ conn,
+ "insert into t_4704( keyCol, stringCol )\n" +
+ "values ( 0, 'false' ), ( 1, 'true' ), ( 2, 'unknown' )\n"
+ );
+ }
+
if ( !tableExists( conn, "STRING_TYPES" ) )
{
//
@@ -764,6 +782,8 @@ public class BooleanValuesTest extends
vetBadStringCast( conn, "clob_col" );
vetBadStringCast( conn, "long_varchar_col" );
vetBadStringCast( conn, "varchar_col" );
+
+ vet4704();
}
private void vetBadStringCast( Connection conn, String columnName ) throws Exception
{
@@ -793,22 +813,27 @@ public class BooleanValuesTest extends
* columns to BOOLEAN, the result column was marked as non-nullable, even
* though the VARCHAR could contain the value 'UNKNOWN', in which case
* the cast should return NULL.
- *
- * The test case is disabled for now. Enable it when the bug is fixed.
*/
- public void disabled_testNullabilityOfCastFromNonNullableVARCHAR()
- throws SQLException {
- setAutoCommit(false); // for automatic rollback when test has completed
+ public void vet4704()
+ throws SQLException
+ {
Statement s = createStatement();
- s.execute("create table nonnullablestrings(x varchar(10) not null)");
- s.execute("insert into nonnullablestrings " +
- "values 'true', 'false', 'unknown'");
- ResultSet rs = s.executeQuery(
- "select cast(x as boolean) from nonnullablestrings");
- JDBC.assertNullability(rs, new boolean[] { true });
- JDBC.assertFullResultSet(
- rs, new String[][] { {"true"}, {"false"}, {null} });
+ ResultSet rs = s.executeQuery( "select keyCol, stringCol from t_4704 order by keyCol" );
+ JDBC.assertNullability(rs, new boolean[] { true, false });
+
+ rs = s.executeQuery( "select keyCol, cast(stringCol as boolean) from t_4704 order by keyCol" );
+ JDBC.assertNullability(rs, new boolean[] { true, true });
+ JDBC.assertFullResultSet
+ (
+ rs,
+ new String[][]
+ {
+ { "0", "false" },
+ { "1", "true" },
+ { "2", null },
+ }
+ );
}
public void test_10_nullabilityOfCastFromLiteral() throws SQLException {