Skip to content
Merged
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
15 changes: 10 additions & 5 deletions core/src/main/java/org/apache/iceberg/TableMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -738,11 +738,16 @@ private static PartitionSpec freshSpec(int specId, Schema schema, PartitionSpec
for (PartitionField field : partitionSpec.fields()) {
// look up the name of the source field in the old schema to get the new schema's id
String sourceName = partitionSpec.schema().findColumnName(field.sourceId());
specBuilder.addField(
field.transform().toString(),
schema.findField(sourceName).fieldId(),
field.fieldId(),
field.name());

final int fieldId;
if (sourceName != null) {
fieldId = schema.findField(sourceName).fieldId();
} else {
// In the case of a null sourceName, the column has been deleted.
// This only happens in V1 tables where the reference is still around as a void transform
fieldId = field.sourceId();
}
specBuilder.addField(field.transform().toString(), fieldId, field.fieldId(), field.name());
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: new empty line?

}

return specBuilder.build().bind(schema);
Expand Down
21 changes: 21 additions & 0 deletions core/src/test/java/org/apache/iceberg/TestPartitioning.java
Original file line number Diff line number Diff line change
Expand Up @@ -386,4 +386,25 @@ public void testGroupingKeyTypeWithIncompatibleSpecEvolution() {
"Conflicting partition fields",
() -> Partitioning.groupingKeyType(table.schema(), table.specs().values()));
}

@Test
public void testDeletingPartitionField() {
TestTables.TestTable table =
TestTables.create(tableDir, "test", SCHEMA, BY_DATA_SPEC, V1_FORMAT_VERSION);
Copy link
Contributor

Choose a reason for hiding this comment

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

should we maybe also add a test that does the same with V2_FORMAT_VERSION?


table.updateSpec().removeField("data").commit();

table.updateSchema().deleteColumn("data").commit();

table.updateSpec().addField("id").commit();

PartitionSpec spec =
PartitionSpec.builderFor(SCHEMA)
.withSpecId(2)
.alwaysNull("data", "data")
.identity("id")
.build();

Assert.assertEquals("The spec should be there", spec, table.spec());
}
}