Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#11980] fix(core): sort column ordinal positions while fetching from entity store (#11979)

MehulBatra61ee16ae6367
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 =