-
Notifications
You must be signed in to change notification settings - Fork 629
[GLUTEN-12474][CORE] Preserve V1 write ordering for dynamic partition writes #12514
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3716fcf
test GLUTEN-12474
wForget 19d6bb6
Preserve V1 write ordering when inserting local sorts
wForget 0bbfcd5
add unit tests for other spark version
wForget 4d776e1
fix
wForget 9cfff27
fix spotless check
wForget bc63bb3
Enhance local sort handling for GlutenPlan support
wForget 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 |
|---|---|---|
|
|
@@ -16,11 +16,15 @@ | |
| */ | ||
| package org.apache.gluten.extension.columnar | ||
|
|
||
| import org.apache.gluten.execution.GlutenPlan | ||
| import org.apache.gluten.extension.columnar.heuristic.HeuristicTransform | ||
| import org.apache.gluten.sql.shims.SparkShimLoader | ||
|
|
||
| import org.apache.spark.sql.catalyst.expressions.SortOrder | ||
| import org.apache.spark.sql.catalyst.rules.Rule | ||
| import org.apache.spark.sql.execution.{SortExec, SparkPlan} | ||
| import org.apache.spark.sql.execution.{ColumnarWriteFilesExec, SortExec, SparkPlan} | ||
| import org.apache.spark.sql.execution.datasources.WriteFilesExec | ||
| import org.apache.spark.sql.internal.SQLConf | ||
|
|
||
| /** | ||
| * This rule is similar with `EnsureRequirements` but only handle local `SortExec`. | ||
|
|
@@ -33,25 +37,61 @@ import org.apache.spark.sql.execution.{SortExec, SparkPlan} | |
| object EnsureLocalSortRequirements extends Rule[SparkPlan] { | ||
| private lazy val transform: HeuristicTransform = HeuristicTransform.static() | ||
|
|
||
| private def numStaticPartitionCols(writeFiles: WriteFilesExec): Int = { | ||
| // HadoopFs writes include static partition columns in partitionColumns, while Hive writes may | ||
| // only include the partition columns that are present in the write query. | ||
| val resolver = SQLConf.get.resolver | ||
| val staticPartitionNames = writeFiles.staticPartitions.keys | ||
| writeFiles.partitionColumns.takeWhile { | ||
| partitionColumn => staticPartitionNames.exists(resolver(_, partitionColumn.name)) | ||
| }.size | ||
| } | ||
|
|
||
| private def requiredChildOrdering(plan: SparkPlan): Seq[Seq[SortOrder]] = { | ||
| plan match { | ||
| // V1Writes assumes that the logical ordering it prepared is preserved in the physical plan, | ||
| // so WriteFilesExec does not expose requiredChildOrdering itself. Gluten may invalidate that | ||
| // ordering when it replaces a SortAggregateExec with a hash aggregate. | ||
| case writeFiles: WriteFilesExec | ||
| if ColumnarWriteFilesExec.OnNoopLeafPath.unapply(writeFiles).isEmpty => | ||
| Seq( | ||
| SparkShimLoader.getSparkShims.getV1WriteRequiredOrdering( | ||
| writeFiles.child.output, | ||
| writeFiles.partitionColumns, | ||
| writeFiles.bucketSpec, | ||
| writeFiles.options, | ||
| numStaticPartitionCols(writeFiles))) | ||
| case _ => plan.requiredChildOrdering | ||
| } | ||
| } | ||
|
|
||
| private def addLocalSort( | ||
| plan: SparkPlan, | ||
| originalChild: SparkPlan, | ||
| requiredOrdering: Seq[SortOrder]): SparkPlan = { | ||
| // FIXME: HeuristicTransform is costly. Re-applying it may cause performance issues. | ||
| val newChild = SortExec(requiredOrdering, global = false, child = originalChild) | ||
| transform.apply(newChild) | ||
| (plan, originalChild) match { | ||
|
Contributor
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. I do not get this change, what's wrong with previous code?
Member
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. Avoid generating expensive plan like: Also fixed the failure at #12514 (comment) |
||
| case (_, child: GlutenPlan) if child.supportsColumnar => | ||
| transform.apply(newChild) | ||
| case (parent: GlutenPlan, _) if parent.supportsColumnar => | ||
| transform.apply(newChild) | ||
| case _ => | ||
| newChild | ||
| } | ||
| } | ||
|
|
||
| override def apply(plan: SparkPlan): SparkPlan = { | ||
| plan.transformUp { | ||
| case p => | ||
| val newChildren = p.children.zip(p.requiredChildOrdering).map { | ||
| val newChildren = p.children.zip(requiredChildOrdering(p)).map { | ||
| case (child, requiredOrdering) => | ||
| // If child.outputOrdering already satisfies the requiredOrdering, | ||
| // we do not need to sort. | ||
| if (SortOrder.orderingSatisfies(child.outputOrdering, requiredOrdering)) { | ||
| child | ||
| } else { | ||
| addLocalSort(child, requiredOrdering) | ||
| addLocalSort(p, child, requiredOrdering) | ||
| } | ||
| } | ||
| p.withNewChildren(newChildren) | ||
|
|
||
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
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
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
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
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.
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.
It seems we can make
WriteFilesExecsupports require child ordering native in Spark upstream.