Skip to content

feat(vector): RemapList type and one gather kernel (#33181 PR 7) - #33289

Merged
fengttt merged 1 commit into
matrixorigin:mainfrom
fengttt:sels-bitmap-33181-pr7
Nov 30, 2026
Merged

fengttt merged 1 commit into
matrixorigin:mainfrom
fengttt:sels-bitmap-33181-pr7

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 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 of int64 source rows with Init, Free, Reset, Identity, Append, Repeat, Len and Rows. It allocates through mpool.MakeSlice/FreeSlice, without unsafe. BuildBuckets comes with its first user in PR 8, InvertInto in PR 11.
  • 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 until PR 8 moves them.
  • NULLs are gathered one output word (64 rows) at a time in a register and published with the new Gobra-verified Bitmap.OrWord, instead of a Contains and an Add per row.
  • apply moves to int64 rows.

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 with int32 rows and 7.1 µs with int64 rows (2.17 / 2.21 ms for 1M rows); sorting a permutation takes 456 / 457 µs (109 ms for 1M rows either way). int64 has no 2^31-row cap on whole-input sorts and is what pkg/sort, Union, every Shuffle caller and the top heaps already use.

Rebase onto main

  • Upstream moved ShrinkByRows onto 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 made ShrinkByRows reuse the gather's value loop is dropped.
  • TestBatchUnionRows uses batch.NewWithSize, since upstream removed batch.New with 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 beats Shuffle for every type, which supports replacing Shuffle in PR 9. Tables are in the design doc.

Tests

  • New tests for remaplist, the gather (against UnionOne for every payload width, from windows, const sources, into itself, empty and pool mismatch), Batch.Union and OrWord; -race clean.
  • pkg/container/..., pkg/common/bitmap, pkg/common/remaplist, pkg/sql/colexec/..., pkg/sql/compile/... and TAE mergesort pass.
  • Gobra: select:OrWord verifies against main's bitmap (0 errors).

🤖 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-pr7 branch from 222e044 to 6873180 Compare November 26, 2026 06:26
@fengttt fengttt changed the title feat(vector): RemapList type and one Gather kernel (#33181 PR 7) feat(vector): RemapList type and one gather kernel (#33181 PR 7) 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
fengttt force-pushed the sels-bitmap-33181-pr7 branch from 6873180 to 88dd903 Compare November 29, 2026 22:39
@fengttt
fengttt marked this pull request as ready for review November 29, 2026 22:39
@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 29, 2026 •

Copy link
Copy Markdown
Contributor

This pull request is merged.

@matrix-meow matrix-meow added size/XL Denotes a PR that changes [1000, 1999] lines and removed size/XXL Denotes a PR that changes 2000+ lines labels Nov 29, 2026
@XuPeng-SH

XuPeng-SH commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · superseded

Commit: 88dd903c726cf8ba1421282d0cd478c5c649ff9c

"interrupted"

@fengttt
fengttt merged commit 0269f1b into matrixorigin:main Nov 30, 2026
51 of 54 checks passed
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

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/enhancement size/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants