Repository navigation
feat(vector): RemapList type and one gather kernel (#33181 PR 7) - #33289
Merged
Merged
Conversation
1 of 7 tasks
fengttt
force-pushed
the
sels-bitmap-33181-pr7
branch
from
November 26, 2026 06:26
222e044 to
6873180
Compare
This was referenced Nov 26, 2026
…PR 7) pkg/common/remaplist adds List, an mpool-owned []int64 of source rows with Init/Free/Reset/Identity/Append/Repeat/Len/Rows. Vector.Union and Batch.Union are the one row-list gather: rows in any order, repeats allowed. UnionInt32 is the same kernel for the remaining int32 lists. The NULL bitmap is built 64 output rows per word in a register and published with the new Gobra-verified Bitmap.OrWord, instead of a Contains and an Add per row. remaplist allocates through mpool's typed slices, without unsafe. apply moves to int64 rows. Rows are int64: the index width does not change gather or sort speed (measured), and int64 has no 2^31-row cap on whole-input sorts. BenchmarkUnionRows (8192 rows, 1 in 10 NULL): int64 3.4x, int32 3.7x, decimal128 2.9x, varchar 1.9x faster; no-NULL int64 unchanged. BenchmarkPermute: gather+swap matches or beats Shuffle for every type. Co-Authored-By: Claude Opus 5.5 <[email protected]>
fengttt
force-pushed
the
sels-bitmap-33181-pr7
branch
from
November 29, 2026 22:39
6873180 to
88dd903
Compare
fengttt
marked this pull request as ready for review
November 29, 2026 22:39
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. |
Contributor
|
mo-bot review · Commit: "interrupted" |
fengttt
added a commit
that referenced
this pull request
Nov 30, 2026
…#33181 PR 8) (#33290) ## What type of PR is this? - [ ] API-change - [ ] BUG - [x] 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 - Upstream's BatchSet rewrite (#33265) replaced `BatchSet.Union` with `AppendSels`. Its row lists (and `appendRows`, `copyRows`, the shuffle pool's appends and the tests) move to `int64` here. - `row_metadata_trailer_test.go` was deleted upstream, so its one-line change is dropped. - `remaplist` allocates through `mpool.MakeSlice`/`FreeSlice`, without `unsafe` (upstream's `checkunsafe` lint). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <[email protected]>
This branch was previously deployed
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 7 of the row-selection plan in
docs/design/20261124-row-selection-bitmap-remaplist.md: the RemapList type and one gather kernel. It is one commit on current main, after PR 6 + 6b (#33272) merged.pkg/common/remaplist.List: an mpool-owned list ofint64source rows withInit,Free,Reset,Identity,Append,Repeat,LenandRows. It allocates throughmpool.MakeSlice/FreeSlice, withoutunsafe.BuildBucketscomes with its first user in PR 8,InvertIntoin PR 11.Vector.UnionandBatch.Unionare the one row-list gather: rows in any order, repeats allowed.UnionInt32is the same kernel for the remainingint32lists until PR 8 moves them.Bitmap.OrWord, instead of aContainsand anAddper row.applymoves toint64rows.Rows are
int64. The index width does not change speed, measured pinned to one core: gathering 8192 values in random order takes 7.5 µs withint32rows and 7.1 µs withint64rows (2.17 / 2.21 ms for 1M rows); sorting a permutation takes 456 / 457 µs (109 ms for 1M rows either way).int64has no 2^31-row cap on whole-input sorts and is whatpkg/sort,Union, everyShufflecaller and the top heaps already use.Rebase onto main
ShrinkByRowsonto the vector type-ops table (ops.shrink), which already replaced its per-type switch. This PR keeps upstream's version, so the earlier hunk that madeShrinkByRowsreuse the gather's value loop is dropped.TestBatchUnionRowsusesbatch.NewWithSize, since upstream removedbatch.Newwith the batch attribute names.Benchmarks
BenchmarkUnionRows, 8192 rows, one in ten NULL, before → after (interleaved, pinned to one core): int64 3.4×, int32 3.7×, decimal128 2.9×, varchar 1.9× faster; int64 without NULLs unchanged.BenchmarkPermute: a gather into scratch plus swap matches or beatsShufflefor every type, which supports replacingShufflein PR 9. Tables are in the design doc.Tests
remaplist, the gather (againstUnionOnefor every payload width, from windows, const sources, into itself, empty and pool mismatch),Batch.UnionandOrWord;-raceclean.pkg/container/...,pkg/common/bitmap,pkg/common/remaplist,pkg/sql/colexec/...,pkg/sql/compile/...and TAEmergesortpass.select:OrWordverifies against main's bitmap (0 errors).🤖 Generated with Claude Code