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



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/NumberUtils.scala
##########
@@ -0,0 +1,189 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.catalyst.util
+
+import java.math.BigDecimal
+import java.text.{DecimalFormat, NumberFormat, ParsePosition}
+import java.util.Locale
+
+import org.apache.spark.sql.errors.{QueryCompilationErrors, 
QueryExecutionErrors}
+import org.apache.spark.sql.types.Decimal
+import org.apache.spark.unsafe.types.UTF8String
+
+object NumberUtils {
+
+  private val pointSign = '.'
+  private val letterPointSign = 'D'
+  private val commaSign = ','
+  private val letterCommaSign = 'G'
+  private val minusSign = '-'
+  private val letterMinusSign = 'S'
+  private val dollarSign = '$'
+
+  private val commaSignStr = commaSign.toString
+
+  private def normalize(format: String): String = {
+    var notFindDecimalPoint = true
+    val normalizedFormat = format.toUpperCase(Locale.ROOT).map {
+      case '9' if notFindDecimalPoint => '#'
+      case '9' if !notFindDecimalPoint => '0'
+      case `letterPointSign` =>
+        notFindDecimalPoint = false
+        pointSign
+      case `letterCommaSign` => commaSign
+      case `letterMinusSign` => minusSign
+      case `pointSign` =>
+        notFindDecimalPoint = false
+        pointSign
+      case other => other
+    }
+    // If the comma is at the beginning or end of number format, then 
DecimalFormat will be invalid.
+    // For example, "##,###," or ",###,###" for DecimalFormat is invalid, so 
we must use "##,###"
+    // or "###,###".
+    normalizedFormat.stripPrefix(commaSignStr).stripSuffix(commaSignStr)
+  }
+
+  private def isSign(c: Char): Boolean = {
+    Set(pointSign, commaSign, minusSign, dollarSign).contains(c)
+  }
+
+  private def transform(format: String): String = {
+    if (format.contains(minusSign)) {
+      val positiveFormatString = format.replaceAll("-", "")
+      s"$positiveFormatString;$format"
+    } else {
+      format
+    }
+  }
+
+  private def check(normalizedFormat: String, numberFormat: String) = {
+    def invalidSignPosition(format: String, c: Char): Boolean = {
+      val signIndex = format.indexOf(c)
+      signIndex > 0 && signIndex < format.length - 1
+    }
+
+    if (normalizedFormat.count(_ == pointSign) > 1) {
+      throw QueryCompilationErrors.multipleSignInNumberFormatError(
+        s"'$letterPointSign' or '$pointSign'", numberFormat)
+    } else if (normalizedFormat.count(_ == minusSign) > 1) {
+      throw QueryCompilationErrors.multipleSignInNumberFormatError(
+        s"'$letterMinusSign' or '$minusSign'", numberFormat)
+    } else if (normalizedFormat.count(_ == dollarSign) > 1) {
+      throw 
QueryCompilationErrors.multipleSignInNumberFormatError(s"'$dollarSign'", 
numberFormat)
+    } else if (invalidSignPosition(normalizedFormat, minusSign)) {
+      throw QueryCompilationErrors.nonFistOrLastCharInNumberFormatError(
+        s"'$letterMinusSign' or '$minusSign'", numberFormat)
+    } else if (invalidSignPosition(normalizedFormat, dollarSign)) {
+      throw QueryCompilationErrors.nonFistOrLastCharInNumberFormatError(
+        s"'$dollarSign'", numberFormat)
+    }
+  }
+
+  /**
+   * Convert string to numeric based on the given number format.
+   * The format can consist of the following characters:
+   * '9':  digit position (can be dropped if insignificant)
+   * '0':  digit position (will not be dropped, even if insignificant)
+   * '.':  decimal point (only allowed once)
+   * ',':  group (thousands) separator
+   * 'S':  sign anchored to number (uses locale)
+   * 'D':  decimal point (uses locale)
+   * 'G':  group separator (uses locale)
+   * '$':  specifies that the input value has a leading $ (Dollar) sign.
+   *
+   * @param input the string need to converted
+   * @param numberFormat the given number format
+   * @return decimal obtained from string parsing
+   */
+  def parse(input: UTF8String, numberFormat: String): Decimal = {
+    val normalizedFormat = normalize(numberFormat)
+    check(normalizedFormat, numberFormat)
+
+    val precision = normalizedFormat.filterNot(isSign).length
+    val formatSplits = normalizedFormat.split(pointSign)
+    val scale = if (formatSplits.length == 1) {
+      0
+    } else {
+      formatSplits(1).filterNot(isSign).length
+    }
+    val transformedFormat = transform(normalizedFormat)
+    val numberFormatInstance = NumberFormat.getInstance()
+    val numberDecimalFormat = numberFormatInstance.asInstanceOf[DecimalFormat]

Review comment:
       Yes. The default choice is `NUMBERSTYLE`, it will return `DecimalFormat` 
indeed.
   ```
   public final static NumberFormat getInstance() {
       return getInstance(Locale.getDefault(Locale.Category.FORMAT), 
NUMBERSTYLE);
   }
   ```
   Let's add assert.




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