Repository navigation
feat(remaplist): one counting sort for bucket splits, int64 row lists (#33181 PR 8) - #33290
Merged
Merged
Conversation
fengttt
force-pushed
the
sels-bitmap-33181-pr8
branch
from
November 26, 2026 06:26
695fca5 to
fa70c9b
Compare
This was referenced 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
force-pushed
the
sels-bitmap-33181-pr8
branch
from
November 30, 2026 00:24
fa70c9b to
3012de1
Compare
fengttt
marked this pull request as ready for review
November 30, 2026 00:24
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
Contributor
|
This pull request is merged. |
Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.
What type of PR is this?
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 remainingint32row lists becomeint64. 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 inoffs.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.SplitBucketscounts 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.classifyRowsand hash-build spill turn hashes into bucket ids in place and call it;computeBucketIndexalready wrote;enqueueOwnedKey) takeshash % bucketsonce per row instead of twice;List.BuildBucketson its 1-based group ids withgroupCount+1buckets. 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.UnionInt32is 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
int64keys, 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.
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
varchargather 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 everyUnioncaller and is left for a separate PR.Benchmarks
BenchmarkSplitBuckets, 8192 rows with random ids, one core, minimum of 6 interleaved runs:Earlier measurements against PR 7, with the one-lane split:
Pinned to one core, minimum of five or more runs, PR 7 → PR 8:
BenchmarkSpillScatterAccounting(whole scatter)BenchmarkHashPartition(whole operator, 64K rows, 1 / 64 / 1024 groups)The whole-operator numbers are within 2.5%: the split is a small part of a spill scatter or a partition.
Tests
remaplisttests forSplitBucketsandBuildBuckets(random ids, empty buckets, stale buffers, errors, reuse,Free); 97% coverage.remaplist,vector,batch,aggexec,group,hashbuild,partition,shuffleandspillutilpass with-race; all ofpkg/sql/colexec/...andpkg/sql/compile/...pass.go vet ./pkg/...and lint are clean.SplitBucketstests 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.Rebase onto main
BatchSet.UnionwithAppendSels. Its row lists (andappendRows,copyRows, the shuffle pool's appends and the tests) move toint64here.row_metadata_trailer_test.gowas deleted upstream, so its one-line change is dropped.remaplistallocates throughmpool.MakeSlice/FreeSlice, withoutunsafe(upstream'scheckunsafelint).🤖 Generated with Claude Code