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 @@ -307,7 +307,6 @@ trait HiveTypeCoercion {

case Sum(e @ StringType()) => Sum(Cast(e, DoubleType))
case Average(e @ StringType()) => Average(Cast(e, DoubleType))
case Sqrt(e @ StringType()) => Sqrt(Cast(e, DoubleType))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,38 +67,6 @@ case class UnaryPositive(child: Expression) extends UnaryArithmetic {
protected override def evalInternal(evalE: Any) = evalE
}

case class Sqrt(child: Expression) extends UnaryArithmetic {
override def dataType: DataType = DoubleType
override def nullable: Boolean = true
override def toString: String = s"SQRT($child)"

override def checkInputDataTypes(): TypeCheckResult =
TypeUtils.checkForNumericExpr(child.dataType, "function sqrt")

private lazy val numeric = TypeUtils.getNumeric(child.dataType)

protected override def evalInternal(evalE: Any) = {
val value = numeric.toDouble(evalE)
if (value < 0) null
else math.sqrt(value)
}

override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): String = {
val eval = child.gen(ctx)
eval.code + s"""
boolean ${ev.isNull} = ${eval.isNull};
${ctx.javaType(dataType)} ${ev.primitive} = ${ctx.defaultValue(dataType)};
if (!${ev.isNull}) {
if (${eval.primitive} < 0.0) {
${ev.isNull} = true;
} else {
${ev.primitive} = java.lang.Math.sqrt(${eval.primitive});
}
}
"""
}
}

/**
* A function that get the absolute value of the numeric value.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ case class Sin(child: Expression) extends UnaryMathExpression(math.sin, "SIN")

case class Sinh(child: Expression) extends UnaryMathExpression(math.sinh, "SINH")

case class Sqrt(child: Expression) extends UnaryMathExpression(math.sqrt, "SQRT")

case class Tan(child: Expression) extends UnaryMathExpression(math.tan, "TAN")

case class Tanh(child: Expression) extends UnaryMathExpression(math.tanh, "TANH")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,4 @@ class ArithmeticExpressionSuite extends SparkFunSuite with ExpressionEvalHelper
checkEvaluation(MinOf(Literal.create(null, IntegerType), 1), 1)
checkEvaluation(MinOf(1, Literal.create(null, IntegerType)), 1)
}

test("SQRT") {
val inputSequence = (1 to (1<<24) by 511).map(_ * (1L<<24))
val expectedResults = inputSequence.map(l => math.sqrt(l.toDouble))
val rowSequence = inputSequence.map(l => create_row(l.toDouble))
val d = 'a.double.at(0)

for ((row, expected) <- rowSequence zip expectedResults) {
checkEvaluation(Sqrt(d), expected, row)
}

checkEvaluation(Sqrt(Literal.create(null, DoubleType)), null, create_row(null))
checkEvaluation(Sqrt(-1), null, EmptyRow)
checkEvaluation(Sqrt(-1.5), null, EmptyRow)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ class ExpressionTypeCheckingSuite extends SparkFunSuite {

test("check types for unary arithmetic") {
assertError(UnaryMinus('stringField), "operator - accepts numeric type")
assertSuccess(Sqrt('stringField)) // We will cast String to Double for sqrt
assertError(Sqrt('booleanField), "function sqrt accepts numeric type")
assertError(Abs('stringField), "function abs accepts numeric type")
assertError(BitwiseNot('stringField), "operator ~ accepts integral type")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.spark.sql.catalyst.expressions

import org.apache.spark.sql.catalyst.dsl.expressions._
import org.apache.spark.SparkFunSuite
import org.apache.spark.sql.types.DoubleType

Expand Down Expand Up @@ -191,6 +192,15 @@ class MathFunctionsSuite extends SparkFunSuite with ExpressionEvalHelper {
testUnary(Log2, f, (-5 to -1).map(_ * 1.0), expectNull = true)
}

test("sqrt") {
testUnary(Sqrt, math.sqrt, (0 to 20).map(_ * 0.1))
testUnary(Sqrt, math.sqrt, (-5 to -1).map(_ * 1.0), expectNull = true)

checkEvaluation(Sqrt(Literal.create(null, DoubleType)), null, create_row(null))
checkEvaluation(Sqrt(Literal(-1.0)), null, EmptyRow)
checkEvaluation(Sqrt(Literal(-1.5)), null, EmptyRow)
}

test("pow") {
testBinary(Pow, math.pow, (-5 to 5).map(v => (v * 1.0, v * 1.0)))
testBinary(Pow, math.pow, Seq((-1.0, 0.9), (-2.2, 1.7), (-2.2, -1.7)), expectNull = true)
Expand Down
10 changes: 9 additions & 1 deletion sql/core/src/main/scala/org/apache/spark/sql/functions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -707,11 +707,19 @@ object functions {
/**
* Computes the square root of the specified float value.
*
* @group normal_funcs
* @group math_funcs
* @since 1.3.0
*/
def sqrt(e: Column): Column = Sqrt(e.expr)

/**
* Computes the square root of the specified float value.
*
* @group math_funcs
* @since 1.5.0
*/
def sqrt(colName: String): Column = sqrt(Column(colName))

/**
* Creates a new struct column. The input column must be a column in a [[DataFrame]], or
* a derived column expression that is named (i.e. aliased).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,16 @@ class MathExpressionsSuite extends QueryTest {
checkAnswer(ctx.sql("SELECT LOG2(8), LOG2(null)"), Row(3, null))
}

test("sqrt") {
val df = Seq((1, 4)).toDF("a", "b")
checkAnswer(
df.select(sqrt("a"), sqrt("b")),
Row(1.0, 2.0))

checkAnswer(ctx.sql("SELECT SQRT(4.0), SQRT(null)"), Row(2.0, null))
checkAnswer(df.selectExpr("sqrt(a)", "sqrt(b)", "sqrt(null)"), Row(1.0, 2.0, null))
}

test("negative") {
checkAnswer(
ctx.sql("SELECT negative(1), negative(0), negative(-1)"),
Expand Down