Skip to content

[Data] Stop Parquet filter pushdown from dropping rows on NaN floats and integer overflow - #68325

Merged
richardliaw merged 8 commits into
ray-project:masterfrom
AarryaSaraf:data-float-nan-pushdown
Nov 20, 2026
Merged

richardliaw merged 8 commits into
ray-project:masterfrom
AarryaSaraf:data-float-nan-pushdown

Conversation

@AarryaSaraf

@AarryaSaraf AarryaSaraf commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Why

A filter pushed into a Parquet read can return fewer rows than the same filter on materialized data:

pq.write_table(pa.table({"id": [0, 1], "x": [40.0, float("nan")]}), path)
ds = ray.data.read_parquet(path)
ds.filter(expr=col("x") != 40.0).take_all()                # before: []   after: [{'id': 1, 'x': nan}]
ds.materialize().filter(expr=col("x") != 40.0).take_all()  # [{'id': 1, 'x': nan}]

NaN != 40.0 is true in Arrow, NumPy and Spark, so row 1 matches. It is lost because PyArrow skips whole row groups using the min/max in the Parquet footer, and those min/max leave NaN out. The group [40.0, NaN] is stored as min = max = 40.0, which reads as "every value is 40", so PyArrow skips it. Whether the row comes back depends on its neighbours: a group [35.0, NaN] keeps it.

Only filters that can match NaN are hit: !=, not_in, is_in with NaN in its list, and negated comparisons such as ~(x >= 30.0) or (x >= 30.0) == False. Signed zero hits is_in too: is_in([0.0]) on [-0.0, 0.0] skips the group. <, <=, >, >=, == and is_in with neither NaN nor zero in its list never lose a row, so skipping on them is safe.

A second bug has the same symptom. PyArrow computes arithmetic in a pushed filter in the column's own type, so on an int8 column x + 100 with x = 100 wraps to -56, while Ray's in-memory filter widens to int64 and gets 200. filter(expr=(col("x") + 100) > 120) returns no rows instead of one.

Changes

datasource_v2/common/pushdown_utils.py adds two checks: is_statistics_safe (a float column is used only in <, <=, >, >=, ==, a null check, or is_in with no NaN or zero in its list) and has_column_arithmetic. A filter that fails either one is not pushed into the scan; Ray runs it on the rows after reading, as it does for any filter it cannot push.

  • V2, formats/parquet/parquet_scanner.py: push_filters checks each part of a top-level & on its own, so (x != 40.0) & (id >= 0) still pushes id >= 0.
  • V1, datasource/parquet_datasource.py: V1 pushes a filter whole or not at all, so apply_predicate keeps the whole filter after the read if any part fails.
  • tests/test_predicate_pushdown.py: test_pushed_filter_matches_in_memory_filter compares pushed and in-memory results for 9 filters on V1 and V2.

Cost

Filters that are no longer pushed lose row-group skipping. On a sorted float column with 100 row groups:

Filter Row groups read, before After
x != 40.0, x.not_in([40.0]) 100 100
~(x >= 30.0) 31 100
x.is_in([40.0, 41.0]) 2 2
x < 30.0 31 31

A TODO on each check names the upstream fix: NaN and signed zero in PyArrow's statistics (apache/arrow#50807, apache/arrow#54915), and widening in Expr.to_pyarrow().

Checks

  • Not a duplicate: no open issue or PR covers this. [Data] Prune Parquet row groups for mixed partition/data OR filters #67855 adds a separate pruning path that copies both checks; it will import them from here once this lands.
  • pytest python/ray/data/tests/test_predicate_pushdown.py -k test_pushed_filter_matches_in_memory_filter -v: 18 passed. With both checks disabled: 16 failed, 2 passed (the x > 30.0 case, which is safe).
  • AI assistance was used; I reviewed every changed line.

Signed-off-by: Aarrya [email protected]

🤖 Generated with Claude Code

