Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -114,6 +116,12 @@ private static Map<Integer, Supplier<UpdateSchema>> updateColumn(
String parentPath) {
Map<Integer, Supplier<UpdateSchema>> updates = new HashMap<>();
if (!latestColumn.equals(currentColumn)) {
// update the name of the column
if (!latestColumn.name().equals(currentColumn.name())) {
updates.put(
latestColumn.fieldId(),
() -> updateSchema.renameColumn(currentColumn.name(), latestColumn.name()));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think column name should be done after updating the type?
The UT's are failing are probably because of this.

Copy link
Author

Choose a reason for hiding this comment

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

I moved after updating the type and still it is failing. I will take a look

// update the type of the column
if (latestColumn.type().isPrimitiveType()
&& !latestColumn.type().equals(currentColumn.type())) {
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