Skip to content

Commit 37526ac

Browse files
Davies Liudavies
authored andcommitted
[SPARK-10980] [SQL] fix bug in create Decimal
The created decimal is wrong if using `Decimal(unscaled, precision, scale)` with unscaled > 1e18 and and precision > 18 and scale > 0. This bug exists since the beginning. Author: Davies Liu <[email protected]> Closes #9014 from davies/fix_decimal.
1 parent 7bf07fa commit 37526ac

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/types/Decimal.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ final class Decimal extends Ordered[Decimal] with Serializable {
8888
if (precision < 19) {
8989
return null // Requested precision is too low to represent this value
9090
}
91-
this.decimalVal = BigDecimal(unscaled)
91+
this.decimalVal = BigDecimal(unscaled, scale)
9292
this.longVal = 0L
9393
} else {
9494
val p = POW_10(math.min(precision, MAX_LONG_DIGITS))

sql/catalyst/src/test/scala/org/apache/spark/sql/types/decimal/DecimalSuite.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class DecimalSuite extends SparkFunSuite with PrivateMethodTester {
4444
checkDecimal(Decimal(170L, 4, 2), "1.70", 4, 2)
4545
checkDecimal(Decimal(17L, 24, 1), "1.7", 24, 1)
4646
checkDecimal(Decimal(1e17.toLong, 18, 0), 1e17.toLong.toString, 18, 0)
47+
checkDecimal(Decimal(1000000000000000000L, 20, 2), "10000000000000000.00", 20, 2)
4748
checkDecimal(Decimal(Long.MaxValue), Long.MaxValue.toString, 20, 0)
4849
checkDecimal(Decimal(Long.MinValue), Long.MinValue.toString, 20, 0)
4950
intercept[IllegalArgumentException](Decimal(170L, 2, 1))

0 commit comments

Comments
 (0)