Skip to content

[Data] Keep nullable integer hash partitions consistent across blocks - #66703

Merged
richardliaw merged 6 commits into
ray-project:masterfrom
laysfire:hash_partitions_null_issue
Oct 14, 2026
Merged

richardliaw merged 6 commits into
ray-project:masterfrom
laysfire:hash_partitions_null_issue

Conversation

@laysfire

@laysfire laysfire commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

Description

In the pandas fallback of _hash_partition, Arrow integer columns with nulls can be converted to float64 during hashing. The same integer key can then hash differently in blocks with and without nulls. Large integers can also lose precision.

Map Arrow integer columns to pandas nullable integer dtypes before hashing, while retaining ArrowDtype for other columns. Add a regression test covering int32, large int64, and large uint64 keys across blocks with and without nulls.

Related issues

Additional information

@laysfire
laysfire requested a review from a team as a code owner October 4, 2026 14:28

@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 ensures consistent hashing across blocks for nullable integer columns by mapping PyArrow integer types to pandas' nullable integer dtypes instead of using pd.ArrowDtype during the to_pandas conversion. A unit test was also added to verify this behavior. The review feedback suggests caching the nullable_integer_dtypes dictionary as a function attribute to avoid the overhead of recreating it on every call to _hash_partition.

Comment on lines +252 to +261
nullable_integer_dtypes = {
pyarrow.int8(): pd.Int8Dtype(),
pyarrow.int16(): pd.Int16Dtype(),
pyarrow.int32(): pd.Int32Dtype(),
pyarrow.int64(): pd.Int64Dtype(),
pyarrow.uint8(): pd.UInt8Dtype(),
pyarrow.uint16(): pd.UInt16Dtype(),
pyarrow.uint32(): pd.UInt32Dtype(),
pyarrow.uint64(): pd.UInt64Dtype(),
}

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.

medium

Recreating the nullable_integer_dtypes dictionary and instantiating all the PyArrow and Pandas dtype objects on every call to _hash_partition introduces unnecessary overhead, especially when processing a large number of blocks. Caching this dictionary as a function attribute avoids this overhead.

        if not hasattr(_hash_partition, "_nullable_integer_dtypes"):
            _hash_partition._nullable_integer_dtypes = {
                pyarrow.int8(): pd.Int8Dtype(),
                pyarrow.int16(): pd.Int16Dtype(),
                pyarrow.int32(): pd.Int32Dtype(),
                pyarrow.int64(): pd.Int64Dtype(),
                pyarrow.uint8(): pd.UInt8Dtype(),
                pyarrow.uint16(): pd.UInt16Dtype(),
                pyarrow.uint32(): pd.UInt32Dtype(),
                pyarrow.uint64(): pd.UInt64Dtype(),
            }
        nullable_integer_dtypes = _hash_partition._nullable_integer_dtypes

@ray-gardener ray-gardener Bot added data Ray Data-related issues community-contribution Contributed by the community labels Oct 4, 2026
Signed-off-by: yifan.xie <[email protected]>
@laysfire
laysfire force-pushed the hash_partitions_null_issue branch from 793946c to 822af19 Compare October 4, 2026 15:01
@owenowenisme owenowenisme self-assigned this Oct 8, 2026

@owenowenisme owenowenisme left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I guess bool type could have the same problem, would you try add that mapping in this PR as well?

Signed-off-by: yifan.xie <[email protected]>
Signed-off-by: yifan.xie <[email protected]>
@owenowenisme
owenowenisme enabled auto-merge (squash) October 12, 2026 11:16
@github-actions github-actions Bot added the go add ONLY when ready to merge, run all tests label Oct 12, 2026

@owenowenisme owenowenisme left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thank you!

@github-actions
github-actions Bot disabled auto-merge October 13, 2026 09:17

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 2a89674. Configure here.

nullable_dtypes = _get_nullable_dtypes()

def types_mapper(dtype):
return nullable_dtypes.get(dtype) or pd.ArrowDtype(dtype)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Dictionary integers hash inconsistently

High Severity

types_mapper only maps exact integer and boolean types onto pandas nullable dtypes. Dictionary-encoded integer columns still use ArrowDtype, so nulls can still be hashed as floats and large values can lose precision. Without Polars, _hash_partition_vectorized returns before dictionary decoding, so the same key can land in different partitions than a plain integer block.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 2a89674. Configure here.

@richardliaw
richardliaw merged commit fde4750 into ray-project:master Oct 14, 2026
6 checks passed
sai-miduthuri added a commit that referenced this pull request Nov 13, 2026
…conciliation and struct alignment (#66521) (#67995)

## Description

Cherry-pick of #66521 (master commit
6fa1a28) onto `releases/2.60.1`.

This is a prerequisite for the #67579 pick, #67999. The two sit in a
3-PR stack on `releases/2.60.1` under #68019 (the #67806 pick); merge it
with `gh stack merge 68019`. #66521 fixes null-type handling when Ray
Data reconciles Arrow schemas across blocks:

- A nested field that is all-null in one block and a struct in another
crashes `_backfill_missing_fields` with `TypeError:
'pyarrow.lib.DataType' object is not iterable`.
- `_reconcile_field` reconciles `[null, list<null>, list<int64>]` to
`null` instead of `list<int64>`.

Without it, two of the tests #67579 adds fail on this branch. Both are
the case where one block's tensor column is fixed-shape, another's is
null, and a third's has a different shape. They fail with
`ArrowTypeError: Casting from
'extension<ray.data.arrow_variable_shaped_tensor<...>>' to different
extension type 'extension<ray.data.arrow_tensor_v2<...>>' not
permitted`.

## Related issues

Related to #66521. Needed by the #67579 pick.

## Additional information

### Cherry-pick details

Applied cleanly with `git cherry-pick -x`. It has the same 2 files,
+233/-30, and the same `git patch-id --stable` as the master commit.

After the pick, `unit/test_transform_pyarrow.py` is identical to master
at 6fa1a28. `transform_pyarrow.py` differs only by #66197's rename of
`PreparedChunkedTensorTake` to `PreparedTensorTake`. #66197 is the only
master commit that touched these files between the branch cut and
#66521, and this change doesn't depend on it.

To find what #67579 needed, I applied each master-only commit that
touched `transform_pyarrow.py` or the tensor extensions on top of the
#67579 pick: #66521, #65961, #66508, #66197, #66703 and #66522. #66521
alone makes the two failing tests pass, and none of the others changes
the result.

Not a duplicate: this branch doesn't contain #66521, and no PR on any
branch references it.

### Testing

| Command | Result |
|---|---|
| `pre-commit run --files <the 2 changed files>` | Passed |
| `pytest python/ray/data/tests/unit/test_transform_pyarrow.py
python/ray/data/tests/test_transform_pyarrow.py
python/ray/data/tests/test_arrow_block.py` | 145 passed, 3 skipped |

The #67579 pick's description has the results with both picks applied.

I ran these against the Ray 2.60.0 release wheel, with this branch's
`python/ray/data` and `python/ray/tests` copied over it. The wheel was
built from `releases/2.60.0` at b7f4259, and its Python sources match
`releases/2.60.1` except for `_version.py`. The environment was Python
3.12 with pandas 2.3.3 and pyarrow 23.0.1, the versions CI pins.
Building Ray from source for `bazel test` wasn't practical on my
machine.

AI assistance was used to prepare this cherry-pick.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

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

Labels

community-contribution Contributed by the community 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.

3 participants