You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by cloud-fan <gi...@git.apache.org> on 2018/08/01 02:33:40 UTC

[GitHub] spark pull request #21852: [SPARK-24893] [SQL] Remove the entire CaseWhen if...

Github user cloud-fan commented on a diff in the pull request:

    https://github.com/apache/spark/pull/21852#discussion_r206739216
  
    --- Diff: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala ---
    @@ -416,6 +416,24 @@ 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, Some(elseValue))
    +          if branches.forall(_._2.semanticEquals(elseValue)) =>
    +        // For non-deterministic conditions with side effect, we can not remove it, or change
    +        // the ordering. As a result, we try to remove the deterministic conditions from the tail.
    +        var hitNonDeterministicCond = false
    +        var i = branches.length
    +        while (i > 0 && !hitNonDeterministicCond) {
    +          hitNonDeterministicCond = !branches(i - 1)._1.deterministic
    +          if (!hitNonDeterministicCond) {
    --- End diff --
    
    nit: we can avoid this per-iteration `if` check by updating the final step
    ```
    if (i == 0 && !hitNonDeterministicCond) {
      elseValue
    } else {
      e.copy(branches = branches.take(i + 1).map(branch => (branch._1, elseValue)))
    }
    ``` 
    feel free to change it in your next PR.


---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org