chenjian2664 commented on code in PR #4346:
URL: https://github.com/apache/iceberg-python/pull/4346#discussion_r4680073860


##########
pyiceberg/io/pyarrow.py:
##########
@@ -1748,6 +1759,27 @@ def _get_column_projection_values(
     return projected_missing_fields
 
 
+def _has_nan_sensitive_predicate(expr: BooleanExpression) -> bool:
+    """Return whether a predicate in the expression can match NaN values that 
pyarrow may prune away.
+
+    Parquet statistics exclude NaN, so a row group whose non-NaN values are 
all equal reports min == max.
+    pyarrow then prunes it for predicates that the NaN values satisfy: IsNaN, 
NotEqualTo and NotIn on a
+    float or double column.

Review Comment:
   Nit: note in this docstring and `_nan_safe_conjuncts` that the input must be 
bound and already through `rewrite_not`, since `Not` nodes are not inspected. A 
new caller passing `Not(...)` would silently get False.



##########
pyiceberg/io/pyarrow.py:
##########
@@ -1817,8 +1862,7 @@ def _task_to_record_batches(
             fragment=fragment,
             schema=physical_schema,
             # This will push down the query to Arrow.

Review Comment:
   Nit: reword this comment. The PR removed its qualifying second line, and 
`scanner_filter` may now be only the NaN-safe conjuncts or `None`.
   
   ```suggestion
               # Push down the part of the filter that is safe to apply in 
Arrow; the rest is applied after the read
   ```



##########
tests/io/test_pyarrow.py:
##########
@@ -4587,6 +4592,62 @@ def 
test_task_to_record_batches_filter_after_positional_deletes_empty_result(tmp
     assert result_batches == []
 
 
+def 
test_task_to_record_batches_keeps_nan_rows_for_nan_sensitive_filters(tmpdir: 
str) -> None:
+    """Regression test: pyarrow must not prune a row group that holds NaN rows 
matching the filter.
+
+    Parquet statistics exclude NaN, so the row group [NaN, 5.0, 5.0] reports 
min == max == 5.0.
+    pyarrow prunes such a row group for IsNaN, NotEqualTo and NotIn, which 
drops the NaN row.
+    NotNaN is a control.
+    """
+    from pyiceberg.expressions.visitors import bind
+
+    for arrow_type, iceberg_type in [(pa.float32(), FloatType()), 
(pa.float64(), DoubleType())]:
+        arrow_schema = pa.schema(
+            (
+                pa.field("id", pa.int32(), nullable=True, 
metadata={PYARROW_PARQUET_FIELD_ID_KEY: "1"}),
+                pa.field("x", arrow_type, nullable=True, 
metadata={PYARROW_PARQUET_FIELD_ID_KEY: "2"}),
+            )
+        )
+        arrow_table = pa.table(
+            {"id": pa.array([0, 1, 2], type=pa.int32()), "x": 
pa.array([float("nan"), 5.0, 5.0], type=arrow_type)},
+            schema=arrow_schema,
+        )
+        data_file = _write_table_to_data_file(
+            f"{tmpdir}/nan_row_group_equal_bounds_{iceberg_type}.parquet", 
arrow_schema, arrow_table
+        )
+
+        table_schema = Schema(
+            NestedField(1, "id", IntegerType(), required=False),
+            NestedField(2, "x", iceberg_type, required=False),
+        )
+        projected_schema = Schema(NestedField(1, "id", IntegerType(), 
required=False))
+
+        cases: list[tuple[BooleanExpression, list[int]]] = [
+            (IsNaN("x"), [0]),
+            (NotEqualTo("x", 5.0), [0]),
+            (NotIn("x", {5.0, 6.0}), [0]),
+            (And(IsNaN("x"), EqualTo("id", 0)), [0]),
+            (Or(IsNaN("x"), EqualTo("id", 2)), [0, 2]),
+            (NotNaN("x"), [1, 2]),

Review Comment:
   Suggestion: add negated cases so this test guards the `rewrite_not` ordering 
at `pyarrow.py:1831`. `_has_nan_sensitive_predicate` never descends into `Not`, 
so if the check ran before the rewrite, `Not(NotNaN(x))` and `Not(EqualTo(x, 
5.0))` would drop the NaN row again while this test and #4350's 
`test_scan_negated_comparison_matches_file_pruning_for_nan` still pass.
   
   ```suggestion
               (NotNaN("x"), [1, 2]),
               (Not(NotNaN("x")), [0]),
               (Not(EqualTo("x", 5.0)), [0]),
   ```



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