-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-37287][SQL] Pull out dynamic partition and bucket sort from FileFormatWriter #37099
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
allisonwang-db
wants to merge
6
commits into
apache:master
from
allisonwang-db:spark-37287-v1-writes
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f486f18
v1 writes
allisonwang-db eab24ab
add aqe ordering and more tests
allisonwang-db 021ff95
turn on config
allisonwang-db 44662ed
update tests
allisonwang-db f5e05ae
apply AQE on top of DataWritingCommandExec
allisonwang-db aa00cda
fix AQE tests and add more Hive tests
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
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
137 changes: 137 additions & 0 deletions
137
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/V1Writes.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,137 @@ | ||
| /* | ||
| * 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.datasources | ||
|
|
||
| import org.apache.spark.sql.catalyst.SQLConfHelper | ||
| import org.apache.spark.sql.catalyst.catalog.BucketSpec | ||
| import org.apache.spark.sql.catalyst.expressions.{Ascending, Attribute, AttributeSet, BitwiseAnd, HiveHash, Literal, Pmod, SortOrder} | ||
| import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, Sort} | ||
| import org.apache.spark.sql.catalyst.plans.physical.HashPartitioning | ||
| import org.apache.spark.sql.catalyst.rules.Rule | ||
| import org.apache.spark.sql.execution.command.DataWritingCommand | ||
| import org.apache.spark.sql.internal.SQLConf | ||
|
|
||
| trait V1WriteCommand extends DataWritingCommand { | ||
| // Specify the required ordering for the V1 write command. `FileFormatWriter` will | ||
| // add SortExec if necessary when the requiredOrdering is empty. | ||
| def requiredOrdering: Seq[SortOrder] | ||
| } | ||
|
|
||
| /** | ||
| * A rule that adds logical sorts to V1 data writing commands. | ||
| */ | ||
| object V1Writes extends Rule[LogicalPlan] with SQLConfHelper { | ||
| override def apply(plan: LogicalPlan): LogicalPlan = { | ||
| if (conf.plannedWriteEnabled) { | ||
| plan.transformDown { | ||
| case write: V1WriteCommand => | ||
| val newQuery = prepareQuery(write, write.query) | ||
| write.withNewChildren(newQuery :: Nil) | ||
| } | ||
| } else { | ||
| plan | ||
| } | ||
| } | ||
|
|
||
| private def prepareQuery(write: V1WriteCommand, query: LogicalPlan): LogicalPlan = { | ||
| val requiredOrdering = write.requiredOrdering | ||
| val outputOrdering = query.outputOrdering | ||
| // Check if the ordering is already matched. It is needed to ensure the | ||
| // idempotency of the rule. | ||
| val orderingMatched = if (requiredOrdering.length > outputOrdering.length) { | ||
| false | ||
| } else { | ||
| requiredOrdering.zip(outputOrdering).forall { | ||
| case (requiredOrder, outputOrder) => requiredOrder.semanticEquals(outputOrder) | ||
| } | ||
| } | ||
| if (orderingMatched) { | ||
| query | ||
| } else { | ||
| Sort(requiredOrdering, global = false, query) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| object V1WritesUtils { | ||
|
|
||
| def getWriterBucketSpec( | ||
| bucketSpec: Option[BucketSpec], | ||
| dataColumns: Seq[Attribute], | ||
| options: Map[String, String]): Option[WriterBucketSpec] = { | ||
| bucketSpec.map { spec => | ||
| val bucketColumns = spec.bucketColumnNames.map(c => dataColumns.find(_.name == c).get) | ||
|
|
||
| if (options.getOrElse(BucketingUtils.optionForHiveCompatibleBucketWrite, "false") == | ||
| "true") { | ||
| // Hive bucketed table: use `HiveHash` and bitwise-and as bucket id expression. | ||
| // Without the extra bitwise-and operation, we can get wrong bucket id when hash value of | ||
| // columns is negative. See Hive implementation in | ||
| // `org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorUtils#getBucketNumber()`. | ||
| val hashId = BitwiseAnd(HiveHash(bucketColumns), Literal(Int.MaxValue)) | ||
| val bucketIdExpression = Pmod(hashId, Literal(spec.numBuckets)) | ||
|
|
||
| // The bucket file name prefix is following Hive, Presto and Trino conversion, so this | ||
| // makes sure Hive bucketed table written by Spark, can be read by other SQL engines. | ||
| // | ||
| // Hive: `org.apache.hadoop.hive.ql.exec.Utilities#getBucketIdFromFile()`. | ||
| // Trino: `io.trino.plugin.hive.BackgroundHiveSplitLoader#BUCKET_PATTERNS`. | ||
| val fileNamePrefix = (bucketId: Int) => f"$bucketId%05d_0_" | ||
| WriterBucketSpec(bucketIdExpression, fileNamePrefix) | ||
| } else { | ||
| // Spark bucketed table: use `HashPartitioning.partitionIdExpression` as bucket id | ||
| // expression, so that we can guarantee the data distribution is same between shuffle and | ||
| // bucketed data source, which enables us to only shuffle one side when join a bucketed | ||
| // table and a normal one. | ||
| val bucketIdExpression = HashPartitioning(bucketColumns, spec.numBuckets) | ||
| .partitionIdExpression | ||
| WriterBucketSpec(bucketIdExpression, (_: Int) => "") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| def getBucketSortColumns( | ||
| bucketSpec: Option[BucketSpec], | ||
| dataColumns: Seq[Attribute]): Seq[Attribute] = { | ||
| bucketSpec.toSeq.flatMap { | ||
| spec => spec.sortColumnNames.map(c => dataColumns.find(_.name == c).get) | ||
| } | ||
| } | ||
|
|
||
| def getSortOrder( | ||
| outputColumns: Seq[Attribute], | ||
| partitionColumns: Seq[Attribute], | ||
| bucketSpec: Option[BucketSpec], | ||
| options: Map[String, String]): Seq[SortOrder] = { | ||
| val partitionSet = AttributeSet(partitionColumns) | ||
| val dataColumns = outputColumns.filterNot(partitionSet.contains) | ||
| val writerBucketSpec = V1WritesUtils.getWriterBucketSpec(bucketSpec, dataColumns, options) | ||
| val sortColumns = V1WritesUtils.getBucketSortColumns(bucketSpec, dataColumns) | ||
|
|
||
| if (SQLConf.get.maxConcurrentOutputFileWriters > 0 && sortColumns.isEmpty) { | ||
| // Do not insert logical sort when concurrent output writers are enabled. | ||
| Seq.empty | ||
| } else { | ||
| // We should first sort by partition columns, then bucket id, and finally sorting columns. | ||
| // Note we do not need to convert empty string partition columns to null when sorting the | ||
| // columns since null and empty string values will be next to each other. | ||
| (partitionColumns ++writerBucketSpec.map(_.bucketIdExpression) ++ sortColumns) | ||
| .map(SortOrder(_, Ascending)) | ||
| } | ||
| } | ||
| } | ||
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.
Just brainstorming here, if we plan to add a requirement for partitioning, e.g. support shuffle before writing bucket table. Do we want to add a similar
RequiresDistributionAndOrderingas v2 now or not?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.
I think we can add one more method:
requiredPartitioning