From 9f5bd0da1674ea79a362ca7043cddea0c3e386a7 Mon Sep 17 00:00:00 2001 From: Xinyi Yu Date: Sun, 17 Apr 2022 18:33:44 -0700 Subject: [PATCH 1/9] update the error class and related code --- .../main/resources/error/error-classes.json | 8 +- .../spark/sql/catalyst/expressions/Cast.scala | 11 ++- .../sql/catalyst/util/UTF8StringUtils.scala | 13 +-- .../sql/errors/QueryExecutionErrors.scala | 9 +- .../org/apache/spark/sql/types/Decimal.scala | 2 +- .../expressions/AnsiCastSuiteBase.scala | 47 +++++++---- .../catalyst/expressions/TryCastSuite.scala | 3 +- .../apache/spark/sql/types/DecimalSuite.scala | 2 +- .../sql-tests/results/ansi/cast.sql.out | 84 +++++++++---------- .../sql-tests/results/ansi/date.sql.out | 8 +- .../sql-tests/results/ansi/interval.sql.out | 12 +-- .../results/ansi/string-functions.sql.out | 18 ++-- .../results/postgreSQL/float4.sql.out | 8 +- .../results/postgreSQL/float8.sql.out | 8 +- .../sql-tests/results/postgreSQL/text.sql.out | 8 +- .../results/postgreSQL/window_part2.sql.out | 4 +- .../results/postgreSQL/window_part4.sql.out | 2 +- .../results/string-functions.sql.out | 2 +- .../apache/spark/sql/SQLInsertTestSuite.scala | 2 +- 19 files changed, 135 insertions(+), 116 deletions(-) diff --git a/core/src/main/resources/error/error-classes.json b/core/src/main/resources/error/error-classes.json index f8c7084e0a19c..91bce48ad2c10 100644 --- a/core/src/main/resources/error/error-classes.json +++ b/core/src/main/resources/error/error-classes.json @@ -102,13 +102,13 @@ "message" : [ "The fraction of sec must be zero. Valid range is [0, 60]. If necessary set %s to false to bypass this error. " ], "sqlState" : "22023" }, - "INVALID_INPUT_SYNTAX_FOR_NUMERIC_TYPE" : { - "message" : [ "invalid input syntax for type numeric: %s. To return NULL instead, use 'try_cast'. If necessary set %s to false to bypass this error." ], - "sqlState" : "42000" - }, "INVALID_JSON_SCHEMA_MAPTYPE" : { "message" : [ "Input schema %s can only contain StringType as a key type for a MapType." ] }, + "INVALID_LITERAL_FORMAT_FOR_CAST" : { + "message" : [ "Invalid `%s` literal: %s. To return NULL instead, use 'try_cast'. If necessary set %s to false to bypass this error." ], + "sqlState" : "42000" + }, "INVALID_PANDAS_UDF_PLACEMENT" : { "message" : [ "The group aggregate pandas UDF %s cannot be invoked together with as other, non-pandas aggregate functions." ] }, 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 03ecaecca066c..322bf97d983b4 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 @@ -844,7 +844,7 @@ abstract class CastBase extends UnaryExpression with TimeZoneAwareExpression wit case _: NumberFormatException => val d = Cast.processFloatingPointSpecialLiterals(doubleStr, false) if(ansiEnabled && d == null) { - throw QueryExecutionErrors.invalidInputSyntaxForNumericError(s) + throw QueryExecutionErrors.invalidInputSyntaxForNumericError(DoubleType, s) } else { d } @@ -869,7 +869,7 @@ abstract class CastBase extends UnaryExpression with TimeZoneAwareExpression wit case _: NumberFormatException => val f = Cast.processFloatingPointSpecialLiterals(floatStr, true) if (ansiEnabled && f == null) { - throw QueryExecutionErrors.invalidInputSyntaxForNumericError(s) + throw QueryExecutionErrors.invalidInputSyntaxForNumericError(FloatType, s) } else { f } @@ -1886,7 +1886,8 @@ abstract class CastBase extends UnaryExpression with TimeZoneAwareExpression wit val floatStr = ctx.freshVariable("floatStr", StringType) (c, evPrim, evNull) => val handleNull = if (ansiEnabled) { - s"throw QueryExecutionErrors.invalidInputSyntaxForNumericError($c);" + s"""throw QueryExecutionErrors.invalidInputSyntaxForNumericError( + |org.apache.spark.sql.types.FloatType$$.MODULE$$, $c);""".stripMargin } else { s"$evNull = true;" } @@ -1921,8 +1922,10 @@ abstract class CastBase extends UnaryExpression with TimeZoneAwareExpression wit case StringType => val doubleStr = ctx.freshVariable("doubleStr", StringType) (c, evPrim, evNull) => + val dt = ctx.addReferenceObj("doubleType", DoubleType, DoubleType.getClass.getName) val handleNull = if (ansiEnabled) { - s"throw QueryExecutionErrors.invalidInputSyntaxForNumericError($c);" + s"""throw QueryExecutionErrors.invalidInputSyntaxForNumericError( + |org.apache.spark.sql.types.DoubleType$$.MODULE$$, $c);""".stripMargin } else { s"$evNull = true;" } diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/UTF8StringUtils.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/UTF8StringUtils.scala index 7fb564d1bd35d..700a0d2c5bbcb 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/UTF8StringUtils.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/UTF8StringUtils.scala @@ -18,6 +18,7 @@ package org.apache.spark.sql.catalyst.util import org.apache.spark.sql.errors.QueryExecutionErrors +import org.apache.spark.sql.types.{ByteType, DataType, IntegerType, LongType, ShortType} import org.apache.spark.unsafe.types.UTF8String /** @@ -25,20 +26,20 @@ import org.apache.spark.unsafe.types.UTF8String */ object UTF8StringUtils { - def toLongExact(s: UTF8String): Long = withException(s.toLongExact) + def toLongExact(s: UTF8String): Long = withException(s.toLongExact, LongType, s) - def toIntExact(s: UTF8String): Int = withException(s.toIntExact) + def toIntExact(s: UTF8String): Int = withException(s.toIntExact, IntegerType, s) - def toShortExact(s: UTF8String): Short = withException(s.toShortExact) + def toShortExact(s: UTF8String): Short = withException(s.toShortExact, ShortType, s) - def toByteExact(s: UTF8String): Byte = withException(s.toByteExact) + def toByteExact(s: UTF8String): Byte = withException(s.toByteExact, ByteType, s) - private def withException[A](f: => A): A = { + private def withException[A](f: => A, to: DataType, s: UTF8String): A = { try { f } catch { case e: NumberFormatException => - throw QueryExecutionErrors.invalidInputSyntaxForNumericError(e) + throw QueryExecutionErrors.invalidInputSyntaxForNumericError(to, s) } } } diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala index 1dcbe82126d4a..5c0c04dcbdc38 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala @@ -109,9 +109,12 @@ object QueryExecutionErrors extends QueryErrorsBase { s"If necessary set ${SQLConf.ANSI_ENABLED.key} to false to bypass this error.") } - def invalidInputSyntaxForNumericError(s: UTF8String): NumberFormatException = { - new SparkNumberFormatException(errorClass = "INVALID_INPUT_SYNTAX_FOR_NUMERIC_TYPE", - messageParameters = Array(toSQLValue(s, StringType), SQLConf.ANSI_ENABLED.key)) + def invalidInputSyntaxForNumericError( + to: AbstractDataType, + s: UTF8String): NumberFormatException = { + new SparkNumberFormatException(errorClass = "INVALID_LITERAL_FORMAT_FOR_CAST", + messageParameters = Array(to.simpleString, + toSQLValue(s, StringType), SQLConf.ANSI_ENABLED.key)) } def cannotCastFromNullTypeError(to: DataType): Throwable = { diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/types/Decimal.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/types/Decimal.scala index 39c7e6ba58007..f8a82396ce04e 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/types/Decimal.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/types/Decimal.scala @@ -626,7 +626,7 @@ object Decimal { } } catch { case _: NumberFormatException => - throw QueryExecutionErrors.invalidInputSyntaxForNumericError(str) + throw QueryExecutionErrors.invalidInputSyntaxForNumericError(DecimalType, str) } } diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/AnsiCastSuiteBase.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/AnsiCastSuiteBase.scala index 6494fb29fda59..1d10062e4dd1f 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/AnsiCastSuiteBase.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/AnsiCastSuiteBase.scala @@ -175,41 +175,52 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase { // cast to IntegerType Seq(IntegerType, ShortType, ByteType, LongType).foreach { dataType => checkExceptionInExpression[NumberFormatException]( - cast("string", dataType), "invalid input syntax for type numeric: 'string'") + cast("string", dataType), s"Invalid `${dataType.simpleString}` literal: 'string'") checkExceptionInExpression[NumberFormatException]( - cast("123-string", dataType), "invalid input syntax for type numeric: '123-string'") + cast("123-string", dataType), s"Invalid `${dataType.simpleString}` literal: '123-string'") checkExceptionInExpression[NumberFormatException]( - cast("2020-07-19", dataType), "invalid input syntax for type numeric: '2020-07-19'") + cast("2020-07-19", dataType), s"Invalid `${dataType.simpleString}` literal: '2020-07-19'") checkExceptionInExpression[NumberFormatException]( - cast("1.23", dataType), "invalid input syntax for type numeric: '1.23'") + cast("1.23", dataType), s"Invalid `${dataType.simpleString}` literal: '1.23'") } - Seq(DoubleType, FloatType, DecimalType.USER_DEFAULT).foreach { dataType => + Seq(DoubleType, FloatType).foreach { dataType => checkExceptionInExpression[NumberFormatException]( - cast("string", dataType), "invalid input syntax for type numeric: 'string'") + cast("string", dataType), s"Invalid `${dataType.simpleString}` literal: 'string'") checkExceptionInExpression[NumberFormatException]( - cast("123.000.00", dataType), "invalid input syntax for type numeric: '123.000.00'") + cast("123.000.00", dataType), s"Invalid `${dataType.simpleString}` literal: '123.000.00'") checkExceptionInExpression[NumberFormatException]( - cast("abc.com", dataType), "invalid input syntax for type numeric: 'abc.com'") + cast("abc.com", dataType), s"Invalid `${dataType.simpleString}` literal: 'abc.com'") } + + checkExceptionInExpression[NumberFormatException]( + cast("string", DecimalType.USER_DEFAULT), + s"Invalid `${DecimalType.simpleString}` literal: 'string'") + checkExceptionInExpression[NumberFormatException]( + cast("123.000.00", DecimalType.USER_DEFAULT), + s"Invalid `${DecimalType.simpleString}` literal: '123.000.00'") + checkExceptionInExpression[NumberFormatException]( + cast("abc.com", DecimalType.USER_DEFAULT), + s"Invalid `${DecimalType.simpleString}` literal: 'abc.com'") } - protected def checkCastToNumericError(l: Literal, to: DataType, tryCastResult: Any): Unit = { + protected def checkCastToNumericError(l: Literal, to: DataType, + expectedDataTypeInErrorMsg: DataType, tryCastResult: Any): Unit = { checkExceptionInExpression[NumberFormatException]( - cast(l, to), "invalid input syntax for type numeric: 'true'") + cast(l, to), s"Invalid `${expectedDataTypeInErrorMsg.simpleString}` literal: 'true'") } test("cast from invalid string array to numeric array should throw NumberFormatException") { val array = Literal.create(Seq("123", "true", "f", null), ArrayType(StringType, containsNull = true)) - checkCastToNumericError(array, ArrayType(ByteType, containsNull = true), + checkCastToNumericError(array, ArrayType(ByteType, containsNull = true), ByteType, Seq(123.toByte, null, null, null)) - checkCastToNumericError(array, ArrayType(ShortType, containsNull = true), + checkCastToNumericError(array, ArrayType(ShortType, containsNull = true), ShortType, Seq(123.toShort, null, null, null)) - checkCastToNumericError(array, ArrayType(IntegerType, containsNull = true), + checkCastToNumericError(array, ArrayType(IntegerType, containsNull = true), IntegerType, Seq(123, null, null, null)) - checkCastToNumericError(array, ArrayType(LongType, containsNull = true), + checkCastToNumericError(array, ArrayType(LongType, containsNull = true), LongType, Seq(123L, null, null, null)) } @@ -243,7 +254,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase { checkExceptionInExpression[NumberFormatException]( cast("abcd", DecimalType(38, 1)), - "invalid input syntax for type numeric") + s"Invalid `${DecimalType.simpleString}` literal: 'abcd'") } protected def checkCastToBooleanError(l: Literal, to: DataType, tryCastResult: Any): Unit = { @@ -369,7 +380,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase { assert(ret.resolved == !isTryCast) if (!isTryCast) { checkExceptionInExpression[NumberFormatException]( - ret, "invalid input syntax for type numeric") + ret, s"Invalid `${IntegerType.simpleString}` literal:") } } @@ -387,7 +398,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase { assert(ret.resolved == !isTryCast) if (!isTryCast) { checkExceptionInExpression[NumberFormatException]( - ret, "invalid input syntax for type numeric") + ret, s"Invalid `${IntegerType.simpleString}` literal:") } } } @@ -512,7 +523,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase { assert(ret.resolved === !isTryCast) if (!isTryCast) { checkExceptionInExpression[NumberFormatException]( - ret, "invalid input syntax for type numeric") + ret, s"Invalid `${IntegerType.simpleString}` literal:") } } diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/TryCastSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/TryCastSuite.scala index 1394ec8c8e2fc..bb9ab88894741 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/TryCastSuite.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/TryCastSuite.scala @@ -45,7 +45,8 @@ class TryCastSuite extends AnsiCastSuiteBase { checkEvaluation(cast(l, to), tryCastResult, InternalRow(l.value)) } - override def checkCastToNumericError(l: Literal, to: DataType, tryCastResult: Any): Unit = { + override def checkCastToNumericError(l: Literal, to: DataType, + expectedDataTypeInErrorMsg: DataType, tryCastResult: Any): Unit = { checkEvaluation(cast(l, to), tryCastResult, InternalRow(l.value)) } diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/types/DecimalSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/types/DecimalSuite.scala index 5433c561a0379..3618298923055 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/types/DecimalSuite.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/types/DecimalSuite.scala @@ -284,7 +284,7 @@ class DecimalSuite extends SparkFunSuite with PrivateMethodTester with SQLHelper assert(Decimal.fromString(UTF8String.fromString("str")) === null) val e = intercept[NumberFormatException](Decimal.fromStringANSI(UTF8String.fromString("str"))) - assert(e.getMessage.contains("invalid input syntax for type numeric")) + assert(e.getMessage.contains(s"Invalid `${DecimalType.simpleString}` literal: 'str'")) } test("SPARK-35841: Casting string to decimal type doesn't work " + diff --git a/sql/core/src/test/resources/sql-tests/results/ansi/cast.sql.out b/sql/core/src/test/resources/sql-tests/results/ansi/cast.sql.out index 6b705274dc885..9c4c4d80f13e1 100644 --- a/sql/core/src/test/resources/sql-tests/results/ansi/cast.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/ansi/cast.sql.out @@ -7,8 +7,8 @@ SELECT CAST('1.23' AS int) -- !query schema struct<> -- !query output -java.lang.NumberFormatException -invalid input syntax for type numeric: '1.23'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +org.apache.spark.SparkNumberFormatException +Invalid `int` literal: '1.23'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -16,8 +16,8 @@ SELECT CAST('1.23' AS long) -- !query schema struct<> -- !query output -java.lang.NumberFormatException -invalid input syntax for type numeric: '1.23'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +org.apache.spark.SparkNumberFormatException +Invalid `bigint` literal: '1.23'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -25,8 +25,8 @@ SELECT CAST('-4.56' AS int) -- !query schema struct<> -- !query output -java.lang.NumberFormatException -invalid input syntax for type numeric: '-4.56'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +org.apache.spark.SparkNumberFormatException +Invalid `int` literal: '-4.56'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -34,8 +34,8 @@ SELECT CAST('-4.56' AS long) -- !query schema struct<> -- !query output -java.lang.NumberFormatException -invalid input syntax for type numeric: '-4.56'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +org.apache.spark.SparkNumberFormatException +Invalid `bigint` literal: '-4.56'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -43,8 +43,8 @@ SELECT CAST('abc' AS int) -- !query schema struct<> -- !query output -java.lang.NumberFormatException -invalid input syntax for type numeric: 'abc'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +org.apache.spark.SparkNumberFormatException +Invalid `int` literal: 'abc'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -52,8 +52,8 @@ SELECT CAST('abc' AS long) -- !query schema struct<> -- !query output -java.lang.NumberFormatException -invalid input syntax for type numeric: 'abc'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +org.apache.spark.SparkNumberFormatException +Invalid `bigint` literal: 'abc'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -61,8 +61,8 @@ SELECT CAST('1234567890123' AS int) -- !query schema struct<> -- !query output -java.lang.NumberFormatException -invalid input syntax for type numeric: '1234567890123'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +org.apache.spark.SparkNumberFormatException +Invalid `int` literal: '1234567890123'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -70,8 +70,8 @@ SELECT CAST('12345678901234567890123' AS long) -- !query schema struct<> -- !query output -java.lang.NumberFormatException -invalid input syntax for type numeric: '12345678901234567890123'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +org.apache.spark.SparkNumberFormatException +Invalid `bigint` literal: '12345678901234567890123'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -79,8 +79,8 @@ SELECT CAST('' AS int) -- !query schema struct<> -- !query output -java.lang.NumberFormatException -invalid input syntax for type numeric: ''. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +org.apache.spark.SparkNumberFormatException +Invalid `int` literal: ''. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -88,8 +88,8 @@ SELECT CAST('' AS long) -- !query schema struct<> -- !query output -java.lang.NumberFormatException -invalid input syntax for type numeric: ''. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +org.apache.spark.SparkNumberFormatException +Invalid `bigint` literal: ''. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -113,8 +113,8 @@ SELECT CAST('123.a' AS int) -- !query schema struct<> -- !query output -java.lang.NumberFormatException -invalid input syntax for type numeric: '123.a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +org.apache.spark.SparkNumberFormatException +Invalid `int` literal: '123.a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -122,8 +122,8 @@ SELECT CAST('123.a' AS long) -- !query schema struct<> -- !query output -java.lang.NumberFormatException -invalid input syntax for type numeric: '123.a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +org.apache.spark.SparkNumberFormatException +Invalid `bigint` literal: '123.a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -139,8 +139,8 @@ SELECT CAST('-2147483649' AS int) -- !query schema struct<> -- !query output -java.lang.NumberFormatException -invalid input syntax for type numeric: '-2147483649'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +org.apache.spark.SparkNumberFormatException +Invalid `int` literal: '-2147483649'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -156,8 +156,8 @@ SELECT CAST('2147483648' AS int) -- !query schema struct<> -- !query output -java.lang.NumberFormatException -invalid input syntax for type numeric: '2147483648'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +org.apache.spark.SparkNumberFormatException +Invalid `int` literal: '2147483648'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -173,8 +173,8 @@ SELECT CAST('-9223372036854775809' AS long) -- !query schema struct<> -- !query output -java.lang.NumberFormatException -invalid input syntax for type numeric: '-9223372036854775809'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +org.apache.spark.SparkNumberFormatException +Invalid `bigint` literal: '-9223372036854775809'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -190,8 +190,8 @@ SELECT CAST('9223372036854775808' AS long) -- !query schema struct<> -- !query output -java.lang.NumberFormatException -invalid input syntax for type numeric: '9223372036854775808'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +org.apache.spark.SparkNumberFormatException +Invalid `bigint` literal: '9223372036854775808'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -446,8 +446,8 @@ select cast('1中文' as tinyint) -- !query schema struct<> -- !query output -java.lang.NumberFormatException -invalid input syntax for type numeric: '1中文'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +org.apache.spark.SparkNumberFormatException +Invalid `tinyint` literal: '1中文'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -455,8 +455,8 @@ select cast('1中文' as smallint) -- !query schema struct<> -- !query output -java.lang.NumberFormatException -invalid input syntax for type numeric: '1中文'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +org.apache.spark.SparkNumberFormatException +Invalid `smallint` literal: '1中文'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -464,8 +464,8 @@ select cast('1中文' as INT) -- !query schema struct<> -- !query output -java.lang.NumberFormatException -invalid input syntax for type numeric: '1中文'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +org.apache.spark.SparkNumberFormatException +Invalid `int` literal: '1中文'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -473,8 +473,8 @@ select cast('中文1' as bigint) -- !query schema struct<> -- !query output -java.lang.NumberFormatException -invalid input syntax for type numeric: '中文1'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +org.apache.spark.SparkNumberFormatException +Invalid `bigint` literal: '中文1'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -482,8 +482,8 @@ select cast('1中文' as bigint) -- !query schema struct<> -- !query output -java.lang.NumberFormatException -invalid input syntax for type numeric: '1中文'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +org.apache.spark.SparkNumberFormatException +Invalid `bigint` literal: '1中文'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query diff --git a/sql/core/src/test/resources/sql-tests/results/ansi/date.sql.out b/sql/core/src/test/resources/sql-tests/results/ansi/date.sql.out index c7058cd7e3be4..9474483ff7d5d 100644 --- a/sql/core/src/test/resources/sql-tests/results/ansi/date.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/ansi/date.sql.out @@ -323,8 +323,8 @@ select date_add('2011-11-11', '1.2') -- !query schema struct<> -- !query output -java.lang.NumberFormatException -invalid input syntax for type numeric: '1.2'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +org.apache.spark.SparkNumberFormatException +Invalid `int` literal: '1.2'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -431,8 +431,8 @@ select date_sub(date'2011-11-11', '1.2') -- !query schema struct<> -- !query output -java.lang.NumberFormatException -invalid input syntax for type numeric: '1.2'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +org.apache.spark.SparkNumberFormatException +Invalid `int` literal: '1.2'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query diff --git a/sql/core/src/test/resources/sql-tests/results/ansi/interval.sql.out b/sql/core/src/test/resources/sql-tests/results/ansi/interval.sql.out index 8f88727f66fb8..329ed0bd28662 100644 --- a/sql/core/src/test/resources/sql-tests/results/ansi/interval.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/ansi/interval.sql.out @@ -122,7 +122,7 @@ select interval 2 second * 'a' struct<> -- !query output org.apache.spark.SparkNumberFormatException -invalid input syntax for type numeric: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid `double` literal: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -131,7 +131,7 @@ select interval 2 second / 'a' struct<> -- !query output org.apache.spark.SparkNumberFormatException -invalid input syntax for type numeric: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid `double` literal: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -140,7 +140,7 @@ select interval 2 year * 'a' struct<> -- !query output org.apache.spark.SparkNumberFormatException -invalid input syntax for type numeric: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid `double` literal: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -149,7 +149,7 @@ select interval 2 year / 'a' struct<> -- !query output org.apache.spark.SparkNumberFormatException -invalid input syntax for type numeric: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid `double` literal: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -174,7 +174,7 @@ select 'a' * interval 2 second struct<> -- !query output org.apache.spark.SparkNumberFormatException -invalid input syntax for type numeric: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid `double` literal: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -183,7 +183,7 @@ select 'a' * interval 2 year struct<> -- !query output org.apache.spark.SparkNumberFormatException -invalid input syntax for type numeric: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid `double` literal: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query diff --git a/sql/core/src/test/resources/sql-tests/results/ansi/string-functions.sql.out b/sql/core/src/test/resources/sql-tests/results/ansi/string-functions.sql.out index c65384673c2b1..e122a8f5bd847 100644 --- a/sql/core/src/test/resources/sql-tests/results/ansi/string-functions.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/ansi/string-functions.sql.out @@ -1,5 +1,5 @@ -- Automatically generated by SQLQueryTestSuite --- Number of queries: 142 +-- Number of queries: 143 -- !query @@ -81,8 +81,8 @@ select left("abcd", -2), left("abcd", 0), left("abcd", 'a') -- !query schema struct<> -- !query output -java.lang.NumberFormatException -invalid input syntax for type numeric: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +org.apache.spark.SparkNumberFormatException +Invalid `int` literal: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -106,8 +106,8 @@ select right("abcd", -2), right("abcd", 0), right("abcd", 'a') -- !query schema struct<> -- !query output -java.lang.NumberFormatException -invalid input syntax for type numeric: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +org.apache.spark.SparkNumberFormatException +Invalid `int` literal: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -412,8 +412,8 @@ SELECT lpad('hi', 'invalid_length') -- !query schema struct<> -- !query output -java.lang.NumberFormatException -invalid input syntax for type numeric: 'invalid_length'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +org.apache.spark.SparkNumberFormatException +Invalid `int` literal: 'invalid_length'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -421,8 +421,8 @@ SELECT rpad('hi', 'invalid_length') -- !query schema struct<> -- !query output -java.lang.NumberFormatException -invalid input syntax for type numeric: 'invalid_length'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +org.apache.spark.SparkNumberFormatException +Invalid `int` literal: 'invalid_length'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query diff --git a/sql/core/src/test/resources/sql-tests/results/postgreSQL/float4.sql.out b/sql/core/src/test/resources/sql-tests/results/postgreSQL/float4.sql.out index eccfdbae75768..6c1db84d1b592 100644 --- a/sql/core/src/test/resources/sql-tests/results/postgreSQL/float4.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/postgreSQL/float4.sql.out @@ -96,7 +96,7 @@ SELECT float('N A N') struct<> -- !query output org.apache.spark.SparkNumberFormatException -invalid input syntax for type numeric: 'N A N'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid `float` literal: 'N A N'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -105,7 +105,7 @@ SELECT float('NaN x') struct<> -- !query output org.apache.spark.SparkNumberFormatException -invalid input syntax for type numeric: 'NaN x'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid `float` literal: 'NaN x'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -114,7 +114,7 @@ SELECT float(' INFINITY x') struct<> -- !query output org.apache.spark.SparkNumberFormatException -invalid input syntax for type numeric: ' INFINITY x'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid `float` literal: ' INFINITY x'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -147,7 +147,7 @@ SELECT float(decimal('nan')) struct<> -- !query output org.apache.spark.SparkNumberFormatException -invalid input syntax for type numeric: 'nan'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid `decimal` literal: 'nan'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query diff --git a/sql/core/src/test/resources/sql-tests/results/postgreSQL/float8.sql.out b/sql/core/src/test/resources/sql-tests/results/postgreSQL/float8.sql.out index d143e1f1c5991..d74cff543c525 100644 --- a/sql/core/src/test/resources/sql-tests/results/postgreSQL/float8.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/postgreSQL/float8.sql.out @@ -128,7 +128,7 @@ SELECT double('N A N') struct<> -- !query output org.apache.spark.SparkNumberFormatException -invalid input syntax for type numeric: 'N A N'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid `double` literal: 'N A N'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -137,7 +137,7 @@ SELECT double('NaN x') struct<> -- !query output org.apache.spark.SparkNumberFormatException -invalid input syntax for type numeric: 'NaN x'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid `double` literal: 'NaN x'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -146,7 +146,7 @@ SELECT double(' INFINITY x') struct<> -- !query output org.apache.spark.SparkNumberFormatException -invalid input syntax for type numeric: ' INFINITY x'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid `double` literal: ' INFINITY x'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -179,7 +179,7 @@ SELECT double(decimal('nan')) struct<> -- !query output org.apache.spark.SparkNumberFormatException -invalid input syntax for type numeric: 'nan'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid `decimal` literal: 'nan'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query diff --git a/sql/core/src/test/resources/sql-tests/results/postgreSQL/text.sql.out b/sql/core/src/test/resources/sql-tests/results/postgreSQL/text.sql.out index 9f9f212c731b2..3577a204201b6 100755 --- a/sql/core/src/test/resources/sql-tests/results/postgreSQL/text.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/postgreSQL/text.sql.out @@ -64,8 +64,8 @@ select string('four: ') || 2+2 -- !query schema struct<> -- !query output -java.lang.NumberFormatException -invalid input syntax for type numeric: 'four: 2'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +org.apache.spark.SparkNumberFormatException +Invalid `bigint` literal: 'four: 2'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query @@ -73,8 +73,8 @@ select 'four: ' || 2+2 -- !query schema struct<> -- !query output -java.lang.NumberFormatException -invalid input syntax for type numeric: 'four: 2'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +org.apache.spark.SparkNumberFormatException +Invalid `bigint` literal: 'four: 2'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query diff --git a/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part2.sql.out b/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part2.sql.out index 158196e7c8280..a2b827d9f2b02 100644 --- a/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part2.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part2.sql.out @@ -461,8 +461,8 @@ window w as (order by f_numeric range between -- !query schema struct<> -- !query output -java.lang.NumberFormatException -invalid input syntax for type numeric: 'NaN'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +org.apache.spark.SparkNumberFormatException +Invalid `int` literal: 'NaN'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query diff --git a/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part4.sql.out b/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part4.sql.out index c937d6637716a..aa1c8f011136b 100644 --- a/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part4.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part4.sql.out @@ -501,4 +501,4 @@ FROM (VALUES(1,1),(2,2),(3,(cast('nan' as int))),(4,3),(5,4)) t(a,b) struct<> -- !query output org.apache.spark.sql.AnalysisException -failed to evaluate expression CAST('nan' AS INT): invalid input syntax for type numeric: 'nan'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error.; line 3 pos 6 +failed to evaluate expression CAST('nan' AS INT): Invalid `int` literal: 'nan'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error.; line 3 pos 6 diff --git a/sql/core/src/test/resources/sql-tests/results/string-functions.sql.out b/sql/core/src/test/resources/sql-tests/results/string-functions.sql.out index af861e3913b6e..dc72dfe137d7e 100644 --- a/sql/core/src/test/resources/sql-tests/results/string-functions.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/string-functions.sql.out @@ -1,5 +1,5 @@ -- Automatically generated by SQLQueryTestSuite --- Number of queries: 142 +-- Number of queries: 143 -- !query diff --git a/sql/core/src/test/scala/org/apache/spark/sql/SQLInsertTestSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/SQLInsertTestSuite.scala index ab5c66dfec7c6..e617a94f226a8 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/SQLInsertTestSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/SQLInsertTestSuite.scala @@ -314,7 +314,7 @@ trait SQLInsertTestSuite extends QueryTest with SQLTestUtils { val errorMsg = intercept[NumberFormatException] { sql("insert into t partition(a='ansi') values('ansi')") }.getMessage - assert(errorMsg.contains("invalid input syntax for type numeric: 'ansi'")) + assert(errorMsg.contains("Invalid `int` literal: 'ansi'")) } else { sql("insert into t partition(a='ansi') values('ansi')") checkAnswer(sql("select * from t"), Row("ansi", null) :: Nil) From 3d4d79197d963c9c8bdbfbf5eea9d8c448a6ac62 Mon Sep 17 00:00:00 2001 From: Xinyi Yu Date: Sun, 17 Apr 2022 20:34:03 -0700 Subject: [PATCH 2/9] use the SQLid function to wrap type with --- core/src/main/resources/error/error-classes.json | 2 +- .../org/apache/spark/sql/errors/QueryExecutionErrors.scala | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/resources/error/error-classes.json b/core/src/main/resources/error/error-classes.json index 3c7607722de63..1744a8431e8ca 100644 --- a/core/src/main/resources/error/error-classes.json +++ b/core/src/main/resources/error/error-classes.json @@ -106,7 +106,7 @@ "message" : [ "Input schema %s can only contain StringType as a key type for a MapType." ] }, "INVALID_LITERAL_FORMAT_FOR_CAST" : { - "message" : [ "Invalid `%s` literal: %s. To return NULL instead, use 'try_cast'. If necessary set %s to false to bypass this error.%s" ], + "message" : [ "Invalid %s literal: %s. To return NULL instead, use 'try_cast'. If necessary set %s to false to bypass this error.%s" ], "sqlState" : "42000" }, "INVALID_PANDAS_UDF_PLACEMENT" : { diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala index cb8cdab1c4fe7..7686a20d00bf9 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala @@ -116,7 +116,7 @@ object QueryExecutionErrors extends QueryErrorsBase { s: UTF8String, errorContext: String): NumberFormatException = { new SparkNumberFormatException(errorClass = "INVALID_LITERAL_FORMAT_FOR_CAST", - messageParameters = Array(to.simpleString, toSQLValue(s, StringType), + messageParameters = Array(toSQLId(to.simpleString), toSQLValue(s, StringType), SQLConf.ANSI_ENABLED.key, errorContext)) } From e592b4331c3e7e908d693f0f5d5a19106ee7f826 Mon Sep 17 00:00:00 2001 From: Xinyi Yu Date: Sun, 17 Apr 2022 21:50:25 -0700 Subject: [PATCH 3/9] date cast ansi error improvement --- .../apache/spark/sql/errors/QueryExecutionErrors.scala | 10 ++++++++-- .../sql/catalyst/expressions/AnsiCastSuiteBase.scala | 9 +++++---- .../spark/sql/catalyst/util/DateFormatterSuite.scala | 2 +- .../sql/catalyst/util/TimestampFormatterSuite.scala | 2 +- .../test/resources/sql-tests/results/ansi/cast.sql.out | 10 +++++----- .../test/resources/sql-tests/results/ansi/date.sql.out | 2 +- .../results/ansi/datetime-parsing-invalid.sql.out | 4 ++-- .../resources/sql-tests/results/ansi/interval.sql.out | 8 ++++---- .../sql-tests/results/postgreSQL/window_part3.sql.out | 2 +- .../results/timestampNTZ/timestamp-ansi.sql.out | 2 +- 10 files changed, 29 insertions(+), 22 deletions(-) diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala index 7686a20d00bf9..a130709a679f7 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala @@ -1014,8 +1014,14 @@ object QueryExecutionErrors extends QueryErrorsBase { } def cannotCastToDateTimeError(value: Any, to: DataType, errorContext: String): Throwable = { - new DateTimeException(s"Cannot cast $value to $to. To return NULL instead, use 'try_cast'. " + - s"If necessary set ${SQLConf.ANSI_ENABLED.key} to false to bypass this error." + errorContext) + new DateTimeException(s"Invalid ${toSQLId(to.simpleString)} literal: " + + s"${if (value.isInstanceOf[UTF8String]) { + toSQLValue(value, StringType) + } else { + toSQLValue(value) + }}. " + + s"To return NULL instead, use 'try_cast'. If necessary set ${SQLConf.ANSI_ENABLED.key} " + + s"to false to bypass this error." + errorContext) } def registeringStreamingQueryListenerError(e: Exception): Throwable = { diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/AnsiCastSuiteBase.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/AnsiCastSuiteBase.scala index 1d10062e4dd1f..21ae61d9dd904 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/AnsiCastSuiteBase.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/AnsiCastSuiteBase.scala @@ -26,6 +26,7 @@ import org.apache.spark.sql.catalyst.InternalRow import org.apache.spark.sql.catalyst.util.DateTimeConstants.MILLIS_PER_SECOND import org.apache.spark.sql.catalyst.util.DateTimeTestUtils import org.apache.spark.sql.catalyst.util.DateTimeTestUtils.{withDefaultTimeZone, UTC} +import org.apache.spark.sql.errors.QueryExecutionErrors.toSQLValue import org.apache.spark.sql.internal.SQLConf import org.apache.spark.sql.types._ import org.apache.spark.unsafe.types.UTF8String @@ -269,7 +270,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase { protected def checkCastToTimestampError(l: Literal, to: DataType): Unit = { checkExceptionInExpression[DateTimeException]( - cast(l, to), s"Cannot cast $l to $to") + cast(l, to), s"Invalid `timestamp` literal: ${toSQLValue(l)}") } test("cast from timestamp II") { @@ -532,7 +533,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase { def checkCastWithParseError(str: String): Unit = { checkExceptionInExpression[DateTimeException]( cast(Literal(str), TimestampType, Option(zid.getId)), - s"Cannot cast $str to TimestampType.") + s"Invalid `timestamp` literal: '$str'") } checkCastWithParseError("123") @@ -553,7 +554,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase { def checkCastWithParseError(str: String): Unit = { checkExceptionInExpression[DateTimeException]( cast(Literal(str), DateType, Option(zid.getId)), - s"Cannot cast $str to DateType.") + s"Invalid `date` literal: '$str'") } checkCastWithParseError("2015-13-18") @@ -581,7 +582,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase { "2021-06-17 00:00:00ABC").foreach { invalidInput => checkExceptionInExpression[DateTimeException]( cast(invalidInput, TimestampNTZType), - s"Cannot cast $invalidInput to TimestampNTZType") + s"Invalid `timestamp_ntz` literal: '$invalidInput'") } } } diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/DateFormatterSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/DateFormatterSuite.scala index 44c90db7630ac..feb046c1fe686 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/DateFormatterSuite.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/DateFormatterSuite.scala @@ -208,6 +208,6 @@ class DateFormatterSuite extends DatetimeFormatterSuite { val errMsg = intercept[DateTimeException] { formatter.parse("x123") }.getMessage - assert(errMsg.contains("Cannot cast x123 to DateType")) + assert(errMsg.contains("Invalid `date` literal: 'x123'")) } } diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/TimestampFormatterSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/TimestampFormatterSuite.scala index 661e624efa592..bbf364527a981 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/TimestampFormatterSuite.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/TimestampFormatterSuite.scala @@ -453,7 +453,7 @@ class TimestampFormatterSuite extends DatetimeFormatterSuite { val errMsg = intercept[DateTimeException] { formatter.parse("x123") }.getMessage - assert(errMsg.contains("Cannot cast x123 to TimestampType")) + assert(errMsg.contains("Invalid `timestamp` literal: 'x123'")) } } } diff --git a/sql/core/src/test/resources/sql-tests/results/ansi/cast.sql.out b/sql/core/src/test/resources/sql-tests/results/ansi/cast.sql.out index 25c4686fe7ad1..bfebdc22672da 100644 --- a/sql/core/src/test/resources/sql-tests/results/ansi/cast.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/ansi/cast.sql.out @@ -626,7 +626,7 @@ select cast('a' as date) struct<> -- !query output java.time.DateTimeException -Cannot cast a to DateType. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid `date` literal: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select cast('a' as date) ^^^^^^^^^^^^^^^^^ @@ -646,7 +646,7 @@ select cast('a' as timestamp) struct<> -- !query output java.time.DateTimeException -Cannot cast a to TimestampType. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid `timestamp` literal: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select cast('a' as timestamp) ^^^^^^^^^^^^^^^^^^^^^^ @@ -666,7 +666,7 @@ select cast('a' as timestamp_ntz) struct<> -- !query output java.time.DateTimeException -Cannot cast a to TimestampNTZType. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid `timestamp_ntz` literal: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select cast('a' as timestamp_ntz) ^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -678,7 +678,7 @@ select cast(cast('inf' as double) as timestamp) struct<> -- !query output java.time.DateTimeException -Cannot cast Infinity to TimestampType. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid `timestamp` literal: Infinity. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select cast(cast('inf' as double) as timestamp) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -690,7 +690,7 @@ select cast(cast('inf' as float) as timestamp) struct<> -- !query output java.time.DateTimeException -Cannot cast Infinity to TimestampType. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid `timestamp` literal: Infinity. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select cast(cast('inf' as float) as timestamp) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/sql/core/src/test/resources/sql-tests/results/ansi/date.sql.out b/sql/core/src/test/resources/sql-tests/results/ansi/date.sql.out index 5147c29c3b800..e48603dcd18b5 100644 --- a/sql/core/src/test/resources/sql-tests/results/ansi/date.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/ansi/date.sql.out @@ -232,7 +232,7 @@ select next_day("xx", "Mon") struct<> -- !query output java.time.DateTimeException -Cannot cast xx to DateType. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid `date` literal: 'xx'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select next_day("xx", "Mon") ^^^^^^^^^^^^^^^^^^^^^ diff --git a/sql/core/src/test/resources/sql-tests/results/ansi/datetime-parsing-invalid.sql.out b/sql/core/src/test/resources/sql-tests/results/ansi/datetime-parsing-invalid.sql.out index 57e39bbfe3a00..28c3b634f3a7c 100644 --- a/sql/core/src/test/resources/sql-tests/results/ansi/datetime-parsing-invalid.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/ansi/datetime-parsing-invalid.sql.out @@ -242,7 +242,7 @@ select cast("Unparseable" as timestamp) struct<> -- !query output java.time.DateTimeException -Cannot cast Unparseable to TimestampType. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid `timestamp` literal: 'Unparseable'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select cast("Unparseable" as timestamp) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -254,7 +254,7 @@ select cast("Unparseable" as date) struct<> -- !query output java.time.DateTimeException -Cannot cast Unparseable to DateType. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid `date` literal: 'Unparseable'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select cast("Unparseable" as date) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/sql/core/src/test/resources/sql-tests/results/ansi/interval.sql.out b/sql/core/src/test/resources/sql-tests/results/ansi/interval.sql.out index 78a6b50cb34ed..5916c55808d32 100644 --- a/sql/core/src/test/resources/sql-tests/results/ansi/interval.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/ansi/interval.sql.out @@ -1516,7 +1516,7 @@ select '4 11:11' - interval '4 22:12' day to minute struct<> -- !query output java.time.DateTimeException -Cannot cast 4 11:11 to TimestampType. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid `timestamp` literal: '4 11:11'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select '4 11:11' - interval '4 22:12' day to minute ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -1528,7 +1528,7 @@ select '4 12:12:12' + interval '4 22:12' day to minute struct<> -- !query output java.time.DateTimeException -Cannot cast 4 12:12:12 to TimestampType. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid `timestamp` literal: '4 12:12:12'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select '4 12:12:12' + interval '4 22:12' day to minute ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -1566,7 +1566,7 @@ select str - interval '4 22:12' day to minute from interval_view struct<> -- !query output java.time.DateTimeException -Cannot cast 1 to TimestampType. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid `timestamp` literal: '1'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select str - interval '4 22:12' day to minute from interval_view ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -1578,7 +1578,7 @@ select str + interval '4 22:12' day to minute from interval_view struct<> -- !query output java.time.DateTimeException -Cannot cast 1 to TimestampType. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid `timestamp` literal: '1'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select str + interval '4 22:12' day to minute from interval_view ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part3.sql.out b/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part3.sql.out index c799d65985d5d..016e248bbb5fc 100644 --- a/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part3.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part3.sql.out @@ -72,7 +72,7 @@ insert into datetimes values struct<> -- !query output org.apache.spark.sql.AnalysisException -failed to evaluate expression CAST('11:00 BST' AS TIMESTAMP): Cannot cast 11:00 BST to TimestampType. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +failed to evaluate expression CAST('11:00 BST' AS TIMESTAMP): Invalid `timestamp` literal: '11:00 BST'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 2, position 23) == (1, timestamp '11:00', cast ('11:00 BST' as timestamp), cast ('1 year' as timestamp), ... ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/sql/core/src/test/resources/sql-tests/results/timestampNTZ/timestamp-ansi.sql.out b/sql/core/src/test/resources/sql-tests/results/timestampNTZ/timestamp-ansi.sql.out index 3f275b2a2bdeb..a85604ddc62b8 100644 --- a/sql/core/src/test/resources/sql-tests/results/timestampNTZ/timestamp-ansi.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/timestampNTZ/timestamp-ansi.sql.out @@ -332,7 +332,7 @@ select to_timestamp(1) struct<> -- !query output java.time.DateTimeException -Cannot cast 1 to TimestampNTZType. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid `timestamp_ntz` literal: '1'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query From b45f9cbbc936251509af29acb3acc074e58e8a87 Mon Sep 17 00:00:00 2001 From: Xinyi Yu Date: Mon, 18 Apr 2022 18:04:41 -0700 Subject: [PATCH 4/9] address comments; use toSQLType --- .../main/resources/error/error-classes.json | 4 +- .../spark/sql/catalyst/expressions/Cast.scala | 1 - .../spark/sql/errors/QueryErrorsBase.scala | 9 ++-- .../sql/errors/QueryExecutionErrors.scala | 6 +-- .../expressions/AnsiCastSuiteBase.scala | 52 +++++++++--------- .../catalyst/util/DateFormatterSuite.scala | 2 +- .../util/TimestampFormatterSuite.scala | 2 +- .../apache/spark/sql/types/DecimalSuite.scala | 3 +- .../sql-tests/results/ansi/cast.sql.out | 54 +++++++++---------- .../sql-tests/results/ansi/date.sql.out | 6 +-- .../ansi/datetime-parsing-invalid.sql.out | 4 +- .../sql-tests/results/ansi/interval.sql.out | 20 +++---- .../results/ansi/string-functions.sql.out | 8 +-- .../results/postgreSQL/float4.sql.out | 8 +-- .../results/postgreSQL/float8.sql.out | 8 +-- .../sql-tests/results/postgreSQL/text.sql.out | 4 +- .../results/postgreSQL/window_part2.sql.out | 2 +- .../results/postgreSQL/window_part3.sql.out | 2 +- .../results/postgreSQL/window_part4.sql.out | 2 +- .../timestampNTZ/timestamp-ansi.sql.out | 2 +- .../apache/spark/sql/SQLInsertTestSuite.scala | 2 +- 21 files changed, 102 insertions(+), 99 deletions(-) diff --git a/core/src/main/resources/error/error-classes.json b/core/src/main/resources/error/error-classes.json index e8da1dd62b59b..cac114be07e86 100644 --- a/core/src/main/resources/error/error-classes.json +++ b/core/src/main/resources/error/error-classes.json @@ -108,8 +108,8 @@ "INVALID_JSON_SCHEMA_MAPTYPE" : { "message" : [ "Input schema %s can only contain StringType as a key type for a MapType." ] }, - "INVALID_LITERAL_FORMAT_FOR_CAST" : { - "message" : [ "Invalid %s literal: %s. To return NULL instead, use 'try_cast'. If necessary set %s to false to bypass this error.%s" ], + "INVALID_FORMAT_FOR_CAST" : { + "message" : [ "Invalid input value for type %s: %s. To return NULL instead, use 'try_cast'. If necessary set %s to false to bypass this error.%s" ], "sqlState" : "42000" }, "INVALID_PANDAS_UDF_PLACEMENT" : { 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 de62271193fbc..ad23983fc5132 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 @@ -1937,7 +1937,6 @@ abstract class CastBase extends UnaryExpression with TimeZoneAwareExpression wit case StringType => val doubleStr = ctx.freshVariable("doubleStr", StringType) (c, evPrim, evNull) => - val dt = ctx.addReferenceObj("doubleType", DoubleType, DoubleType.getClass.getName) val handleNull = if (ansiEnabled) { val errorContext = ctx.addReferenceObj("errCtx", origin.context) s"throw QueryExecutionErrors.invalidInputSyntaxForNumericError(" + diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryErrorsBase.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryErrorsBase.scala index 4708b47cfb890..00358dc0c01c4 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryErrorsBase.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryErrorsBase.scala @@ -19,7 +19,7 @@ package org.apache.spark.sql.errors import org.apache.spark.sql.catalyst.expressions.Literal import org.apache.spark.sql.catalyst.util.quoteIdentifier -import org.apache.spark.sql.types.{DataType, DoubleType, FloatType} +import org.apache.spark.sql.types.{AbstractDataType, DataType, DoubleType, FloatType} trait QueryErrorsBase { private def litToErrorValue(l: Literal): String = l match { @@ -59,7 +59,10 @@ trait QueryErrorsBase { toSQLId(parts.split("\\.")) } - def toSQLType(t: DataType): String = { - t.sql + def toSQLType(t: AbstractDataType): String = { + t match { + case dt: DataType => dt.sql + case adt => adt.simpleString + } } } diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala index 97df301555806..524109664e115 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala @@ -115,8 +115,8 @@ object QueryExecutionErrors extends QueryErrorsBase { to: AbstractDataType, s: UTF8String, errorContext: String): NumberFormatException = { - new SparkNumberFormatException(errorClass = "INVALID_LITERAL_FORMAT_FOR_CAST", - messageParameters = Array(toSQLId(to.simpleString), toSQLValue(s, StringType), + new SparkNumberFormatException(errorClass = "INVALID_FORMAT_FOR_CAST", + messageParameters = Array(toSQLType(to), toSQLValue(s, StringType), SQLConf.ANSI_ENABLED.key, errorContext)) } @@ -1013,7 +1013,7 @@ object QueryExecutionErrors extends QueryErrorsBase { } def cannotCastToDateTimeError(value: Any, to: DataType, errorContext: String): Throwable = { - new DateTimeException(s"Invalid ${toSQLId(to.simpleString)} literal: " + + new DateTimeException(s"Invalid input value for type ${toSQLType(to)}: " + s"${if (value.isInstanceOf[UTF8String]) { toSQLValue(value, StringType) } else { diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/AnsiCastSuiteBase.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/AnsiCastSuiteBase.scala index 93486dbc72530..4871addd43fe8 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/AnsiCastSuiteBase.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/AnsiCastSuiteBase.scala @@ -175,40 +175,40 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase { test("cast from invalid string to numeric should throw NumberFormatException") { // cast to IntegerType Seq(IntegerType, ShortType, ByteType, LongType).foreach { dataType => - checkExceptionInExpression[NumberFormatException]( - cast("string", dataType), s"Invalid `${dataType.simpleString}` literal: 'string'") - checkExceptionInExpression[NumberFormatException]( - cast("123-string", dataType), s"Invalid `${dataType.simpleString}` literal: '123-string'") - checkExceptionInExpression[NumberFormatException]( - cast("2020-07-19", dataType), s"Invalid `${dataType.simpleString}` literal: '2020-07-19'") - checkExceptionInExpression[NumberFormatException]( - cast("1.23", dataType), s"Invalid `${dataType.simpleString}` literal: '1.23'") + checkExceptionInExpression[NumberFormatException](cast("string", dataType), + s"Invalid input value for type ${dataType.sql}: 'string'") + checkExceptionInExpression[NumberFormatException](cast("123-string", dataType), + s"Invalid input value for type ${dataType.sql}: '123-string'") + checkExceptionInExpression[NumberFormatException](cast("2020-07-19", dataType), + s"Invalid input value for type ${dataType.sql}: '2020-07-19'") + checkExceptionInExpression[NumberFormatException](cast("1.23", dataType), + s"Invalid input value for type ${dataType.sql}: '1.23'") } Seq(DoubleType, FloatType).foreach { dataType => - checkExceptionInExpression[NumberFormatException]( - cast("string", dataType), s"Invalid `${dataType.simpleString}` literal: 'string'") - checkExceptionInExpression[NumberFormatException]( - cast("123.000.00", dataType), s"Invalid `${dataType.simpleString}` literal: '123.000.00'") - checkExceptionInExpression[NumberFormatException]( - cast("abc.com", dataType), s"Invalid `${dataType.simpleString}` literal: 'abc.com'") + checkExceptionInExpression[NumberFormatException](cast("string", dataType), + s"Invalid input value for type ${dataType.sql}: 'string'") + checkExceptionInExpression[NumberFormatException](cast("123.000.00", dataType), + s"Invalid input value for type ${dataType.sql}: '123.000.00'") + checkExceptionInExpression[NumberFormatException](cast("abc.com", dataType), + s"Invalid input value for type ${dataType.sql}: 'abc.com'") } checkExceptionInExpression[NumberFormatException]( cast("string", DecimalType.USER_DEFAULT), - s"Invalid `${DecimalType.simpleString}` literal: 'string'") + s"Invalid input value for type ${DecimalType.simpleString}: 'string'") checkExceptionInExpression[NumberFormatException]( cast("123.000.00", DecimalType.USER_DEFAULT), - s"Invalid `${DecimalType.simpleString}` literal: '123.000.00'") + s"Invalid input value for type ${DecimalType.simpleString}: '123.000.00'") checkExceptionInExpression[NumberFormatException]( cast("abc.com", DecimalType.USER_DEFAULT), - s"Invalid `${DecimalType.simpleString}` literal: 'abc.com'") + s"Invalid input value for type ${DecimalType.simpleString}: 'abc.com'") } protected def checkCastToNumericError(l: Literal, to: DataType, expectedDataTypeInErrorMsg: DataType, tryCastResult: Any): Unit = { checkExceptionInExpression[NumberFormatException]( - cast(l, to), s"Invalid `${expectedDataTypeInErrorMsg.simpleString}` literal: 'true'") + cast(l, to), s"Invalid input value for type ${expectedDataTypeInErrorMsg.sql}: 'true'") } test("cast from invalid string array to numeric array should throw NumberFormatException") { @@ -255,7 +255,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase { checkExceptionInExpression[NumberFormatException]( cast("abcd", DecimalType(38, 1)), - s"Invalid `${DecimalType.simpleString}` literal: 'abcd'") + s"Invalid input value for type ${DecimalType.simpleString}: 'abcd'") } protected def checkCastToBooleanError(l: Literal, to: DataType, tryCastResult: Any): Unit = { @@ -270,7 +270,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase { protected def checkCastToTimestampError(l: Literal, to: DataType): Unit = { checkExceptionInExpression[DateTimeException]( - cast(l, to), s"Invalid `timestamp` literal: ${toSQLValue(l)}") + cast(l, to), s"Invalid input value for type TIMESTAMP: ${toSQLValue(l)}") } test("cast from timestamp II") { @@ -381,7 +381,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase { assert(ret.resolved == !isTryCast) if (!isTryCast) { checkExceptionInExpression[NumberFormatException]( - ret, s"Invalid `${IntegerType.simpleString}` literal:") + ret, s"Invalid input value for type ${IntegerType.sql}") } } @@ -399,7 +399,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase { assert(ret.resolved == !isTryCast) if (!isTryCast) { checkExceptionInExpression[NumberFormatException]( - ret, s"Invalid `${IntegerType.simpleString}` literal:") + ret, s"Invalid input value for type ${IntegerType.sql}") } } } @@ -524,7 +524,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase { assert(ret.resolved === !isTryCast) if (!isTryCast) { checkExceptionInExpression[NumberFormatException]( - ret, s"Invalid `${IntegerType.simpleString}` literal:") + ret, s"Invalid input value for type ${IntegerType.sql}") } } @@ -533,7 +533,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase { def checkCastWithParseError(str: String): Unit = { checkExceptionInExpression[DateTimeException]( cast(Literal(str), TimestampType, Option(zid.getId)), - s"Invalid `timestamp` literal: '$str'") + s"Invalid input value for type TIMESTAMP: '$str'") } checkCastWithParseError("123") @@ -554,7 +554,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase { def checkCastWithParseError(str: String): Unit = { checkExceptionInExpression[DateTimeException]( cast(Literal(str), DateType, Option(zid.getId)), - s"Invalid `date` literal: '$str'") + s"Invalid input value for type DATE: '$str'") } checkCastWithParseError("2015-13-18") @@ -582,7 +582,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase { "2021-06-17 00:00:00ABC").foreach { invalidInput => checkExceptionInExpression[DateTimeException]( cast(invalidInput, TimestampNTZType), - s"Invalid `timestamp_ntz` literal: '$invalidInput'") + s"Invalid input value for type TIMESTAMP_NTZ: '$invalidInput'") } } } diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/DateFormatterSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/DateFormatterSuite.scala index feb046c1fe686..4804ea267a3e9 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/DateFormatterSuite.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/DateFormatterSuite.scala @@ -208,6 +208,6 @@ class DateFormatterSuite extends DatetimeFormatterSuite { val errMsg = intercept[DateTimeException] { formatter.parse("x123") }.getMessage - assert(errMsg.contains("Invalid `date` literal: 'x123'")) + assert(errMsg.contains("Invalid input value for type DATE: 'x123'")) } } diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/TimestampFormatterSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/TimestampFormatterSuite.scala index bbf364527a981..9bec89d520bc5 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/TimestampFormatterSuite.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/TimestampFormatterSuite.scala @@ -453,7 +453,7 @@ class TimestampFormatterSuite extends DatetimeFormatterSuite { val errMsg = intercept[DateTimeException] { formatter.parse("x123") }.getMessage - assert(errMsg.contains("Invalid `timestamp` literal: 'x123'")) + assert(errMsg.contains("Invalid input value for type TIMESTAMP: 'x123'")) } } } diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/types/DecimalSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/types/DecimalSuite.scala index 3618298923055..534137134e512 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/types/DecimalSuite.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/types/DecimalSuite.scala @@ -284,7 +284,8 @@ class DecimalSuite extends SparkFunSuite with PrivateMethodTester with SQLHelper assert(Decimal.fromString(UTF8String.fromString("str")) === null) val e = intercept[NumberFormatException](Decimal.fromStringANSI(UTF8String.fromString("str"))) - assert(e.getMessage.contains(s"Invalid `${DecimalType.simpleString}` literal: 'str'")) + assert(e.getMessage.contains(s"Invalid input value for type ${DecimalType.simpleString}: " + + "'str'")) } test("SPARK-35841: Casting string to decimal type doesn't work " + diff --git a/sql/core/src/test/resources/sql-tests/results/ansi/cast.sql.out b/sql/core/src/test/resources/sql-tests/results/ansi/cast.sql.out index bfebdc22672da..1c81eafd3174e 100644 --- a/sql/core/src/test/resources/sql-tests/results/ansi/cast.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/ansi/cast.sql.out @@ -8,7 +8,7 @@ SELECT CAST('1.23' AS int) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `int` literal: '1.23'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type INT: '1.23'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT CAST('1.23' AS int) ^^^^^^^^^^^^^^^^^^^ @@ -20,7 +20,7 @@ SELECT CAST('1.23' AS long) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `bigint` literal: '1.23'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type BIGINT: '1.23'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT CAST('1.23' AS long) ^^^^^^^^^^^^^^^^^^^^ @@ -32,7 +32,7 @@ SELECT CAST('-4.56' AS int) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `int` literal: '-4.56'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type INT: '-4.56'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT CAST('-4.56' AS int) ^^^^^^^^^^^^^^^^^^^^ @@ -44,7 +44,7 @@ SELECT CAST('-4.56' AS long) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `bigint` literal: '-4.56'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type BIGINT: '-4.56'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT CAST('-4.56' AS long) ^^^^^^^^^^^^^^^^^^^^^ @@ -56,7 +56,7 @@ SELECT CAST('abc' AS int) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `int` literal: 'abc'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type INT: 'abc'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT CAST('abc' AS int) ^^^^^^^^^^^^^^^^^^ @@ -68,7 +68,7 @@ SELECT CAST('abc' AS long) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `bigint` literal: 'abc'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type BIGINT: 'abc'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT CAST('abc' AS long) ^^^^^^^^^^^^^^^^^^^ @@ -80,7 +80,7 @@ SELECT CAST('1234567890123' AS int) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `int` literal: '1234567890123'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type INT: '1234567890123'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT CAST('1234567890123' AS int) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -92,7 +92,7 @@ SELECT CAST('12345678901234567890123' AS long) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `bigint` literal: '12345678901234567890123'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type BIGINT: '12345678901234567890123'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT CAST('12345678901234567890123' AS long) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -104,7 +104,7 @@ SELECT CAST('' AS int) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `int` literal: ''. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type INT: ''. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT CAST('' AS int) ^^^^^^^^^^^^^^^ @@ -116,7 +116,7 @@ SELECT CAST('' AS long) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `bigint` literal: ''. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type BIGINT: ''. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT CAST('' AS long) ^^^^^^^^^^^^^^^^ @@ -144,7 +144,7 @@ SELECT CAST('123.a' AS int) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `int` literal: '123.a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type INT: '123.a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT CAST('123.a' AS int) ^^^^^^^^^^^^^^^^^^^^ @@ -156,7 +156,7 @@ SELECT CAST('123.a' AS long) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `bigint` literal: '123.a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type BIGINT: '123.a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT CAST('123.a' AS long) ^^^^^^^^^^^^^^^^^^^^^ @@ -176,7 +176,7 @@ SELECT CAST('-2147483649' AS int) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `int` literal: '-2147483649'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type INT: '-2147483649'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT CAST('-2147483649' AS int) ^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -196,7 +196,7 @@ SELECT CAST('2147483648' AS int) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `int` literal: '2147483648'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type INT: '2147483648'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT CAST('2147483648' AS int) ^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -216,7 +216,7 @@ SELECT CAST('-9223372036854775809' AS long) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `bigint` literal: '-9223372036854775809'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type BIGINT: '-9223372036854775809'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT CAST('-9223372036854775809' AS long) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -236,7 +236,7 @@ SELECT CAST('9223372036854775808' AS long) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `bigint` literal: '9223372036854775808'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type BIGINT: '9223372036854775808'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT CAST('9223372036854775808' AS long) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -495,7 +495,7 @@ select cast('1中文' as tinyint) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `tinyint` literal: '1中文'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type TINYINT: '1中文'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select cast('1中文' as tinyint) ^^^^^^^^^^^^^^^^^^^^^^ @@ -507,7 +507,7 @@ select cast('1中文' as smallint) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `smallint` literal: '1中文'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type SMALLINT: '1中文'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select cast('1中文' as smallint) ^^^^^^^^^^^^^^^^^^^^^^^ @@ -519,7 +519,7 @@ select cast('1中文' as INT) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `int` literal: '1中文'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type INT: '1中文'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select cast('1中文' as INT) ^^^^^^^^^^^^^^^^^^ @@ -531,7 +531,7 @@ select cast('中文1' as bigint) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `bigint` literal: '中文1'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type BIGINT: '中文1'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select cast('中文1' as bigint) ^^^^^^^^^^^^^^^^^^^^^ @@ -543,7 +543,7 @@ select cast('1中文' as bigint) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `bigint` literal: '1中文'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type BIGINT: '1中文'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select cast('1中文' as bigint) ^^^^^^^^^^^^^^^^^^^^^ @@ -606,7 +606,7 @@ select cast('xyz' as decimal(4, 2)) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `decimal` literal: 'xyz'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type decimal: 'xyz'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select cast('xyz' as decimal(4, 2)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -626,7 +626,7 @@ select cast('a' as date) struct<> -- !query output java.time.DateTimeException -Invalid `date` literal: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type DATE: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select cast('a' as date) ^^^^^^^^^^^^^^^^^ @@ -646,7 +646,7 @@ select cast('a' as timestamp) struct<> -- !query output java.time.DateTimeException -Invalid `timestamp` literal: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type TIMESTAMP: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select cast('a' as timestamp) ^^^^^^^^^^^^^^^^^^^^^^ @@ -666,7 +666,7 @@ select cast('a' as timestamp_ntz) struct<> -- !query output java.time.DateTimeException -Invalid `timestamp_ntz` literal: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type TIMESTAMP_NTZ: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select cast('a' as timestamp_ntz) ^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -678,7 +678,7 @@ select cast(cast('inf' as double) as timestamp) struct<> -- !query output java.time.DateTimeException -Invalid `timestamp` literal: Infinity. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type TIMESTAMP: Infinity. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select cast(cast('inf' as double) as timestamp) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -690,7 +690,7 @@ select cast(cast('inf' as float) as timestamp) struct<> -- !query output java.time.DateTimeException -Invalid `timestamp` literal: Infinity. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type TIMESTAMP: Infinity. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select cast(cast('inf' as float) as timestamp) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/sql/core/src/test/resources/sql-tests/results/ansi/date.sql.out b/sql/core/src/test/resources/sql-tests/results/ansi/date.sql.out index e48603dcd18b5..80f7c8dc8c92f 100644 --- a/sql/core/src/test/resources/sql-tests/results/ansi/date.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/ansi/date.sql.out @@ -232,7 +232,7 @@ select next_day("xx", "Mon") struct<> -- !query output java.time.DateTimeException -Invalid `date` literal: 'xx'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type DATE: 'xx'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select next_day("xx", "Mon") ^^^^^^^^^^^^^^^^^^^^^ @@ -327,7 +327,7 @@ select date_add('2011-11-11', '1.2') struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `int` literal: '1.2'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type INT: '1.2'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select date_add('2011-11-11', '1.2') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -438,7 +438,7 @@ select date_sub(date'2011-11-11', '1.2') struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `int` literal: '1.2'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type INT: '1.2'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select date_sub(date'2011-11-11', '1.2') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/sql/core/src/test/resources/sql-tests/results/ansi/datetime-parsing-invalid.sql.out b/sql/core/src/test/resources/sql-tests/results/ansi/datetime-parsing-invalid.sql.out index 28c3b634f3a7c..8909a6032b776 100644 --- a/sql/core/src/test/resources/sql-tests/results/ansi/datetime-parsing-invalid.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/ansi/datetime-parsing-invalid.sql.out @@ -242,7 +242,7 @@ select cast("Unparseable" as timestamp) struct<> -- !query output java.time.DateTimeException -Invalid `timestamp` literal: 'Unparseable'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type TIMESTAMP: 'Unparseable'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select cast("Unparseable" as timestamp) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -254,7 +254,7 @@ select cast("Unparseable" as date) struct<> -- !query output java.time.DateTimeException -Invalid `date` literal: 'Unparseable'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type DATE: 'Unparseable'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select cast("Unparseable" as date) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/sql/core/src/test/resources/sql-tests/results/ansi/interval.sql.out b/sql/core/src/test/resources/sql-tests/results/ansi/interval.sql.out index 5916c55808d32..7904a79b3493f 100644 --- a/sql/core/src/test/resources/sql-tests/results/ansi/interval.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/ansi/interval.sql.out @@ -122,7 +122,7 @@ select interval 2 second * 'a' struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `double` literal: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type DOUBLE: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select interval 2 second * 'a' ^^^^^^^^^^^^^^^^^^^^^^^ @@ -134,7 +134,7 @@ select interval 2 second / 'a' struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `double` literal: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type DOUBLE: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select interval 2 second / 'a' ^^^^^^^^^^^^^^^^^^^^^^^ @@ -146,7 +146,7 @@ select interval 2 year * 'a' struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `double` literal: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type DOUBLE: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select interval 2 year * 'a' ^^^^^^^^^^^^^^^^^^^^^ @@ -158,7 +158,7 @@ select interval 2 year / 'a' struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `double` literal: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type DOUBLE: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select interval 2 year / 'a' ^^^^^^^^^^^^^^^^^^^^^ @@ -186,7 +186,7 @@ select 'a' * interval 2 second struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `double` literal: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type DOUBLE: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select 'a' * interval 2 second ^^^^^^^^^^^^^^^^^^^^^^^ @@ -198,7 +198,7 @@ select 'a' * interval 2 year struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `double` literal: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type DOUBLE: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select 'a' * interval 2 year ^^^^^^^^^^^^^^^^^^^^^ @@ -1516,7 +1516,7 @@ select '4 11:11' - interval '4 22:12' day to minute struct<> -- !query output java.time.DateTimeException -Invalid `timestamp` literal: '4 11:11'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type TIMESTAMP: '4 11:11'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select '4 11:11' - interval '4 22:12' day to minute ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -1528,7 +1528,7 @@ select '4 12:12:12' + interval '4 22:12' day to minute struct<> -- !query output java.time.DateTimeException -Invalid `timestamp` literal: '4 12:12:12'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type TIMESTAMP: '4 12:12:12'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select '4 12:12:12' + interval '4 22:12' day to minute ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -1566,7 +1566,7 @@ select str - interval '4 22:12' day to minute from interval_view struct<> -- !query output java.time.DateTimeException -Invalid `timestamp` literal: '1'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type TIMESTAMP: '1'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select str - interval '4 22:12' day to minute from interval_view ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -1578,7 +1578,7 @@ select str + interval '4 22:12' day to minute from interval_view struct<> -- !query output java.time.DateTimeException -Invalid `timestamp` literal: '1'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type TIMESTAMP: '1'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select str + interval '4 22:12' day to minute from interval_view ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/sql/core/src/test/resources/sql-tests/results/ansi/string-functions.sql.out b/sql/core/src/test/resources/sql-tests/results/ansi/string-functions.sql.out index 43699b377dd27..699752609050f 100644 --- a/sql/core/src/test/resources/sql-tests/results/ansi/string-functions.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/ansi/string-functions.sql.out @@ -82,7 +82,7 @@ select left("abcd", -2), left("abcd", 0), left("abcd", 'a') struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `int` literal: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type INT: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 42) == ...t("abcd", -2), left("abcd", 0), left("abcd", 'a') ^^^^^^^^^^^^^^^^^ @@ -110,7 +110,7 @@ select right("abcd", -2), right("abcd", 0), right("abcd", 'a') struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `int` literal: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type INT: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 44) == ...("abcd", -2), right("abcd", 0), right("abcd", 'a') ^^^^^^^^^^^^^^^^^^ @@ -419,7 +419,7 @@ SELECT lpad('hi', 'invalid_length') struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `int` literal: 'invalid_length'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type INT: 'invalid_length'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT lpad('hi', 'invalid_length') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -431,7 +431,7 @@ SELECT rpad('hi', 'invalid_length') struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `int` literal: 'invalid_length'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type INT: 'invalid_length'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT rpad('hi', 'invalid_length') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/sql/core/src/test/resources/sql-tests/results/postgreSQL/float4.sql.out b/sql/core/src/test/resources/sql-tests/results/postgreSQL/float4.sql.out index 83ca8d22d98c7..1924aa00b39ed 100644 --- a/sql/core/src/test/resources/sql-tests/results/postgreSQL/float4.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/postgreSQL/float4.sql.out @@ -96,7 +96,7 @@ SELECT float('N A N') struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `float` literal: 'N A N'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type FLOAT: 'N A N'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT float('N A N') ^^^^^^^^^^^^^^ @@ -108,7 +108,7 @@ SELECT float('NaN x') struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `float` literal: 'NaN x'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type FLOAT: 'NaN x'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT float('NaN x') ^^^^^^^^^^^^^^ @@ -120,7 +120,7 @@ SELECT float(' INFINITY x') struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `float` literal: ' INFINITY x'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type FLOAT: ' INFINITY x'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT float(' INFINITY x') ^^^^^^^^^^^^^^^^^^^^^^^ @@ -156,7 +156,7 @@ SELECT float(decimal('nan')) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `decimal` literal: 'nan'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type decimal: 'nan'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 13) == SELECT float(decimal('nan')) ^^^^^^^^^^^^^^ diff --git a/sql/core/src/test/resources/sql-tests/results/postgreSQL/float8.sql.out b/sql/core/src/test/resources/sql-tests/results/postgreSQL/float8.sql.out index 4b0539a6b1e18..b78914300a77a 100644 --- a/sql/core/src/test/resources/sql-tests/results/postgreSQL/float8.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/postgreSQL/float8.sql.out @@ -128,7 +128,7 @@ SELECT double('N A N') struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `double` literal: 'N A N'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type DOUBLE: 'N A N'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT double('N A N') ^^^^^^^^^^^^^^^ @@ -140,7 +140,7 @@ SELECT double('NaN x') struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `double` literal: 'NaN x'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type DOUBLE: 'NaN x'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT double('NaN x') ^^^^^^^^^^^^^^^ @@ -152,7 +152,7 @@ SELECT double(' INFINITY x') struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `double` literal: ' INFINITY x'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type DOUBLE: ' INFINITY x'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT double(' INFINITY x') ^^^^^^^^^^^^^^^^^^^^^^^^ @@ -188,7 +188,7 @@ SELECT double(decimal('nan')) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `decimal` literal: 'nan'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type decimal: 'nan'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 14) == SELECT double(decimal('nan')) ^^^^^^^^^^^^^^ diff --git a/sql/core/src/test/resources/sql-tests/results/postgreSQL/text.sql.out b/sql/core/src/test/resources/sql-tests/results/postgreSQL/text.sql.out index f5aa543480fd7..ebe9aba0abe7d 100755 --- a/sql/core/src/test/resources/sql-tests/results/postgreSQL/text.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/postgreSQL/text.sql.out @@ -65,7 +65,7 @@ select string('four: ') || 2+2 struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `bigint` literal: 'four: 2'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type BIGINT: 'four: 2'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select string('four: ') || 2+2 ^^^^^^^^^^^^^^^^^^^^^^^ @@ -77,7 +77,7 @@ select 'four: ' || 2+2 struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `bigint` literal: 'four: 2'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type BIGINT: 'four: 2'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select 'four: ' || 2+2 ^^^^^^^^^^^^^^^ diff --git a/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part2.sql.out b/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part2.sql.out index 8ec356076a723..f818ff86f3f79 100644 --- a/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part2.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part2.sql.out @@ -462,7 +462,7 @@ window w as (order by f_numeric range between struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid `int` literal: 'NaN'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type INT: 'NaN'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 3, position 12) == window w as (order by f_numeric range between ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part3.sql.out b/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part3.sql.out index 016e248bbb5fc..19bd061255985 100644 --- a/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part3.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part3.sql.out @@ -72,7 +72,7 @@ insert into datetimes values struct<> -- !query output org.apache.spark.sql.AnalysisException -failed to evaluate expression CAST('11:00 BST' AS TIMESTAMP): Invalid `timestamp` literal: '11:00 BST'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +failed to evaluate expression CAST('11:00 BST' AS TIMESTAMP): Invalid input value for type TIMESTAMP: '11:00 BST'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 2, position 23) == (1, timestamp '11:00', cast ('11:00 BST' as timestamp), cast ('1 year' as timestamp), ... ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part4.sql.out b/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part4.sql.out index d766d4858aebd..f7c3becd6a4b2 100644 --- a/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part4.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part4.sql.out @@ -501,7 +501,7 @@ FROM (VALUES(1,1),(2,2),(3,(cast('nan' as int))),(4,3),(5,4)) t(a,b) struct<> -- !query output org.apache.spark.sql.AnalysisException -failed to evaluate expression CAST('nan' AS INT): Invalid `int` literal: 'nan'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +failed to evaluate expression CAST('nan' AS INT): Invalid input value for type INT: 'nan'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 3, position 28) == FROM (VALUES(1,1),(2,2),(3,(cast('nan' as int))),(4,3),(5,4)) t(a,b) ^^^^^^^^^^^^^^^^^^ diff --git a/sql/core/src/test/resources/sql-tests/results/timestampNTZ/timestamp-ansi.sql.out b/sql/core/src/test/resources/sql-tests/results/timestampNTZ/timestamp-ansi.sql.out index a85604ddc62b8..0efaa9cd7c88a 100644 --- a/sql/core/src/test/resources/sql-tests/results/timestampNTZ/timestamp-ansi.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/timestampNTZ/timestamp-ansi.sql.out @@ -332,7 +332,7 @@ select to_timestamp(1) struct<> -- !query output java.time.DateTimeException -Invalid `timestamp_ntz` literal: '1'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input value for type TIMESTAMP_NTZ: '1'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query diff --git a/sql/core/src/test/scala/org/apache/spark/sql/SQLInsertTestSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/SQLInsertTestSuite.scala index 24c7046ae594f..5d8d31dcb8303 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/SQLInsertTestSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/SQLInsertTestSuite.scala @@ -314,7 +314,7 @@ trait SQLInsertTestSuite extends QueryTest with SQLTestUtils { val errorMsg = intercept[NumberFormatException] { sql("insert into t partition(a='ansi') values('ansi')") }.getMessage - assert(errorMsg.contains("Invalid `int` literal: 'ansi'")) + assert(errorMsg.contains("Invalid input value for type INT: 'ansi'")) } else { sql("insert into t partition(a='ansi') values('ansi')") checkAnswer(sql("select * from t"), Row("ansi", null) :: Nil) From d28fb91a89af685794b5a10c7c1e8efe3a87a9e0 Mon Sep 17 00:00:00 2001 From: Xinyi Yu Date: Mon, 18 Apr 2022 18:05:52 -0700 Subject: [PATCH 5/9] update the error-class position --- core/src/main/resources/error/error-classes.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/src/main/resources/error/error-classes.json b/core/src/main/resources/error/error-classes.json index cac114be07e86..5eab256706643 100644 --- a/core/src/main/resources/error/error-classes.json +++ b/core/src/main/resources/error/error-classes.json @@ -101,6 +101,10 @@ "message" : [ "Field name %s is invalid: %s is not a struct." ], "sqlState" : "42000" }, + "INVALID_FORMAT_FOR_CAST" : { + "message" : [ "Invalid input value for type %s: %s. To return NULL instead, use 'try_cast'. If necessary set %s to false to bypass this error.%s" ], + "sqlState" : "42000" + }, "INVALID_FRACTION_OF_SECOND" : { "message" : [ "The fraction of sec must be zero. Valid range is [0, 60]. If necessary set %s to false to bypass this error. " ], "sqlState" : "22023" @@ -108,10 +112,6 @@ "INVALID_JSON_SCHEMA_MAPTYPE" : { "message" : [ "Input schema %s can only contain StringType as a key type for a MapType." ] }, - "INVALID_FORMAT_FOR_CAST" : { - "message" : [ "Invalid input value for type %s: %s. To return NULL instead, use 'try_cast'. If necessary set %s to false to bypass this error.%s" ], - "sqlState" : "42000" - }, "INVALID_PANDAS_UDF_PLACEMENT" : { "message" : [ "The group aggregate pandas UDF %s cannot be invoked together with as other, non-pandas aggregate functions." ] }, From 45e3b81a15d515a9bf5ee555e5f9984d40ddef69 Mon Sep 17 00:00:00 2001 From: Xinyi Yu Date: Mon, 18 Apr 2022 22:34:56 -0700 Subject: [PATCH 6/9] address comments --- .../main/resources/error/error-classes.json | 8 +-- .../sql/errors/QueryExecutionErrors.scala | 4 +- .../expressions/AnsiCastSuiteBase.scala | 38 ++++++------- .../catalyst/util/DateFormatterSuite.scala | 2 +- .../util/TimestampFormatterSuite.scala | 2 +- .../apache/spark/sql/types/DecimalSuite.scala | 2 +- .../sql-tests/results/ansi/cast.sql.out | 54 +++++++++---------- .../sql-tests/results/ansi/date.sql.out | 6 +-- .../ansi/datetime-parsing-invalid.sql.out | 4 +- .../sql-tests/results/ansi/interval.sql.out | 20 +++---- .../results/ansi/string-functions.sql.out | 8 +-- .../results/postgreSQL/float4.sql.out | 8 +-- .../results/postgreSQL/float8.sql.out | 8 +-- .../sql-tests/results/postgreSQL/text.sql.out | 4 +- .../results/postgreSQL/window_part2.sql.out | 2 +- .../results/postgreSQL/window_part3.sql.out | 2 +- .../results/postgreSQL/window_part4.sql.out | 2 +- .../timestampNTZ/timestamp-ansi.sql.out | 2 +- .../apache/spark/sql/SQLInsertTestSuite.scala | 2 +- 19 files changed, 89 insertions(+), 89 deletions(-) diff --git a/core/src/main/resources/error/error-classes.json b/core/src/main/resources/error/error-classes.json index 5eab256706643..23c1cee1c7214 100644 --- a/core/src/main/resources/error/error-classes.json +++ b/core/src/main/resources/error/error-classes.json @@ -101,10 +101,6 @@ "message" : [ "Field name %s is invalid: %s is not a struct." ], "sqlState" : "42000" }, - "INVALID_FORMAT_FOR_CAST" : { - "message" : [ "Invalid input value for type %s: %s. To return NULL instead, use 'try_cast'. If necessary set %s to false to bypass this error.%s" ], - "sqlState" : "42000" - }, "INVALID_FRACTION_OF_SECOND" : { "message" : [ "The fraction of sec must be zero. Valid range is [0, 60]. If necessary set %s to false to bypass this error. " ], "sqlState" : "22023" @@ -123,6 +119,10 @@ "message" : [ "Invalid SQL syntax: %s" ], "sqlState" : "42000" }, + "INVALID_SYNTAX_FOR_CAST" : { + "message" : [ "Invalid input syntax for type %s: %s. To return NULL instead, use 'try_cast'. If necessary set %s to false to bypass this error.%s" ], + "sqlState" : "42000" + }, "MAP_KEY_DOES_NOT_EXIST" : { "message" : [ "Key %s does not exist. If necessary set %s to false to bypass this error.%s" ] }, diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala index 524109664e115..5151e79ad4f67 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala @@ -115,7 +115,7 @@ object QueryExecutionErrors extends QueryErrorsBase { to: AbstractDataType, s: UTF8String, errorContext: String): NumberFormatException = { - new SparkNumberFormatException(errorClass = "INVALID_FORMAT_FOR_CAST", + new SparkNumberFormatException(errorClass = "INVALID_SYNTAX_FOR_CAST", messageParameters = Array(toSQLType(to), toSQLValue(s, StringType), SQLConf.ANSI_ENABLED.key, errorContext)) } @@ -1013,7 +1013,7 @@ object QueryExecutionErrors extends QueryErrorsBase { } def cannotCastToDateTimeError(value: Any, to: DataType, errorContext: String): Throwable = { - new DateTimeException(s"Invalid input value for type ${toSQLType(to)}: " + + new DateTimeException(s"Invalid input syntax for type ${toSQLType(to)}: " + s"${if (value.isInstanceOf[UTF8String]) { toSQLValue(value, StringType) } else { diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/AnsiCastSuiteBase.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/AnsiCastSuiteBase.scala index 4871addd43fe8..48a4c51a559ab 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/AnsiCastSuiteBase.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/AnsiCastSuiteBase.scala @@ -176,39 +176,39 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase { // cast to IntegerType Seq(IntegerType, ShortType, ByteType, LongType).foreach { dataType => checkExceptionInExpression[NumberFormatException](cast("string", dataType), - s"Invalid input value for type ${dataType.sql}: 'string'") + s"Invalid input syntax for type ${dataType.sql}: 'string'") checkExceptionInExpression[NumberFormatException](cast("123-string", dataType), - s"Invalid input value for type ${dataType.sql}: '123-string'") + s"Invalid input syntax for type ${dataType.sql}: '123-string'") checkExceptionInExpression[NumberFormatException](cast("2020-07-19", dataType), - s"Invalid input value for type ${dataType.sql}: '2020-07-19'") + s"Invalid input syntax for type ${dataType.sql}: '2020-07-19'") checkExceptionInExpression[NumberFormatException](cast("1.23", dataType), - s"Invalid input value for type ${dataType.sql}: '1.23'") + s"Invalid input syntax for type ${dataType.sql}: '1.23'") } Seq(DoubleType, FloatType).foreach { dataType => checkExceptionInExpression[NumberFormatException](cast("string", dataType), - s"Invalid input value for type ${dataType.sql}: 'string'") + s"Invalid input syntax for type ${dataType.sql}: 'string'") checkExceptionInExpression[NumberFormatException](cast("123.000.00", dataType), - s"Invalid input value for type ${dataType.sql}: '123.000.00'") + s"Invalid input syntax for type ${dataType.sql}: '123.000.00'") checkExceptionInExpression[NumberFormatException](cast("abc.com", dataType), - s"Invalid input value for type ${dataType.sql}: 'abc.com'") + s"Invalid input syntax for type ${dataType.sql}: 'abc.com'") } checkExceptionInExpression[NumberFormatException]( cast("string", DecimalType.USER_DEFAULT), - s"Invalid input value for type ${DecimalType.simpleString}: 'string'") + s"Invalid input syntax for type ${DecimalType.simpleString}: 'string'") checkExceptionInExpression[NumberFormatException]( cast("123.000.00", DecimalType.USER_DEFAULT), - s"Invalid input value for type ${DecimalType.simpleString}: '123.000.00'") + s"Invalid input syntax for type ${DecimalType.simpleString}: '123.000.00'") checkExceptionInExpression[NumberFormatException]( cast("abc.com", DecimalType.USER_DEFAULT), - s"Invalid input value for type ${DecimalType.simpleString}: 'abc.com'") + s"Invalid input syntax for type ${DecimalType.simpleString}: 'abc.com'") } protected def checkCastToNumericError(l: Literal, to: DataType, expectedDataTypeInErrorMsg: DataType, tryCastResult: Any): Unit = { checkExceptionInExpression[NumberFormatException]( - cast(l, to), s"Invalid input value for type ${expectedDataTypeInErrorMsg.sql}: 'true'") + cast(l, to), s"Invalid input syntax for type ${expectedDataTypeInErrorMsg.sql}: 'true'") } test("cast from invalid string array to numeric array should throw NumberFormatException") { @@ -255,7 +255,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase { checkExceptionInExpression[NumberFormatException]( cast("abcd", DecimalType(38, 1)), - s"Invalid input value for type ${DecimalType.simpleString}: 'abcd'") + s"Invalid input syntax for type ${DecimalType.simpleString}: 'abcd'") } protected def checkCastToBooleanError(l: Literal, to: DataType, tryCastResult: Any): Unit = { @@ -270,7 +270,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase { protected def checkCastToTimestampError(l: Literal, to: DataType): Unit = { checkExceptionInExpression[DateTimeException]( - cast(l, to), s"Invalid input value for type TIMESTAMP: ${toSQLValue(l)}") + cast(l, to), s"Invalid input syntax for type TIMESTAMP: ${toSQLValue(l)}") } test("cast from timestamp II") { @@ -381,7 +381,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase { assert(ret.resolved == !isTryCast) if (!isTryCast) { checkExceptionInExpression[NumberFormatException]( - ret, s"Invalid input value for type ${IntegerType.sql}") + ret, s"Invalid input syntax for type ${IntegerType.sql}") } } @@ -399,7 +399,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase { assert(ret.resolved == !isTryCast) if (!isTryCast) { checkExceptionInExpression[NumberFormatException]( - ret, s"Invalid input value for type ${IntegerType.sql}") + ret, s"Invalid input syntax for type ${IntegerType.sql}") } } } @@ -524,7 +524,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase { assert(ret.resolved === !isTryCast) if (!isTryCast) { checkExceptionInExpression[NumberFormatException]( - ret, s"Invalid input value for type ${IntegerType.sql}") + ret, s"Invalid input syntax for type ${IntegerType.sql}") } } @@ -533,7 +533,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase { def checkCastWithParseError(str: String): Unit = { checkExceptionInExpression[DateTimeException]( cast(Literal(str), TimestampType, Option(zid.getId)), - s"Invalid input value for type TIMESTAMP: '$str'") + s"Invalid input syntax for type TIMESTAMP: '$str'") } checkCastWithParseError("123") @@ -554,7 +554,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase { def checkCastWithParseError(str: String): Unit = { checkExceptionInExpression[DateTimeException]( cast(Literal(str), DateType, Option(zid.getId)), - s"Invalid input value for type DATE: '$str'") + s"Invalid input syntax for type DATE: '$str'") } checkCastWithParseError("2015-13-18") @@ -582,7 +582,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase { "2021-06-17 00:00:00ABC").foreach { invalidInput => checkExceptionInExpression[DateTimeException]( cast(invalidInput, TimestampNTZType), - s"Invalid input value for type TIMESTAMP_NTZ: '$invalidInput'") + s"Invalid input syntax for type TIMESTAMP_NTZ: '$invalidInput'") } } } diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/DateFormatterSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/DateFormatterSuite.scala index 4804ea267a3e9..71351f6263fbe 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/DateFormatterSuite.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/DateFormatterSuite.scala @@ -208,6 +208,6 @@ class DateFormatterSuite extends DatetimeFormatterSuite { val errMsg = intercept[DateTimeException] { formatter.parse("x123") }.getMessage - assert(errMsg.contains("Invalid input value for type DATE: 'x123'")) + assert(errMsg.contains("Invalid input syntax for type DATE: 'x123'")) } } diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/TimestampFormatterSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/TimestampFormatterSuite.scala index 9bec89d520bc5..204fe93e2d1b6 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/TimestampFormatterSuite.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/TimestampFormatterSuite.scala @@ -453,7 +453,7 @@ class TimestampFormatterSuite extends DatetimeFormatterSuite { val errMsg = intercept[DateTimeException] { formatter.parse("x123") }.getMessage - assert(errMsg.contains("Invalid input value for type TIMESTAMP: 'x123'")) + assert(errMsg.contains("Invalid input syntax for type TIMESTAMP: 'x123'")) } } } diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/types/DecimalSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/types/DecimalSuite.scala index 534137134e512..9a01002df33fa 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/types/DecimalSuite.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/types/DecimalSuite.scala @@ -284,7 +284,7 @@ class DecimalSuite extends SparkFunSuite with PrivateMethodTester with SQLHelper assert(Decimal.fromString(UTF8String.fromString("str")) === null) val e = intercept[NumberFormatException](Decimal.fromStringANSI(UTF8String.fromString("str"))) - assert(e.getMessage.contains(s"Invalid input value for type ${DecimalType.simpleString}: " + + assert(e.getMessage.contains(s"Invalid input syntax for type ${DecimalType.simpleString}: " + "'str'")) } diff --git a/sql/core/src/test/resources/sql-tests/results/ansi/cast.sql.out b/sql/core/src/test/resources/sql-tests/results/ansi/cast.sql.out index 1c81eafd3174e..d8f510d2a215e 100644 --- a/sql/core/src/test/resources/sql-tests/results/ansi/cast.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/ansi/cast.sql.out @@ -8,7 +8,7 @@ SELECT CAST('1.23' AS int) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type INT: '1.23'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type INT: '1.23'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT CAST('1.23' AS int) ^^^^^^^^^^^^^^^^^^^ @@ -20,7 +20,7 @@ SELECT CAST('1.23' AS long) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type BIGINT: '1.23'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type BIGINT: '1.23'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT CAST('1.23' AS long) ^^^^^^^^^^^^^^^^^^^^ @@ -32,7 +32,7 @@ SELECT CAST('-4.56' AS int) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type INT: '-4.56'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type INT: '-4.56'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT CAST('-4.56' AS int) ^^^^^^^^^^^^^^^^^^^^ @@ -44,7 +44,7 @@ SELECT CAST('-4.56' AS long) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type BIGINT: '-4.56'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type BIGINT: '-4.56'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT CAST('-4.56' AS long) ^^^^^^^^^^^^^^^^^^^^^ @@ -56,7 +56,7 @@ SELECT CAST('abc' AS int) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type INT: 'abc'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type INT: 'abc'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT CAST('abc' AS int) ^^^^^^^^^^^^^^^^^^ @@ -68,7 +68,7 @@ SELECT CAST('abc' AS long) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type BIGINT: 'abc'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type BIGINT: 'abc'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT CAST('abc' AS long) ^^^^^^^^^^^^^^^^^^^ @@ -80,7 +80,7 @@ SELECT CAST('1234567890123' AS int) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type INT: '1234567890123'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type INT: '1234567890123'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT CAST('1234567890123' AS int) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -92,7 +92,7 @@ SELECT CAST('12345678901234567890123' AS long) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type BIGINT: '12345678901234567890123'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type BIGINT: '12345678901234567890123'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT CAST('12345678901234567890123' AS long) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -104,7 +104,7 @@ SELECT CAST('' AS int) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type INT: ''. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type INT: ''. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT CAST('' AS int) ^^^^^^^^^^^^^^^ @@ -116,7 +116,7 @@ SELECT CAST('' AS long) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type BIGINT: ''. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type BIGINT: ''. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT CAST('' AS long) ^^^^^^^^^^^^^^^^ @@ -144,7 +144,7 @@ SELECT CAST('123.a' AS int) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type INT: '123.a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type INT: '123.a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT CAST('123.a' AS int) ^^^^^^^^^^^^^^^^^^^^ @@ -156,7 +156,7 @@ SELECT CAST('123.a' AS long) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type BIGINT: '123.a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type BIGINT: '123.a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT CAST('123.a' AS long) ^^^^^^^^^^^^^^^^^^^^^ @@ -176,7 +176,7 @@ SELECT CAST('-2147483649' AS int) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type INT: '-2147483649'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type INT: '-2147483649'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT CAST('-2147483649' AS int) ^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -196,7 +196,7 @@ SELECT CAST('2147483648' AS int) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type INT: '2147483648'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type INT: '2147483648'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT CAST('2147483648' AS int) ^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -216,7 +216,7 @@ SELECT CAST('-9223372036854775809' AS long) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type BIGINT: '-9223372036854775809'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type BIGINT: '-9223372036854775809'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT CAST('-9223372036854775809' AS long) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -236,7 +236,7 @@ SELECT CAST('9223372036854775808' AS long) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type BIGINT: '9223372036854775808'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type BIGINT: '9223372036854775808'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT CAST('9223372036854775808' AS long) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -495,7 +495,7 @@ select cast('1中文' as tinyint) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type TINYINT: '1中文'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type TINYINT: '1中文'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select cast('1中文' as tinyint) ^^^^^^^^^^^^^^^^^^^^^^ @@ -507,7 +507,7 @@ select cast('1中文' as smallint) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type SMALLINT: '1中文'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type SMALLINT: '1中文'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select cast('1中文' as smallint) ^^^^^^^^^^^^^^^^^^^^^^^ @@ -519,7 +519,7 @@ select cast('1中文' as INT) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type INT: '1中文'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type INT: '1中文'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select cast('1中文' as INT) ^^^^^^^^^^^^^^^^^^ @@ -531,7 +531,7 @@ select cast('中文1' as bigint) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type BIGINT: '中文1'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type BIGINT: '中文1'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select cast('中文1' as bigint) ^^^^^^^^^^^^^^^^^^^^^ @@ -543,7 +543,7 @@ select cast('1中文' as bigint) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type BIGINT: '1中文'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type BIGINT: '1中文'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select cast('1中文' as bigint) ^^^^^^^^^^^^^^^^^^^^^ @@ -606,7 +606,7 @@ select cast('xyz' as decimal(4, 2)) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type decimal: 'xyz'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type decimal: 'xyz'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select cast('xyz' as decimal(4, 2)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -626,7 +626,7 @@ select cast('a' as date) struct<> -- !query output java.time.DateTimeException -Invalid input value for type DATE: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type DATE: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select cast('a' as date) ^^^^^^^^^^^^^^^^^ @@ -646,7 +646,7 @@ select cast('a' as timestamp) struct<> -- !query output java.time.DateTimeException -Invalid input value for type TIMESTAMP: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type TIMESTAMP: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select cast('a' as timestamp) ^^^^^^^^^^^^^^^^^^^^^^ @@ -666,7 +666,7 @@ select cast('a' as timestamp_ntz) struct<> -- !query output java.time.DateTimeException -Invalid input value for type TIMESTAMP_NTZ: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type TIMESTAMP_NTZ: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select cast('a' as timestamp_ntz) ^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -678,7 +678,7 @@ select cast(cast('inf' as double) as timestamp) struct<> -- !query output java.time.DateTimeException -Invalid input value for type TIMESTAMP: Infinity. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type TIMESTAMP: Infinity. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select cast(cast('inf' as double) as timestamp) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -690,7 +690,7 @@ select cast(cast('inf' as float) as timestamp) struct<> -- !query output java.time.DateTimeException -Invalid input value for type TIMESTAMP: Infinity. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type TIMESTAMP: Infinity. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select cast(cast('inf' as float) as timestamp) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/sql/core/src/test/resources/sql-tests/results/ansi/date.sql.out b/sql/core/src/test/resources/sql-tests/results/ansi/date.sql.out index 80f7c8dc8c92f..fa65b4dd07110 100644 --- a/sql/core/src/test/resources/sql-tests/results/ansi/date.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/ansi/date.sql.out @@ -232,7 +232,7 @@ select next_day("xx", "Mon") struct<> -- !query output java.time.DateTimeException -Invalid input value for type DATE: 'xx'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type DATE: 'xx'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select next_day("xx", "Mon") ^^^^^^^^^^^^^^^^^^^^^ @@ -327,7 +327,7 @@ select date_add('2011-11-11', '1.2') struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type INT: '1.2'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type INT: '1.2'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select date_add('2011-11-11', '1.2') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -438,7 +438,7 @@ select date_sub(date'2011-11-11', '1.2') struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type INT: '1.2'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type INT: '1.2'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select date_sub(date'2011-11-11', '1.2') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/sql/core/src/test/resources/sql-tests/results/ansi/datetime-parsing-invalid.sql.out b/sql/core/src/test/resources/sql-tests/results/ansi/datetime-parsing-invalid.sql.out index 8909a6032b776..e30b592020d97 100644 --- a/sql/core/src/test/resources/sql-tests/results/ansi/datetime-parsing-invalid.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/ansi/datetime-parsing-invalid.sql.out @@ -242,7 +242,7 @@ select cast("Unparseable" as timestamp) struct<> -- !query output java.time.DateTimeException -Invalid input value for type TIMESTAMP: 'Unparseable'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type TIMESTAMP: 'Unparseable'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select cast("Unparseable" as timestamp) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -254,7 +254,7 @@ select cast("Unparseable" as date) struct<> -- !query output java.time.DateTimeException -Invalid input value for type DATE: 'Unparseable'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type DATE: 'Unparseable'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select cast("Unparseable" as date) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/sql/core/src/test/resources/sql-tests/results/ansi/interval.sql.out b/sql/core/src/test/resources/sql-tests/results/ansi/interval.sql.out index 7904a79b3493f..d7975dfb58a5f 100644 --- a/sql/core/src/test/resources/sql-tests/results/ansi/interval.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/ansi/interval.sql.out @@ -122,7 +122,7 @@ select interval 2 second * 'a' struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type DOUBLE: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type DOUBLE: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select interval 2 second * 'a' ^^^^^^^^^^^^^^^^^^^^^^^ @@ -134,7 +134,7 @@ select interval 2 second / 'a' struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type DOUBLE: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type DOUBLE: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select interval 2 second / 'a' ^^^^^^^^^^^^^^^^^^^^^^^ @@ -146,7 +146,7 @@ select interval 2 year * 'a' struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type DOUBLE: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type DOUBLE: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select interval 2 year * 'a' ^^^^^^^^^^^^^^^^^^^^^ @@ -158,7 +158,7 @@ select interval 2 year / 'a' struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type DOUBLE: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type DOUBLE: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select interval 2 year / 'a' ^^^^^^^^^^^^^^^^^^^^^ @@ -186,7 +186,7 @@ select 'a' * interval 2 second struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type DOUBLE: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type DOUBLE: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select 'a' * interval 2 second ^^^^^^^^^^^^^^^^^^^^^^^ @@ -198,7 +198,7 @@ select 'a' * interval 2 year struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type DOUBLE: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type DOUBLE: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select 'a' * interval 2 year ^^^^^^^^^^^^^^^^^^^^^ @@ -1516,7 +1516,7 @@ select '4 11:11' - interval '4 22:12' day to minute struct<> -- !query output java.time.DateTimeException -Invalid input value for type TIMESTAMP: '4 11:11'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type TIMESTAMP: '4 11:11'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select '4 11:11' - interval '4 22:12' day to minute ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -1528,7 +1528,7 @@ select '4 12:12:12' + interval '4 22:12' day to minute struct<> -- !query output java.time.DateTimeException -Invalid input value for type TIMESTAMP: '4 12:12:12'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type TIMESTAMP: '4 12:12:12'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select '4 12:12:12' + interval '4 22:12' day to minute ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -1566,7 +1566,7 @@ select str - interval '4 22:12' day to minute from interval_view struct<> -- !query output java.time.DateTimeException -Invalid input value for type TIMESTAMP: '1'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type TIMESTAMP: '1'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select str - interval '4 22:12' day to minute from interval_view ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -1578,7 +1578,7 @@ select str + interval '4 22:12' day to minute from interval_view struct<> -- !query output java.time.DateTimeException -Invalid input value for type TIMESTAMP: '1'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type TIMESTAMP: '1'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select str + interval '4 22:12' day to minute from interval_view ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/sql/core/src/test/resources/sql-tests/results/ansi/string-functions.sql.out b/sql/core/src/test/resources/sql-tests/results/ansi/string-functions.sql.out index 699752609050f..083471b15d4d4 100644 --- a/sql/core/src/test/resources/sql-tests/results/ansi/string-functions.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/ansi/string-functions.sql.out @@ -82,7 +82,7 @@ select left("abcd", -2), left("abcd", 0), left("abcd", 'a') struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type INT: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type INT: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 42) == ...t("abcd", -2), left("abcd", 0), left("abcd", 'a') ^^^^^^^^^^^^^^^^^ @@ -110,7 +110,7 @@ select right("abcd", -2), right("abcd", 0), right("abcd", 'a') struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type INT: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type INT: 'a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 44) == ...("abcd", -2), right("abcd", 0), right("abcd", 'a') ^^^^^^^^^^^^^^^^^^ @@ -419,7 +419,7 @@ SELECT lpad('hi', 'invalid_length') struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type INT: 'invalid_length'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type INT: 'invalid_length'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT lpad('hi', 'invalid_length') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -431,7 +431,7 @@ SELECT rpad('hi', 'invalid_length') struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type INT: 'invalid_length'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type INT: 'invalid_length'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT rpad('hi', 'invalid_length') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/sql/core/src/test/resources/sql-tests/results/postgreSQL/float4.sql.out b/sql/core/src/test/resources/sql-tests/results/postgreSQL/float4.sql.out index 1924aa00b39ed..7e854a624f688 100644 --- a/sql/core/src/test/resources/sql-tests/results/postgreSQL/float4.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/postgreSQL/float4.sql.out @@ -96,7 +96,7 @@ SELECT float('N A N') struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type FLOAT: 'N A N'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type FLOAT: 'N A N'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT float('N A N') ^^^^^^^^^^^^^^ @@ -108,7 +108,7 @@ SELECT float('NaN x') struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type FLOAT: 'NaN x'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type FLOAT: 'NaN x'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT float('NaN x') ^^^^^^^^^^^^^^ @@ -120,7 +120,7 @@ SELECT float(' INFINITY x') struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type FLOAT: ' INFINITY x'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type FLOAT: ' INFINITY x'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT float(' INFINITY x') ^^^^^^^^^^^^^^^^^^^^^^^ @@ -156,7 +156,7 @@ SELECT float(decimal('nan')) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type decimal: 'nan'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type decimal: 'nan'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 13) == SELECT float(decimal('nan')) ^^^^^^^^^^^^^^ diff --git a/sql/core/src/test/resources/sql-tests/results/postgreSQL/float8.sql.out b/sql/core/src/test/resources/sql-tests/results/postgreSQL/float8.sql.out index b78914300a77a..e5effae3f1329 100644 --- a/sql/core/src/test/resources/sql-tests/results/postgreSQL/float8.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/postgreSQL/float8.sql.out @@ -128,7 +128,7 @@ SELECT double('N A N') struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type DOUBLE: 'N A N'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type DOUBLE: 'N A N'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT double('N A N') ^^^^^^^^^^^^^^^ @@ -140,7 +140,7 @@ SELECT double('NaN x') struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type DOUBLE: 'NaN x'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type DOUBLE: 'NaN x'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT double('NaN x') ^^^^^^^^^^^^^^^ @@ -152,7 +152,7 @@ SELECT double(' INFINITY x') struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type DOUBLE: ' INFINITY x'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type DOUBLE: ' INFINITY x'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == SELECT double(' INFINITY x') ^^^^^^^^^^^^^^^^^^^^^^^^ @@ -188,7 +188,7 @@ SELECT double(decimal('nan')) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type decimal: 'nan'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type decimal: 'nan'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 14) == SELECT double(decimal('nan')) ^^^^^^^^^^^^^^ diff --git a/sql/core/src/test/resources/sql-tests/results/postgreSQL/text.sql.out b/sql/core/src/test/resources/sql-tests/results/postgreSQL/text.sql.out index ebe9aba0abe7d..cff6bf280401f 100755 --- a/sql/core/src/test/resources/sql-tests/results/postgreSQL/text.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/postgreSQL/text.sql.out @@ -65,7 +65,7 @@ select string('four: ') || 2+2 struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type BIGINT: 'four: 2'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type BIGINT: 'four: 2'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select string('four: ') || 2+2 ^^^^^^^^^^^^^^^^^^^^^^^ @@ -77,7 +77,7 @@ select 'four: ' || 2+2 struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type BIGINT: 'four: 2'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type BIGINT: 'four: 2'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select 'four: ' || 2+2 ^^^^^^^^^^^^^^^ diff --git a/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part2.sql.out b/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part2.sql.out index f818ff86f3f79..c48d92a99007f 100644 --- a/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part2.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part2.sql.out @@ -462,7 +462,7 @@ window w as (order by f_numeric range between struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input value for type INT: 'NaN'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type INT: 'NaN'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 3, position 12) == window w as (order by f_numeric range between ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part3.sql.out b/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part3.sql.out index 19bd061255985..b5281d4c6051c 100644 --- a/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part3.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part3.sql.out @@ -72,7 +72,7 @@ insert into datetimes values struct<> -- !query output org.apache.spark.sql.AnalysisException -failed to evaluate expression CAST('11:00 BST' AS TIMESTAMP): Invalid input value for type TIMESTAMP: '11:00 BST'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +failed to evaluate expression CAST('11:00 BST' AS TIMESTAMP): Invalid input syntax for type TIMESTAMP: '11:00 BST'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 2, position 23) == (1, timestamp '11:00', cast ('11:00 BST' as timestamp), cast ('1 year' as timestamp), ... ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part4.sql.out b/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part4.sql.out index f7c3becd6a4b2..6beb6fd595817 100644 --- a/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part4.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part4.sql.out @@ -501,7 +501,7 @@ FROM (VALUES(1,1),(2,2),(3,(cast('nan' as int))),(4,3),(5,4)) t(a,b) struct<> -- !query output org.apache.spark.sql.AnalysisException -failed to evaluate expression CAST('nan' AS INT): Invalid input value for type INT: 'nan'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +failed to evaluate expression CAST('nan' AS INT): Invalid input syntax for type INT: 'nan'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 3, position 28) == FROM (VALUES(1,1),(2,2),(3,(cast('nan' as int))),(4,3),(5,4)) t(a,b) ^^^^^^^^^^^^^^^^^^ diff --git a/sql/core/src/test/resources/sql-tests/results/timestampNTZ/timestamp-ansi.sql.out b/sql/core/src/test/resources/sql-tests/results/timestampNTZ/timestamp-ansi.sql.out index 0efaa9cd7c88a..c09a7a1811c71 100644 --- a/sql/core/src/test/resources/sql-tests/results/timestampNTZ/timestamp-ansi.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/timestampNTZ/timestamp-ansi.sql.out @@ -332,7 +332,7 @@ select to_timestamp(1) struct<> -- !query output java.time.DateTimeException -Invalid input value for type TIMESTAMP_NTZ: '1'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type TIMESTAMP_NTZ: '1'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. -- !query diff --git a/sql/core/src/test/scala/org/apache/spark/sql/SQLInsertTestSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/SQLInsertTestSuite.scala index 5d8d31dcb8303..259d00746ac07 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/SQLInsertTestSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/SQLInsertTestSuite.scala @@ -314,7 +314,7 @@ trait SQLInsertTestSuite extends QueryTest with SQLTestUtils { val errorMsg = intercept[NumberFormatException] { sql("insert into t partition(a='ansi') values('ansi')") }.getMessage - assert(errorMsg.contains("Invalid input value for type INT: 'ansi'")) + assert(errorMsg.contains("Invalid input syntax for type INT: 'ansi'")) } else { sql("insert into t partition(a='ansi') values('ansi')") checkAnswer(sql("select * from t"), Row("ansi", null) :: Nil) From 30a3b82d2f9f45c6a04169746cb4f1eb327e3101 Mon Sep 17 00:00:00 2001 From: Xinyi Yu Date: Mon, 18 Apr 2022 23:43:13 -0700 Subject: [PATCH 7/9] address comment --- .../spark/sql/catalyst/expressions/Cast.scala | 5 +++-- .../apache/spark/sql/errors/QueryErrorsBase.scala | 9 +++------ .../spark/sql/errors/QueryExecutionErrors.scala | 2 +- .../scala/org/apache/spark/sql/types/Decimal.scala | 5 +++-- .../catalyst/expressions/AnsiCastSuiteBase.scala | 14 ++------------ .../org/apache/spark/sql/types/DecimalSuite.scala | 4 ++-- .../resources/sql-tests/results/ansi/cast.sql.out | 2 +- .../sql-tests/results/postgreSQL/float4.sql.out | 2 +- .../sql-tests/results/postgreSQL/float8.sql.out | 2 +- 9 files changed, 17 insertions(+), 28 deletions(-) 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 ad23983fc5132..865202caa5fc2 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 @@ -816,7 +816,7 @@ abstract class CastBase extends UnaryExpression with TimeZoneAwareExpression wit }) case StringType if ansiEnabled => buildCast[UTF8String](_, - s => changePrecision(Decimal.fromStringANSI(s, origin.context), target)) + s => changePrecision(Decimal.fromStringANSI(s, target, origin.context), target)) case BooleanType => buildCast[Boolean](_, b => toPrecision(if (b) Decimal.ONE else Decimal.ZERO, target)) case DateType => @@ -1378,9 +1378,10 @@ abstract class CastBase extends UnaryExpression with TimeZoneAwareExpression wit """ case StringType if ansiEnabled => val errorContext = ctx.addReferenceObj("errCtx", origin.context) + val toType = ctx.addReferenceObj("toType", target) (c, evPrim, evNull) => code""" - Decimal $tmp = Decimal.fromStringANSI($c, $errorContext); + Decimal $tmp = Decimal.fromStringANSI($c, $toType, $errorContext); ${changePrecision(tmp, target, evPrim, evNull, canNullSafeCast, ctx)} """ case BooleanType => diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryErrorsBase.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryErrorsBase.scala index 00358dc0c01c4..4708b47cfb890 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryErrorsBase.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryErrorsBase.scala @@ -19,7 +19,7 @@ package org.apache.spark.sql.errors import org.apache.spark.sql.catalyst.expressions.Literal import org.apache.spark.sql.catalyst.util.quoteIdentifier -import org.apache.spark.sql.types.{AbstractDataType, DataType, DoubleType, FloatType} +import org.apache.spark.sql.types.{DataType, DoubleType, FloatType} trait QueryErrorsBase { private def litToErrorValue(l: Literal): String = l match { @@ -59,10 +59,7 @@ trait QueryErrorsBase { toSQLId(parts.split("\\.")) } - def toSQLType(t: AbstractDataType): String = { - t match { - case dt: DataType => dt.sql - case adt => adt.simpleString - } + def toSQLType(t: DataType): String = { + t.sql } } diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala index 5151e79ad4f67..290dce1542eec 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala @@ -112,7 +112,7 @@ object QueryExecutionErrors extends QueryErrorsBase { } def invalidInputSyntaxForNumericError( - to: AbstractDataType, + to: DataType, s: UTF8String, errorContext: String): NumberFormatException = { new SparkNumberFormatException(errorClass = "INVALID_SYNTAX_FOR_CAST", diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/types/Decimal.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/types/Decimal.scala index 863acd9ea13f9..7ea0067e63771 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/types/Decimal.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/types/Decimal.scala @@ -613,7 +613,8 @@ object Decimal { } } - def fromStringANSI(str: UTF8String, errorContext: String = ""): Decimal = { + def fromStringANSI(str: UTF8String, to: DecimalType = DecimalType.USER_DEFAULT, + errorContext: String = ""): Decimal = { try { val bigDecimal = stringToJavaBigDecimal(str) // We fast fail because constructing a very large JavaBigDecimal to Decimal is very slow. @@ -626,7 +627,7 @@ object Decimal { } } catch { case _: NumberFormatException => - throw QueryExecutionErrors.invalidInputSyntaxForNumericError(DecimalType, str, errorContext) + throw QueryExecutionErrors.invalidInputSyntaxForNumericError(to, str, errorContext) } } diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/AnsiCastSuiteBase.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/AnsiCastSuiteBase.scala index 48a4c51a559ab..9be144efd7738 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/AnsiCastSuiteBase.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/AnsiCastSuiteBase.scala @@ -185,7 +185,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase { s"Invalid input syntax for type ${dataType.sql}: '1.23'") } - Seq(DoubleType, FloatType).foreach { dataType => + Seq(DoubleType, FloatType, DecimalType.USER_DEFAULT).foreach { dataType => checkExceptionInExpression[NumberFormatException](cast("string", dataType), s"Invalid input syntax for type ${dataType.sql}: 'string'") checkExceptionInExpression[NumberFormatException](cast("123.000.00", dataType), @@ -193,16 +193,6 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase { checkExceptionInExpression[NumberFormatException](cast("abc.com", dataType), s"Invalid input syntax for type ${dataType.sql}: 'abc.com'") } - - checkExceptionInExpression[NumberFormatException]( - cast("string", DecimalType.USER_DEFAULT), - s"Invalid input syntax for type ${DecimalType.simpleString}: 'string'") - checkExceptionInExpression[NumberFormatException]( - cast("123.000.00", DecimalType.USER_DEFAULT), - s"Invalid input syntax for type ${DecimalType.simpleString}: '123.000.00'") - checkExceptionInExpression[NumberFormatException]( - cast("abc.com", DecimalType.USER_DEFAULT), - s"Invalid input syntax for type ${DecimalType.simpleString}: 'abc.com'") } protected def checkCastToNumericError(l: Literal, to: DataType, @@ -255,7 +245,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase { checkExceptionInExpression[NumberFormatException]( cast("abcd", DecimalType(38, 1)), - s"Invalid input syntax for type ${DecimalType.simpleString}: 'abcd'") + s"Invalid input syntax for type ${DecimalType(38, 1).sql}: 'abcd'") } protected def checkCastToBooleanError(l: Literal, to: DataType, tryCastResult: Any): Unit = { diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/types/DecimalSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/types/DecimalSuite.scala index 9a01002df33fa..77b07ce533ece 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/types/DecimalSuite.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/types/DecimalSuite.scala @@ -284,8 +284,8 @@ class DecimalSuite extends SparkFunSuite with PrivateMethodTester with SQLHelper assert(Decimal.fromString(UTF8String.fromString("str")) === null) val e = intercept[NumberFormatException](Decimal.fromStringANSI(UTF8String.fromString("str"))) - assert(e.getMessage.contains(s"Invalid input syntax for type ${DecimalType.simpleString}: " + - "'str'")) + assert(e.getMessage.contains("Invalid input syntax for type " + + s"${DecimalType.USER_DEFAULT.sql}: 'str'")) } test("SPARK-35841: Casting string to decimal type doesn't work " + diff --git a/sql/core/src/test/resources/sql-tests/results/ansi/cast.sql.out b/sql/core/src/test/resources/sql-tests/results/ansi/cast.sql.out index d8f510d2a215e..9cbe8c7993f82 100644 --- a/sql/core/src/test/resources/sql-tests/results/ansi/cast.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/ansi/cast.sql.out @@ -606,7 +606,7 @@ select cast('xyz' as decimal(4, 2)) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input syntax for type decimal: 'xyz'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type DECIMAL(4,2): 'xyz'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 7) == select cast('xyz' as decimal(4, 2)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/sql/core/src/test/resources/sql-tests/results/postgreSQL/float4.sql.out b/sql/core/src/test/resources/sql-tests/results/postgreSQL/float4.sql.out index 7e854a624f688..b63d2d1307ec3 100644 --- a/sql/core/src/test/resources/sql-tests/results/postgreSQL/float4.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/postgreSQL/float4.sql.out @@ -156,7 +156,7 @@ SELECT float(decimal('nan')) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input syntax for type decimal: 'nan'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type DECIMAL(10,0): 'nan'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 13) == SELECT float(decimal('nan')) ^^^^^^^^^^^^^^ diff --git a/sql/core/src/test/resources/sql-tests/results/postgreSQL/float8.sql.out b/sql/core/src/test/resources/sql-tests/results/postgreSQL/float8.sql.out index e5effae3f1329..b0582c0952387 100644 --- a/sql/core/src/test/resources/sql-tests/results/postgreSQL/float8.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/postgreSQL/float8.sql.out @@ -188,7 +188,7 @@ SELECT double(decimal('nan')) struct<> -- !query output org.apache.spark.SparkNumberFormatException -Invalid input syntax for type decimal: 'nan'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +Invalid input syntax for type DECIMAL(10,0): 'nan'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. == SQL(line 1, position 14) == SELECT double(decimal('nan')) ^^^^^^^^^^^^^^ From 691e1e3719def98992a2b566d9d3eada7a707825 Mon Sep 17 00:00:00 2001 From: Xinyi Yu Date: Mon, 18 Apr 2022 23:55:45 -0700 Subject: [PATCH 8/9] add several more tests --- .../test/resources/sql-tests/inputs/cast.sql | 10 ++- .../sql-tests/results/ansi/cast.sql.out | 74 ++++++++++++++++++- 2 files changed, 81 insertions(+), 3 deletions(-) diff --git a/sql/core/src/test/resources/sql-tests/inputs/cast.sql b/sql/core/src/test/resources/sql-tests/inputs/cast.sql index e391c31690fd7..4610716902e5d 100644 --- a/sql/core/src/test/resources/sql-tests/inputs/cast.sql +++ b/sql/core/src/test/resources/sql-tests/inputs/cast.sql @@ -4,9 +4,11 @@ SELECT CAST('1.23' AS long); SELECT CAST('-4.56' AS int); SELECT CAST('-4.56' AS long); --- cast string which are not numbers to integral should return null +-- cast string which are not numbers to numeric types SELECT CAST('abc' AS int); SELECT CAST('abc' AS long); +SELECT CAST('abc' AS float); +SELECT CAST('abc' AS double); -- cast string representing a very large number to integral should return null SELECT CAST('1234567890123' AS int); @@ -15,14 +17,18 @@ SELECT CAST('12345678901234567890123' AS long); -- cast empty string to integral should return null SELECT CAST('' AS int); SELECT CAST('' AS long); +SELECT CAST('' AS float); +SELECT CAST('' AS double); -- cast null to integral should return null SELECT CAST(NULL AS int); SELECT CAST(NULL AS long); --- cast invalid decimal string to integral should return null +-- cast invalid decimal string to numeric types SELECT CAST('123.a' AS int); SELECT CAST('123.a' AS long); +SELECT CAST('123.a' AS float); +SELECT CAST('123.a' AS double); -- '-2147483648' is the smallest int value SELECT CAST('-2147483648' AS int); diff --git a/sql/core/src/test/resources/sql-tests/results/ansi/cast.sql.out b/sql/core/src/test/resources/sql-tests/results/ansi/cast.sql.out index 9cbe8c7993f82..a2cb4ca11252d 100644 --- a/sql/core/src/test/resources/sql-tests/results/ansi/cast.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/ansi/cast.sql.out @@ -1,5 +1,5 @@ -- Automatically generated by SQLQueryTestSuite --- Number of queries: 66 +-- Number of queries: 72 -- !query @@ -74,6 +74,30 @@ SELECT CAST('abc' AS long) ^^^^^^^^^^^^^^^^^^^ +-- !query +SELECT CAST('abc' AS float) +-- !query schema +struct<> +-- !query output +org.apache.spark.SparkNumberFormatException +Invalid input syntax for type FLOAT: 'abc'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +== SQL(line 1, position 7) == +SELECT CAST('abc' AS float) + ^^^^^^^^^^^^^^^^^^^^ + + +-- !query +SELECT CAST('abc' AS double) +-- !query schema +struct<> +-- !query output +org.apache.spark.SparkNumberFormatException +Invalid input syntax for type DOUBLE: 'abc'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +== SQL(line 1, position 7) == +SELECT CAST('abc' AS double) + ^^^^^^^^^^^^^^^^^^^^^ + + -- !query SELECT CAST('1234567890123' AS int) -- !query schema @@ -122,6 +146,30 @@ SELECT CAST('' AS long) ^^^^^^^^^^^^^^^^ +-- !query +SELECT CAST('' AS float) +-- !query schema +struct<> +-- !query output +org.apache.spark.SparkNumberFormatException +Invalid input syntax for type FLOAT: ''. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +== SQL(line 1, position 7) == +SELECT CAST('' AS float) + ^^^^^^^^^^^^^^^^^ + + +-- !query +SELECT CAST('' AS double) +-- !query schema +struct<> +-- !query output +org.apache.spark.SparkNumberFormatException +Invalid input syntax for type DOUBLE: ''. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +== SQL(line 1, position 7) == +SELECT CAST('' AS double) + ^^^^^^^^^^^^^^^^^^ + + -- !query SELECT CAST(NULL AS int) -- !query schema @@ -162,6 +210,30 @@ SELECT CAST('123.a' AS long) ^^^^^^^^^^^^^^^^^^^^^ +-- !query +SELECT CAST('123.a' AS float) +-- !query schema +struct<> +-- !query output +org.apache.spark.SparkNumberFormatException +Invalid input syntax for type FLOAT: '123.a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +== SQL(line 1, position 7) == +SELECT CAST('123.a' AS float) + ^^^^^^^^^^^^^^^^^^^^^^ + + +-- !query +SELECT CAST('123.a' AS double) +-- !query schema +struct<> +-- !query output +org.apache.spark.SparkNumberFormatException +Invalid input syntax for type DOUBLE: '123.a'. To return NULL instead, use 'try_cast'. If necessary set spark.sql.ansi.enabled to false to bypass this error. +== SQL(line 1, position 7) == +SELECT CAST('123.a' AS double) + ^^^^^^^^^^^^^^^^^^^^^^^ + + -- !query SELECT CAST('-2147483648' AS int) -- !query schema From ae43c19276d6842064ece4b9fb2529c8a364f9e9 Mon Sep 17 00:00:00 2001 From: Xinyi Yu Date: Tue, 19 Apr 2022 10:05:56 -0700 Subject: [PATCH 9/9] address final comments --- .../sql/errors/QueryExecutionErrors.scala | 12 ++--- .../org/apache/spark/sql/types/Decimal.scala | 4 +- .../resources/sql-tests/results/cast.sql.out | 50 ++++++++++++++++++- 3 files changed, 58 insertions(+), 8 deletions(-) diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala index 290dce1542eec..20be5de7bc64c 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala @@ -1013,12 +1013,12 @@ object QueryExecutionErrors extends QueryErrorsBase { } def cannotCastToDateTimeError(value: Any, to: DataType, errorContext: String): Throwable = { - new DateTimeException(s"Invalid input syntax for type ${toSQLType(to)}: " + - s"${if (value.isInstanceOf[UTF8String]) { - toSQLValue(value, StringType) - } else { - toSQLValue(value) - }}. " + + val valueString = if (value.isInstanceOf[UTF8String]) { + toSQLValue(value, StringType) + } else { + toSQLValue(value) + } + new DateTimeException(s"Invalid input syntax for type ${toSQLType(to)}: $valueString. " + s"To return NULL instead, use 'try_cast'. If necessary set ${SQLConf.ANSI_ENABLED.key} " + s"to false to bypass this error." + errorContext) } diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/types/Decimal.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/types/Decimal.scala index 7ea0067e63771..22e57fae52de4 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/types/Decimal.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/types/Decimal.scala @@ -613,7 +613,9 @@ object Decimal { } } - def fromStringANSI(str: UTF8String, to: DecimalType = DecimalType.USER_DEFAULT, + def fromStringANSI( + str: UTF8String, + to: DecimalType = DecimalType.USER_DEFAULT, errorContext: String = ""): Decimal = { try { val bigDecimal = stringToJavaBigDecimal(str) diff --git a/sql/core/src/test/resources/sql-tests/results/cast.sql.out b/sql/core/src/test/resources/sql-tests/results/cast.sql.out index 9ed02e3bed2c6..aaa82e4351351 100644 --- a/sql/core/src/test/resources/sql-tests/results/cast.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/cast.sql.out @@ -1,5 +1,5 @@ -- Automatically generated by SQLQueryTestSuite --- Number of queries: 66 +-- Number of queries: 72 -- !query @@ -50,6 +50,22 @@ struct NULL +-- !query +SELECT CAST('abc' AS float) +-- !query schema +struct +-- !query output +NULL + + +-- !query +SELECT CAST('abc' AS double) +-- !query schema +struct +-- !query output +NULL + + -- !query SELECT CAST('1234567890123' AS int) -- !query schema @@ -82,6 +98,22 @@ struct NULL +-- !query +SELECT CAST('' AS float) +-- !query schema +struct +-- !query output +NULL + + +-- !query +SELECT CAST('' AS double) +-- !query schema +struct +-- !query output +NULL + + -- !query SELECT CAST(NULL AS int) -- !query schema @@ -114,6 +146,22 @@ struct NULL +-- !query +SELECT CAST('123.a' AS float) +-- !query schema +struct +-- !query output +NULL + + +-- !query +SELECT CAST('123.a' AS double) +-- !query schema +struct +-- !query output +NULL + + -- !query SELECT CAST('-2147483648' AS int) -- !query schema