spark-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gurwls...@apache.org
Subject [spark] branch branch-3.0 updated: [SPARK-32586][SQL] Fix NumberFormatException error message when ansi is enabled
Date Wed, 12 Aug 2020 04:23:54 GMT
This is an automated email from the ASF dual-hosted git repository.

gurwls223 pushed a commit to branch branch-3.0
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/branch-3.0 by this push:
     new 292bfc3  [SPARK-32586][SQL] Fix NumberFormatException error message when ansi is
enabled
292bfc3 is described below

commit 292bfc3b92d0d59b3f05781a3a3436286f317bce
Author: Yuming Wang <yumwang@ebay.com>
AuthorDate: Wed Aug 12 13:16:57 2020 +0900

    [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>
    (cherry picked from commit 5d130f03607d2448e2f01814de7d330c512901b7)
    Signed-off-by: HyukjinKwon <gurwls223@apache.org>
---
 .../apache/spark/sql/catalyst/expressions/Cast.scala    |  6 +++---
 .../spark/sql/catalyst/expressions/CastSuite.scala      | 17 +++++++++--------
 2 files changed, 12 insertions(+), 11 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 ef70915..c8985d4 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
@@ -1153,7 +1153,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;"
           }
@@ -1513,7 +1513,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;"
           }
@@ -1549,7 +1549,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;"
           }
diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastSuite.scala
b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastSuite.scala
index f563720..08aa2aa 100644
--- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastSuite.scala
+++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastSuite.scala
@@ -1332,24 +1332,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")
     }
   }
 


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org


Mime
View raw message