Skip to content

Expose the ranks kernels, and make sort_intervals' groups keyword-only - #37

Merged
marco-mariotti merged 2 commits into
masterfrom
shared-string-ranker
Jul 31, 2026
Merged

Expose the ranks kernels, and make sort_intervals' groups keyword-only#37
marco-mariotti merged 2 commits into
masterfrom
shared-string-ranker

Conversation

@marco-mariotti

Copy link
Copy Markdown
Member

Summary

  • Binds natural_rank, lexical_rank and fold_ranks from ruranges-core::ranks (0.1.12). Together with sort_intervals these 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.
  • 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 (legacy object-dtype pandas columns; zero for pandas 3's arrow-backed str), against 24.9 s to natural-sort them in Python.
  • Breaking: sort_intervals takes groups keyword-only now. All three of starts, ends, 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.
  • README gains a worked example (sort_intervals + natural_rank/lexical_rank/fold_ranks composed 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's natsort 8.4.0 on 18,507 randomized realistic strings
  • Confirmed sort_intervals(starts, ends, groups) (old positional call) now raises TypeError
  • Rebuilt and smoke-tested against the published ruranges-core 0.1.12 (no path patch)
  • README example runs as written

Generated with Claude Code

marco-mariotti and others added 2 commits July 31, 2026 15:33
`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.
@marco-mariotti
marco-mariotti merged commit 599287f into master Jul 31, 2026
14 of 15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant