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

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


The following commit(s) were added to refs/heads/branch-3.0 by this push:
     new 635feaa  [SPARK-31712][SQL][TESTS] Check casting timestamps before the 
epoch to Byte/Short/Int/Long types
635feaa is described below

commit 635feaa0ef6298e336a447e1fdcfeca403b741bd
Author: Max Gekk <max.g...@gmail.com>
AuthorDate: Fri May 15 04:24:58 2020 +0000

    [SPARK-31712][SQL][TESTS] Check casting timestamps before the epoch to 
Byte/Short/Int/Long types
    
    ### What changes were proposed in this pull request?
    Added tests to check casting timestamps before 1970-01-01 00:00:00Z to 
ByteType, ShortType, IntegerType and LongType in ansi and non-ansi modes.
    
    ### Why are the changes needed?
    To improve test coverage and prevent errors while modifying the CAST 
expression code.
    
    ### Does this PR introduce _any_ user-facing change?
    No
    
    ### How was this patch tested?
    By running the modified test suites:
    ```
    $ ./build/sbt "test:testOnly *CastSuite"
    ```
    
    Closes #28531 from MaxGekk/test-cast-timestamp-to-byte.
    
    Authored-by: Max Gekk <max.g...@gmail.com>
    Signed-off-by: Wenchen Fan <wenc...@databricks.com>
    (cherry picked from commit c7ce37dfa713f80c5f0157719f0e3d9bf0d271dd)
    Signed-off-by: Wenchen Fan <wenc...@databricks.com>
---
 .../spark/sql/catalyst/expressions/CastSuite.scala | 27 ++++++++++++++++++++++
 1 file changed, 27 insertions(+)

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 5c57843..334b43e 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
@@ -1299,6 +1299,18 @@ class CastSuite extends CastSuiteBase {
       }
     }
   }
+
+  test("cast a timestamp before the epoch 1970-01-01 00:00:00Z") {
+    withDefaultTimeZone(UTC) {
+      val negativeTs = Timestamp.valueOf("1900-05-05 18:34:56.1")
+      assert(negativeTs.getTime < 0)
+      val expectedSecs = Math.floorDiv(negativeTs.getTime, MILLIS_PER_SECOND)
+      checkEvaluation(cast(negativeTs, ByteType), expectedSecs.toByte)
+      checkEvaluation(cast(negativeTs, ShortType), expectedSecs.toShort)
+      checkEvaluation(cast(negativeTs, IntegerType), expectedSecs.toInt)
+      checkEvaluation(cast(negativeTs, LongType), expectedSecs)
+    }
+  }
 }
 
 /**
@@ -1341,4 +1353,19 @@ class AnsiCastSuite extends CastSuiteBase {
         cast("abc.com", dataType), "invalid input")
     }
   }
+
+  test("cast a timestamp before the epoch 1970-01-01 00:00:00Z") {
+    withDefaultTimeZone(UTC) {
+      val negativeTs = Timestamp.valueOf("1900-05-05 18:34:56.1")
+      assert(negativeTs.getTime < 0)
+      val expectedSecs = Math.floorDiv(negativeTs.getTime, MILLIS_PER_SECOND)
+      checkExceptionInExpression[ArithmeticException](
+        cast(negativeTs, ByteType), "to byte causes overflow")
+      checkExceptionInExpression[ArithmeticException](
+        cast(negativeTs, ShortType), "to short causes overflow")
+      checkExceptionInExpression[ArithmeticException](
+        cast(negativeTs, IntegerType), "to int causes overflow")
+      checkEvaluation(cast(negativeTs, LongType), expectedSecs)
+    }
+  }
 }


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

Reply via email to