Skip to content
Snippets Groups Projects
Commit 902e4d54 authored by Daoyuan Wang's avatar Daoyuan Wang Committed by Michael Armbrust
Browse files

[SPARK-4755] [SQL] sqrt(negative value) should return null

Author: Daoyuan Wang <daoyuan.wang@intel.com>

Closes #3616 from adrian-wang/sqrt and squashes the following commits:

d877439 [Daoyuan Wang] fix NULLTYPE
3effa2c [Daoyuan Wang] sqrt(negative value) should return null
parent 62771353
No related branches found
No related tags found
No related merge requests found
......@@ -38,11 +38,22 @@ case class Sqrt(child: Expression) extends UnaryExpression {
def dataType = DoubleType
override def foldable = child.foldable
def nullable = child.nullable
def nullable = true
override def toString = s"SQRT($child)"
override def eval(input: Row): Any = {
n1(child, input, (na,a) => math.sqrt(na.toDouble(a)))
val evalE = child.eval(input)
if (evalE == null) {
null
} else {
child.dataType match {
case n: NumericType =>
val value = n.numeric.toDouble(evalE.asInstanceOf[n.JvmType])
if (value < 0) null
else math.sqrt(value)
case other => sys.error(s"Type $other does not support non-negative numeric operations")
}
}
}
}
......
......@@ -1037,6 +1037,8 @@ class ExpressionEvaluationSuite extends FunSuite {
}
checkEvaluation(Sqrt(Literal(null, DoubleType)), null, new GenericRow(Array[Any](null)))
checkEvaluation(Sqrt(-1), null, EmptyRow)
checkEvaluation(Sqrt(-1.5), null, EmptyRow)
}
test("Bitwise operations") {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment