Skip to content

[SPARK-20876][SQL][Backport-2.2]If the input parameter is float type for ceil or floor,the result is not we expected #18155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -232,18 +232,20 @@ case class Ceil(child: Expression) extends UnaryMathExpression(math.ceil, "CEIL"
}

override def inputTypes: Seq[AbstractDataType] =
Seq(TypeCollection(DoubleType, DecimalType))
Seq(TypeCollection(DoubleType, DecimalType, LongType))

protected override def nullSafeEval(input: Any): Any = child.dataType match {
case LongType => input.asInstanceOf[Long]
case DoubleType => f(input.asInstanceOf[Double]).toLong
case DecimalType.Fixed(precision, scale) => input.asInstanceOf[Decimal].ceil
case DecimalType.Fixed(_, _) => input.asInstanceOf[Decimal].ceil
}

override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
child.dataType match {
case DecimalType.Fixed(_, 0) => defineCodeGen(ctx, ev, c => s"$c")
case DecimalType.Fixed(precision, scale) =>
case DecimalType.Fixed(_, _) =>
defineCodeGen(ctx, ev, c => s"$c.ceil()")
case LongType => defineCodeGen(ctx, ev, c => s"$c")
case _ => defineCodeGen(ctx, ev, c => s"(long)(java.lang.Math.${funcName}($c))")
}
}
Expand Down Expand Up @@ -347,18 +349,20 @@ case class Floor(child: Expression) extends UnaryMathExpression(math.floor, "FLO
}

override def inputTypes: Seq[AbstractDataType] =
Seq(TypeCollection(DoubleType, DecimalType))
Seq(TypeCollection(DoubleType, DecimalType, LongType))

protected override def nullSafeEval(input: Any): Any = child.dataType match {
case LongType => input.asInstanceOf[Long]
case DoubleType => f(input.asInstanceOf[Double]).toLong
case DecimalType.Fixed(precision, scale) => input.asInstanceOf[Decimal].floor
case DecimalType.Fixed(_, _) => input.asInstanceOf[Decimal].floor
}

override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
child.dataType match {
case DecimalType.Fixed(_, 0) => defineCodeGen(ctx, ev, c => s"$c")
case DecimalType.Fixed(precision, scale) =>
case DecimalType.Fixed(_, _) =>
defineCodeGen(ctx, ev, c => s"$c.floor()")
case LongType => defineCodeGen(ctx, ev, c => s"$c")
case _ => defineCodeGen(ctx, ev, c => s"(long)(java.lang.Math.${funcName}($c))")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,16 @@ class MathExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
checkConsistencyBetweenInterpretedAndCodegen(Ceil, DecimalType(25, 3))
checkConsistencyBetweenInterpretedAndCodegen(Ceil, DecimalType(25, 0))
checkConsistencyBetweenInterpretedAndCodegen(Ceil, DecimalType(5, 0))

val doublePi: Double = 3.1415
val floatPi: Float = 3.1415f
val longLit: Long = 12345678901234567L
checkEvaluation(Ceil(doublePi), 4L, EmptyRow)
checkEvaluation(Ceil(floatPi.toDouble), 4L, EmptyRow)
checkEvaluation(Ceil(longLit), longLit, EmptyRow)
checkEvaluation(Ceil(-doublePi), -3L, EmptyRow)
checkEvaluation(Ceil(-floatPi.toDouble), -3L, EmptyRow)
checkEvaluation(Ceil(-longLit), -longLit, EmptyRow)
}

test("floor") {
Expand All @@ -262,6 +272,16 @@ class MathExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
checkConsistencyBetweenInterpretedAndCodegen(Floor, DecimalType(25, 3))
checkConsistencyBetweenInterpretedAndCodegen(Floor, DecimalType(25, 0))
checkConsistencyBetweenInterpretedAndCodegen(Floor, DecimalType(5, 0))

val doublePi: Double = 3.1415
val floatPi: Float = 3.1415f
val longLit: Long = 12345678901234567L
checkEvaluation(Floor(doublePi), 3L, EmptyRow)
checkEvaluation(Floor(floatPi.toDouble), 3L, EmptyRow)
checkEvaluation(Floor(longLit), longLit, EmptyRow)
checkEvaluation(Floor(-doublePi), -4L, EmptyRow)
checkEvaluation(Floor(-floatPi.toDouble), -4L, EmptyRow)
checkEvaluation(Floor(-longLit), -longLit, EmptyRow)
}

test("factorial") {
Expand Down