File tree Expand file tree Collapse file tree 2 files changed +17
-5
lines changed
main/scala/org/apache/spark/sql/types
test/scala/org/apache/spark/sql/types Expand file tree Collapse file tree 2 files changed +17
-5
lines changed Original file line number Diff line number Diff 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 /**
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments