Skip to content

Commit 392b65b

Browse files
committed
add negative scale test in DecimalSuite
1 parent 61760ee commit 392b65b

File tree

2 files changed

+19
-8
lines changed
  • sql/catalyst/src

2 files changed

+19
-8
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/math.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,9 @@ case class Round(child: Expression, scale: Expression)
555555
override def foldable: Boolean = child.foldable
556556

557557
override lazy val dataType: DataType = child.dataType match {
558-
case DecimalType.Fixed(p, s) => DecimalType(p, _scale)
558+
// if the new scale is bigger which means we are scaling up,
559+
// keep the original scale as `Decimal` does
560+
case DecimalType.Fixed(p, s) => DecimalType(p, if (_scale > s) s else _scale)
559561
case t => t
560562
}
561563

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ import org.scalatest.PrivateMethodTester
2424
import scala.language.postfixOps
2525

2626
class DecimalSuite extends SparkFunSuite with PrivateMethodTester {
27-
test("creating decimals") {
28-
/** Check that a Decimal has the given string representation, precision and scale */
29-
def checkDecimal(d: Decimal, string: String, precision: Int, scale: Int): Unit = {
30-
assert(d.toString === string)
31-
assert(d.precision === precision)
32-
assert(d.scale === scale)
33-
}
27+
/** Check that a Decimal has the given string representation, precision and scale */
28+
private def checkDecimal(d: Decimal, string: String, precision: Int, scale: Int): Unit = {
29+
assert(d.toString === string)
30+
assert(d.precision === precision)
31+
assert(d.scale === scale)
32+
}
3433

34+
test("creating decimals") {
3535
checkDecimal(new Decimal(), "0", 1, 0)
3636
checkDecimal(Decimal(BigDecimal("10.030")), "10.030", 5, 3)
3737
checkDecimal(Decimal(BigDecimal("10.030"), 4, 1), "10.0", 4, 1)
@@ -53,6 +53,15 @@ class DecimalSuite extends SparkFunSuite with PrivateMethodTester {
5353
intercept[IllegalArgumentException](Decimal(1e17.toLong, 17, 0))
5454
}
5555

56+
test("creating decimals with negative scale") {
57+
checkDecimal(Decimal(BigDecimal("98765"), 5, -3), "9.9E+4", 5, -3)
58+
checkDecimal(Decimal(BigDecimal("314.159"), 6, -2), "3E+2", 6, -2)
59+
checkDecimal(Decimal(BigDecimal(1.579e12), 4, -9), "1.579E+12", 4, -9)
60+
checkDecimal(Decimal(BigDecimal(1.579e12), 4, -10), "1.58E+12", 4, -10)
61+
checkDecimal(Decimal(103050709L, 9, -10), "1.03050709E+18", 9, -10)
62+
checkDecimal(Decimal(1e8.toLong, 10, -10), "1.00000000E+18", 10, -10)
63+
}
64+
5665
test("double and long values") {
5766
/** Check that a Decimal converts to the given double and long values */
5867
def checkValues(d: Decimal, doubleValue: Double, longValue: Long): Unit = {

0 commit comments

Comments
 (0)