This is an automated email from the ASF dual-hosted git repository. maxgekk pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/spark.git
The following commit(s) were added to refs/heads/master by this push: new ead45889278 [SPARK-39028][SQL] Use SparkDateTimeException when casting to datetime types failed ead45889278 is described below commit ead45889278e8c5f71dc2ff2c7b020592e5e897f Author: Gengliang Wang <gengli...@apache.org> AuthorDate: Tue Apr 26 22:06:07 2022 +0300 [SPARK-39028][SQL] Use SparkDateTimeException when casting to datetime types failed ### What changes were proposed in this pull request? Use SparkDateTimeException when casting to datetime types failed ### Why are the changes needed? It is more reasonable to throw `SparkDateTimeException` instead of `java.time.DateTimeException` ### Does this PR introduce _any_ user-facing change? Yes, a minor change for the exception type. ### How was this patch tested? UT Closes #36362 from gengliangwang/datetimeException. Authored-by: Gengliang Wang <gengli...@apache.org> Signed-off-by: Max Gekk <max.g...@gmail.com> --- .../spark/sql/errors/QueryExecutionErrors.scala | 5 ++--- .../resources/sql-tests/results/ansi/cast.sql.out | 20 ++++++++++---------- .../resources/sql-tests/results/ansi/date.sql.out | 4 ++-- .../results/ansi/datetime-parsing-invalid.sql.out | 8 ++++---- .../sql-tests/results/ansi/interval.sql.out | 16 ++++++++-------- .../results/postgreSQL/window_part3.sql.out | 2 +- .../results/timestampNTZ/timestamp-ansi.sql.out | 4 ++-- 7 files changed, 29 insertions(+), 30 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 59172682925..dd45f62ac09 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 @@ -1019,9 +1019,8 @@ object QueryExecutionErrors extends QueryErrorsBase { } 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) + new SparkDateTimeException("INVALID_SYNTAX_FOR_CAST", + Array(toSQLType(to), valueString, SQLConf.ANSI_ENABLED.key, errorContext)) } def registeringStreamingQueryListenerError(e: Exception): Throwable = { 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 96db4f2db42..566e27a0e20 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 @@ -697,8 +697,8 @@ select cast('a' as date) -- !query schema struct<> -- !query output -java.time.DateTimeException -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. +org.apache.spark.SparkDateTimeException +[INVALID_SYNTAX_FOR_CAST] 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) ^^^^^^^^^^^^^^^^^ @@ -717,8 +717,8 @@ select cast('a' as timestamp) -- !query schema struct<> -- !query output -java.time.DateTimeException -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. +org.apache.spark.SparkDateTimeException +[INVALID_SYNTAX_FOR_CAST] 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) ^^^^^^^^^^^^^^^^^^^^^^ @@ -737,8 +737,8 @@ select cast('a' as timestamp_ntz) -- !query schema struct<> -- !query output -java.time.DateTimeException -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. +org.apache.spark.SparkDateTimeException +[INVALID_SYNTAX_FOR_CAST] 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) ^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -749,8 +749,8 @@ select cast(cast('inf' as double) as timestamp) -- !query schema struct<> -- !query output -java.time.DateTimeException -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. +org.apache.spark.SparkDateTimeException +[INVALID_SYNTAX_FOR_CAST] 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) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -761,8 +761,8 @@ select cast(cast('inf' as float) as timestamp) -- !query schema struct<> -- !query output -java.time.DateTimeException -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. +org.apache.spark.SparkDateTimeException +[INVALID_SYNTAX_FOR_CAST] 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 cac99ba2057..41515cdee2a 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 @@ -231,8 +231,8 @@ select next_day("xx", "Mon") -- !query schema struct<> -- !query output -java.time.DateTimeException -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. +org.apache.spark.SparkDateTimeException +[INVALID_SYNTAX_FOR_CAST] 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") ^^^^^^^^^^^^^^^^^^^^^ 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 18587b6d005..22694bf84e2 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 @@ -241,8 +241,8 @@ select cast("Unparseable" as timestamp) -- !query schema struct<> -- !query output -java.time.DateTimeException -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. +org.apache.spark.SparkDateTimeException +[INVALID_SYNTAX_FOR_CAST] 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) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -253,8 +253,8 @@ select cast("Unparseable" as date) -- !query schema struct<> -- !query output -java.time.DateTimeException -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. +org.apache.spark.SparkDateTimeException +[INVALID_SYNTAX_FOR_CAST] 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 94d69354546..3cc089ebbf6 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 @@ -1515,8 +1515,8 @@ select '4 11:11' - interval '4 22:12' day to minute -- !query schema struct<> -- !query output -java.time.DateTimeException -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. +org.apache.spark.SparkDateTimeException +[INVALID_SYNTAX_FOR_CAST] 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 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -1527,8 +1527,8 @@ select '4 12:12:12' + interval '4 22:12' day to minute -- !query schema struct<> -- !query output -java.time.DateTimeException -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. +org.apache.spark.SparkDateTimeException +[INVALID_SYNTAX_FOR_CAST] 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 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -1565,8 +1565,8 @@ select str - interval '4 22:12' day to minute from interval_view -- !query schema struct<> -- !query output -java.time.DateTimeException -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. +org.apache.spark.SparkDateTimeException +[INVALID_SYNTAX_FOR_CAST] 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 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -1577,8 +1577,8 @@ select str + interval '4 22:12' day to minute from interval_view -- !query schema struct<> -- !query output -java.time.DateTimeException -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. +org.apache.spark.SparkDateTimeException +[INVALID_SYNTAX_FOR_CAST] 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/postgreSQL/window_part3.sql.out b/sql/core/src/test/resources/sql-tests/results/postgreSQL/window_part3.sql.out index 91657b2228e..79c54248768 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 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. +failed to evaluate expression CAST('11:00 BST' AS TIMESTAMP): [INVALID_SYNTAX_FOR_CAST] 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/timestampNTZ/timestamp-ansi.sql.out b/sql/core/src/test/resources/sql-tests/results/timestampNTZ/timestamp-ansi.sql.out index 920f3a7462b..1dee8d0ad98 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 @@ -331,8 +331,8 @@ select to_timestamp(1) -- !query schema struct<> -- !query output -java.time.DateTimeException -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. +org.apache.spark.SparkDateTimeException +[INVALID_SYNTAX_FOR_CAST] 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 --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@spark.apache.org For additional commands, e-mail: commits-h...@spark.apache.org