Github user dbtsai commented on a diff in the pull request: https://github.com/apache/spark/pull/21904#discussion_r205963712 --- Diff: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala --- @@ -416,6 +416,29 @@ object SimplifyConditionals extends Rule[LogicalPlan] with PredicateHelper { // these branches can be pruned away val (h, t) = branches.span(_._1 != TrueLiteral) CaseWhen( h :+ t.head, None) + + case e @ CaseWhen(branches, _) => + val newBranches = branches.foldLeft(List[(Expression, Expression)]()) { + case (newBranches, branch) => + if (newBranches.exists(_._1.semanticEquals(branch._1))) { + // If a condition in a branch is previously seen, this branch can be pruned. + // TODO: In fact, if a condition is a sub-condition of the previous one, + // TODO: it can be pruned. This is less strict and can be implemented + // TODO: by decomposing seen conditions. + newBranches + } else if (newBranches.nonEmpty && newBranches.last._2.semanticEquals(branch._2)) { + // If the outputs of two adjacent branches are the same, two branches can be combined. + newBranches.take(newBranches.length - 1) + .:+((Or(newBranches.last._1, branch._1), newBranches.last._2)) --- End diff -- For example, the following case can be benefitted from this rule, ```scala CaseWhen((UnresolvedAttribute("a"), Literal(1)) :: (Not(UnresolvedAttribute("a")), Literal(1)) :: (LessThan(Rand(1), Literal(0.5)), Literal(3)) :: (NonFoldableLiteral(true), Literal(4)) :: (NonFoldableLiteral(false), Literal(5)) :: Nil, None), Literal(1)) ```
--- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org