Skip to content

Commit fd01863

Browse files
committed
For comments.
1 parent beed631 commit fd01863

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

python/pyspark/sql/functions.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"""
1919
A collections of builtin functions
2020
"""
21+
import math
2122
import sys
2223

2324
if sys.version < "3":
@@ -144,7 +145,6 @@ def _():
144145
'polar coordinates (r, theta).',
145146
'hypot': 'Computes `sqrt(a^2^ + b^2^)` without intermediate overflow or underflow.',
146147
'pow': 'Returns the value of the first argument raised to the power of the second argument.',
147-
'log': 'Returns the first argument-based logarithm of the second argument',
148148
}
149149

150150
_window_functions = {
@@ -404,6 +404,21 @@ def when(condition, value):
404404
return Column(jc)
405405

406406

407+
@since(1.4)
408+
def log(col, base=math.e):
409+
"""Returns the first argument-based logarithm of the second argument.
410+
411+
>>> df.select(log(df.age, 10.0).alias('ten')).collect()
412+
[Row(ten=0.30102999566398114), Row(ten=0.6989700043360187)]
413+
414+
>>> df.select(log(df.age).alias('e')).collect()
415+
[Row(e=0.6931471805597018), Row(e=1.609437912433535)]
416+
"""
417+
sc = SparkContext._active_spark_context
418+
jc = sc._jvm.functions.log(base, _to_java_column(col))
419+
return Column(jc)
420+
421+
407422
@since(1.4)
408423
def lag(col, count=1, default=None):
409424
"""

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,15 @@ case class Logarithm(left: Expression, right: Expression)
263263
}
264264

265265
override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): String = {
266-
defineCodeGen(ctx, ev, (c1, c2) => s"java.lang.Math.log($c2) / java.lang.Math.log($c1)") + s"""
266+
val logCode = if (left.isInstanceOf[EulerNumber]) {
267+
defineCodeGen(ctx, ev, (c1, c2) => s"java.lang.Math.log($c2)")
268+
} else {
269+
defineCodeGen(ctx, ev, (c1, c2) => s"java.lang.Math.log($c2) / java.lang.Math.log($c1)")
270+
}
271+
logCode + s"""
267272
if (Double.valueOf(${ev.primitive}).isNaN()) {
268273
${ev.isNull} = true;
269274
}
270-
"""
275+
"""
271276
}
272277
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ class MathFunctionsSuite extends SparkFunSuite with ExpressionEvalHelper {
211211
domain.foreach { case (v1, v2) =>
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)
214+
checkEvaluation(new Logarithm(Literal(v1)), f(math.E, v1 + 0.0), EmptyRow)
214215
}
215216
checkEvaluation(
216217
Logarithm(Literal.create(null, DoubleType), Literal(1.0)),

0 commit comments

Comments
 (0)