-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-30314] Add identifier and catalog information to DataSourceV2Relation #26957
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
Changes from all commits
ac8b0b9
d4c9ba5
cf2cef2
374def9
9c784b3
6513824
f145973
9b8d4de
4625a57
9fd9302
4150d70
2bd689e
442a873
ef4e725
2730a49
7766129
eb54fa0
c80a155
42bf872
904278f
68c66f5
ee1a612
713afc9
e59a38a
600d289
df29683
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,40 @@ | ||
| /* | ||
| * 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.mockito.Mockito.{mock, when} | ||
|
|
||
| import org.apache.spark.SparkFunSuite | ||
| import org.apache.spark.sql.execution.datasources.v2.DataSourceV2Relation | ||
| import org.apache.spark.sql.types.StructType | ||
|
|
||
| class CatalogV2UtilSuite extends SparkFunSuite { | ||
| test("Load relation should encode the identifiers for V2Relations") { | ||
| val testCatalog = mock(classOf[TableCatalog]) | ||
| val ident = mock(classOf[Identifier]) | ||
| val table = mock(classOf[Table]) | ||
| when(table.schema()).thenReturn(mock(classOf[StructType])) | ||
| when(testCatalog.loadTable(ident)).thenReturn(table) | ||
| val r = CatalogV2Util.loadRelation(testCatalog, ident) | ||
| assert(r.isDefined) | ||
| assert(r.get.isInstanceOf[DataSourceV2Relation]) | ||
| val v2Relation = r.get.asInstanceOf[DataSourceV2Relation] | ||
| assert(v2Relation.catalog.exists(_ == testCatalog)) | ||
| assert(v2Relation.identifier.exists(_ == ident)) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -258,20 +258,20 @@ final class DataFrameWriter[T] private[sql](ds: Dataset[T]) { | |
| val dsOptions = new CaseInsensitiveStringMap(options.asJava) | ||
|
|
||
| import org.apache.spark.sql.execution.datasources.v2.DataSourceV2Implicits._ | ||
| val catalogManager = df.sparkSession.sessionState.catalogManager | ||
| mode match { | ||
| case SaveMode.Append | SaveMode.Overwrite => | ||
| val table = provider match { | ||
| val (table, catalog, ident) = provider match { | ||
| case supportsExtract: SupportsCatalogOptions => | ||
| val ident = supportsExtract.extractIdentifier(dsOptions) | ||
| val sessionState = df.sparkSession.sessionState | ||
| val catalog = CatalogV2Util.getTableProviderCatalog( | ||
| supportsExtract, sessionState.catalogManager, dsOptions) | ||
| supportsExtract, catalogManager, dsOptions) | ||
|
|
||
| catalog.loadTable(ident) | ||
| (catalog.loadTable(ident), Some(catalog), Some(ident)) | ||
| case tableProvider: TableProvider => | ||
| val t = tableProvider.getTable(dsOptions) | ||
| if (t.supports(BATCH_WRITE)) { | ||
| t | ||
| (t, None, None) | ||
| } else { | ||
| // Streaming also uses the data source V2 API. So it may be that the data source | ||
| // implements v2, but has no v2 implementation for batch writes. In that case, we | ||
|
|
@@ -280,7 +280,7 @@ final class DataFrameWriter[T] private[sql](ds: Dataset[T]) { | |
| } | ||
| } | ||
|
|
||
| val relation = DataSourceV2Relation.create(table, dsOptions) | ||
| val relation = DataSourceV2Relation.create(table, catalog, ident, dsOptions) | ||
| checkPartitioningMatchesV2Table(table) | ||
| if (mode == SaveMode.Append) { | ||
| runCommand(df.sparkSession, "save") { | ||
|
|
@@ -299,9 +299,8 @@ final class DataFrameWriter[T] private[sql](ds: Dataset[T]) { | |
| provider match { | ||
| case supportsExtract: SupportsCatalogOptions => | ||
| val ident = supportsExtract.extractIdentifier(dsOptions) | ||
| val sessionState = df.sparkSession.sessionState | ||
| val catalog = CatalogV2Util.getTableProviderCatalog( | ||
| supportsExtract, sessionState.catalogManager, dsOptions) | ||
| supportsExtract, catalogManager, dsOptions) | ||
|
|
||
| val location = Option(dsOptions.get("path")).map(TableCatalog.PROP_LOCATION -> _) | ||
|
|
||
|
|
@@ -419,7 +418,7 @@ final class DataFrameWriter[T] private[sql](ds: Dataset[T]) { | |
| case _: V1Table => | ||
| return insertInto(TableIdentifier(ident.name(), ident.namespace().headOption)) | ||
| case t => | ||
| DataSourceV2Relation.create(t) | ||
| DataSourceV2Relation.create(t, Some(catalog), Some(ident)) | ||
| } | ||
|
|
||
| val command = mode match { | ||
|
|
@@ -554,12 +553,13 @@ final class DataFrameWriter[T] private[sql](ds: Dataset[T]) { | |
| } | ||
|
|
||
| val command = (mode, tableOpt) match { | ||
| case (_, Some(table: V1Table)) => | ||
| case (_, Some(_: V1Table)) => | ||
| return saveAsTable(TableIdentifier(ident.name(), ident.namespace().headOption)) | ||
|
|
||
| case (SaveMode.Append, Some(table)) => | ||
| checkPartitioningMatchesV2Table(table) | ||
| AppendData.byName(DataSourceV2Relation.create(table), df.logicalPlan, extraOptions.toMap) | ||
yuchenhuo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| val v2Relation = DataSourceV2Relation.create(table, Some(catalog), Some(ident)) | ||
| AppendData.byName(v2Relation, df.logicalPlan, extraOptions.toMap) | ||
|
|
||
| case (SaveMode.Overwrite, _) => | ||
|
Contributor
Author
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. A little curious why
Contributor
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. It drops and recreates a table. It's a DDL operation instead of DML
Contributor
Author
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. Still probably a dumb question. Why does DDL/DML affects how we generate the query plan? I'm asking this because in the
Contributor
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. in save, we don't update table information in a catalog. saveAsTable updates the catalog, therefore is doing different work. So we need to do a separate operation |
||
| ReplaceTableAsSelect( | ||
|
|
||
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.
You probably want the catalog identifier here too, not the plugin