Skip to content
Merged
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 @@ -69,8 +69,7 @@ class BaseUpdatePartitionSpec implements UpdatePartitionSpec {
this.schema = spec.schema();
this.nameToField = indexSpecByName(spec);
this.transformToField = indexSpecByTransform(spec);
this.lastAssignedPartitionId =
base.specs().stream().mapToInt(PartitionSpec::lastAssignedFieldId).max().orElse(999);
this.lastAssignedPartitionId = base.lastAssignedFieldId();

spec.fields().stream()
.filter(field -> field.transform() instanceof UnknownTransform)
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/java/org/apache/iceberg/TableMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ public String toString() {
private final List<Snapshot> snapshots;
private final Map<Long, Snapshot> snapshotsById;
private final Map<Integer, PartitionSpec> specsById;
private final int lastAssignedFieldId;
private final Map<Integer, SortOrder> sortOrdersById;
private final List<HistoryEntry> snapshotLog;
private final List<MetadataLogEntry> previousFiles;
Expand Down Expand Up @@ -277,6 +278,7 @@ public String toString() {

this.snapshotsById = indexAndValidateSnapshots(snapshots, lastSequenceNumber);
this.specsById = indexSpecs(specs);
this.lastAssignedFieldId = specs().stream().mapToInt(PartitionSpec::lastAssignedFieldId).max().orElse(999);
this.sortOrdersById = indexSortOrders(sortOrders);

HistoryEntry last = null;
Expand Down Expand Up @@ -371,6 +373,10 @@ public Map<Integer, PartitionSpec> specsById() {
return specsById;
}

int lastAssignedFieldId() {
Copy link
Contributor

@rdblue rdblue Jan 29, 2021

Choose a reason for hiding this comment

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

I think this name is potentially confusing. It is the last assigned partition field ID, and "partition" is a key part of it. How about renaming it to lastAssignedPartitionId instead? Same with the instance field.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Updated the field name in TableMetadata.
In PartitionSpec, there is also a lastAssignedFieldId field. It seems to be fine as it is a field inside partition spec, which already indicates it is about partition.

return lastAssignedFieldId;
}

public int defaultSpecId() {
return defaultSpecId;
}
Expand Down
4 changes: 4 additions & 0 deletions core/src/test/java/org/apache/iceberg/TestTableMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ public void testJsonConversion() throws Exception {
expected.defaultSpecId(), metadata.defaultSpecId());
Assert.assertEquals("PartitionSpec map should match",
expected.specs(), metadata.specs());
Assert.assertEquals("lastAssignedFieldId across all PartitionSpecs should match",
expected.spec().lastAssignedFieldId(), metadata.lastAssignedFieldId());
Assert.assertEquals("Properties should match",
expected.properties(), metadata.properties());
Assert.assertEquals("Snapshot logs should match",
Expand Down Expand Up @@ -188,6 +190,8 @@ public void testBackwardCompat() throws Exception {
metadata.specs().get(0).compatibleWith(spec));
Assert.assertEquals("PartitionSpec should have ID TableMetadata.INITIAL_SPEC_ID",
TableMetadata.INITIAL_SPEC_ID, metadata.specs().get(0).specId());
Assert.assertEquals("lastAssignedFieldId across all PartitionSpecs should match",
expected.spec().lastAssignedFieldId(), metadata.lastAssignedFieldId());
Assert.assertEquals("Properties should match",
expected.properties(), metadata.properties());
Assert.assertEquals("Snapshot logs should match",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,19 @@ public void testAddAfterLastFieldRemoved() {
.removeField("data_bucket")
.commit();

V1Assert.assertEquals("Should add a new id bucket", PartitionSpec.builderFor(table.schema())
.withSpecId(1)
.alwaysNull("data", "data_bucket")
.build(), table.spec());
V1Assert.assertEquals("Should match the last assigned field id",
1000, table.spec().lastAssignedFieldId());
V2Assert.assertEquals("Should add a new id bucket", PartitionSpec.builderFor(table.schema())
.withSpecId(1)
.build(), table.spec());
V2Assert.assertEquals("Should match the last assigned field id",
999, table.spec().lastAssignedFieldId());
Assert.assertEquals(1000, table.ops().current().lastAssignedFieldId());

table.updateSpec()
.addField(bucket("id", 8))
.commit();
Expand All @@ -217,5 +230,6 @@ public void testAddAfterLastFieldRemoved() {
.add(1, 1001, "id_bucket_8", "bucket[8]")
.build(), table.spec());
Assert.assertEquals(1001, table.spec().lastAssignedFieldId());
Assert.assertEquals(1001, table.ops().current().lastAssignedFieldId());
}
}