maropu commented on a change in pull request #27231: [SPARK-28478][SQL] Remove redundant null checks URL: https://github.com/apache/spark/pull/27231#discussion_r368782709
########## File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala ########## @@ -483,6 +507,19 @@ object SimplifyConditionals extends Rule[LogicalPlan] with PredicateHelper { } else { e.copy(branches = branches.take(i).map(branch => (branch._1, elseValue))) } + + case e @ CaseWhen(branches, elseValue) if branches.length == 1 => + // remove redundant null checks for CaseWhen with one branch + branches(0)._1 match { + case IsNotNull(child) if isRedundantNullCheck( + elseValue.getOrElse(Literal.create(null, child.dataType)), + branches(0)._2, child) => branches(0)._2 + case IsNull(child) if isRedundantNullCheck( + branches(0)._2, + elseValue.getOrElse(Literal.create(null, child.dataType)), + child) => elseValue.getOrElse(Literal.create(null, child.dataType)) + case _ => e + } Review comment: How about this? ``` // remove redundant null checks for CaseWhen with one branch case CaseWhen(Seq((IsNotNull(child), trueValue)), Some(falseValue)) if isRedundantNullCheck(falseValue, trueValue, child) => trueValue case CaseWhen(Seq((IsNull(child), trueValue)), Some(falseValue)) if isRedundantNullCheck(trueValue, falseValue, child) => falseValue case CaseWhen(Seq((IsNotNull(child), trueValue)), None) if isRedundantNullCheck(Literal.create(null, child.dataType), trueValue, child) => trueValue case e @ CaseWhen(Seq((IsNull(child), trueValue)), None) => val nullValue = Literal.create(null, child.dataType) if (isRedundantNullCheck(trueValue, nullValue, child)) { nullValue } else { e } ``` ---------------------------------------------------------------- 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: us...@infra.apache.org With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org