Skip to content

fix(frontend): apply data branch diff limit after PK ordering - #26570

Merged
XuPeng-SH merged 4 commits into
matrixorigin:mainfrom
gouhongshen:agent/issue-26056
Aug 2, 2026
Merged

XuPeng-SH merged 4 commits into
matrixorigin:mainfrom
gouhongshen:agent/issue-26056

Conversation

@gouhongshen

@gouhongshen gouhongshen commented Aug 1, 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:

Fixes #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.

@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 →

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@XuPeng-SH
XuPeng-SH merged commit ae2785f into matrixorigin:main Aug 2, 2026
25 checks passed
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…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]>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…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]>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…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]>

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/bug Something isn't working size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: DATA BRANCH DIFF OUTPUT LIMIT is applied before final primary-key sorting

3 participants