stevenzwu commented on code in PR #16285: URL: https://github.com/apache/iceberg/pull/16285#discussion_r4255085651
########## core/src/test/java/org/apache/iceberg/TestColumnFileStruct.java: ########## @@ -0,0 +1,210 @@ +/* + * 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 static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.List; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; +import org.apache.iceberg.types.Comparators; +import org.apache.iceberg.types.Types; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +class TestColumnFileStruct { + + private static final String LOCATION = "s3://bucket/data/column.parquet"; + private static final List<Integer> FIELD_IDS = Lists.newArrayList(1, 2, 3); + private static final FileFormat FILE_FORMAT = FileFormat.PARQUET; + private static final long FILE_SIZE_IN_BYTES = 1024L; + private static final ByteBuffer KEY_METADATA = ByteBuffer.wrap(new byte[] {1, 2, 3}); + + @Test + void fieldAccess() { + ColumnFile columnFile = + new ColumnFileStruct(LOCATION, FIELD_IDS, FILE_FORMAT, FILE_SIZE_IN_BYTES, KEY_METADATA); + + assertThat(columnFile.location()).isEqualTo(LOCATION); + assertThat(columnFile.fieldIds()).containsExactlyElementsOf(FIELD_IDS); + assertThat(columnFile.fileFormat()).isEqualTo(FILE_FORMAT); + assertThat(columnFile.fileSizeInBytes()).isEqualTo(FILE_SIZE_IN_BYTES); + assertThat(columnFile.keyMetadata()).isEqualTo(KEY_METADATA); + } + + @Test + void copy() { + ColumnFile columnFile = + new ColumnFileStruct(LOCATION, FIELD_IDS, FILE_FORMAT, FILE_SIZE_IN_BYTES, KEY_METADATA); + + ColumnFile copy = columnFile.copy(); + + assertThat(copy.location()).isEqualTo(LOCATION); + assertThat(copy.fieldIds()).containsExactlyElementsOf(FIELD_IDS); + assertThat(copy.fileFormat()).isEqualTo(FILE_FORMAT); + assertThat(copy.fileSizeInBytes()).isEqualTo(FILE_SIZE_IN_BYTES); + assertThat(copy.keyMetadata()).isEqualTo(KEY_METADATA); + } + + @Test + void structLikeSize() { + ColumnFileStruct columnFile = new ColumnFileStruct(); + assertThat(columnFile.size()).isEqualTo(5); + } + + @Test + void setFieldsByOrdinals() { + ColumnFileStruct columnFile = new ColumnFileStruct(); + + columnFile.set(0, LOCATION); + columnFile.set(1, FIELD_IDS); + columnFile.set(2, FILE_FORMAT.toString()); + columnFile.set(3, FILE_SIZE_IN_BYTES); + columnFile.set(4, KEY_METADATA); + + assertThat(columnFile.location()).isEqualTo(LOCATION); + assertThat(columnFile.fieldIds()).containsExactlyElementsOf(FIELD_IDS); + assertThat(columnFile.fileFormat()).isEqualTo(FILE_FORMAT); + assertThat(columnFile.fileSizeInBytes()).isEqualTo(FILE_SIZE_IN_BYTES); + assertThat(columnFile.keyMetadata()).isEqualTo(KEY_METADATA); + } + + @Test + void getFieldsByOrdinals() { + ColumnFileStruct columnFile = + new ColumnFileStruct(LOCATION, FIELD_IDS, FILE_FORMAT, FILE_SIZE_IN_BYTES, KEY_METADATA); + + assertThat(columnFile.get(0, String.class)).isEqualTo(LOCATION); + assertThat(columnFile.get(1, List.class)).containsExactlyElementsOf(FIELD_IDS); + assertThat(columnFile.get(2, String.class)).isEqualTo(FILE_FORMAT.toString()); + assertThat(columnFile.get(3, Long.class)).isEqualTo(FILE_SIZE_IN_BYTES); + assertThat(columnFile.get(4, ByteBuffer.class)).isEqualTo(KEY_METADATA); + } + + @Test + void projectedStructLike() { + Types.StructType projection = + Types.StructType.of(ColumnFile.LOCATION, ColumnFile.FILE_SIZE_IN_BYTES); + + ColumnFileStruct columnFile = new ColumnFileStruct(projection); + assertThat(columnFile.size()).isEqualTo(2); + + // projected position 0 maps to internal position of location + // projected position 1 maps to internal position of file_size_in_bytes + columnFile.set(0, LOCATION); + columnFile.set(1, FILE_SIZE_IN_BYTES); + + assertThat(columnFile.location()).isEqualTo(LOCATION); + assertThat(columnFile.fileSizeInBytes()).isEqualTo(FILE_SIZE_IN_BYTES); + assertThat(columnFile.get(0, String.class)).isEqualTo(LOCATION); + assertThat(columnFile.get(1, Long.class)).isEqualTo(FILE_SIZE_IN_BYTES); + } + + @ParameterizedTest + @MethodSource("org.apache.iceberg.TestHelpers#serializers") + void serializationRoundTrip(TestHelpers.RoundTripSerializer<ColumnFile> roundTripSerializer) + throws IOException, ClassNotFoundException { + ColumnFile columnFile = Review Comment: this column file object can also be a private static constant. the `Comparators.forType(ColumnFile.schema())` can also be a static constant. ########## core/src/main/java/org/apache/iceberg/ColumnFileStruct.java: ########## @@ -0,0 +1,218 @@ +/* + * 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.io.Serializable; +import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.List; +import org.apache.iceberg.avro.SupportsIndexProjection; +import org.apache.iceberg.relocated.com.google.common.base.MoreObjects; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.relocated.com.google.common.collect.Sets; +import org.apache.iceberg.types.Types; +import org.apache.iceberg.util.ArrayUtil; +import org.apache.iceberg.util.ByteBuffers; + +/** Mutable {@link StructLike} implementation of {@link ColumnFile}. */ +class ColumnFileStruct extends SupportsIndexProjection implements ColumnFile, Serializable { + private static final Types.StructType BASE_TYPE = + Types.StructType.of( + ColumnFile.LOCATION, + ColumnFile.FIELD_IDS, + ColumnFile.FILE_FORMAT, + ColumnFile.FILE_SIZE_IN_BYTES, + ColumnFile.KEY_METADATA); + + private String location = null; + private int[] fieldIds = null; + private FileFormat fileFormat = null; + private long fileSizeInBytes = -1L; + private byte[] keyMetadata = null; + + /** Used by internal readers to instantiate this class with a projection schema. */ + ColumnFileStruct(Types.StructType projection) { + super(BASE_TYPE, projection); + } + + ColumnFileStruct( + String location, + List<Integer> fieldIds, + FileFormat fileFormat, + long fileSizeInBytes, + ByteBuffer keyMetadata) { + super(BASE_TYPE.fields().size()); + this.location = location; + this.fieldIds = ArrayUtil.toIntArray(fieldIds); + this.fileFormat = fileFormat; + this.fileSizeInBytes = fileSizeInBytes; + this.keyMetadata = ByteBuffers.toByteArray(keyMetadata); + } + + /** Copy constructor. */ + private ColumnFileStruct(ColumnFileStruct toCopy) { + super(toCopy); + this.location = toCopy.location; + this.fieldIds = + toCopy.fieldIds != null ? Arrays.copyOf(toCopy.fieldIds, toCopy.fieldIds.length) : null; + this.fileFormat = toCopy.fileFormat; + this.fileSizeInBytes = toCopy.fileSizeInBytes; + this.keyMetadata = + toCopy.keyMetadata != null + ? Arrays.copyOf(toCopy.keyMetadata, toCopy.keyMetadata.length) + : null; + } + + /** Constructor for Java serialization. */ + ColumnFileStruct() { + super(BASE_TYPE.fields().size()); + } + + @Override + public String location() { + return location; + } + + @Override + public List<Integer> fieldIds() { + return fieldIds != null ? ArrayUtil.toUnmodifiableIntList(fieldIds) : null; + } + + @Override + public FileFormat fileFormat() { + return fileFormat; + } + + @Override + public long fileSizeInBytes() { + return fileSizeInBytes; + } + + @Override + public ByteBuffer keyMetadata() { + return keyMetadata != null ? ByteBuffer.wrap(keyMetadata) : null; + } + + @Override + public ColumnFile copy() { + return new ColumnFileStruct(this); + } + + @Override + protected <T> T internalGet(int pos, Class<T> javaClass) { + return javaClass.cast(getByPos(pos)); + } + + private Object getByPos(int pos) { + return switch (pos) { + case 0 -> location; + case 1 -> fieldIds(); + case 2 -> fileFormat != null ? fileFormat.toString() : null; + case 3 -> fileSizeInBytes; + case 4 -> keyMetadata(); + default -> throw new UnsupportedOperationException("Unknown field ordinal: " + pos); + }; + } + + @Override + @SuppressWarnings("unchecked") + protected <T> void internalSet(int pos, T value) { + switch (pos) { + // always coerce to String for Serializable + case 0 -> this.location = value.toString(); + case 1 -> this.fieldIds = ArrayUtil.toIntArray((List<Integer>) value); + case 2 -> this.fileFormat = FileFormat.fromString(value.toString()); + case 3 -> this.fileSizeInBytes = (long) value; + case 4 -> this.keyMetadata = ByteBuffers.toByteArray((ByteBuffer) value); + default -> { + // ignore the object, it must be from a newer version of the format + } + } + } + + static Builder builder() { + return new Builder(); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("location", location) + .add("field_ids", fieldIds) + .add("file_format", fileFormat) + .add("file_size_in_bytes", fileSizeInBytes) + .add("key_metadata", keyMetadata == null ? "null" : "(redacted)") + .toString(); + } + + static class Builder { + private String location = null; + private List<Integer> fieldIds = null; + private FileFormat fileFormat = null; + private Long fileSizeInBytes = null; + private ByteBuffer keyMetadata = null; + + Builder location(String newLocation) { + Preconditions.checkArgument(newLocation != null, "Invalid location: null"); + Preconditions.checkArgument(!newLocation.isEmpty(), "Invalid location: empty"); + this.location = newLocation; + return this; + } + + Builder fieldIds(List<Integer> newFieldIds) { + Preconditions.checkArgument(newFieldIds != null, "Invalid field IDs: null"); + Preconditions.checkArgument(!newFieldIds.isEmpty(), "Invalid field IDs: empty"); + Preconditions.checkArgument( + Sets.newHashSet(newFieldIds).size() == newFieldIds.size(), Review Comment: why not just make the arg a `Set`? ########## core/src/test/java/org/apache/iceberg/TestV4ManifestReader.java: ########## @@ -172,7 +172,8 @@ public void readDataFile(FileFormat format) throws IOException { DV, null, // manifest info ByteBuffer.wrap(new byte[] {1, 2, 3}), // key metadata - ImmutableList.of(50L, 100L)); + ImmutableList.of(50L, 100L), + null); // column files Review Comment: I know there is a resolved comment on this topic already. but I want to ask it again. if the V4ManifestReader follow-up is not big, might be better to include it in this 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: [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]