@AarryaSaraf
AarryaSaraf marked this pull request as ready for review November 19, 2026 03:35
@AarryaSaraf
AarryaSaraf requested review from a team as code owners November 19, 2026 03:35
@AarryaSaraf AarryaSaraf added the go add ONLY when ready to merge, run all tests label Nov 19, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request addresses an issue where Parquet row-group pruning can incorrectly drop NaN rows because Parquet min/max statistics skip NaN values. It introduces a safety check, is_statistics_safe, to identify predicates that are safe to push down on float columns, and updates both ParquetDatasource and ParquetScanner to only push down safe filters while keeping unsafe ones above the read. The review feedback suggests optimizing this safety check by precomputing and passing the set of float columns through recursive calls and during filter pushdown planning to avoid redundant schema scans.

return is_statistics_safe(
predicate=predicate.left, schema=schema
) and is_statistics_safe(predicate=predicate.right, schema=schema)
float_columns = {field.name for field in schema if pa.types.is_floating(field.type)}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to Gemini’s comment, this line rebuilds the float-column set for each visited leaf. Precomputing it in push_filters covers V2, but V1 still passes only schema. If the helper builds the set after recursing into AND/OR, V1 still repeats the scan. Could we compute it once per planning call and pass only the set to the internal checker?

I compared 3021ed6 with the once-per-call variant on single-node Ray with PyArrow 23.0.1:

Check PR head Once per call
Focused Ray tests (14 from this PR + 22 additional V1/V2 cases) 36 passed 36 passed
Schema field type checks (256 columns, 32 terms; both paths) 8,192 256
V1 safety check (2,048-column synthetic schema) 34.0 ms 1.20 ms
V2 predicate planning (2,048-column synthetic schema) 36.5 ms 1.15 ms

The 22 additional cases matched on expected row IDs and pushdown/residual decisions. The timings are for planning only, not Parquet read latency. I’d treat this as non-blocking.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for measuring this. Done in 6304dd7: float_column_names(schema) builds the set once per planning call and is_statistics_safe takes the set, for both V1 and V2.

@ray-gardener ray-gardener Bot added the data Ray Data-related issues label Nov 19, 2026
m4ru1 added a commit to m4ru1/ray that referenced this pull request Nov 19, 2026
Keep unsafe float predicates and column arithmetic out of the residual hint
so mixed partition/data filters retain matching rows. Add both row-loss
regressions and address the named-argument and docstring review comments.

Reuse is_statistics_safe unchanged from Aarrya Saraf's PR ray-project#68325:
ray-project#68325
Source revision: 3021ed6.
Only the shared helper is included; the existing-filter pushdown fixes
remain in ray-project#68325.

Validated the residual, footer, planning, and synthesized-column tests on
PyArrow 23.0.1, 25.0.1, and 26.0.1. Local checks also covered ordinary UDF
fallback and the arithmetic pruning boundary. Pre-commit passed.

Co-authored-by: Aarrya <[email protected]>
Signed-off-by: mora <[email protected]>
@AarryaSaraf AarryaSaraf changed the title [Data] Stop Parquet filter pushdown from dropping NaN rows on float columns [Data] Stop Parquet filter pushdown from dropping rows on NaN floats and integer overflow Nov 19, 2026
@AarryaSaraf

Copy link
Copy Markdown
Contributor Author

Pushed daf8618d83: float is_in is pushed down again unless its list contains NaN or zero, so it keeps row-group skipping. On a sorted float column with 100 row groups, x.is_in([40.0, 41.0]) reads 2 row groups instead of all 100.

Lists with zero stay after the read because PyArrow's statistics don't tell -0.0 from 0.0:

pq.write_table(pa.table({"x": [-0.0, 0.0]}), path)
ray.data.read_parquet(path).filter(expr=col("x").is_in([0.0])).take_all()
# pushed: []   in memory: [{'x': 0.0}]

The other filters stay after the read. For !=, not_in and column arithmetic that costs almost nothing: they read 98-100 of 100 row groups even when pushed. Negated comparisons such as ~(x >= 30.0) do lose skipping, but pushing them drops NaN rows until PyArrow reads Parquet's NaN count (apache/arrow#50807).

