ajantha-bhat commented on code in PR #9170: URL: https://github.com/apache/iceberg/pull/9170#discussion_r1407505119
########## core/src/main/java/org/apache/iceberg/Partition.java: ########## @@ -0,0 +1,355 @@ +/* + * 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 java.util.Objects; +import org.apache.avro.Schema; +import org.apache.avro.generic.IndexedRecord; +import org.apache.iceberg.avro.AvroSchemaUtil; +import org.apache.iceberg.types.Types; + +public class Partition implements IndexedRecord { + private PartitionData partitionData; + private int specId; + private long dataRecordCount; + private int dataFileCount; + private long dataFileSizeInBytes; + private long posDeleteRecordCount; + private int posDeleteFileCount; + private long eqDeleteRecordCount; + private int eqDeleteFileCount; + // Optional accurate count of records in a partition after applying the delete files if any + private long totalRecordCount; + // Commit time of snapshot that last updated this partition + private long lastUpdatedAt; + // ID of snapshot that last updated this partition + private long lastUpdatedSnapshotId; + + public enum Column { + PARTITION_DATA, + SPEC_ID, + DATA_RECORD_COUNT, + DATA_FILE_COUNT, + DATA_FILE_SIZE_IN_BYTES, + POSITION_DELETE_RECORD_COUNT, + POSITION_DELETE_FILE_COUNT, + EQUALITY_DELETE_RECORD_COUNT, + EQUALITY_DELETE_FILE_COUNT, + TOTAL_RECORD_COUNT, + LAST_UPDATED_AT, + LAST_UPDATED_SNAPSHOT_ID + } + + public Partition() {} + + public static Builder builder() { + return new Builder(); + } + + public PartitionData partitionData() { + return partitionData; + } + + public int specId() { + return specId; + } + + public long dataRecordCount() { + return dataRecordCount; + } + + public int dataFileCount() { + return dataFileCount; + } + + public long dataFileSizeInBytes() { + return dataFileSizeInBytes; + } + + public Long posDeleteRecordCount() { + return posDeleteRecordCount; + } + + public Integer posDeleteFileCount() { + return posDeleteFileCount; + } + + public Long eqDeleteRecordCount() { + return eqDeleteRecordCount; + } + + public Integer eqDeleteFileCount() { + return eqDeleteFileCount; + } + + public Long totalRecordCount() { + return totalRecordCount; + } + + public Long lastUpdatedAt() { + return lastUpdatedAt; + } + + public Long lastUpdatedSnapshotId() { + return lastUpdatedSnapshotId; + } + + @Override + public void put(int i, Object v) { + switch (i) { + case 0: + this.partitionData = (PartitionData) v; + return; + case 1: + this.specId = (int) v; + return; + case 2: + this.dataRecordCount = (long) v; + return; + case 3: + this.dataFileCount = (int) v; + return; + case 4: + this.dataFileSizeInBytes = (long) v; + return; + case 5: + this.posDeleteRecordCount = (long) v; + return; + case 6: + this.posDeleteFileCount = (int) v; + return; + case 7: + this.eqDeleteRecordCount = (long) v; + return; + case 8: + this.eqDeleteFileCount = (int) v; + return; + case 9: + this.totalRecordCount = (long) v; + return; + case 10: + this.lastUpdatedAt = (long) v; + return; + case 11: + this.lastUpdatedSnapshotId = (long) v; + return; + default: + throw new UnsupportedOperationException("Unknown field ordinal: " + i); + } + } + + @Override + public Object get(int i) { + switch (i) { + case 0: + return partitionData; + case 1: + return specId; + case 2: + return dataRecordCount; + case 3: + return dataFileCount; + case 4: + return dataFileSizeInBytes; + case 5: + return posDeleteRecordCount; + case 6: + return posDeleteFileCount; + case 7: + return eqDeleteRecordCount; + case 8: + return eqDeleteFileCount; + case 9: + return totalRecordCount; + case 10: + return lastUpdatedAt; + case 11: + return lastUpdatedSnapshotId; + default: + throw new UnsupportedOperationException("Unknown field ordinal: " + i); + } + } + + @Override + public Schema getSchema() { + return prepareAvroSchema(partitionData.getPartitionType()); + } + + @Override + @SuppressWarnings("checkstyle:CyclomaticComplexity") + public boolean equals(Object o) { + if (this == o) { + return true; + } else if (!(o instanceof Partition)) { + return false; + } + + Partition that = (Partition) o; + return partitionData.equals(that.partitionData) + && specId == that.specId + && dataRecordCount == that.dataRecordCount + && dataFileCount == that.dataFileCount + && dataFileSizeInBytes == that.dataFileSizeInBytes + && posDeleteRecordCount == that.posDeleteRecordCount + && posDeleteFileCount == that.posDeleteFileCount + && eqDeleteRecordCount == that.eqDeleteRecordCount + && eqDeleteFileCount == that.eqDeleteFileCount + && totalRecordCount == that.totalRecordCount + && lastUpdatedAt == that.lastUpdatedAt + && lastUpdatedSnapshotId == that.lastUpdatedSnapshotId; + } + + @Override + public int hashCode() { + return Objects.hash( + partitionData, + specId, + dataRecordCount, + dataFileCount, + dataFileSizeInBytes, + posDeleteRecordCount, + posDeleteFileCount, + eqDeleteRecordCount, + eqDeleteFileCount, + totalRecordCount, + lastUpdatedAt, + lastUpdatedSnapshotId); + } + + public static org.apache.iceberg.Schema icebergSchema(Types.StructType partitionType) { + if (partitionType.fields().isEmpty()) { + throw new IllegalArgumentException("getting schema for an unpartitioned table"); + } + + return new org.apache.iceberg.Schema( + Types.NestedField.required(1, Column.PARTITION_DATA.name(), partitionType), + Types.NestedField.required(2, Column.SPEC_ID.name(), Types.IntegerType.get()), + Types.NestedField.required(3, Column.DATA_RECORD_COUNT.name(), Types.LongType.get()), + Types.NestedField.required(4, Column.DATA_FILE_COUNT.name(), Types.IntegerType.get()), + Types.NestedField.required(5, Column.DATA_FILE_SIZE_IN_BYTES.name(), Types.LongType.get()), + Types.NestedField.optional( + 6, Column.POSITION_DELETE_RECORD_COUNT.name(), Types.LongType.get()), + Types.NestedField.optional( + 7, Column.POSITION_DELETE_FILE_COUNT.name(), Types.IntegerType.get()), + Types.NestedField.optional( + 8, Column.EQUALITY_DELETE_RECORD_COUNT.name(), Types.LongType.get()), + Types.NestedField.optional( + 9, Column.EQUALITY_DELETE_FILE_COUNT.name(), Types.IntegerType.get()), + Types.NestedField.optional(10, Column.TOTAL_RECORD_COUNT.name(), Types.LongType.get()), + Types.NestedField.optional(11, Column.LAST_UPDATED_AT.name(), Types.LongType.get()), + Types.NestedField.optional( + 12, Column.LAST_UPDATED_SNAPSHOT_ID.name(), Types.LongType.get())); + } + + private static Schema prepareAvroSchema(Types.StructType partitionType) { + return AvroSchemaUtil.convert(icebergSchema(partitionType), "partitionEntry"); + } + + public static class Builder { Review Comment: using the builder instead of `Immutables` because. These objects will be mutable in a partition stats map. Will be adding the update functions during impl. ########## data/src/main/java/org/apache/iceberg/data/PartitionStatsUtil.java: ########## @@ -0,0 +1,136 @@ +/* + * 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.data; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import org.apache.avro.generic.GenericData; +import org.apache.iceberg.Partition; +import org.apache.iceberg.PartitionData; +import org.apache.iceberg.PartitionSpec; +import org.apache.iceberg.Partitioning; +import org.apache.iceberg.Schema; +import org.apache.iceberg.SortOrder; +import org.apache.iceberg.io.CloseableIterable; +import org.apache.iceberg.io.DataWriter; +import org.apache.iceberg.io.InputFile; +import org.apache.iceberg.io.OutputFile; +import org.apache.iceberg.parquet.Parquet; +import org.apache.iceberg.parquet.ParquetAvroValueReaders; +import org.apache.iceberg.parquet.ParquetAvroWriter; +import org.apache.iceberg.types.Types; + +public final class PartitionStatsUtil { + + private PartitionStatsUtil() {} + + private static final String PARQUET_SUFFIX = ".parquet"; + + public static void writePartitionStatsFile( + Iterator<Partition> partitions, OutputFile outputFile, Collection<PartitionSpec> specs) { + validateFormat(outputFile.location()); + writeAsParquetFile( + Partition.icebergSchema(Partitioning.partitionType(specs)), partitions, outputFile); + } + + private static void validateFormat(String filePath) { + if (!filePath.toLowerCase().endsWith(PARQUET_SUFFIX)) { Review Comment: Other formats will be supported in the follow up PR -- 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: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org