From 195090afcc8ed138336b353edc0a4db6f0f5f168 Mon Sep 17 00:00:00 2001 From: Gengliang Wang Date: Tue, 15 Jun 2021 12:15:13 +0300 Subject: [PATCH] [SPARK-35764][SQL] Assign pretty names to TimestampWithoutTZType ### What changes were proposed in this pull request? In the PR, I propose to override the typeName() method in TimestampWithoutTZType, and assign it a name according to the ANSI SQL standard ![image](https://user-images.githubusercontent.com/1097932/122013859-2cf50680-cdf1-11eb-9fcd-0ec1b59fb5c0.png) ### Why are the changes needed? To improve Spark SQL user experience, and have readable types in error messages. ### Does this PR introduce _any_ user-facing change? No, the new timestamp type is not released yet. ### How was this patch tested? Unit test Closes #32915 from gengliangwang/typename. Authored-by: Gengliang Wang Signed-off-by: Max Gekk --- .../spark/sql/types/TimestampWithoutTZType.scala | 2 ++ .../spark/sql/catalyst/expressions/CastSuite.scala | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/types/TimestampWithoutTZType.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/types/TimestampWithoutTZType.scala index 558f5ee94a..856d5497f7 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/types/TimestampWithoutTZType.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/types/TimestampWithoutTZType.scala @@ -48,6 +48,8 @@ class TimestampWithoutTZType private() extends AtomicType { */ override def defaultSize: Int = 8 + override def typeName: String = "timestamp without time zone" + private[spark] override def asNullable: TimestampWithoutTZType = this } 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 c268d52c4f..910c757233 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 @@ -1295,6 +1295,19 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase { } } } + + test("disallow type conversions between Numeric types and Timestamp without time zone type") { + import DataTypeTestUtils.numericTypes + checkInvalidCastFromNumericType(TimestampWithoutTZType) + var errorMsg = "cannot cast bigint to timestamp without time zone" + verifyCastFailure(cast(Literal(0L), TimestampWithoutTZType), Some(errorMsg)) + + val timestampWithoutTZLiteral = Literal.create(LocalDateTime.now(), TimestampWithoutTZType) + errorMsg = "cannot cast timestamp without time zone to" + numericTypes.foreach { numericType => + verifyCastFailure(cast(timestampWithoutTZLiteral, numericType), Some(errorMsg)) + } + } } /**