This is an automated email from the ASF dual-hosted git repository.

gurwls223 pushed a commit to branch branch-3.3
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/branch-3.3 by this push:
     new 04edc7e6c93 [SPARK-42176][SQL] Fix cast of a boolean value to timestamp
04edc7e6c93 is described below

commit 04edc7e6c9376f8a6c5f8f7b1e43a3e72e71a065
Author: Ivan Sadikov <[email protected]>
AuthorDate: Wed Jan 25 12:32:25 2023 +0900

    [SPARK-42176][SQL] Fix cast of a boolean value to timestamp
    
    The PR fixes an issue when casting a boolean to timestamp.
    
    While `select cast(true as timestamp)` works and returns `1970-01-01 
00:00:00.000001`, casting `false` to timestamp fails with the following error:
    > IllegalArgumentException: requirement failed: Literal must have a 
corresponding value to timestamp, but class Integer found.
    
    SBT test also fails with this error:
    ```
    [info]   java.lang.ClassCastException: java.lang.Integer cannot be cast to 
java.lang.Long
    [info]   at scala.runtime.BoxesRunTime.unboxToLong(BoxesRunTime.java:107)
    [info]   at 
org.apache.spark.sql.catalyst.InternalRow$.$anonfun$getWriter$5(InternalRow.scala:178)
    [info]   at 
org.apache.spark.sql.catalyst.InternalRow$.$anonfun$getWriter$5$adapted(InternalRow.scala:178)
    ```
    
    The issue was that we need to return `0L` instead of `0` when converting 
`false` to a long.
    
    Fixes a small bug in cast.
    
    No.
    
    I added a unit test to verify the fix.
    
    Closes #39729 from sadikovi/fix_spark_boolean_to_timestamp.
    
    Authored-by: Ivan Sadikov <[email protected]>
    Signed-off-by: Hyukjin Kwon <[email protected]>
    (cherry picked from commit 866343c7be47d71b88ae9a6b4dda26f8c4f5964b)
    Signed-off-by: Hyukjin Kwon <[email protected]>
---
 .../main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala  | 2 +-
 .../scala/org/apache/spark/sql/catalyst/expressions/CastSuite.scala  | 5 +++++
 2 files changed, 6 insertions(+), 1 deletion(-)

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 ee95ea7f9f6..737bce492c4 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
@@ -515,7 +515,7 @@ abstract class CastBase extends UnaryExpression
         }
       })
     case BooleanType =>
-      buildCast[Boolean](_, b => if (b) 1L else 0)
+      buildCast[Boolean](_, b => if (b) 1L else 0L)
     case LongType =>
       buildCast[Long](_, l => longToTimestamp(l))
     case IntegerType =>
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 630c45adba1..1ce3efd1d0b 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
@@ -619,6 +619,11 @@ class CastSuite extends CastSuiteBase {
     checkEvaluation(cast(Literal("2015-03-18T"), TimestampType), null)
   }
 
+  test("SPARK-42176: cast boolean to timestamp") {
+    checkEvaluation(cast(true, TimestampType), 1L)
+    checkEvaluation(cast(false, TimestampType), 0L)
+  }
+
   private def castOverflowErrMsg(targetType: DataType): String = {
     s"""cannot be cast to "${targetType.sql}" due to an overflow."""
   }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to