-
Notifications
You must be signed in to change notification settings - Fork 28.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPARK-11725][SQL] correctly handle null inputs for UDF #9770
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,7 @@ import org.apache.spark.sql.catalyst.expressions.aggregate._ | |
import org.apache.spark.sql.catalyst.plans.logical._ | ||
import org.apache.spark.sql.catalyst.rules._ | ||
import org.apache.spark.sql.catalyst.trees.TreeNodeRef | ||
import org.apache.spark.sql.catalyst.{SimpleCatalystConf, CatalystConf} | ||
import org.apache.spark.sql.catalyst.{ScalaReflection, SimpleCatalystConf, CatalystConf} | ||
import org.apache.spark.sql.types._ | ||
|
||
/** | ||
|
@@ -85,6 +85,8 @@ class Analyzer( | |
extendedResolutionRules : _*), | ||
Batch("Nondeterministic", Once, | ||
PullOutNondeterministic), | ||
Batch("UDF", Once, | ||
HandleNullInputsForUDF), | ||
Batch("Cleanup", fixedPoint, | ||
CleanupAliases) | ||
) | ||
|
@@ -1063,6 +1065,34 @@ class Analyzer( | |
Project(p.output, newPlan.withNewChildren(newChild :: Nil)) | ||
} | ||
} | ||
|
||
/** | ||
* Correctly handle null primitive inputs for UDF by adding extra [[If]] expression to do the | ||
* null check. When user defines a UDF with primitive parameters, there is no way to tell if the | ||
* primitive parameter is null or not, so here we assume the primitive input is null-propagatable | ||
* and we should return null if the input is null. | ||
*/ | ||
object HandleNullInputsForUDF extends Rule[LogicalPlan] { | ||
override def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators { | ||
case p if !p.resolved => p // Skip unresolved nodes. | ||
|
||
case plan => plan transformExpressionsUp { | ||
|
||
case udf @ ScalaUDF(func, _, inputs, _) => | ||
val parameterTypes = ScalaReflection.getParameterTypes(func) | ||
assert(parameterTypes.length == inputs.length) | ||
|
||
val inputsNullCheck = parameterTypes.zip(inputs) | ||
// TODO: skip null handling for not-nullable primitive inputs after we can completely | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given the fact that most of the common code passes are not using I'd vote to do that in next release (consider There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To play devils advocate, I think when the info is wrong is usually likely to be too conservative (allow nulls when there are none). Also, I'm not really sure what is going to change between now and 1.7 (i.e. if there are bugs we need to find them eventually). That said, I'm fine waiting, but we should use this info eventually given the amount of effort we spend passing it around. |
||
// trust the `nullable` information. | ||
// .filter { case (cls, expr) => cls.isPrimitive && expr.nullable } | ||
.filter { case (cls, _) => cls.isPrimitive } | ||
.map { case (_, expr) => IsNull(expr) } | ||
.reduceLeftOption[Expression]((e1, e2) => Or(e1, e2)) | ||
inputsNullCheck.map(If(_, Literal.create(null, udf.dataType), udf)).getOrElse(udf) | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Scaladoc please