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

yuqi1129 pushed a commit to branch branch-1.3
in repository https://gitbox.apache.org/repos/asf/gravitino.git


The following commit(s) were added to refs/heads/branch-1.3 by this push:
     new b5a83c8380 [Cherry-pick to branch-1.3] [#11980] fix(core): sort column 
ordinal positions while fetching from entity store (#11979) (#12003)
b5a83c8380 is described below

commit b5a83c83808ba3b677a5ae37ff9c5889b291e12d
Author: github-actions[bot] 
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Mon Jul 13 20:37:09 2026 +0800

    [Cherry-pick to branch-1.3] [#11980] fix(core): sort column ordinal 
positions while fetching from entity store (#11979) (#12003)
    
    **Cherry-pick Information:**
    - Original commit: 61ee16ae6367576200e9b2ff13c30fd1a627602e
    - Target branch: `branch-1.3`
    - Status: ✅ Clean cherry-pick (no conflicts)
    
    Co-authored-by: MehulBatra <[email protected]>
---
 .../gravitino/catalog/ManagedTableOperations.java  |  7 +++-
 .../catalog/TestManagedTableOperations.java        | 42 ++++++++++++++++++++++
 2 files changed, 48 insertions(+), 1 deletion(-)

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 39710b2cce..fe761e7962 100644
--- 
a/core/src/main/java/org/apache/gravitino/catalog/ManagedTableOperations.java
+++ 
b/core/src/main/java/org/apache/gravitino/catalog/ManagedTableOperations.java
@@ -472,7 +472,12 @@ public abstract class ManagedTableOperations implements 
TableCatalog {
         .withName(tableEntity.name())
         .withComment(tableEntity.comment())
         .withColumns(
-            
tableEntity.columns().stream().map(this::toGenericColumn).toArray(Column[]::new))
+            // Columns may be returned unordered from the store; sort by 
position
+            // so the table's column order matches what was declared.
+            tableEntity.columns().stream()
+                .sorted(Comparator.comparingInt(ColumnEntity::position))
+                .map(this::toGenericColumn)
+                .toArray(Column[]::new))
         .withProperties(tableEntity.properties())
         .withAuditInfo(tableEntity.auditInfo())
         .withSortOrders(tableEntity.sortOrders())
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 76e6698f5a..c660a40b67 100644
--- 
a/core/src/test/java/org/apache/gravitino/catalog/TestManagedTableOperations.java
+++ 
b/core/src/test/java/org/apache/gravitino/catalog/TestManagedTableOperations.java
@@ -18,8 +18,10 @@
  */
 package org.apache.gravitino.catalog;
 
+import java.time.Instant;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.stream.Collectors;
@@ -31,6 +33,9 @@ import org.apache.gravitino.connector.GenericColumn;
 import org.apache.gravitino.connector.SupportsSchemas;
 import org.apache.gravitino.exceptions.NoSuchTableException;
 import org.apache.gravitino.exceptions.TableAlreadyExistsException;
+import org.apache.gravitino.meta.AuditInfo;
+import org.apache.gravitino.meta.ColumnEntity;
+import org.apache.gravitino.meta.TableEntity;
 import org.apache.gravitino.rel.Column;
 import org.apache.gravitino.rel.Table;
 import org.apache.gravitino.rel.TableChange;
@@ -213,6 +218,43 @@ public class TestManagedTableOperations {
         NoSuchTableException.class, () -> 
tableOperations.loadTable(nonExistingTableIdent));
   }
 
+  @Test
+  public void testLoadTableSortsColumnsByPosition() throws Exception {
+    // Columns can be returned unordered from the store; loadTable must order 
them
+    // by position. Store a table whose column list order differs from the 
column
+    // positions, then verify the loaded columns come back in position order.
+    AuditInfo auditInfo =
+        
AuditInfo.builder().withCreator("test").withCreateTime(Instant.now()).build();
+    Column col1 = createColumn("col1", Types.StringType.get(), null);
+    Column col2 = createColumn("col2", Types.IntegerType.get(), null);
+    Column col3 = createColumn("col3", Types.StringType.get(), null);
+
+    // List order [col3(pos 2), col1(pos 0), col2(pos 1)] - intentionally not 
by position.
+    List<ColumnEntity> unordered =
+        Arrays.asList(
+            ColumnEntity.toColumnEntity(col3, 2, idGenerator.nextId(), 
auditInfo),
+            ColumnEntity.toColumnEntity(col1, 0, idGenerator.nextId(), 
auditInfo),
+            ColumnEntity.toColumnEntity(col2, 1, idGenerator.nextId(), 
auditInfo));
+
+    NameIdentifier tableIdent =
+        NameIdentifierUtil.ofTable(METALAKE_NAME, CATALOG_NAME, SCHEMA_NAME, 
"table_order");
+    TableEntity tableEntity =
+        TableEntity.builder()
+            .withId(idGenerator.nextId())
+            .withName("table_order")
+            .withNamespace(NamespaceUtil.ofTable(METALAKE_NAME, CATALOG_NAME, 
SCHEMA_NAME))
+            .withColumns(unordered)
+            .withProperties(Collections.emptyMap())
+            .withAuditInfo(auditInfo)
+            .build();
+    store.put(tableEntity, false /* overwrite */);
+
+    Table loaded = tableOperations.loadTable(tableIdent);
+    Assertions.assertArrayEquals(
+        new String[] {"col1", "col2", "col3"},
+        
Arrays.stream(loaded.columns()).map(Column::name).toArray(String[]::new));
+  }
+
   @Test
   public void testCreateAndDropTable() {
     NameIdentifier table1Ident =

Reply via email to