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 @@ -291,10 +291,6 @@ case class Cosh(child: Expression) extends UnaryMathExpression(math.cosh, "COSH"
usage = """
_FUNC_(expr) - Returns inverse hyperbolic cosine of `expr`.
""",
arguments = """
Arguments:
* expr - hyperbolic angle

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This description is wrong; the argument is not an angle. For consistency with other inverse functions, I just removed the description.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oops. Thanks for the fix.

""",
examples = """
Examples:
> SELECT _FUNC_(1);
Expand All @@ -304,9 +300,10 @@ case class Cosh(child: Expression) extends UnaryMathExpression(math.cosh, "COSH"
""",
since = "3.0.0")
case class Acosh(child: Expression)
extends UnaryMathExpression((x: Double) => math.log(x + math.sqrt(x * x - 1.0)), "ACOSH") {
extends UnaryMathExpression((x: Double) => StrictMath.log(x + math.sqrt(x * x - 1.0)), "ACOSH") {
override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
defineCodeGen(ctx, ev, c => s"java.lang.Math.log($c + java.lang.Math.sqrt($c * $c - 1.0))")
defineCodeGen(ctx, ev,
c => s"java.lang.StrictMath.log($c + java.lang.Math.sqrt($c * $c - 1.0))")
}
}

Expand Down Expand Up @@ -486,7 +483,7 @@ case class Factorial(child: Expression) extends UnaryExpression with ImplicitCas
> SELECT _FUNC_(1);
0.0
""")
case class Log(child: Expression) extends UnaryLogExpression(math.log, "LOG")
case class Log(child: Expression) extends UnaryLogExpression(StrictMath.log, "LOG")
Comment thread
srowen marked this conversation as resolved.

@ExpressionDescription(
usage = "_FUNC_(expr) - Returns the logarithm of `expr` with base 2.",
Expand All @@ -496,14 +493,14 @@ case class Log(child: Expression) extends UnaryLogExpression(math.log, "LOG")
1.0
""")
case class Log2(child: Expression)
extends UnaryLogExpression((x: Double) => math.log(x) / math.log(2), "LOG2") {
extends UnaryLogExpression((x: Double) => StrictMath.log(x) / StrictMath.log(2), "LOG2") {
override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
nullSafeCodeGen(ctx, ev, c =>
s"""
if ($c <= $yAsymptote) {
${ev.isNull} = true;
} else {
${ev.value} = java.lang.Math.log($c) / java.lang.Math.log(2);
${ev.value} = java.lang.StrictMath.log($c) / java.lang.StrictMath.log(2);
}
"""
)
Expand All @@ -517,7 +514,7 @@ case class Log2(child: Expression)
> SELECT _FUNC_(10);
1.0
""")
case class Log10(child: Expression) extends UnaryLogExpression(math.log10, "LOG10")
case class Log10(child: Expression) extends UnaryLogExpression(StrictMath.log10, "LOG10")

@ExpressionDescription(
usage = "_FUNC_(expr) - Returns log(1 + `expr`).",
Expand All @@ -526,7 +523,7 @@ case class Log10(child: Expression) extends UnaryLogExpression(math.log10, "LOG1
> SELECT _FUNC_(0);
0.0
""")
case class Log1p(child: Expression) extends UnaryLogExpression(math.log1p, "LOG1P") {
case class Log1p(child: Expression) extends UnaryLogExpression(StrictMath.log1p, "LOG1P") {
protected override val yAsymptote: Double = -1.0
}

Expand Down Expand Up @@ -584,10 +581,6 @@ case class Sinh(child: Expression) extends UnaryMathExpression(math.sinh, "SINH"
usage = """
_FUNC_(expr) - Returns inverse hyperbolic sine of `expr`.
""",
arguments = """
Arguments:
* expr - hyperbolic angle
""",
examples = """
Examples:
> SELECT _FUNC_(0);
Expand All @@ -597,11 +590,11 @@ case class Sinh(child: Expression) extends UnaryMathExpression(math.sinh, "SINH"
case class Asinh(child: Expression)
extends UnaryMathExpression((x: Double) => x match {
case Double.NegativeInfinity => Double.NegativeInfinity
case _ => math.log(x + math.sqrt(x * x + 1.0)) }, "ASINH") {
case _ => StrictMath.log(x + math.sqrt(x * x + 1.0)) }, "ASINH") {
override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
defineCodeGen(ctx, ev, c =>
s"$c == Double.NEGATIVE_INFINITY ? Double.NEGATIVE_INFINITY : " +
s"java.lang.Math.log($c + java.lang.Math.sqrt($c * $c + 1.0))")
s"java.lang.StrictMath.log($c + java.lang.Math.sqrt($c * $c + 1.0))")
}
}

Expand Down Expand Up @@ -669,10 +662,6 @@ case class Tanh(child: Expression) extends UnaryMathExpression(math.tanh, "TANH"
usage = """
_FUNC_(expr) - Returns inverse hyperbolic tangent of `expr`.
""",
arguments = """
Arguments:
* expr - hyperbolic angle
""",
examples = """
Examples:
> SELECT _FUNC_(0);
Expand All @@ -682,9 +671,12 @@ case class Tanh(child: Expression) extends UnaryMathExpression(math.tanh, "TANH"
""",
since = "3.0.0")
case class Atanh(child: Expression)
extends UnaryMathExpression((x: Double) => 0.5 * math.log((1.0 + x) / (1.0 - x)), "ATANH") {
// SPARK-28519: more accurate express for 1/2 * ln((1 + x) / (1 - x))
extends UnaryMathExpression((x: Double) =>
0.5 * (StrictMath.log1p(x) - StrictMath.log1p(-x)), "ATANH") {
override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
defineCodeGen(ctx, ev, c => s"0.5 * java.lang.Math.log((1.0 + $c)/(1.0 - $c))")
defineCodeGen(ctx, ev,
c => s"0.5 * (java.lang.StrictMath.log1p($c) - java.lang.StrictMath.log1p(-$c))")
}
}

Expand Down Expand Up @@ -931,9 +923,9 @@ case class Atan2(left: Expression, right: Expression)
8.0
""")
case class Pow(left: Expression, right: Expression)
extends BinaryMathExpression(math.pow, "POWER") {
extends BinaryMathExpression(StrictMath.pow, "POWER") {
override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
defineCodeGen(ctx, ev, (c1, c2) => s"java.lang.Math.pow($c1, $c2)")
defineCodeGen(ctx, ev, (c1, c2) => s"java.lang.StrictMath.pow($c1, $c2)")
}
}

Expand Down Expand Up @@ -1064,7 +1056,7 @@ case class Hypot(left: Expression, right: Expression)
2.0
""")
case class Logarithm(left: Expression, right: Expression)
extends BinaryMathExpression((c1, c2) => math.log(c2) / math.log(c1), "LOG") {
extends BinaryMathExpression((c1, c2) => StrictMath.log(c2) / StrictMath.log(c1), "LOG") {

/**
* Natural log, i.e. using e as the base.
Expand All @@ -1079,7 +1071,7 @@ case class Logarithm(left: Expression, right: Expression)
val dLeft = input1.asInstanceOf[Double]
val dRight = input2.asInstanceOf[Double]
// Unlike Hive, we support Log base in (0.0, 1.0]
if (dLeft <= 0.0 || dRight <= 0.0) null else math.log(dRight) / math.log(dLeft)
if (dLeft <= 0.0 || dRight <= 0.0) null else StrictMath.log(dRight) / StrictMath.log(dLeft)
}

override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
Expand All @@ -1089,7 +1081,7 @@ case class Logarithm(left: Expression, right: Expression)
if ($c2 <= 0.0) {
${ev.isNull} = true;
} else {
${ev.value} = java.lang.Math.log($c2);
${ev.value} = java.lang.StrictMath.log($c2);
}
""")
} else {
Expand All @@ -1098,7 +1090,7 @@ case class Logarithm(left: Expression, right: Expression)
if ($c1 <= 0.0 || $c2 <= 0.0) {
${ev.isNull} = true;
} else {
${ev.value} = java.lang.Math.log($c2) / java.lang.Math.log($c1);
${ev.value} = java.lang.StrictMath.log($c2) / java.lang.StrictMath.log($c1);
}
""")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ class MathExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
}

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

checkEvaluation(Asinh(Double.NegativeInfinity), Double.NegativeInfinity)
Expand Down Expand Up @@ -228,7 +228,7 @@ class MathExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
}

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

val nullLit = Literal.create(null, NullType)
Expand Down Expand Up @@ -267,7 +267,8 @@ class MathExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
}

test("atanh") {
testUnary(Atanh, (x: Double) => 0.5 * math.log((1.0 + x) / (1.0 - x)))
// SPARK-28519: more accurate express for 1/2 * ln((1 + x) / (1 - x))
testUnary(Atanh, (x: Double) => 0.5 * (StrictMath.log1p(x) - StrictMath.log1p(-x)))
checkConsistencyBetweenInterpretedAndCodegen(Atanh, DoubleType)

val nullLit = Literal.create(null, NullType)
Expand Down Expand Up @@ -387,20 +388,20 @@ class MathExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
}

test("log") {
testUnary(Log, math.log, (1 to 20).map(_ * 0.1))
testUnary(Log, math.log, (-5 to 0).map(_ * 0.1), expectNull = true)
testUnary(Log, StrictMath.log, (1 to 20).map(_ * 0.1))
testUnary(Log, StrictMath.log, (-5 to 0).map(_ * 0.1), expectNull = true)
checkConsistencyBetweenInterpretedAndCodegen(Log, DoubleType)
}

test("log10") {
testUnary(Log10, math.log10, (1 to 20).map(_ * 0.1))
testUnary(Log10, math.log10, (-5 to 0).map(_ * 0.1), expectNull = true)
testUnary(Log10, StrictMath.log10, (1 to 20).map(_ * 0.1))
testUnary(Log10, StrictMath.log10, (-5 to 0).map(_ * 0.1), expectNull = true)
checkConsistencyBetweenInterpretedAndCodegen(Log10, DoubleType)
}

test("log1p") {
testUnary(Log1p, math.log1p, (0 to 20).map(_ * 0.1))
testUnary(Log1p, math.log1p, (-10 to -1).map(_ * 1.0), expectNull = true)
testUnary(Log1p, StrictMath.log1p, (0 to 20).map(_ * 0.1))
testUnary(Log1p, StrictMath.log1p, (-10 to -1).map(_ * 1.0), expectNull = true)
checkConsistencyBetweenInterpretedAndCodegen(Log1p, DoubleType)
}

Expand All @@ -427,7 +428,7 @@ class MathExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
}

test("log2") {
def f: (Double) => Double = (x: Double) => math.log(x) / math.log(2)
def f: (Double) => Double = (x: Double) => StrictMath.log(x) / StrictMath.log(2)
testUnary(Log2, f, (1 to 20).map(_ * 0.1))
testUnary(Log2, f, (-5 to 0).map(_ * 1.0), expectNull = true)
checkConsistencyBetweenInterpretedAndCodegen(Log2, DoubleType)
Expand All @@ -444,8 +445,8 @@ class MathExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
}

test("pow") {
testBinary(Pow, math.pow, (-5 to 5).map(v => (v * 1.0, v * 1.0)))
testBinary(Pow, math.pow, Seq((-1.0, 0.9), (-2.2, 1.7), (-2.2, -1.7)), expectNaN = true)
testBinary(Pow, StrictMath.pow, (-5 to 5).map(v => (v * 1.0, v * 1.0)))
testBinary(Pow, StrictMath.pow, Seq((-1.0, 0.9), (-2.2, 1.7), (-2.2, -1.7)), expectNaN = true)
checkConsistencyBetweenInterpretedAndCodegen(Pow, DoubleType, DoubleType)
}

Expand Down Expand Up @@ -569,7 +570,7 @@ class MathExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
}

test("binary log") {
val f = (c1: Double, c2: Double) => math.log(c2) / math.log(c1)
val f = (c1: Double, c2: Double) => StrictMath.log(c2) / StrictMath.log(c1)
val domain = (1 to 20).map(v => (v * 0.1, v * 0.2))

domain.foreach { case (v1, v2) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ SELECT atanh(double('0.5'))
-- !query 56 schema
struct<ATANH(CAST(0.5 AS DOUBLE)):double>
-- !query 56 output
0.5493061443340549
0.5493061443340548
Comment thread
srowen marked this conversation as resolved.


-- !query 57
Expand Down