From 9b17b2ca50ec58a9cbebe033832c0a2b7534d033 Mon Sep 17 00:00:00 2001 From: rahulsmahadev Date: Wed, 11 Nov 2026 17:20:16 +0000 Subject: [PATCH 1/6] Fix scans dropping NaN rows for IsNaN, NotEqualTo, NotIn and NOT Parquet min/max statistics exclude NaN, so pyarrow could prune a row group whose non-NaN values are all equal and drop the NaN rows that these predicates match on float and double columns. Push only the conjuncts that cannot hide NaN rows to the scanner, and filter each batch with the full expression when a NaN-sensitive predicate is present. Co-authored-by: Isaac --- pyiceberg/io/pyarrow.py | 76 ++++++++++++++++++++----- tests/catalog/test_catalog_behaviors.py | 22 +++++++ tests/io/test_pyarrow.py | 60 +++++++++++++++++++ 3 files changed, 145 insertions(+), 13 deletions(-) diff --git a/pyiceberg/io/pyarrow.py b/pyiceberg/io/pyarrow.py index f62e53a911..b0379bcef4 100644 --- a/pyiceberg/io/pyarrow.py +++ b/pyiceberg/io/pyarrow.py @@ -70,7 +70,19 @@ from pyiceberg.conversions import to_bytes from pyiceberg.encryption.key_metadata import StandardKeyMetadata from pyiceberg.exceptions import ResolveError -from pyiceberg.expressions import AlwaysTrue, BooleanExpression, BoundIsNaN, BoundIsNull, BoundTerm, Not, Or +from pyiceberg.expressions import ( + AlwaysTrue, + And, + BooleanExpression, + BoundIsNaN, + BoundIsNull, + BoundNotEqualTo, + BoundNotIn, + BoundPredicate, + BoundTerm, + Not, + Or, +) from pyiceberg.expressions.literals import Literal from pyiceberg.expressions.visitors import ( BoundBooleanExpressionVisitor, @@ -1721,6 +1733,29 @@ def _get_column_projection_values( return projected_missing_fields +def _has_nan_sensitive_predicate(expr: BooleanExpression, negated: bool = False) -> 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, and any predicate on such a column under a NOT. + """ + if isinstance(expr, (And, Or)): + return _has_nan_sensitive_predicate(expr.left, negated) or _has_nan_sensitive_predicate(expr.right, negated) + if isinstance(expr, Not): + return _has_nan_sensitive_predicate(expr.child, not negated) + if isinstance(expr, BoundPredicate) and isinstance(expr.term.ref().field.field_type, (FloatType, DoubleType)): + return negated or isinstance(expr, (BoundIsNaN, BoundNotEqualTo, BoundNotIn)) + return False + + +def _nan_safe_conjuncts(expr: BooleanExpression) -> BooleanExpression: + """Return the top-level conjuncts that pyarrow can prune on without dropping NaN rows, joined with AND.""" + if isinstance(expr, And): + return And(_nan_safe_conjuncts(expr.left), _nan_safe_conjuncts(expr.right)) + return AlwaysTrue() if _has_nan_sensitive_predicate(expr) else expr + + def _task_to_record_batches( io: FileIO, task: FileScanTask, @@ -1761,12 +1796,27 @@ def _task_to_record_batches( ) pyarrow_filter = None + scanner_filter = None + # Set when the scanner does not apply the whole filter, so each batch is filtered after the read + filter_batches = False if bound_row_filter is not AlwaysTrue(): translated_row_filter = translate_column_names( bound_row_filter, file_schema, case_sensitive=case_sensitive, projected_field_values=projected_missing_fields ) bound_file_filter = bind(file_schema, translated_row_filter, case_sensitive=case_sensitive) pyarrow_filter = expression_to_pyarrow(bound_file_filter, file_schema) + if positional_deletes: + # Positional deletes must be applied before any row is filtered + filter_batches = True + elif _has_nan_sensitive_predicate(bound_file_filter): + # Parquet statistics exclude NaN, so pyarrow can prune a row group that holds NaN rows matching + # a NaN-sensitive conjunct. Only the other conjuncts are pushed down. + filter_batches = True + nan_safe_filter = _nan_safe_conjuncts(bound_file_filter) + if nan_safe_filter is not AlwaysTrue(): + scanner_filter = expression_to_pyarrow(nan_safe_filter, file_schema) + else: + scanner_filter = pyarrow_filter file_project_schema = prune_columns(file_schema, projected_field_ids, select_full_types=False) @@ -1774,8 +1824,7 @@ def _task_to_record_batches( fragment=fragment, schema=physical_schema, # This will push down the query to Arrow. - # But in case there are positional deletes, we have to apply them first - filter=pyarrow_filter if not positional_deletes else None, + filter=scanner_filter, columns=[col.name for col in file_project_schema.columns], ) @@ -1790,16 +1839,17 @@ def _task_to_record_batches( # Create the mask of indices that we're interested in indices = _combine_positional_deletes(positional_deletes, current_index, current_index + len(batch)) current_batch = current_batch.take(indices) - if pyarrow_filter is not None: - # Temporary fix until PyArrow 21 is the minimum supported version - # (https://github.com/apache/arrow/pull/46057): RecordBatch.filter raises - # IndexError on PyArrow <21 when the result is empty; Table.filter does not. - table = pa.Table.from_batches([current_batch]) - table = table.filter(pyarrow_filter) - if table.num_rows == 0: - current_batch = current_batch.slice(0, 0) - else: - current_batch = table.combine_chunks().to_batches()[0] + + if filter_batches and pyarrow_filter is not None: + # Temporary fix until PyArrow 21 is the minimum supported version + # (https://github.com/apache/arrow/pull/46057): RecordBatch.filter raises + # IndexError on PyArrow <21 when the result is empty; Table.filter does not. + table = pa.Table.from_batches([current_batch]) + table = table.filter(pyarrow_filter) + if table.num_rows == 0: + current_batch = current_batch.slice(0, 0) + else: + current_batch = table.combine_chunks().to_batches()[0] # skip empty batches if current_batch.num_rows == 0: diff --git a/tests/catalog/test_catalog_behaviors.py b/tests/catalog/test_catalog_behaviors.py index 1ed9ea4096..328fd7689b 100644 --- a/tests/catalog/test_catalog_behaviors.py +++ b/tests/catalog/test_catalog_behaviors.py @@ -1376,6 +1376,28 @@ def test_append_nan_to_identity_partitioned_table(catalog: Catalog) -> None: assert sorted(tbl.scan(row_filter="value is nan").to_arrow()["id"].to_pylist()) == [2, 4] +def test_scan_nan_row_with_equal_finite_statistics(catalog: Catalog) -> None: + catalog.create_namespace("default") + identifier = f"default.scan_nan_equal_statistics_{catalog.name}" + iceberg_schema = Schema( + NestedField(1, "id", IntegerType(), required=False), + NestedField(2, "value", DoubleType(), required=False), + ) + tbl = catalog.create_table(identifier=identifier, schema=iceberg_schema) + # Parquet statistics ignore NaN, so the single row group reports min == max == 5.0 for this file. + tbl.append( + pa.Table.from_pydict( + {"id": [1, 2, 3], "value": [float("nan"), 5.0, 5.0]}, + schema=schema_to_pyarrow(iceberg_schema), + ) + ) + + assert tbl.scan(row_filter="value is nan").to_arrow()["id"].to_pylist() == [1] + assert tbl.scan(row_filter="value != 5.0").to_arrow()["id"].to_pylist() == [1] + assert tbl.scan(row_filter="value not in (5.0, 6.0)").to_arrow()["id"].to_pylist() == [1] + assert tbl.scan(row_filter="value is not nan").to_arrow()["id"].to_pylist() == [2, 3] + + def test_scan_integer_column_with_decimal_literal(catalog: Catalog) -> None: catalog.create_namespace("default") identifier = f"default.scan_integer_decimal_literal_{catalog.name}" diff --git a/tests/io/test_pyarrow.py b/tests/io/test_pyarrow.py index 419d130965..e292139cc4 100644 --- a/tests/io/test_pyarrow.py +++ b/tests/io/test_pyarrow.py @@ -68,8 +68,13 @@ BoundStartsWith, EqualTo, GreaterThan, + IsNaN, IsNull, + LessThan, Not, + NotEqualTo, + NotIn, + NotNaN, Or, ) from pyiceberg.expressions.literals import literal @@ -4198,6 +4203,61 @@ 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, NotIn and a NOT over a float comparison, + which drops the NaN row. NotNaN and a NOT over an integer column are controls. + """ + from pyiceberg.expressions.visitors import bind + + arrow_schema = pa.schema( + ( + pa.field("id", pa.int32(), nullable=True, metadata={PYARROW_PARQUET_FIELD_ID_KEY: "1"}), + pa.field("x", pa.float64(), 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=pa.float64())}, + schema=arrow_schema, + ) + data_file = _write_table_to_data_file(f"{tmpdir}/nan_row_group_equal_bounds.parquet", arrow_schema, arrow_table) + + table_schema = Schema( + NestedField(1, "id", IntegerType(), required=False), + NestedField(2, "x", DoubleType(), 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]), + (Not(LessThan("x", 6.0)), [0]), + (Not(EqualTo("id", 1)), [0, 2]), + (NotNaN("x"), [1, 2]), + ] + for row_filter, expected_ids in cases: + # The filter column is not selected, so the filter must still see it after the read. + result_batches = list( + _task_to_record_batches( + PyArrowFileIO(), + FileScanTask(data_file), + bound_row_filter=bind(table_schema, row_filter, case_sensitive=True), + projected_schema=projected_schema, + table_schema=table_schema, + projected_field_ids={1, 2}, + positional_deletes=None, + case_sensitive=True, + ) + ) + actual_ids = [value for batch in result_batches for value in batch.column(0).to_pylist()] + assert actual_ids == expected_ids, str(row_filter) + + def test_task_to_record_batches_encrypted_by_iceberg_rust() -> None: """The data file was encrypted by iceberg-rust, see tests/table/encryption/README.md.""" data_file = DataFile.from_args( From 191387d0c070c63241a4dd31ac4010d42367a54f Mon Sep 17 00:00:00 2001 From: rahulsmahadev Date: Wed, 11 Nov 2026 20:30:45 +0000 Subject: [PATCH 2/6] Drop NOT handling and cover float32 in NaN-sensitive scans NOT is left to a follow-up that applies rewrite_not before the Arrow conversion, as the metrics evaluators do. Run the regression cases on float and double columns, and link the upstream Arrow issue. Co-authored-by: Isaac --- pyiceberg/io/pyarrow.py | 16 ++++---- tests/io/test_pyarrow.py | 89 ++++++++++++++++++++-------------------- 2 files changed, 52 insertions(+), 53 deletions(-) diff --git a/pyiceberg/io/pyarrow.py b/pyiceberg/io/pyarrow.py index b0379bcef4..f07cdd3108 100644 --- a/pyiceberg/io/pyarrow.py +++ b/pyiceberg/io/pyarrow.py @@ -78,7 +78,6 @@ BoundIsNull, BoundNotEqualTo, BoundNotIn, - BoundPredicate, BoundTerm, Not, Or, @@ -1733,19 +1732,17 @@ def _get_column_projection_values( return projected_missing_fields -def _has_nan_sensitive_predicate(expr: BooleanExpression, negated: bool = False) -> bool: +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, and any predicate on such a column under a NOT. + float or double column. """ if isinstance(expr, (And, Or)): - return _has_nan_sensitive_predicate(expr.left, negated) or _has_nan_sensitive_predicate(expr.right, negated) - if isinstance(expr, Not): - return _has_nan_sensitive_predicate(expr.child, not negated) - if isinstance(expr, BoundPredicate) and isinstance(expr.term.ref().field.field_type, (FloatType, DoubleType)): - return negated or isinstance(expr, (BoundIsNaN, BoundNotEqualTo, BoundNotIn)) + return _has_nan_sensitive_predicate(expr.left) or _has_nan_sensitive_predicate(expr.right) + if isinstance(expr, (BoundIsNaN, BoundNotEqualTo, BoundNotIn)): + return isinstance(expr.term.ref().field.field_type, (FloatType, DoubleType)) return False @@ -1810,7 +1807,8 @@ def _task_to_record_batches( filter_batches = True elif _has_nan_sensitive_predicate(bound_file_filter): # Parquet statistics exclude NaN, so pyarrow can prune a row group that holds NaN rows matching - # a NaN-sensitive conjunct. Only the other conjuncts are pushed down. + # a NaN-sensitive conjunct (https://github.com/apache/arrow/issues/51491). Only the other + # conjuncts are pushed down. filter_batches = True nan_safe_filter = _nan_safe_conjuncts(bound_file_filter) if nan_safe_filter is not AlwaysTrue(): diff --git a/tests/io/test_pyarrow.py b/tests/io/test_pyarrow.py index e292139cc4..b06bfca13e 100644 --- a/tests/io/test_pyarrow.py +++ b/tests/io/test_pyarrow.py @@ -4207,55 +4207,56 @@ def test_task_to_record_batches_keeps_nan_rows_for_nan_sensitive_filters(tmpdir: """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, NotIn and a NOT over a float comparison, - which drops the NaN row. NotNaN and a NOT over an integer column are controls. + 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 - arrow_schema = pa.schema( - ( - pa.field("id", pa.int32(), nullable=True, metadata={PYARROW_PARQUET_FIELD_ID_KEY: "1"}), - pa.field("x", pa.float64(), nullable=True, metadata={PYARROW_PARQUET_FIELD_ID_KEY: "2"}), + 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 ) - ) - arrow_table = pa.table( - {"id": pa.array([0, 1, 2], type=pa.int32()), "x": pa.array([float("nan"), 5.0, 5.0], type=pa.float64())}, - schema=arrow_schema, - ) - data_file = _write_table_to_data_file(f"{tmpdir}/nan_row_group_equal_bounds.parquet", arrow_schema, arrow_table) - table_schema = Schema( - NestedField(1, "id", IntegerType(), required=False), - NestedField(2, "x", DoubleType(), 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]), - (Not(LessThan("x", 6.0)), [0]), - (Not(EqualTo("id", 1)), [0, 2]), - (NotNaN("x"), [1, 2]), - ] - for row_filter, expected_ids in cases: - # The filter column is not selected, so the filter must still see it after the read. - result_batches = list( - _task_to_record_batches( - PyArrowFileIO(), - FileScanTask(data_file), - bound_row_filter=bind(table_schema, row_filter, case_sensitive=True), - projected_schema=projected_schema, - table_schema=table_schema, - projected_field_ids={1, 2}, - positional_deletes=None, - case_sensitive=True, - ) + table_schema = Schema( + NestedField(1, "id", IntegerType(), required=False), + NestedField(2, "x", iceberg_type, required=False), ) - actual_ids = [value for batch in result_batches for value in batch.column(0).to_pylist()] - assert actual_ids == expected_ids, str(row_filter) + 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]), + ] + for row_filter, expected_ids in cases: + # The filter column is not selected, so the filter must still see it after the read. + result_batches = list( + _task_to_record_batches( + PyArrowFileIO(), + FileScanTask(data_file), + bound_row_filter=bind(table_schema, row_filter, case_sensitive=True), + projected_schema=projected_schema, + table_schema=table_schema, + projected_field_ids={1, 2}, + positional_deletes=None, + case_sensitive=True, + ) + ) + actual_ids = [value for batch in result_batches for value in batch.column(0).to_pylist()] + assert actual_ids == expected_ids, f"{iceberg_type}: {row_filter}" def test_task_to_record_batches_encrypted_by_iceberg_rust() -> None: @@ -4514,7 +4515,7 @@ def test_orc_row_filtering_predicate_pushdown(tmp_path: Path) -> None: import pyarrow as pa import pyarrow.orc as orc - from pyiceberg.expressions import And, EqualTo, GreaterThan, In, LessThan, Or + from pyiceberg.expressions import And, EqualTo, GreaterThan, In, Or from pyiceberg.io.pyarrow import ArrowScan, PyArrowFileIO from pyiceberg.manifest import DataFileContent, FileFormat from pyiceberg.partitioning import PartitionSpec From 241c84ed2f3c089994d722da84923befa139a1ff Mon Sep 17 00:00:00 2001 From: rahulsmahadev Date: Thu, 12 Nov 2026 01:27:45 +0000 Subject: [PATCH 3/6] Fix unreachable-statement lint in _has_nan_sensitive_predicate mypy (warn_unreachable) flagged the final return in the pre-commit environment; fold the leaf check into one expression. Co-authored-by: Isaac --- pyiceberg/io/pyarrow.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pyiceberg/io/pyarrow.py b/pyiceberg/io/pyarrow.py index 90ae0526ab..8d3968a03a 100644 --- a/pyiceberg/io/pyarrow.py +++ b/pyiceberg/io/pyarrow.py @@ -1754,9 +1754,9 @@ def _has_nan_sensitive_predicate(expr: BooleanExpression) -> bool: """ if isinstance(expr, (And, Or)): return _has_nan_sensitive_predicate(expr.left) or _has_nan_sensitive_predicate(expr.right) - if isinstance(expr, (BoundIsNaN, BoundNotEqualTo, BoundNotIn)): - return isinstance(expr.term.ref().field.field_type, (FloatType, DoubleType)) - return False + return isinstance(expr, (BoundIsNaN, BoundNotEqualTo, BoundNotIn)) and isinstance( + expr.term.ref().field.field_type, (FloatType, DoubleType) + ) def _nan_safe_conjuncts(expr: BooleanExpression) -> BooleanExpression: From 0520938e4986b65bfa0f856e0e7d30da4b9081a6 Mon Sep 17 00:00:00 2001 From: rahulsmahadev Date: Wed, 25 Nov 2026 00:12:07 +0000 Subject: [PATCH 4/6] Document the rewrite_not precondition and test negated filters Reword the pushdown comment, since scanner_filter can be only the NaN-safe conjuncts, and add Not(NotNaN(x)) and Not(EqualTo(x, 5.0)) cases, which fail if the NaN check runs before rewrite_not. Co-authored-by: Isaac --- pyiceberg/io/pyarrow.py | 9 +++++++-- tests/io/test_pyarrow.py | 2 ++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/pyiceberg/io/pyarrow.py b/pyiceberg/io/pyarrow.py index 2b7311a422..549468d8d5 100644 --- a/pyiceberg/io/pyarrow.py +++ b/pyiceberg/io/pyarrow.py @@ -1765,6 +1765,8 @@ def _has_nan_sensitive_predicate(expr: BooleanExpression) -> bool: 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. + + The expression must be bound and already rewritten by `rewrite_not`, because `Not` nodes are not inspected. """ if isinstance(expr, (And, Or)): return _has_nan_sensitive_predicate(expr.left) or _has_nan_sensitive_predicate(expr.right) @@ -1774,7 +1776,10 @@ def _has_nan_sensitive_predicate(expr: BooleanExpression) -> bool: def _nan_safe_conjuncts(expr: BooleanExpression) -> BooleanExpression: - """Return the top-level conjuncts that pyarrow can prune on without dropping NaN rows, joined with AND.""" + """Return the top-level conjuncts that pyarrow can prune on without dropping NaN rows, joined with AND. + + The expression must be bound and already rewritten by `rewrite_not`, because `Not` nodes are not inspected. + """ if isinstance(expr, And): return And(_nan_safe_conjuncts(expr.left), _nan_safe_conjuncts(expr.right)) return AlwaysTrue() if _has_nan_sensitive_predicate(expr) else expr @@ -1861,7 +1866,7 @@ def _task_to_record_batches( fragment_scanner = ds.Scanner.from_fragment( fragment=fragment, schema=physical_schema, - # This will push down the query to Arrow. + # Push down the part of the filter that is safe to apply in Arrow; the rest is applied after the read filter=scanner_filter, columns=[col.name for col in file_project_schema.columns], ) diff --git a/tests/io/test_pyarrow.py b/tests/io/test_pyarrow.py index 8b8680e008..e2c4a00829 100644 --- a/tests/io/test_pyarrow.py +++ b/tests/io/test_pyarrow.py @@ -4629,6 +4629,8 @@ def test_task_to_record_batches_keeps_nan_rows_for_nan_sensitive_filters(tmpdir: (And(IsNaN("x"), EqualTo("id", 0)), [0]), (Or(IsNaN("x"), EqualTo("id", 2)), [0, 2]), (NotNaN("x"), [1, 2]), + (Not(NotNaN("x")), [0]), + (Not(EqualTo("x", 5.0)), [0]), ] for row_filter, expected_ids in cases: # The filter column is not selected, so the filter must still see it after the read. From 31b282a8b4d1fb2c154670e96374fb029cd03cf2 Mon Sep 17 00:00:00 2001 From: rahulsmahadev Date: Sun, 29 Nov 2026 02:16:12 +0000 Subject: [PATCH 5/6] Test _nan_safe_conjuncts directly IsNaN(x) AND id = 0 keeps only id = 0, and IsNaN(x) alone is AlwaysTrue. Co-authored-by: Isaac --- tests/io/test_pyarrow.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/io/test_pyarrow.py b/tests/io/test_pyarrow.py index 11e997dc3d..67830a86a9 100644 --- a/tests/io/test_pyarrow.py +++ b/tests/io/test_pyarrow.py @@ -97,6 +97,7 @@ _ConvertViewTypes, _create_arrow_read_format, _determine_partitions, + _nan_safe_conjuncts, _primitive_to_physical, _read_all_delete_files, _read_deletes, @@ -4847,6 +4848,15 @@ def test_task_to_record_batches_keeps_nan_rows_for_nan_sensitive_filters(tmpdir: assert actual_ids == expected_ids, f"{iceberg_type}: {row_filter}" +def test_nan_safe_conjuncts_keeps_only_nan_safe_conjuncts() -> None: + from pyiceberg.expressions.visitors import bind + + table_schema = Schema(NestedField(1, "id", IntegerType(), required=False), NestedField(2, "x", DoubleType(), required=False)) + row_filter = bind(table_schema, And(IsNaN("x"), EqualTo("id", 0)), case_sensitive=True) + assert _nan_safe_conjuncts(row_filter) == bind(table_schema, EqualTo("id", 0), case_sensitive=True) + assert _nan_safe_conjuncts(bind(table_schema, IsNaN("x"), case_sensitive=True)) is AlwaysTrue() + + @pytest.mark.parametrize( "stored_row_ids, first_row_id, deleted_positions, row_filter, expected", [ From f145b9999a6cf16773ffe19cf7a2c96703cd2993 Mon Sep 17 00:00:00 2001 From: rahulsmahadev Date: Mon, 30 Nov 2026 00:07:31 +0000 Subject: [PATCH 6/6] Recurse into Or when pushing down NaN-safe predicates _nan_safe_conjuncts kept only top-level conjuncts, so a NaN-sensitive leaf anywhere under an Or dropped the whole Or from the pushed-down filter. _nan_safe_filter recurses into And and Or and replaces only the NaN-sensitive predicates with AlwaysTrue, so (x is nan AND id = 7) OR id = 8 pushes down id = 7 OR id = 8. Co-authored-by: Isaac --- pyiceberg/io/pyarrow.py | 18 +++++++++++------- tests/io/test_pyarrow.py | 14 +++++++++----- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/pyiceberg/io/pyarrow.py b/pyiceberg/io/pyarrow.py index adadab4ca6..183bd35a4b 100644 --- a/pyiceberg/io/pyarrow.py +++ b/pyiceberg/io/pyarrow.py @@ -1857,13 +1857,17 @@ def _has_nan_sensitive_predicate(expr: BooleanExpression) -> bool: ) -def _nan_safe_conjuncts(expr: BooleanExpression) -> BooleanExpression: - """Return the top-level conjuncts that pyarrow can prune on without dropping NaN rows, joined with AND. +def _nan_safe_filter(expr: BooleanExpression) -> BooleanExpression: + """Return a weaker form of the expression that pyarrow can prune on without dropping NaN rows. - The expression must be bound and already rewritten by `rewrite_not`, because `Not` nodes are not inspected. + Each NaN-sensitive predicate is replaced by AlwaysTrue, and the And/Or structure is kept, so the result is + implied by the expression. The expression must be bound and already rewritten by `rewrite_not`, because `Not` + nodes are not inspected. """ if isinstance(expr, And): - return And(_nan_safe_conjuncts(expr.left), _nan_safe_conjuncts(expr.right)) + return And(_nan_safe_filter(expr.left), _nan_safe_filter(expr.right)) + if isinstance(expr, Or): + return Or(_nan_safe_filter(expr.left), _nan_safe_filter(expr.right)) return AlwaysTrue() if _has_nan_sensitive_predicate(expr) else expr @@ -1929,10 +1933,10 @@ def _task_to_record_batches( pyarrow_filter = expression_to_pyarrow(bound_file_filter, file_schema) if not _has_nan_sensitive_predicate(bound_file_filter): scanner_filter = pyarrow_filter - elif (nan_safe_filter := _nan_safe_conjuncts(bound_file_filter)) is not AlwaysTrue(): + elif (nan_safe_filter := _nan_safe_filter(bound_file_filter)) is not AlwaysTrue(): # Parquet statistics exclude NaN, so pyarrow can prune a row group that holds NaN rows matching - # a NaN-sensitive conjunct (https://github.com/apache/arrow/issues/51491). Only the other - # conjuncts are pushed down. + # a NaN-sensitive predicate (https://github.com/apache/arrow/issues/51491). Only the parts of the + # filter that cannot drop such rows are pushed down. scanner_filter = expression_to_pyarrow(nan_safe_filter, file_schema) file_project_schema = prune_columns(file_schema, projected_field_ids, select_full_types=False) diff --git a/tests/io/test_pyarrow.py b/tests/io/test_pyarrow.py index 67830a86a9..e9c27ffa7e 100644 --- a/tests/io/test_pyarrow.py +++ b/tests/io/test_pyarrow.py @@ -97,7 +97,7 @@ _ConvertViewTypes, _create_arrow_read_format, _determine_partitions, - _nan_safe_conjuncts, + _nan_safe_filter, _primitive_to_physical, _read_all_delete_files, _read_deletes, @@ -4825,7 +4825,7 @@ def test_task_to_record_batches_keeps_nan_rows_for_nan_sensitive_filters(tmpdir: (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]), + (Or(IsNaN("x"), EqualTo("id", 7)), [0]), (NotNaN("x"), [1, 2]), (Not(NotNaN("x")), [0]), (Not(EqualTo("x", 5.0)), [0]), @@ -4848,13 +4848,17 @@ def test_task_to_record_batches_keeps_nan_rows_for_nan_sensitive_filters(tmpdir: assert actual_ids == expected_ids, f"{iceberg_type}: {row_filter}" -def test_nan_safe_conjuncts_keeps_only_nan_safe_conjuncts() -> None: +def test_nan_safe_filter_keeps_only_nan_safe_predicates() -> None: from pyiceberg.expressions.visitors import bind table_schema = Schema(NestedField(1, "id", IntegerType(), required=False), NestedField(2, "x", DoubleType(), required=False)) row_filter = bind(table_schema, And(IsNaN("x"), EqualTo("id", 0)), case_sensitive=True) - assert _nan_safe_conjuncts(row_filter) == bind(table_schema, EqualTo("id", 0), case_sensitive=True) - assert _nan_safe_conjuncts(bind(table_schema, IsNaN("x"), case_sensitive=True)) is AlwaysTrue() + assert _nan_safe_filter(row_filter) == bind(table_schema, EqualTo("id", 0), case_sensitive=True) + assert _nan_safe_filter(bind(table_schema, IsNaN("x"), case_sensitive=True)) is AlwaysTrue() + # Or keeps its branches, so the id predicates of both branches can still prune row groups + row_filter = bind(table_schema, Or(And(IsNaN("x"), EqualTo("id", 7)), EqualTo("id", 8)), case_sensitive=True) + assert _nan_safe_filter(row_filter) == bind(table_schema, Or(EqualTo("id", 7), EqualTo("id", 8)), case_sensitive=True) + assert _nan_safe_filter(bind(table_schema, Or(IsNaN("x"), EqualTo("id", 8)), case_sensitive=True)) is AlwaysTrue() @pytest.mark.parametrize(