Repository: spark
Updated Branches:
  refs/heads/master e6e44e46e -> 8e37ed6eb


[SPARK-1608] [SQL] Fix Cast.nullable when cast from StringType to 
NumericType/TimestampType.

`Cast.nullable` should be `true` when cast from `StringType` to `NumericType` 
or `TimestampType`.
Because if `StringType` expression has an illegal number string or illegal 
timestamp string, the casted value becomes `null`.

Author: Takuya UESHIN <[email protected]>

Closes #532 from ueshin/issues/SPARK-1608 and squashes the following commits:

065d37c [Takuya UESHIN] Add tests to check nullabilities of cast expressions.
f278ed7 [Takuya UESHIN] Revert test to keep it readable and concise.
9fc9380 [Takuya UESHIN] Fix Cast.nullable when cast from StringType to 
NumericType/TimestampType.


Project: http://git-wip-us.apache.org/repos/asf/spark/repo
Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/8e37ed6e
Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/8e37ed6e
Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/8e37ed6e

Branch: refs/heads/master
Commit: 8e37ed6eb81687140b6cdb00f4ec609ec7ba9be1
Parents: e6e44e4
Author: Takuya UESHIN <[email protected]>
Authored: Sat Apr 26 14:39:54 2014 -0700
Committer: Reynold Xin <[email protected]>
Committed: Sat Apr 26 14:39:54 2014 -0700

----------------------------------------------------------------------
 .../apache/spark/sql/catalyst/expressions/Cast.scala    |  6 +++++-
 .../expressions/ExpressionEvaluationSuite.scala         | 12 ++++++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/spark/blob/8e37ed6e/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala
----------------------------------------------------------------------
diff --git 
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala
 
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala
index 8b79b0c..40d2b42 100644
--- 
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala
+++ 
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala
@@ -24,7 +24,11 @@ import org.apache.spark.sql.catalyst.types._
 /** Cast the child expression to the target data type. */
 case class Cast(child: Expression, dataType: DataType) extends UnaryExpression 
{
   override def foldable = child.foldable
-  def nullable = child.nullable
+  def nullable = (child.dataType, dataType) match {
+    case (StringType, _: NumericType) => true
+    case (StringType, TimestampType)  => true
+    case _                            => child.nullable
+  }
   override def toString = s"CAST($child, $dataType)"
 
   type EvaluatedType = Any

http://git-wip-us.apache.org/repos/asf/spark/blob/8e37ed6e/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ExpressionEvaluationSuite.scala
----------------------------------------------------------------------
diff --git 
a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ExpressionEvaluationSuite.scala
 
b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ExpressionEvaluationSuite.scala
index 4ce0dff..d287ad7 100644
--- 
a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ExpressionEvaluationSuite.scala
+++ 
b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ExpressionEvaluationSuite.scala
@@ -245,6 +245,18 @@ class ExpressionEvaluationSuite extends FunSuite {
     checkEvaluation(Literal(23.toShort) + Cast(true, ShortType), 24)
 
     intercept[Exception] {evaluate(Literal(1) cast BinaryType, null)}
+
+    assert(("abcdef" cast StringType).nullable === false)
+    assert(("abcdef" cast BinaryType).nullable === false)
+    assert(("abcdef" cast BooleanType).nullable === false)
+    assert(("abcdef" cast TimestampType).nullable === true)
+    assert(("abcdef" cast LongType).nullable === true)
+    assert(("abcdef" cast IntegerType).nullable === true)
+    assert(("abcdef" cast ShortType).nullable === true)
+    assert(("abcdef" cast ByteType).nullable === true)
+    assert(("abcdef" cast DecimalType).nullable === true)
+    assert(("abcdef" cast DoubleType).nullable === true)
+    assert(("abcdef" cast FloatType).nullable === true)
   }
 
   test("timestamp") {

Reply via email to