-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-27724][SQL] Implement REPLACE TABLE and REPLACE TABLE AS SELECT with V2 #24798
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
9a21551
266784e
baeabc8
bc8d3b5
6c958b9
8c0270f
95ba92d
08f115e
89aea5e
a9142e9
842886e
b68df7c
2bf4b5f
80dc0cc
71f29ed
a333df8
4011a8b
aebf767
6231f6a
96b6db6
295ff75
d7e1e67
deaf255
0b5c029
581dba2
be04476
609eb9c
2f6e0b6
05a827d
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 |
|---|---|---|
|
|
@@ -113,6 +113,14 @@ statement | |
| (AS? query)? #createHiveTable | ||
| | CREATE TABLE (IF NOT EXISTS)? target=tableIdentifier | ||
| LIKE source=tableIdentifier locationSpec? #createTableLike | ||
| | replaceTableHeader ('(' colTypeList ')')? tableProvider | ||
| ((OPTIONS options=tablePropertyList) | | ||
|
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. Should OPTIONS be supported in v2? Right now, we copy options into table properties because v2 has no separate options. I also think it is confusing to users that there are table properties and options.
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. In general I copied this entirely from the equivalent create table statement. How does the syntax for
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.
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. True, it should be the same a |
||
| (PARTITIONED BY partitioning=transformList) | | ||
| bucketSpec | | ||
|
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. Should bucketing be added using
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. Same as #24798 (comment) - this is copied from the create table spec. |
||
| locationSpec | | ||
| (COMMENT comment=STRING) | | ||
| (TBLPROPERTIES tableProps=tablePropertyList))* | ||
| (AS? query)? #replaceTable | ||
| | ANALYZE TABLE tableIdentifier partitionSpec? COMPUTE STATISTICS | ||
| (identifier | FOR COLUMNS identifierSeq | FOR ALL COLUMNS)? #analyze | ||
| | ALTER TABLE multipartIdentifier | ||
|
|
@@ -261,6 +269,10 @@ createTableHeader | |
| : CREATE TEMPORARY? EXTERNAL? TABLE (IF NOT EXISTS)? multipartIdentifier | ||
| ; | ||
|
|
||
| replaceTableHeader | ||
|
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. I'm worried about creating new SQL syntax in Spark. AFAIK a similar syntax is This is not a standard SQL syntax, so it's not surprising to see that Oracle doesn't support it. If Spark want a API for replace table, I think it's more reasonable to follow DB2 and BigQuery here.
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. I'm fine using
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. So do we remove the existing support for the Or do we support: and then throw
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.
We don't support
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. I'd say let's remove Then, we should add an option to add
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. I think that makes sense, though the current implementation doesn't have a rule supporting
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 it isn't supported, then we can throw an exception.
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. Since that clause isn't supported in the |
||
| : (CREATE OR)? REPLACE TABLE multipartIdentifier | ||
| ; | ||
|
|
||
| bucketSpec | ||
| : CLUSTERED BY identifierList | ||
| (SORTED BY orderedIdentifierList)? | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| /* | ||
| * 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.catalog.v2; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import org.apache.spark.sql.catalog.v2.expressions.Transform; | ||
| import org.apache.spark.sql.catalyst.analysis.NoSuchNamespaceException; | ||
| import org.apache.spark.sql.catalyst.analysis.NoSuchTableException; | ||
| import org.apache.spark.sql.catalyst.analysis.TableAlreadyExistsException; | ||
| import org.apache.spark.sql.sources.v2.StagedTable; | ||
| import org.apache.spark.sql.sources.v2.SupportsWrite; | ||
| import org.apache.spark.sql.sources.v2.writer.BatchWrite; | ||
| import org.apache.spark.sql.sources.v2.writer.WriterCommitMessage; | ||
| import org.apache.spark.sql.types.StructType; | ||
| import org.apache.spark.sql.util.CaseInsensitiveStringMap; | ||
|
|
||
| /** | ||
| * An optional mix-in for implementations of {@link TableCatalog} that support staging creation of | ||
| * the a table before committing the table's metadata along with its contents in CREATE TABLE AS | ||
| * SELECT or REPLACE TABLE AS SELECT operations. | ||
| * <p> | ||
| * It is highly recommended to implement this trait whenever possible so that CREATE TABLE AS | ||
| * SELECT and REPLACE TABLE AS SELECT operations are atomic. For example, when one runs a REPLACE | ||
| * TABLE AS SELECT operation, if the catalog does not implement this trait, the planner will first | ||
| * drop the table via {@link TableCatalog#dropTable(Identifier)}, then create the table via | ||
| * {@link TableCatalog#createTable(Identifier, StructType, Transform[], Map)}, and then perform | ||
| * the write via {@link SupportsWrite#newWriteBuilder(CaseInsensitiveStringMap)}. However, if the | ||
| * write operation fails, the catalog will have already dropped the table, and the planner cannot | ||
| * roll back the dropping of the table. | ||
| * <p> | ||
| * If the catalog implements this plugin, the catalog can implement the methods to "stage" the | ||
| * creation and the replacement of a table. After the table's | ||
|
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. Looks like this doc is unfinished.
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. It's not, the comment continues to the links in the following line.
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. Oops, I misread it. Thanks! |
||
| * {@link BatchWrite#commit(WriterCommitMessage[])} is called, | ||
| * {@link StagedTable#commitStagedChanges()} is called, at which point the staged table can | ||
| * complete both the data write and the metadata swap operation atomically. | ||
| */ | ||
| public interface StagingTableCatalog extends TableCatalog { | ||
|
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. Can we move this API out to a new PR and implement atomic CTAS? I think REPLACE TABLE is not a blocker to this API and we don't have to do them together. This can also help us move forward faster, since designing a new SQL syntax (REPLACE TABLE) usually needs more time to get consensus.
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. I would think that
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. Also thinking about this a bit more, perhaps the reason why Since we don't have support for specifically opening and closing transactions, we have to support this use case via an atomic |
||
|
|
||
| /** | ||
| * Stage the creation of a table, preparing it to be committed into the metastore. | ||
| * <p> | ||
| * When the table is committed, the contents of any writes performed by the Spark planner are | ||
| * committed along with the metadata about the table passed into this method's arguments. If the | ||
| * table exists when this method is called, the method should throw an exception accordingly. If | ||
| * another process concurrently creates the table before this table's staged changes are | ||
| * committed, an exception should be thrown by {@link StagedTable#commitStagedChanges()}. | ||
| * | ||
| * @param ident a table identifier | ||
| * @param schema the schema of the new table, as a struct type | ||
| * @param partitions transforms to use for partitioning data in the table | ||
| * @param properties a string map of table properties | ||
| * @return metadata for the new table | ||
| * @throws TableAlreadyExistsException If a table or view already exists for the identifier | ||
| * @throws UnsupportedOperationException If a requested partition transform is not supported | ||
| * @throws NoSuchNamespaceException If the identifier namespace does not exist (optional) | ||
| */ | ||
| StagedTable stageCreate( | ||
| Identifier ident, | ||
| StructType schema, | ||
| Transform[] partitions, | ||
| Map<String, String> properties) throws TableAlreadyExistsException, NoSuchNamespaceException; | ||
|
|
||
| /** | ||
| * Stage the replacement of a table, preparing it to be committed into the metastore when the | ||
| * returned table's {@link StagedTable#commitStagedChanges()} is called. | ||
| * <p> | ||
| * When the table is committed, the contents of any writes performed by the Spark planner are | ||
| * committed along with the metadata about the table passed into this method's arguments. If the | ||
| * table exists, the metadata and the contents of this table replace the metadata and contents of | ||
| * the existing table. If a concurrent process commits changes to the table's data or metadata | ||
| * while the write is being performed but before the staged changes are committed, the catalog | ||
| * can decide whether to move forward with the table replacement anyways or abort the commit | ||
| * operation. | ||
| * <p> | ||
| * If the table does not exist, committing the staged changes should fail with | ||
| * {@link NoSuchTableException}. This differs from the semantics of | ||
| * {@link #stageCreateOrReplace(Identifier, StructType, Transform[], Map)}, which should create | ||
| * the table in the data source if the table does not exist at the time of committing the | ||
| * operation. | ||
| * | ||
| * @param ident a table identifier | ||
| * @param schema the schema of the new table, as a struct type | ||
| * @param partitions transforms to use for partitioning data in the table | ||
| * @param properties a string map of table properties | ||
| * @return metadata for the new table | ||
| * @throws UnsupportedOperationException If a requested partition transform is not supported | ||
| * @throws NoSuchNamespaceException If the identifier namespace does not exist (optional) | ||
|
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. I think the implementation should throw |
||
| * @throws NoSuchTableException If the table does not exist | ||
| */ | ||
| StagedTable stageReplace( | ||
| Identifier ident, | ||
| StructType schema, | ||
| Transform[] partitions, | ||
| Map<String, String> properties) throws NoSuchNamespaceException, NoSuchTableException; | ||
|
|
||
| /** | ||
| * Stage the creation or replacement of a table, preparing it to be committed into the metastore | ||
| * when the returned table's {@link StagedTable#commitStagedChanges()} is called. | ||
| * <p> | ||
| * When the table is committed, the contents of any writes performed by the Spark planner are | ||
| * committed along with the metadata about the table passed into this method's arguments. If the | ||
| * table exists, the metadata and the contents of this table replace the metadata and contents of | ||
| * the existing table. If a concurrent process commits changes to the table's data or metadata | ||
| * while the write is being performed but before the staged changes are committed, the catalog | ||
| * can decide whether to move forward with the table replacement anyways or abort the commit | ||
| * operation. | ||
| * <p> | ||
| * If the table does not exist when the changes are committed, the table should be created in the | ||
| * backing data source. This differs from the expected semantics of | ||
| * {@link #stageReplace(Identifier, StructType, Transform[], Map)}, which should fail when | ||
| * the staged changes are committed but the table doesn't exist at commit time. | ||
| * | ||
| * @param ident a table identifier | ||
| * @param schema the schema of the new table, as a struct type | ||
| * @param partitions transforms to use for partitioning data in the table | ||
| * @param properties a string map of table properties | ||
| * @return metadata for the new table | ||
| * @throws UnsupportedOperationException If a requested partition transform is not supported | ||
| * @throws NoSuchNamespaceException If the identifier namespace does not exist (optional) | ||
| */ | ||
| StagedTable stageCreateOrReplace( | ||
| Identifier ident, | ||
| StructType schema, | ||
| Transform[] partitions, | ||
| Map<String, String> properties) throws NoSuchNamespaceException; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* | ||
| * 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.sources.v2; | ||
|
|
||
| import java.util.Map; | ||
| import org.apache.spark.sql.catalog.v2.Identifier; | ||
| import org.apache.spark.sql.catalog.v2.StagingTableCatalog; | ||
| import org.apache.spark.sql.catalog.v2.expressions.Transform; | ||
| import org.apache.spark.sql.types.StructType; | ||
| import org.apache.spark.sql.util.CaseInsensitiveStringMap; | ||
|
|
||
| /** | ||
| * Represents a table which is staged for being committed to the metastore. | ||
| * <p> | ||
| * This is used to implement atomic CREATE TABLE AS SELECT and REPLACE TABLE AS SELECT queries. The | ||
| * planner will create one of these via | ||
| * {@link StagingTableCatalog#stageCreate(Identifier, StructType, Transform[], Map)} or | ||
| * {@link StagingTableCatalog#stageReplace(Identifier, StructType, Transform[], Map)} to prepare the | ||
| * table for being written to. This table should usually implement {@link SupportsWrite}. A new | ||
| * writer will be constructed via {@link SupportsWrite#newWriteBuilder(CaseInsensitiveStringMap)}, | ||
| * and the write will be committed. The job concludes with a call to {@link #commitStagedChanges()}, | ||
| * at which point implementations are expected to commit the table's metadata into the metastore | ||
| * along with the data that was written by the writes from the write builder this table created. | ||
| */ | ||
| public interface StagedTable extends Table { | ||
|
|
||
| /** | ||
| * Finalize the creation or replacement of this table. | ||
| */ | ||
| void commitStagedChanges(); | ||
|
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. It's not immediately obvious if this API belongs in
If we wanted to move the swap implementation behind 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. I like this. So the write's commit stashes changes in the staged table, which can finish or roll back.
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. This also solves the problem of where to document how to complete the changes staged in a |
||
|
|
||
| /** | ||
| * Abort the changes that were staged, both in metadata and from temporary outputs of this | ||
| * table's writers. | ||
| */ | ||
| void abortStagedChanges(); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /* | ||
| * 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.spark.sql.AnalysisException | ||
| import org.apache.spark.sql.catalog.v2.Identifier | ||
|
|
||
| class CannotReplaceMissingTableException( | ||
| tableIdentifier: Identifier, | ||
| cause: Option[Throwable] = None) | ||
| extends AnalysisException( | ||
| s"Table $tableIdentifier cannot be replaced as it did not exist." + | ||
| s" Use CREATE OR REPLACE TABLE to create the table.", cause = cause) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -441,6 +441,47 @@ case class CreateTableAsSelect( | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Replace a table with a v2 catalog. | ||
| * | ||
| * If the table does not exist, and orCreate is true, then it will be created. | ||
| * If the table does not exist, and orCreate is false, then an exception will be thrown. | ||
| * | ||
| * The persisted table will have no contents as a result of this operation. | ||
| */ | ||
| case class ReplaceTable( | ||
| catalog: TableCatalog, | ||
| tableName: Identifier, | ||
| tableSchema: StructType, | ||
| partitioning: Seq[Transform], | ||
| properties: Map[String, String], | ||
| orCreate: Boolean) extends Command | ||
|
|
||
| /** | ||
| * Replaces a table from a select query with a v2 catalog. | ||
| * | ||
| * If the table does not exist, and orCreate is true, then it will be created. | ||
| * If the table does not exist, and orCreate is false, then an exception will be thrown. | ||
| */ | ||
| case class ReplaceTableAsSelect( | ||
|
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. What is the behavior if
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. Great question. I'm fairly certain that:
@rdblue - curious to hear your thoughts on the first situation. It makes a stronger case for trying to ban replacing tables without an atomic catalog.
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. I agree. If the drop happens first and is not atomic, then the create will fail. We should be able to add a rule to check for this and fail the query in analysis if the table doesn't support atomic updates. @mccheah, can you open an issue for adding a rule like that?
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. +1, I think we should fail the query if atomic RTAS is not supported. It's a valid use case to access the table being replaced in RTAS, Spark shouldn't throw table not found exception in this case, which is quite confusing.
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. @cloud-fan, we discussed this in the DSv2 sync and decided that, in general, RTAS should be supported even if the source is not atomic. Just to clarify, this would be a rule to fail RTAS if it is not atomic when the query depends on the table being replaced. We don't want to drop a table and then find that we can't replace it because it was dropped. |
||
| catalog: TableCatalog, | ||
| tableName: Identifier, | ||
| partitioning: Seq[Transform], | ||
| query: LogicalPlan, | ||
| properties: Map[String, String], | ||
| writeOptions: Map[String, String], | ||
| orCreate: Boolean) extends Command { | ||
|
|
||
| override def children: Seq[LogicalPlan] = Seq(query) | ||
|
|
||
| override lazy val resolved: Boolean = { | ||
| // the table schema is created from the query schema, so the only resolution needed is to check | ||
| // that the columns referenced by the table's partitioning exist in the query schema | ||
| val references = partitioning.flatMap(_.references).toSet | ||
| references.map(_.fieldNames).forall(query.schema.findNestedField(_).isDefined) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Append data to an existing table. | ||
| */ | ||
|
|
||
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.
Are there other flavors of
REPLACE TABLEthat we need to support?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.
I'm not sure that we should support all of what's already here, at least not to begin with.
I think that the main use of
REPLACE TABLEas an atomic operation isREPLACE TABLE ... AS SELECT. That's because the replacement should only happen if the write succeeds and the write could easily fail for a lot of reasons. Without a write, this is just syntactic sugar for a combined drop and create.I think the initial PR should focus on just the RTAS case. That simplifies this because it no longer needs the type list. What do you think?
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.
I think this should support the
USINGclause that is used to pass the provider name.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.
Because of the
tableProviderfield at the end I thinkUSINGis still supported right? As mentioned elsewhere, this is copied from CTAS.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.
Is there a test for it?