Skip to content

Commit 2de9afe

Browse files
committed
Revert "[SPARK-8800] [SQL] Fix inaccurate precision/scale of Decimal division operation"
This reverts commit 4b5cfc9.
1 parent 9716a72 commit 2de9afe

File tree

2 files changed

+4
-20
lines changed

2 files changed

+4
-20
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/types/Decimal.scala

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,6 @@ final class Decimal extends Ordered[Decimal] with Serializable {
145145
}
146146
}
147147

148-
def toLimitedBigDecimal: BigDecimal = {
149-
if (decimalVal.ne(null)) {
150-
decimalVal
151-
} else {
152-
BigDecimal(longVal, _scale)
153-
}
154-
}
155-
156148
def toJavaBigDecimal: java.math.BigDecimal = toBigDecimal.underlying()
157149

158150
def toUnscaledLong: Long = {
@@ -277,9 +269,9 @@ final class Decimal extends Ordered[Decimal] with Serializable {
277269
if (that.isZero) {
278270
null
279271
} else {
280-
// To avoid non-terminating decimal expansion problem, we get scala's BigDecimal with limited
281-
// precision and scala.
282-
Decimal(toLimitedBigDecimal / that.toLimitedBigDecimal)
272+
// To avoid non-terminating decimal expansion problem, we turn to Java BigDecimal's divide
273+
// with specified ROUNDING_MODE.
274+
Decimal(toJavaBigDecimal.divide(that.toJavaBigDecimal, ROUNDING_MODE.id))
283275
}
284276
}
285277

sql/catalyst/src/test/scala/org/apache/spark/sql/types/decimal/DecimalSuite.scala

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,6 @@ class DecimalSuite extends SparkFunSuite with PrivateMethodTester {
179179

180180
test("fix non-terminating decimal expansion problem") {
181181
val decimal = Decimal(1.0, 10, 3) / Decimal(3.0, 10, 3)
182-
// The difference between decimal should not be more than 0.001.
183-
assert(decimal.toDouble - 0.333 < 0.001)
184-
}
185-
186-
test("fix loss of precision/scale when doing division operation") {
187-
val a = Decimal(2) / Decimal(3)
188-
assert(a.toDouble < 1.0 && a.toDouble > 0.6)
189-
val b = Decimal(1) / Decimal(8)
190-
assert(b.toDouble === 0.125)
182+
assert(decimal.toString === "0.333")
191183
}
192184
}

0 commit comments

Comments
 (0)