Skip to content

Commit 65fb8c2

Browse files
committed
address feedback
1 parent 584ec81 commit 65fb8c2

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -417,19 +417,26 @@ object SimplifyConditionals extends Rule[LogicalPlan] with PredicateHelper {
417417
val (h, t) = branches.span(_._1 != TrueLiteral)
418418
CaseWhen( h :+ t.head, None)
419419

420-
case e @ CaseWhen(branches, Some(elseValue)) if branches
421-
.forall(_._2.semanticEquals(elseValue)) =>
420+
case e @ CaseWhen(branches, Some(elseValue))
421+
if branches.forall(_._2.semanticEquals(elseValue)) =>
422422
// For non-deterministic conditions with side effect, we can not remove it, or change
423423
// the ordering. As a result, we try to remove the deterministic conditions from the tail.
424-
val newBranches = branches.map(_._1).foldRight((List[Expression](), false)) {
425-
case (cond, (branches, true)) => (cond :: branches, true)
426-
case (cond, (branches, false)) if !cond.deterministic => (cond :: branches, true)
427-
case (_, (branches, false)) => (branches, false)
428-
}._1.map(cond => (cond, elseValue))
429-
if (newBranches.nonEmpty) {
430-
e.copy(branches = newBranches)
424+
var hitNonDeterministicCond = false
425+
var i = branches.length
426+
while (i > 0 && !hitNonDeterministicCond) {
427+
hitNonDeterministicCond = !branches(i - 1)._1.deterministic
428+
if (!hitNonDeterministicCond) {
429+
i -= 1
430+
}
431+
}
432+
if (i != branches.length) {
433+
if (i == 0) {
434+
elseValue
435+
} else {
436+
e.copy(branches = branches.take(i))
437+
}
431438
} else {
432-
elseValue
439+
e
433440
}
434441
}
435442
}

0 commit comments

Comments
 (0)