iRakson commented on a change in pull request #26933: [SPARK-30292][SQL]Throw 
Exception when invalid string is cast to numeric type in ANSI mode
URL: https://github.com/apache/spark/pull/26933#discussion_r364179887
 
 

 ##########
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala
 ##########
 @@ -1473,6 +1543,22 @@ abstract class CastBase extends UnaryExpression with 
TimeZoneAwareExpression wit
 
   private[this] def castToFloatCode(from: DataType, ctx: CodegenContext): 
CastFunction = {
     from match {
+      case StringType if ansiEnabled =>
+        val floatStr = ctx.freshVariable("floatStr", StringType)
+        (c, evPrim, evNull) =>
+          code"""
+          final String $floatStr = $c.toString();
+          try {
+            $evPrim = Float.valueOf($floatStr);
+          } catch (java.lang.NumberFormatException e) {
+            final Float f = (Float) 
Cast.processFloatingPointSpecialLiterals($floatStr, true);
+            if (f == null) {
 
 Review comment:
   Done

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to