Skip to content
Closed
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 @@ -204,15 +204,15 @@ final class Decimal extends Ordered[Decimal] with Serializable {
if (decimalVal.ne(null)) {
decimalVal.toBigInt
} else {
BigInt(toLong)
BigInt(actualLongVal)
}
}

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

Expand All @@ -226,7 +226,7 @@ final class Decimal extends Ordered[Decimal] with Serializable {

override def toString: String = toBigDecimal.toString()

def toPlainString: String = toBigDecimal.bigDecimal.toPlainString
def toPlainString: String = toJavaBigDecimal.toPlainString

def toDebugString: String = {
if (decimalVal.ne(null)) {
Expand All @@ -240,9 +240,11 @@ final class Decimal extends Ordered[Decimal] with Serializable {

def toFloat: Float = toBigDecimal.floatValue

private def actualLongVal: Long = longVal / POW_10(_scale)

def toLong: Long = {
if (decimalVal.eq(null)) {
longVal / POW_10(_scale)
actualLongVal
} else {
decimalVal.longValue
}
Expand Down Expand Up @@ -278,7 +280,6 @@ final class Decimal extends Ordered[Decimal] with Serializable {
private def roundToNumeric[T <: AnyVal](integralType: IntegralType, maxValue: Int, minValue: Int)
(f1: Long => T) (f2: Double => T): T = {
if (decimalVal.eq(null)) {
val actualLongVal = longVal / POW_10(_scale)
val numericVal = f1(actualLongVal)
if (actualLongVal == numericVal) {
numericVal
Expand All @@ -303,7 +304,7 @@ final class Decimal extends Ordered[Decimal] with Serializable {
*/
private[sql] def roundToLong(): Long = {
if (decimalVal.eq(null)) {
longVal / POW_10(_scale)
actualLongVal
} else {
try {
// We cannot store Long.MAX_VALUE as a Double without losing precision.
Expand Down Expand Up @@ -455,7 +456,7 @@ final class Decimal extends Ordered[Decimal] with Serializable {

override def hashCode(): Int = toBigDecimal.hashCode()

def isZero: Boolean = if (decimalVal.ne(null)) decimalVal == BIG_DEC_ZERO else longVal == 0
def isZero: Boolean = if (decimalVal.ne(null)) decimalVal.signum == 0 else longVal == 0

// We should follow DecimalPrecision promote if use longVal for add and subtract:
// Operation Result Precision Result Scale
Expand All @@ -466,15 +467,15 @@ final class Decimal extends Ordered[Decimal] with Serializable {
if (decimalVal.eq(null) && that.decimalVal.eq(null) && scale == that.scale) {
Decimal(longVal + that.longVal, Math.max(precision, that.precision) + 1, scale)
} else {
Decimal(toBigDecimal.bigDecimal.add(that.toBigDecimal.bigDecimal))
Decimal(toJavaBigDecimal.add(that.toJavaBigDecimal))
}
}

def - (that: Decimal): Decimal = {
if (decimalVal.eq(null) && that.decimalVal.eq(null) && scale == that.scale) {
Decimal(longVal - that.longVal, Math.max(precision, that.precision) + 1, scale)
} else {
Decimal(toBigDecimal.bigDecimal.subtract(that.toBigDecimal.bigDecimal))
Decimal(toJavaBigDecimal.subtract(that.toJavaBigDecimal))
}
}

Expand Down Expand Up @@ -504,7 +505,7 @@ final class Decimal extends Ordered[Decimal] with Serializable {
}
}

def abs: Decimal = if (this.compare(Decimal.ZERO) < 0) this.unary_- else this
def abs: Decimal = if (this < Decimal.ZERO) this.unary_- else this
Copy link
Contributor

Choose a reason for hiding this comment

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

does this have a real impact?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this < Decimal.ZERO is more clear.


def floor: Decimal = if (scale == 0) this else {
val newPrecision = DecimalType.bounded(precision - scale + 1, 0).precision
Expand Down Expand Up @@ -532,8 +533,6 @@ object Decimal {

val POW_10 = Array.tabulate[Long](MAX_LONG_DIGITS + 1)(i => math.pow(10, i).toLong)

private val BIG_DEC_ZERO = BigDecimal(0)

private val MATH_CONTEXT = new MathContext(DecimalType.MAX_PRECISION, RoundingMode.HALF_UP)

private[sql] val ZERO = Decimal(0)
Expand Down Expand Up @@ -575,9 +574,8 @@ object Decimal {
}
}

private def numDigitsInIntegralPart(bigDecimal: JavaBigDecimal): Int = {
bigDecimal.precision - bigDecimal.scale
}
private def numDigitsInIntegralPart(bigDecimal: JavaBigDecimal): Int =
bigDecimal.precision - bigDecimal.scale

private def stringToJavaBigDecimal(str: UTF8String): JavaBigDecimal = {
// According the benchmark test, `s.toString.trim` is much faster than `s.trim.toString`.
Expand Down