Repository navigation
fix(frontend): apply data branch diff limit after PK ordering - #26570
Conversation
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? |
XuPeng-SH
left a comment
There was a problem hiding this comment.
Blocking performance issue at f450bce:
pkg/frontend/data_branch_output.go:921-946 materializes every candidate's complete output row before deciding whether the bounded heap will retain it. extractRowFromVector(..., false) clones every varlen value (VARCHAR/BLOB/JSON/arrays). Therefore OUTPUT LIMIT 1 over M differing wide rows scans the required M PKs, but also allocates/copies all M payloads even though M-1 are immediately discarded. A table with a small PK plus a 1 MiB BLOB turns LIMIT 1 over 100k differences into roughly 100 GiB of avoidable allocation/copy churn; the heap's O(N) live slice does not prevent severe GC pressure or an OOM.
Please split candidate handling into two phases: extract/compare the PK first, and materialize the displayed columns only when the candidate fills or replaces a heap slot (reusing already-extracted PK values). The desired cost is O(M * PK-width + N * output-row-width), not O(M * output-row-width). Add a focused wide-varlen/small-limit allocation benchmark or regression guard.
The semantic algorithm itself is sound for legal limits: the max-heap produces the same prefix as the existing comparator, LIMIT 0 still stops producers, positive limits fully consume unordered producers, and projection includes hidden PK columns. Q1/Q2/Q3 found no new resource leak, missed wakeup, or unbounded wait. Fresh verification passed both focused tests 20 times and under -race -count=100; CI is green. The heap is proportionate rather than over-designed, but the avoidable full-row materialization must be fixed before approval.
f450bce to
8834b52
Compare
XuPeng-SH
left a comment
There was a problem hiding this comment.
Re-reviewed exact head 35ae7d3. The prior blocking performance issue is closed: candidates clone and compare only PK values before heap admission, and non-PK output columns are materialized only for heap fills/replacements; the retained row reuses the extracted PK values. The bounded max-heap returns the same prefix as the final composite-PK ordering, preserves LIMIT 0 and unlimited behavior, drains positive-limit producers safely, and keeps memory bounded by LIMIT rows plus one reusable candidate PK. Q1/Q2/Q3 found no leak, hang, or unbounded accumulation. Fresh local evidence passed build, vet, formatting/error checks, focused tests x20, each changed test under race x100, full pkg/frontend under race, and the 64x64KiB LIMIT 1 benchmark at 68,424 B/op and 9 allocs/op. CI is green. No remaining blocker.
…origin#26570) ## What type of PR is this? - [ ] API-change - [x] BUG - [ ] Improvement - [ ] Documentation - [ ] Feature - [ ] Test and CI - [ ] Code Refactoring ## Which issue(s) this PR fixes: Fixes matrixorigin#26056 ## What this PR does / why we need it: `DATA BRANCH DIFF ... OUTPUT LIMIT N` previously stopped parallel diff producers as soon as N rows arrived. The consumer sorted only that arrival-order subset afterward, so rows with smaller primary keys produced later were permanently discarded. A focused pre-fix test reproduced the issue: with PK 100 arriving before PK 1, `OUTPUT LIMIT 1` returned 100. This PR: - consumes all positive-limit diff rows before final selection instead of stopping producers at the arrival-order boundary; - uses a bounded max-heap keyed by the existing composite-PK comparator to retain the globally smallest N rows in O(N) retained memory and O(M log N) selection time; - extracts and compares only primary-key values before heap admission, then materializes displayed columns only when a candidate fills or replaces a heap slot, reusing the extracted PK values; - preserves the existing `LIMIT 0` fast-stop behavior and unlimited-output sorting behavior; - centralizes PK comparison so heap admission and final ascending sort use the same ordering; - adds focused unit coverage for zero, one, exact, above-count, multi-replacement, and retained payload/PK association; - adds a wide-varlen/small-limit allocation benchmark and BVT coverage for both-side producers, hidden projected PKs, LIMIT 1/2, and composite primary keys. Validation on the final commit rebased onto `main`: - `make build` - `make err-check` - `GOWORK=off go vet -mod=readonly ./pkg/frontend` with the repository CGo include/library paths - focused unit tests passed 20 consecutive runs - focused unit tests passed with `-race -count=100` after the final rebase - full `pkg/frontend` passed with `-race -count=1` - `BenchmarkDataBranchOutputLimitWideRows` (64 rows × 64 KiB, LIMIT 1): 68,424 B/op and 9 allocs/op after the fix; the pre-fix implementation measured 4,204,796 B/op and 324 allocs/op - BVT `branch_output_limit_order`: 21/21 statements passed at 100%; every result was manually checked for final PK order, projection, and composite-key semantics - existing BVT `branch_output_limit_zero`: 15/15 statements passed at 100% before this follow-up; LIMIT 0 behavior is unchanged Residual risk / tradeoff: a correct positive limit cannot short-circuit unordered parallel producers, so it must scan the complete diff and clone each candidate PK. Wide non-PK payloads are cloned only for heap fills or replacements; a descending-key stream can replace the heap repeatedly. Rows with equal primary keys retain the pre-existing comparator's unspecified tie order. --------- Co-authored-by: XuPeng-SH <[email protected]>
…origin#26570) ## What type of PR is this? - [ ] API-change - [x] BUG - [ ] Improvement - [ ] Documentation - [ ] Feature - [ ] Test and CI - [ ] Code Refactoring ## Which issue(s) this PR fixes: Fixes matrixorigin#26056 ## What this PR does / why we need it: `DATA BRANCH DIFF ... OUTPUT LIMIT N` previously stopped parallel diff producers as soon as N rows arrived. The consumer sorted only that arrival-order subset afterward, so rows with smaller primary keys produced later were permanently discarded. A focused pre-fix test reproduced the issue: with PK 100 arriving before PK 1, `OUTPUT LIMIT 1` returned 100. This PR: - consumes all positive-limit diff rows before final selection instead of stopping producers at the arrival-order boundary; - uses a bounded max-heap keyed by the existing composite-PK comparator to retain the globally smallest N rows in O(N) retained memory and O(M log N) selection time; - extracts and compares only primary-key values before heap admission, then materializes displayed columns only when a candidate fills or replaces a heap slot, reusing the extracted PK values; - preserves the existing `LIMIT 0` fast-stop behavior and unlimited-output sorting behavior; - centralizes PK comparison so heap admission and final ascending sort use the same ordering; - adds focused unit coverage for zero, one, exact, above-count, multi-replacement, and retained payload/PK association; - adds a wide-varlen/small-limit allocation benchmark and BVT coverage for both-side producers, hidden projected PKs, LIMIT 1/2, and composite primary keys. Validation on the final commit rebased onto `main`: - `make build` - `make err-check` - `GOWORK=off go vet -mod=readonly ./pkg/frontend` with the repository CGo include/library paths - focused unit tests passed 20 consecutive runs - focused unit tests passed with `-race -count=100` after the final rebase - full `pkg/frontend` passed with `-race -count=1` - `BenchmarkDataBranchOutputLimitWideRows` (64 rows × 64 KiB, LIMIT 1): 68,424 B/op and 9 allocs/op after the fix; the pre-fix implementation measured 4,204,796 B/op and 324 allocs/op - BVT `branch_output_limit_order`: 21/21 statements passed at 100%; every result was manually checked for final PK order, projection, and composite-key semantics - existing BVT `branch_output_limit_zero`: 15/15 statements passed at 100% before this follow-up; LIMIT 0 behavior is unchanged Residual risk / tradeoff: a correct positive limit cannot short-circuit unordered parallel producers, so it must scan the complete diff and clone each candidate PK. Wide non-PK payloads are cloned only for heap fills or replacements; a descending-key stream can replace the heap repeatedly. Rows with equal primary keys retain the pre-existing comparator's unspecified tie order. --------- Co-authored-by: XuPeng-SH <[email protected]>
…origin#26570) ## What type of PR is this? - [ ] API-change - [x] BUG - [ ] Improvement - [ ] Documentation - [ ] Feature - [ ] Test and CI - [ ] Code Refactoring ## Which issue(s) this PR fixes: Fixes matrixorigin#26056 ## What this PR does / why we need it: `DATA BRANCH DIFF ... OUTPUT LIMIT N` previously stopped parallel diff producers as soon as N rows arrived. The consumer sorted only that arrival-order subset afterward, so rows with smaller primary keys produced later were permanently discarded. A focused pre-fix test reproduced the issue: with PK 100 arriving before PK 1, `OUTPUT LIMIT 1` returned 100. This PR: - consumes all positive-limit diff rows before final selection instead of stopping producers at the arrival-order boundary; - uses a bounded max-heap keyed by the existing composite-PK comparator to retain the globally smallest N rows in O(N) retained memory and O(M log N) selection time; - extracts and compares only primary-key values before heap admission, then materializes displayed columns only when a candidate fills or replaces a heap slot, reusing the extracted PK values; - preserves the existing `LIMIT 0` fast-stop behavior and unlimited-output sorting behavior; - centralizes PK comparison so heap admission and final ascending sort use the same ordering; - adds focused unit coverage for zero, one, exact, above-count, multi-replacement, and retained payload/PK association; - adds a wide-varlen/small-limit allocation benchmark and BVT coverage for both-side producers, hidden projected PKs, LIMIT 1/2, and composite primary keys. Validation on the final commit rebased onto `main`: - `make build` - `make err-check` - `GOWORK=off go vet -mod=readonly ./pkg/frontend` with the repository CGo include/library paths - focused unit tests passed 20 consecutive runs - focused unit tests passed with `-race -count=100` after the final rebase - full `pkg/frontend` passed with `-race -count=1` - `BenchmarkDataBranchOutputLimitWideRows` (64 rows × 64 KiB, LIMIT 1): 68,424 B/op and 9 allocs/op after the fix; the pre-fix implementation measured 4,204,796 B/op and 324 allocs/op - BVT `branch_output_limit_order`: 21/21 statements passed at 100%; every result was manually checked for final PK order, projection, and composite-key semantics - existing BVT `branch_output_limit_zero`: 15/15 statements passed at 100% before this follow-up; LIMIT 0 behavior is unchanged Residual risk / tradeoff: a correct positive limit cannot short-circuit unordered parallel producers, so it must scan the complete diff and clone each candidate PK. Wide non-PK payloads are cloned only for heap fills or replacements; a descending-key stream can replace the heap repeatedly. Rows with equal primary keys retain the pre-existing comparator's unspecified tie order. --------- Co-authored-by: XuPeng-SH <[email protected]>
What type of PR is this?
Which issue(s) this PR fixes:
Fixes #26056
What this PR does / why we need it:
DATA BRANCH DIFF ... OUTPUT LIMIT Npreviously stopped parallel diff producers as soon as N rows arrived. The consumer sorted only that arrival-order subset afterward, so rows with smaller primary keys produced later were permanently discarded. A focused pre-fix test reproduced the issue: with PK 100 arriving before PK 1,OUTPUT LIMIT 1returned 100.This PR:
LIMIT 0fast-stop behavior and unlimited-output sorting behavior;Validation on the final commit rebased onto
main:make buildmake err-checkGOWORK=off go vet -mod=readonly ./pkg/frontendwith the repository CGo include/library paths-race -count=100after the final rebasepkg/frontendpassed with-race -count=1BenchmarkDataBranchOutputLimitWideRows(64 rows × 64 KiB, LIMIT 1): 68,424 B/op and 9 allocs/op after the fix; the pre-fix implementation measured 4,204,796 B/op and 324 allocs/opbranch_output_limit_order: 21/21 statements passed at 100%; every result was manually checked for final PK order, projection, and composite-key semanticsbranch_output_limit_zero: 15/15 statements passed at 100% before this follow-up; LIMIT 0 behavior is unchangedResidual risk / tradeoff: a correct positive limit cannot short-circuit unordered parallel producers, so it must scan the complete diff and clone each candidate PK. Wide non-PK payloads are cloned only for heap fills or replacements; a descending-key stream can replace the heap repeatedly. Rows with equal primary keys retain the pre-existing comparator's unspecified tie order.