-
Notifications
You must be signed in to change notification settings - Fork 28.8k
[SPARK-31705][SQL] Push predicate through join by rewriting join condition to conjunctive normal form #28575
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
21fb7c5
b38404c
6c44d64
e8f2471
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,7 +51,8 @@ abstract class Optimizer(catalogManager: CatalogManager) | |
override protected val blacklistedOnceBatches: Set[String] = | ||
Set( | ||
"PartitionPruning", | ||
"Extract Python UDFs") | ||
"Extract Python UDFs", | ||
"Push predicate through join by CNF") | ||
|
||
protected def fixedPoint = | ||
FixedPoint( | ||
|
@@ -118,7 +119,11 @@ abstract class Optimizer(catalogManager: CatalogManager) | |
Batch("Infer Filters", Once, | ||
InferFiltersFromConstraints) :: | ||
Batch("Operator Optimization after Inferring Filters", fixedPoint, | ||
rulesWithoutInferFiltersFromConstraints: _*) :: Nil | ||
rulesWithoutInferFiltersFromConstraints: _*) :: | ||
// Set strategy to Once to avoid pushing filter every time because we do not change the | ||
// join condition. | ||
Batch("Push predicate through join by CNF", Once, | ||
PushPredicateThroughJoinByCNF) :: Nil | ||
} | ||
|
||
val batches = (Batch("Eliminate Distinct", Once, EliminateDistinct) :: | ||
|
@@ -1372,6 +1377,96 @@ 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 && d) --> (a || c) && (a || d) && (b || c) && (b && d) | ||
wangyum marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* 2. (a && b) || c --> (a || c) && (b || c) | ||
* 3. a || (b && c) --> (a || b) && (a || c) | ||
* | ||
* To avoid generating too many predicates, we first group the 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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. group by qualifier to avoid generating too many predicates. For example:
Group by qualifier:
|
||
val rhs = splitConjunctivePredicates(right).groupBy(_.references.map(_.qualifier)) | ||
if (lhs.size > 1) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shall we pick There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. We will pick it at We will pick it at |
||
lhs.values.map(_.reduceLeft(And)).map { e => | ||
toCNF(Or(toCNF(e, depth + 1), toCNF(right, depth + 1)), depth + 1) | ||
}.reduce(And) | ||
} else if (rhs.size > 1) { | ||
rhs.values.map(_.reduceLeft(And)).map { e => | ||
toCNF(Or(toCNF(left, depth + 1), toCNF(e, 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 { e => | ||
toCNF(Or(toCNF(e, 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 { e => | ||
toCNF(Or(toCNF(left, depth + 1), toCNF(e, 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 | ||
} | ||
} | ||
|
||
def apply(plan: LogicalPlan): LogicalPlan = plan transform { | ||
case j @ Join(left, right, joinType, Some(joinCondition), hint) => | ||
val pushDownCandidates = | ||
splitConjunctivePredicates(toCNF(joinCondition)).filter(_.deterministic) | ||
val (leftFilterConditions, rest) = | ||
pushDownCandidates.partition(_.references.subsetOf(left.outputSet)) | ||
val (rightFilterConditions, _) = | ||
rest.partition(expr => expr.references.subsetOf(right.outputSet)) | ||
|
||
val newLeft = leftFilterConditions. | ||
reduceLeftOption(And).map(Filter(_, left)).getOrElse(left) | ||
val newRight = rightFilterConditions. | ||
reduceLeftOption(And).map(Filter(_, right)).getOrElse(right) | ||
|
||
joinType match { | ||
case _: InnerLike | LeftSemi => | ||
Join(newLeft, newRight, joinType, Some(joinCondition), hint) | ||
case RightOuter => | ||
Join(newLeft, right, RightOuter, Some(joinCondition), hint) | ||
case LeftOuter | LeftAnti | ExistenceJoin(_) => | ||
Join(left, newRight, joinType, Some(joinCondition), hint) | ||
case FullOuter => j | ||
case NaturalJoin(_) => sys.error("Untransformed NaturalJoin node") | ||
case UsingJoin(_, _) => sys.error("Untransformed Using join node") | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Combines two adjacent [[Limit]] operators into one, merging the | ||
* expressions into one single expression. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,12 +25,17 @@ import org.apache.spark.sql.catalyst.expressions._ | |
import org.apache.spark.sql.catalyst.plans._ | ||
import org.apache.spark.sql.catalyst.plans.logical._ | ||
import org.apache.spark.sql.catalyst.rules._ | ||
import org.apache.spark.sql.internal.SQLConf | ||
import org.apache.spark.sql.types.{BooleanType, IntegerType} | ||
import org.apache.spark.unsafe.types.CalendarInterval | ||
|
||
class FilterPushdownSuite extends PlanTest { | ||
|
||
object Optimize extends RuleExecutor[LogicalPlan] { | ||
|
||
override protected val blacklistedOnceBatches: Set[String] = | ||
Set("Push predicate through join by CNF") | ||
|
||
val batches = | ||
Batch("Subqueries", Once, | ||
EliminateSubqueryAliases) :: | ||
|
@@ -39,7 +44,9 @@ class FilterPushdownSuite extends PlanTest { | |
PushPredicateThroughNonJoin, | ||
BooleanSimplification, | ||
PushPredicateThroughJoin, | ||
CollapseProject) :: Nil | ||
CollapseProject) :: | ||
Batch("Push predicate through join by CNF", Once, | ||
PushPredicateThroughJoinByCNF) :: Nil | ||
} | ||
|
||
val attrA = 'a.int | ||
|
@@ -1230,4 +1237,154 @@ class FilterPushdownSuite extends PlanTest { | |
|
||
comparePlans(Optimize.execute(query.analyze), expected) | ||
} | ||
|
||
test("inner join: rewrite filter predicates to conjunctive normal form") { | ||
val x = testRelation.subquery('x) | ||
val y = testRelation.subquery('y) | ||
|
||
val originalQuery = { | ||
x.join(y) | ||
.where(("x.b".attr === "y.b".attr) | ||
&& (("x.a".attr > 3) && ("y.a".attr > 13) || ("x.a".attr > 1) && ("y.a".attr > 11))) | ||
} | ||
|
||
val optimized = Optimize.execute(originalQuery.analyze) | ||
val left = testRelation.where(('a > 3 || 'a > 1)).subquery('x) | ||
val right = testRelation.where('a > 13 || 'a > 11).subquery('y) | ||
val correctAnswer = | ||
left.join(right, condition = Some("x.b".attr === "y.b".attr | ||
&& (("x.a".attr > 3) && ("y.a".attr > 13) || ("x.a".attr > 1) && ("y.a".attr > 11)))) | ||
.analyze | ||
|
||
comparePlans(optimized, correctAnswer) | ||
} | ||
|
||
test("inner join: rewrite join predicates to conjunctive normal form") { | ||
val x = testRelation.subquery('x) | ||
val y = testRelation.subquery('y) | ||
|
||
val originalQuery = { | ||
x.join(y, condition = Some(("x.b".attr === "y.b".attr) | ||
&& (("x.a".attr > 3) && ("y.a".attr > 13) || ("x.a".attr > 1) && ("y.a".attr > 11)))) | ||
} | ||
|
||
val optimized = Optimize.execute(originalQuery.analyze) | ||
val left = testRelation.where('a > 3 || 'a > 1).subquery('x) | ||
val right = testRelation.where('a > 13 || 'a > 11).subquery('y) | ||
val correctAnswer = | ||
left.join(right, condition = Some("x.b".attr === "y.b".attr | ||
&& (("x.a".attr > 3) && ("y.a".attr > 13) || ("x.a".attr > 1) && ("y.a".attr > 11)))) | ||
.analyze | ||
|
||
comparePlans(optimized, correctAnswer) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Match the
|
||
} | ||
|
||
test("inner join: rewrite join predicates(with NOT predicate) to conjunctive normal form") { | ||
val x = testRelation.subquery('x) | ||
val y = testRelation.subquery('y) | ||
|
||
val originalQuery = { | ||
x.join(y, condition = Some(("x.b".attr === "y.b".attr) | ||
&& Not(("x.a".attr > 3) | ||
&& ("x.a".attr < 2 || ("y.a".attr > 13)) || ("x.a".attr > 1) && ("y.a".attr > 11)))) | ||
} | ||
|
||
val optimized = Optimize.execute(originalQuery.analyze) | ||
val left = testRelation.where('a <= 3 || 'a >= 2).subquery('x) | ||
val right = testRelation.subquery('y) | ||
val correctAnswer = | ||
left.join(right, condition = Some("x.b".attr === "y.b".attr | ||
&& (("x.a".attr <= 3) || (("x.a".attr >= 2) && ("y.a".attr <= 13))) | ||
&& (("x.a".attr <= 1) || ("y.a".attr <= 11)))) | ||
.analyze | ||
comparePlans(optimized, correctAnswer) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Match the
|
||
} | ||
|
||
test("left join: rewrite join predicates to conjunctive normal form") { | ||
val x = testRelation.subquery('x) | ||
val y = testRelation.subquery('y) | ||
|
||
val originalQuery = { | ||
x.join(y, joinType = LeftOuter, condition = Some(("x.b".attr === "y.b".attr) | ||
&& (("x.a".attr > 3) && ("y.a".attr > 13) || ("x.a".attr > 1) && ("y.a".attr > 11)))) | ||
} | ||
|
||
val optimized = Optimize.execute(originalQuery.analyze) | ||
val left = testRelation.subquery('x) | ||
val right = testRelation.where('a > 13 || 'a > 11).subquery('y) | ||
val correctAnswer = | ||
left.join(right, joinType = LeftOuter, condition = Some("x.b".attr === "y.b".attr | ||
&& (("x.a".attr > 3) && ("y.a".attr > 13) || ("x.a".attr > 1) && ("y.a".attr > 11)))) | ||
.analyze | ||
|
||
comparePlans(optimized, correctAnswer) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Match the
|
||
} | ||
|
||
test("right join: rewrite join predicates to conjunctive normal form") { | ||
val x = testRelation.subquery('x) | ||
val y = testRelation.subquery('y) | ||
|
||
val originalQuery = { | ||
x.join(y, joinType = RightOuter, condition = Some(("x.b".attr === "y.b".attr) | ||
&& (("x.a".attr > 3) && ("y.a".attr > 13) || ("x.a".attr > 1) && ("y.a".attr > 11)))) | ||
} | ||
|
||
val optimized = Optimize.execute(originalQuery.analyze) | ||
val left = testRelation.where('a > 3 || 'a > 1).subquery('x) | ||
val right = testRelation.subquery('y) | ||
val correctAnswer = | ||
left.join(right, joinType = RightOuter, condition = Some("x.b".attr === "y.b".attr | ||
&& (("x.a".attr > 3) && ("y.a".attr > 13) || ("x.a".attr > 1) && ("y.a".attr > 11)))) | ||
.analyze | ||
|
||
comparePlans(optimized, correctAnswer) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Match the
|
||
} | ||
|
||
test("inner join: rewrite to conjunctive normal form avoid generating too many predicates") { | ||
val x = testRelation.subquery('x) | ||
val y = testRelation.subquery('y) | ||
|
||
val originalQuery = { | ||
x.join(y, condition = Some(("x.b".attr === "y.b".attr) | ||
&& ((("x.a".attr > 3) && ("x.a".attr < 13) && ("y.c".attr <= 5)) | ||
|| (("y.a".attr > 2) && ("y.c".attr < 1))))) | ||
} | ||
|
||
val optimized = Optimize.execute(originalQuery.analyze) | ||
val left = testRelation.subquery('x) | ||
val right = testRelation.where('c <= 5 || ('a > 2 && 'c < 1)).subquery('y) | ||
val correctAnswer = left.join(right, condition = Some("x.b".attr === "y.b".attr | ||
&& ((("x.a".attr > 3) && ("x.a".attr < 13) && ("y.c".attr <= 5)) | ||
|| (("y.a".attr > 2) && ("y.c".attr < 1))))).analyze | ||
|
||
comparePlans(optimized, correctAnswer) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Match the
|
||
} | ||
|
||
test(s"Disable rewrite to CNF by setting ${SQLConf.MAX_REWRITING_CNF_DEPTH.key}=0") { | ||
val x = testRelation.subquery('x) | ||
val y = testRelation.subquery('y) | ||
|
||
val originalQuery = { | ||
x.join(y, condition = Some(("x.b".attr === "y.b".attr) | ||
&& ((("x.a".attr > 3) && ("x.a".attr < 13) && ("y.c".attr <= 5)) | ||
|| (("y.a".attr > 2) && ("y.c".attr < 1))))) | ||
} | ||
|
||
Seq(0, 10).foreach { depth => | ||
withSQLConf(SQLConf.MAX_REWRITING_CNF_DEPTH.key -> depth.toString) { | ||
val optimized = Optimize.execute(originalQuery.analyze) | ||
val (left, right) = if (depth == 0) { | ||
(testRelation.subquery('x), testRelation.subquery('y)) | ||
} else { | ||
(testRelation.subquery('x), | ||
testRelation.where('c <= 5 || ('a > 2 && 'c < 1)).subquery('y)) | ||
} | ||
val correctAnswer = left.join(right, condition = Some("x.b".attr === "y.b".attr | ||
&& ((("x.a".attr > 3) && ("x.a".attr < 13) && ("y.c".attr <= 5)) | ||
|| (("y.a".attr > 2) && ("y.c".attr < 1))))).analyze | ||
|
||
comparePlans(optimized, correctAnswer) | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does it apply to all the predicates? like when we pushdown filters to the data source?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, such as the step of scanning web_sales table for TPC-DS q85.
Before this pr:
After this pr: