dongjoon-hyun commented on code in PR #60651:
URL: https://github.com/apache/spark/pull/60651#discussion_r4611582592
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/ScalaUDF.scala:
##########
@@ -52,12 +52,19 @@ case class ScalaUDF(
inputEncoders: Seq[Option[ExpressionEncoder[_]]] = Nil,
outputEncoder: Option[ExpressionEncoder[_]] = None,
udfName: Option[String] = None,
- nullable: Boolean = true,
+ udfNullable: Boolean = true,
udfDeterministic: Boolean = true)
extends Expression with NonSQLExpression with UserDefinedExpression {
override lazy val deterministic: Boolean = udfDeterministic &&
children.forall(_.deterministic)
+ // The analyzer makes the UDF return null for a null input of a primitive
parameter (see
+ // `HandleNullInputsForUDF`) only after resolution, so the nullability
covers that case for the
+ // rules that run before, e.g. the ones adding NOT NULL checks to table
writes.
+ override def nullable: Boolean = udfNullable ||
inputPrimitives.zip(children).exists {
Review Comment:
Thank you. I reproduced it: with ANSI off, `UPDATE t SET salary =
plus1(dep)` and `MERGE ... WHEN MATCHED THEN UPDATE SET salary = plus1(s.dep)`
over a malformed non-NULL string wrote NULL (or `-1`) into the NOT NULL column
for both group-based and delta-based writes, while INSERT and insert-only MERGE
failed with `NOT_NULL_ASSERT_VIOLATION`. `nullable` now also counts a primitive
parameter whose input can become NULL through the cast to the parameter type,
`Cast.forceNullable(child.dataType, inputType)`. Now both UPDATE and MERGE fail
with `NOT_NULL_ASSERT_VIOLATION` for a malformed string and write the computed
value for a valid one. The MERGE test checks `UPDATE SET salary = plus1('x')`
with ANSI off, and the `ScalaUDFSuite` test checks the nullability of a UDF
over a String input for its Int parameter.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/ScalaUDF.scala:
##########
@@ -52,12 +52,19 @@ case class ScalaUDF(
inputEncoders: Seq[Option[ExpressionEncoder[_]]] = Nil,
outputEncoder: Option[ExpressionEncoder[_]] = None,
udfName: Option[String] = None,
- nullable: Boolean = true,
+ udfNullable: Boolean = true,
udfDeterministic: Boolean = true)
extends Expression with NonSQLExpression with UserDefinedExpression {
override lazy val deterministic: Boolean = udfDeterministic &&
children.forall(_.deterministic)
+ // The analyzer makes the UDF return null for a null input of a primitive
parameter (see
+ // `HandleNullInputsForUDF`) only after resolution, so the nullability
covers that case for the
+ // rules that run before, e.g. the ones adding NOT NULL checks to table
writes.
+ override def nullable: Boolean = udfNullable ||
inputPrimitives.zip(children).exists {
Review Comment:
Thank you. `nullable` is now a `lazy val`, like `deterministic`, so it is
computed once per instance. It now checks the cast too, so this helps a bit
more.
##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/AnalysisSuite.scala:
##########
@@ -2203,7 +2203,7 @@ class AnalysisSuite extends AnalysisTest with Matchers {
function = (i: Int) => i + 1,
dataType = IntegerType,
children = $"a" :: Nil,
- nullable = false,
+ udfNullable = false,
Review Comment:
Thank you. I confirmed it by changing the batch to `FixedPoint(1)`: the
three SPARK-47927 tests still passed, and a UDF over `ds2("value")` from the
null-supplying side of a left outer join returned `1` instead of NULL. I added
that case to `UDFSuite`. I also updated the comment of the batch to give the
reason that remains: an attribute can become nullable after resolution.
##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ScalaUDFSuite.scala:
##########
@@ -139,4 +139,16 @@ class ScalaUDFSuite extends SparkFunSuite with
ExpressionEvalHelper {
val code = udf.genCode(new CodegenContext).code.toString
assert(!code.contains("fieldName") && !code.contains("udfName") &&
!code.contains("outputName"))
}
+
+ test("SPARK-61935: nullable when the input of a primitive parameter is
nullable") {
Review Comment:
Thank you. The test now uses one `(String, Int)` UDF. A NULL String input
keeps it non-nullable, while a NULL Int input makes it nullable, and so does a
String input for the Int parameter. Both of the bodies you mentioned fail it,
and so does dropping the cast check.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/ScalaUDF.scala:
##########
@@ -41,7 +41,7 @@ import org.apache.spark.util.Utils
* @param outputEncoder ExpressionEncoder for the return type of function.
It's only defined when
* this is a typed Scala UDF.
* @param udfName The user-specified name of this UDF.
- * @param nullable True if the UDF can return null value.
+ * @param udfNullable True if the UDF can return null value.
Review Comment:
Thank you. I updated it based on your suggestion.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]