timothyw553 commented on code in PR #19065:
URL: https://github.com/apache/iceberg/pull/19065#discussion_r4709451637


##########
orc/src/main/java/org/apache/iceberg/orc/ExpressionToSearchArgument.java:
##########
@@ -46,14 +49,46 @@ class ExpressionToSearchArgument
     extends ExpressionVisitors.BoundVisitor<ExpressionToSearchArgument.Action> 
{
 
   static SearchArgument convert(Expression expr, TypeDescription readSchema) {
-    Map<Integer, String> idToColumnName =
-        ORCSchemaUtil.idToOrcName(ORCSchemaUtil.convert(readSchema));
+    Schema schema = ORCSchemaUtil.convert(readSchema);
+    Map<Integer, String> idToColumnName = ORCSchemaUtil.idToOrcName(schema);
     SearchArgument.Builder builder = SearchArgumentFactory.newBuilder();
-    ExpressionVisitors.visit(expr, new ExpressionToSearchArgument(builder, 
idToColumnName))
+    ExpressionVisitors.visit(
+            expr,

Review Comment:
   to my understanding `ExpressionVisitors.visit` here still passes `NOT` 
directly to ORC, so something like `not(notNull("parent.leaf"))` bypasses the 
new `isNull` guard and can still drop rows with a null parent. should we 
normalize with `Expressions.rewriteNot(expr)` before converting, and test both 
filters on a real ORC read?
   ```diff
    ExpressionVisitors.visit(
   -        expr,
   +        Expressions.rewriteNot(expr),
   ```



##########
api/src/main/java/org/apache/iceberg/expressions/InclusiveMetricsEvaluator.java:
##########
@@ -96,9 +131,24 @@ private boolean eval(ContentFile<?> file) {
       return ExpressionVisitors.visitEvaluator(expr, this);
     }
 
+    private boolean hasSuspectLegacyOrcCounts(int id) {
+      if (fileFormat != FileFormat.ORC || 
!fieldsWithOptionalStructAncestor.contains(id)) {

Review Comment:
   what happens if a manifest projection omits `file_format` (like selecting 
only `file_path` or `ScanSummary`)? then `fileFormat` is `null` and `fileFormat 
!= FileFormat.ORC` skips the guard, so a legacy ORC file can still be pruned. 
could we treat `null` `fileFormat` conservatively here (or always project 
`file_format` when filtering) and add a partial-projection test?
   ```diff
   -if (fileFormat != FileFormat.ORC || 
!fieldsWithOptionalStructAncestor.contains(id)) {
   +if ((fileFormat != null && fileFormat != FileFormat.ORC)
   +    || !fieldsWithOptionalStructAncestor.contains(id)) {
   ```



##########
api/src/main/java/org/apache/iceberg/expressions/InclusiveMetricsEvaluator.java:
##########
@@ -55,6 +61,31 @@ public InclusiveMetricsEvaluator(Schema schema, Expression 
unbound) {
   public InclusiveMetricsEvaluator(Schema schema, Expression unbound, boolean 
caseSensitive) {
     StructType struct = schema.asStruct();
     this.expr = Binder.bind(struct, rewriteNot(unbound), caseSensitive);
+    this.fieldsWithOptionalStructAncestor = 
fieldsWithOptionalStructAncestor(schema);
+  }
+
+  private static Set<Integer> fieldsWithOptionalStructAncestor(Schema schema) {
+    Map<Integer, Integer> parents = TypeUtil.indexParents(schema.asStruct());
+    Set<Integer> fields = new HashSet<>();

Review Comment:
   checkstyle will flag `HashSet` here—could we switch to `Sets.newHashSet()` 
and run `spotlessCheck` / `checkstyleMain`?
   ```diff
   -import java.util.HashSet;
   +import org.apache.iceberg.relocated.com.google.common.collect.Sets;
   
   -Set<Integer> fields = new HashSet<>();
   +Set<Integer> fields = Sets.newHashSet();
   ```



##########
orc/src/main/java/org/apache/iceberg/orc/ExpressionToSearchArgument.java:
##########
@@ -46,14 +49,46 @@ class ExpressionToSearchArgument
     extends ExpressionVisitors.BoundVisitor<ExpressionToSearchArgument.Action> 
{
 
   static SearchArgument convert(Expression expr, TypeDescription readSchema) {
-    Map<Integer, String> idToColumnName =
-        ORCSchemaUtil.idToOrcName(ORCSchemaUtil.convert(readSchema));
+    Schema schema = ORCSchemaUtil.convert(readSchema);

Review Comment:
   isn't `readSchema` from `buildOrcProjection` missing requiredness attributes 
on structs? converting it back via `ORCSchemaUtil.convert(readSchema)` makes 
required parent structs look optional, which disables pruning unnecessarily on 
production reads (and the current test converts the schema directly so it 
doesn't hit this). could we pass the Iceberg schema from `OrcIterable` (or 
preserve requiredness in `buildOrcProjection`) and add a test through the 
projection path?



##########
core/src/test/java/org/apache/iceberg/TestLegacyOrcManifestReader.java:
##########
@@ -0,0 +1,94 @@
+/*
+ * 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 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.expressions.Expressions.isNull;
+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 java.io.IOException;
+import java.util.Map;
+import java.util.stream.Collectors;
+import org.apache.iceberg.io.OutputFile;
+import org.apache.iceberg.relocated.com.google.common.collect.Streams;
+import org.apache.iceberg.types.Types;
+import org.junit.jupiter.api.TestTemplate;
+
+public class TestLegacyOrcManifestReader extends TestBase {
+  @TestTemplate
+  public void testPreviouslyWrittenOrcManifestRemainsReadable() throws 
IOException {
+    Schema schema =
+        new Schema(
+            required(1, "id", Types.IntegerType.get()),
+            optional(2, "parent", Types.StructType.of(optional(3, "leaf", 
Types.IntegerType.get()))));
+    PartitionSpec spec = PartitionSpec.builderFor(schema).build();
+
+    // This is the metadata emitted by the pre-fix ORC metrics conversion for 
a file with
+    // two rows: parent = NULL, and parent = {leaf: 10}. No physical ORC data 
is needed
+    // because the regression happens before the reader opens the file.
+    Metrics legacyMetrics =
+        new Metrics(2L, null, Map.of(1, 2L, 3, 1L), Map.of(1, 0L, 3, 0L), 
null);
+    Metrics healthyMetrics =
+        new Metrics(2L, null, Map.of(1, 2L, 3, 2L), Map.of(1, 0L, 3, 0L), 
null);
+    DataFile legacy =
+        DataFiles.builder(spec)
+            .withPath("/path/to/legacy-nested-null.orc")
+            .withFileSizeInBytes(100)
+            .withMetrics(legacyMetrics)
+            .build();
+    DataFile healthy =
+        DataFiles.builder(spec)
+            .withPath("/path/to/healthy-nested.orc")
+            .withFileSizeInBytes(100)
+            .withMetrics(healthyMetrics)
+            .build();
+    DataFile parquet =
+        DataFiles.builder(spec)
+            .withPath("/path/to/healthy-nested.parquet")
+            .withFileSizeInBytes(100)
+            .withMetrics(healthyMetrics)
+            .build();
+
+    OutputFile outputFile =
+        table.ops()
+            .io()
+            .newOutputFile(
+                
temp.resolve(manifestFormat().addExtension("legacy-orc-m0")).toString());
+    ManifestWriter<DataFile> writer = ManifestFiles.write(formatVersion, spec, 
outputFile, 1000L);
+    try {
+      writer.add(legacy);
+      writer.add(healthy);
+      writer.add(parquet);
+    } finally {
+      writer.close();
+    }
+
+    // Exercise the persisted Avro (V1-V3) or Parquet (V4) manifest, not an 
in-memory evaluator.

Review Comment:
   nit: `TestBase` only runs v1-v3, not v4, so could we update this comment?



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