Github user liancheng commented on a diff in the pull request:

    https://github.com/apache/spark/pull/8200#discussion_r37075991
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/predicates.scala
 ---
    @@ -59,6 +59,30 @@ trait PredicateHelper {
         }
       }
     
    +  /**
    +   * Convert an expression into its conjunctive normal form (CNF), i.e. 
AND of ORs.
    +   * For example, a && b || c is normalized to (a || c) && (b || c) by 
this method.
    +   *
    +   * Refer to https://en.wikipedia.org/wiki/Conjunctive_normal_form for 
more information
    +   */
    +  protected def cnfNormalization(condition: Expression): Expression = {
    +    condition.transformUp {
    +      case or: Or => pushOrToBottom(or)
    +    }
    +  }
    +
    +  private def pushOrToBottom(condition: Expression): Expression = {
    +    condition match {
    +      case Or(left, right) => (left, right) match {
    +        case (And(ll, lr), r) =>
    +          And(pushOrToBottom(Or(ll, r)), pushOrToBottom(Or(lr, r)))
    +        case (l, And(rl, rr)) =>
    +          And(pushOrToBottom(Or(l, rl)), pushOrToBottom(Or(l, rr)))
    +        case (l, r) => Or(l, r)
    +      }
    +    }
    +  }
    --- End diff --
    
    The following version may be questionably more readable:
    
    ```scala
      private def pushOrToBottom(condition: Expression): Expression = {
        condition match {
          case Or(And(innerLhs, innerRhs), rhs) =>
            And(pushOrToBottom(Or(innerLhs, rhs)), pushOrToBottom(Or(innerRhs, 
rhs)))
    
          case Or(lhs, And(innerLhs, innerRhs)) =>
            And(pushOrToBottom(Or(lhs, innerLhs)), pushOrToBottom(Or(lhs, 
innerRhs)))
    
          case _ => condition
        }
      }
    ```
    
    Shall we also cover cases like `Not(Or(x, y))` => `And(Not(x), Not(y))` 
here?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to