Skip to content

[Data] Use Polars in hash_partitions and use native pac.sort_indices in _group_indices - #66269

Merged
goutamvenkat-anyscale merged 8 commits into
masterfrom
youcheng/data-polars-numba-hash-partition
Sep 23, 2026
Merged

goutamvenkat-anyscale merged 8 commits into
masterfrom
youcheng/data-polars-numba-hash-partition

Conversation

@owenowenisme

@owenowenisme owenowenisme commented Sep 18, 2026 •

Copy link
Copy Markdown
Member

Description

As title this pr does 2 things:

  1. Use Polars in hash_partitions : polars is proven to be more efficient than pandas so we leverage polars as the fast path for hash_partition
    Mirco benchmarks:
Key type Rows pandas path (ms) polars path (ms) Speedup
int64 key 1,000,000 5.9 3.8 1.5x
float64 key 1,000,000 5.6 3.7 1.5x
string key (100k unique) 1,000,000 58.3 8.0 7.3x
composite key (int64 + string) 1,000,000 61.9 9.5 6.5x
dictionary-encoded string key¹ 1,000,000 41.6 11.8 3.5x
struct key² 200,000 94.1 0.9 110x
list key² 200,000 69.6 0.9 74x
  1. Use pac.sort_indices in _group_indices to replace np.argsort

Related issues

Link related issues: "Fixes #1234", "Closes #1234", or "Related to #1234".

Additional information

Optional: Add implementation details, API changes, usage examples, screenshots, etc.

@owenowenisme owenowenisme added data Ray Data-related issues go add ONLY when ready to merge, run all tests labels Sep 18, 2026
@owenowenisme
owenowenisme marked this pull request as ready for review September 18, 2026 06:15
@owenowenisme
owenowenisme requested a review from a team as a code owner September 18, 2026 06:15

@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 introduces a vectorized hash partitioning implementation using Polars, along with optimized row index grouping using a Numba JIT-compiled kernel (with an Arrow-based fallback). Driver-side warnings are added to alert users if Numba is missing. Unit tests are also included to ensure consistency and correctness. The review feedback suggests further optimizations: performing modulo and cast operations directly within Polars before converting to NumPy, simplifying the exclusive prefix sum calculation in the Numba kernel to a single pass, and specifying a contiguous array layout (int64[::1]) in the Numba signature to enable better vectorization.

@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.

Stale Bugbot comment from a previous run.

@owenowenisme owenowenisme changed the title [Data] Use Polars in hash_partitions and Numba for _group_indices [Data] Use Polars in hash_partitions and use native pac.sort_indices in _group_indices Sep 18, 2026
Signed-off-by: You-Cheng Lin <[email protected]>
Signed-off-by: You-Cheng Lin <[email protected]>
Signed-off-by: You-Cheng Lin <[email protected]>
Signed-off-by: You-Cheng Lin <[email protected]>
Signed-off-by: You-Cheng Lin <[email protected]>
Signed-off-by: You-Cheng Lin <[email protected]>
@owenowenisme
owenowenisme force-pushed the youcheng/data-polars-numba-hash-partition branch from 01f1bc6 to 0e8089a Compare September 23, 2026 03:52

@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 using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 0e8089a. Configure here.

@goutamvenkat-anyscale
goutamvenkat-anyscale enabled auto-merge (squash) September 23, 2026 21:03
@goutamvenkat-anyscale
goutamvenkat-anyscale merged commit 1226345 into master Sep 23, 2026
7 checks passed
@goutamvenkat-anyscale
goutamvenkat-anyscale deleted the youcheng/data-polars-numba-hash-partition branch September 23, 2026 22:02
nadongjun pushed a commit to nadongjun/ray that referenced this pull request Oct 6, 2026
…es` in `_group_indices` (ray-project#66269)

## Description
As title this pr does 2 things:
1. Use Polars in `hash_partitions` : polars is proven to be more
efficient than pandas so we leverage polars as the fast path for
`hash_partition`
Mirco benchmarks:

| Key type | Rows | pandas path (ms) | polars path (ms) | Speedup |
|---|---|---|---|---|
| int64 key | 1,000,000 | 5.9 | 3.8 | 1.5x |
| float64 key | 1,000,000 | 5.6 | 3.7 | 1.5x |
| string key (100k unique) | 1,000,000 | 58.3 | 8.0 | 7.3x |
| composite key (int64 + string) | 1,000,000 | 61.9 | 9.5 | 6.5x |
| dictionary-encoded string key¹ | 1,000,000 | 41.6 | 11.8 | 3.5x |
| struct key² | 200,000 | 94.1 | 0.9 | 110x |
| list key² | 200,000 | 69.6 | 0.9 | 74x |

2. Use `pac.sort_indices` in `_group_indices` to replace `np.argsort`

## Related issues
> Link related issues: "Fixes ray-project#1234", "Closes ray-project#1234", or "Related to
ray-project#1234".

## Additional information
> Optional: Add implementation details, API changes, usage examples,
screenshots, etc.

---------

Signed-off-by: You-Cheng Lin <[email protected]>
Co-authored-by: Goutam <[email protected]>
sai-miduthuri added a commit that referenced this pull request Nov 13, 2026
…conciliation and struct alignment (#66521) (#68024)

## Description

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

**Bottom of a 3-PR stack on `releases/2.59.2`:** this PR, then #68025
(the #67579 pick), then #68026 (the #67806 pick). Merge the stack with
`gh stack merge 68026`, which lands all three in order.

This is a prerequisite for the #67579 pick. #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. The same pick for 2.60.1
is #67995.

## 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.

The two files differ from master at 6fa1a28 only by master commits
this change doesn't use: #66197, #66269 and #64961 in
`transform_pyarrow.py`, and #66269 in `unit/test_transform_pyarrow.py`.

Not a duplicate: this branch doesn't contain #66521, and no PR against
`releases/2.59.2` references it. #67995 picks the same commit onto
`releases/2.60.1`.

### 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` | 123 passed |

The #67579 pick's description, #68025, has the results with the stack
applied.

I ran these against the Ray 2.59.1 release wheel, with this branch's
`python/ray/data` and `python/ray/tests` copied over it. The wheel was
built from `releases/2.59.1` at 7bdaad7, and its Python sources match
`releases/2.59.2` except for `_version.py`. The environment was Python
3.12 with pandas 2.3.3, pyarrow 23.0.1 and numpy 2.1.3, the versions CI
pins on this branch. 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

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.

Ray fails to serialize self-reference objects

2 participants