Skip to content

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

[#13447] fix(core): re-insert updated columns at their current index in bundled alters (#13448)

YangJiec17fc03b0173
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 0e226abcbb..64c9af8b9c 100644
--- a/core/src/main/java/org/apache/gravitino/catalog/ManagedTableOperations.java
+++ b/core/src/main/java/org/apache/gravitino/catalog/ManagedTableOperations.java
@@ -335,10 +335,10 @@ public abstract class ManagedTableOperations implements TableCatalog {
|| change instanceof TableChange.UpdateColumnPosition
|| change instanceof TableChange.UpdateColumnNullability
|| change instanceof TableChange.UpdateColumnAutoIncrement) {
- int i;
+ int removedIndex;
ColumnEntity oldColumn = null;
- for (i = 0; i < newColumns.size(); i++) {
- ColumnEntity col = newColumns.get(i);
+ for (removedIndex = 0; removedIndex < newColumns.size(); removedIndex++) {
+ ColumnEntity col = newColumns.get(removedIndex);
if (col.name().equals(DOT.join(change.fieldName()))) {
oldColumn = col;
break;
@@ -407,7 +407,14 @@ public abstract class ManagedTableOperations implements TableCatalog {
newPosition,
newNullable,
newAutoIncrement);
- newColumns.add(newColumn.position(), newColumn);
+ if (newPosition.isPresent()) {
+ newColumns.add(newColumn.position(), newColumn);
+ } else {
+ // Stored positions go stale as sibling changes in the same alter add,
+ // remove or move columns, so without an explicit position change the
+ // column goes back to the list index it was removed from.
+ newColumns.add(removedIndex, newColumn);
+ }
} else if (change instanceof TableChange.DeleteColumn deleteColumn) {
boolean removed =
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 c660a40b67..cd0ce98ff8 100644
--- a/core/src/test/java/org/apache/gravitino/catalog/TestManagedTableOperations.java
+++ b/core/src/test/java/org/apache/gravitino/catalog/TestManagedTableOperations.java
@@ -336,6 +336,54 @@ public class TestManagedTableOperations {
.build();
}
+ @Test
+ public void testAlterTableDeleteAndRenameInOneRequest() {
+ // Deleting a lower-position column shifts the columns after it, so renaming the last one in
+ // the same request must not re-insert it past the end of the list.
+ NameIdentifier lastIdent = createThreeColumnTable("rename_last");
+ Table renamedLast =
+ tableOperations.alterTable(
+ lastIdent,
+ TableChange.deleteColumn(new String[] {"col1"}, false),
+ TableChange.renameColumn(new String[] {"col3"}, "col3_renamed"));
+ Assertions.assertArrayEquals(
+ new String[] {"col2", "col3_renamed"},
+ Arrays.stream(renamedLast.columns()).map(Column::name).toArray(String[]::new));
+
+ // After deleting col1 in the same request, renaming col2 must not move it behind col3.
+ NameIdentifier middleIdent = createThreeColumnTable("rename_middle");
+ Table renamedMiddle =
+ tableOperations.alterTable(
+ middleIdent,
+ TableChange.deleteColumn(new String[] {"col1"}, false),
+ TableChange.renameColumn(new String[] {"col2"}, "col2_renamed"));
+ Assertions.assertArrayEquals(
+ new String[] {"col2_renamed", "col3"},
+ Arrays.stream(renamedMiddle.columns()).map(Column::name).toArray(String[]::new));
+ }
+
+ private NameIdentifier createThreeColumnTable(String tableName) {
+ NameIdentifier tableIdent =
+ NameIdentifierUtil.ofTable(METALAKE_NAME, CATALOG_NAME, SCHEMA_NAME, tableName);
+ Column[] columns =
+ new Column[] {
+ createColumn("col1", Types.StringType.get(), null),
+ createColumn("col2", Types.IntegerType.get(), Literals.integerLiteral(1)),
+ createColumn("col3", Types.StringType.get(), null)
+ };
+ tableOperations.createTable(
+ tableIdent,
+ columns,
+ "Test bundled alters",
+ StringIdentifier.newPropertiesWithId(
+ StringIdentifier.fromId(idGenerator.nextId()), Collections.emptyMap()),
+ new Transform[0],
+ Distributions.NONE,
+ new SortOrder[0],
+ Indexes.EMPTY_INDEXES);
+ return tableIdent;
+ }
+
@Test
public void testAlterTable() {
NameIdentifier table1Ident =