Skip to content

Commit 595c6c8

Browse files
committed
Implements eval for expression AssertNotNull
1 parent f5ea7fe commit 595c6c8

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -690,17 +690,23 @@ case class AssertNotNull(child: Expression, walkedTypePath: Seq[String])
690690
override def foldable: Boolean = false
691691
override def nullable: Boolean = false
692692

693-
override def eval(input: InternalRow): Any =
694-
throw new UnsupportedOperationException("Only code-generated evaluation is supported.")
693+
private val errMsg = "Null value appeared in non-nullable field:" +
694+
walkedTypePath.mkString("\n", "\n", "\n") +
695+
"If the schema is inferred from a Scala tuple/case class, or a Java bean, " +
696+
"please try to use scala.Option[_] or other nullable types " +
697+
"(e.g. java.lang.Integer instead of int/scala.Int)."
698+
699+
override def eval(input: InternalRow): Any = {
700+
val result = child.eval(input)
701+
if (result == null) {
702+
throw new RuntimeException(errMsg);
703+
}
704+
result
705+
}
695706

696707
override protected def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
697708
val childGen = child.genCode(ctx)
698709

699-
val errMsg = "Null value appeared in non-nullable field:" +
700-
walkedTypePath.mkString("\n", "\n", "\n") +
701-
"If the schema is inferred from a Scala tuple/case class, or a Java bean, " +
702-
"please try to use scala.Option[_] or other nullable types " +
703-
"(e.g. java.lang.Integer instead of int/scala.Int)."
704710
val errMsgField = ctx.addReferenceObj("errMsg", errMsg)
705711

706712
val code = s"""

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.spark.sql.catalyst.expressions
1919

2020
import org.apache.spark.SparkFunSuite
21+
import org.apache.spark.sql.catalyst.expressions.objects.AssertNotNull
2122
import org.apache.spark.sql.types._
2223

2324
class NullFunctionsSuite extends SparkFunSuite with ExpressionEvalHelper {
@@ -45,6 +46,13 @@ class NullFunctionsSuite extends SparkFunSuite with ExpressionEvalHelper {
4546
}
4647
}
4748

49+
test("AssertNotNUll") {
50+
val ex = intercept[RuntimeException] {
51+
evaluate(AssertNotNull(Literal(null), Seq.empty[String]))
52+
}.getMessage
53+
assert(ex.contains("Null value appeared in non-nullable field"))
54+
}
55+
4856
test("IsNaN") {
4957
checkEvaluation(IsNaN(Literal(Double.NaN)), true)
5058
checkEvaluation(IsNaN(Literal(Float.NaN)), true)

0 commit comments

Comments
 (0)