cloud-fan commented on a change in pull request #29731:
URL: https://github.com/apache/spark/pull/29731#discussion_r487712474



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala
##########
@@ -658,6 +658,41 @@ abstract class CastBase extends UnaryExpression with 
TimeZoneAwareExpression wit
     }
   }
 
+  private[this] def stringToDecimal(str: UTF8String, decimalType: 
DecimalType): Decimal = {
+    val bigDecimal = try {
+      // According the benchmark test,  `s.toString.trim` is much faster than 
`s.trim.toString`.
+      // Please refer to https://github.com/apache/spark/pull/26640
+      new JavaBigDecimal(str.toString.trim)
+    } catch {
+      case _: NumberFormatException =>
+        if (ansiEnabled) {
+          throw new NumberFormatException(s"invalid input syntax for type 
numeric: $str")
+        } else {
+          null
+        }
+    }
+
+    if (bigDecimal != null) {
+      val precision = if (bigDecimal.scale < 0) {
+        bigDecimal.precision - bigDecimal.scale
+      } else {
+        bigDecimal.precision
+      }
+
+      if (precision > DecimalType.MAX_PRECISION) {

Review comment:
       +1, let's explain which part is slow.




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



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

Reply via email to