Repository navigation
[Data] Keep nullable integer hash partitions consistent across blocks - #66703
Conversation
Signed-off-by: yifan.xie <[email protected]>
There was a problem hiding this comment.
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.
| 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(), | ||
| } |
There was a problem hiding this comment.
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_dtypesSigned-off-by: yifan.xie <[email protected]>
793946c to
822af19
Compare
owenowenisme
left a comment
There was a problem hiding this comment.
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]>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
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) |
There was a problem hiding this comment.
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)
Reviewed by Cursor Bugbot for commit 2a89674. Configure here.
…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]>


Description
In the pandas fallback of
_hash_partition, Arrow integer columns with nulls can be converted tofloat64during 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
ArrowDtypefor other columns. Add a regression test coveringint32, largeint64, and largeuint64keys across blocks with and without nulls.Related issues
Additional information