Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,15 @@ object FunctionRegistry {
expression[Cbrt]("cbrt"),
expression[Ceil]("ceil"),
expression[Cos]("cos"),
expression[EulerNumber]("e"),
expression[Exp]("exp"),
expression[Expm1]("expm1"),
expression[Floor]("floor"),
expression[Hypot]("hypot"),
expression[Log]("log"),
expression[Log10]("log10"),
expression[Log1p]("log1p"),
expression[Pi]("pi"),
expression[Pow]("pow"),
expression[Rint]("rint"),
expression[Signum]("signum"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,34 @@ package org.apache.spark.sql.catalyst.expressions
import org.apache.spark.sql.catalyst.expressions.codegen._
import org.apache.spark.sql.types.{DataType, DoubleType}

/**
* A leaf expression specifically for math constants. Math constants expect no input.
* @param c The math constant.
* @param name The short name of the function
*/
abstract class LeafMathExpression(c: Double, name: String)
extends LeafExpression with Serializable {
self: Product =>

override def dataType: DataType = DoubleType
override def foldable: Boolean = true
override def nullable: Boolean = false
override def toString: String = s"$name()"

override def eval(input: Row): Any = c

override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): String = {
s"""
boolean ${ev.isNull} = false;
${ctx.javaType(dataType)} ${ev.primitive} = java.lang.Math.$name;
"""
}
}

/**
* A unary expression specifically for math functions. Math Functions expect a specific type of
* input format, therefore these functions extend `ExpectsInputTypes`.
* @param f The math function.
* @param name The short name of the function
*/
abstract class UnaryMathExpression(f: Double => Double, name: String)
Expand Down Expand Up @@ -98,6 +123,16 @@ abstract class BinaryMathExpression(f: (Double, Double) => Double, name: String)
}
}

////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
// Leaf math functions
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////

case class EulerNumber() extends LeafMathExpression(math.E, "E")

case class Pi() extends LeafMathExpression(math.Pi, "PI")

////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
// Unary math functions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ import org.apache.spark.sql.types.DoubleType

class MathFunctionsSuite extends SparkFunSuite with ExpressionEvalHelper {

/**
* Used for testing leaf math expressions.
*
* @param e expression
* @param c The constants in scala.math
* @tparam T Generic type for primitives
*/
private def testLeaf[T](
e: () => Expression,
c: T): Unit = {
checkEvaluation(e(), c, EmptyRow)
checkEvaluation(e(), c, create_row(null))
}

/**
* Used for testing unary math expressions.
*
Expand Down Expand Up @@ -74,6 +88,14 @@ class MathFunctionsSuite extends SparkFunSuite with ExpressionEvalHelper {
checkEvaluation(c(Literal(1.0), Literal.create(null, DoubleType)), null, create_row(null))
}

test("e") {
testLeaf(EulerNumber, math.E)
}

test("pi") {
testLeaf(Pi, math.Pi)
}

test("sin") {
testUnary(Sin, math.sin)
}
Expand Down
18 changes: 18 additions & 0 deletions sql/core/src/main/scala/org/apache/spark/sql/functions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,15 @@ object functions {
*/
def cosh(columnName: String): Column = cosh(Column(columnName))

/**
* Returns the double value that is closer than any other to e, the base of the natural
* logarithms.
*
* @group math_funcs
* @since 1.5.0
*/
def e(): Column = EulerNumber()

/**
* Computes the exponential of the given value.
*
Expand Down Expand Up @@ -1105,6 +1114,15 @@ object functions {
*/
def log1p(columnName: String): Column = log1p(Column(columnName))

/**
* Returns the double value that is closer than any other to pi, the ratio of the circumference
* of a circle to its diameter.
*
* @group math_funcs
* @since 1.5.0
*/
def pi(): Column = Pi()

/**
* Returns the value of the first argument raised to the power of the second argument.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,25 @@ class DataFrameFunctionsSuite extends QueryTest {
}
}

test("constant functions") {
checkAnswer(
testData2.select(e()).limit(1),
Row(scala.math.E)
)
checkAnswer(
testData2.select(pi()).limit(1),
Row(scala.math.Pi)
)
checkAnswer(
ctx.sql("SELECT E()"),
Row(scala.math.E)
)
checkAnswer(
ctx.sql("SELECT PI()"),
Row(scala.math.Pi)
)
}

test("bitwiseNOT") {
checkAnswer(
testData2.select(bitwiseNOT($"a")),
Expand Down