Repository: spark
Updated Branches:
  refs/heads/master f93ad4fe7 -> 338a98d65


[SPARK-16791][SQL] cast struct with timestamp field fails

## What changes were proposed in this pull request?
a failing test case + fix to SPARK-16791 
(https://issues.apache.org/jira/browse/SPARK-16791)

## How was this patch tested?
added a failing test case to CastSuit, then fixed the Cast code and rerun the 
entire CastSuit

Author: eyal farago <eyal farago>
Author: Eyal Farago <eyal.far...@actimize.com>

Closes #14400 from eyalfa/SPARK-16791_cast_struct_with_timestamp_field_fails.


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

Branch: refs/heads/master
Commit: 338a98d65c8efe0c41f39a8dddeab7040dcda125
Parents: f93ad4f
Author: eyal farago <eyal farago>
Authored: Mon Aug 1 22:43:32 2016 +0800
Committer: Wenchen Fan <wenc...@databricks.com>
Committed: Mon Aug 1 22:43:32 2016 +0800

----------------------------------------------------------------------
 .../org/apache/spark/sql/catalyst/expressions/Cast.scala  |  2 +-
 .../apache/spark/sql/catalyst/expressions/CastSuite.scala | 10 ++++++++++
 2 files changed, 11 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/spark/blob/338a98d6/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 c452765..70fff51 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
@@ -416,7 +416,7 @@ case class Cast(child: Expression, dataType: DataType) 
extends UnaryExpression w
   }
 
   private[this] def cast(from: DataType, to: DataType): Any => Any = to match {
-    case dt if dt == child.dataType => identity[Any]
+    case dt if dt == from => identity[Any]
     case StringType => castToString(from)
     case BinaryType => castToBinary(from)
     case DateType => castToDate(from)

http://git-wip-us.apache.org/repos/asf/spark/blob/338a98d6/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastSuite.scala
----------------------------------------------------------------------
diff --git 
a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastSuite.scala
 
b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastSuite.scala
index 5ae0527..5c35baa 100644
--- 
a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastSuite.scala
+++ 
b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastSuite.scala
@@ -727,6 +727,16 @@ class CastSuite extends SparkFunSuite with 
ExpressionEvalHelper {
     }
   }
 
+  test("cast struct with a timestamp field") {
+    val originalSchema = new StructType().add("tsField", TimestampType, 
nullable = false)
+    // nine out of ten times I'm casting a struct, it's to normalize its 
fields nullability
+    val targetSchema = new StructType().add("tsField", TimestampType, nullable 
= true)
+
+    val inp = Literal.create(InternalRow(0L), originalSchema)
+    val expected = InternalRow(0L)
+    checkEvaluation(cast(inp, targetSchema), expected)
+  }
+
   test("complex casting") {
     val complex = Literal.create(
       Row(


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@spark.apache.org
For additional commands, e-mail: commits-h...@spark.apache.org

Reply via email to