pvary commented on code in PR #14508: URL: https://github.com/apache/iceberg/pull/14508#discussion_r2523130907
########## core/src/main/java/org/apache/iceberg/BasePartitionStatistics.java: ########## @@ -0,0 +1,353 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.iceberg; + +import org.apache.iceberg.avro.SupportsIndexProjection; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.types.Types; + +public class BasePartitionStatistics extends SupportsIndexProjection + implements PartitionStatistics { + + private StructLike partition; + private Integer specId; + private Long dataRecordCount; + private Integer dataFileCount; + private Long totalDataFileSizeInBytes; + private Long positionDeleteRecordCount; + private Integer positionDeleteFileCount; + private Long equalityDeleteRecordCount; + private Integer equalityDeleteFileCount; + private Long totalRecordCount; // null by default + private Long lastUpdatedAt; // null by default + private Long lastUpdatedSnapshotId; // null by default + private Integer dvCount; + + private static final int STATS_COUNT = 13; + + public BasePartitionStatistics(StructLike partition, int specId) { + super(STATS_COUNT); + + this.partition = partition; + this.specId = specId; + + dataRecordCount = 0L; + dataFileCount = 0; + totalDataFileSizeInBytes = 0L; + positionDeleteRecordCount = 0L; + positionDeleteFileCount = 0; + equalityDeleteRecordCount = 0L; + equalityDeleteFileCount = 0; + dvCount = 0; + } + + /** Used by internal readers to instantiate this class with a projection schema. */ + public BasePartitionStatistics(Types.StructType projection) { + super(STATS_COUNT); + + // TODO: For implementing projection using a custom Schema here we have to call the other super + // constructor with the base Schema and the projection Schema. + } + + @Override + public StructLike partition() { + return partition; + } + + @Override + public Integer specId() { + return specId; + } + + @Override + public Long dataRecordCount() { + return dataRecordCount; + } + + @Override + public Integer dataFileCount() { + return dataFileCount; + } + + @Override + public Long totalDataFileSizeInBytes() { + return totalDataFileSizeInBytes; + } + + @Override + public Long positionDeleteRecordCount() { + return positionDeleteRecordCount; + } + + @Override + public Integer positionDeleteFileCount() { + return positionDeleteFileCount; + } + + @Override + public Long equalityDeleteRecordCount() { + return equalityDeleteRecordCount; + } + + @Override + public Integer equalityDeleteFileCount() { + return equalityDeleteFileCount; + } + + @Override + public Long totalRecords() { + return totalRecordCount; + } + + @Override + public Long lastUpdatedAt() { + return lastUpdatedAt; + } + + @Override + public Long lastUpdatedSnapshotId() { + return lastUpdatedSnapshotId; + } + + @Override + public Integer dvCount() { + return dvCount; + } + + /** + * Updates the partition stats from the data/delete file. + * + * @param file the {@link ContentFile} from the manifest entry. + * @param snapshot the snapshot corresponding to the live entry. + */ + void liveEntry(ContentFile<?> file, Snapshot snapshot) { + Preconditions.checkArgument(file.specId() == specId, "Spec IDs must match"); + + switch (file.content()) { + case DATA: + this.dataRecordCount += file.recordCount(); + this.dataFileCount += 1; + this.totalDataFileSizeInBytes += file.fileSizeInBytes(); + break; + case POSITION_DELETES: + this.positionDeleteRecordCount += file.recordCount(); + if (file.format() == FileFormat.PUFFIN) { + this.dvCount += 1; + } else { + this.positionDeleteFileCount += 1; + } + + break; + case EQUALITY_DELETES: + this.equalityDeleteRecordCount += file.recordCount(); + this.equalityDeleteFileCount += 1; + break; + default: + throw new UnsupportedOperationException("Unsupported file content type: " + file.content()); + } + + if (snapshot != null) { + updateSnapshotInfo(snapshot.snapshotId(), snapshot.timestampMillis()); + } + + // Note: Not computing the `TOTAL_RECORD_COUNT` for now as it needs scanning the data. + } + + /** + * Updates the modified time and snapshot ID for the deleted manifest entry. + * + * @param snapshot the snapshot corresponding to the deleted manifest entry. + */ + void deletedEntry(Snapshot snapshot) { + if (snapshot != null) { + updateSnapshotInfo(snapshot.snapshotId(), snapshot.timestampMillis()); + } + } + + /** + * Decrement the counters as it was included in the previous stats and updates the modified time + * and snapshot ID for the deleted manifest entry. + * + * @param snapshot the snapshot corresponding to the deleted manifest entry. + */ + void deletedEntryForIncrementalCompute(ContentFile<?> file, Snapshot snapshot) { + Preconditions.checkArgument(file.specId() == specId, "Spec IDs must match"); + + switch (file.content()) { + case DATA: + this.dataRecordCount -= file.recordCount(); + this.dataFileCount -= 1; + this.totalDataFileSizeInBytes -= file.fileSizeInBytes(); + break; + case POSITION_DELETES: + this.positionDeleteRecordCount -= file.recordCount(); + if (file.format() == FileFormat.PUFFIN) { + this.dvCount -= 1; + } else { + this.positionDeleteFileCount -= 1; + } + + break; + case EQUALITY_DELETES: + this.equalityDeleteRecordCount -= file.recordCount(); + this.equalityDeleteFileCount -= 1; + break; + default: + throw new UnsupportedOperationException("Unsupported file content type: " + file.content()); + } + + if (snapshot != null) { + updateSnapshotInfo(snapshot.snapshotId(), snapshot.timestampMillis()); + } + } + + /** + * Appends statistics from given entry to current entry. + * + * @param entry the entry from which statistics will be sourced. + */ + void appendStats(PartitionStatistics entry) { + Preconditions.checkArgument(entry.specId() != null, "Invalid spec ID: mull"); + Preconditions.checkArgument(entry.specId().equals(this.specId), "Spec IDs must match"); + + this.dataRecordCount += entry.dataRecordCount(); + this.dataFileCount += entry.dataFileCount(); + this.totalDataFileSizeInBytes += entry.totalDataFileSizeInBytes(); + this.positionDeleteRecordCount += entry.positionDeleteRecordCount(); + this.positionDeleteFileCount += entry.positionDeleteFileCount(); + this.equalityDeleteRecordCount += entry.equalityDeleteRecordCount(); + this.equalityDeleteFileCount += entry.equalityDeleteFileCount(); Review Comment: What happens when one of these are null? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
