World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#9517] feat(iceberg): support rename table across different namespace in Gravitino Iceberg catalog (#9572)
FANNG7a84c4c17d71
diff --git a/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergCatalogOperations.java b/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergCatalogOperations.javaindex 492a69bd29..64a951cb81 100644--- a/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergCatalogOperations.java+++ b/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergCatalogOperations.java@@ -449,15 +449,28 @@ public class IcebergCatalogOperations implements CatalogOperations, SupportsScheprivate Table renameTable(NameIdentifier tableIdent, TableChange.RenameTable renameTable)throws NoSuchTableException, IllegalArgumentException {try {+ Namespace destNamespace = tableIdent.namespace();+ if (renameTable.getNewSchemaName().isPresent()) {+ String[] namespaceLevels = tableIdent.namespace().levels();+ String[] destLevels = Arrays.copyOf(namespaceLevels, namespaceLevels.length);+ destLevels[destLevels.length - 1] = renameTable.getNewSchemaName().get();+ NameIdentifier destSchemaIdent = NameIdentifier.of(destLevels);+ if (!schemaExists(destSchemaIdent)) {+ throw new NoSuchSchemaException("Iceberg schema does not exist %s", destSchemaIdent);+ }+ destNamespace = Namespace.of(destLevels);+ }+RenameTableRequest renameTableRequest =RenameTableRequest.builder().withSource(IcebergCatalogWrapperHelper.buildIcebergTableIdentifier(tableIdent)).withDestination(IcebergCatalogWrapperHelper.buildIcebergTableIdentifier(- tableIdent.namespace(), renameTable.getNewName()))+ destNamespace, renameTable.getNewName())).build();icebergCatalogWrapper.renameTable(renameTableRequest);- return loadTable(NameIdentifier.of(tableIdent.namespace(), renameTable.getNewName()));+ return loadTable(+ NameIdentifier.of(ArrayUtils.add(destNamespace.levels(), renameTable.getNewName())));} catch (org.apache.iceberg.exceptions.NoSuchTableException e) {throw new NoSuchTableException(e, ICEBERG_TABLE_DOES_NOT_EXIST_MSG, tableIdent.name());}diff --git a/catalogs/catalog-lakehouse-iceberg/src/test/java/org/apache/gravitino/catalog/lakehouse/iceberg/TestIcebergTable.java b/catalogs/catalog-lakehouse-iceberg/src/test/java/org/apache/gravitino/catalog/lakehouse/iceberg/TestIcebergTable.javaindex 28ffc707df..3cff762850 100644--- a/catalogs/catalog-lakehouse-iceberg/src/test/java/org/apache/gravitino/catalog/lakehouse/iceberg/TestIcebergTable.java+++ b/catalogs/catalog-lakehouse-iceberg/src/test/java/org/apache/gravitino/catalog/lakehouse/iceberg/TestIcebergTable.java@@ -491,6 +491,90 @@ public class TestIcebergTable {Assertions.assertArrayEquals(createdTable.partitioning(), alteredTable.partitioning());}+ @Test+ public void testRenameTableAcrossSchema() {+ NameIdentifier tableIdentifier =+ NameIdentifier.of(+ META_LAKE_NAME, icebergCatalog.name(), icebergSchema.name(), "test_iceberg_table");+ Map<String, String> properties = Maps.newHashMap();+ properties.put("key1", "val1");++ IcebergColumn col1 =+ IcebergColumn.builder()+ .withName("col_1")+ .withType(Types.IntegerType.get())+ .withComment(ICEBERG_COMMENT)+ .build();+ Column[] columns = new Column[] {col1};++ String targetSchemaName = "target_schema_" + genRandomName();+ NameIdentifier targetSchemaIdent =+ NameIdentifier.of(META_LAKE_NAME, icebergCatalog.name(), targetSchemaName);+ NameIdentifier renamedIdentifier =+ NameIdentifier.of(META_LAKE_NAME, icebergCatalog.name(), targetSchemaName, "renamed_table");++ try {+ icebergCatalogOperations.createSchema(targetSchemaIdent, ICEBERG_COMMENT, Maps.newHashMap());+ icebergCatalogOperations.createTable(+ tableIdentifier,+ columns,+ ICEBERG_COMMENT,+ properties,+ new Transform[0],+ Distributions.NONE,+ new SortOrder[0]);++ icebergCatalogOperations.alterTable(+ tableIdentifier, TableChange.rename("renamed_table", targetSchemaName));++ Assertions.assertFalse(icebergCatalogOperations.tableExists(tableIdentifier));+ Assertions.assertTrue(icebergCatalogOperations.tableExists(renamedIdentifier));+ Assertions.assertEquals(+ "renamed_table", icebergCatalogOperations.loadTable(renamedIdentifier).name());+ } finally {+ if (icebergCatalogOperations.tableExists(renamedIdentifier)) {+ icebergCatalogOperations.dropTable(renamedIdentifier);+ }+ if (icebergCatalogOperations.schemaExists(targetSchemaIdent)) {+ icebergCatalogOperations.dropSchema(targetSchemaIdent, false);+ }+ }+ }++ @Test+ public void testRenameTableToMissingSchema() {+ NameIdentifier tableIdentifier =+ NameIdentifier.of(+ META_LAKE_NAME, icebergCatalog.name(), icebergSchema.name(), "test_iceberg_table");+ Map<String, String> properties = Maps.newHashMap();+ properties.put("key1", "val1");++ IcebergColumn col1 =+ IcebergColumn.builder()+ .withName("col_1")+ .withType(Types.IntegerType.get())+ .withComment(ICEBERG_COMMENT)+ .build();+ Column[] columns = new Column[] {col1};++ icebergCatalogOperations.createTable(+ tableIdentifier,+ columns,+ ICEBERG_COMMENT,+ properties,+ new Transform[0],+ Distributions.NONE,+ new SortOrder[0]);++ String missingSchemaName = "missing_schema_" + genRandomName();+ Assertions.assertThrows(+ NoSuchSchemaException.class,+ () ->+ icebergCatalogOperations.alterTable(+ tableIdentifier, TableChange.rename("renamed_table", missingSchemaName)));+ Assertions.assertTrue(icebergCatalogOperations.tableExists(tableIdentifier));+ }+@Testpublic void testTableProperty() {CatalogEntity entity = createDefaultCatalogEntity();