Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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 @@ -132,14 +132,22 @@ final class Decimal extends Ordered[Decimal] with Serializable {
}

/**
* Set this Decimal to the given BigInteger value. Will have precision 38 and scale 0.
* Set this Decimal to the given BigInteger value. Will have precision 38 and scale 0
* if value is fit into long value range. Otherwise, use BigDecimal
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about

If the value is not in the range of long, convert it to BigDecimal and the precision and scale is based on the converted value.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, updated.

*
* This code avoids BigDecimal object allocation as possible to improve runtime efficiency
*/
def set(bigintval: BigInteger): Decimal = {
this.decimalVal = null
this.longVal = bigintval.longValueExact()
this._precision = DecimalType.MAX_PRECISION
this._scale = 0
this
try {
this.decimalVal = null
this.longVal = bigintval.longValueExact()
this._precision = DecimalType.MAX_PRECISION
this._scale = 0
this
} catch {
case _: ArithmeticException =>
set(BigDecimal(bigintval))
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,10 @@ class DecimalSuite extends SparkFunSuite with PrivateMethodTester {
}
}
}

test("SPARK-20341: support BigInt's value does not fit in long value range") {
val bigInt = scala.math.BigInt("9223372036854775808")
val decimal = Decimal.apply(bigInt)
assert(decimal.toJavaBigDecimal.unscaledValue.toString === "9223372036854775808")
}
}