Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
more tests on round
  • Loading branch information
yjshen committed Jul 14, 2015
commit 7c83e13bd772e1dee6e94bf1b0456995264110cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

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

import scala.math.BigDecimal.RoundingMode

import com.google.common.math.LongMath

import org.apache.spark.SparkFunSuite
Expand Down Expand Up @@ -338,14 +340,31 @@ class MathFunctionsSuite extends SparkFunSuite with ExpressionEvalHelper {
}

test("round test") {
val piRounds = Seq(
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,
3.1, 3.14, 3.142, 3.1416, 3.14159, 3.141593, 3.1415927, 3.14159265, 3.141592654,
3.1415926536, 3.14159265359, 3.14159265359, 3.1415926535898, 3.14159265358979,
3.141592653589793, 3.141592653589793)
(-16 to 16).zipWithIndex.foreach {
case (scale, i) =>
checkEvaluation(Round(Seq(3.141592653589793, scale)), piRounds(i), EmptyRow)
val domain = -16 to 16
val doublePi = math.Pi
val stringPi = "3.141592653589793"
val intPi = 314159265
val bdPi = BigDecimal(31415926535897932L, 10)

domain.foreach { scale =>
checkEvaluation(Round(Seq(doublePi, scale)),
BigDecimal.valueOf(doublePi).setScale(scale, RoundingMode.HALF_UP).toDouble, EmptyRow)
checkEvaluation(Round(Seq(stringPi, scale)),
BigDecimal.valueOf(doublePi).setScale(scale, RoundingMode.HALF_UP).toDouble, EmptyRow)
checkEvaluation(Round(Seq(intPi, scale)),
BigDecimal.valueOf(intPi).setScale(scale, RoundingMode.HALF_UP).toInt, EmptyRow)
}
checkEvaluation(Round(Seq("invalid input")), null, EmptyRow)

// round_scale > current_scale would result in precision increase
// and not allowed by o.a.s.s.types.Decimal.changePrecision, therefore null
val (validScales, nullScales) = domain.splitAt(27)
validScales.foreach { scale =>
checkEvaluation(Round(Seq(bdPi, scale)),
Decimal(bdPi.setScale(scale, RoundingMode.HALF_UP)), EmptyRow)
}
nullScales.foreach { scale =>
checkEvaluation(Round(Seq(bdPi, scale)), null, EmptyRow)
}
}
}