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

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


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

commit 866343c7be47d71b88ae9a6b4dda26f8c4f5964b
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
    
    ### What changes were proposed in this pull request?
    
    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.
    
    ### Why are the changes needed?
    
    Fixes a small bug in cast.
    
    ### Does this PR introduce _any_ user-facing change?
    
    No.
    
    ### How was this patch tested?
    
    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]>
---
 .../main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala  | 2 +-
 .../apache/spark/sql/catalyst/expressions/CastWithAnsiOffSuite.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 b72ba3ea8a0..6900aa873bb 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
@@ -774,7 +774,7 @@ case class Cast(
         }
       })
     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/CastWithAnsiOffSuite.scala
 
b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastWithAnsiOffSuite.scala
index b3b6abaa573..1dbf03b1538 100644
--- 
a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastWithAnsiOffSuite.scala
+++ 
b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastWithAnsiOffSuite.scala
@@ -608,6 +608,11 @@ class CastWithAnsiOffSuite extends CastSuiteBase {
     checkEvaluation(cast(input, StringType), "1.23E-7")
   }
 
+  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