gaborkaszab commented on code in PR #16285:
URL: https://github.com/apache/iceberg/pull/16285#discussion_r4329523686
##########
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:
These, and resolving the location of the column files, and whatever else I
haven't noticed so far :) Including tests that would be an additional ~100-150
lines of code and complexity on top of this pretty simple metadata structure
addition.
I'd still like to avoid merging the 2 PRs, and also they logically separate
easily: introducing the structs + wiring them in to the reader.
##########
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:
1. I disagree with the constructor being the right place. `TrackedFile` and
`Tracking` also have their own invariants that we don't verify in their
constructors. A future builder is the right place for verification and the
constructor should just accept whatever the builder passes.
2. Changed that we input and output `fieldIds` as Set, but internally still
keep it as `int[]`. WDYT?
##########
core/src/test/java/org/apache/iceberg/TestTrackedFileStruct.java:
##########
@@ -421,7 +438,8 @@ void
serializationRoundTrip(RoundTripSerializer<TrackedFileStruct> serializer) t
null, // DeletionVector has its own serialization tests
null, // ManifestInfo has its own serialization tests
ByteBuffer.wrap(new byte[] {1, 2, 3}),
- ImmutableList.of(50L));
+ ImmutableList.of(50L),
+ null); // ColumnFile has its own serialization tests
Review Comment:
I checked those 2 comments, both came from @rdblue on the initial PR for
`TrackedFileStruct`. Then it was himself who [introduced mocks for the
tests](https://github.com/apache/iceberg/pull/17133/) of that class and in this
particular test used null for `DeletionVector` and `ManifestInfo` above. I
followed the same pattern.
We can revisit this, but let's make it in a follow-up covering all the
nested structs in case we still want that coverage to avoid the test being
asymmetrical.
##########
core/src/test/java/org/apache/iceberg/TestColumnFileStruct.java:
##########
@@ -0,0 +1,133 @@
+/*
+ * 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 java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Comparator;
+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});
+ private static final Comparator<StructLike> COLUMN_FILE_COMPARATOR =
+ Comparators.forType(ColumnFile.schema());
+
+ @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((StructLike)
copy).usingComparator(COLUMN_FILE_COMPARATOR).isEqualTo(columnFile);
Review Comment:
Thanks for the pointer! Done
##########
core/src/test/java/org/apache/iceberg/TestTrackingBuilder.java:
##########
@@ -280,4 +280,31 @@ void manifestDVPositionsProduceModified() {
assertThat(modified.modifiedSnapshotId()).isEqualTo(999L);
assertThat(modified.deletedPositions()).isEqualTo(deletedBytes);
}
+
+ @Test
+ void manifestPositionsWithColumnFilesUpdated() {
Review Comment:
Aaah, you're right. I haven't adjusted the test after we merged the 2
dedicated snapshot IDs. Added a dedicated test.
Maybe this test still makes sense to simulate a manifest entry pointing to a
leaf manifest having both column files and deleted/replaced positions.
--
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]