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


##########
core/src/test/java/org/apache/iceberg/TestFilePlanner.java:
##########
@@ -0,0 +1,758 @@
+/*
+ * 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.V4TestHelpers.ADDED_TRACKING;
+import static org.apache.iceberg.V4TestHelpers.FILE_SIZE_IN_BYTES;
+import static org.apache.iceberg.V4TestHelpers.RECORD_COUNT;
+import static org.apache.iceberg.V4TestHelpers.SNAPSHOT_ID;
+import static org.apache.iceberg.V4TestHelpers.dataFile;
+import static org.apache.iceberg.V4TestHelpers.deletionVector;
+import static org.apache.iceberg.V4TestHelpers.manifestRef;
+import static org.apache.iceberg.V4TestHelpers.manifestRefWithStats;
+import static org.apache.iceberg.V4TestHelpers.writeTrackedFiles;
+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 static org.assertj.core.api.Assertions.tuple;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.UnaryOperator;
+import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.expressions.Expression;
+import org.apache.iceberg.expressions.Expressions;
+import org.apache.iceberg.inmemory.InMemoryFileIO;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.io.CloseableIterator;
+import org.apache.iceberg.io.FileIO;
+import org.apache.iceberg.io.InputFile;
+import org.apache.iceberg.io.OutputFile;
+import org.apache.iceberg.io.SeekableInputStream;
+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.Iterables;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.relocated.com.google.common.collect.Sets;
+import org.apache.iceberg.types.Types;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.FieldSource;
+
+class TestFilePlanner {
+  private static final long SEQUENCE_NUMBER = 7L;
+  private static final long FIRST_ROW_ID = 1000L;
+  private static final String TABLE_LOCATION = "s3://bucket/db/table";
+  private static final DeletionVector DV =
+      deletionVector("s3://bucket/db/table/dv.puffin", 100L, 50L, 5L);
+
+  private static final String FILE_A = "s3://bucket/db/table/file-a.parquet";
+  private static final String FILE_B = "s3://bucket/db/table/file-b.parquet";
+  private static final String FILE_C = "s3://bucket/db/table/file-c.parquet";
+  private static final String FILE_D = "s3://bucket/db/table/file-d.parquet";
+  private static final String PARTITIONED_FILE = 
"s3://bucket/db/table/partitioned.parquet";
+  private static final String UNPARTITIONED_FILE = 
"s3://bucket/db/table/unpartitioned.parquet";
+
+  private static final Schema TABLE_SCHEMA =
+      new Schema(
+          optional(1, "id", Types.IntegerType.get()), optional(2, "data", 
Types.StringType.get()));
+  private static final int UNPARTITIONED_SPEC_ID = 
PartitionSpec.unpartitioned().specId();
+  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> UNPARTITIONED_SPECS =
+      ImmutableMap.of(UNPARTITIONED_SPEC_ID, PartitionSpec.unpartitioned());
+
+  private static final PartitionSpec UNPARTITIONED_WITH_SCHEMA =
+      PartitionSpec.builderFor(TABLE_SCHEMA).withSpecId(0).build();
+  private static final PartitionSpec BY_ID_SPEC =
+      
PartitionSpec.builderFor(TABLE_SCHEMA).withSpecId(1).identity("id").build();
+  private static final Map<Integer, PartitionSpec> MIXED_SPECS =
+      ImmutableMap.of(
+          UNPARTITIONED_WITH_SCHEMA.specId(), UNPARTITIONED_WITH_SCHEMA,
+          BY_ID_SPEC.specId(), BY_ID_SPEC);
+
+  private static final List<FileFormat> MANIFEST_FORMATS =
+      ImmutableList.of(FileFormat.AVRO, FileFormat.PARQUET);
+
+  private static final MetricsConfig METRICS_CONFIG =
+      MetricsConfig.from(ImmutableMap.of(), TABLE_SCHEMA, null);
+  private static final Types.StructType STATS_TYPE =
+      StatsUtil.statsWriteSchema(TABLE_SCHEMA, METRICS_CONFIG);
+
+  private final InMemoryFileIO fileIO = new InMemoryFileIO();
+
+  @ParameterizedTest
+  @FieldSource("MANIFEST_FORMATS")
+  void rootWithOnlyDataFiles(FileFormat format) throws IOException {
+    ManifestFile root =
+        writeManifest(
+            format,
+            EMPTY_PARTITION,
+            ImmutableList.of(unpartitionedDataFile(FILE_A), 
unpartitionedDataFile(FILE_B)));
+
+    List<FileScanTask> tasks = plan(root, UNPARTITIONED_SPECS);
+
+    assertThat(tasks)
+        .hasSize(2)
+        .extracting(task -> task.file().location())
+        .containsExactlyInAnyOrder(FILE_A, FILE_B);
+  }
+
+  @ParameterizedTest
+  @FieldSource("MANIFEST_FORMATS")
+  void rootWithOnlyLeafManifests(FileFormat format) throws IOException {
+    ManifestFile leafA =
+        writeManifest(format, EMPTY_PARTITION, 
ImmutableList.of(unpartitionedDataFile(FILE_A)));
+    ManifestFile leafB =
+        writeManifest(format, EMPTY_PARTITION, 
ImmutableList.of(unpartitionedDataFile(FILE_B)));
+    ManifestFile root =
+        writeManifest(
+            format,
+            EMPTY_PARTITION,
+            ImmutableList.of(dataManifest(leafA.path()), 
dataManifest(leafB.path())));
+
+    List<FileScanTask> tasks = plan(root, UNPARTITIONED_SPECS);
+
+    assertThat(tasks)
+        .hasSize(2)
+        .extracting(task -> task.file().location())
+        .containsExactlyInAnyOrder(FILE_A, FILE_B);
+  }
+
+  @ParameterizedTest
+  @FieldSource("MANIFEST_FORMATS")
+  void rootWithMixedDataAndLeafManifests(FileFormat format) throws IOException 
{
+    ManifestFile leaf1 =
+        writeManifest(format, EMPTY_PARTITION, 
ImmutableList.of(unpartitionedDataFile(FILE_C)));
+    ManifestFile leaf2 =
+        writeManifest(format, EMPTY_PARTITION, 
ImmutableList.of(unpartitionedDataFile(FILE_D)));
+    ManifestFile root =
+        writeManifest(
+            format,
+            EMPTY_PARTITION,
+            ImmutableList.of(
+                unpartitionedDataFile(FILE_A),
+                dataManifest(leaf1.path()),
+                dataManifest(leaf2.path())));
+
+    List<FileScanTask> tasks = plan(root, UNPARTITIONED_SPECS);
+
+    assertThat(tasks)
+        .hasSize(3)
+        .extracting(task -> task.file().location())
+        .containsExactlyInAnyOrder(FILE_A, FILE_C, FILE_D);
+  }
+
+  @ParameterizedTest
+  @FieldSource("MANIFEST_FORMATS")
+  void filterSkipsAndMatchesRootDataFiles(FileFormat format) throws 
IOException {
+    ManifestFile root =
+        writeManifest(
+            format,
+            EMPTY_PARTITION,
+            ImmutableList.of(
+                dataFileWithStats(FILE_A, idStats(0, 99)),
+                dataFileWithStats(FILE_B, idStats(100, 199))));
+
+    List<FileScanTask> tasks =
+        plan(root, UNPARTITIONED_SPECS, planner -> 
planner.filterData(Expressions.equal("id", 50)));
+
+    assertThat(tasks)
+        .extracting(task -> task.file().location())
+        .as("only the root data file whose stats match the filter survives")
+        .containsExactlyInAnyOrder(FILE_A);
+  }
+
+  @ParameterizedTest
+  @FieldSource("MANIFEST_FORMATS")
+  void filterSkipsAndMatchesRootDataFilesAndManifests(FileFormat format) 
throws IOException {
+    ManifestFile matchedLeaf =
+        writeManifest(
+            format,
+            EMPTY_PARTITION,
+            ImmutableList.of(
+                dataFileWithStats(FILE_C, idStats(0, 99)),
+                dataFileWithStats(FILE_D, idStats(100, 199))));
+    ManifestFile root =
+        writeManifest(
+            format,
+            EMPTY_PARTITION,
+            ImmutableList.of(
+                dataFileWithStats(FILE_A, idStats(0, 99)),
+                dataFileWithStats(FILE_B, idStats(100, 199)),
+                dataManifestWithStats(matchedLeaf.path(), idStats(0, 199)),
+                dataManifestWithStats(
+                    "s3://bucket/db/table/pruned-leaf.parquet", idStats(100, 
199))));
+
+    List<FileScanTask> tasks =
+        plan(root, UNPARTITIONED_SPECS, planner -> 
planner.filterData(Expressions.equal("id", 50)));
+
+    assertThat(tasks)
+        .extracting(task -> task.file().location())
+        .as(
+            "the matched root data file and the file in the matched leaf 
survive; the pruned data file and leaf do not")
+        .containsExactlyInAnyOrder(FILE_A, FILE_C);
+  }
+
+  @ParameterizedTest
+  @FieldSource("MANIFEST_FORMATS")
+  void filterSkipsAndMatchesLeafDataFiles(FileFormat format) throws 
IOException {
+    ManifestFile leaf =
+        writeManifest(
+            format,
+            EMPTY_PARTITION,
+            ImmutableList.of(
+                dataFileWithStats(FILE_C, idStats(0, 99)),
+                dataFileWithStats(FILE_D, idStats(100, 199))));
+    ManifestFile root =
+        writeManifest(format, EMPTY_PARTITION, 
ImmutableList.of(dataManifest(leaf.path(), 2)));
+
+    List<FileScanTask> tasks =
+        plan(root, UNPARTITIONED_SPECS, planner -> 
planner.filterData(Expressions.equal("id", 50)));
+
+    assertThat(Iterables.getOnlyElement(tasks).file().location())
+        .as("only the leaf file whose stats match the filter survives")
+        .isEqualTo(FILE_C);
+  }
+
+  @ParameterizedTest
+  @FieldSource("MANIFEST_FORMATS")
+  void filterHonorsCaseSensitivity(FileFormat format) throws IOException {
+    ManifestFile leaf =
+        writeManifest(
+            format,
+            EMPTY_PARTITION,
+            ImmutableList.of(
+                dataFileWithStats(FILE_C, idStats(0, 99)),
+                dataFileWithStats(FILE_D, idStats(100, 199))));
+    ManifestFile root =
+        writeManifest(
+            format,
+            EMPTY_PARTITION,
+            ImmutableList.of(
+                dataFileWithStats(FILE_A, idStats(0, 99)),
+                dataFileWithStats(FILE_B, idStats(100, 199)),
+                dataManifestWithStats(leaf.path(), idStats(0, 199))));
+
+    List<FileScanTask> tasks =
+        plan(
+            root,
+            UNPARTITIONED_SPECS,
+            planner -> planner.filterData(Expressions.equal("ID", 
50)).caseSensitive(false));
+    assertThat(tasks)
+        .extracting(task -> task.file().location())
+        .as("a case-insensitive filter resolves \"ID\" to \"id\" in both the 
root and leaf readers")
+        .containsExactlyInAnyOrder(FILE_A, FILE_C);
+
+    assertThatThrownBy(
+            () ->
+                plan(
+                    root,
+                    UNPARTITIONED_SPECS,
+                    planner -> planner.filterData(Expressions.equal("ID", 
50))))
+        .as("the default case-sensitive filter cannot resolve \"ID\"")
+        .isInstanceOf(ValidationException.class)
+        .hasMessageContaining("Cannot find field 'ID'");
+  }
+
+  @ParameterizedTest
+  @FieldSource("MANIFEST_FORMATS")
+  void taskAttachesColocatedDV(FileFormat format) throws IOException {
+    TrackedFile fileWithDV =
+        unpartitionedDataFileWithDV("s3://bucket/db/table/with-dv.parquet", 
DV);
+    TrackedFile fileWithoutDV = unpartitionedDataFile(FILE_A);
+    ManifestFile root =
+        writeManifest(format, EMPTY_PARTITION, ImmutableList.of(fileWithDV, 
fileWithoutDV));
+
+    List<FileScanTask> tasks = plan(root, UNPARTITIONED_SPECS);
+
+    assertThat(tasks)
+        .hasSize(2)
+        .satisfiesExactlyInAnyOrder(
+            task -> {
+              
assertThat(task.file().location()).isEqualTo("s3://bucket/db/table/with-dv.parquet");
+              assertThat(task.deletes())
+                  .as("the colocated DV is attached as the file's only delete")
+                  .hasSize(1)
+                  .allSatisfy(delete -> 
assertThat(delete.location()).isEqualTo(DV.location()));
+            },
+            task -> {
+              assertThat(task.file().location()).isEqualTo(FILE_A);
+              assertThat(task.deletes())
+                  .as("a file without a colocated DV has no deletes")
+                  .isEmpty();
+            });
+  }
+
+  @ParameterizedTest
+  @FieldSource("MANIFEST_FORMATS")
+  void taskExposesFileSchemaAndSpec(FileFormat format) throws IOException {
+    Types.StructType unionType = 
Partitioning.unionPartitionTypes(MIXED_SPECS.values());
+    ManifestFile root =
+        writeManifest(
+            format,
+            unionType,
+            ImmutableList.of(
+                dataFile(
+                    PARTITIONED_FILE, BY_ID_SPEC.specId(), 
idPartition(unionType, 1), null, null),

Review Comment:
   Will annotate them. 



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