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 @@ -19,6 +19,7 @@
import io.trino.execution.warnings.WarningCollector;
import io.trino.metadata.Metadata;
import io.trino.metadata.QualifiedObjectName;
import io.trino.metadata.RedirectionAwareTableHandle;
import io.trino.metadata.TableHandle;
import io.trino.security.AccessControl;
import io.trino.spi.TrinoException;
Expand All @@ -29,7 +30,6 @@
import javax.inject.Inject;

import java.util.List;
import java.util.Optional;

import static com.google.common.util.concurrent.Futures.immediateVoidFuture;
import static io.trino.metadata.MetadataUtil.createQualifiedObjectName;
Expand Down Expand Up @@ -91,27 +91,29 @@ public ListenableFuture<Void> execute(
return immediateVoidFuture();
}

Optional<TableHandle> tableHandle = metadata.getTableHandle(session, tableName);
if (tableHandle.isEmpty()) {
RedirectionAwareTableHandle redirectionAwareTableHandle = metadata.getRedirectionAwareTableHandle(session, tableName);
if (redirectionAwareTableHandle.getTableHandle().isEmpty()) {
if (!statement.isExists()) {
throw semanticException(TABLE_NOT_FOUND, statement, "Table '%s' does not exist", tableName);
}
return immediateVoidFuture();
}

QualifiedObjectName target = createTargetQualifiedObjectName(tableName, statement.getTarget());
TableHandle tableHandle = redirectionAwareTableHandle.getTableHandle().get();
QualifiedObjectName source = redirectionAwareTableHandle.getRedirectedTableName().orElse(tableName);
QualifiedObjectName target = createTargetQualifiedObjectName(source, statement.getTarget());
if (metadata.getCatalogHandle(session, target.getCatalogName()).isEmpty()) {
throw semanticException(CATALOG_NOT_FOUND, statement, "Target catalog '%s' does not exist", target.getCatalogName());
}
if (metadata.getTableHandle(session, target).isPresent()) {
throw semanticException(TABLE_ALREADY_EXISTS, statement, "Target table '%s' already exists", target);
}
if (!tableName.getCatalogName().equals(target.getCatalogName())) {
if (!tableHandle.getCatalogName().getCatalogName().equals(target.getCatalogName())) {
throw semanticException(NOT_SUPPORTED, statement, "Table rename across catalogs is not supported");
}
accessControl.checkCanRenameTable(session.toSecurityContext(), tableName, target);
accessControl.checkCanRenameTable(session.toSecurityContext(), source, target);

metadata.renameTable(session, tableHandle.get(), target);
metadata.renameTable(session, tableHandle, target);

return immediateVoidFuture();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,21 @@ public void testAlterTableRename()

createIcebergTable(icebergTableName, false);

//TODO restore test assertions after adding redirection awareness to the RenameTableTask
assertQueryFailure(() -> onTrino().executeQuery("ALTER TABLE " + hiveTableName + " RENAME TO " + tableName + "_new"))
.hasMessageMatching("\\QQuery failed (#\\E\\S+\\Q): Cannot query Iceberg table 'default." + tableName + "'");
assertQueryFailure(() -> onTrino().executeQuery("ALTER TABLE " + hiveTableName + " RENAME TO hive.default." + tableName + "_new"))
.hasMessageMatching("\\QQuery failed (#\\E\\S+\\Q): line 1:1: Table rename across catalogs is not supported");

onTrino().executeQuery("DROP TABLE " + icebergTableName);
String newTableNameWithoutCatalogWithoutSchema = tableName + "_new_without_catalog_without_schema";
onTrino().executeQuery("ALTER TABLE " + hiveTableName + " RENAME TO " + newTableNameWithoutCatalogWithoutSchema);
String newTableNameWithoutCatalogWithSchema = tableName + "_new_without_catalog_with_schema";
onTrino().executeQuery("ALTER TABLE hive.default." + newTableNameWithoutCatalogWithoutSchema + " RENAME TO default." + newTableNameWithoutCatalogWithSchema);
String newTableNameWithCatalogWithSchema = tableName + "_new_with_catalog_with_schema";
onTrino().executeQuery("ALTER TABLE hive.default." + newTableNameWithoutCatalogWithSchema + " RENAME TO iceberg.default." + newTableNameWithCatalogWithSchema);

assertResultsEqual(
onTrino().executeQuery("TABLE " + icebergTableName + "_new_with_catalog_with_schema"),
onTrino().executeQuery("TABLE " + hiveTableName + "_new_with_catalog_with_schema"));

onTrino().executeQuery("DROP TABLE " + icebergTableName + "_new_with_catalog_with_schema");
}

@Test(groups = {HIVE_ICEBERG_REDIRECTIONS, PROFILE_SPECIFIC_TESTS})
Expand Down