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

    https://github.com/apache/spark/pull/10445#discussion_r48592772
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
 ---
    @@ -473,112 +474,97 @@ object OptimizeIn extends Rule[LogicalPlan] {
     object BooleanSimplification extends Rule[LogicalPlan] with 
PredicateHelper {
       def apply(plan: LogicalPlan): LogicalPlan = plan transform {
         case q: LogicalPlan => q transformExpressionsUp {
    -      case and @ And(left, right) => (left, right) match {
    -        // true && r  =>  r
    -        case (Literal(true, BooleanType), r) => r
    -        // l && true  =>  l
    -        case (l, Literal(true, BooleanType)) => l
    -        // false && r  =>  false
    -        case (Literal(false, BooleanType), _) => Literal(false)
    -        // l && false  =>  false
    -        case (_, Literal(false, BooleanType)) => Literal(false)
    -        // a && a  =>  a
    -        case (l, r) if l fastEquals r => l
    -        // a && (not(a) || b) => a && b
    -        case (l, Or(l1, r)) if (Not(l) == l1) => And(l, r)
    -        case (l, Or(r, l1)) if (Not(l) == l1) => And(l, r)
    -        case (Or(l, l1), r) if (l1 == Not(r)) => And(l, r)
    -        case (Or(l1, l), r) if (l1 == Not(r)) => And(l, r)
    -        // (a || b) && (a || c)  =>  a || (b && c)
    -        case _ =>
    -          // 1. Split left and right to get the disjunctive predicates,
    -          //   i.e. lhs = (a, b), rhs = (a, c)
    -          // 2. Find the common predict between lhsSet and rhsSet, i.e. 
common = (a)
    -          // 3. Remove common predict from lhsSet and rhsSet, i.e. ldiff = 
(b), rdiff = (c)
    -          // 4. Apply the formula, get the optimized predicate: common || 
(ldiff && rdiff)
    -          val lhs = splitDisjunctivePredicates(left)
    -          val rhs = splitDisjunctivePredicates(right)
    -          val common = lhs.filter(e => rhs.exists(e.semanticEquals(_)))
    -          if (common.isEmpty) {
    -            // No common factors, return the original predicate
    -            and
    +      case TrueLiteral And e => e
    +      case e And TrueLiteral => e
    +      case FalseLiteral Or e => e
    +      case e Or FalseLiteral => e
    +
    +      case FalseLiteral And _ => FalseLiteral
    +      case _ And FalseLiteral => FalseLiteral
    +      case TrueLiteral Or _ => TrueLiteral
    +      case _ Or TrueLiteral => TrueLiteral
    +
    +      case a And b if a.semanticEquals(b) => a
    +      case a Or b if a.semanticEquals(b) => a
    +
    +      case a And (b Or c) if Not(a).semanticEquals(b) => And(a, c)
    +      case a And (b Or c) if Not(a).semanticEquals(c) => And(a, b)
    +      case (a Or b) And c if a.semanticEquals(Not(c)) => And(b, c)
    +      case (a Or b) And c if b.semanticEquals(Not(c)) => And(a, c)
    +
    +      case a Or (b And c) if Not(a).semanticEquals(b) => Or(a, c)
    +      case a Or (b And c) if Not(a).semanticEquals(c) => Or(a, b)
    +      case (a And b) Or c if a.semanticEquals(Not(c)) => Or(b, c)
    +      case (a And b) Or c if b.semanticEquals(Not(c)) => Or(a, c)
    +
    +      // Common factor elimination for conjunction
    +      case and @ (left And right) =>
    +        // 1. Split left and right to get the disjunctive predicates,
    +        //   i.e. lhs = (a, b), rhs = (a, c)
    +        // 2. Find the common predict between lhsSet and rhsSet, i.e. 
common = (a)
    +        // 3. Remove common predict from lhsSet and rhsSet, i.e. ldiff = 
(b), rdiff = (c)
    +        // 4. Apply the formula, get the optimized predicate: common || 
(ldiff && rdiff)
    +        val lhs = splitDisjunctivePredicates(left)
    +        val rhs = splitDisjunctivePredicates(right)
    +        val common = lhs.filter(e => rhs.exists(e.semanticEquals))
    +        if (common.isEmpty) {
    +          // No common factors, return the original predicate
    +          and
    +        } else {
    +          val ldiff = lhs.filterNot(e => common.exists(e.semanticEquals))
    +          val rdiff = rhs.filterNot(e => common.exists(e.semanticEquals))
    +          if (ldiff.isEmpty || rdiff.isEmpty) {
    +            // (a || b || c || ...) && (a || b) => (a || b)
    +            common.reduce(Or)
               } else {
    -            val ldiff = lhs.filterNot(e => 
common.exists(e.semanticEquals(_)))
    -            val rdiff = rhs.filterNot(e => 
common.exists(e.semanticEquals(_)))
    -            if (ldiff.isEmpty || rdiff.isEmpty) {
    -              // (a || b || c || ...) && (a || b) => (a || b)
    -              common.reduce(Or)
    -            } else {
    -              // (a || b || c || ...) && (a || b || d || ...) =>
    -              // ((c || ...) && (d || ...)) || a || b
    -              (common :+ And(ldiff.reduce(Or), 
rdiff.reduce(Or))).reduce(Or)
    -            }
    +            // (a || b || c || ...) && (a || b || d || ...) =>
    +            // ((c || ...) && (d || ...)) || a || b
    +            (common :+ And(ldiff.reduce(Or), rdiff.reduce(Or))).reduce(Or)
               }
    -      }  // end of And(left, right)
    -
    -      case or @ Or(left, right) => (left, right) match {
    -        // true || r  =>  true
    -        case (Literal(true, BooleanType), _) => Literal(true)
    -        // r || true  =>  true
    -        case (_, Literal(true, BooleanType)) => Literal(true)
    -        // false || r  =>  r
    -        case (Literal(false, BooleanType), r) => r
    -        // l || false  =>  l
    -        case (l, Literal(false, BooleanType)) => l
    -        // a || a => a
    -        case (l, r) if l fastEquals r => l
    -        // (a && b) || (a && c)  =>  a && (b || c)
    -        case _ =>
    -           // 1. Split left and right to get the conjunctive predicates,
    -           //   i.e.  lhs = (a, b), rhs = (a, c)
    -           // 2. Find the common predict between lhsSet and rhsSet, i.e. 
common = (a)
    -           // 3. Remove common predict from lhsSet and rhsSet, i.e. ldiff 
= (b), rdiff = (c)
    -           // 4. Apply the formula, get the optimized predicate: common && 
(ldiff || rdiff)
    -          val lhs = splitConjunctivePredicates(left)
    -          val rhs = splitConjunctivePredicates(right)
    -          val common = lhs.filter(e => rhs.exists(e.semanticEquals(_)))
    -          if (common.isEmpty) {
    -            // No common factors, return the original predicate
    -            or
    +        }
    +
    +      // Common factor elimination for disjunction
    +      case or @ (left Or right) =>
    +        // 1. Split left and right to get the conjunctive predicates,
    +        //   i.e.  lhs = (a, b), rhs = (a, c)
    +        // 2. Find the common predict between lhsSet and rhsSet, i.e. 
common = (a)
    +        // 3. Remove common predict from lhsSet and rhsSet, i.e. ldiff = 
(b), rdiff = (c)
    +        // 4. Apply the formula, get the optimized predicate: common && 
(ldiff || rdiff)
    +        val lhs = splitConjunctivePredicates(left)
    +        val rhs = splitConjunctivePredicates(right)
    +        val common = lhs.filter(e => rhs.exists(e.semanticEquals))
    +        if (common.isEmpty) {
    +          // No common factors, return the original predicate
    +          or
    +        } else {
    +          val ldiff = lhs.filterNot(e => common.exists(e.semanticEquals))
    +          val rdiff = rhs.filterNot(e => common.exists(e.semanticEquals))
    +          if (ldiff.isEmpty || rdiff.isEmpty) {
    +            // (a && b) || (a && b && c && ...) => a && b
    +            common.reduce(And)
               } else {
    -            val ldiff = lhs.filterNot(e => 
common.exists(e.semanticEquals(_)))
    -            val rdiff = rhs.filterNot(e => 
common.exists(e.semanticEquals(_)))
    -            if (ldiff.isEmpty || rdiff.isEmpty) {
    -              // (a && b) || (a && b && c && ...) => a && b
    -              common.reduce(And)
    -            } else {
    -              // (a && b && c && ...) || (a && b && d && ...) =>
    -              // ((c && ...) || (d && ...)) && a && b
    -              (common :+ Or(ldiff.reduce(And), 
rdiff.reduce(And))).reduce(And)
    -            }
    +            // (a && b && c && ...) || (a && b && d && ...) =>
    +            // ((c && ...) || (d && ...)) && a && b
    +            (common :+ Or(ldiff.reduce(And), 
rdiff.reduce(And))).reduce(And)
    --- End diff --
    
    (Code for common factor elimination for conjunction and disjunction is 
untouched except for indentation changes.)


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