beliefer commented on a change in pull request #35060:
URL: https://github.com/apache/spark/pull/35060#discussion_r783659930



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/NumberUtils.scala
##########
@@ -164,26 +192,74 @@ object NumberUtils {
     val bigDecimal = input.toJavaBigDecimal
     val decimalPlainStr = bigDecimal.toPlainString
     if (decimalPlainStr.length > transformedFormat.length) {
-      transformedFormat.replaceAll("0", "#")
+      transformedFormat.replaceAll("0", POUND_SIGN_STRING)
     } else {
-      val decimalFormat = new DecimalFormat(transformedFormat)
-      var resultStr = decimalFormat.format(bigDecimal)
+      val numberDecimalFormat = {
+        val decimalFormat = new DecimalFormat()
+        decimalFormat.setParseBigDecimal(true)
+        try {
+          decimalFormat.applyLocalizedPattern(transformedFormat)
+        } catch {
+          case _: IllegalArgumentException =>
+            throw QueryExecutionErrors.invalidNumberFormatError(numberFormat)
+        }
+        decimalFormat
+      }
+      var resultStr = numberDecimalFormat.format(bigDecimal)
       // Since we trimmed the comma at the beginning or end of number format 
in function
       // `normalize`, we restore the comma to the result here.
       // For example, if the specified number format is "99,999," or 
",999,999", function
       // `normalize` normalize them to "##,###" or "###,###".
       // new DecimalFormat("##,###").parse(12454) and new 
DecimalFormat("###,###").parse(124546)
       // will return "12,454" and "124,546" respectively. So we add ',' at the 
end and head of
       // the result, then the final output are "12,454," or ",124,546".
-      if (numberFormat.last == commaSign || numberFormat.last == 
letterCommaSign) {
-        resultStr = resultStr + commaSign
+      if (numberFormat.last == COMMA_SIGN || numberFormat.last == 
COMMA_LETTER) {
+        resultStr = resultStr + COMMA_SIGN
       }
-      if (numberFormat.charAt(0) == commaSign || numberFormat.charAt(0) == 
letterCommaSign) {
-        resultStr = commaSign + resultStr
+      if (numberFormat.charAt(0) == COMMA_SIGN || numberFormat.charAt(0) == 
COMMA_LETTER) {
+        resultStr = COMMA_SIGN + resultStr
       }
 
       resultStr
     }
   }
 
+  class NumberFormatBuilder(originNumberFormat: String) extends Serializable {

Review comment:
       `NumberFormatBuilder` used to keep context of number format and some 
cache.
   `NumberFormatBuilder` also used as the public API for `parse` and `format`.




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

To unsubscribe, e-mail: [email protected]

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