Skip to content

Commit

Permalink
Core: Fix move/update/updateDoc/makeRequire/makeOptional fail after r…
Browse files Browse the repository at this point in the history
…ename schema (#10830)
  • Loading branch information
imtzer committed Feb 10, 2025
1 parent d935460 commit af2c9fb
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
19 changes: 10 additions & 9 deletions core/src/main/java/org/apache/iceberg/SchemaUpdate.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public UpdateSchema makeColumnOptional(String name) {
}

private void internalUpdateColumnRequirement(String name, boolean isOptional) {
Types.NestedField field = findField(name);
Types.NestedField field = findFieldFromUpdateFirst(name);
Preconditions.checkArgument(field != null, "Cannot update missing column: %s", name);

if ((!isOptional && field.isRequired()) || (isOptional && field.isOptional())) {
Expand Down Expand Up @@ -273,7 +273,7 @@ private void internalUpdateColumnRequirement(String name, boolean isOptional) {

@Override
public UpdateSchema updateColumn(String name, Type.PrimitiveType newType) {
Types.NestedField field = findField(name);
Types.NestedField field = findFieldFromUpdateFirst(name);
Preconditions.checkArgument(field != null, "Cannot update missing column: %s", name);
Preconditions.checkArgument(
!deletes.contains(field.fieldId()),
Expand Down Expand Up @@ -309,7 +309,7 @@ public UpdateSchema updateColumn(String name, Type.PrimitiveType newType) {

@Override
public UpdateSchema updateColumnDoc(String name, String doc) {
Types.NestedField field = findField(name);
Types.NestedField field = findFieldFromUpdateFirst(name);
Preconditions.checkArgument(field != null, "Cannot update missing column: %s", name);
Preconditions.checkArgument(
!deletes.contains(field.fieldId()),
Expand Down Expand Up @@ -391,13 +391,14 @@ private Integer findForMove(String name) {
if (addedId != null) {
return addedId;
}
Types.NestedField field = findFieldFromUpdateFirst(name);
return field != null ? field.fieldId() : null;
}

Types.NestedField field = findField(name);
if (field != null) {
return field.fieldId();
}

return null;
private Types.NestedField findFieldFromUpdateFirst(String name) {
List<Types.NestedField> updatedFields =
updates.values().stream().filter(f -> f.name().equals(name)).collect(Collectors.toList());
return !updatedFields.isEmpty() ? updatedFields.get(0) : findField(name);
}

private void internalMove(String name, Move move) {
Expand Down
48 changes: 48 additions & 0 deletions core/src/test/java/org/apache/iceberg/TestSchemaUpdate.java
Original file line number Diff line number Diff line change
Expand Up @@ -2193,4 +2193,52 @@ public void testMoveDeletedNestedStructFieldToFirst() {

assertThat(actual.asStruct()).isEqualTo(expected.asStruct());
}

@Test
public void testUpdateDocAfterRename() {
Schema schema =
new Schema(
required(1, "b", Types.IntegerType.get()), required(2, "c", Types.IntegerType.get()));

Schema actual =
new SchemaUpdate(schema, 2).renameColumn("c", "a").updateColumnDoc("a", "doc of a").apply();

Schema expected =
new Schema(
required(1, "b", Types.IntegerType.get()),
required(2, "a", Types.IntegerType.get(), "doc of a"));
assertThat(actual.asStruct()).isEqualTo(expected.asStruct());
}

@Test
public void testUpdateAfterRename() {
Schema schema =
new Schema(
required(1, "b", Types.IntegerType.get()), required(2, "c", Types.IntegerType.get()));

Schema actual =
new SchemaUpdate(schema, 2)
.renameColumn("c", "a")
.updateColumn("a", Types.LongType.get())
.apply();

Schema expected =
new Schema(
required(1, "b", Types.IntegerType.get()), required(2, "a", Types.LongType.get()));
assertThat(actual.asStruct()).isEqualTo(expected.asStruct());
}

@Test
public void testMoveAfterRename() {
Schema schema =
new Schema(
required(1, "b", Types.IntegerType.get()), required(2, "c", Types.IntegerType.get()));

Schema actual = new SchemaUpdate(schema, 2).renameColumn("c", "a").moveBefore("a", "b").apply();

Schema expected =
new Schema(
required(2, "a", Types.IntegerType.get()), required(1, "b", Types.IntegerType.get()));
assertThat(actual.asStruct()).isEqualTo(expected.asStruct());
}
}

0 comments on commit af2c9fb

Please sign in to comment.