Skip to content

Commit bc89597

Browse files
committed
For comments.
1 parent db7dc38 commit bc89597

File tree

2 files changed

+5
-44
lines changed

2 files changed

+5
-44
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/math.scala

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -267,54 +267,16 @@ case class Pow(left: Expression, right: Expression)
267267
}
268268

269269
case class Logarithm(left: Expression, right: Expression)
270-
extends AbstractBinaryMathExpression[Double, Double, Double]("LOG") {
270+
extends BinaryMathExpression((c1, c2) => math.log(c2) / math.log(c1), "LOG") {
271271
def this(child: Expression) = {
272-
this(Literal(math.E), child)
273-
}
274-
275-
override def expectedChildTypes: Seq[DataType] = Seq(DoubleType, DoubleType)
276-
override def dataType: DataType = DoubleType
277-
278-
def base: Expression = left
279-
def value: Expression = right
280-
281-
override def eval(input: InternalRow): Any = {
282-
val evalE2 = value.eval(input)
283-
if (evalE2 == null) {
284-
null
285-
} else {
286-
val evalE1 = base.eval(input)
287-
var result: Double = 0.0
288-
if (evalE1 == null) {
289-
result = math.log(evalE2.asInstanceOf[Double]) / math.log(10.0)
290-
} else {
291-
result = math.log(evalE2.asInstanceOf[Double]) / math.log(evalE1.asInstanceOf[Double])
292-
}
293-
if (result.isNaN) null else result
294-
}
272+
this(EulerNumber(), child)
295273
}
296274

297275
override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): String = {
298-
val eval1 = base.gen(ctx)
299-
val eval2 = value.gen(ctx)
300-
val resultCode =
301-
s"java.lang.Math.log(${eval2.primitive}) / java.lang.Math.log(${eval1.primitive})"
302-
303-
s"""
304-
${eval2.code}
305-
boolean ${ev.isNull} = ${eval2.isNull};
306-
${ctx.javaType(dataType)} ${ev.primitive} = ${ctx.defaultValue(dataType)};
307-
if (!${ev.isNull}) {
308-
${eval1.code}
309-
if (!${eval1.isNull}) {
310-
${ev.primitive} = ${resultCode};
311-
} else {
312-
${ev.primitive} = java.lang.Math.log(${eval2.primitive}) / java.lang.Math.log(10.0);
313-
}
314-
}
276+
defineCodeGen(ctx, ev, (c1, c2) => s"java.lang.Math.log($c2) / java.lang.Math.log($c1)") + s"""
315277
if (Double.valueOf(${ev.primitive}).isNaN()) {
316278
${ev.isNull} = true;
317279
}
318-
"""
280+
"""
319281
}
320282
}

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/MathFunctionsSuite.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,9 @@ class MathFunctionsSuite extends SparkFunSuite with ExpressionEvalHelper {
212212
checkEvaluation(Logarithm(Literal(v1), Literal(v2)), f(v1 + 0.0, v2 + 0.0), EmptyRow)
213213
checkEvaluation(Logarithm(Literal(v2), Literal(v1)), f(v2 + 0.0, v1 + 0.0), EmptyRow)
214214
}
215-
// When base is null, Logarithm is 10-based log.
216215
checkEvaluation(
217216
Logarithm(Literal.create(null, DoubleType), Literal(1.0)),
218-
math.log(1.0) / math.log(10.0),
217+
null,
219218
create_row(null))
220219
checkEvaluation(
221220
Logarithm(Literal(1.0), Literal.create(null, DoubleType)),

0 commit comments

Comments
 (0)