Skip to content

Commit

Permalink
[SPARK-15445][SQL] Build fails for java 1.7 after adding java.mathBig…
Browse files Browse the repository at this point in the history
…Integer 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 <[email protected]>

Closes apache#13223 from techaddict/SPARK-15445.
  • Loading branch information
techaddict authored and srowen committed May 21, 2016
1 parent 45b7557 commit 666bf2e
Showing 1 changed file with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.spark.sql.types

import java.lang.{Long => JLong}
import java.math.{BigInteger, MathContext, RoundingMode}

import org.apache.spark.annotation.DeveloperApi
Expand Down Expand Up @@ -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.
*/
def set(bigintval: BigInteger): Decimal = {
try {
this.decimalVal = null
this.longVal = bigintval.longValueExact()
this._precision = DecimalType.MAX_PRECISION
this._scale = 0
this
}
catch {
case e: ArithmeticException =>
throw new IllegalArgumentException(s"BigInteger ${bigintval} too large for decimal")
}
// TODO: Remove this once we migrate to java8 and use longValueExact() instead.
require(
bigintval.compareTo(LONG_MAX_BIG_INT) <= 0 && bigintval.compareTo(LONG_MIN_BIG_INT) >= 0,
s"BigInteger $bigintval too large for decimal")
this.decimalVal = null
this.longVal = bigintval.longValue()
this._precision = DecimalType.MAX_PRECISION
this._scale = 0
this
}

/**
Expand Down Expand Up @@ -382,6 +381,9 @@ object Decimal {
private[sql] val ZERO = Decimal(0)
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: Long): Decimal = new Decimal().set(value)
Expand Down

0 comments on commit 666bf2e

Please sign in to comment.