Skip to content
Snippets Groups Projects
Commit 666bf2e8 authored by Sandeep Singh's avatar Sandeep Singh Committed by Sean Owen
Browse files

[SPARK-15445][SQL] Build fails for java 1.7 after adding java.mathBigInteger support

## What changes were proposed in this pull request?
Using longValue() and then checking whether the value is in the range for a long manually.

## How was this patch tested?
Existing tests

Author: Sandeep Singh <sandeep@techaddict.me>

Closes #13223 from techaddict/SPARK-15445.
parent 45b7557e
No related branches found
No related tags found
No related merge requests found
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
package org.apache.spark.sql.types package org.apache.spark.sql.types
import java.lang.{Long => JLong}
import java.math.{BigInteger, MathContext, RoundingMode} import java.math.{BigInteger, MathContext, RoundingMode}
import org.apache.spark.annotation.DeveloperApi import org.apache.spark.annotation.DeveloperApi
...@@ -132,17 +133,15 @@ final class Decimal extends Ordered[Decimal] with Serializable { ...@@ -132,17 +133,15 @@ 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.
*/ */
def set(bigintval: BigInteger): Decimal = { def set(bigintval: BigInteger): Decimal = {
try { // TODO: Remove this once we migrate to java8 and use longValueExact() instead.
this.decimalVal = null require(
this.longVal = bigintval.longValueExact() bigintval.compareTo(LONG_MAX_BIG_INT) <= 0 && bigintval.compareTo(LONG_MIN_BIG_INT) >= 0,
this._precision = DecimalType.MAX_PRECISION s"BigInteger $bigintval too large for decimal")
this._scale = 0 this.decimalVal = null
this this.longVal = bigintval.longValue()
} this._precision = DecimalType.MAX_PRECISION
catch { this._scale = 0
case e: ArithmeticException => this
throw new IllegalArgumentException(s"BigInteger ${bigintval} too large for decimal")
}
} }
/** /**
...@@ -382,6 +381,9 @@ object Decimal { ...@@ -382,6 +381,9 @@ object Decimal {
private[sql] val ZERO = Decimal(0) private[sql] val ZERO = Decimal(0)
private[sql] val ONE = Decimal(1) private[sql] val ONE = Decimal(1)
private val LONG_MAX_BIG_INT = BigInteger.valueOf(JLong.MAX_VALUE)
private val LONG_MIN_BIG_INT = BigInteger.valueOf(JLong.MIN_VALUE)
def apply(value: Double): Decimal = new Decimal().set(value) def apply(value: Double): Decimal = new Decimal().set(value)
def apply(value: Long): Decimal = new Decimal().set(value) def apply(value: Long): Decimal = new Decimal().set(value)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment