From 9d3948358c15d68fbfcafc59b7ca0d380c66e83b Mon Sep 17 00:00:00 2001 From: GuoPhilipse <46367746+GuoPhilipse@users.noreply.github.com> Date: Mon, 18 May 2020 16:34:15 +0800 Subject: [PATCH] SPARK-31710:Add compatibility flag to cast long to timestamp As we know,long datatype is interpreted as milliseconds when conversion to timestamp in hive, while long is interpreted as seconds when conversion to timestamp in spark, we have many sqls runing in product, so we need a compatibility flag to make them migrating smoothly ,meanwhile do not change the user behavior in spark. --- .../sql/catalyst/expressions/CastSuite.scala | 29 +++++++++++++++++++ 1 file changed, 29 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 ee94f3587b55c..fabe7ff6654e0 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 @@ -1341,4 +1341,33 @@ class AnsiCastSuite extends CastSuiteBase { cast("abc.com", dataType), "invalid input") } } + + test("SPARK-31710:Add compatibility flag to cast long to timestamp") { + withSQLConf( + SQLConf.LONG_TIMESTAMP_CONVERSION_IN_SECONDS.key -> "false") { + for (tz <- ALL_TIMEZONES) { + def checkLongToTimestamp(str: Long, expected: Long): Unit = { + checkEvaluation(cast(str, TimestampType, Option(tz.getID)), expected) + } + checkLongToTimestamp(253402272000L, 253402272000000L) + checkLongToTimestamp(-5L, -5000L) + checkLongToTimestamp(1L, 1000L) + checkLongToTimestamp(0L, 0L) + checkLongToTimestamp(123L, 123000L) + } + } + withSQLConf( + SQLConf.LONG_TIMESTAMP_CONVERSION_IN_SECONDS.key -> "true") { + for (tz <- ALL_TIMEZONES) { + def checkLongToTimestamp(str: Long, expected: Long): Unit = { + checkEvaluation(cast(str, TimestampType, Option(tz.getID)), expected) + } + checkLongToTimestamp(253402272000L, 253402272000000000L) + checkLongToTimestamp(-5L, -5000000L) + checkLongToTimestamp(1L, 1000000L) + checkLongToTimestamp(0L, 0L) + checkLongToTimestamp(123L, 123000000L) + } + } + } }