Skip to content

Commit 9a15ba2

Browse files
committed
initial commit
1 parent 74aa0df commit 9a15ba2

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,17 @@ final class Decimal extends Ordered[Decimal] with Serializable {
135135
* Set this Decimal to the given BigInteger value. Will have precision 38 and scale 0.
136136
*/
137137
def set(bigintval: BigInteger): Decimal = {
138-
this.decimalVal = null
139-
this.longVal = bigintval.longValueExact()
140-
this._precision = DecimalType.MAX_PRECISION
141-
this._scale = 0
142-
this
138+
try {
139+
this.decimalVal = null
140+
this.longVal = bigintval.longValueExact()
141+
this._precision = DecimalType.MAX_PRECISION
142+
this._scale = 0
143+
this
144+
} catch {
145+
case _: ArithmeticException =>
146+
set(BigDecimal(bigintval))
147+
this
148+
}
143149
}
144150

145151
/**

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,10 @@ class DecimalSuite extends SparkFunSuite with PrivateMethodTester {
212212
}
213213
}
214214
}
215+
216+
test("SPARK-20341: support BigInt's value does not fit in long value range") {
217+
val bigInt = scala.math.BigInt("9223372036854775808")
218+
val decimal = Decimal.apply(bigInt)
219+
assert(decimal.toJavaBigDecimal.unscaledValue.toString === "9223372036854775808")
220+
}
215221
}

0 commit comments

Comments
 (0)