davidvrba commented on a change in pull request #27231: [SPARK-28478] [SQL]
Remove redundant null checks
URL: https://github.com/apache/spark/pull/27231#discussion_r367372431
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala
##########
@@ -757,3 +757,45 @@ object CombineConcats extends Rule[LogicalPlan] {
flattenConcats(concat)
}
}
+
+/**
+ * Removes unnecessary null checks for If/CaseWhen conditions
+ * with NullIntolerant expressions.
+ */
+object RemoveRedundantNullChecks extends Rule[LogicalPlan] {
+ /**
+ * @param ifNullExpr expression that takes place if checkedExpr is null
+ * @param ifNotNullExpr expression that takes place if checkedExpr is not
null
+ * @param checkedExpr expression that is checked for null value
+ */
+ private def isRedundant(
+ ifNullExpr: Expression,
+ ifNotNullExpr: Expression,
+ checkedExpr: Expression): Boolean = {
+ (ifNullExpr == checkedExpr || ifNullExpr == Literal.create(null,
checkedExpr.dataType)) &&
+ ifNotNullExpr.isInstanceOf[NullIntolerant] &&
+ ifNotNullExpr.children.contains(checkedExpr)
+ }
+
+ def apply(plan: LogicalPlan): LogicalPlan = plan transformAllExpressions {
+ case i @ If(predicate, trueValue, falseValue) => predicate match {
Review comment:
The `NullPropagation` rule simplifies expressions to literals. But i feel
that my pr is covering slightly different case. Here the expression that is
being null-checked is in general not `Literal` and can not be converted to
`Literal` (in general).
However I can also see that the logic of my rule can be moved to
`SimplifyConditionals`, so I can move it there if this is the preferred way.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]