Skip to content

Commit 2758ff0

Browse files
adrian-wangrxin
authored andcommitted
[SPARK-8217] [SQL] math function log2
Author: Daoyuan Wang <[email protected]> This patch had conflicts when merged, resolved by Committer: Reynold Xin <[email protected]> Closes apache#6718 from adrian-wang/udflog2 and squashes the following commits: 3909f48 [Daoyuan Wang] math function: log2
1 parent 9fe3adc commit 2758ff0

File tree

5 files changed

+54
-2
lines changed

5 files changed

+54
-2
lines changed

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
@@ -111,6 +111,7 @@ object FunctionRegistry {
111111
expression[Log10]("log10"),
112112
expression[Log1p]("log1p"),
113113
expression[Pi]("pi"),
114+
expression[Log2]("log2"),
114115
expression[Pow]("pow"),
115116
expression[Rint]("rint"),
116117
expression[Signum]("signum"),

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,23 @@ case class Floor(child: Expression) extends UnaryMathExpression(math.floor, "FLO
161161

162162
case class Log(child: Expression) extends UnaryMathExpression(math.log, "LOG")
163163

164+
case class Log2(child: Expression)
165+
extends UnaryMathExpression((x: Double) => math.log(x) / math.log(2), "LOG2") {
166+
override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): String = {
167+
val eval = child.gen(ctx)
168+
eval.code + s"""
169+
boolean ${ev.isNull} = ${eval.isNull};
170+
${ctx.javaType(dataType)} ${ev.primitive} = ${ctx.defaultValue(dataType)};
171+
if (!${ev.isNull}) {
172+
${ev.primitive} = java.lang.Math.log(${eval.primitive}) / java.lang.Math.log(2);
173+
if (Double.valueOf(${ev.primitive}).isNaN()) {
174+
${ev.isNull} = true;
175+
}
176+
}
177+
"""
178+
}
179+
}
180+
164181
case class Log10(child: Expression) extends UnaryMathExpression(math.log10, "LOG10")
165182

166183
case class Log1p(child: Expression) extends UnaryMathExpression(math.log1p, "LOG1P")

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,12 @@ class MathFunctionsSuite extends SparkFunSuite with ExpressionEvalHelper {
185185
testUnary(Log1p, math.log1p, (-10 to -2).map(_ * 1.0), expectNull = true)
186186
}
187187

188+
test("log2") {
189+
def f: (Double) => Double = (x: Double) => math.log(x) / math.log(2)
190+
testUnary(Log2, f, (0 to 20).map(_ * 0.1))
191+
testUnary(Log2, f, (-5 to -1).map(_ * 1.0), expectNull = true)
192+
}
193+
188194
test("pow") {
189195
testBinary(Pow, math.pow, (-5 to 5).map(v => (v * 1.0, v * 1.0)))
190196
testBinary(Pow, math.pow, Seq((-1.0, 0.9), (-2.2, 1.7), (-2.2, -1.7)), expectNull = true)

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,15 +1084,15 @@ object functions {
10841084
def log(columnName: String): Column = log(Column(columnName))
10851085

10861086
/**
1087-
* Computes the logarithm of the given value in Base 10.
1087+
* Computes the logarithm of the given value in base 10.
10881088
*
10891089
* @group math_funcs
10901090
* @since 1.4.0
10911091
*/
10921092
def log10(e: Column): Column = Log10(e.expr)
10931093

10941094
/**
1095-
* Computes the logarithm of the given value in Base 10.
1095+
* Computes the logarithm of the given value in base 10.
10961096
*
10971097
* @group math_funcs
10981098
* @since 1.4.0
@@ -1124,6 +1124,22 @@ object functions {
11241124
*/
11251125
def pi(): Column = Pi()
11261126

1127+
/**
1128+
* Computes the logarithm of the given column in base 2.
1129+
*
1130+
* @group math_funcs
1131+
* @since 1.5.0
1132+
*/
1133+
def log2(expr: Column): Column = Log2(expr.expr)
1134+
1135+
/**
1136+
* Computes the logarithm of the given value in base 2.
1137+
*
1138+
* @group math_funcs
1139+
* @since 1.5.0
1140+
*/
1141+
def log2(columnName: String): Column = log2(Column(columnName))
1142+
11271143
/**
11281144
* Returns the value of the first argument raised to the power of the second argument.
11291145
*

sql/core/src/test/scala/org/apache/spark/sql/DataFrameFunctionsSuite.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,5 +128,17 @@ class DataFrameFunctionsSuite extends QueryTest {
128128
})
129129
}
130130

131+
test("log2 functions test") {
132+
val df = Seq((1, 2)).toDF("a", "b")
133+
checkAnswer(
134+
df.select(log2("b") + log2("a")),
135+
Row(1))
131136

137+
checkAnswer(
138+
ctx.sql("SELECT LOG2(8)"),
139+
Row(3))
140+
checkAnswer(
141+
ctx.sql("SELECT LOG2(null)"),
142+
Row(null))
143+
}
132144
}

0 commit comments

Comments
 (0)