-
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
Changes from all commits
6c916c2
ed9adc8
0a87228
1578f6c
a441604
5c11b94
33abbd5
b94bfc5
d8fd371
33ae658
746e0d1
8827d93
12f4ce4
963133e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 CatalogManager.SESSION_CATALOG_NAME(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,7 @@ import org.apache.spark.sql.catalyst.csv.{CSVHeaderChecker, CSVOptions, Univocit | |
import org.apache.spark.sql.catalyst.expressions.ExprUtils | ||
import org.apache.spark.sql.catalyst.json.{CreateJacksonParser, JacksonParser, JSONOptions} | ||
import org.apache.spark.sql.catalyst.util.FailureSafeParser | ||
import org.apache.spark.sql.connector.catalog.SupportsRead | ||
import org.apache.spark.sql.connector.catalog.{CatalogV2Util, SupportsCatalogOptions, SupportsRead} | ||
import org.apache.spark.sql.connector.catalog.TableCapability._ | ||
import org.apache.spark.sql.execution.command.DDLUtils | ||
import org.apache.spark.sql.execution.datasources.DataSource | ||
|
@@ -215,9 +215,22 @@ class DataFrameReader private[sql](sparkSession: SparkSession) extends Logging { | |
|
||
val finalOptions = sessionOptions ++ extraOptions.toMap ++ pathsOption | ||
val dsOptions = new CaseInsensitiveStringMap(finalOptions.asJava) | ||
val table = userSpecifiedSchema match { | ||
case Some(schema) => provider.getTable(dsOptions, schema) | ||
case _ => provider.getTable(dsOptions) | ||
val table = provider match { | ||
case _: SupportsCatalogOptions if userSpecifiedSchema.nonEmpty => | ||
throw new IllegalArgumentException( | ||
s"$source does not support user specified schema. Please don't specify the schema.") | ||
case hasCatalog: SupportsCatalogOptions => | ||
val ident = hasCatalog.extractIdentifier(dsOptions) | ||
val catalog = CatalogV2Util.getTableProviderCatalog( | ||
hasCatalog, | ||
sparkSession.sessionState.catalogManager, | ||
dsOptions) | ||
catalog.loadTable(ident) | ||
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 always call 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. if we call 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. We don't, we simply ignore it. If the TableProvider SupportsCatalogOptions, then we will always load the table through the catalog, therefore we don't need user options or partitioning info 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. then shall we fail or log a warning if schema is specified by users? BTW 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 don't have a strong opinion on whether to warn, fail, or ignore when there is a user-specified schema. Warnings are almost always ignored, so I'd rather fail and highlight the problem to the user. |
||
case _ => | ||
userSpecifiedSchema match { | ||
case Some(schema) => provider.getTable(dsOptions, schema) | ||
case _ => provider.getTable(dsOptions) | ||
} | ||
} | ||
import org.apache.spark.sql.execution.datasources.v2.DataSourceV2Implicits._ | ||
table match { | ||
|
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.
let's fail if the user specifies schema.