World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#9477]improve(core): Support rename table to a new schema for ManagedTableOperations (#9481)
Jerry Shao7a83832a4732
diff --git a/core/src/main/java/org/apache/gravitino/catalog/ManagedTableOperations.java b/core/src/main/java/org/apache/gravitino/catalog/ManagedTableOperations.javaindex 44060c6cae..39710b2cce 100644--- a/core/src/main/java/org/apache/gravitino/catalog/ManagedTableOperations.java+++ b/core/src/main/java/org/apache/gravitino/catalog/ManagedTableOperations.java@@ -60,6 +60,7 @@ import org.apache.gravitino.rel.indexes.Index;import org.apache.gravitino.rel.indexes.Indexes;import org.apache.gravitino.rel.types.Type;import org.apache.gravitino.storage.IdGenerator;+import org.apache.gravitino.utils.NamespaceUtil;import org.apache.gravitino.utils.PrincipalUtils;public abstract class ManagedTableOperations implements TableCatalog {@@ -208,6 +209,7 @@ public abstract class ManagedTableOperations implements TableCatalog {private TableEntity applyChanges(TableEntity oldTableEntity, TableChange... changes) {String newName = oldTableEntity.name();+ Namespace newNs = oldTableEntity.namespace();String newComment = oldTableEntity.comment();Map<String, String> newProps = Maps.newHashMap(oldTableEntity.properties());List<ColumnEntity> newColumns = Lists.newArrayList(oldTableEntity.columns());@@ -225,13 +227,13 @@ public abstract class ManagedTableOperations implements TableCatalog {for (TableChange change : tableChanges) {if (change instanceof TableChange.RenameTable rename) {- if (rename.getNewSchemaName().isPresent()) {- throw new IllegalArgumentException(- "Gravitino managed table doesn't support renaming "- + "the table across schemas for now");- }-+ Namespace oldNs = oldTableEntity.namespace();newName = rename.getNewName();+ newNs =+ rename+ .getNewSchemaName()+ .map(s -> NamespaceUtil.ofTable(oldNs.level(0), oldNs.level(1), s))+ .orElse(oldNs);} else if (change instanceof TableChange.UpdateComment updateComment) {newComment = updateComment.getNewComment();@@ -269,7 +271,7 @@ public abstract class ManagedTableOperations implements TableCatalog {return TableEntity.builder().withId(oldTableEntity.id()).withName(newName)- .withNamespace(oldTableEntity.namespace())+ .withNamespace(newNs).withComment(newComment).withColumns(newColumns).withProperties(newProps)diff --git a/core/src/main/java/org/apache/gravitino/meta/TableEntity.java b/core/src/main/java/org/apache/gravitino/meta/TableEntity.javaindex 3055c8fff9..8110f5a6ac 100644--- a/core/src/main/java/org/apache/gravitino/meta/TableEntity.java+++ b/core/src/main/java/org/apache/gravitino/meta/TableEntity.java@@ -51,7 +51,6 @@ public class TableEntity implements Entity, Auditable, HasIdentifier {Field.required("audit_info", AuditInfo.class, "The audit details of the table");public static final Field COLUMNS =Field.optional("columns", List.class, "The columns of the table");- public static final Field FORMAT = Field.optional("format", String.class, "The table's format");public static final Field PROPERTIES =Field.optional("properties", Map.class, "The table's properties");public static final Field PARTITIONING =diff --git a/core/src/test/java/org/apache/gravitino/catalog/TestManagedTableOperations.java b/core/src/test/java/org/apache/gravitino/catalog/TestManagedTableOperations.javaindex 88f1575427..76e6698f5a 100644--- a/core/src/test/java/org/apache/gravitino/catalog/TestManagedTableOperations.java+++ b/core/src/test/java/org/apache/gravitino/catalog/TestManagedTableOperations.java@@ -328,13 +328,15 @@ public class TestManagedTableOperations {Assertions.assertEquals("table1_renamed", loadedRenamedTable.name());// Test rename the table to another schema+ Table renamedTable1 =+ tableOperations.alterTable(+ NameIdentifierUtil.ofTable(METALAKE_NAME, CATALOG_NAME, SCHEMA_NAME, "table1_renamed"),+ TableChange.rename("table1_moved", "schema2"));+NameIdentifier renamedTable1Ident =- NameIdentifierUtil.ofTable(METALAKE_NAME, CATALOG_NAME, SCHEMA_NAME, "table1_renamed");- Assertions.assertThrows(- IllegalArgumentException.class,- () ->- tableOperations.alterTable(- renamedTable1Ident, TableChange.rename("table1_moved", "schema2")));+ NameIdentifierUtil.ofTable(METALAKE_NAME, CATALOG_NAME, "schema2", "table1_moved");+ Table loadedRenamedTable1 = tableOperations.loadTable(renamedTable1Ident);+ Assertions.assertEquals(renamedTable1.name(), loadedRenamedTable1.name());// Test update the table commentString newComment = "Updated Test Table 1 Comment";diff --git a/core/src/test/java/org/apache/gravitino/storage/memory/TestMemoryEntityStore.java b/core/src/test/java/org/apache/gravitino/storage/memory/TestMemoryEntityStore.javaindex 725d440dde..95dded4f9c 100644--- a/core/src/test/java/org/apache/gravitino/storage/memory/TestMemoryEntityStore.java+++ b/core/src/test/java/org/apache/gravitino/storage/memory/TestMemoryEntityStore.java@@ -125,6 +125,7 @@ public class TestMemoryEntityStore {E newE = updater.apply(e);NameIdentifier newIdent = NameIdentifier.of(newE.namespace(), newE.name());+ // If the schema is changed for rename table operation, delete the old entry firstif (!newIdent.equals(ident)) {delete(ident, entityType);}