Skip to content
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ abstract class UnaryMathExpression(f: Double => Double, name: String)
}
}

// for floor and ceil which returns bigint instead of double
abstract class UnaryMathExpressionWithBigIntRet(f: Double => Double, name: String)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you consider parameterizing UnaryMathExpression with the return type as opposed to using inheritance?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion. Tried my best (I'm new to scala) but can be different with what you expected.

extends UnaryMathExpression(f, name) {
override def dataType: DataType = LongType
protected override def nullSafeEval(input: Any): Any = {
f(input.asInstanceOf[Double]).toLong
}
override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): String = {
defineCodeGen(ctx, ev, c => s"(long)java.lang.Math.${funcName}($c)")
}
}

abstract class UnaryLogExpression(f: Double => Double, name: String)
extends UnaryMathExpression(f, name) {

Expand Down Expand Up @@ -152,7 +164,7 @@ case class Atan(child: Expression) extends UnaryMathExpression(math.atan, "ATAN"

case class Cbrt(child: Expression) extends UnaryMathExpression(math.cbrt, "CBRT")

case class Ceil(child: Expression) extends UnaryMathExpression(math.ceil, "CEIL")
case class Ceil(child: Expression) extends UnaryMathExpressionWithBigIntRet(math.ceil, "CEIL")

case class Cos(child: Expression) extends UnaryMathExpression(math.cos, "COS")

Expand Down Expand Up @@ -195,7 +207,7 @@ case class Exp(child: Expression) extends UnaryMathExpression(math.exp, "EXP")

case class Expm1(child: Expression) extends UnaryMathExpression(math.expm1, "EXPM1")

case class Floor(child: Expression) extends UnaryMathExpression(math.floor, "FLOOR")
case class Floor(child: Expression) extends UnaryMathExpressionWithBigIntRet(math.floor, "FLOOR")

object Factorial {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,12 @@ class MathFunctionsSuite extends SparkFunSuite with ExpressionEvalHelper {
}

test("ceil") {
testUnary(Ceil, math.ceil)
testUnary(Ceil, (x: Double) => math.ceil(x).toLong)
checkConsistencyBetweenInterpretedAndCodegen(Ceil, DoubleType)
}

test("floor") {
testUnary(Floor, math.floor)
testUnary(Floor, (x: Double) => math.floor(x).toLong)
checkConsistencyBetweenInterpretedAndCodegen(Floor, DoubleType)
}

Expand Down