-
Notifications
You must be signed in to change notification settings - Fork 29.2k
[SPARK-56703][SQL] Avoid redundant propagatedFilter aliases in PlanMerger
#55654
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
Draft
peter-toth
wants to merge
1
commit into
apache:master
Choose a base branch
from
peter-toth:SPARK-56703-avoid-unnecessary-propagated-filters
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -252,74 +252,12 @@ class PlanMerger( | |
| filterPropagationSupported: Boolean): Option[TryMergeResult] = { | ||
| checkIdenticalPlans(newPlan, cachedPlan).map(TryMergeResult(cachedPlan, _)).orElse( | ||
| (newPlan, cachedPlan) match { | ||
| case (np: Project, cp: Project) => | ||
| tryMergePlans(np.child, cp.child, filterPropagationSupported).map { | ||
| case TryMergeResult(mergedChild, npMapping, npFilter, cpFilter) => | ||
| val (mergedProjectList, newNPMapping) = | ||
| mergeNamedExpressions(np.projectList, cp.projectList, npMapping, npFilter, cpFilter) | ||
| TryMergeResult(Project(mergedProjectList, mergedChild), newNPMapping, npFilter, | ||
| cpFilter) | ||
| } | ||
| case (np, cp: Project) => | ||
| tryMergePlans(np, cp.child, filterPropagationSupported).map { | ||
| case TryMergeResult(mergedChild, npMapping, npFilter, cpFilter) => | ||
| val (mergedProjectList, newNPMapping) = | ||
| mergeNamedExpressions(np.output, cp.projectList, npMapping, npFilter, cpFilter) | ||
| TryMergeResult(Project(mergedProjectList, mergedChild), newNPMapping, npFilter, | ||
| cpFilter) | ||
| } | ||
| case (np: Project, cp) => | ||
| tryMergePlans(np.child, cp, filterPropagationSupported).map { | ||
| case TryMergeResult(mergedChild, npMapping, npFilter, cpFilter) => | ||
| val (mergedProjectList, newNPMapping) = | ||
| mergeNamedExpressions(np.projectList, cp.output, npMapping, npFilter, cpFilter) | ||
| TryMergeResult(Project(mergedProjectList, mergedChild), newNPMapping, npFilter, | ||
| cpFilter) | ||
| } | ||
|
|
||
| case (np: Aggregate, cp: Aggregate) if supportedAggregateMerge(np, cp) => | ||
| // Filter propagation into the aggregate is only safe when there is no grouping. | ||
| val childFilterPropagationSupported = filterPropagationEnabled && | ||
| np.groupingExpressions.isEmpty && cp.groupingExpressions.isEmpty | ||
| tryMergePlans(np.child, cp.child, childFilterPropagationSupported).flatMap { | ||
| case TryMergeResult(mergedChild, npMapping, None, None) => | ||
| val mappedNPGroupingExpression = | ||
| np.groupingExpressions.map(mapAttributes(_, npMapping)) | ||
| // Order of grouping expression does matter as merging different grouping orders can | ||
| // introduce "extra" shuffles/sorts that might not present in all of the original | ||
| // subqueries. | ||
| if (mappedNPGroupingExpression.map(_.canonicalized) == | ||
| cp.groupingExpressions.map(_.canonicalized)) { | ||
| val (mergedAggregateExpressions, newNPMapping) = | ||
| mergeNamedExpressions(np.aggregateExpressions, cp.aggregateExpressions, npMapping) | ||
| val mergedPlan = | ||
| Aggregate(cp.groupingExpressions, mergedAggregateExpressions, mergedChild) | ||
| Some(TryMergeResult(mergedPlan, newNPMapping)) | ||
| } else { | ||
| None | ||
| } | ||
| case TryMergeResult(mergedChild, npMapping, npFilterOpt, cpFilterOpt) => | ||
| // childFilterPropagationSupported guarantees both aggregates have no grouping, so | ||
| // the grouping-match check is skipped. | ||
| assert(childFilterPropagationSupported) | ||
|
|
||
| // Apply each propagated boolean attribute as a FILTER (WHERE ...) clause on the | ||
| // corresponding side's aggregate expressions. | ||
| // A None filter means the side's aggregate expressions already carry their individual | ||
| // FILTER attributes from a previous merge round and should be left unchanged. | ||
| // Filter propagation is consumed here and not passed further up. | ||
| val filteredNPAggregateExpressions = npFilterOpt.fold(np.aggregateExpressions) { | ||
| case (f, _) => applyFilterToAggregateExpressions(np.aggregateExpressions, f) | ||
| } | ||
| val filteredCPAggregateExpressions = cpFilterOpt.fold(cp.aggregateExpressions)( | ||
| applyFilterToAggregateExpressions(cp.aggregateExpressions, _)) | ||
| val (mergedAggregateExpressions, newNPMapping) = | ||
| mergeNamedExpressions(filteredNPAggregateExpressions, | ||
| filteredCPAggregateExpressions, npMapping) | ||
| val mergedPlan = Aggregate(Seq.empty, mergedAggregateExpressions, mergedChild) | ||
| Some(TryMergeResult(mergedPlan, newNPMapping)) | ||
| } | ||
|
|
||
| // Filter cases must precede the generic Project-peeling cases below. | ||
| // When filterPropagationSupported is true, a (Filter, Project) pair must be handled here so | ||
| // that the reuse check can find an already-aliased condition in the merged child Project. | ||
| // If (np, cp: Project) fired first, it would peel the Project layer and recurse with | ||
| // (Filter, ...), where no Project exists yet, causing a redundant alias to be created | ||
| // instead of reusing the existing one. | ||
| case (np: Filter, cp: Filter) => | ||
| tryMergePlans(np.child, cp.child, filterPropagationSupported).flatMap { | ||
| case TryMergeResult(mergedChild, npMapping, npFilter, cpFilter) => | ||
|
|
@@ -406,13 +344,28 @@ class PlanMerger( | |
| val newNPCondition = npFilter.fold(mappedNPCondition) { | ||
| case (f, _) => And(f, mappedNPCondition) | ||
| } | ||
| val newNPFilterAlias = | ||
| Alias(newNPCondition, s"propagatedFilter_${PlanMerger.newId}")() | ||
| val newNPFilter = newNPFilterAlias.toAttribute | ||
| val project = Project( | ||
| mergedChild.output.toList ++ Seq(newNPFilterAlias) ++ cpFilter.toSeq, | ||
| mergedChild) | ||
| TryMergeResult(project, npMapping, Some((newNPFilter, true)), cpFilter) | ||
| // If newNPCondition is already aliased in the child Project (e.g. a subsequent | ||
| // subplan whose filter matches one already propagated in a previous round), reuse | ||
| // the existing attribute instead of creating a redundant alias. | ||
| val existingNPFilter = mergedChild match { | ||
| case p: Project => p.projectList.collectFirst { | ||
| case a: Alias if a.child.canonicalized == newNPCondition.canonicalized => | ||
| a.toAttribute | ||
| } | ||
| case _ => None | ||
| } | ||
| existingNPFilter match { | ||
| case Some(reusedFilter) => | ||
| TryMergeResult(mergedChild, npMapping, Some((reusedFilter, false)), cpFilter) | ||
| case None => | ||
| val newNPFilterAlias = | ||
| Alias(newNPCondition, s"propagatedFilter_${PlanMerger.newId}")() | ||
| val newNPFilter = newNPFilterAlias.toAttribute | ||
| val project = Project( | ||
| mergedChild.output.toList ++ Seq(newNPFilterAlias) ++ cpFilter.toSeq, | ||
| mergedChild) | ||
| TryMergeResult(project, npMapping, Some((newNPFilter, true)), cpFilter) | ||
| } | ||
| } | ||
| case (np, cp: Filter) if filterPropagationSupported => | ||
| tryMergePlans(np, cp.child, filterPropagationSupported).collect { | ||
|
|
@@ -431,6 +384,74 @@ class PlanMerger( | |
| TryMergeResult(project, npMapping, npFilter, Some(newCPFilter)) | ||
| } | ||
|
|
||
| case (np: Project, cp: Project) => | ||
|
Contributor
Author
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. This is just a code move as Filter cases should come before Projects. |
||
| tryMergePlans(np.child, cp.child, filterPropagationSupported).map { | ||
| case TryMergeResult(mergedChild, npMapping, npFilter, cpFilter) => | ||
| val (mergedProjectList, newNPMapping) = | ||
| mergeNamedExpressions(np.projectList, cp.projectList, npMapping, npFilter, cpFilter) | ||
| TryMergeResult(Project(mergedProjectList, mergedChild), newNPMapping, npFilter, | ||
| cpFilter) | ||
| } | ||
| case (np, cp: Project) => | ||
| tryMergePlans(np, cp.child, filterPropagationSupported).map { | ||
| case TryMergeResult(mergedChild, npMapping, npFilter, cpFilter) => | ||
| val (mergedProjectList, newNPMapping) = | ||
| mergeNamedExpressions(np.output, cp.projectList, npMapping, npFilter, cpFilter) | ||
| TryMergeResult(Project(mergedProjectList, mergedChild), newNPMapping, npFilter, | ||
| cpFilter) | ||
| } | ||
| case (np: Project, cp) => | ||
| tryMergePlans(np.child, cp, filterPropagationSupported).map { | ||
| case TryMergeResult(mergedChild, npMapping, npFilter, cpFilter) => | ||
| val (mergedProjectList, newNPMapping) = | ||
| mergeNamedExpressions(np.projectList, cp.output, npMapping, npFilter, cpFilter) | ||
| TryMergeResult(Project(mergedProjectList, mergedChild), newNPMapping, npFilter, | ||
| cpFilter) | ||
| } | ||
|
|
||
| case (np: Aggregate, cp: Aggregate) if supportedAggregateMerge(np, cp) => | ||
| // Filter propagation into the aggregate is only safe when there is no grouping. | ||
| val childFilterPropagationSupported = filterPropagationEnabled && | ||
| np.groupingExpressions.isEmpty && cp.groupingExpressions.isEmpty | ||
| tryMergePlans(np.child, cp.child, childFilterPropagationSupported).flatMap { | ||
| case TryMergeResult(mergedChild, npMapping, None, None) => | ||
| val mappedNPGroupingExpression = | ||
| np.groupingExpressions.map(mapAttributes(_, npMapping)) | ||
| // Order of grouping expression does matter as merging different grouping orders can | ||
| // introduce "extra" shuffles/sorts that might not present in all of the original | ||
| // subqueries. | ||
| if (mappedNPGroupingExpression.map(_.canonicalized) == | ||
| cp.groupingExpressions.map(_.canonicalized)) { | ||
| val (mergedAggregateExpressions, newNPMapping) = | ||
| mergeNamedExpressions(np.aggregateExpressions, cp.aggregateExpressions, npMapping) | ||
| val mergedPlan = | ||
| Aggregate(cp.groupingExpressions, mergedAggregateExpressions, mergedChild) | ||
| Some(TryMergeResult(mergedPlan, newNPMapping)) | ||
| } else { | ||
| None | ||
| } | ||
| case TryMergeResult(mergedChild, npMapping, npFilterOpt, cpFilterOpt) => | ||
| // childFilterPropagationSupported guarantees both aggregates have no grouping, so | ||
| // the grouping-match check is skipped. | ||
| assert(childFilterPropagationSupported) | ||
|
|
||
| // Apply each propagated boolean attribute as a FILTER (WHERE ...) clause on the | ||
| // corresponding side's aggregate expressions. | ||
| // A None filter means the side's aggregate expressions already carry their individual | ||
| // FILTER attributes from a previous merge round and should be left unchanged. | ||
| // Filter propagation is consumed here and not passed further up. | ||
| val filteredNPAggregateExpressions = npFilterOpt.fold(np.aggregateExpressions) { | ||
| case (f, _) => applyFilterToAggregateExpressions(np.aggregateExpressions, f) | ||
| } | ||
| val filteredCPAggregateExpressions = cpFilterOpt.fold(cp.aggregateExpressions)( | ||
| applyFilterToAggregateExpressions(cp.aggregateExpressions, _)) | ||
| val (mergedAggregateExpressions, newNPMapping) = | ||
| mergeNamedExpressions(filteredNPAggregateExpressions, | ||
| filteredCPAggregateExpressions, npMapping) | ||
| val mergedPlan = Aggregate(Seq.empty, mergedAggregateExpressions, mergedChild) | ||
| Some(TryMergeResult(mergedPlan, newNPMapping)) | ||
| } | ||
|
|
||
| case (np: Join, cp: Join) if np.joinType == cp.joinType && np.hint == cp.hint => | ||
| tryMergePlans(np.left, cp.left, filterPropagationSupported).flatMap { | ||
| case TryMergeResult(mergedLeft, leftNPMapping, leftNPFilter, leftCPFilter) => | ||
|
|
||
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
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.
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.
This is the new logic symmetrical to (np: Filter, cp: Filter) case.