ahmedabu98 commented on code in PR #32666: URL: https://github.com/apache/beam/pull/32666#discussion_r1792383861
########## sdks/java/io/iceberg/src/main/java/org/apache/beam/sdk/io/iceberg/SerializableDataFile.java: ########## @@ -0,0 +1,206 @@ +/* + * 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.beam.sdk.io.iceberg; + +import com.google.auto.value.AutoValue; +import java.nio.ByteBuffer; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.apache.beam.sdk.schemas.AutoValueSchema; +import org.apache.beam.sdk.schemas.annotations.DefaultSchema; +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions; +import org.apache.iceberg.DataFile; +import org.apache.iceberg.DataFiles; +import org.apache.iceberg.FileFormat; +import org.apache.iceberg.Metrics; +import org.apache.iceberg.PartitionKey; +import org.apache.iceberg.PartitionSpec; +import org.checkerframework.checker.nullness.qual.Nullable; + +/** + * Serializable version of an Iceberg {@link DataFile}. + * + * <p>{@link DataFile} is not serializable and the Iceberg API doesn't offer an easy way to + * encode/decode it. This class is an identical version that can be used as a PCollection element + * type. + * + * <p>Use {@link #from(DataFile, PartitionKey)} to create a {@link SerializableDataFile} and {@link + * #createDataFile(PartitionSpec)} to reconstruct the original {@link DataFile}. + */ +@DefaultSchema(AutoValueSchema.class) +@AutoValue +abstract class SerializableDataFile { + public static Builder builder() { + return new AutoValue_SerializableDataFile.Builder(); + } + + abstract String getPath(); + + abstract String getFileFormat(); + + abstract long getRecordCount(); + + abstract long getFileSizeInBytes(); + + abstract String getPartitionPath(); + + abstract int getPartitionSpecId(); + + abstract @Nullable ByteBuffer getKeyMetadata(); + + abstract @Nullable List<Long> getSplitOffsets(); + + abstract @Nullable Map<Integer, Long> getColumnSizes(); + + abstract @Nullable Map<Integer, Long> getValueCounts(); + + abstract @Nullable Map<Integer, Long> getNullValueCounts(); + + abstract @Nullable Map<Integer, Long> getNanValueCounts(); + + abstract @Nullable Map<Integer, byte[]> getLowerBounds(); + + abstract @Nullable Map<Integer, byte[]> getUpperBounds(); + + @AutoValue.Builder + abstract static class Builder { + abstract Builder setPath(String path); + + abstract Builder setFileFormat(String fileFormat); + + abstract Builder setRecordCount(long recordCount); + + abstract Builder setFileSizeInBytes(long fileSizeInBytes); + + abstract Builder setPartitionPath(String partitionPath); + + abstract Builder setPartitionSpecId(int partitionSpec); + + abstract Builder setKeyMetadata(ByteBuffer keyMetadata); + + abstract Builder setSplitOffsets(List<Long> splitOffsets); + + abstract Builder setColumnSizes(Map<Integer, Long> columnSizes); + + abstract Builder setValueCounts(Map<Integer, Long> valueCounts); + + abstract Builder setNullValueCounts(Map<Integer, Long> nullValueCounts); + + abstract Builder setNanValueCounts(Map<Integer, Long> nanValueCounts); + + abstract Builder setLowerBounds(Map<Integer, byte[]> lowerBounds); + + abstract Builder setUpperBounds(Map<Integer, byte[]> upperBounds); + + abstract SerializableDataFile build(); + } + + /** + * Create a {@link SerializableDataFile} from a {@link DataFile} and its associated {@link + * PartitionKey}. + */ + static SerializableDataFile from(DataFile f, PartitionKey key) { + SerializableDataFile.Builder builder = + SerializableDataFile.builder() + .setPath(f.path().toString()) + .setFileFormat(f.format().toString()) + .setRecordCount(f.recordCount()) + .setFileSizeInBytes(f.fileSizeInBytes()) + .setPartitionPath(key.toPath()) + .setPartitionSpecId(f.specId()) + .setKeyMetadata(f.keyMetadata()) + .setSplitOffsets(f.splitOffsets()) + .setColumnSizes(f.columnSizes()) + .setValueCounts(f.valueCounts()) + .setNullValueCounts(f.nullValueCounts()) + .setNanValueCounts(f.nanValueCounts()); + + // ByteBuddyUtils has trouble converting Map value type ByteBuffer Review Comment: Investigated for some time and I think I narrowed it down, but I still am not close to finding a solution. I opened a github issue for this: #32701 To move forward and get this in for the release, I'll just create a function and reference this issue -- 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]
