This is an automated email from the ASF dual-hosted git repository.
jshao pushed a commit to branch branch-1.1
in repository https://gitbox.apache.org/repos/asf/gravitino.git
The following commit(s) were added to refs/heads/branch-1.1 by this push:
new 1e9a79e0e9 [#9571] feat(iceberg): support rename table across
different namespace in Gravitino Iceberg catalog (#9584)
1e9a79e0e9 is described below
commit 1e9a79e0e9adbbef8b162f45228dea2e06ba41fe
Author: github-actions[bot]
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Tue Dec 30 16:55:40 2025 +0800
[#9571] feat(iceberg): support rename table across different namespace in
Gravitino Iceberg catalog (#9584)
### What changes were proposed in this pull request?
support rename table across different namespace in Gravitino Iceberg
catalog
### Why are the changes needed?
Fix: #9571
### Does this PR introduce _any_ user-facing change?
no
### How was this patch tested?
adding tests
Co-authored-by: FANNG <[email protected]>
---
.../iceberg/IcebergCatalogOperations.java | 17 ++++-
.../lakehouse/iceberg/TestIcebergTable.java | 84 ++++++++++++++++++++++
2 files changed, 99 insertions(+), 2 deletions(-)
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.java
index 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, SupportsSche
private 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.java
index 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));
+ }
+
@Test
public void testTableProperty() {
CatalogEntity entity = createDefaultCatalogEntity();