spark-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From marmb...@apache.org
Subject spark git commit: [SPARK-4755] [SQL] sqrt(negative value) should return null
Date Wed, 17 Dec 2014 20:51:33 GMT
Repository: spark
Updated Branches:
  refs/heads/master 627713537 -> 902e4d54a


[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


Project: http://git-wip-us.apache.org/repos/asf/spark/repo
Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/902e4d54
Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/902e4d54
Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/902e4d54

Branch: refs/heads/master
Commit: 902e4d54acbc3c88163a5c6447aff68ed57475c1
Parents: 6277135
Author: Daoyuan Wang <daoyuan.wang@intel.com>
Authored: Wed Dec 17 12:51:27 2014 -0800
Committer: Michael Armbrust <michael@databricks.com>
Committed: Wed Dec 17 12:51:27 2014 -0800

----------------------------------------------------------------------
 .../spark/sql/catalyst/expressions/arithmetic.scala  | 15 +++++++++++++--
 .../expressions/ExpressionEvaluationSuite.scala      |  2 ++
 2 files changed, 15 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/spark/blob/902e4d54/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala
----------------------------------------------------------------------
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala
index 79a742a..168a963 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala
@@ -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")
+      }
+    }
   }
 }
 

http://git-wip-us.apache.org/repos/asf/spark/blob/902e4d54/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ExpressionEvaluationSuite.scala
----------------------------------------------------------------------
diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ExpressionEvaluationSuite.scala
b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ExpressionEvaluationSuite.scala
index 1e371db..4ba7d87 100644
--- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ExpressionEvaluationSuite.scala
+++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ExpressionEvaluationSuite.scala
@@ -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") {


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org


Mime
View raw message