-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-40387][SQL] Improve the implementation of Spark Decimal #37830
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9bb6947
[SPARK-40387][SQL] Improve the implementation of Spark Decimal
beliefer 61d7489
Update code
beliefer c5f743f
Update code
beliefer 0ae7dff
Update code
beliefer 0efecd5
Update code
beliefer aa60e0e
Update code
beliefer 9af453d
Update code
beliefer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -204,15 +204,15 @@ final class Decimal extends Ordered[Decimal] with Serializable { | |
| if (decimalVal.ne(null)) { | ||
| decimalVal.toBigInt | ||
| } else { | ||
| BigInt(toLong) | ||
| BigInt(rawLongValue) | ||
| } | ||
| } | ||
|
|
||
| def toJavaBigInteger: java.math.BigInteger = { | ||
| if (decimalVal.ne(null)) { | ||
| decimalVal.underlying().toBigInteger() | ||
| } else { | ||
| java.math.BigInteger.valueOf(toLong) | ||
| java.math.BigInteger.valueOf(rawLongValue) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -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)) { | ||
|
|
@@ -240,9 +240,11 @@ final class Decimal extends Ordered[Decimal] with Serializable { | |
|
|
||
| def toFloat: Float = toBigDecimal.floatValue | ||
|
|
||
| private def rawLongValue: Long = longVal / POW_10(_scale) | ||
|
|
||
| def toLong: Long = { | ||
| if (decimalVal.eq(null)) { | ||
| longVal / POW_10(_scale) | ||
| rawLongValue | ||
| } else { | ||
| decimalVal.longValue | ||
| } | ||
|
|
@@ -278,9 +280,8 @@ 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) { | ||
| val numericVal = f1(rawLongValue) | ||
| if (rawLongValue == numericVal) { | ||
| numericVal | ||
| } else { | ||
| throw QueryExecutionErrors.castingCauseOverflowError( | ||
|
|
@@ -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) | ||
| rawLongValue | ||
| } else { | ||
| try { | ||
| // We cannot store Long.MAX_VALUE as a Double without losing precision. | ||
|
|
@@ -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 | ||
|
|
@@ -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)) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does this have a real impact?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| def floor: Decimal = if (scale == 0) this else { | ||
| val newPrecision = DecimalType.bounded(precision - scale + 1, 0).precision | ||
|
|
@@ -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) | ||
|
|
@@ -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`. | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's still use the old name
actualLongValThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK