anoopj commented on code in PR #16958:
URL: https://github.com/apache/iceberg/pull/16958#discussion_r3501813483


##########
core/src/test/java/org/apache/iceberg/TestV4ManifestReader.java:
##########
@@ -0,0 +1,570 @@
+/*
+ * 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.apache.iceberg.types.Types.NestedField.optional;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.nio.ByteBuffer;
+import java.nio.file.Path;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import org.apache.iceberg.expressions.Expressions;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.io.FileAppender;
+import org.apache.iceberg.io.FileIO;
+import org.apache.iceberg.io.InputFile;
+import org.apache.iceberg.io.OutputFile;
+import org.apache.iceberg.metrics.DefaultMetricsContext;
+import org.apache.iceberg.metrics.ScanMetrics;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.transforms.Transforms;
+import org.apache.iceberg.types.Types;
+import org.junit.jupiter.api.TestTemplate;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.api.io.TempDir;
+
+@ExtendWith(ParameterizedTestExtension.class)
+public class TestV4ManifestReader {
+  private static final long SNAPSHOT_ID = 42L;
+  private static final int FORMAT_VERSION_V4 = 4;
+
+  private static final Schema TABLE_SCHEMA =
+      new Schema(
+          optional(1, "id", Types.IntegerType.get()), optional(2, "data", 
Types.StringType.get()));
+  private static final PartitionSpec SPEC =
+      PartitionSpec.builderFor(TABLE_SCHEMA).identity("id").build();
+  private static final Types.StructType PARTITION_TYPE = SPEC.partitionType();
+  private static final Types.StructType EMPTY_PARTITION = 
Types.StructType.of();
+  private static final PartitionData EMPTY_PARTITION_DATA = new 
PartitionData(EMPTY_PARTITION);
+  private static final Map<Integer, PartitionSpec> PARTITIONED_SPECS =
+      ImmutableMap.of(SPEC.specId(), SPEC);
+  private static final Map<Integer, PartitionSpec> UNPARTITIONED_SPECS =
+      ImmutableMap.of(PartitionSpec.unpartitioned().specId(), 
PartitionSpec.unpartitioned());
+
+  private static final List<Types.NestedField> SCHEMA_FIELDS =
+      TrackedFile.schemaWithContentStats(Types.StructType.of(), 
Types.StructType.of()).fields();
+
+  @Parameter private FileFormat format;
+
+  @Parameters(name = "format = {0}")
+  protected static List<FileFormat> parameters() {
+    return Arrays.asList(FileFormat.AVRO, FileFormat.PARQUET);
+  }
+
+  @TempDir private Path tempDir;
+
+  private final FileIO fileIO = new TestTables.LocalFileIO();
+
+  @TestTemplate
+  public void testRoundTrip() {
+    DeletionVector dv =
+        DeletionVectorStruct.builder()
+            .location("s3://bucket/dv.puffin")
+            .offset(100L)
+            .sizeInBytes(50L)
+            .cardinality(5L)
+            .build();
+
+    TrackedFile file =
+        dataFileBuilder("s3://bucket/data/file.parquet", partition(7))
+            .sortOrderId(1)
+            .deletionVector(dv)
+            .keyMetadata(ByteBuffer.wrap(new byte[] {1, 2, 3}))
+            .splitOffsets(ImmutableList.of(50L, 100L))
+            .build();
+
+    InputFile manifest = writeManifest(PARTITION_TYPE, ImmutableList.of(file));
+
+    List<TrackedFile> read = read(manifest, PARTITIONED_SPECS);
+    assertThat(read).hasSize(1);
+    TrackedFile actual = read.get(0);
+
+    assertThat(actual.contentType()).isEqualTo(file.contentType());
+    assertThat(actual.formatVersion()).isEqualTo(file.formatVersion());
+    assertThat(actual.location()).isEqualTo(file.location());
+    assertThat(actual.fileFormat()).isEqualTo(file.fileFormat());
+    assertThat(actual.recordCount()).isEqualTo(file.recordCount());
+    assertThat(actual.fileSizeInBytes()).isEqualTo(file.fileSizeInBytes());
+    assertThat(actual.specId()).isEqualTo(file.specId());
+    assertThat(actual.sortOrderId()).isEqualTo(file.sortOrderId());
+    assertThat(actual.keyMetadata()).isEqualTo(file.keyMetadata());
+    assertThat(actual.splitOffsets()).isEqualTo(file.splitOffsets());
+    assertThat(actual.partition().get(0, Integer.class))
+        .isEqualTo(file.partition().get(0, Integer.class));
+
+    assertThat(actual.tracking()).isNotNull();
+    assertThat(actual.tracking().status()).isEqualTo(file.tracking().status());
+    
assertThat(actual.tracking().snapshotId()).isEqualTo(file.tracking().snapshotId());
+
+    assertThat(actual.deletionVector()).isNotNull();
+    
assertThat(actual.deletionVector().location()).isEqualTo(file.deletionVector().location());
+    
assertThat(actual.deletionVector().offset()).isEqualTo(file.deletionVector().offset());
+    assertThat(actual.deletionVector().sizeInBytes())
+        .isEqualTo(file.deletionVector().sizeInBytes());
+    assertThat(actual.deletionVector().cardinality())
+        .isEqualTo(file.deletionVector().cardinality());
+  }
+
+  @TestTemplate
+  public void testEqualityDeleteRoundTrip() {
+    TrackedFile delete =
+        TrackedFileBuilder.equalityDelete(SNAPSHOT_ID)
+            .formatVersion(FORMAT_VERSION_V4)
+            .location("s3://bucket/eq-delete.parquet")
+            .fileFormat(FileFormat.PARQUET)
+            .recordCount(10L)
+            .fileSizeInBytes(128L)
+            .partition(EMPTY_PARTITION_DATA)
+            .specId(0)
+            .equalityIds(ImmutableList.of(1, 2))
+            .build();
+
+    InputFile manifest = writeManifest(EMPTY_PARTITION, 
ImmutableList.of(delete));
+
+    TrackedFile actual = read(manifest, UNPARTITIONED_SPECS).get(0);
+    assertThat(actual.contentType()).isEqualTo(FileContent.EQUALITY_DELETES);
+    assertThat(actual.equalityIds()).containsExactly(1, 2);
+  }
+
+  @TestTemplate
+  public void testLiveFilesExcludesDeletedAndReplaced() {
+    List<TrackedFile> files =
+        ImmutableList.of(
+            fileWithStatus(EntryStatus.ADDED, "s3://bucket/added.parquet"),
+            fileWithStatus(EntryStatus.EXISTING, 
"s3://bucket/existing.parquet"),
+            fileWithStatus(EntryStatus.MODIFIED, 
"s3://bucket/modified.parquet"),
+            fileWithStatus(EntryStatus.DELETED, "s3://bucket/deleted.parquet"),
+            fileWithStatus(EntryStatus.REPLACED, 
"s3://bucket/replaced.parquet"));
+
+    InputFile manifest = writeManifest(EMPTY_PARTITION, files);
+
+    try (V4ManifestReader reader = newReader(manifest, 
UNPARTITIONED_SPECS).build()) {
+      assertThat(reader.allFiles())
+          .extracting(file -> file.tracking().status())
+          .containsExactly(
+              EntryStatus.ADDED,
+              EntryStatus.EXISTING,
+              EntryStatus.MODIFIED,
+              EntryStatus.DELETED,
+              EntryStatus.REPLACED);
+
+      assertThat(reader.liveFiles())
+          .extracting(file -> file.tracking().status())
+          .containsExactly(EntryStatus.ADDED, EntryStatus.EXISTING, 
EntryStatus.MODIFIED);
+    } catch (IOException e) {
+      throw new UncheckedIOException(e);
+    }
+  }
+
+  @TestTemplate
+  public void testManifestLocationAndPosition() {
+    List<TrackedFile> files =
+        ImmutableList.of(
+            dataFile("s3://bucket/a.parquet", EMPTY_PARTITION_DATA),
+            dataFile("s3://bucket/b.parquet", EMPTY_PARTITION_DATA),
+            dataFile("s3://bucket/c.parquet", EMPTY_PARTITION_DATA));
+
+    InputFile manifest = writeManifest(EMPTY_PARTITION, files);
+
+    List<TrackedFile> read = read(manifest, UNPARTITIONED_SPECS);
+    assertThat(read)
+        .allSatisfy(
+            file -> 
assertThat(file.tracking().manifestLocation()).isEqualTo(manifest.location()));
+    assertThat(read).extracting(file -> 
file.tracking().manifestPos()).containsExactly(0L, 1L, 2L);
+  }
+
+  @TestTemplate
+  public void testProjectionRestrictsFields() {
+    // sort_order_id is written but not projected below, so it must not be 
read back
+    TrackedFile file =
+        dataFileBuilder("s3://bucket/file.parquet", 
EMPTY_PARTITION_DATA).sortOrderId(7).build();
+
+    InputFile manifest = writeManifest(EMPTY_PARTITION, 
ImmutableList.of(file));
+
+    Schema projection = new Schema(TrackedFile.LOCATION);
+    try (V4ManifestReader reader =
+        newReader(manifest, UNPARTITIONED_SPECS).project(projection).build()) {
+      TrackedFile actual = Lists.newArrayList(reader.allFiles()).get(0);
+      assertThat(actual.location()).isEqualTo("s3://bucket/file.parquet");
+      // tracking is always projected even when the caller omits it
+      assertThat(actual.tracking()).isNotNull();
+      assertThat(actual.tracking().status()).isEqualTo(EntryStatus.ADDED);
+      assertThat(actual.tracking().manifestPos()).isEqualTo(0L);
+      // sort_order_id was written but not projected, so it should not be read
+      assertThat(actual.sortOrderId()).isNull();
+    } catch (IOException e) {
+      throw new UncheckedIOException(e);
+    }
+  }
+
+  @TestTemplate
+  public void testUnpartitioned() {
+    TrackedFile file = dataFile("s3://bucket/file.parquet", 
EMPTY_PARTITION_DATA);
+
+    InputFile manifest = writeManifest(EMPTY_PARTITION, 
ImmutableList.of(file));
+
+    TrackedFile actual = read(manifest, UNPARTITIONED_SPECS).get(0);
+    assertThat(actual.partition()).isNotNull();

Review Comment:
   Agreed on partition: once #17000 makes the partition field optional, I can 
update the test to expect null. For now, leaving the current assertions.
   
   On `spec_id`: in this test the file is written with specId(0) and 
UNPARTITIONED_SPECS includes the unpartitioned spec (id 0), so it reads back as 
0, not null. spec_id is null only for a file that has no spec at all. 



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