[SPARK-32586][SQL] Fix NumberFormatException error message when ansi is enabled
### What changes were proposed in this pull request? This pr fixes the error message of `NumberFormatException` when casting invalid input to FractionalType and enabling **ansi**: ``` spark-sql> set spark.sql.ansi.enabled=true; spark.sql.ansi.enabled true spark-sql> create table SPARK_32586 using parquet as select 's' s; spark-sql> select * from SPARK_32586 where s > 1.13D; java.lang.NumberFormatException: invalid input syntax for type numeric: columnartorow_value_0 ``` After this pr: ``` spark-sql> select * from SPARK_32586 where s > 1.13D; java.lang.NumberFormatException: invalid input syntax for type numeric: s ``` ### Why are the changes needed? Improve error message. ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? Unit test. Closes #29405 from wangyum/SPARK-32586. Authored-by: Yuming Wang <yumwang@ebay.com> Signed-off-by: HyukjinKwon <gurwls223@apache.org>
This commit is contained in:
parent
e7c1204f6c
commit
5d130f0360
|
@ -1189,7 +1189,7 @@ abstract class CastBase extends UnaryExpression with TimeZoneAwareExpression wit
|
|||
case StringType =>
|
||||
(c, evPrim, evNull) =>
|
||||
val handleException = if (ansiEnabled) {
|
||||
s"""throw new NumberFormatException("invalid input syntax for type numeric: $c");"""
|
||||
s"""throw new NumberFormatException("invalid input syntax for type numeric: " + $c);"""
|
||||
} else {
|
||||
s"$evNull =true;"
|
||||
}
|
||||
|
@ -1549,7 +1549,7 @@ abstract class CastBase extends UnaryExpression with TimeZoneAwareExpression wit
|
|||
val floatStr = ctx.freshVariable("floatStr", StringType)
|
||||
(c, evPrim, evNull) =>
|
||||
val handleNull = if (ansiEnabled) {
|
||||
s"""throw new NumberFormatException("invalid input syntax for type numeric: $c");"""
|
||||
s"""throw new NumberFormatException("invalid input syntax for type numeric: " + $c);"""
|
||||
} else {
|
||||
s"$evNull = true;"
|
||||
}
|
||||
|
@ -1585,7 +1585,7 @@ abstract class CastBase extends UnaryExpression with TimeZoneAwareExpression wit
|
|||
val doubleStr = ctx.freshVariable("doubleStr", StringType)
|
||||
(c, evPrim, evNull) =>
|
||||
val handleNull = if (ansiEnabled) {
|
||||
s"""throw new NumberFormatException("invalid input syntax for type numeric: $c");"""
|
||||
s"""throw new NumberFormatException("invalid input syntax for type numeric: " + $c);"""
|
||||
} else {
|
||||
s"$evNull = true;"
|
||||
}
|
||||
|
|
|
@ -1366,24 +1366,25 @@ class AnsiCastSuite extends CastSuiteBase {
|
|||
val array = Literal.create(Seq("123", "true", "f", null),
|
||||
ArrayType(StringType, containsNull = true))
|
||||
checkExceptionInExpression[NumberFormatException](
|
||||
cast(array, ArrayType(dataType, containsNull = true)), "invalid input")
|
||||
cast(array, ArrayType(dataType, containsNull = true)),
|
||||
"invalid input syntax for type numeric: true")
|
||||
checkExceptionInExpression[NumberFormatException](
|
||||
cast("string", dataType), "invalid input")
|
||||
cast("string", dataType), "invalid input syntax for type numeric: string")
|
||||
checkExceptionInExpression[NumberFormatException](
|
||||
cast("123-string", dataType), "invalid input")
|
||||
cast("123-string", dataType), "invalid input syntax for type numeric: 123-string")
|
||||
checkExceptionInExpression[NumberFormatException](
|
||||
cast("2020-07-19", dataType), "invalid input")
|
||||
cast("2020-07-19", dataType), "invalid input syntax for type numeric: 2020-07-19")
|
||||
checkExceptionInExpression[NumberFormatException](
|
||||
cast("1.23", dataType), "invalid input")
|
||||
cast("1.23", dataType), "invalid input syntax for type numeric: 1.23")
|
||||
}
|
||||
|
||||
Seq(DoubleType, FloatType, DecimalType.USER_DEFAULT).foreach { dataType =>
|
||||
checkExceptionInExpression[NumberFormatException](
|
||||
cast("string", dataType), "invalid input")
|
||||
cast("string", dataType), "invalid input syntax for type numeric: string")
|
||||
checkExceptionInExpression[NumberFormatException](
|
||||
cast("123.000.00", dataType), "invalid input")
|
||||
cast("123.000.00", dataType), "invalid input syntax for type numeric: 123.000.00")
|
||||
checkExceptionInExpression[NumberFormatException](
|
||||
cast("abc.com", dataType), "invalid input")
|
||||
cast("abc.com", dataType), "invalid input syntax for type numeric: abc.com")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue