-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-32216][SQL] Remove redundant ProjectExec #29031
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 all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cb88819
remove redundant project exec
allisonwang-db 4aa7bd8
address comments
allisonwang-db 69e67ff
address comments
allisonwang-db fac1c7e
address comments
allisonwang-db f4ac613
fix tests
allisonwang-db e7d3e1d
fix golden file
allisonwang-db b798f5b
add nullability check
allisonwang-db ae290bc
update
allisonwang-db 394126d
resolve conflict
allisonwang-db 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
99 changes: 99 additions & 0 deletions
99
sql/core/src/main/scala/org/apache/spark/sql/execution/RemoveRedundantProjects.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,99 @@ | ||
| /* | ||
| * 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.execution | ||
|
|
||
| import org.apache.spark.sql.catalyst.expressions.Attribute | ||
| import org.apache.spark.sql.catalyst.expressions.aggregate.{Final, PartialMerge} | ||
| import org.apache.spark.sql.catalyst.rules.Rule | ||
| import org.apache.spark.sql.execution.aggregate.BaseAggregateExec | ||
| import org.apache.spark.sql.execution.datasources.v2.DataSourceV2ScanExecBase | ||
| import org.apache.spark.sql.execution.window.WindowExec | ||
| import org.apache.spark.sql.internal.SQLConf | ||
|
|
||
| /** | ||
| * Remove redundant ProjectExec node from the spark plan. A ProjectExec node is redundant when | ||
| * - It has the same output attributes and orders as its child's output and the ordering of | ||
| * the attributes is required. | ||
| * - It has the same output attributes as its child's output when attribute output ordering | ||
| * is not required. | ||
| * This rule needs to be a physical rule because project nodes are useful during logical | ||
| * optimization to prune data. During physical planning, redundant project nodes can be removed | ||
| * to simplify the query plan. | ||
| */ | ||
| case class RemoveRedundantProjects(conf: SQLConf) extends Rule[SparkPlan] { | ||
cloud-fan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| def apply(plan: SparkPlan): SparkPlan = { | ||
| if (!conf.getConf(SQLConf.REMOVE_REDUNDANT_PROJECTS_ENABLED)) { | ||
| plan | ||
| } else { | ||
| removeProject(plan, true) | ||
| } | ||
| } | ||
|
|
||
| private def removeProject(plan: SparkPlan, requireOrdering: Boolean): SparkPlan = { | ||
| plan match { | ||
| case p @ ProjectExec(_, child) => | ||
| if (isRedundant(p, child, requireOrdering)) { | ||
| val newPlan = removeProject(child, requireOrdering) | ||
| newPlan.setLogicalLink(child.logicalLink.get) | ||
| newPlan | ||
| } else { | ||
| p.mapChildren(removeProject(_, false)) | ||
| } | ||
| case op: TakeOrderedAndProjectExec => | ||
| op.mapChildren(removeProject(_, false)) | ||
| case a: BaseAggregateExec => | ||
| // BaseAggregateExec require specific column ordering when mode is Final or PartialMerge. | ||
| // See comments in BaseAggregateExec inputAttributes method. | ||
| val keepOrdering = a.aggregateExpressions | ||
| .exists(ae => ae.mode.equals(Final) || ae.mode.equals(PartialMerge)) | ||
| a.mapChildren(removeProject(_, keepOrdering)) | ||
| case g: GenerateExec => g.mapChildren(removeProject(_, false)) | ||
| // JoinExec ordering requirement will inherit from its parent. If there is no ProjectExec in | ||
| // its ancestors, JoinExec should require output columns to be ordered. | ||
| case o => o.mapChildren(removeProject(_, requireOrdering)) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Check if the nullability change is positive. It catches the case when the project output | ||
| * attribute is not nullable, but the child output attribute is nullable. | ||
| */ | ||
| private def checkNullability(output: Seq[Attribute], childOutput: Seq[Attribute]): Boolean = | ||
| output.zip(childOutput).forall { case (attr1, attr2) => attr1.nullable || !attr2.nullable } | ||
|
|
||
| private def isRedundant( | ||
| project: ProjectExec, | ||
| child: SparkPlan, | ||
| requireOrdering: Boolean): Boolean = { | ||
| child match { | ||
| // If a DataSourceV2ScanExec node does not support columnar, a ProjectExec node is required | ||
| // to convert the rows to UnsafeRow. See DataSourceV2Strategy for more details. | ||
| case d: DataSourceV2ScanExecBase if !d.supportsColumnar => false | ||
| case _ => | ||
| if (requireOrdering) { | ||
| project.output.map(_.exprId.id) == child.output.map(_.exprId.id) && | ||
| checkNullability(project.output, child.output) | ||
| } else { | ||
| val orderedProjectOutput = project.output.sortBy(_.exprId.id) | ||
| val orderedChildOutput = child.output.sortBy(_.exprId.id) | ||
| orderedProjectOutput.map(_.exprId.id) == orderedChildOutput.map(_.exprId.id) && | ||
| checkNullability(orderedProjectOutput, orderedChildOutput) | ||
| } | ||
| } | ||
| } | ||
| } | ||
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
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.