Skip to content

fix(window): support split aggregate results - #25852

Merged
XuPeng-SH merged 5 commits into
matrixorigin:mainfrom
XuPeng-SH:fix/window-split-agg-results
Jul 18, 2026
Merged

XuPeng-SH merged 5 commits into
matrixorigin:mainfrom
XuPeng-SH:fix/window-split-agg-results

Conversation

@XuPeng-SH

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 #25813

What this PR does / why we need it:

AggFuncExec.Flush returns multiple physical vectors after its result exceeds the aggregation chunk size (8192 rows). Window treated that internal storage detail as an unsupported logical result and failed the query. TimeWin had copied the same single-vector assumption and was vulnerable when one flush cohort crossed the chunk boundary.

This PR introduces a shared aggregate-result merge primitive and uses it in both single-batch consumers:

  • preserve the common single-chunk path as zero-copy;
  • reuse the first vector and pre-grow it once for multi-chunk results;
  • append complete chunks in order through UnionBatch, preserving fixed values, NULLs, grouping metadata, and varlen areas;
  • consume all result vectors on every path, including nil chunks, inconsistent types, allocation failure, and append failure;
  • keep Group unchanged because its physical chunks intentionally correspond to separate output batches.

The regression matrix covers ordinary SUM, DECIMAL(20,2) SUM from the issue, ROW_NUMBER's dedicated window executor, partitioned and non-partitioned Window input paths, TimeWin, fixed and varlen vectors, NULLs, multiple chunks, invalid results, off-heap OOM, and mpool cleanup.

Performance on Apple M4 with production-style off-heap vectors:

  • one chunk: ~1.36 ns/op, zero copy, 0 Go allocations;
  • two fixed chunks: ~10.2 us/op;
  • four fixed chunks: ~19.3 us/op;
  • two varlen chunks: ~4.8 us/op.

Multi-chunk work is linear in the result bytes and performs one capacity plan instead of incremental or per-row growth. Multi-chunk Window/TimeWin queries previously returned an error, so the existing successful path has no regression.

Validation:

  • go test ./pkg/sql/colexec/... -count=1 -timeout=300s with the repository CGo/rpath settings
  • focused regression tests for Window, TimeWin, and the shared merge helper
  • focused regression matrix under go test -race
  • go vet ./pkg/sql/colexec/aggexec ./pkg/sql/colexec/window ./pkg/sql/colexec/timewin
  • BenchmarkMergeSplitResult with fixed/varlen and 1/2/4 chunk cases

@XuPeng-SH
XuPeng-SH requested a review from aunjgr as a code owner July 17, 2026 17:46
@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 requested a review from a user July 17, 2026 18:11
@XuPeng-SH

Copy link
Copy Markdown
Contributor Author

BVT coverage added in commit 6259e0df9e.

Validation with the local mo-tester runner:

  • affected multi-CN baseline (87e33a9cf2, without this fix): 3/3 window assertions failed with the Window operator currently does not support sending split result of window function (6/9 statements passed overall);
  • this PR built as a local multi-CN image: 9/9 statements passed, 100%;
  • stability rerun on this PR: 3 consecutive runs passed at 100% (about 0.09–0.12s per case file).

The BVT crosses the 8192/8193 chunk boundary and checks partitioned/non-partitioned Window paths, SUM and ROW_NUMBER, total row/value checksums, and row-to-window-result alignment.

@LeftHandCold

Copy link
Copy Markdown
Collaborator

Review notes for approved head 6259e0df9e2460a1ac43e7d9decb63cf8f042067:

  • MergeSplitResult preserves chunk order, NULL/grouping metadata, fixed-width and varlen payloads.
  • Flush result ownership is transferred exactly once, including nil/type/OOM/append failure cleanup.
  • The zero-copy single-chunk path and pre-growth semantics match the vector APIs.
  • Window and TimeWin lifecycle integration does not leave returned vectors owned by the aggregate executor.
  • Regression coverage crosses 8192/8193 for unpartitioned, partitioned, decimal SUM, ROW_NUMBER, and TimeWin paths.

Local validation: full aggexec/window/timewin tests passed; focused new tests passed with -race -count=3; go vet, go build, and git diff --check passed. No blocking issues found.

@LeftHandCold LeftHandCold left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

发现 1 个需要修复的资源生命周期问题:TimeWin 支持 split result 后,非最终 flush 会覆盖仍持有 DISTINCT 状态的旧聚合器,造成随中间批次持续累积的 mpool 泄漏。详情见 inline comment。

Comment thread pkg/sql/colexec/timewin/timewin.go Resolved

@gouhongshen gouhongshen 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.

Codex automated review

No defects found. The merge preserves row order/types/nulls and transfers vector ownership safely; TimeWin also releases flushed executor state and prior output batches correctly. Local focused tests were blocked by absent CGo third-party headers; PR CI is green.

@XuPeng-SH
XuPeng-SH merged commit c6a8d94 into matrixorigin:main Jul 18, 2026
23 checks passed
@XuPeng-SH
XuPeng-SH deleted the fix/window-split-agg-results branch July 18, 2026 09:02
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## 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#25813

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

`AggFuncExec.Flush` returns multiple physical vectors after its result
exceeds the aggregation chunk size (8192 rows). `Window` treated that
internal storage detail as an unsupported logical result and failed the
query. `TimeWin` had copied the same single-vector assumption and was
vulnerable when one flush cohort crossed the chunk boundary.

This PR introduces a shared aggregate-result merge primitive and uses it
in both single-batch consumers:

- preserve the common single-chunk path as zero-copy;
- reuse the first vector and pre-grow it once for multi-chunk results;
- append complete chunks in order through `UnionBatch`, preserving fixed
values, NULLs, grouping metadata, and varlen areas;
- consume all result vectors on every path, including nil chunks,
inconsistent types, allocation failure, and append failure;
- keep `Group` unchanged because its physical chunks intentionally
correspond to separate output batches.

