Skip to content

[C++][Dataset] Negated comparison with a NaN literal skips row groups with Parquet statistics #55947

Backlog

Description

@zhaohaisun

Describe the bug, including details regarding any error messages, version, and platform.

A dataset filter of the form ~(x < NaN) or ~(x <= NaN) returns no rows when Parquet statistics are present, although it is true for every non-null row. In memory and without statistics the rows are returned. The column does not need to contain NaN, and integer columns compared through a cast are affected too.

import math
import pyarrow as pa, pyarrow.parquet as pq, pyarrow.dataset as ds, pyarrow.compute as pc

t = pa.table({"d": pa.array([-6.0, 3.0]), "i": pa.array([-6, 3], pa.int8())})
pq.write_table(t, "with_stats.parquet")
pq.write_table(t, "no_stats.parquet", write_statistics=False)

nan = pc.scalar(math.nan)
for e in [~(pc.field("d") < nan), ~(pc.field("d") <= nan), ~(pc.field("i").cast(pa.float64()) < nan)]:
    print(t.filter(e).num_rows,                                           # 2
          ds.dataset("no_stats.parquet").to_table(filter=e).num_rows,     # 2
          ds.dataset("with_stats.parquet").to_table(filter=e).num_rows)   # 0

~(x > NaN), ~(x >= NaN), x != NaN and ~(x == NaN) return the correct 2 rows.

Cause, from reading the code: Comparison::Execute in cpp/src/arrow/compute/expression_internal.h calls equal and then less, and returns GREATER when both are false. For a NaN literal both are false, so NaN is treated as greater than the statistics bound. Inequality::Simplify in expression.cc then sees the guarantee x <= 3 and the filter x < NaN with cmp_rhs_bound == GREATER and simplifies the filter to true. That is harmless on its own, because the filter is still evaluated per row, but under invert it becomes false and the row group is skipped. With a x >= min bound the same code bails out, which is why >/>= are not affected.

A possible fix: have Comparison::Execute also call greater and return NA when the values are unordered, and have Inequality::Simplify return the expression unchanged on NA (it currently only has DCHECK_NE(cmp_rhs_bound, Comparison::NA)). Alternatively, do not simplify comparisons whose literal is NaN.

Related but different: GH-51491 is about NaN values in the data that are not part of min/max. Here the data has no NaN; the NaN is in the literal, and fixing GH-51491 by widening the guarantee for float columns would not cover the integer case above.

Reproduces on pyarrow 10.0.1, 13.0.0, 16.1.0, 18.1.0, 22.0.0 and 26.0.1 (Linux x86_64, Python 3.11). The code is unchanged on main.

Component(s)

C++, Python

Drafted with Claude Code; reproduced and reviewed by me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions