Skip to content
Closed
Show file tree
Hide file tree
Changes from 8 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 @@ -222,9 +222,12 @@ object FunctionRegistry {

// math functions
expression[Acos]("acos"),
expression[Acosh]("acosh"),
expression[Asin]("asin"),
expression[Asinh]("asinh"),
expression[Atan]("atan"),
expression[Atan2]("atan2"),
expression[Atanh]("atanh"),
expression[Bin]("bin"),
expression[BRound]("bround"),
expression[Cbrt]("cbrt"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,30 @@ case class Cos(child: Expression) extends UnaryMathExpression(math.cos, "COS")
""")
case class Cosh(child: Expression) extends UnaryMathExpression(math.cosh, "COSH")

@ExpressionDescription(
usage = """
_FUNC_(expr) - Returns inverse hyperbolic cosine of `expr`.
""",
arguments = """
Arguments:
* expr - hyperbolic angle
""",
examples = """
Examples:
> SELECT _FUNC_(1);
0.0
> SELECT _FUNC_(0);
NaN
""",
since = "3.0.0")
case class Acosh(child: Expression)
extends UnaryMathExpression((x: Double) => math.log(x + math.sqrt(x * x - 1.0)), "ACOSH") {
override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
defineCodeGen(ctx, ev, c =>
s"${ev.value} = java.lang.Math.log($c + java.lang.Math.sqrt($c * $c - 1.0));")
}
}

/**
* Convert a num from one base to another
*
Expand Down Expand Up @@ -557,6 +581,28 @@ case class Sin(child: Expression) extends UnaryMathExpression(math.sin, "SIN")
""")
case class Sinh(child: Expression) extends UnaryMathExpression(math.sinh, "SINH")

@ExpressionDescription(
usage = """
_FUNC_(expr) - Returns inverse hyperbolic sine of `expr`.
""",
arguments = """
Arguments:
* expr - hyperbolic angle
""",
examples = """
Examples:
> SELECT _FUNC_(0);
0.0
""",
since = "3.0.0")
case class Asinh(child: Expression)
extends UnaryMathExpression((x: Double) => math.log(x + math.sqrt(x * x + 1.0)), "ASINH") {
override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
defineCodeGen(ctx, ev, c =>
Copy link
Contributor

Choose a reason for hiding this comment

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

as I mentioned earlier, I would prefer rewriting this as an if. There may be stage cases where $c contains a cast or something similar which may cause issues (I has similar problem in another PR, Janino is not perfect with this syntax in the version we're using). So it think it would be safer to rewrite as an if

Copy link
Member

Choose a reason for hiding this comment

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

I also agreed to that. Could you give us the pointer to that PR(or some example)? Actually, I tried to reproduce that kind of issue within the scope of this PR. Until now, I didn't succeed. Maybe, we had better a test case for that to make it sure.

Copy link
Contributor

Choose a reason for hiding this comment

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

The PR was this one: #24636. The bug opened there for janino cannot happen here, but I remember I also had issues in that PR in a case when there was a cast added to the terms.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks, @mgaido91 !

s"${ev.value} = java.lang.Math.log($c + java.lang.Math.sqrt($c * $c + 1.0));")
}
}

@ExpressionDescription(
usage = "_FUNC_(expr) - Returns the square root of `expr`.",
examples = """
Expand Down Expand Up @@ -617,6 +663,30 @@ case class Cot(child: Expression)
""")
case class Tanh(child: Expression) extends UnaryMathExpression(math.tanh, "TANH")

@ExpressionDescription(
usage = """
_FUNC_(expr) - Returns inverse hyperbolic tangent of `expr`.
""",
arguments = """
Arguments:
* expr - hyperbolic angle
""",
examples = """
Examples:
> SELECT _FUNC_(0);
0.0
> SELECT _FUNC_(2);
NaN
""",
since = "3.0.0")
case class Atanh(child: Expression)
extends UnaryMathExpression((x: Double) => 0.5 * math.log((1.0 + x) / (1.0 - x)), "ATANH") {
override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
defineCodeGen(ctx, ev, c =>
s"${ev.value} = 0.5 * java.lang.Math.log((1.0 + $c)/(1.0 - $c));")
}
}

@ExpressionDescription(
usage = "_FUNC_(expr) - Converts radians to degrees.",
arguments = """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ class MathExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
checkConsistencyBetweenInterpretedAndCodegen(Sinh, DoubleType)
}

test("asinh") {
testUnary(Asinh, (x: Double) => math.log(x + math.sqrt(x * x + 1.0)))
checkConsistencyBetweenInterpretedAndCodegen(Asinh, DoubleType)
}

test("cos") {
testUnary(Cos, math.cos)
checkConsistencyBetweenInterpretedAndCodegen(Cos, DoubleType)
Expand All @@ -215,6 +220,11 @@ class MathExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
checkConsistencyBetweenInterpretedAndCodegen(Cosh, DoubleType)
}

test("acosh") {
testUnary(Acosh, (x: Double) => math.log(x + math.sqrt(x * x - 1.0)))
checkConsistencyBetweenInterpretedAndCodegen(Cosh, DoubleType)
}

test("tan") {
testUnary(Tan, math.tan)
checkConsistencyBetweenInterpretedAndCodegen(Tan, DoubleType)
Expand Down Expand Up @@ -244,6 +254,11 @@ class MathExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
checkConsistencyBetweenInterpretedAndCodegen(Tanh, DoubleType)
}

test("atanh") {
testUnary(Atanh, (x: Double) => 0.5 * math.log((1.0 + x) / (1.0 - x)))
checkConsistencyBetweenInterpretedAndCodegen(Atanh, DoubleType)
}

test("toDegrees") {
testUnary(ToDegrees, math.toDegrees)
checkConsistencyBetweenInterpretedAndCodegen(ToDegrees, DoubleType)
Expand Down