-
Notifications
You must be signed in to change notification settings - Fork 3k
Core: Add in RenameTableRequest for the RESTCatalog #4448
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
Merged
Merged
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 hidden or 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 hidden or 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 hidden or 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
94 changes: 94 additions & 0 deletions
94
core/src/main/java/org/apache/iceberg/rest/requests/RenameTableRequest.java
This file contains hidden or 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,94 @@ | ||
| /* | ||
| * 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.iceberg.rest.requests; | ||
|
|
||
| import org.apache.iceberg.catalog.TableIdentifier; | ||
| import org.apache.iceberg.relocated.com.google.common.base.MoreObjects; | ||
| import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
| import org.apache.iceberg.rest.RESTRequest; | ||
|
|
||
| /** | ||
| * A REST request to rename a table. | ||
| */ | ||
| public class RenameTableRequest implements RESTRequest { | ||
|
|
||
| private TableIdentifier source; | ||
| private TableIdentifier destination; | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public RenameTableRequest() { | ||
| // Needed for Jackson Deserialization. | ||
| } | ||
|
|
||
| private RenameTableRequest(TableIdentifier source, TableIdentifier destination) { | ||
| this.source = source; | ||
| this.destination = destination; | ||
| validate(); | ||
| } | ||
|
|
||
| public void validate() { | ||
| Preconditions.checkArgument(source != null, "Invalid source table: null"); | ||
| Preconditions.checkArgument(destination != null, "Invalid destination table: null"); | ||
| } | ||
|
|
||
| public TableIdentifier source() { | ||
| return source; | ||
| } | ||
|
|
||
| public TableIdentifier destination() { | ||
| return destination; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return MoreObjects.toStringHelper(this) | ||
| .add("source", source) | ||
| .add("destination", destination) | ||
| .toString(); | ||
| } | ||
|
|
||
| public static Builder builder() { | ||
| return new Builder(); | ||
| } | ||
|
|
||
| public static class Builder { | ||
| private TableIdentifier source; | ||
| private TableIdentifier destination; | ||
|
|
||
| private Builder() { | ||
| } | ||
|
|
||
| public Builder withSource(TableIdentifier sourceTable) { | ||
| Preconditions.checkNotNull(sourceTable, "Invalid source table identifier: null"); | ||
| this.source = sourceTable; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder withDestination(TableIdentifier destinationTable) { | ||
| Preconditions.checkNotNull(destinationTable, "Invalid destination table identifier: null"); | ||
| this.destination = destinationTable; | ||
| return this; | ||
| } | ||
|
|
||
| public RenameTableRequest build() { | ||
| return new RenameTableRequest(source, destination); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ | |
| import org.apache.iceberg.DataFile; | ||
| import org.apache.iceberg.DataFiles; | ||
| import org.apache.iceberg.FileScanTask; | ||
| import org.apache.iceberg.HasTableOperations; | ||
| import org.apache.iceberg.PartitionSpec; | ||
| import org.apache.iceberg.ReplaceSortOrder; | ||
| import org.apache.iceberg.Schema; | ||
|
|
@@ -65,6 +66,7 @@ | |
| public abstract class CatalogTests<C extends Catalog & SupportsNamespaces> { | ||
| private static final Namespace NS = Namespace.of("newdb"); | ||
| private static final TableIdentifier TABLE = TableIdentifier.of(NS, "table"); | ||
| private static final TableIdentifier RENAMED_TABLE = TableIdentifier.of(NS, "table_renamed"); | ||
|
|
||
| // Schema passed to create tables | ||
| private static final Schema SCHEMA = new Schema( | ||
|
|
@@ -562,6 +564,78 @@ public void testLoadMissingTable() { | |
| () -> catalog.loadTable(ident)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRenameTable() { | ||
| C catalog = catalog(); | ||
|
|
||
| if (requiresNamespaceCreate()) { | ||
| catalog.createNamespace(NS); | ||
| } | ||
|
|
||
| Assert.assertFalse("Source table should not exist before create", catalog.tableExists(TABLE)); | ||
|
|
||
| catalog.buildTable(TABLE, SCHEMA).create(); | ||
| Assert.assertTrue("Table should exist after create", catalog.tableExists(TABLE)); | ||
|
|
||
| Assert.assertFalse("Destination table should not exist before rename", catalog.tableExists(RENAMED_TABLE)); | ||
|
|
||
| catalog.renameTable(TABLE, RENAMED_TABLE); | ||
| Assert.assertTrue("Table should exist with new name", catalog.tableExists(RENAMED_TABLE)); | ||
| Assert.assertFalse("Original table should no longer exist", catalog.tableExists(TABLE)); | ||
|
|
||
| catalog.dropTable(RENAMED_TABLE); | ||
| assertEmpty("Should not contain table after drop", catalog, NS); | ||
| } | ||
kbendick marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @Test | ||
| public void testRenameTableMissingSourceTable() { | ||
| C catalog = catalog(); | ||
|
|
||
| if (requiresNamespaceCreate()) { | ||
| catalog.createNamespace(NS); | ||
| } | ||
|
|
||
| Assert.assertFalse("Source table should not exist before rename", catalog.tableExists(TABLE)); | ||
| Assert.assertFalse("Destination table should not exist before rename", catalog.tableExists(RENAMED_TABLE)); | ||
|
|
||
| AssertHelpers.assertThrows("Should reject renaming a table that does not exist", | ||
| NoSuchTableException.class, | ||
| "Table does not exist", | ||
| () -> catalog.renameTable(TABLE, RENAMED_TABLE)); | ||
|
|
||
| Assert.assertFalse("Destination table should not exist after failed rename", catalog.tableExists(RENAMED_TABLE)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRenameTableDestinationTableAlreadyExists() { | ||
| C catalog = catalog(); | ||
|
|
||
| if (requiresNamespaceCreate()) { | ||
| catalog.createNamespace(NS); | ||
| } | ||
|
|
||
| Assert.assertFalse("Source table should not exist before create", catalog.tableExists(TABLE)); | ||
| catalog.buildTable(TABLE, SCHEMA).create(); | ||
| Assert.assertTrue("Source table should exist after create", catalog.tableExists(TABLE)); | ||
|
|
||
| Assert.assertFalse("Destination table should not exist before create", catalog.tableExists(RENAMED_TABLE)); | ||
| catalog.buildTable(RENAMED_TABLE, SCHEMA).create(); | ||
| Assert.assertTrue("Destination table should exist after create", catalog.tableExists(RENAMED_TABLE)); | ||
|
|
||
| AssertHelpers.assertThrows("Should reject renaming a table if the new name already exists", | ||
| AlreadyExistsException.class, | ||
| "Table already exists", | ||
| () -> catalog.renameTable(TABLE, RENAMED_TABLE)); | ||
|
|
||
| Assert.assertTrue("Source table should still exist after failed rename", catalog.tableExists(TABLE)); | ||
| Assert.assertTrue("Destination table should still exist after failed rename", catalog.tableExists(RENAMED_TABLE)); | ||
kbendick marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| String sourceTableUUID = ((HasTableOperations) catalog.loadTable(TABLE)).operations().current().uuid(); | ||
| String destinationTableUUID = ((HasTableOperations) catalog.loadTable(RENAMED_TABLE)).operations().current().uuid(); | ||
| Assert.assertNotEquals("Source and destination table should remain distinct after failed rename", | ||
| sourceTableUUID, destinationTableUUID); | ||
|
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 good. |
||
| } | ||
|
|
||
| @Test | ||
| public void testDropTable() { | ||
| C catalog = catalog(); | ||
|
|
||
This file contains hidden or 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 hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.