beliefer commented on a change in pull request #35060:
URL: https://github.com/apache/spark/pull/35060#discussion_r782846377
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/NumberUtils.scala
##########
@@ -77,80 +107,91 @@ object NumberUtils {
signIndex > 0 && signIndex < format.length - 1
}
- if (normalizedFormat.count(_ == pointSign) > 1) {
+ if (normalizedFormat.length == 0) {
+ throw QueryCompilationErrors.emptyNumberFormatError()
+ } else if (normalizedFormat.count(_ == POINT_SIGN) > 1) {
throw QueryCompilationErrors.multipleSignInNumberFormatError(
- s"'$letterPointSign' or '$pointSign'", numberFormat)
- } else if (normalizedFormat.count(_ == minusSign) > 1) {
+ s"'$POINT_LETTER' or '$POINT_SIGN'", numberFormat)
+ } else if (normalizedFormat.count(_ == MINUS_SIGN) > 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)) {
+ s"'$MINUS_LETTER' or '$MINUS_SIGN'", numberFormat)
+ } else if (normalizedFormat.count(_ == DOLLAR_SIGN) > 1) {
+ throw
QueryCompilationErrors.multipleSignInNumberFormatError(s"'$DOLLAR_SIGN'",
numberFormat)
+ } else if (invalidSignPosition(normalizedFormat, MINUS_SIGN)) {
throw QueryCompilationErrors.nonFistOrLastCharInNumberFormatError(
- s"'$letterMinusSign' or '$minusSign'", numberFormat)
- } else if (invalidSignPosition(normalizedFormat, dollarSign)) {
+ s"'$MINUS_LETTER' or '$MINUS_SIGN'", numberFormat)
+ } else if (invalidSignPosition(normalizedFormat, DOLLAR_SIGN)) {
throw QueryCompilationErrors.nonFistOrLastCharInNumberFormatError(
- s"'$dollarSign'", numberFormat)
+ s"'$DOLLAR_SIGN'", numberFormat)
+ }
+ }
+
+ private def getPrecision(numberFormat: String): Int =
+ numberFormat.filterNot(isSign).length
+
+ private def getScale(numberFormat: String): Int = {
+ val formatSplits = numberFormat.split(POINT_SIGN)
+ if (formatSplits.length == 1) {
+ 0
+ } else {
+ formatSplits(1).filterNot(isSign).length
}
}
/**
* 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.
+ * '0' or '9': digit position
+ * '.' or 'D': decimal point (only allowed once)
+ * ',' or 'G': group (thousands) separator
+ * '-' or 'S': sign anchored to number (only allowed once)
+ * '$': value with a leading dollar sign (only allowed once)
*
* @param input the string need to converted
- * @param numberFormat the given number format
+ * @param originNumberFormat the origin number format
+ * @param normalizedNumberFormat normalized number format
+ * @param precision decimal precision
+ * @param scale decimal scale
* @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]
- numberDecimalFormat.setParseBigDecimal(true)
- numberDecimalFormat.applyPattern(transformedFormat)
+ private def parse(
+ input: UTF8String,
+ originNumberFormat: String,
+ normalizedNumberFormat: String,
+ precision: Int,
+ scale: Int): Decimal = {
val inputStr = input.toString.trim
- val inputSplits = inputStr.split(pointSign)
+ val inputSplits = inputStr.split(POINT_SIGN)
if (inputSplits.length == 1) {
if (inputStr.filterNot(isSign).length > precision - scale) {
- throw QueryExecutionErrors.invalidNumberFormatError(numberFormat)
+ throw QueryExecutionErrors.invalidNumberFormatError(originNumberFormat)
}
} else if (inputSplits(0).filterNot(isSign).length > precision - scale ||
inputSplits(1).filterNot(isSign).length > scale) {
- throw QueryExecutionErrors.invalidNumberFormatError(numberFormat)
+ throw QueryExecutionErrors.invalidNumberFormatError(originNumberFormat)
+ }
+
+ val transformedFormat = transform(normalizedNumberFormat)
+ try {
+ numberDecimalFormat.applyLocalizedPattern(transformedFormat)
Review comment:
Updated
--
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]