Skip to content

[Python][C++][Dataset] Parquet row groups are skipped for comparisons on cast(x, float32) when x is float64 #55886

Backlog

Description

@zhaohaisun

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

When a dataset filter compares a float64 column cast to float32 with a literal, Parquet row-group statistics can skip row groups that contain matching rows. Without statistics, or on an in-memory table, the same filter matches.

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

t = pa.table({"x": pa.array([17.799999999999997, 23.0])})
pq.write_table(t, "with_stats.parquet")
pq.write_table(t, "no_stats.parquet", write_statistics=False)

e = pc.field("x").cast(pa.float32()) <= pa.scalar(17.799999237060547, pa.float32())
print(ds.dataset("with_stats.parquet").to_table(filter=e).num_rows)  # 0
print(ds.dataset("no_stats.parquet").to_table(filter=e).num_rows)    # 1
print(t.filter(e).num_rows)                                          # 1

17.799999999999997 cast to float32 is 17.799999237060547, so the first row matches. The >= direction fails the same way:

t = pa.table({"x": pa.array([1.0, 17.8000008])})
e = pc.field("x").cast(pa.float32()) >= pa.scalar(17.80000114440918, pa.float32())
# with statistics: 0 rows; without statistics and in memory: 1 row

Cause, from reading the code: Inequality::Simplify in cpp/src/arrow/compute/expression.cc calls Comparison::StripOrderPreservingCasts on the left-hand side, then compares the float32 literal with the guarantee bound, which is in the column's own type (float64). StripOrderPreservingCasts in expression_internal.h strips every cast to a floating-point type whose source is an integer or floating-point type, including float64 -> float32. That cast is monotonic but rounds, so cast(x) <= c can hold while x > c, and comparing the literal with the uncast bound proves the row group empty when it is not.

A possible fix: strip only casts that are exact for every input (float32 -> float64, narrower -> wider integers, integer -> floating point only when the integer fits in the mantissa), or apply the same cast to the guarantee bound before comparing. SimplifyIsIn uses the same helper.

Reproduces on pyarrow 10.0.1, 12.0.1, 14.0.2, 15.0.2, 16.1.0, 17.0.0, 18.0.0, 20.0.0, 22.0.0, 24.0.0 and 26.0.1 (Linux x86_64, Python 3.11).

Component(s)

C++, Python

Activity

  1. changed the title [-][C++][Dataset] Parquet row groups are skipped for comparisons on cast(x, float32) when x is float64[/-] [+][Python][C++][Dataset] Parquet row groups are skipped for comparisons on cast(x, float32) when x is float64[/+] on Dec 1, 2026
  2. TatvagnaTandel commented on Dec 3, 2026

    @TatvagnaTandel

    Hi @zhaohaisun , could you take a look at PR #55941? If either of your original test cases still fails with the fix, could you share the result you get? Thanks for the clear repro :D

  3. zhaohaisun commented on Dec 3, 2026

    @zhaohaisun
    Author

    Thanks for the PR. I have not built #55941, but both original cases (cast(x, float32) <= c and >= c on a float64 column) are what its new Parquet tests cover, so I expect them to pass.

    One related case that the patch leaves unchanged: integer → floating-point casts are still stripped, although they can round as well (int32/int64 above 2^24 for float32, int64 above 2^53 for float64). With an unsafe cast the scan then fails only when statistics are present (pyarrow 26.0.1):

    import pyarrow as pa, pyarrow.parquet as pq, pyarrow.dataset as ds, pyarrow.compute as pc
    
    t = pa.table({"x": pa.array([16777217, 20000000], pa.int32())})
    pq.write_table(t, "with_stats.parquet")
    pq.write_table(t, "no_stats.parquet", write_statistics=False)
    
    e = pc.field("x").cast(pa.float32(), safe=False) <= pa.scalar(16777216.0, pa.float32())
    print(t.filter(e).num_rows)                                          # 1  (16777217 -> 16777216.0)
    print(ds.dataset("no_stats.parquet").to_table(filter=e).num_rows)    # 1
    ds.dataset("with_stats.parquet").to_table(filter=e)                  # ArrowInvalid

    The same happens for an int64 column cast to float64 with values above 2^53. With the default safe cast all three raise, so this only concerns safe=False. It may be worth applying the same rule as for float narrowing here (strip integer → floating-point only when every value of the source type is exactly representable), either in this PR or separately.

    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