Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -556,19 +556,27 @@ public synchronized void renameTable(HiveIdentity identity, String databaseName,
Table table = getRequiredTable(databaseName, tableName);
getRequiredDatabase(newDatabaseName);

if (isIcebergTable(table)) {
throw new TrinoException(NOT_SUPPORTED, "Rename not supported for Iceberg tables");
}

// verify new table does not exist
verifyTableNotExists(newDatabaseName, newTableName);

Path oldPath = getTableMetadataDirectory(databaseName, tableName);
Path newPath = getTableMetadataDirectory(newDatabaseName, newTableName);

try {
if (!metadataFileSystem.rename(oldPath, newPath)) {
throw new TrinoException(HIVE_METASTORE_ERROR, "Could not rename table directory");
if (isIcebergTable(table)) {
if (!metadataFileSystem.mkdirs(newPath)) {
throw new TrinoException(HIVE_METASTORE_ERROR, "Could not create new table directory");
}
// Iceberg metadata references files in old path, so these cannot be moved. Moving table description (metadata from metastore perspective) only.
if (!metadataFileSystem.rename(new Path(oldPath, TRINO_SCHEMA_FILE_NAME), new Path(newPath, TRINO_SCHEMA_FILE_NAME))) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a comment saying that we do this for metadata referencing the old path?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

throw new TrinoException(HIVE_METASTORE_ERROR, "Could not rename table schema file");
}
// TODO drop data files when table is being dropped
}
else {
if (!metadataFileSystem.rename(oldPath, newPath)) {
throw new TrinoException(HIVE_METASTORE_ERROR, "Could not rename table directory");
}
}
}
catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
{
switch (connectorBehavior) {
case SUPPORTS_COMMENT_ON_COLUMN:
case SUPPORTS_RENAME_TABLE:
case SUPPORTS_TOPN_PUSHDOWN:
return false;

Expand Down Expand Up @@ -183,16 +182,6 @@ public void testRowLevelDelete()
.hasStackTraceContaining("This connector only supports delete where one or more partitions are deleted entirely");
}

@Test
@Override
public void testRenameTable()
{
// Iceberg table rename is not supported in FileHiveMetastore
// TODO add a test with a different metastore, or block rename in IcebergMetadata
assertThatThrownBy(super::testRenameTable)
.hasStackTraceContaining("Rename not supported for Iceberg tables");
}

@Test
@Override
public void testShowCreateSchema()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
{
switch (connectorBehavior) {
case SUPPORTS_COMMENT_ON_COLUMN:
case SUPPORTS_RENAME_TABLE:
case SUPPORTS_TOPN_PUSHDOWN:
return false;

Expand All @@ -67,16 +66,6 @@ public void testRowLevelDelete()
.hasStackTraceContaining("This connector only supports delete where one or more partitions are deleted entirely");
}

@Test
@Override
public void testRenameTable()
{
// Iceberg table rename is not supported in FileHiveMetastore
// TODO add a test with a different metastore, or block rename in IcebergMetadata
assertThatThrownBy(super::testRenameTable)
.hasStackTraceContaining("Rename not supported for Iceberg tables");
}

@Test
@Override
public void testShowCreateTable()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Licensed 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 io.trino.tests.product.iceberg;

import io.trino.tempto.ProductTest;
import org.testng.annotations.Test;

import static io.trino.tempto.assertions.QueryAssert.Row.row;
import static io.trino.tempto.assertions.QueryAssert.assertThat;
import static io.trino.tests.product.TestGroups.ICEBERG;
import static io.trino.tests.product.TestGroups.STORAGE_FORMATS;
import static io.trino.tests.product.hive.util.TemporaryHiveTable.randomTableSuffix;
import static io.trino.tests.product.utils.QueryExecutors.onTrino;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class TestIcebergRenameTable
extends ProductTest
{
@Test(groups = {ICEBERG, STORAGE_FORMATS})
public void testRenameTable()
{
String tableName = "iceberg.default.test_rename_table_" + randomTableSuffix();
String newName = "iceberg.default.test_rename_table_new_" + randomTableSuffix();
onTrino().executeQuery("CREATE TABLE " + tableName + "(a bigint, b varchar)");
try {
onTrino().executeQuery("INSERT INTO " + tableName + "(a, b) VALUES " +
"(NULL, NULL), " +
"(-42, 'abc'), " +
"(9223372036854775807, 'abcdefghijklmnopqrstuvwxyz')");
onTrino().executeQuery("ALTER TABLE " + tableName + " RENAME TO " + newName);
assertThatThrownBy(() -> onTrino().executeQuery("SELECT * FROM " + tableName))
.hasMessageContaining("Table '" + tableName + "' does not exist");
assertThat(onTrino().executeQuery("SELECT * FROM " + newName))
.containsOnly(
row(null, null),
row(-42, "abc"),
row(9223372036854775807L, "abcdefghijklmnopqrstuvwxyz"));
}
finally {
onTrino().executeQuery("DROP TABLE IF EXISTS " + tableName);
onTrino().executeQuery("DROP TABLE IF EXISTS " + newName);
}
}
}