The regression matrix covers ordinary SUM, DECIMAL(20,2) SUM from the
issue, ROW_NUMBER's dedicated window executor, partitioned and
non-partitioned Window input paths, TimeWin, fixed and varlen vectors,
NULLs, multiple chunks, invalid results, off-heap OOM, and mpool
cleanup.

Performance on Apple M4 with production-style off-heap vectors:

- one chunk: ~1.36 ns/op, zero copy, 0 Go allocations;
- two fixed chunks: ~10.2 us/op;
- four fixed chunks: ~19.3 us/op;
- two varlen chunks: ~4.8 us/op.

Multi-chunk work is linear in the result bytes and performs one capacity
plan instead of incremental or per-row growth. Multi-chunk
Window/TimeWin queries previously returned an error, so the existing
successful path has no regression.

Validation:

- `go test ./pkg/sql/colexec/... -count=1 -timeout=300s` with the
repository CGo/rpath settings
- focused regression tests for Window, TimeWin, and the shared merge
helper
- focused regression matrix under `go test -race`
- `go vet ./pkg/sql/colexec/aggexec ./pkg/sql/colexec/window
./pkg/sql/colexec/timewin`
- `BenchmarkMergeSplitResult` with fixed/varlen and 1/2/4 chunk cases
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## 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#25813

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

`AggFuncExec.Flush` returns multiple physical vectors after its result
exceeds the aggregation chunk size (8192 rows). `Window` treated that
internal storage detail as an unsupported logical result and failed the
query. `TimeWin` had copied the same single-vector assumption and was
vulnerable when one flush cohort crossed the chunk boundary.

This PR introduces a shared aggregate-result merge primitive and uses it
in both single-batch consumers:

- preserve the common single-chunk path as zero-copy;
- reuse the first vector and pre-grow it once for multi-chunk results;
- append complete chunks in order through `UnionBatch`, preserving fixed
values, NULLs, grouping metadata, and varlen areas;
- consume all result vectors on every path, including nil chunks,
inconsistent types, allocation failure, and append failure;
- keep `Group` unchanged because its physical chunks intentionally
correspond to separate output batches.

The regression matrix covers ordinary SUM, DECIMAL(20,2) SUM from the
issue, ROW_NUMBER's dedicated window executor, partitioned and
non-partitioned Window input paths, TimeWin, fixed and varlen vectors,
NULLs, multiple chunks, invalid results, off-heap OOM, and mpool
cleanup.

Performance on Apple M4 with production-style off-heap vectors:

- one chunk: ~1.36 ns/op, zero copy, 0 Go allocations;
- two fixed chunks: ~10.2 us/op;
- four fixed chunks: ~19.3 us/op;
- two varlen chunks: ~4.8 us/op.

Multi-chunk work is linear in the result bytes and performs one capacity
plan instead of incremental or per-row growth. Multi-chunk
Window/TimeWin queries previously returned an error, so the existing
successful path has no regression.

Validation:

- `go test ./pkg/sql/colexec/... -count=1 -timeout=300s` with the
repository CGo/rpath settings
- focused regression tests for Window, TimeWin, and the shared merge
helper
- focused regression matrix under `go test -race`
- `go vet ./pkg/sql/colexec/aggexec ./pkg/sql/colexec/window
./pkg/sql/colexec/timewin`
- `BenchmarkMergeSplitResult` with fixed/varlen and 1/2/4 chunk cases
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## 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#25813

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

`AggFuncExec.Flush` returns multiple physical vectors after its result
exceeds the aggregation chunk size (8192 rows). `Window` treated that
internal storage detail as an unsupported logical result and failed the
query. `TimeWin` had copied the same single-vector assumption and was
vulnerable when one flush cohort crossed the chunk boundary.

This PR introduces a shared aggregate-result merge primitive and uses it
in both single-batch consumers:

- preserve the common single-chunk path as zero-copy;
- reuse the first vector and pre-grow it once for multi-chunk results;
- append complete chunks in order through `UnionBatch`, preserving fixed
values, NULLs, grouping metadata, and varlen areas;
- consume all result vectors on every path, including nil chunks,
inconsistent types, allocation failure, and append failure;
- keep `Group` unchanged because its physical chunks intentionally
correspond to separate output batches.

The regression matrix covers ordinary SUM, DECIMAL(20,2) SUM from the
issue, ROW_NUMBER's dedicated window executor, partitioned and
non-partitioned Window input paths, TimeWin, fixed and varlen vectors,
NULLs, multiple chunks, invalid results, off-heap OOM, and mpool
cleanup.

Performance on Apple M4 with production-style off-heap vectors:

- one chunk: ~1.36 ns/op, zero copy, 0 Go allocations;
- two fixed chunks: ~10.2 us/op;
- four fixed chunks: ~19.3 us/op;
- two varlen chunks: ~4.8 us/op.

Multi-chunk work is linear in the result bytes and performs one capacity
plan instead of incremental or per-row growth. Multi-chunk
Window/TimeWin queries previously returned an error, so the existing
successful path has no regression.

Validation:

- `go test ./pkg/sql/colexec/... -count=1 -timeout=300s` with the
repository CGo/rpath settings
- focused regression tests for Window, TimeWin, and the shared merge
helper
- focused regression matrix under `go test -race`
- `go vet ./pkg/sql/colexec/aggexec ./pkg/sql/colexec/window
./pkg/sql/colexec/timewin`
- `BenchmarkMergeSplitResult` with fixed/varlen and 1/2/4 chunk cases

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Window operator fails when agg Flush returns split vectors (>8192 rows): "does not support sending split result of window function"

4 participants