Skip to content

[4.2-dev] fix(window): reuse full and cumulative aggregate frames - #31546

Merged
XuPeng-SH merged 2 commits into
matrixorigin:4.2-devfrom
XuPeng-SH:fix/31530-unbounded-window-4.2
Oct 28, 2026
Merged

XuPeng-SH merged 2 commits into
matrixorigin:4.2-devfrom
XuPeng-SH:fix/31530-unbounded-window-4.2

Conversation

@XuPeng-SH

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • BUG
  • Improvement

Which issue(s) this PR fixes:

Addresses #31530 on 4.2-dev.

What this PR does / why we need it:

Aggregate windows currently rescan the entire frame for every output row. Full-partition and unbounded-prefix frames therefore perform quadratic aggregate work, even when the query ends with LIMIT 20.

Compute full-partition aggregates once and broadcast the finalized value using the existing off-heap allocation contract. For cumulative ROWS and RANGE, reuse independent, unfinalized COUNT/SUM/AVG/fixed-width MIN/MAX states and fill each input row once. RANGE snapshots include complete peer groups; finalization occurs only after all snapshots are complete. This preserves NULL handling, decimal scales, floating-point addition order, and result ownership.

Tests cover peers, partition isolation, grouped pagination, prepared reuse, the 8192-group boundary, variable-length results, deterministic linear work, aggregate errors, cancellation, and allocation denial.

Validation

  • Go 1.26.4, Linux/amd64, repository native/CGo setup: full Window package passes normally and under -race; seven focused tests also pass 100 repetitions each under -race.
  • Incremental go build, go vet, configured golangci-lint, molint, and changed-file license checks pass, including the new Go tests.
  • New BVT passes mo-tester's normal comparison twice on the same isolated cluster: 20/20 SQL statements each, with teardown checked. Public MySQL queries validate returned values and grouped/partitioned execution.
  • Astra/max design and final overall review completed; the review's allocator finding is resolved.

Same-harness measurements at 4,096 rows:

Aggregate work Before After
Whole-partition SUM, operator 1,551 ms 0.418 ms
Cumulative RANGE SUM, operator 806 ms 0.493 ms
Whole-partition SUM with LIMIT 20, public SQL 2,928 ms 0.970 ms

The final public SQL check at 50,000 rows takes 12.7 ms for whole-partition SUM. These are local measurements; CI is pending.

Unsupported cumulative aggregate states retain the existing evaluator. Many-singleton-partition setup costs and eager evaluation before LIMIT remain separate limitations.

@XuPeng-SH
XuPeng-SH requested a review from aunjgr as a code owner October 28, 2026 06:24
@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 commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor Author

mo-bot review · mo-bot-local · done

Commit: a11250d9e5f408e6d970f7bfa67ee30a7a4390a3

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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

Commit: a11250d9e5f408e6d970f7bfa67ee30a7a4390a3

Assessment: approve

Reviewed the complete five-file delta and its consumers. Aggregate reuse preserves frame semantics and result ownership while removing quadratic work on the targeted paths. No actionable findings remain.

Model: gpt-6-astra / xhigh

Session: 01a13dcf-1586-7a01-9000-d45e20995cf8 · slot 1 · generation 1

Validation:

  • All 83 Window tests passed normally and under race detection; seven focused tests each passed 100 race repetitions. Build and vet passed.

  • 60 decimal/wide-integer prefix controls passed across consecutive partitions.

  • All 20 SQL fixture statements and 11 result sets matched through an isolated MySQL endpoint, including metadata and prepared reuse.

  • At 4096 rows, median full-frame SUM improved from 1531 ms to 0.44 ms; cumulative RANGE improved from 770 ms to 0.56 ms. Warmed bounded-frame controls remained essentially unchanged.

  • Preflight, native provenance, unchanged source, discussion coverage and 21 command/log pairs reconciled in /home/xupeng/.local/state/mo-bot/jobs/62a55ccc1fc291d73222c84d/attempt-1/validation-audit.json; final audit receipt 1793170194383932379-5.json passed.

Limitations:

  • Validation used Linux/amd64 and an embedded MySQL fixture adapter; mo-tester, arm64 and multi-CN performance were not independently rerun. Frozen CI was pending.

  • Unsupported cumulative aggregates retain the existing evaluator; eager evaluation before LIMIT and singleton-partition setup costs remain outside this fix.

@XuPeng-SH
XuPeng-SH merged commit 0501178 into matrixorigin:4.2-dev Oct 28, 2026
2 of 3 checks passed
@XuPeng-SH
XuPeng-SH deleted the fix/31530-unbounded-window-4.2 branch October 28, 2026 09:12
XuPeng-SH added a commit that referenced this pull request Oct 28, 2026
## What type of PR is this?

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

## Which issue(s) this PR fixes:

Addresses #31530 on `release/v4.2.1-hotfix-20261026`.

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

Backport #31546 to the active 4.2.1 hotfix branch. Aggregate windows
currently rescan unbounded frames for every output row, causing
quadratic aggregate work, including with a `LIMIT` above the Window
operator.

Compute a whole-partition aggregate once and broadcast its result. Reuse
independent unfinalized states for supported cumulative COUNT/SUM/AVG
and fixed-width MIN/MAX frames; RANGE includes the complete peer group.
Preserve the existing evaluator for unsupported cumulative aggregates
and frames, and keep the target branch's unrelated vector and lifecycle
code unchanged.

Validation on the target branch, macOS arm64 and Go 1.26.4:

- Full Window package passed normally and with `-race`; tests cover
aggregate values, NULLs, peers, partition isolation, varlen/DISTINCT
full frames, work counts, cancellation and allocation failures.
- Exact public SQL BVT passed twice on a two-CN cluster: 20/20 each, no
skipped or failed statements, with database cleanup checked on both CNs.
- Incremental 31-package `go vet`, `molint` and configured
`golangci-lint` passed; lint reported zero issues. Molint printed six
diagnostics in unchanged frontend/compile files; the same six were
reproduced against the target baseline.
- Same local SQL harness, target baseline versus backport, 8,000 rows:
full SUM 10.708 s → 1.639 ms, cumulative SUM 5.323 s → 1.952 ms, full
COUNT 332.37 ms → 0.285 ms; returned values checked. These are single
observations on separate local cluster starts, not a statistical
no-regression benchmark. The UT also checks aggregate work counts.
- gpt-6.1-terra/max design and final overall review completed.

Unsupported cumulative DISTINCT, variable-length MIN/MAX and other
unsupported states, and frames ending at a non-current offset retain the
generic evaluator. CI remains separate.

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 kind/enhancement size/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants