Github user rxin commented on a diff in the pull request:
https://github.com/apache/spark/pull/6835#discussion_r34226650
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/math.scala
---
@@ -248,30 +269,81 @@ case class Hypot(left: Expression, right: Expression)
case class Pow(left: Expression, right: Expression)
extends BinaryMathExpression(math.pow, "POWER") {
override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode):
String = {
- defineCodeGen(ctx, ev, (c1, c2) => s"java.lang.Math.pow($c1, $c2)") +
s"""
- if (Double.valueOf(${ev.primitive}).isNaN()) {
- ${ev.isNull} = true;
- }
- """
+ defineCodeGen(ctx, ev, (c1, c2) => s"java.lang.Math.pow($c1, $c2)")
}
}
case class Logarithm(left: Expression, right: Expression)
extends BinaryMathExpression((c1, c2) => math.log(c2) / math.log(c1),
"LOG") {
- def this(child: Expression) = {
- this(EulerNumber(), child)
- }
- override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode):
String = {
- val logCode = if (left.isInstanceOf[EulerNumber]) {
- defineCodeGen(ctx, ev, (c1, c2) => s"java.lang.Math.log($c2)")
+ override def eval(input: InternalRow): Any = {
+ val evalE1 = left.eval(input)
+ if (evalE1 == null || evalE1.asInstanceOf[Double] <= 0.0) {
+ null
} else {
- defineCodeGen(ctx, ev, (c1, c2) => s"java.lang.Math.log($c2) /
java.lang.Math.log($c1)")
+ val evalE2 = right.eval(input)
+ if (evalE2 == null || evalE2.asInstanceOf[Double] <= 0.0) {
+ null
+ } else {
+ math.log(evalE2.asInstanceOf[Double]) /
math.log(evalE1.asInstanceOf[Double])
+ }
}
- logCode + s"""
- if (Double.valueOf(${ev.primitive}).isNaN()) {
+ }
+
+ override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode):
String = {
+ val eval1 = left.gen(ctx)
+ val eval2 = right.gen(ctx)
+ s"""
+ ${eval1.code}
+ boolean ${ev.isNull} = ${eval1.isNull} || ${eval1.primitive} <= 0.0;
+ ${ctx.javaType(dataType)} ${ev.primitive} =
${ctx.defaultValue(dataType)};
+ if (${ev.isNull}) {
${ev.isNull} = true;
+ } else {
+ ${eval2.code}
+ if (${eval2.isNull} || ${eval2.primitive} <= 0.0) {
+ ${ev.isNull} = true;
+ } else {
+ ${ev.primitive} = java.lang.Math.${funcName}(${eval2.primitive})
/
+ java.lang.Math.${funcName}(${eval1.primitive});
+ }
}
"""
}
+
+ // TODO: Hive's UDFLog doesn't support base in range (0.0, 1.0]
+ // If we want just behaves like Hive, use the code below and turn
`udf_7` on
+
+// override def eval(input: InternalRow): Any = {
+// val evalE1 = left.eval(input)
+// val evalE2 = right.eval(input)
+// if (evalE1 == null || evalE2 == null) {
+// null
+// } else {
+// if (evalE1.asInstanceOf[Double] <= 1.0 ||
evalE2.asInstanceOf[Double] <= 0.0) {
+// null
+// } else {
+// math.log(evalE2.asInstanceOf[Double]) /
math.log(evalE1.asInstanceOf[Double])
+// }
+// }
+// }
--- End diff --
we should support these. just remove the commented code, and add to inline
comment for log that we support (0.0, 1.0], unlike hive.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]