Skip to content

GH-46183: [C++][Compute] Make is_in and index_in treat -0.0 and +0.0 as equal - #54915

Closed
sankalpsthakur wants to merge 1 commit into
apache:mainfrom
sankalpsthakur:GH-46183-is-in-signed-zero
Closed

sankalpsthakur wants to merge 1 commit into
apache:mainfrom
sankalpsthakur:GH-46183-is-in-signed-zero

Conversation

@sankalpsthakur

@sankalpsthakur sankalpsthakur commented Oct 21, 2026 •

Copy link
Copy Markdown

Rationale for this change

is_in and index_in look floating-point values up through their unsigned integer bit patterns, so -0.0 and +0.0 are different keys even though equal treats them as the same value:

>>> pc.equal(pa.array([-0.0]), pa.array([0.0]))
[true]
>>> pc.is_in(pa.array([-0.0]), value_set=pa.array([0.0]))
[false]

This silently drops rows from dataset scans. The Parquet format asks writers to put -0.0 in the min and +0.0 in the max statistic when the computed value is zero, so a column that only holds zeros gets min=-0.0, max=+0.0. EvaluateStatisticsAsExpression turns that into the guarantee x == -0.0, SimplifyWithGuarantee substitutes x by -0.0 and constant-folds x IN (0.0) through the is_in kernel, which says false, so the whole fragment is skipped:

pq.write_table(pa.table({"x": [0.0, 0.0]}), "zeros.parquet")
ds.dataset("zeros.parquet").to_table(filter=ds.field("x").isin([0.0]))   # empty, expected both rows

The reporter's analysis in the issue traced the empty result to the signed zero in the Parquet statistics.

What changes are included in this PR?

SetLookupState in scalar_set_lookup.cc folds the bit pattern of -0.0 into +0.0, both when the value set is built and when values are probed, if the value set is floating-point. Integers of the same width are not touched (their smallest value has the same bit pattern as -0.0).

I did not change ScalarHelper<float/double>::ComputeHash in hashing.h, which is where the issue discussion pointed. These kernels do not use it: InitStateVisitor maps every fixed-width numeric type to an unsigned integer state, so it is ScalarMemoTable<uint64_t> that hashes doubles here. And ScalarMemoTable is also the table behind unique, value_counts, dictionary_encode, DictionaryBuilder and the Parquet dictionary encoder, where merging -0.0 into 0.0 would silently change stored values (a dictionary-encoded Parquet column keeps the sign of zero across a round trip today; see also ARROW-10211). So this only makes set membership agree with equal. NaN handling, the other hash-based kernels and the Acero hash join / group_by (which still keep the two zeros apart) are unchanged.

Are these changes tested?

Yes: C++ tests for is_in and index_in on float and double (single, chunked and duplicated value sets, and integer guards) in scalar_set_lookup_test.cc, Expression.SimplifyIsInSignedZero for the dataset guarantee path, and two pytests (test_is_in_index_in_signed_zero, test_filter_isin_signed_zero). They fail without the change.

Are there any user-facing changes?

is_in and index_in on float and double now match -0.0 with +0.0 and the reverse, as equal does.

This PR contains a "Critical Fix". It fixes (b), a bug that caused incorrect data to be produced: dataset filters using isin on a float column silently returned no rows for fragments whose values are all zero.

Was AI used for this PR?

PR code and description written by:

  • Human
  • AI

Reviewed before submission by:

  • Human
  • AI
  • Not reviewed

AI disclosure: an AI coding tool wrote the fix, the tests and this description; the tests were run with and without the change before submitting.

… +0.0 as equal

is_in and index_in look floating-point values up through their unsigned
integer bit patterns, so -0.0 and +0.0 were different keys although they
compare equal. A Parquet column that only holds zeros has statistics
min=-0.0 and max=+0.0, which the dataset layer turns into the guarantee
`x == -0.0`. That folded `x IN (0.0)` to false and silently dropped every
row of the fragment.

Fold -0.0 into +0.0, for both the value set and the probed values, when
the value set is floating point.
@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #46183 has been automatically assigned in GitHub to PR creator.

@dmitry-chirkov-dremio dmitry-chirkov-dremio 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.

lgtm

@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #46183 has been automatically assigned in GitHub to PR creator.

richardliaw pushed a commit to ray-project/ray that referenced this pull request Nov 20, 2026
…and integer overflow (#68325)

## Why

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

```python
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. #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](https://claude.com/claude-code)

---------

Signed-off-by: Aarrya <[email protected]>
Co-authored-by: Claude Fable 5.2 <[email protected]>
@thisisnic

Copy link
Copy Markdown
Member

Closing this PR as it doesn't meet our guidelines on AI-generated contributions:

https://arrow.apache.org/docs/developers/overview.html#ai-generated-code

If you'd like to contribute this change, please review and test the code yourself, then open a new PR.

@thisisnic thisisnic closed this Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants