Skip to content

Commit 7c83e13

Browse files
committed
more tests on round
1 parent 56db4bb commit 7c83e13

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/MathFunctionsSuite.scala

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package org.apache.spark.sql.catalyst.expressions
1919

20+
import scala.math.BigDecimal.RoundingMode
21+
2022
import com.google.common.math.LongMath
2123

2224
import org.apache.spark.SparkFunSuite
@@ -338,14 +340,31 @@ class MathFunctionsSuite extends SparkFunSuite with ExpressionEvalHelper {
338340
}
339341

340342
test("round test") {
341-
val piRounds = Seq(
342-
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 3.0,
343-
3.1, 3.14, 3.142, 3.1416, 3.14159, 3.141593, 3.1415927, 3.14159265, 3.141592654,
344-
3.1415926536, 3.14159265359, 3.14159265359, 3.1415926535898, 3.14159265358979,
345-
3.141592653589793, 3.141592653589793)
346-
(-16 to 16).zipWithIndex.foreach {
347-
case (scale, i) =>
348-
checkEvaluation(Round(Seq(3.141592653589793, scale)), piRounds(i), EmptyRow)
343+
val domain = -16 to 16
344+
val doublePi = math.Pi
345+
val stringPi = "3.141592653589793"
346+
val intPi = 314159265
347+
val bdPi = BigDecimal(31415926535897932L, 10)
348+
349+
domain.foreach { scale =>
350+
checkEvaluation(Round(Seq(doublePi, scale)),
351+
BigDecimal.valueOf(doublePi).setScale(scale, RoundingMode.HALF_UP).toDouble, EmptyRow)
352+
checkEvaluation(Round(Seq(stringPi, scale)),
353+
BigDecimal.valueOf(doublePi).setScale(scale, RoundingMode.HALF_UP).toDouble, EmptyRow)
354+
checkEvaluation(Round(Seq(intPi, scale)),
355+
BigDecimal.valueOf(intPi).setScale(scale, RoundingMode.HALF_UP).toInt, EmptyRow)
356+
}
357+
checkEvaluation(Round(Seq("invalid input")), null, EmptyRow)
358+
359+
// round_scale > current_scale would result in precision increase
360+
// and not allowed by o.a.s.s.types.Decimal.changePrecision, therefore null
361+
val (validScales, nullScales) = domain.splitAt(27)
362+
validScales.foreach { scale =>
363+
checkEvaluation(Round(Seq(bdPi, scale)),
364+
Decimal(bdPi.setScale(scale, RoundingMode.HALF_UP)), EmptyRow)
365+
}
366+
nullScales.foreach { scale =>
367+
checkEvaluation(Round(Seq(bdPi, scale)), null, EmptyRow)
349368
}
350369
}
351370
}

0 commit comments

Comments
 (0)