Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,8 @@ object Decimal {
val bigDecimal = stringToJavaBigDecimal(str)
// We fast fail because constructing a very large JavaBigDecimal to Decimal is very slow.
// For example: Decimal("6.0790316E+25569151")
if (numDigitsInIntegralPart(bigDecimal) > DecimalType.MAX_PRECISION) {
if (numDigitsInIntegralPart(bigDecimal) > DecimalType.MAX_PRECISION &&
!SQLConf.get.allowNegativeScaleOfDecimalEnabled) {
null
} else {
Decimal(bigDecimal)
Expand All @@ -617,7 +618,8 @@ object Decimal {
val bigDecimal = stringToJavaBigDecimal(str)
// We fast fail because constructing a very large JavaBigDecimal to Decimal is very slow.
// For example: Decimal("6.0790316E+25569151")
if (numDigitsInIntegralPart(bigDecimal) > DecimalType.MAX_PRECISION) {
if (numDigitsInIntegralPart(bigDecimal) > DecimalType.MAX_PRECISION &&
!SQLConf.get.allowNegativeScaleOfDecimalEnabled) {
throw new ArithmeticException(s"out of decimal type range: $str")
} else {
Decimal(bigDecimal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,4 +299,19 @@ class DecimalSuite extends SparkFunSuite with PrivateMethodTester with SQLHelper
assert(Decimal.fromStringANSI(UTF8String.fromString(string)) === Decimal(string))
}
}

test("SPARK-37451: Performance improvement regressed String to Decimal cast") {
val values = Array("7.836725755512218E38")
for (string <- values) {
assert(Decimal.fromString(UTF8String.fromString(string)) === null)
intercept[ArithmeticException](Decimal.fromStringANSI(UTF8String.fromString(string)))
}

withSQLConf(SQLConf.LEGACY_ALLOW_NEGATIVE_SCALE_OF_DECIMAL_ENABLED.key -> "true") {
for (string <- values) {
assert(Decimal.fromString(UTF8String.fromString(string)) === Decimal(string))
assert(Decimal.fromStringANSI(UTF8String.fromString(string)) === Decimal(string))
}
}
}
}