Skip to content

Added a fix for column rename in IcebergSchemaSync #712

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

Open
wants to merge 2 commits 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
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ private static Map<Integer, Supplier<UpdateSchema>> addUpdates(
UpdateSchema updateSchema,
String parentPath) {
Map<Integer, Supplier<UpdateSchema>> updates = new HashMap<>();
Set<String> allColumnNames = new HashSet<>();
current.fields().stream().map(Types.NestedField::name).forEach(allColumnNames::add);
latest.fields().stream().map(Types.NestedField::name).forEach(allColumnNames::add);
for (String columnName : allColumnNames) {
Types.NestedField latestColumn = latest.field(columnName);
Types.NestedField currentColumn = current.field(columnName);
Set<Integer> allColumnFieldIds = new HashSet<>();
current.fields().stream().map(Types.NestedField::fieldId).forEach(allColumnFieldIds::add);
latest.fields().stream().map(Types.NestedField::fieldId).forEach(allColumnFieldIds::add);
for (int columnFieldId : allColumnFieldIds) {
Types.NestedField latestColumn = latest.field(columnFieldId);
Types.NestedField currentColumn = current.field(columnFieldId);
if (currentColumn == null) {
// add a new column
if (latestColumn.isOptional()) {
Expand All @@ -99,7 +99,9 @@ private static Map<Integer, Supplier<UpdateSchema>> addUpdates(
// drop the existing column, use fieldId 0 to perform deletes first
updates.put(
0,
() -> updateSchema.deleteColumn(constructFullyQualifiedName(columnName, parentPath)));
() ->
updateSchema.deleteColumn(
constructFullyQualifiedName(currentColumn.name(), parentPath)));
} else {
updates.putAll(updateColumn(latestColumn, currentColumn, updateSchema, parentPath));
}
Expand All @@ -123,6 +125,12 @@ private static Map<Integer, Supplier<UpdateSchema>> updateColumn(
updateSchema.updateColumn(
latestColumn.name(), latestColumn.type().asPrimitiveType()));
}
// update the name of the column
if (!latestColumn.name().equals(currentColumn.name())) {
updates.put(
latestColumn.fieldId(),
() -> updateSchema.renameColumn(currentColumn.name(), latestColumn.name()));
}
// update whether the column is required
if (latestColumn.isOptional() != currentColumn.isOptional()) {
if (latestColumn.isOptional()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,33 @@ void testAddMapFieldComment() {
verify(mockUpdateSchema).commit();
}

@Test
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add a unit test which writes an actual iceberg table to a tempDir path? There are many examples in ITConversionController to follow from.

Copy link
Author

Choose a reason for hiding this comment

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

I will take a look and will add it @vinishjail97

public void testUpdateColumnName() {
UpdateSchema mockUpdateSchema = Mockito.mock(UpdateSchema.class);
when(mockTransaction.updateSchema()).thenReturn(mockUpdateSchema);
schemaSync.sync(SCHEMA, updateColumnName(2), mockTransaction);

verify(mockUpdateSchema).renameColumn(SCHEMA.findColumnName(2), "updateColumnName");
verify(mockUpdateSchema).commit();
}

private Schema updateColumnName(int fieldId) {
List<Types.NestedField> fields = new ArrayList<>();
for (Types.NestedField existingField : SCHEMA.columns()) {
if (existingField.fieldId() == fieldId) {
fields.add(
Types.NestedField.of(
existingField.fieldId(),
existingField.isOptional(),
"updateColumnName",
existingField.type()));
} else {
fields.add(existingField);
}
}
return new Schema(fields);
}

private Schema addColumnToDefault(Schema schema, Types.NestedField field, Integer parentId) {
List<Types.NestedField> fields = new ArrayList<>();
for (Types.NestedField existingField : schema.columns()) {
Expand Down
Loading