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_r363250085
 
 

 ##########
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala
 ##########
 @@ -482,6 +482,15 @@ abstract class CastBase extends UnaryExpression with 
TimeZoneAwareExpression wit
 
   // LongConverter
   private[this] def castToLong(from: DataType): Any => Any = from match {
+    case StringType if ansiEnabled =>
 
 Review comment:
   The implementation of `toLongExact` will be almost similar to `toLong`. 
Won't it be duplication of code? 
   What about this implementation ->
   
   ```
     public long toLongExact() {
       LongWrapper result = new LongWrapper();
       if (toLong(result)) {
         return result.value;
       }
       throw new NumberFormatException("invalid input syntax for type numeric: 
" + this);
     }
   ```
   
   Here, i can use the `toLong` code for checking whether string contains valid 
value or not.
   
   If we are not using the wrapper then implementation will be almost similar, 
as i will be throwing exception whenever a invalid value is there instead of 
returning `false` in `toLongExact`.

----------------------------------------------------------------
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