Fokko commented on code in PR #17648:
URL: https://github.com/apache/iceberg/pull/17648#discussion_r4712415008


##########
api/src/main/java/org/apache/iceberg/expressions/StrictEvalVisitor.java:
##########
@@ -457,6 +450,27 @@ public <T> Boolean notStartsWith(BoundReference<T> ref, 
Literal<T> lit) {
     return ROWS_MIGHT_NOT_MATCH;
   }
 
+  /**
+   * Returns true if the field is optional and its null count is non-zero or 
unknown.

Review Comment:
   `BoundReference.producesNull()` only returns `field.isOptional()` and 
ignores parent structs. A required field inside an optional struct can still be 
null. The spec says so for equality deletes: "if a parent struct column is null 
it implies the leaf column is null". It's also why fields in optional structs 
can't be identifier fields. For `optional struct s { required int req }`, 
`producesNull()` returns `false` for `s.req`.
   
   The Binder already handles this with `!producesNull() && 
allAncestorFieldsAreRequired(...)` in `UnboundPredicate.bindUnaryOperation`. 
Here, correctness depends on every caller returning early on 
`isNestedColumn(id)` first. That's true today, so the PR has no bug. But if 
nested support is added to the strict evaluator later, `s.req < X` would return 
`ROWS_MUST_MATCH` for files where `s` is null, and those files would be deleted 
whole. The Javadoc ("Returns true if the field is optional") makes that mistake 
easy.
   
   Could we make the check ancestor-aware, reusing `TypeUtil.ancestorFields` 
like the Binder does, ideally through a shared helper rather than a copy? At 
minimum, please document that this assumes a top-level reference.
   
   A test would pin the current behavior down. The struct in the test schema 
only has optional fields, so a required one is needed, e.g. `required(19, 
"nested_required_col", IntegerType.get())` under `struct` (id 15). Then assert 
that `lessThan("struct.nested_required_col", ...)` returns `false` when null 
counts are missing.
   



##########
api/src/main/java/org/apache/iceberg/expressions/StrictEvalVisitor.java:
##########
@@ -457,6 +450,27 @@ public <T> Boolean notStartsWith(BoundReference<T> ref, 
Literal<T> lit) {
     return ROWS_MIGHT_NOT_MATCH;
   }
 
+  /**
+   * Returns true if the field is optional and its null count is non-zero or 
unknown.
+   *
+   * <p>A null value does not match a comparison predicate, so all rows can 
only match when the
+   * field has no nulls.
+   */
+  private boolean canContainNulls(BoundReference<?> ref) {
+    return ref.producesNull() && mayContainNull(ref.fieldId());

Review Comment:
   For required fields this skips the null count entirely, even when the stats 
show nulls. Before this change, a known non-zero null count always returned 
`ROWS_MIGHT_NOT_MATCH`. Now a required column with `null_value_counts = {1: 
10}` and bounds `[1, 5]` returns `ROWS_MUST_MATCH` for `id < 10`. I checked 
this against the parent commit: `false` before, `true` with this PR.
   
   This can happen in practice. `allowIncompatibleChanges().requireColumn(...)` 
makes a column required after files with nulls were written. 
`ManifestFilterManager` and `SparkTable` evaluate with the current schema, so a 
metadata-only delete would drop the whole file, including the null rows.
   
   Optionality should only be used when the null count is unknown:
   
   ```java
   private boolean canContainNulls(BoundReference<?> ref) {
     int id = ref.fieldId();
     if (ref.producesNull()) {
       return mayContainNull(id);
     }
   
     // required fields may still contain nulls in files written before the 
field was required
     return containsNulls(id);
   }
   ```
   
   `containsNulls(int id)` would be a new abstract method: "true if the null 
count is known and non-zero". It's close to the `canContainNulls(int)` this PR 
removes, but without the `nullCounts == null -> true` branch.
   
   It would also be good to have a test for this:
   
   ```java
   @Test
   void requiredColumnWithNullCount() {
     DataFile file =
         new TestDataFile(
             "required_with_nulls.avro",
             Row.of(),
             50,
             ImmutableMap.of(1, 50L),
             ImmutableMap.of(1, 10L),
             null,
             ImmutableMap.of(1, toByteBuffer(IntegerType.get(), INT_MIN_VALUE)),
             ImmutableMap.of(1, toByteBuffer(IntegerType.get(), 
INT_MAX_VALUE)));
   
     boolean shouldRead =
         new StrictMetricsEvaluator(SCHEMA, lessThan("id", INT_MAX_VALUE + 
1)).eval(file);
     assertThat(shouldRead)
         .as("Should not match: null count shows that the column contains 
nulls")
         .isFalse();
   }
   ```
   



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