|
17 | 17 |
|
18 | 18 | package org.apache.spark.sql.catalyst.expressions |
19 | 19 |
|
| 20 | +import scala.math.BigDecimal.RoundingMode |
| 21 | + |
20 | 22 | import com.google.common.math.LongMath |
21 | 23 |
|
22 | 24 | import org.apache.spark.SparkFunSuite |
@@ -338,14 +340,31 @@ class MathFunctionsSuite extends SparkFunSuite with ExpressionEvalHelper { |
338 | 340 | } |
339 | 341 |
|
340 | 342 | 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) |
349 | 368 | } |
350 | 369 | } |
351 | 370 | } |
0 commit comments