Skip to content

Commit 24fda73

Browse files
viiryaDavies Liu
authored andcommitted
[SPARK-8677] [SQL] Fix non-terminating decimal expansion for decimal divide operation
JIRA: https://issues.apache.org/jira/browse/SPARK-8677 Author: Liang-Chi Hsieh <[email protected]> Closes #7056 from viirya/fix_decimal3 and squashes the following commits: 34d7419 [Liang-Chi Hsieh] Fix Non-terminating decimal expansion for decimal divide operation.
1 parent 9ce78b4 commit 24fda73

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,15 @@ final class Decimal extends Ordered[Decimal] with Serializable {
265265

266266
def * (that: Decimal): Decimal = Decimal(toBigDecimal * that.toBigDecimal)
267267

268-
def / (that: Decimal): Decimal =
269-
if (that.isZero) null else Decimal(toBigDecimal / that.toBigDecimal)
268+
def / (that: Decimal): Decimal = {
269+
if (that.isZero) {
270+
null
271+
} else {
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))
275+
}
276+
}
270277

271278
def % (that: Decimal): Decimal =
272279
if (that.isZero) null else Decimal(toBigDecimal % that.toBigDecimal)

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,9 @@ class DecimalSuite extends SparkFunSuite with PrivateMethodTester {
167167
val decimal = (Decimal(Long.MaxValue, 38, 0) * Decimal(Long.MaxValue, 38, 0)).toJavaBigDecimal
168168
assert(decimal.unscaledValue.toString === "85070591730234615847396907784232501249")
169169
}
170+
171+
test("fix non-terminating decimal expansion problem") {
172+
val decimal = Decimal(1.0, 10, 3) / Decimal(3.0, 10, 3)
173+
assert(decimal.toString === "0.333")
174+
}
170175
}

0 commit comments

Comments
 (0)