@@ -267,54 +267,16 @@ case class Pow(left: Expression, right: Expression)
267267}
268268
269269case 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}
0 commit comments