Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: Fix move/update/makeRequire/makeOptional fail after rename schema (#10830) #12202

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 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,19 @@ 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());
Comment on lines +399 to +400
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: The size of updatedFields should be 0 or 1, right? How about verifying the size with Preconditions.checkArgument method?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: The size of updatedFields should be 0 or 1, right? How about verifying the size with Preconditions.checkArgument method?

Thanks for review!
I have thought about the size of updatedFields before, as you say it should be 0 or 1, but if your call renameColumn for different column to a same name , the size will greater than 1, but validation has been set in applyChanges, so I don't add a Preconditions.checkArgument
Should I add a Preconditions.checkArgument? Add would be better, validate violation at method call

Preconditions.checkArgument(
updatedFields.size() <= 1,
"Find duplicated field of name %s in id %s",
name,
updatedFields.stream().map(Types.NestedField::fieldId).toArray());
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());
}
}