Skip to content

feat(remaplist): one counting sort for bucket splits, int64 row lists (#33181 PR 8) - #33290

Merged
fengttt merged 3 commits into
matrixorigin:mainfrom
fengttt:sels-bitmap-33181-pr8
Nov 30, 2026
Merged

fengttt merged 3 commits into
matrixorigin:mainfrom
fengttt:sels-bitmap-33181-pr8

Conversation

@fengttt

@fengttt fengttt commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #33181

What this PR does / why we need it:

PR 8 of the row-selection plan in docs/design/20261124-row-selection-bitmap-remaplist.md: one counting sort for bucket splits, and the remaining int32 row lists become int64. It is one commit on current main, after PR 7 (#33289) merged.

  • remaplist.SplitBuckets(ids, rows, offs) is the counting sort: ids[i] is the bucket of row i, and the rows come out grouped by bucket, ascending within each bucket, with the boundaries in offs. List.BuildBuckets(ids, buckets) / Bucket(b) run it on List-owned storage. The function form lets spill, shuffle and group keep charging their buffers to their allocation accounts.
  • Four lanes for few buckets. With at most 32 buckets (and at least 256 rows), SplitBuckets counts and places the rows in four contiguous lanes, each with its own counters and cursors on the stack. With few buckets, consecutive rows mostly hit one counter, and every increment waited for the store before it. Lane j's rows of a bucket follow lane j−1's, so buckets stay in ascending row order.
  • It replaces five copies of the split:
    • spill classifyRows and hash-build spill turn hashes into bucket ids in place and call it;
    • group spill calls it on the bucket numbers computeBucketIndex already wrote;
    • the complete-key shuffle (enqueueOwnedKey) takes hash % buckets once per row instead of twice;
    • hash window partition calls List.BuildBuckets on its 1-based group ids with groupCount+1 buckets. That also validates the ids (out of range is rejected, bucket 0 must be empty), so its separate validation pass is gone.
  • int32 → int64: shuffle buckets (kernels, ShufflePool.tryWrite, BatchSet.AppendSels), spill row ids, and group spill rows through the aggregate spill writers (SaveSpillIntermediateRows, MarshalSelectedRowsTo, WriteSelected*Rows). Group spill's recovery floor reserves 8 bytes per row id. UnionInt32 is removed, and the gather kernel takes only []int64.

This deviates from the plan in one place: the typed shuffle kernels keep their fused per-bucket append. A prototype that wrote bucket ids first and then sorted was 30–60% slower (8192 random int64 keys, 4–256 buckets: 19.2–19.7 µs appending vs 25.5–32.0 µs), because the appends overlap the hash computation. A counting sort pays only where the ids exist before the split.

Why a list per split, not a bitmap per bucket

Each bucket's rows are an ascending subset of the batch, so a bitmap per bucket was measured as the alternative: 8192 rows (1 in 10 NULL) split by hash and each bucket gathered into its own output, one core, minimum of 5 runs.

case list, one-lane split list, four-lane split bitmap per bucket
int64, 1 column, 2 buckets 42.6 µs ≈31 µs 25.5 µs
int64, 1 column, 16 buckets 34.2 µs ≈31 µs 33.2 µs
int64, 4 columns, 2 buckets 65.8 µs ≈54 µs 51.4 µs
int64, 4 columns, 64 buckets 63.2 µs 63.2 µs 169.9 µs
int64, 4 columns, 256 buckets 78.6 µs 78.6 µs 432.6 µs

The four-lane column sums the measured phases (hash to id 15.8 µs, split, gather); 64 and 256 buckets keep the one-lane split. Filling the bitmaps costs about the same as the four-lane split, and for fixed-width columns the two gathers cost the same at few buckets. With more buckets, every bucket's gather scans a batch-length bitmap, so its cost grows with buckets × batch length. Window partition splits by group, often thousands of groups, and the shuffle pool cuts each bucket into batch-sized pieces, which for a list is a slice. So the split stays a list.

One gap the measurement found is not about the form: a varchar gather by list is slower than by mask (82.9 vs 50.0 µs for 4 columns), because the mask kernel has an inline-only fast path the list kernel lacks. That touches every Union caller and is left for a separate PR.

Benchmarks

BenchmarkSplitBuckets, 8192 rows with random ids, one core, minimum of 6 interleaved runs:

buckets 2 4 8 16 32 33 64 256
one lane 13.0 µs 14.7 µs 11.6 µs 9.8 µs 9.6 µs 10.1 µs 8.6 µs 8.2 µs
this PR 7.5 µs 7.4 µs 7.5 µs 7.2 µs 7.6 µs 10.2 µs 8.7 µs 8.5 µs

Earlier measurements against PR 7, with the one-lane split:

Pinned to one core, minimum of five or more runs, PR 7 → PR 8:

step old new
split 4096 hashes into 32 buckets 8.1 µs 5.6 µs
split 4096 hashes into 4 buckets 8.4 µs 8.6 µs
complete-key shuffle split, 8192 rows, 4 / 24 / 64 buckets 37.9 / 40.0 / 38.9 µs 32.0 / 26.3 / 26.1 µs
BenchmarkSpillScatterAccounting (whole scatter) 231–233 µs 230–234 µs
BenchmarkHashPartition (whole operator, 64K rows, 1 / 64 / 1024 groups) 1.78 / 2.06 / 2.09 ms 1.76 / 2.07 / 2.14 ms

The whole-operator numbers are within 2.5%: the split is a small part of a spill scatter or a partition.

Tests

  • New remaplist tests for SplitBuckets and BuildBuckets (random ids, empty buckets, stale buffers, errors, reuse, Free); 97% coverage.
  • remaplist, vector, batch, aggexec, group, hashbuild, partition, shuffle and spillutil pass with -race; all of pkg/sql/colexec/... and pkg/sql/compile/... pass. go vet ./pkg/... and lint are clean.
  • SplitBuckets tests cover both paths: 1–33 and 1000 buckets, row counts around the four-lane minimum and with a lane tail, and an out-of-range id in each lane and in the tail.
  • BVT not run.

Rebase onto main

🤖 Generated with Claude Code

@matrix-meow matrix-meow added the size/XXL Denotes a PR that changes 2000+ lines label Nov 26, 2026
@fengttt
fengttt force-pushed the sels-bitmap-33181-pr8 branch from 695fca5 to fa70c9b Compare November 26, 2026 06:26
@fengttt fengttt changed the title feat(remaplist): one counting sort for bucket splits (#33181 PR 8) feat(remaplist): one counting sort for bucket splits, int64 row lists (#33181 PR 8) Nov 26, 2026
…matrixorigin#33181 PR 8)

remaplist.SplitBuckets(ids, rows, offs) is the counting sort: ids[i] is the
bucket of row i, and rows come out grouped by bucket, ascending within each.
List.BuildBuckets/Bucket run it on List-owned storage; the function form lets
spill, shuffle and group keep charging their buffers to their allocation
accounts.

With at most 32 buckets it counts and places rows in four contiguous lanes,
each with its own stack counters and cursors. With few buckets consecutive
rows hit one counter, and each increment waited for the store before it.
8192 rows: 2 buckets 13.0 -> 7.5 us, 4: 14.7 -> 7.4, 16: 9.8 -> 7.2,
32: 9.6 -> 7.6. A bitmap per bucket was measured instead and rejected: it
only matches the list at 2-16 buckets, and its gather costs 3-6x more at 64+.

It replaces five copies of the split:
- spill classifyRows and hash-build spill turn hashes into bucket ids in
  place and call it;
- group spill calls it on the bucket numbers computeBucketIndex already
  wrote;
- the complete-key shuffle takes hash % buckets once per row instead of
  twice (split 15-35% faster);
- hash window partition uses List.BuildBuckets on its 1-based group ids,
  which also validates them, so its separate validation pass is gone.

The typed shuffle kernels keep their fused per-bucket append: a prototype
writing ids first and sorting was 30-60% slower, because the appends overlap
the hash computation.

The remaining int32 row lists become int64: shuffle buckets (kernels,
ShufflePool, BatchSet.AppendSels), spill row ids, and group spill rows through the
aggregate spill writers and MarshalSelectedRowsTo. remaplist allocates through
mpool's typed slices, without unsafe. Group spill's recovery
floor reserves 8 bytes per row id. UnionInt32 is removed and the gather
kernel takes only []int64. Spill scatter and partition operator benchmarks
are within 2.5%.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fengttt
fengttt force-pushed the sels-bitmap-33181-pr8 branch from fa70c9b to 3012de1 Compare November 30, 2026 00:24
@fengttt
fengttt marked this pull request as ready for review November 30, 2026 00:24
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@mergify

mergify Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

This pull request is merged.

@fengttt
fengttt merged commit c255da6 into matrixorigin:main Nov 30, 2026
3 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/enhancement size/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants