Skip to content

Commit bb15eef

Browse files
author
Davies Liu
committed
fix bug
1 parent cf0e445 commit bb15eef

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,12 @@ final class Decimal extends Ordered[Decimal] with Serializable {
257257
}
258258
case ROUND_HALF_UP =>
259259
if (math.abs(droppedDigits) * 2 >= pow10diff) {
260-
longVal += (if (longVal < 0) -1L else 1L)
260+
longVal += (if (droppedDigits < 0) -1L else 1L)
261261
}
262262
case ROUND_HALF_EVEN =>
263263
val doubled = math.abs(droppedDigits) * 2
264264
if (doubled > pow10diff || doubled == pow10diff && longVal % 2 != 0) {
265-
longVal += (if (longVal < 0) -1L else 1L)
265+
longVal += (if (droppedDigits < 0) -1L else 1L)
266266
}
267267
case _ =>
268268
sys.error(s"Not supported rounding mode: $roundMode")

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,14 @@ class DecimalSuite extends SparkFunSuite with PrivateMethodTester {
195195

196196
test("changePrecision() on compact decimal should respect rounding mode") {
197197
Seq(ROUND_FLOOR, ROUND_CEILING, ROUND_HALF_UP, ROUND_HALF_EVEN).foreach { mode =>
198-
Seq("1.0", "1.1", "1.6", "2.5", "5.5", "-1.0", "-1.1", "-1.6", "-2.5", "-5.5").foreach { n =>
199-
val bd = BigDecimal(n)
200-
val unscaled = (bd * 10).toLongExact
201-
val d = Decimal(unscaled, 8, 1)
202-
assert(d.changePrecision(10, 0, mode))
203-
assert(d.toString === bd.setScale(0, mode).toString(), s"num: $n, mode: $mode")
198+
Seq("0.4", "0.5", "0.6", "1.0", "1.1", "1.6", "2.5", "5.5").foreach { n =>
199+
Seq("", "-").foreach { sigh =>
200+
val bd = BigDecimal(sigh + n)
201+
val unscaled = (bd * 10).toLongExact
202+
val d = Decimal(unscaled, 8, 1)
203+
assert(d.changePrecision(10, 0, mode))
204+
assert(d.toString === bd.setScale(0, mode).toString(), s"num: $sigh$n, mode: $mode")
205+
}
204206
}
205207
}
206208
}

0 commit comments

Comments
 (0)