Skip to content

Commit f373bac

Browse files
committed
Add binary log expression.
1 parent 490d5a7 commit f373bac

File tree

5 files changed

+82
-1
lines changed

5 files changed

+82
-1
lines changed

python/pyspark/sql/functions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ def _():
143143
'atan2': 'Returns the angle theta from the conversion of rectangular coordinates (x, y) to' +
144144
'polar coordinates (r, theta).',
145145
'hypot': 'Computes `sqrt(a^2^ + b^2^)` without intermediate overflow or underflow.',
146-
'pow': 'Returns the value of the first argument raised to the power of the second argument.'
146+
'pow': 'Returns the value of the first argument raised to the power of the second argument.',
147+
'logarithm': 'Returns the first argument-based logarithm of the second argument',
147148
}
148149

149150
_window_functions = {

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/FunctionRegistry.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ object FunctionRegistry {
122122
expression[Tanh]("tanh"),
123123
expression[ToDegrees]("todegrees"),
124124
expression[ToRadians]("toradians"),
125+
expression[Logarithm]("logarithm"),
125126

126127
// aggregate functions
127128
expression[Average]("avg"),

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,14 @@ case class Pow(left: Expression, right: Expression)
202202
"""
203203
}
204204
}
205+
206+
case class Logarithm(left: Expression, right: Expression)
207+
extends BinaryMathExpression((c1, c2) => math.log(c2) / math.log(c1), "LOG") {
208+
override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): String = {
209+
defineCodeGen(ctx, ev, (c1, c2) => s"java.lang.Math.log($c2) / java.lang.Math.log($c1)") + s"""
210+
if (Double.valueOf(${ev.primitive}).isNaN()) {
211+
${ev.isNull} = true;
212+
}
213+
"""
214+
}
215+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,8 @@ class MathFunctionsSuite extends SparkFunSuite with ExpressionEvalHelper {
176176
testBinary(Atan2, math.atan2)
177177
}
178178

179+
test("binary log") {
180+
testBinary(Logarithm, (c1, c2) => math.log(c2) / math.log(c1),
181+
(1 to 20).map(v => (v * 0.1, v * 0.2)))
182+
}
179183
}

sql/core/src/main/scala/org/apache/spark/sql/functions.scala

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,6 +1299,70 @@ object functions {
12991299
*/
13001300
def toRadians(columnName: String): Column = toRadians(Column(columnName))
13011301

1302+
/**
1303+
* Returns the first argument-base logarithm of the second argument.
1304+
*
1305+
* @group math_funcs
1306+
* @since 1.4.0
1307+
*/
1308+
def logarithm(l: Column, r: Column): Column = Logarithm(l.expr, r.expr)
1309+
1310+
/**
1311+
* Returns the first argument-base logarithm of the second argument.
1312+
*
1313+
* @group math_funcs
1314+
* @since 1.4.0
1315+
*/
1316+
def logarithm(l: Column, rightName: String): Column = logarithm(l, Column(rightName))
1317+
1318+
/**
1319+
* Returns the first argument-base logarithm of the second argument.
1320+
*
1321+
* @group math_funcs
1322+
* @since 1.4.0
1323+
*/
1324+
def logarithm(leftName: String, r: Column): Column = logarithm(Column(leftName), r)
1325+
1326+
/**
1327+
* Returns the first argument-base logarithm of the second argument.
1328+
*
1329+
* @group math_funcs
1330+
* @since 1.4.0
1331+
*/
1332+
def logarithm(leftName: String, rightName: String): Column =
1333+
logarithm(Column(leftName), Column(rightName))
1334+
1335+
/**
1336+
* Returns the first argument-base logarithm of the second argument.
1337+
*
1338+
* @group math_funcs
1339+
* @since 1.4.0
1340+
*/
1341+
def logarithm(l: Column, r: Double): Column = logarithm(l, lit(r).expr)
1342+
1343+
/**
1344+
* Returns the first argument-base logarithm of the second argument.
1345+
*
1346+
* @group math_funcs
1347+
* @since 1.4.0
1348+
*/
1349+
def logarithm(leftName: String, r: Double): Column = logarithm(Column(leftName), r)
1350+
1351+
/**
1352+
* Returns the first argument-base logarithm of the second argument.
1353+
*
1354+
* @group math_funcs
1355+
* @since 1.4.0
1356+
*/
1357+
def logarithm(l: Double, r: Column): Column = logarithm(lit(l).expr, r)
1358+
1359+
/**
1360+
* Returns the first argument-base logarithm of the second argument.
1361+
*
1362+
* @group math_funcs
1363+
* @since 1.4.0
1364+
*/
1365+
def logarithm(l: Double, rightName: String): Column = logarithm(l, Column(rightName))
13021366

13031367
//////////////////////////////////////////////////////////////////////////////////////////////
13041368
//////////////////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)