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 @@ -185,9 +185,21 @@ final class Decimal extends Ordered[Decimal] with Serializable {
}
}

def toScalaBigInt: BigInt = BigInt(toLong)
def toScalaBigInt: BigInt = {
if (decimalVal.ne(null)) {
decimalVal.toBigInt()
} else {
BigInt(toLong)
}
}

def toJavaBigInteger: java.math.BigInteger = java.math.BigInteger.valueOf(toLong)
def toJavaBigInteger: java.math.BigInteger = {
if (decimalVal.ne(null)) {
decimalVal.underlying().toBigInteger()
} else {
java.math.BigInteger.valueOf(toLong)
}
}

def toUnscaledLong: Long = {
if (decimalVal.ne(null)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,15 @@ class DecimalSuite extends SparkFunSuite with PrivateMethodTester {
val decimal = Decimal.apply(bigInt)
assert(decimal.toJavaBigDecimal.unscaledValue.toString === "9223372036854775808")
}

test("SPARK-26038: toScalaBigInt/toJavaBigInteger") {
// not fitting long
val decimal = Decimal("1234568790123456789012348790.1234879012345678901234568790")
assert(decimal.toScalaBigInt == scala.math.BigInt("1234568790123456789012348790"))
assert(decimal.toJavaBigInteger == new java.math.BigInteger("1234568790123456789012348790"))
// fitting long
val decimalLong = Decimal(123456789123456789L, 18, 9)
assert(decimalLong.toScalaBigInt == scala.math.BigInt("123456789"))
assert(decimalLong.toJavaBigInteger == new java.math.BigInteger("123456789"))
}
}