Repository navigation
feat(vector): Permute replaces Shuffle; UnionRowsFrom for hash-join build gathers (#33181 PR 9 + 10) - #33329
Closed
fengttt wants to merge 2 commits into
Closed
feat(vector): Permute replaces Shuffle; UnionRowsFrom for hash-join build gathers (#33181 PR 9 + 10)#33329fengttt wants to merge 2 commits into
fengttt wants to merge 2 commits into
Conversation
1 of 7 tasks
…igin#33181 PR 9) Vector.Permute(rows, mp) and Batch.Permute apply a sort's permutation in place with the gather kernel's value loop and word-wise NULL gather. They replace Shuffle and Batch.Shuffle in order, mergeorder, top, mergetop, window, s3util, TN mergesort, logtail replay, readutil and publication; all callers already had []int64 rows, so pkg/sort is unchanged. Vector.PermuteWithScratch replaces ShuffleWithBuf for the TN merge sort: it gathers into a caller-kept Go-heap scratch and copies back, so the column is not reallocated. A scratch that overlaps the vector's data is replaced instead of kept. The identity permutation of the full length is a no-op. The old per-type Shuffle switches and shuffleWithSortedProof are gone. Permute matches Shuffle on random permutations; already-sorted input drops from 2.7-4.3 us to 1.1 us per 8192 rows; sort and top-k operator benchmarks are flat. An untyped (ANY) vector keeps Shuffle's rules: only its NULLs and length follow the rows, a constant takes the new length, and one holding values is rejected. Batch.Permute rejects a selected batch. The type-ops table loses its shuffle entries, and the ANY, selection and type-ops tests move to Permute. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rom (matrixorigin#33181 PR 10) Vector.UnionRowsFrom(srcs, rows, mp) gathers row rows[i] of srcs[i]. It takes one pass per column when every source is a flat vector of the destination's type: NULLs a word at a time, the fixed-width loop per payload width, varlena copied into an area grown once. Other sources fall back to UnionOne per row. A row outside its flat source is an error and the destination is unchanged. The hash join decodes build ordinals into (batch, row) once per output chunk and uses it for every build column, varlena included. TryUnionFlatFixedRows, FixedSourceRow and the run-wise varlena fallback are removed. BenchmarkHashJoinDuplicateFanout (8192 output rows over 4 build batches), pinned, min of 6 interleaved runs: int64 78.9 -> 69.5 us, decimal128 109.1 -> 81.2 us, varchar inline 134.8 -> 115.3 us, varchar 48 B 287.2 -> 255.3 us. Other hash-join benchmarks unchanged. The gather's unchecked per-row reads (a fixed value, a varlena header) live in tools.go, which may import unsafe; gather_sources.go does not. Co-Authored-By: Claude Opus 5.5 <[email protected]>
fengttt
force-pushed
the
sels-bitmap-33181-pr10
branch
from
November 30, 2026 03:46
8fb1a5b to
aa315ff
Compare
fengttt
marked this pull request as ready for review
November 30, 2026 03:46
fengttt
requested review from
LeftHandCold,
XuPeng-SH,
aptend and
gouhongshen
as code owners
November 30, 2026 03:46
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 closed, so it cannot be queued. Reopen it to get the box back. |
1 of 7 tasks
Contributor
Author
fengttt
added a commit
that referenced
this pull request
Nov 30, 2026
…gather kernel (#33181 PR 9 + 10 + 11) (#33333) ## 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: PRs 9, 10 and 11 of the row-selection plan in `docs/design/20261124-row-selection-bitmap-remaplist.md`, as three commits on current main (PRs 1–8 have merged). #33299 (PR 9) and #33329 (PRs 9 + 10) are closed and folded in here. - **PR 9 (`cd6e5f6e7c`):** sorts apply their permutation with the gather kernel, and `Shuffle` / `ShuffleWithBuf` go away. - **PR 10 (`aa315ff03e`):** the hash join's build-side gathers use one many-source gather kernel, and `TryUnionFlatFixedRows` goes away. - **PR 11 (`d196b27585`):** the TN merge gathers columns with `UnionRowsFrom`, and its transfer map comes from `remaplist.List.InvertInto`. ### PR 9: Permute replaces Shuffle - **`Vector.Permute(rows, mp)` and `Batch.Permute`** reorder in place: row i becomes old row `rows[i]`. They use the gather kernel's value loop per payload width and its word-wise NULL gather. They replace `Shuffle` / `Batch.Shuffle` in order, mergeorder, top, mergetop, window, s3util, TN mergesort, logtail replay, readutil and publication. Every caller already had `[]int64` rows, so `pkg/sort` and the callers' lists are unchanged. - **`Vector.PermuteWithScratch`** replaces `ShuffleWithBuf` for the TN merge sort: it gathers into a Go-heap scratch the caller keeps and copies back, so the column is not reallocated per sort. A scratch that overlaps the vector's own data is replaced instead of kept (the old code handled that alias only for the identity permutation). - **The identity permutation of the full length is a no-op**, the common case when a sort sees data already in order. - **Untyped (ANY) vectors keep `Shuffle`'s rules** (#33058): only their NULLs and length follow the rows, a constant takes the new length, and one holding values is rejected. `Batch.Permute` rejects a selected batch, as `Batch.Shuffle` did. - The per-type `Shuffle` switches, `shuffleWithSortedProof` and the type-ops table's `shuffle` / `shuffleWithBuf` entries are deleted. `Permute` is one loop per payload width for every type, so it needs no table entry. #### Rebase onto main (PR 9) - Upstream routed `Shuffle` through the type-ops table (#33339) and added the ANY rules (#33058) and the selected-batch check. `Permute` carries the ANY rules and the check, and the table drops its shuffle entries; the legacy-switch comparison in `type_ops_test.go` no longer covers a shuffle, since `Permute` is tested against `UnionOne` instead. - Upstream's ANY tests (`shuffle_any_null_test.go`, now `permute_any_null_test.go`), the selected-batch test and `join_util_test.go` call `Permute` / `PermuteWithScratch` with their assertions unchanged. - `GetDownstreamVector` is gone upstream, so publication and TN mergesort call `Permute` on the column directly. `TestBatchPermute` uses `batch.NewWithSize`. #### Benchmarks (PR 9) Pinned to one core, minimum of five runs, PR 8 → PR 9 (measured before the rebase): | benchmark | old | new | | --- | --- | --- | | permute 8192 rows, random order, int64 / int64 with NULLs | 3.5 / 8.6 µs | 3.5 / 8.7 µs | | same, decimal128 / varchar with NULLs | 22.6 / 28.9 µs | 22.7 / 29.0 µs | | same with a reused scratch (`PermuteWithScratch`), decimal128 / varchar | — | 10.9 / 13.4 µs | | scratch permute of already sorted input, int64 / varchar | 2.7 / 4.3 µs | 1.1 / 1.1 µs | | scratch permute in reverse order, int64 / varchar | 2.9 / 6.7 µs | 2.7 / 6.8 µs | | `BenchmarkTopUnaccountedResident` / `TopAccountedResident` | 1124 / 1139 µs | 1114 / 1126 µs | | `BenchmarkReviewSmallVarlenTop` | 5783 µs | 5694 µs | | `BenchmarkMergeTopUnaccountedResident` | 15.9 ms | 16.0 ms | | `BenchmarkOrderUnaccountedResident` | 8.79 ms | 8.81 ms | | mergeorder `BenchmarkOrder` / `MergeOrderUnaccountedResident` | 3029 / 724 µs | 3016 / 723 µs | | TN `BenchmarkSortBlockColumns` | 636 µs | 638 µs | The operators are flat: the permutation is a small part of a sort. `order`'s `BenchmarkOrder` panics ("mismatched multi-column semantic types") before and after this change, so it is left out. #### Tests (PR 9) - New tests: `Permute` and `PermuteWithScratch` against `UnionOne` for every payload width and NULL rate, with lists that repeat, drop and add rows; sorted flag, constants, pool mismatch; `Batch.Permute` with a vector shared by two columns; the scratch alias, reuse and string-metadata tests rewritten for `PermuteWithScratch`. - After the rebase: `vector` and `batch` pass with `-race`; `pkg/container/...`, `bitmap`, `remaplist`, `pkg/sql/colexec/...`, `publication`, TN `mergesort` and `blockio`, `readutil`, `logtailreplay`, `objectio` and `plan/function` pass. `go vet ./pkg/...` is clean and `make static-check-changed` reports 0 issues. - BVT not run. ### PR 10: UnionRowsFrom for hash-join build gathers - **`Vector.UnionRowsFrom(srcs, rows, mp)`** gathers row `rows[i]` of `srcs[i]`. - When every source is a flat vector of the destination's type, it takes one pass per column: NULLs a word at a time, the fixed-width value loop per payload width, and varchar payloads copied into an area grown once for the whole call. - Any other source (a constant, a different type, the destination itself) falls back to `UnionOne` per row, with the same result. - A row outside its flat source returns an error and leaves the destination unchanged. - **Hash join build side:** a matched build row is an ordinal over all build batches. Each ordinal is decoded into (batch, row) once per output chunk and used for every build column, varchar included. The probe side already used `Union` and is unchanged. - **Removed:** `TryUnionFlatFixedRows`, `FixedSourceRow` and the run-wise varchar fallback. - **`unsafe`:** the lint allows `unsafe` in `vector` only in `tools.go` and `vector.go`, so the gather's two unchecked per-row reads (`fixedAtUnchecked`, `varlenaAtUnchecked`) live in `tools.go`. Every row is checked against its source's length before them. The bounds-checked `util` casts instead cost 10–15% on the fanout benchmark (int64 68.2 → 77.9 µs); the helpers are within 1% of inline `unsafe`. #### Benchmarks (PR 10) Pinned to one core, minimum of six interleaved runs, PR 9 → PR 10, measured before the rebase. `BenchmarkHashJoinDuplicateFanout` (new) joins 64 probe rows against 4 × 8192 build rows with 128 matches per key: 8192 output rows, 3 build columns. | benchmark | old | new | | --- | --- | --- | | fanout, int64 | 78.9 µs | 69.5 µs | | fanout, decimal128 | 109.1 µs | 81.2 µs | | fanout, varchar 8 B (inline) | 134.8 µs | 115.3 µs | | fanout, varchar 48 B | 287.2 µs | 255.3 µs | | `HashJoinUniqueProjection`, 256 rows from alternating build batches, int32 / decimal128 / varchar | 2.87 / 4.20 / 18.3 µs | 2.58 / 3.33 / 15.4 µs | | `HashJoinSmallDuplicatePayload`, `HashJoinMembershipProjection`, `RightExistentialRepeatedKeys` | | within ±1.5% | The machine was shared with other load, so only the interleaved minimums are compared. TPC-H join queries have not been run yet. #### Tests (PR 10) - `TestUnionRowsFromMatchesUnionOne`: every gather test type, several NULL rates and destination prefixes, compared with `UnionOne`. - `TestUnionRowsFromUnusualSources`: window, constant NULL and self sources. - `TestUnionRowsFromErrors`. - The former `TryUnionFlatFixedRows` tests are rewritten for `UnionRowsFrom`, including the allocation-account admission case (the destination is unchanged on rejection). - After the rebase: `pkg/container/vector` and `pkg/sql/colexec/hashjoin` pass with `-race`; `pkg/container/...`, `pkg/sql/colexec/...` and `pkg/sql/compile/...` pass. `go vet ./pkg/...` is clean and `make static-check-changed` reports 0 issues. ### PR 11: InvertInto for the TN merge transfer map - **`List.InvertInto(srcRows, dst)`** writes the inverse of a forward list into a list the caller owns: `srcRows` entries, with -1 for every source row the list leaves out. A repeated or out-of-range source row is an error. - **`MergeAObj` and `ReshapeBatches`** used to copy each kept row with `UnionOne` per column and fill a `[]int` map by hand. Now: - The heap (or the scan in input order) only records the kept rows as global row ids. - The columns are then gathered one at a time per output block with `UnionRowsFrom` (PR 10). - `InvertInto` derives the map. It is allocated from the merge's pool and freed by the merge's release function. - **Map type:** the map is `[]int64`, and so is `UpdateMappingAfterMerge`'s argument. - **Behaviour change:** a heap merge with more rows than the layout returns an error instead of indexing past the blocks. - **Unchanged:** `ReshapeBatches`' rules for which blocks it creates and which rows past the layout it drops. - **Not converted:** `AddSortPhaseMapping` inverts straight into the transfer slab's `TransferDestPos` entries, and an `int64` map in between would be an extra pass. #### Rebase (PR 11) PR 11's commit is cherry-picked onto PR 10 (aa315ff) rather than merged, since the old branch still carried the pre-squash PR 7–10 commits. - `aobj_merger.go` conflicted with upstream's removal of the TAE vector wrapper (#33342) inside the per-row `UnionOne` loop this PR deletes; the loop stays deleted. - With the wrapper gone, the gather takes each batch's column directly (no `GetDownstreamVector`, no staging slice), and the new tests and benchmark build their columns with `containers.MakeVector`. #### Benchmarks (PR 11) The input is 4 appendable batches of 8192 rows with an interleaving int64 sort key, an int64, a decimal128 and a varchar column, with every 10th row deleted. Pinned to one core on a quiet machine, minimum of five interleaved runs, PR 10 → PR 11, measured before the rebase: | benchmark | old | new | | --- | --- | --- | | `BenchmarkMergeAObjGather` | 1864 µs, 265 KB Go heap | 1020 µs, 134 KB | | `BenchmarkReshapeBatchesGather` | 1282 µs, 266 KB | 632 µs, 134 KB | #### Tests (PR 11) - `TestInvertInto`, `TestInvertIntoErrors` (96.6% coverage of `remaplist`). - `TestReshapeBatchesMapping` and `TestAObjMergeMapping` check every kept row, with all its columns and NULLs, against where the map says it went. Dropped rows must map to -1. The cases cover deletes, rows past the layout, a block larger than one gather chunk, a full block making the next one, and everything deleted. - `TestAObjMergeErrors`: too many rows for the layout, cancelled context. - Before the rebase: `./pkg/vm/engine/tae/...` passed (32 packages). - After the rebase: `remaplist` and `mergesort` pass with `-race`; TAE `mergesort`, `tables`, `containers` and `rpc`, `objectio` and `pkg/container/...` pass. `go vet ./pkg/...` is clean. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
This branch was successfully 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:
PRs 9 and 10 of the row-selection plan in
docs/design/20261124-row-selection-bitmap-remaplist.md, as two commits on current main (PRs 1–8 have merged). #33299 (PR 9) is closed and folded in here.cd6e5f6e7c): sorts apply their permutation with the gather kernel, andShuffle/ShuffleWithBufgo away.aa315ff03e): the hash join's build-side gathers use one many-source gather kernel, andTryUnionFlatFixedRowsgoes away.PR 9: Permute replaces Shuffle
Vector.Permute(rows, mp)andBatch.Permutereorder in place: row i becomes old rowrows[i]. They use the gather kernel's value loop per payload width and its word-wise NULL gather. They replaceShuffle/Batch.Shufflein order, mergeorder, top, mergetop, window, s3util, TN mergesort, logtail replay, readutil and publication. Every caller already had[]int64rows, sopkg/sortand the callers' lists are unchanged.Vector.PermuteWithScratchreplacesShuffleWithBuffor the TN merge sort: it gathers into a Go-heap scratch the caller keeps and copies back, so the column is not reallocated per sort. A scratch that overlaps the vector's own data is replaced instead of kept (the old code handled that alias only for the identity permutation).Shuffle's rules (fix: handle untyped NULL vectors during selection #33058): only their NULLs and length follow the rows, a constant takes the new length, and one holding values is rejected.Batch.Permuterejects a selected batch, asBatch.Shuffledid.Shuffleswitches,shuffleWithSortedProofand the type-ops table'sshuffle/shuffleWithBufentries are deleted.Permuteis one loop per payload width for every type, so it needs no table entry.Rebase onto main (PR 9)
Shufflethrough the type-ops table (refactor(vector): dispatch vector operations through a type-ops table (#10181 steps 4, 4b) #33339) and added the ANY rules (fix: handle untyped NULL vectors during selection #33058) and the selected-batch check.Permutecarries the ANY rules and the check, and the table drops its shuffle entries; the legacy-switch comparison intype_ops_test.gono longer covers a shuffle, sincePermuteis tested againstUnionOneinstead.shuffle_any_null_test.go, nowpermute_any_null_test.go), the selected-batch test andjoin_util_test.gocallPermute/PermuteWithScratchwith their assertions unchanged.GetDownstreamVectoris gone upstream, so publication and TN mergesort callPermuteon the column directly.TestBatchPermuteusesbatch.NewWithSize.Benchmarks (PR 9)
Pinned to one core, minimum of five runs, PR 8 → PR 9 (measured before the rebase):
PermuteWithScratch), decimal128 / varcharBenchmarkTopUnaccountedResident/TopAccountedResidentBenchmarkReviewSmallVarlenTopBenchmarkMergeTopUnaccountedResidentBenchmarkOrderUnaccountedResidentBenchmarkOrder/MergeOrderUnaccountedResidentBenchmarkSortBlockColumnsThe operators are flat: the permutation is a small part of a sort.
order'sBenchmarkOrderpanics ("mismatched multi-column semantic types") before and after this change, so it is left out.Tests (PR 9)
PermuteandPermuteWithScratchagainstUnionOnefor every payload width and NULL rate, with lists that repeat, drop and add rows; sorted flag, constants, pool mismatch;Batch.Permutewith a vector shared by two columns; the scratch alias, reuse and string-metadata tests rewritten forPermuteWithScratch.vectorandbatchpass with-race;pkg/container/...,bitmap,remaplist,pkg/sql/colexec/...,publication, TNmergesortandblockio,readutil,logtailreplay,objectioandplan/functionpass.go vet ./pkg/...is clean andmake static-check-changedreports 0 issues.PR 10: UnionRowsFrom for hash-join build gathers
Vector.UnionRowsFrom(srcs, rows, mp)gathers rowrows[i]ofsrcs[i].UnionOneper row, with the same result.Unionand is unchanged.TryUnionFlatFixedRows,FixedSourceRowand the run-wise varchar fallback.unsafe: the lint allowsunsafeinvectoronly intools.goandvector.go, so the gather's two unchecked per-row reads (fixedAtUnchecked,varlenaAtUnchecked) live intools.go. Every row is checked against its source's length before them. The bounds-checkedutilcasts instead cost 10–15% on the fanout benchmark (int64 68.2 → 77.9 µs); the helpers are within 1% of inlineunsafe.Benchmarks (PR 10)
Pinned to one core, minimum of six interleaved runs, PR 9 → PR 10, measured before the rebase.
BenchmarkHashJoinDuplicateFanout(new) joins 64 probe rows against 4 × 8192 build rows with 128 matches per key: 8192 output rows, 3 build columns.HashJoinUniqueProjection, 256 rows from alternating build batches, int32 / decimal128 / varcharHashJoinSmallDuplicatePayload,HashJoinMembershipProjection,RightExistentialRepeatedKeysThe machine was shared with other load, so only the interleaved minimums are compared. TPC-H join queries have not been run yet.
Tests (PR 10)
TestUnionRowsFromMatchesUnionOne: every gather test type, several NULL rates and destination prefixes, compared withUnionOne.TestUnionRowsFromUnusualSources: window, constant NULL and self sources.TestUnionRowsFromErrors.TryUnionFlatFixedRowstests are rewritten forUnionRowsFrom, including the allocation-account admission case (the destination is unchanged on rejection).pkg/container/vectorandpkg/sql/colexec/hashjoinpass with-race;pkg/container/...,pkg/sql/colexec/...andpkg/sql/compile/...pass.go vet ./pkg/...is clean andmake static-check-changedreports 0 issues.🤖 Generated with Claude Code