-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
[Spark] Define and use DeltaTableV2.startTransaction helper method #2053
Closed
ryan-johnson-databricks
wants to merge
1
commit into
delta-io:master
from
ryan-johnson-databricks:frj-delta-table-v2-helpers
Closed
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 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 |
---|---|---|
|
@@ -22,7 +22,7 @@ import java.{util => ju} | |
import scala.collection.JavaConverters._ | ||
import scala.collection.mutable | ||
|
||
import org.apache.spark.sql.delta.{ColumnWithDefaultExprUtils, DeltaColumnMapping, DeltaErrors, DeltaLog, DeltaOptions, DeltaTableIdentifier, DeltaTableUtils, DeltaTimeTravelSpec, GeneratedColumn, Snapshot} | ||
import org.apache.spark.sql.delta._ | ||
import org.apache.spark.sql.delta.commands.WriteIntoDelta | ||
import org.apache.spark.sql.delta.commands.cdc.CDCReader | ||
import org.apache.spark.sql.delta.metering.DeltaLogging | ||
|
@@ -32,10 +32,13 @@ import org.apache.hadoop.fs.Path | |
|
||
import org.apache.spark.sql.{DataFrame, SaveMode, SparkSession} | ||
import org.apache.spark.sql.catalyst.TableIdentifier | ||
import org.apache.spark.sql.catalyst.analysis.{ResolvedTable, UnresolvedTable} | ||
import org.apache.spark.sql.catalyst.catalog.{CatalogTable, CatalogTableType, CatalogUtils} | ||
import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan | ||
import org.apache.spark.sql.connector.catalog.{SupportsWrite, Table, TableCapability, TableCatalog, V2TableWithV1Fallback} | ||
import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._ | ||
import org.apache.spark.sql.connector.catalog.TableCapability._ | ||
import org.apache.spark.sql.connector.catalog.V1Table | ||
import org.apache.spark.sql.connector.expressions._ | ||
import org.apache.spark.sql.connector.write.{LogicalWriteInfo, SupportsDynamicOverwrite, SupportsOverwrite, SupportsTruncate, V1Write, WriteBuilder} | ||
import org.apache.spark.sql.errors.QueryCompilationErrors | ||
|
@@ -189,6 +192,23 @@ case class DeltaTableV2( | |
deltaLog, info.options, spark.sessionState.conf.useNullsForMissingDefaultColumnValues) | ||
} | ||
|
||
/** | ||
* Starts a transaction for this table, using the snapshot captured during table resolution. | ||
* | ||
* WARNING: Caller is responsible to ensure that table resolution was recent (e.g. if working with | ||
* [[DataFrame]] or [[DeltaTable]] API, where the table could have been resolved long ago). | ||
*/ | ||
def startTransactionWithInitialSnapshot(): OptimisticTransaction = | ||
startTransaction(Some(snapshot)) | ||
|
||
/** | ||
* Starts a transaction for this table, using Some provided snapshot, or a fresh snapshot if None | ||
* was provided. | ||
*/ | ||
def startTransaction(snapshotOpt: Option[Snapshot] = None): OptimisticTransaction = { | ||
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. s/snapshotOpt/snapshot |
||
deltaLog.startTransaction(snapshotOpt) | ||
} | ||
|
||
/** | ||
* Creates a V1 BaseRelation from this Table to allow read APIs to go through V1 DataSource code | ||
* paths. | ||
|
@@ -266,6 +286,31 @@ case class DeltaTableV2( | |
} | ||
} | ||
|
||
object DeltaTableV2 { | ||
/** Resolves a path into a DeltaTableV2, leveraging standard v2 table resolution. */ | ||
def apply(spark: SparkSession, tablePath: Path, cmd: String): DeltaTableV2 = | ||
resolve(spark, UnresolvedPathBasedDeltaTable(tablePath.toString, cmd), cmd) | ||
|
||
/** Resolves a table identifier into a DeltaTableV2, leveraging standard v2 table resolution. */ | ||
def apply(spark: SparkSession, tableId: TableIdentifier, cmd: String): DeltaTableV2 = | ||
resolve(spark, UnresolvedTable(tableId.nameParts, cmd, None), cmd) | ||
|
||
/** Applies standard v2 table resolution to an unresolved Delta table plan node */ | ||
def resolve(spark: SparkSession, unresolved: LogicalPlan, cmd: String): DeltaTableV2 = | ||
extractFrom(spark.sessionState.analyzer.ResolveRelations(unresolved), cmd) | ||
|
||
/** | ||
* Extracts the DeltaTableV2 from a resolved Delta table plan node, throwing "table not found" if | ||
* the node does not actually represent a resolved Delta table. | ||
*/ | ||
def extractFrom(plan: LogicalPlan, cmd: String): DeltaTableV2 = plan match { | ||
case ResolvedTable(_, _, d: DeltaTableV2, _) => d | ||
case ResolvedTable(_, _, t: V1Table, _) if DeltaTableUtils.isDeltaTable(t.catalogTable) => | ||
DeltaTableV2(SparkSession.active, new Path(t.v1Table.location), Some(t.v1Table)) | ||
case _ => throw DeltaErrors.notADeltaTableException(cmd) | ||
} | ||
} | ||
|
||
private class WriteIntoDeltaBuilder( | ||
log: DeltaLog, | ||
writeOptions: CaseInsensitiveStringMap, | ||
|
This file contains 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 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 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 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 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 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.
s/Some/some
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.
That was intentional... Some/None as in
Option
.