zhaohaisun opened a new issue, #55886:
URL: https://github.com/apache/arrow/issues/55886
### 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.
```python
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:
```python
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
--
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]