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_r376882451
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala
##########
@@ -434,6 +434,27 @@ object SimplifyConditionals extends Rule[LogicalPlan]
with PredicateHelper {
case _ => false
}
+ /**
+ * Condition for redundant null check based on intolerant expressions.
+ * @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 isRedundantNullCheck(
+ ifNullExpr: Expression,
+ ifNotNullExpr: Expression,
+ checkedExpr: Expression): Boolean = {
+ val isNullIntolerant = ifNotNullExpr.find { x =>
+ !x.isInstanceOf[NullIntolerant] && x.find(e =>
e.semanticEquals(checkedExpr)).nonEmpty
Review comment:
I agree that the committed code is not very intuitive so i can think of this
way which seems to be more readable (added also some comments):
```
private def isRedundantNullCheck(
ifNullExpr: Expression,
ifNotNullExpr: Expression,
checkedExpr: Expression): Boolean = {
// checks if expr is null-intolerant with respect to checkedExpr
def isNullIntolerant(expr: Expression): Boolean = expr match {
case e: NullIntolerant => e.children.forall(isNullIntolerant)
// if some child is null-tolerant but the checkedEpxr is not in its
subtree
// we can still consider the whole expr as null-intolerant
// with respect to checkedExpr
case e if e.find(x => x.semanticEquals(checkedExpr)).isEmpty => true
case _ => false
}
isNullIntolerant(ifNotNullExpr) && {
(ifNullExpr.semanticEquals(checkedExpr) ||
(ifNullExpr.foldable && ifNullExpr.eval() == null)) &&
// we still need to make sure that checkedExpr is inside ifNotNullExpr
ifNotNullExpr.find(x => x.semanticEquals(checkedExpr)).nonEmpty
}
}
```
But not sure if this is what you had in mind when suggesting to split the
condition. Can you think of a better way how to compose this?
----------------------------------------------------------------
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]