rdblue commented on code in PR #16100:
URL: https://github.com/apache/iceberg/pull/16100#discussion_r3244590008


##########
core/src/test/java/org/apache/iceberg/TestTrackedFileAdapters.java:
##########
@@ -0,0 +1,424 @@
+/*
+ * 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.nio.ByteBuffer;
+import java.util.List;
+import java.util.Map;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.types.Conversions;
+import org.apache.iceberg.types.Types;
+import org.junit.jupiter.api.Test;
+
+class TestTrackedFileAdapters {
+
+  private static final String MANIFEST_LOCATION = 
"s3://bucket/table/manifest.parquet";
+
+  private static final Map<Integer, PartitionSpec> UNPARTITIONED =
+      ImmutableMap.of(0, PartitionSpec.unpartitioned());
+
+  private static Map<Integer, PartitionSpec> specsById(PartitionSpec spec) {
+    return ImmutableMap.of(spec.specId(), spec);
+  }
+
+  @Test
+  void testDataFileAdapterDelegatesAllFields() {
+    TrackingStruct tracking = createTracking(3L);
+    ContentStats stats = createContentStats();
+
+    TrackedFileStruct file =
+        new TrackedFileStruct(
+            tracking,
+            FileContent.DATA,
+            "s3://bucket/data/file.parquet",
+            FileFormat.PARQUET,
+            null,
+            100L,
+            1024L);
+    file.set(6, 0);
+    file.set(8, stats);
+    file.set(9, 3);
+    file.set(12, ByteBuffer.wrap(new byte[] {1, 2, 3}));
+    file.set(13, ImmutableList.of(50L, 100L));
+
+    DataFile dataFile = TrackedFileAdapters.asDataFile(file, UNPARTITIONED);
+
+    assertThat(dataFile.pos()).isEqualTo(3L);
+    assertThat(dataFile.specId()).isEqualTo(0);
+    assertThat(dataFile.content()).isEqualTo(FileContent.DATA);
+    assertThat(dataFile.location()).isEqualTo("s3://bucket/data/file.parquet");
+    assertThat(dataFile.format()).isEqualTo(FileFormat.PARQUET);
+    assertThat(dataFile.recordCount()).isEqualTo(100L);
+    assertThat(dataFile.fileSizeInBytes()).isEqualTo(1024L);
+    assertThat(dataFile.sortOrderId()).isEqualTo(3);
+    assertThat(dataFile.dataSequenceNumber()).isEqualTo(10L);
+    assertThat(dataFile.fileSequenceNumber()).isEqualTo(11L);
+    assertThat(dataFile.firstRowId()).isEqualTo(1000L);
+    assertThat(dataFile.keyMetadata()).isEqualTo(ByteBuffer.wrap(new byte[] 
{1, 2, 3}));
+    assertThat(dataFile.splitOffsets()).containsExactly(50L, 100L);
+    assertThat(dataFile.manifestLocation()).isEqualTo(MANIFEST_LOCATION);
+    assertThat(dataFile.equalityFieldIds()).isNull();
+    assertThat(dataFile.columnSizes()).isNull();
+    assertThat(dataFile.valueCounts()).containsOnly(entry(1, 100L), entry(2, 
200L));
+    assertThat(dataFile.nullValueCounts()).containsOnly(entry(1, 5L), entry(2, 
10L));
+    assertThat(dataFile.nanValueCounts()).containsOnly(entry(2, 3L));
+    assertThat(dataFile.lowerBounds())
+        .containsEntry(1, Conversions.toByteBuffer(Types.IntegerType.get(), 1))
+        .containsEntry(2, Conversions.toByteBuffer(Types.FloatType.get(), 
1.0f));
+    assertThat(dataFile.upperBounds())
+        .containsEntry(1, Conversions.toByteBuffer(Types.IntegerType.get(), 
1000))
+        .containsEntry(2, Conversions.toByteBuffer(Types.FloatType.get(), 
100.0f));
+  }
+
+  @Test
+  void testDataFileAdapterRejectsNonData() {
+    TrackedFileStruct file =
+        new TrackedFileStruct(
+            null,
+            FileContent.EQUALITY_DELETES,
+            "s3://bucket/delete.avro",
+            FileFormat.AVRO,
+            null,
+            50L,
+            512L);
+    file.set(6, 0);
+
+    assertThatThrownBy(() -> TrackedFileAdapters.asDataFile(file, 
UNPARTITIONED))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Invalid content type for DataFile: %s", 
FileContent.EQUALITY_DELETES);
+  }
+
+  @Test
+  void testEqualityDeleteFileAdapterDelegatesAllFields() {
+    TrackingStruct tracking = createTracking(5L);
+    PartitionSpec spec = PartitionSpec.builderFor(new 
Schema()).withSpecId(1).build();
+    ContentStats stats = createContentStats();
+
+    TrackedFileStruct file =
+        new TrackedFileStruct(
+            tracking,
+            FileContent.EQUALITY_DELETES,
+            "s3://bucket/eq-delete.avro",
+            FileFormat.AVRO,
+            null,
+            50L,
+            512L);
+    file.set(6, 1);
+    file.set(8, stats);
+    file.set(9, 5);
+    file.set(12, ByteBuffer.wrap(new byte[] {4, 5}));
+    file.set(13, ImmutableList.of(200L));
+    file.set(14, ImmutableList.of(1, 2, 3));
+
+    DeleteFile deleteFile = TrackedFileAdapters.asEqualityDeleteFile(file, 
specsById(spec));
+
+    assertThat(deleteFile.pos()).isEqualTo(5L);
+    assertThat(deleteFile.specId()).isEqualTo(1);
+    assertThat(deleteFile.content()).isEqualTo(FileContent.EQUALITY_DELETES);
+    assertThat(deleteFile.location()).isEqualTo("s3://bucket/eq-delete.avro");
+    assertThat(deleteFile.format()).isEqualTo(FileFormat.AVRO);
+    assertThat(deleteFile.recordCount()).isEqualTo(50L);
+    assertThat(deleteFile.fileSizeInBytes()).isEqualTo(512L);
+    assertThat(deleteFile.sortOrderId()).isEqualTo(5);
+    assertThat(deleteFile.dataSequenceNumber()).isEqualTo(10L);
+    assertThat(deleteFile.fileSequenceNumber()).isEqualTo(11L);
+    assertThat(deleteFile.firstRowId()).isNull();
+    assertThat(deleteFile.keyMetadata()).isEqualTo(ByteBuffer.wrap(new byte[] 
{4, 5}));
+    assertThat(deleteFile.splitOffsets()).containsExactly(200L);
+    assertThat(deleteFile.manifestLocation()).isEqualTo(MANIFEST_LOCATION);
+    assertThat(deleteFile.equalityFieldIds()).containsExactly(1, 2, 3);
+    assertThat(deleteFile.columnSizes()).isNull();
+    assertThat(deleteFile.valueCounts()).containsOnly(entry(1, 100L), entry(2, 
200L));
+    assertThat(deleteFile.nullValueCounts()).containsOnly(entry(1, 5L), 
entry(2, 10L));
+    assertThat(deleteFile.nanValueCounts()).containsOnly(entry(2, 3L));
+    assertThat(deleteFile.lowerBounds())
+        .containsEntry(1, Conversions.toByteBuffer(Types.IntegerType.get(), 1))
+        .containsEntry(2, Conversions.toByteBuffer(Types.FloatType.get(), 
1.0f));
+    assertThat(deleteFile.upperBounds())
+        .containsEntry(1, Conversions.toByteBuffer(Types.IntegerType.get(), 
1000))
+        .containsEntry(2, Conversions.toByteBuffer(Types.FloatType.get(), 
100.0f));
+  }
+
+  @Test
+  void testEqualityDeleteFileAdapterRejectsNonEqualityDeletes() {
+    TrackedFileStruct file =
+        new TrackedFileStruct(
+            null,
+            FileContent.DATA,
+            "s3://bucket/data.parquet",
+            FileFormat.PARQUET,
+            null,
+            100L,
+            1024L);
+    file.set(6, 0);
+
+    assertThatThrownBy(() -> TrackedFileAdapters.asEqualityDeleteFile(file, 
UNPARTITIONED))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Invalid content type for equality delete file: %s", 
FileContent.DATA);
+  }
+
+  @Test
+  void testDVDeleteFileAdapterDelegatesAllFields() {
+    TrackingStruct tracking = createTracking(7L);
+    PartitionSpec spec = PartitionSpec.builderFor(new 
Schema()).withSpecId(2).build();
+
+    TrackedFileStruct file = createDataFileWithDV(tracking, 2);

Review Comment:
   Using a method to create the file and DV here but not in the tests above is 
strange and make the test harder to read. For this test in particular it isn't 
a great choice because I'm looking to see that `file.location()` is available 
as `dvFile.referencedDataFile()`.
   
   Test correctness depends on what was set in `createDataFileWithDV`. But 
separating that isn't a good idea because an LLM will probably see that 
method's choices as arbitrary, like how `fileSizeInBytes` is 256. As a result, 
a future LLM doesn't have to use `file.parquet` and could choose `dv-file.bin` 
instead, but if that is changed then this must be updated and it is a gamble 
what update is "correct".



-- 
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]

Reply via email to