Expose the ranks kernels, and make sort_intervals' groups keyword-only - #37
Merged
Conversation
`natural_rank`, `lexical_rank` and `fold_ranks` bind the new `ruranges_core::ranks` module. Together with `sort_intervals` they are the whole of a multi-key interval sort: code each key column to an ascending integer, fold all but the innermost two into a group id, one kernel call, one gather. The pieces that stay on the caller's side -- resolving column names, coding its own numeric dtypes, and the final take -- are the ones that genuinely depend on whether the frame is pandas or polars. The ranking functions take the *distinct* values of a key column, not the rows, so their cost is per distinct value: 25 chromosome names are free however many rows there are. `PyBackedStr` borrows each Python string's UTF-8 buffer rather than copying it, and the sort runs with the GIL released. Handing 10 million distinct values across the boundary costs 0.16 s worst case, against 24.9 s to natural-sort them in Python. `sort_intervals` now takes `groups` keyword-only. All three of `starts`, `ends` and `groups` are same-length integer arrays, so a transposition is undetectable at runtime and produces a plausible-looking wrong answer rather than an error -- `pyranges1.RangeFrame.sort_ranges` shipped for two releases sorting by `End` before `Start` for exactly this reason, putting 97,964 of 100,000 rows in the wrong place on a realistic frame. The same mistake is now a TypeError at the first call. This is a breaking change for positional callers, hence 0.1.7. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Cargo.lock is untracked, matching this repo's existing convention.
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
natural_rank,lexical_rankandfold_ranksfromruranges-core::ranks(0.1.12). Together withsort_intervalsthese are the whole of a multi-key interval sort: code each key column to an ascending integer, fold all but the innermost two into a group id, one kernel call, one gather. What stays on the caller's side is what genuinely depends on whether the frame is pandas or polars -- column resolution, numeric dtype coding, the final take.PyBackedStrborrows each Python string's UTF-8 buffer rather than copying it, and the sort runs with the GIL released. Handing 10 million distinct values across the boundary costs 0.16 s worst case (legacyobject-dtype pandas columns; zero for pandas 3's arrow-backedstr), against 24.9 s to natural-sort them in Python.sort_intervalstakesgroupskeyword-only now. All three ofstarts,ends,groupsare same-length integer arrays, so a transposition is undetectable at runtime and produces a plausible-looking wrong answer rather than an error --pyranges1.RangeFrame.sort_rangesshipped for two releases sorting byEndbeforeStartfor exactly this reason, putting 97,964 of 100,000 rows in the wrong place on a realistic frame. The same mistake is now aTypeErrorat the first call.sort_intervals+natural_rank/lexical_rank/fold_rankscomposed for a multi-key sort) and a cheat-sheet row for the new functions.0.1.6 -> 0.1.7.Test plan
maturin develop --release, then confirmed the bindings agree with Python'snatsort8.4.0 on 18,507 randomized realistic stringssort_intervals(starts, ends, groups)(old positional call) now raisesTypeErrorruranges-core0.1.12 (no path patch)Generated with Claude Code