"""
# Filters PyArrow would evaluate differently from Ray must stay in the
# ``Filter`` above the read; returning ``self`` keeps it there.
if has_column_arithmetic(expr=predicate_expr) or not is_statistics_safe(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In V1, if any part of the filter is risky, this returns early and pushes nothing. That includes partition pruning (skipping folders like country=CA/), which is always safe. Example: a table with country=US/CA/DE/FR and filter((col("country") == "US") & (col("x") != 1.0)) read 1 file before this PR and reads all 4 with it. On a table split by date, that means reading every date instead of one. This hits users who switch to V1 with use_datasource_v2=False.

The pushdown rule already splits A & B into parts it pushes and parts it keeps as a Filter (PredicatePushdown._split_by_convertibility). Could risky parts go into the "keep" list there? Then V1 still skips partitions and pushes the safe parts, as the V2 change in this PR does.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right this is a v1 regression but this is parquet specific and v1 for parquet is deprecated

return {field.name for field in schema if pa.types.is_floating(field.type)}


def is_statistics_safe(predicate: Expr, float_columns: AbstractSet[str]) -> bool:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Iceberg has the same problem: read_iceberg(...).filter(col("f") != 0.1) drops the NaN row, in V1 on master and in the V2 scanner in #68152. These checks are shared, but only Parquet uses them. Could Iceberg use them too, in #68152 or a follow-up?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Iceberg metadata contains NaN counts so this wont be an issue there.

"int8_add",
],
)
def test_pushed_filter_matches_in_memory_filter(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test only compares rows, so it also passes if nothing is pushed at all. For example, it doesn't notice that in V1, filter((col("country") == "US") & (col("x") != 1.0)) on a partitioned table now reads every partition instead of one. Could it also check what gets pushed? For example: on V2, ne_and_int still pushes id >= 0; and on V1, a table with a partition column still skips the other partitions.

@abhishekverma-ray abhishekverma-ray left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM, this fixes silently dropped rows on the default path. Left some non-blocking comments inline.

Could you open a follow-up for the V1 partition-pruning slowdown, and use the same checks for Iceberg (#68152 or an issue)?

@richardliaw
richardliaw merged commit d6432c7 into ray-project:master Nov 20, 2026
6 checks passed
m4ru1 added a commit to m4ru1/ray that referenced this pull request Nov 20, 2026
Use partition facts and the existing Arrow footer pruning path during
Datasource V2 listing, retaining the full residual filter after the read.
Keep safe AND conjuncts eligible for pruning when other conjuncts are unsafe.

Rebase onto d6432c7 after ray-project#68325 merged and import its shared
float and arithmetic checks instead of carrying local copies. Consolidate
the reader-cast regression with the NaN and integer-overflow cases.

Signed-off-by: mora <[email protected]>
cryptus-neoxys added a commit to cryptus-neoxys/ray that referenced this pull request Nov 24, 2026
A filtered count whose predicate the scanner only partially pushes
(ray-project#68325 declines NaN-unsafe float comparisons and column arithmetic)
keeps a residual Filter above the read. That Filter requires its
columns, so the read's projection cannot be empty and the exact-count
stub cannot fire while the residual still has rows to drop. Pin the
plan shape so the two conditions stay mutually exclusive.

Signed-off-by: Dev Sharma <[email protected]>
m4ru1 added a commit to m4ru1/ray that referenced this pull request Nov 24, 2026
Use partition facts and the existing Arrow footer pruning path during
Datasource V2 listing, retaining the full residual filter after the read.
Keep safe AND conjuncts eligible for pruning when other conjuncts are unsafe.

Rebase onto d6432c7 after ray-project#68325 merged and import its shared
float and arithmetic checks instead of carrying local copies. Consolidate
the reader-cast regression with the NaN and integer-overflow cases.

Signed-off-by: mora <[email protected]>
m4ru1 added a commit to m4ru1/ray that referenced this pull request Nov 24, 2026
Use partition facts and the existing Arrow footer pruning path during
Datasource V2 listing, retaining the full residual filter after the read.
Keep safe AND conjuncts eligible for pruning when other conjuncts are unsafe.

Rebase onto d6432c7 after ray-project#68325 merged and import its shared
float and arithmetic checks instead of carrying local copies. Consolidate
the reader-cast regression with the NaN and integer-overflow cases.

Signed-off-by: mora <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

data Ray Data-related issues go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants