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

    https://github.com/apache/spark/pull/10444#discussion_r48332063
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/predicates.scala
 ---
    @@ -47,6 +48,34 @@ trait Predicate extends Expression {
       override def dataType: DataType = BooleanType
     }
     
    +object Predicate extends PredicateHelper {
    +  def toCNF(predicate: Expression, maybeThreshold: Option[Double] = None): 
Expression = {
    +    val cnf = new CNFExecutor(predicate).execute(predicate)
    +    val threshold = maybeThreshold.map(predicate.size * 
_).getOrElse(Double.MaxValue)
    +    if (cnf.size > threshold) predicate else cnf
    +  }
    +
    +  private class CNFNormalization(input: Expression)
    +    extends Rule[Expression] {
    +
    +    override def apply(tree: Expression): Expression = {
    +      import org.apache.spark.sql.catalyst.dsl.expressions._
    +
    +      tree transformDown {
    +        case Not(Not(e)) => e
    +        case Not(a And b) => !a || !b
    +        case Not(a Or b) => !a && !b
    +        case a Or (b And c) => (a || b) && (a && c)
    +        case (a And b) Or c => (a || c) && (b || c)
    +      }
    +    }
    +  }
    +
    +  private class CNFExecutor(input: Expression) extends 
RuleExecutor[Expression] {
    +    override protected val batches: Seq[Batch] =
    +      Batch("CNFNormalization", FixedPoint.Unlimited, new 
CNFNormalization(input)) :: Nil
    --- End diff --
    
    `FixedPoint.Unlimited` is used here to guarantee that we can really reach 
CNF when no expansion threshold is provided. This should be safe since all the 
rules defined here converge.


---
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