Skip to content

docs(design): row selection with bitmaps and RemapList - #33183

Merged
fengttt merged 1 commit into
matrixorigin:mainfrom
fengttt:design-row-selection-bitmap
Nov 24, 2026
Merged

fengttt merged 1 commit into
matrixorigin:mainfrom
fengttt:design-row-selection-bitmap

Conversation

@fengttt

@fengttt fengttt commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Issue #33181

What

Adds docs/design/20261124-row-selection-bitmap-remaplist.md, the design and implementation plan for #33181. The plan has two parts:

  • Bitmap as the only row selection. Every sels list that selects rows (ascending, unique) becomes a bitmap, following the existing batch-selection model: the producer owns the bitmap and lends it to consumers through the batch.
  • One RemapList type for reordering. Sort permutations, join gathers, hash buckets and inverse maps move into one type with one gather kernel.

The doc covers:

  • the four row-selection representations in use today, and the conversions between them on the hot paths;
  • a classification of the call sites;
  • the ownership rule: one owner, one allocation and one free per query;
  • the bitmap primitives to add, and the RemapList API;
  • an 11-PR plan, with a benchmark for each step;
  • risks and open questions.

This PR changes documentation only.

🤖 Generated with Claude Code

Design and implementation plan for replacing row-selection index lists
(sels) with bitmaps, and consolidating sort, gather and bucket remaps
into one RemapList type. Issue matrixorigin#33181.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fengttt
fengttt requested a review from XuPeng-SH as a code owner November 24, 2026 21:38
@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 →

@fengttt
fengttt merged commit c0cef5c into matrixorigin:main Nov 24, 2026
@matrix-meow matrix-meow added the size/XS Denotes a PR that changes [1, 9] lines label Nov 24, 2026
fengttt added a commit that referenced this pull request Nov 28, 2026
… PR 1) (#33213)

## What type of PR is this?

- [ ] API-change
- [ ] BUG
- [ ] Improvement
- [ ] Documentation
- [x] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue #33181

## What this PR does / why we need it:

PR 1 of the row-selection plan in
`docs/design/20261124-row-selection-bitmap-remaplist.md` (#33183). It
adds the bitmap primitives that later PRs use to replace `[]int64` sels
with bitmaps. No caller is converted yet.

**Stacked on #33144** (pool-owned bitmaps). Until #33144 merges, this
diff also contains #33144's commits. The commits of this PR are the last
three on the branch.

### Design doc update

- RemapList lives in its own package, `pkg/common/remaplist`, as
`remaplist.List` (decision 5). `InvertInto` writes into a `List` the
caller owns.
- Review corrections:
  - `InitWithSize` already reuses its words on #33144.
  - Leak checks are pool `CurrNB()` tests.
- The ascending-sels assertion is new work: `Shrink` does not check
today.
  - New primitives are Gobra-verified.

### Bitmap primitives (`pkg/common/bitmap/bitmap_select.go`,
Gobra-verified)

Contracts are stated in terms of `Has`, the ghost "row is set"
predicate.

| Primitive | Proven behaviour | Replaces |
| --- | --- | --- |
| `InitWithBools(values, nulls, mp)` | row `r` is set iff `values[r]` is
true and `r` is not in `nulls` | bool predicate → sels → `InitWithRows`
|
| `InitDeposit(base, sel, mp)` | row `r` is set iff `r` is in `base` and
`sel` holds `r`'s rank in `base` | per-row rank/select loops over a
delete mask |
| `KeepFirstN(k)` | a row stays iff it was set and fewer than `k` set
rows lie below it | positional split of sels |
| `Word(i)`, `NumWords()` | bit `b` of `Word(i)` is row `64i+b` |
per-row `Contains` / iterator in gather loops |

### Vector and Batch

- `Vector.UnionByMask` / `Batch.UnionByMask` append the selected rows of
a source without building a row list.
- They handle const sources, a source that is the destination itself (or
a window of it), varlen area pre-growth, and NULLs.
  - The loop is word-at-a-time, and full words copy 64 rows at once.
- `Shrink` asserts strictly increasing, non-negative sels in race
builds.

### Benchmarks (8192 rows, 0 allocations for the new paths)

| | new | path it replaces |
| --- | --- | --- |
| `InitWithBools` vs bools → sels → `InitWithRows` | 3.3–4.6 µs | 8.2–12
µs |
| `UnionByMask` vs `ToI64Array` + `Union`, 50% selected | 2.6–2.9 µs |
3.0–3.7 µs |
| same, 95% selected | 5.3–5.5 µs | 6.1–6.8 µs |

### Gobra verification

- **Results:**
- `gobra/verify.sh`: all 70 runs verified, including the 7 members of
`bitmap_select.go`.
  - `pkg/container/nulls/gobra/verify.sh`: 0 errors.
- Vacuity check (`assert false` planted at the entry and exit of every
new and restructured member): caught in all 22 variants.
- **Split file:** `bitmap_select.go` is verified with `bitmap.go`
loaded. Runs for `bitmap.go`'s own members leave it out, so the new
members don't change existing proofs' context.
- **Existing proofs made robust:** `removeOrderedWord`/`removeOrdered`
and `removeMaskWord`/`removeMask` passed or failed depending on
unrelated edits elsewhere, and even on the directory Gobra ran in, with
identical files.
- Their output relation is now a named ghost function (`RemoveRel`,
`RemoveMaskRel`), so each check matches one term.
  - All four verify from every directory tried.
- **Split proofs:** `KeepFirstN` timed out after 40 minutes as a single
method. It is split into the helper `keepLowBits` and the heap-free
lemma `keepFirstRows`, and each part verifies in minutes. `InitDeposit`
is split the same way, with the lemmas `depositWordRows` and
`depositStep`.
- **Bit loops:** the loops over set bits use `TrailingZeros64` plus
`&^`, because the bit model has no axiom for `w & -w`. Benchmarks are
unchanged.
- `pkg/common/bitmap/gobra/README.md` documents the proofs and these
lessons.

### Testing

- **Unit tests:**
- Every bitmap primitive is compared against a per-row model, with
read-only shifted views as inputs and the pool's `CurrNB()` back to
zero.
- `UnionByMask` is compared against `Union(sels)` across fixed widths
1/2/4/8/16/32/24 bytes and varchar (inline and out-of-line values), with
NULLs, const sources, self-aliasing, and masks longer than the source.
- **Packages:** `go test` passes for `pkg/common/bitmap`,
`pkg/container/{nulls,vector,batch}` and `pkg/objectio`. `go build
./...` and `go vet` are clean.
- **Race build:** the race tests pass for all 21 packages that call
`Shrink`, and the new ascending assertion never fires.

🤖 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
ci — 20d35d4a Deployed Nov 24, 2026 by fengttt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/XS Denotes a PR that changes [1, 9] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants