-
Notifications
You must be signed in to change notification settings - Fork 28.5k
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-29219][SQL] Introduce SupportsCatalogOptions for TableProvider #26913
Closed
+406
−44
Closed
Changes from 4 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
6c916c2
Interface definition
brkyvz ed9adc8
save implementation
brkyvz 0a87228
Added partitioning checks
brkyvz 1578f6c
Update SupportsCatalogOptions.java
brkyvz a441604
Added first set of tests
brkyvz 5c11b94
Added more tests
brkyvz 33abbd5
Merge branch 'catalogOptions' of github.com:brkyvz/spark into catalog…
brkyvz b94bfc5
Update SupportsCatalogOptionsSuite.scala
brkyvz d8fd371
Address comments
brkyvz 33ae658
save
brkyvz 746e0d1
implement for append and overwrite as well
brkyvz 8827d93
more tests
brkyvz 12f4ce4
Address comments
brkyvz 963133e
address comments
brkyvz 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
53 changes: 53 additions & 0 deletions
53
...catalyst/src/main/java/org/apache/spark/sql/connector/catalog/SupportsCatalogOptions.java
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* 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.connector.catalog; | ||
|
||
import org.apache.spark.annotation.Evolving; | ||
import org.apache.spark.sql.util.CaseInsensitiveStringMap; | ||
|
||
/** | ||
* An interface, which TableProviders can implement, to support table existence checks and creation | ||
* through a catalog, without having to use table identifiers. For example, when file based data | ||
* sources use the `DataFrameWriter.save(path)` method, the option `path` can translate to a | ||
* PathIdentifier. A catalog can then use this PathIdentifier to check the existence of a table, or | ||
* whether a table can be created at a given directory. | ||
*/ | ||
@Evolving | ||
public interface SupportsCatalogOptions extends TableProvider { | ||
/** | ||
* Return a {@link Identifier} instance that can identify a table for a DataSource given | ||
* DataFrame[Reader|Writer] options. | ||
* | ||
* @param options the user-specified options that can identify a table, e.g. file path, Kafka | ||
* topic name, etc. It's an immutable case-insensitive string-to-string map. | ||
*/ | ||
Identifier extractIdentifier(CaseInsensitiveStringMap options); | ||
|
||
/** | ||
* Return the name of a catalog that can be used to check the existence of, load, and create | ||
* a table for this DataSource given the identifier that will be extracted by | ||
* {@link #extractIdentifier(CaseInsensitiveStringMap) extractIdentifier}. A `null` value can | ||
* be used to defer to the V2SessionCatalog. | ||
* | ||
* @param options the user-specified options that can identify a table, e.g. file path, Kafka | ||
* topic name, etc. It's an immutable case-insensitive string-to-string map. | ||
*/ | ||
default String extractCatalog(CaseInsensitiveStringMap options) { | ||
return null; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -28,7 +28,7 @@ import org.apache.spark.sql.catalyst.catalog._ | |
import org.apache.spark.sql.catalyst.expressions.Literal | ||
import org.apache.spark.sql.catalyst.plans.logical.{AppendData, CreateTableAsSelect, InsertIntoStatement, LogicalPlan, OverwriteByExpression, OverwritePartitionsDynamic, ReplaceTableAsSelect} | ||
import org.apache.spark.sql.catalyst.util.CaseInsensitiveMap | ||
import org.apache.spark.sql.connector.catalog.{CatalogPlugin, Identifier, SupportsWrite, TableCatalog, TableProvider, V1Table} | ||
import org.apache.spark.sql.connector.catalog.{CatalogPlugin, Catalogs, Identifier, SupportsCatalogOptions, SupportsWrite, Table, TableCatalog, TableProvider, V1Table} | ||
import org.apache.spark.sql.connector.catalog.TableCapability._ | ||
import org.apache.spark.sql.connector.expressions.{BucketTransform, FieldReference, IdentityTransform, LiteralValue, Transform} | ||
import org.apache.spark.sql.execution.SQLExecution | ||
|
@@ -260,24 +260,44 @@ final class DataFrameWriter[T] private[sql](ds: Dataset[T]) { | |
import org.apache.spark.sql.execution.datasources.v2.DataSourceV2Implicits._ | ||
provider.getTable(dsOptions) match { | ||
case table: SupportsWrite if table.supports(BATCH_WRITE) => | ||
if (partitioningColumns.nonEmpty) { | ||
throw new AnalysisException("Cannot write data to TableProvider implementation " + | ||
"if partition columns are specified.") | ||
} | ||
lazy val relation = DataSourceV2Relation.create(table, dsOptions) | ||
mode match { | ||
case SaveMode.Append => | ||
verifyV2Partitioning(table) | ||
runCommand(df.sparkSession, "save") { | ||
AppendData.byName(relation, df.logicalPlan, extraOptions.toMap) | ||
} | ||
|
||
case SaveMode.Overwrite if table.supportsAny(TRUNCATE, OVERWRITE_BY_FILTER) => | ||
verifyV2Partitioning(table) | ||
// truncate the table | ||
runCommand(df.sparkSession, "save") { | ||
OverwriteByExpression.byName( | ||
relation, df.logicalPlan, Literal(true), extraOptions.toMap) | ||
} | ||
|
||
case other if classOf[SupportsCatalogOptions].isAssignableFrom(provider.getClass) => | ||
val catalogOptions = provider.asInstanceOf[SupportsCatalogOptions] | ||
rdblue marked this conversation as resolved.
Show resolved
Hide resolved
|
||
val ident = catalogOptions.extractIdentifier(dsOptions) | ||
val sessionState = df.sparkSession.sessionState | ||
val catalog = Option(catalogOptions.extractCatalog(dsOptions)) | ||
.map(Catalogs.load(_, sessionState.conf)) | ||
rdblue marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.getOrElse(sessionState.catalogManager.v2SessionCatalog) | ||
.asInstanceOf[TableCatalog] | ||
|
||
val location = Option(dsOptions.get("path")).map(TableCatalog.PROP_LOCATION -> _) | ||
|
||
runCommand(df.sparkSession, "save") { | ||
CreateTableAsSelect( | ||
rdblue marked this conversation as resolved.
Show resolved
Hide resolved
|
||
catalog, | ||
ident, | ||
getV2Transforms, | ||
df.queryExecution.analyzed, | ||
Map(TableCatalog.PROP_PROVIDER -> source) ++ location, | ||
extraOptions.toMap, | ||
ignoreIfExists = other == SaveMode.Ignore) | ||
} | ||
|
||
case other => | ||
throw new AnalysisException(s"TableProvider implementation $source cannot be " + | ||
s"written with $other mode, please use Append or Overwrite " + | ||
|
@@ -504,14 +524,6 @@ final class DataFrameWriter[T] private[sql](ds: Dataset[T]) { | |
|
||
|
||
private def saveAsTable(catalog: TableCatalog, ident: Identifier): Unit = { | ||
val partitioning = partitioningColumns.map { colNames => | ||
colNames.map(name => IdentityTransform(FieldReference(name))) | ||
}.getOrElse(Seq.empty[Transform]) | ||
val bucketing = bucketColumnNames.map { cols => | ||
Seq(BucketTransform(LiteralValue(numBuckets.get, IntegerType), cols.map(FieldReference(_)))) | ||
}.getOrElse(Seq.empty[Transform]) | ||
val partitionTransforms = partitioning ++ bucketing | ||
|
||
val tableOpt = try Option(catalog.loadTable(ident)) catch { | ||
case _: NoSuchTableException => None | ||
} | ||
|
@@ -526,13 +538,14 @@ final class DataFrameWriter[T] private[sql](ds: Dataset[T]) { | |
return saveAsTable(TableIdentifier(ident.name(), ident.namespace().headOption)) | ||
|
||
case (SaveMode.Append, Some(table)) => | ||
verifyV2Partitioning(table) | ||
AppendData.byName(DataSourceV2Relation.create(table), df.logicalPlan, extraOptions.toMap) | ||
|
||
case (SaveMode.Overwrite, _) => | ||
ReplaceTableAsSelect( | ||
catalog, | ||
ident, | ||
partitionTransforms, | ||
getV2Transforms, | ||
df.queryExecution.analyzed, | ||
Map(TableCatalog.PROP_PROVIDER -> source) ++ getLocationIfExists, | ||
extraOptions.toMap, | ||
|
@@ -545,7 +558,7 @@ final class DataFrameWriter[T] private[sql](ds: Dataset[T]) { | |
CreateTableAsSelect( | ||
catalog, | ||
ident, | ||
partitionTransforms, | ||
getV2Transforms, | ||
df.queryExecution.analyzed, | ||
Map(TableCatalog.PROP_PROVIDER -> source) ++ getLocationIfExists, | ||
extraOptions.toMap, | ||
|
@@ -623,6 +636,30 @@ final class DataFrameWriter[T] private[sql](ds: Dataset[T]) { | |
CreateTable(tableDesc, mode, Some(df.logicalPlan))) | ||
} | ||
|
||
/** Converts the provided partitioning and bucketing information to DataSourceV2 Transforms. */ | ||
private def getV2Transforms: Seq[Transform] = { | ||
rdblue marked this conversation as resolved.
Show resolved
Hide resolved
|
||
val partitioning = partitioningColumns.map { colNames => | ||
colNames.map(name => IdentityTransform(FieldReference(name))) | ||
}.getOrElse(Seq.empty[Transform]) | ||
val bucketing = bucketColumnNames.map { cols => | ||
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. shall we call |
||
Seq(BucketTransform(LiteralValue(numBuckets.get, IntegerType), cols.map(FieldReference(_)))) | ||
}.getOrElse(Seq.empty[Transform]) | ||
partitioning ++ bucketing | ||
} | ||
|
||
/** | ||
* For V2 DataSources, performs if the provided partitioning matches that of the table. | ||
* Partitioning information is not required when appending data to V2 tables. | ||
*/ | ||
private def verifyV2Partitioning(existingTable: Table): Unit = { | ||
rdblue marked this conversation as resolved.
Show resolved
Hide resolved
|
||
val v2Partitions = getV2Transforms | ||
if (v2Partitions.isEmpty) return | ||
require(v2Partitions.sameElements(existingTable.partitioning()), | ||
"The provided partitioning does not match of the table.\n" + | ||
s" - provided: ${v2Partitions.mkString(", ")}\n" + | ||
s" - table: ${existingTable.partitioning().mkString(", ")}") | ||
} | ||
|
||
/** | ||
* Saves the content of the `DataFrame` to an external database table via JDBC. In the case the | ||
* table already exists in the external database, behavior of this function depends on the | ||
|
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.
shall we by default return
CatalogManager.SESSION_CATALOG_NAME
instead of null?