-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-32302][SQL] Partially push down disjunctive predicates through Join/Partitions #29101
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
Closed
Closed
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
16c78d6
better solution for pushing extra predicates through join
gengliangwang 19fa997
add files
gengliangwang bfa2bc4
address comments
gengliangwang cb16660
rename test suite
gengliangwang 543bd69
fix style
gengliangwang 452f3c9
address more comments
gengliangwang d37c21c
address comment
gengliangwang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 0 additions & 68 deletions
68
.../src/main/scala/org/apache/spark/sql/catalyst/optimizer/PushCNFPredicateThroughJoin.scala
This file was deleted.
Oops, something went wrong.
113 changes: 113 additions & 0 deletions
113
...rc/main/scala/org/apache/spark/sql/catalyst/optimizer/PushExtraPredicateThroughJoin.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.catalyst.optimizer | ||
|
|
||
| import org.apache.spark.sql.catalyst.expressions.{And, Expression, PredicateHelper} | ||
| import org.apache.spark.sql.catalyst.plans._ | ||
| import org.apache.spark.sql.catalyst.plans.logical.{Filter, Join, LogicalPlan} | ||
| import org.apache.spark.sql.catalyst.rules.Rule | ||
| import org.apache.spark.sql.catalyst.trees.TreeNodeTag | ||
|
|
||
| /** | ||
| * Try converting join condition to conjunctive normal form expression so that more predicates may | ||
gengliangwang marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * be able to be pushed down. | ||
| * To avoid expanding the join condition, the join condition will be kept in the original form even | ||
| * when predicate pushdown happens. | ||
| */ | ||
| object PushExtraPredicateThroughJoin extends Rule[LogicalPlan] with PredicateHelper { | ||
|
|
||
| private val processedJoinConditionTag = TreeNodeTag[Expression]("processedJoinCondition") | ||
|
|
||
| private def canPushThrough(joinType: JoinType): Boolean = joinType match { | ||
| case _: InnerLike | LeftSemi | RightOuter | LeftOuter | LeftAnti | ExistenceJoin(_) => true | ||
| case _ => false | ||
| } | ||
|
|
||
| /** | ||
| * Splits join condition expressions or filter predicates (on a given join's output) into three | ||
| * categories based on the attributes required to evaluate them. Note that we explicitly exclude | ||
| * non-deterministic (i.e., stateful) condition expressions in canEvaluateInLeft or | ||
| * canEvaluateInRight to prevent pushing these predicates on either side of the join. | ||
| * | ||
| * @return (canEvaluateInLeft, canEvaluateInRight, haveToEvaluateInBoth) | ||
| */ | ||
| protected def extractConvertibleFilters( | ||
gengliangwang marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| condition: Seq[Expression], | ||
gengliangwang marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| left: LogicalPlan, | ||
| right: LogicalPlan): (Seq[Expression], Seq[Expression], Seq[Expression]) = { | ||
| val (pushDownCandidates, nonDeterministic) = condition.partition(_.deterministic) | ||
| val (leftEvaluateCondition, rest) = | ||
| pushDownCandidates.partition(_.references.subsetOf(left.outputSet)) | ||
| val (rightEvaluateCondition, commonCondition) = | ||
| rest.partition(expr => expr.references.subsetOf(right.outputSet)) | ||
|
|
||
| // For the predicates in `commonCondition`, it is still possible to find sub-predicates which | ||
| // are able to be pushed down. | ||
| val leftExtraCondition = | ||
| commonCondition.flatMap(convertibleFilter(_, left.outputSet)) | ||
|
|
||
| val rightExtraCondition = | ||
| commonCondition.flatMap(convertibleFilter(_, right.outputSet)) | ||
|
|
||
| // To avoid expanding the join condition into conjunctive normal form and making the size | ||
| // of codegen much larger, `commonCondition` will be kept as original form in the new join | ||
| // condition. | ||
| (leftEvaluateCondition ++ leftExtraCondition, rightEvaluateCondition ++ rightExtraCondition, | ||
| commonCondition ++ nonDeterministic) | ||
| } | ||
|
|
||
| def apply(plan: LogicalPlan): LogicalPlan = plan transform { | ||
| case j @ Join(left, right, joinType, Some(joinCondition), hint) | ||
| if canPushThrough(joinType) => | ||
| val filtersOfBothSide = splitConjunctivePredicates(joinCondition).filter { f => | ||
| f.deterministic && f.references.nonEmpty && | ||
| !f.references.subsetOf(left.outputSet) && !f.references.subsetOf(right.outputSet) | ||
| } | ||
| val leftExtraCondition = | ||
| filtersOfBothSide.flatMap(convertibleFilter(_, left.outputSet)) | ||
|
|
||
| val rightExtraCondition = | ||
| filtersOfBothSide.flatMap(convertibleFilter(_, right.outputSet)) | ||
|
|
||
| val alreadyProcessed = j.getTagValue(processedJoinConditionTag).exists { condition => | ||
gengliangwang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| condition.semanticEquals(joinCondition) | ||
| } | ||
|
|
||
| if ((leftExtraCondition.isEmpty && rightExtraCondition.isEmpty) || alreadyProcessed) { | ||
| j | ||
| } else { | ||
| lazy val newLeft = | ||
| leftExtraCondition.reduceLeftOption(And).map(Filter(_, left)).getOrElse(left) | ||
cloud-fan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| lazy val newRight = | ||
| rightExtraCondition.reduceLeftOption(And).map(Filter(_, right)).getOrElse(right) | ||
|
|
||
| val newJoin = 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 other => | ||
| throw new IllegalStateException(s"Unexpected join type: $other") | ||
| } | ||
| newJoin.setTagValue(processedJoinConditionTag, joinCondition) | ||
| newJoin | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.