-
Notifications
You must be signed in to change notification settings - Fork 3k
Spark 4.1: Migrate to new version framework in DSv2 #15240
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
Open
aokolnychyi
wants to merge
1
commit into
apache:main
Choose a base branch
from
aokolnychyi:spark-dsv2-version-framework-rebased
base: main
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.
+1,936
−2,044
Open
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
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
118 changes: 118 additions & 0 deletions
118
...park-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveBranch.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,118 @@ | ||
| /* | ||
| * 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.analysis | ||
|
|
||
| import org.apache.iceberg.spark.PathIdentifier | ||
| import org.apache.iceberg.spark.SparkTableUtil | ||
| import org.apache.iceberg.spark.source.SparkTable | ||
| import org.apache.spark.sql.SparkSession | ||
| import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan | ||
| import org.apache.spark.sql.catalyst.plans.logical.RowLevelWrite | ||
| import org.apache.spark.sql.catalyst.plans.logical.V2WriteCommand | ||
| import org.apache.spark.sql.catalyst.rules.Rule | ||
| import org.apache.spark.sql.connector.catalog.Identifier | ||
| import org.apache.spark.sql.connector.write.RowLevelOperation | ||
| import org.apache.spark.sql.connector.write.RowLevelOperationInfoImpl | ||
| import org.apache.spark.sql.connector.write.RowLevelOperationTable | ||
| import org.apache.spark.sql.execution.datasources.v2.DataSourceV2Relation | ||
| import org.apache.spark.sql.execution.datasources.v2.ExtractV2Table | ||
| import org.apache.spark.sql.util.CaseInsensitiveStringMap | ||
|
|
||
| /** | ||
| * A rule that resolves the target branch for Iceberg reads and writes. | ||
| * <p> | ||
| * The branch must be determined and pinned during analysis. The current DSv2 framework | ||
| * doesn't provide access to all necessary options during the initial table loading, | ||
| * forcing us to finalize the branch selection in a custom analyzer rule. Future Spark | ||
| * versions will have a built-in mechanism to cleanly determine the target branch. | ||
| */ | ||
| case class ResolveBranch(spark: SparkSession) extends Rule[LogicalPlan] { | ||
|
|
||
| override def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators { | ||
| // row-level operations like DELETE, UPDATE, and MERGE | ||
| case w @ IcebergRowLevelWrite(table, operation, options) => | ||
| val branch = SparkTableUtil.determineWriteBranch(spark, table, options) | ||
| if (table.branch != branch) { | ||
| val newTable = table.copyWithBranch(branch) | ||
| val info = RowLevelOperationInfoImpl(operation.command, options) | ||
| val newOperation = newTable.newRowLevelOperationBuilder(info).build() | ||
| val newOperationTable = RowLevelOperationTable(newTable, newOperation) | ||
| val newTarget = transformPreservingType(w.table) { | ||
| case r @ ExtractV2Table(RowLevelOperationTable(_, _)) => r.copy(table = newOperationTable) | ||
| } | ||
| val newQuery = transformPreservingType(w.query) { | ||
| case r @ ExtractV2Table(RowLevelOperationTable(_, _)) => r.copy(table = newOperationTable) | ||
| } | ||
| w.withNewTable(newTarget).withNewQuery(newQuery) | ||
| } else { | ||
| w | ||
| } | ||
|
|
||
| // batch write operations like append or overwrite | ||
| case w: V2WriteCommand => | ||
| val newTarget = transformPreservingType(w.table) { | ||
| case r @ DataSourceV2Relation(table: SparkTable, _, _, _, options, _) => | ||
| val branch = SparkTableUtil.determineWriteBranch(spark, table, options) | ||
| if (table.branch != branch) r.copy(table = table.copyWithBranch(branch)) else r | ||
| } | ||
| w.withNewTable(newTarget) | ||
|
|
||
| // scan operations | ||
| // branch selector is added to identifier to ensure further refreshes point to correct branch | ||
| case r @ DataSourceV2Relation(table: SparkTable, _, _, Some(ident), options, None) => | ||
| val branch = SparkTableUtil.determineReadBranch(spark, table, options) | ||
| if (table.branch != branch) { | ||
| val branchSelector = s"branch_$branch" | ||
| val newIdent = ident match { | ||
| case path: PathIdentifier if path.location.contains("#") => | ||
| new PathIdentifier(path.location + "," + branchSelector) | ||
| case path: PathIdentifier => | ||
| new PathIdentifier(path.location + "#" + branchSelector) | ||
| case _ => | ||
| Identifier.of(ident.namespace :+ ident.name, branchSelector) | ||
| } | ||
| r.copy(table = table.copyWithBranch(branch), identifier = Some(newIdent)) | ||
| } else { | ||
| r | ||
| } | ||
| } | ||
|
|
||
| private def transformPreservingType[T <: LogicalPlan](plan: T)( | ||
| func: PartialFunction[LogicalPlan, LogicalPlan]): T = { | ||
| plan.transform(func).asInstanceOf[T] | ||
| } | ||
| } | ||
|
|
||
| // Iceberg specific extractor for row-level operations like DELETE, UPDATE, and MERGE | ||
| private object IcebergRowLevelWrite { | ||
| def unapply( | ||
| write: RowLevelWrite): Option[(SparkTable, RowLevelOperation, CaseInsensitiveStringMap)] = { | ||
| EliminateSubqueryAliases(write.table) match { | ||
| case DataSourceV2Relation( | ||
| RowLevelOperationTable(table: SparkTable, operation), | ||
| _, | ||
| _, | ||
| _, | ||
| options, | ||
| _) => | ||
| Some((table, operation, options)) | ||
| case _ => None | ||
| } | ||
| } | ||
| } |
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.
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.
Same explanation as in MERGE below.