This is an automated email from the ASF dual-hosted git repository.

mchades pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gravitino.git


The following commit(s) were added to refs/heads/main by this push:
     new 7a83832a47 [#9477]improve(core): Support rename table to a new schema 
for ManagedTableOperations (#9481)
7a83832a47 is described below

commit 7a83832a4732c523f44c2ca6772154224c54848e
Author: Jerry Shao <[email protected]>
AuthorDate: Mon Dec 22 12:52:07 2025 +0800

    [#9477]improve(core): Support rename table to a new schema for 
ManagedTableOperations (#9481)
    
    ### What changes were proposed in this pull request?
    
    Add new schema support when renaming a table to a new schema.
    
    ### Why are the changes needed?
    
    Many table formats support renaming table to a new schema, we should
    also support it for Gravitino's built-in catalog.
    
    Fix: #9477
    
    ### Does this PR introduce _any_ user-facing change?
    
    No.
    
    ### How was this patch tested?
    
    UTs.
---
 .../apache/gravitino/catalog/ManagedTableOperations.java | 16 +++++++++-------
 .../main/java/org/apache/gravitino/meta/TableEntity.java |  1 -
 .../gravitino/catalog/TestManagedTableOperations.java    | 14 ++++++++------
 .../gravitino/storage/memory/TestMemoryEntityStore.java  |  1 +
 4 files changed, 18 insertions(+), 14 deletions(-)

diff --git 
a/core/src/main/java/org/apache/gravitino/catalog/ManagedTableOperations.java 
b/core/src/main/java/org/apache/gravitino/catalog/ManagedTableOperations.java
index 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.java
index 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.java
index 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 comment
     String 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.java
index 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 first
             if (!newIdent.equals(ident)) {
               delete(ident, entityType);
             }

Reply via email to