Skip to content

Commit

Permalink
Add TableMetadata support for statistics information
Browse files Browse the repository at this point in the history
  • Loading branch information
findepi committed Aug 5, 2022
1 parent 07418e2 commit f439151
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
34 changes: 34 additions & 0 deletions core/src/main/java/org/apache/iceberg/MetadataUpdate.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,40 @@ public void applyTo(TableMetadata.Builder metadataBuilder) {
}
}

class SetStatistics implements MetadataUpdate {
private final StatisticsFile statisticsFile;

public SetStatistics(StatisticsFile statisticsFile) {
this.statisticsFile = statisticsFile;
}

public StatisticsFile statisticsFile() {
return statisticsFile;
}

@Override
public void applyTo(TableMetadata.Builder metadataBuilder) {
metadataBuilder.setStatistics(statisticsFile);
}
}

class RemoveStatistics implements MetadataUpdate {
private final long snapshotId;

public RemoveStatistics(long snapshotId) {
this.snapshotId = snapshotId;
}

public long snapshotId() {
return snapshotId;
}

@Override
public void applyTo(TableMetadata.Builder metadataBuilder) {
metadataBuilder.removeStatistics(snapshotId);
}
}

class AddSnapshot implements MetadataUpdate {
private final Snapshot snapshot;

Expand Down
26 changes: 26 additions & 0 deletions core/src/main/java/org/apache/iceberg/TableMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ public String toString() {
private final List<HistoryEntry> snapshotLog;
private final List<MetadataLogEntry> previousFiles;
private final Map<String, SnapshotRef> refs;
private final List<StatisticsFile> statistics;
private final List<MetadataUpdate> changes;

@SuppressWarnings("checkstyle:CyclomaticComplexity")
Expand All @@ -267,6 +268,7 @@ public String toString() {
List<HistoryEntry> snapshotLog,
List<MetadataLogEntry> previousFiles,
Map<String, SnapshotRef> refs,
List<StatisticsFile> statistics,
List<MetadataUpdate> changes) {
Preconditions.checkArgument(
specs != null && !specs.isEmpty(), "Partition specs cannot be null or empty");
Expand Down Expand Up @@ -314,6 +316,7 @@ public String toString() {
this.specsById = indexSpecs(specs);
this.sortOrdersById = indexSortOrders(sortOrders);
this.refs = validateRefs(currentSnapshotId, refs, snapshotsById);
this.statistics = ImmutableList.copyOf(statistics);

HistoryEntry last = null;
for (HistoryEntry logEntry : snapshotLog) {
Expand Down Expand Up @@ -489,6 +492,10 @@ public Map<String, SnapshotRef> refs() {
return refs;
}

public List<StatisticsFile> statistics() {
return statistics;
}

public List<HistoryEntry> snapshotLog() {
return snapshotLog;
}
Expand Down Expand Up @@ -817,6 +824,7 @@ public static class Builder {
private long currentSnapshotId;
private List<Snapshot> snapshots;
private final Map<String, SnapshotRef> refs;
private final Map<Long, List<StatisticsFile>> statistics;

// change tracking
private final List<MetadataUpdate> changes;
Expand Down Expand Up @@ -853,6 +861,7 @@ private Builder() {
this.snapshotLog = Lists.newArrayList();
this.previousFiles = Lists.newArrayList();
this.refs = Maps.newHashMap();
this.statistics = Maps.newHashMap();
this.snapshotsById = Maps.newHashMap();
this.schemasById = Maps.newHashMap();
this.specsById = Maps.newHashMap();
Expand Down Expand Up @@ -884,6 +893,8 @@ private Builder(TableMetadata base) {
this.previousFileLocation = base.metadataFileLocation;
this.previousFiles = base.previousFiles;
this.refs = Maps.newHashMap(base.refs);
this.statistics =
base.statistics.stream().collect(Collectors.groupingBy(StatisticsFile::snapshotId));

this.snapshotsById = Maps.newHashMap(base.snapshotsById);
this.schemasById = Maps.newHashMap(base.schemasById);
Expand Down Expand Up @@ -1176,6 +1187,20 @@ public Builder removeBranch(String branch) {
return this;
}

public Builder setStatistics(StatisticsFile statisticsFile) {
Preconditions.checkNotNull(statisticsFile, "statisticsFile is null");
this.statistics.put(statisticsFile.snapshotId(), ImmutableList.of(statisticsFile));
changes.add(new MetadataUpdate.SetStatistics(statisticsFile));
return this;
}

public Builder removeStatistics(long snapshotId) {
Preconditions.checkNotNull(snapshotId, "snapshotId is null");
this.statistics.remove(snapshotId);
changes.add(new MetadataUpdate.RemoveStatistics(snapshotId));
return this;
}

public Builder removeSnapshots(List<Snapshot> snapshotsToRemove) {
Set<Long> idsToRemove =
snapshotsToRemove.stream().map(Snapshot::snapshotId).collect(Collectors.toSet());
Expand Down Expand Up @@ -1313,6 +1338,7 @@ public TableMetadata build() {
ImmutableList.copyOf(newSnapshotLog),
ImmutableList.copyOf(metadataHistory),
ImmutableMap.copyOf(refs),
statistics.values().stream().flatMap(List::stream).collect(Collectors.toList()),
discardChanges ? ImmutableList.of() : ImmutableList.copyOf(changes));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ static TableMetadata fromJson(FileIO io, String metadataLocation, JsonNode node)
entries.build(),
metadataEntries.build(),
refs,
ImmutableList.of(), /* TODO: support statistics */
ImmutableList.of() /* no changes from the file */);
}

Expand Down
11 changes: 11 additions & 0 deletions core/src/test/java/org/apache/iceberg/TestTableMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ public void testJsonConversion() throws Exception {
snapshotLog,
ImmutableList.of(),
refs,
ImmutableList.of(),
ImmutableList.of());

String asJson = TableMetadataParser.toJson(expected);
Expand Down Expand Up @@ -272,6 +273,7 @@ public void testBackwardCompat() throws Exception {
ImmutableList.of(),
ImmutableList.of(),
ImmutableMap.of(),
ImmutableList.of(),
ImmutableList.of());

String asJson = toJsonWithoutSpecAndSchemaList(expected);
Expand Down Expand Up @@ -415,6 +417,7 @@ public void testInvalidMainBranch() {
snapshotLog,
ImmutableList.of(),
refs,
ImmutableList.of(),
ImmutableList.of()));
}

Expand Down Expand Up @@ -464,6 +467,7 @@ public void testMainWithoutCurrent() {
ImmutableList.of(),
ImmutableList.of(),
refs,
ImmutableList.of(),
ImmutableList.of()));
}

Expand Down Expand Up @@ -502,6 +506,7 @@ public void testBranchSnapshotMissing() {
ImmutableList.of(),
ImmutableList.of(),
refs,
ImmutableList.of(),
ImmutableList.of()));
}

Expand Down Expand Up @@ -607,6 +612,7 @@ public void testJsonWithPreviousMetadataLog() throws Exception {
reversedSnapshotLog,
ImmutableList.copyOf(previousMetadataLog),
ImmutableMap.of(),
ImmutableList.of(),
ImmutableList.of());

String asJson = TableMetadataParser.toJson(base);
Expand Down Expand Up @@ -686,6 +692,7 @@ public void testAddPreviousMetadataRemoveNone() {
reversedSnapshotLog,
ImmutableList.copyOf(previousMetadataLog),
ImmutableMap.of(),
ImmutableList.of(),
ImmutableList.of());

previousMetadataLog.add(latestPreviousMetadata);
Expand Down Expand Up @@ -783,6 +790,7 @@ public void testAddPreviousMetadataRemoveOne() {
reversedSnapshotLog,
ImmutableList.copyOf(previousMetadataLog),
ImmutableMap.of(),
ImmutableList.of(),
ImmutableList.of());

previousMetadataLog.add(latestPreviousMetadata);
Expand Down Expand Up @@ -886,6 +894,7 @@ public void testAddPreviousMetadataRemoveMultiple() {
reversedSnapshotLog,
ImmutableList.copyOf(previousMetadataLog),
ImmutableMap.of(),
ImmutableList.of(),
ImmutableList.of());

previousMetadataLog.add(latestPreviousMetadata);
Expand Down Expand Up @@ -935,6 +944,7 @@ public void testV2UUIDValidation() {
ImmutableList.of(),
ImmutableList.of(),
ImmutableMap.of(),
ImmutableList.of(),
ImmutableList.of()));
}

Expand Down Expand Up @@ -967,6 +977,7 @@ public void testVersionValidation() {
ImmutableList.of(),
ImmutableList.of(),
ImmutableMap.of(),
ImmutableList.of(),
ImmutableList.of()));
}

Expand Down

0 comments on commit f439151

Please sign in to comment.