nastra commented on code in PR #17433:
URL: https://github.com/apache/iceberg/pull/17433#discussion_r3735548148


##########
core/src/test/java/org/apache/iceberg/TestV4ManifestReaderStats.java:
##########
@@ -0,0 +1,798 @@
+/*
+ * 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.apache.iceberg.types.Types.NestedField.required;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import org.apache.iceberg.expressions.Expressions;
+import org.apache.iceberg.inmemory.InMemoryInputFile;
+import org.apache.iceberg.inmemory.InMemoryOutputFile;
+import org.apache.iceberg.io.FileAppender;
+import org.apache.iceberg.io.InputFile;
+import org.apache.iceberg.io.OutputFile;
+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.types.Types;
+import org.apache.iceberg.variants.ShreddedObject;
+import org.apache.iceberg.variants.Variant;
+import org.apache.iceberg.variants.VariantMetadata;
+import org.apache.iceberg.variants.VariantTestUtil;
+import org.apache.iceberg.variants.Variants;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.FieldSource;
+
+class TestV4ManifestReaderStats {
+  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(PartitionSpec.unpartitioned().specId(), 
PartitionSpec.unpartitioned());
+  private static final List<FileFormat> MANIFEST_FORMATS =
+      List.of(FileFormat.AVRO, FileFormat.PARQUET);
+  private static final String TABLE_LOCATION = "s3://bucket/db/table";
+
+  private static final int ID_FIELD_ID = 1;
+  private static final int DATA_FIELD_ID = 2;
+  private static final int MEASURE_FIELD_ID = 3;
+
+  private static final Schema TABLE_SCHEMA =
+      new Schema(
+          optional(ID_FIELD_ID, "id", Types.IntegerType.get()),
+          optional(DATA_FIELD_ID, "data", Types.StringType.get()),
+          optional(MEASURE_FIELD_ID, "measure", Types.DoubleType.get()));
+  private static final Types.StructType CONTENT_STATS_TYPE =
+      StatsUtil.statsReadSchema(
+          TABLE_SCHEMA, List.of(ID_FIELD_ID, DATA_FIELD_ID, MEASURE_FIELD_ID));
+  private static final FieldStats<Integer> ID_STATS =
+      new FieldStatsStruct<>(
+          CONTENT_STATS_TYPE.fieldType("id").asStructType(), 1, 100, true, 
26L, 2L, 0L, null);
+  private static final FieldStats<String> DATA_STATS =
+      new FieldStatsStruct<>(
+          CONTENT_STATS_TYPE.fieldType("data").asStructType(), "a", "z", true, 
26L, 0L, 0L, 4);
+  private static final FieldStats<Double> MEASURE_STATS =
+      new FieldStatsStruct<>(
+          CONTENT_STATS_TYPE.fieldType("measure").asStructType(),
+          1.5,
+          9.5,
+          false,
+          26L,
+          1L,
+          3L,
+          null);
+
+  @Test
+  void invalidProjectStatsArguments() {
+    InputFile manifest = new InMemoryInputFile(new byte[0]);
+
+    assertThatThrownBy(
+            () ->
+                V4ManifestReader.builder(
+                        manifest, TABLE_SCHEMA, UNPARTITIONED_SPECS, 
TABLE_LOCATION)
+                    .projectStats((Iterable<Integer>) null))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Invalid stats projection for field IDs: null");
+  }
+
+  @ParameterizedTest
+  @FieldSource("MANIFEST_FORMATS")
+  void readContentStatsForAllFieldIds(FileFormat format) throws IOException {
+    TrackedFile file = fileWithStats("s3://bucket/file.parquet", 
contentStats());
+    InputFile manifest = writeManifest(format, CONTENT_STATS_TYPE, 
List.of(file));
+
+    try (V4ManifestReader reader =
+        V4ManifestReader.builder(manifest, TABLE_SCHEMA, UNPARTITIONED_SPECS, 
TABLE_LOCATION)
+            .build()) {
+      ContentStats stats = Iterables.getOnlyElement(reader).contentStats();
+      assertThat(stats).isNotNull();
+      assertFieldStats(stats.statsFor(ID_FIELD_ID), ID_STATS);
+      assertFieldStats(stats.statsFor(DATA_FIELD_ID), DATA_STATS);
+      assertFieldStats(stats.statsFor(MEASURE_FIELD_ID), MEASURE_STATS);
+    }
+  }
+
+  @ParameterizedTest
+  @FieldSource("MANIFEST_FORMATS")
+  void rowFilterKeepsStatsForAllFieldIds(FileFormat format) throws IOException 
{
+    TrackedFile file = fileWithStats("s3://bucket/file.parquet", 
contentStats());
+    InputFile manifest = writeManifest(format, CONTENT_STATS_TYPE, 
List.of(file));
+
+    // a filter narrows the stats only for scan planning, so a default read 
still carries the
+    // stats of every field even though the filter needs one of them
+    try (V4ManifestReader reader =
+        V4ManifestReader.builder(manifest, TABLE_SCHEMA, UNPARTITIONED_SPECS, 
TABLE_LOCATION)
+            .filter(Expressions.equal("id", 1))
+            .build()) {
+      ContentStats stats = Iterables.getOnlyElement(reader).contentStats();
+      assertFieldStats(stats.statsFor(ID_FIELD_ID), ID_STATS);
+      assertFieldStats(stats.statsFor(DATA_FIELD_ID), DATA_STATS);
+      assertFieldStats(stats.statsFor(MEASURE_FIELD_ID), MEASURE_STATS);
+    }
+  }
+
+  @ParameterizedTest
+  @FieldSource("MANIFEST_FORMATS")
+  void selectStatsByName(FileFormat format) throws IOException {
+    TrackedFile file = fileWithStats("s3://bucket/file.parquet", 
contentStats());
+    InputFile manifest = writeManifest(format, CONTENT_STATS_TYPE, 
List.of(file));
+
+    // stats are named after the column they describe, so a caller can select 
one column's stats
+    try (V4ManifestReader reader =
+        V4ManifestReader.builder(manifest, TABLE_SCHEMA, UNPARTITIONED_SPECS, 
TABLE_LOCATION)
+            .select("location", "content_stats.data")
+            .build()) {
+      TrackedFile actual = Iterables.getOnlyElement(reader);
+      assertThat(actual.location()).isEqualTo("s3://bucket/file.parquet");
+
+      ContentStats stats = actual.contentStats();
+      assertFieldStats(stats.statsFor(DATA_FIELD_ID), DATA_STATS);
+      assertThat(stats.statsFor(ID_FIELD_ID)).isNull();
+      assertThat(stats.statsFor(MEASURE_FIELD_ID)).isNull();
+    }
+  }
+
+  @ParameterizedTest
+  @FieldSource("MANIFEST_FORMATS")
+  void projectStatsReadsOnlyRequestedColumns(FileFormat format) throws 
IOException {
+    TrackedFile file = fileWithStats("s3://bucket/file.parquet", 
contentStats());
+    InputFile manifest = writeManifest(format, CONTENT_STATS_TYPE, 
List.of(file));
+
+    try (V4ManifestReader reader =
+        V4ManifestReader.builder(manifest, TABLE_SCHEMA, UNPARTITIONED_SPECS, 
TABLE_LOCATION)
+            .projectStats(ID_FIELD_ID)
+            .build()) {
+      ContentStats stats = Iterables.getOnlyElement(reader).contentStats();
+      assertFieldStats(stats.statsFor(ID_FIELD_ID), ID_STATS);
+      assertThat(stats.statsFor(DATA_FIELD_ID)).isNull();
+      assertThat(stats.statsFor(MEASURE_FIELD_ID)).isNull();
+    }
+  }
+
+  @ParameterizedTest
+  @FieldSource("MANIFEST_FORMATS")
+  void projectStatsWithoutFieldIdsOmitsStats(FileFormat format) throws 
IOException {
+    TrackedFile file = fileWithStats("s3://bucket/file.parquet", 
contentStats());
+    InputFile manifest = writeManifest(format, CONTENT_STATS_TYPE, 
List.of(file));
+
+    // requesting no field IDs opts out of the default projection of every 
field
+    try (V4ManifestReader reader =
+        V4ManifestReader.builder(manifest, TABLE_SCHEMA, UNPARTITIONED_SPECS, 
TABLE_LOCATION)
+            .projectStats(List.of())
+            .build()) {
+      assertThat(Iterables.getOnlyElement(reader).contentStats()).isNull();
+    }
+  }
+
+  @ParameterizedTest
+  @FieldSource("MANIFEST_FORMATS")
+  void projectStatsCopiesFieldIds(FileFormat format) throws IOException {
+    TrackedFile file = fileWithStats("s3://bucket/file.parquet", 
contentStats());
+    InputFile manifest = writeManifest(format, CONTENT_STATS_TYPE, 
List.of(file));
+
+    List<Integer> fieldIds = Lists.newArrayList(ID_FIELD_ID);
+    V4ManifestReader.Builder builder =
+        V4ManifestReader.builder(manifest, TABLE_SCHEMA, UNPARTITIONED_SPECS, 
TABLE_LOCATION)
+            .projectStats(fieldIds);
+
+    // the builder copies the field IDs, so a later change to fieldIds does 
not widen the projection
+    fieldIds.add(DATA_FIELD_ID);
+
+    try (V4ManifestReader reader = builder.build()) {
+      ContentStats stats = Iterables.getOnlyElement(reader).contentStats();
+      assertFieldStats(stats.statsFor(ID_FIELD_ID), ID_STATS);
+      assertThat(stats.statsFor(DATA_FIELD_ID)).isNull();
+      assertThat(stats.statsFor(MEASURE_FIELD_ID)).isNull();
+    }
+  }
+
+  @ParameterizedTest
+  @FieldSource("MANIFEST_FORMATS")
+  void requestedStatsAreProjectedWhenOmittedByCaller(FileFormat format) throws 
IOException {
+    TrackedFile file = fileWithStats("s3://bucket/file.parquet", 
contentStats());
+    InputFile manifest = writeManifest(format, CONTENT_STATS_TYPE, 
List.of(file));
+
+    // stats requested by field ID are read even though the projection omits 
them

Review Comment:
   sorry, the comment was outdated. It should really be saying that the 
projection drops `content_stats` completely and so the reader needs to add the 
requested stats only



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