wangyum commented on a change in pull request #28575: URL: https://github.com/apache/spark/pull/28575#discussion_r434622127
########## File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala ########## @@ -1372,6 +1376,108 @@ object PushPredicateThroughJoin extends Rule[LogicalPlan] with PredicateHelper { } } +/** + * Rewriting join condition to conjunctive normal form expression so that we can push + * more predicate. + */ +object PushPredicateThroughJoinByCNF extends Rule[LogicalPlan] with PredicateHelper { + /** + * Rewrite pattern: + * 1. (a && b) || c --> (a || c) && (b || c) + * 2. a || (b && c) --> (a || b) && (a || c) + * + * To avoid generating too many predicates, we first group the filter columns from the same table. + */ + private def toCNF(condition: Expression, depth: Int = 0): Expression = { + if (depth < SQLConf.get.maxRewritingCNFDepth) { + condition match { + case or @ Or(left: And, right: And) => + val lhs = splitConjunctivePredicates(left).groupBy(_.references.map(_.qualifier)) + val rhs = splitConjunctivePredicates(right).groupBy(_.references.map(_.qualifier)) + if (lhs.size > 1) { + lhs.values.map(_.reduceLeft(And)).map { c => + toCNF(Or(toCNF(c, depth + 1), toCNF(right, depth + 1)), depth + 1) + }.reduce(And) + } else if (rhs.size > 1) { + rhs.values.map(_.reduceLeft(And)).map { c => + toCNF(Or(toCNF(left, depth + 1), toCNF(c, depth + 1)), depth + 1) + }.reduce(And) + } else { + or + } + + case or @ Or(left: And, right) => + val lhs = splitConjunctivePredicates(left).groupBy(_.references.map(_.qualifier)) + if (lhs.size > 1) { + lhs.values.map(_.reduceLeft(And)).map { + c => toCNF(Or(toCNF(c, depth + 1), toCNF(right, depth + 1)), depth + 1) + }.reduce(And) + } else { + or + } + + case or @ Or(left, right: And) => + val rhs = splitConjunctivePredicates(right).groupBy(_.references.map(_.qualifier)) + if (rhs.size > 1) { + rhs.values.map(_.reduceLeft(And)).map { c => + toCNF(Or(toCNF(left, depth + 1), toCNF(c, depth + 1)), depth + 1) + }.reduce(And) + } else { + or + } + + case And(left, right) => + And(toCNF(left, depth + 1), toCNF(right, depth + 1)) + + case other => + other + } + } else { + condition + } + } + + private def maybeWithFilter(joinCondition: Option[Expression], plan: LogicalPlan) = { + (joinCondition, plan) match { + // Avoid adding the same filter. + case (Some(condition), filter: Filter) if condition.semanticEquals(filter.condition) => + plan + case (Some(condition), _) => + Filter(condition, plan) + case _ => + plan + } + } + + def apply(plan: LogicalPlan): LogicalPlan = plan transform applyLocally Review comment: Fixed it. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org