Skip to content

fix(exec): batch full-frame aggregate window input#643

Open
zhli1142015 wants to merge 1 commit into
bytedance:mainfrom
zhli1142015:zhli/non-spillable-last-window-test
Open

fix(exec): batch full-frame aggregate window input#643
zhli1142015 wants to merge 1 commit into
bytedance:mainfrom
zhli1142015:zhli/non-spillable-last-window-test

Conversation

@zhli1142015

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Non-spillable aggregate window functions can still evaluate full-partition frames where every output row has the same frame, such as ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING. For aggregate functions outside the spillable-window whitelist, the regular AggregateWindow path used to load the whole partition into argument vectors at once before computing the aggregate.

That creates avoidable memory spikes for large partitions. It also leaves the non-spillable path without regression coverage, because common aggregate functions such as sum, count, min, max, and avg can use the spillable window build instead.

Issue Number: N/A

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Performance improvement (optimization)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Refactoring (no logic changes)
  • Build/CI or Infrastructure changes
  • Documentation only

Description

Detect the non-streaming full-partition same-frame case in AggregateWindowFunction and handle it separately. The new path computes the aggregate once, loads partition input in chunks bounded by max_output_batch_rows, caches the computed result for the rest of the current partition, and copies the cached value to later output batches.

The optimization is intentionally gated to non-row-streaming partitions. Row-streaming partitions use cumulative frame coordinates that are not safe to re-read from row 0, so they continue to use the existing incremental path.

The tests cover both behavior and the intended path:

  • last(d) over an unbounded full frame verifies correctness for a non-spillable aggregate window and asserts that the spillable window build is not used.
  • A test-only aggregate records the largest addSingleGroupRawInput batch and verifies that full-frame aggregation is chunked by max_output_batch_rows instead of loading the whole partition at once.

Performance Impact

  • No Impact: This change does not affect the critical path (e.g., build system, doc, error handling).

  • Positive Impact: I have run targeted validation.

    Click to view validation results

    Regression evidence from SpillableWindowTest.nonSpillableSameFrameAggregationBatchesInput:
    - Before the fix, the test-only aggregate observed a max addSingleGroupRawInput batch of 512 rows.
    - After the fix, the max observed batch is 50 rows, matching max_output_batch_rows.
    
    Unit tests:
    - SpillableWindowTest.nonSpillableLastAggUnboundedFrame: passed
    - SpillableWindowTest.nonSpillableSameFrameAggregationBatchesInput: passed
    - WindowTest.* + SpillableWindowTest.* + RowStreamingWindowTest.* + SortAndWindowTest.* + SortWindowTest.*: 35 tests passed ``` </details>
    
  • Negative Impact: Explained below (e.g., trade-off for correctness).

Release Note

Release Note:

Release Note:
- Reduced memory spikes for non-spillable full-frame aggregate window functions by batching input loading and reusing the computed frame result.

Checklist (For Author)

  • I have added/updated unit tests (ctest).
  • I have verified the code with local build (Release/Debug).
  • I have run clang-format / linters.
  • (Optional) I have run Sanitizers (ASAN/TSAN) locally for complex C++ changes.
  • No need to test or manual test.

Breaking Changes

  • No
  • Yes (Description: ...)

@CLAassistant

CLAassistant commented Jun 16, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

### What problem does this PR solve?

Non-spillable aggregate window functions can still evaluate full-partition frames where every output row has the same frame, such as `ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING`. For aggregate functions outside the spillable-window whitelist, the regular AggregateWindow path used to load the whole partition into argument vectors at once before computing the aggregate.

That creates avoidable memory spikes for large partitions. It also leaves the non-spillable path without regression coverage, because common aggregate functions such as `sum`, `count`, `min`, `max`, and `avg` can use the spillable window build instead.

Issue Number: N/A

### Type of Change
- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [x] Performance improvement (optimization)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
- [ ] Refactoring (no logic changes)
- [ ] Build/CI or Infrastructure changes
- [ ] Documentation only

### Description

Detect the non-streaming full-partition same-frame case in `AggregateWindowFunction` and handle it separately. The new path computes the aggregate once, loads partition input in chunks bounded by `max_output_batch_rows`, caches the computed result for the rest of the current partition, and copies the cached value to later output batches.

The optimization is intentionally gated to non-row-streaming partitions. Row-streaming partitions use cumulative frame coordinates that are not safe to re-read from row 0, so they continue to use the existing incremental path.

The tests cover both behavior and the intended path:
- `last(d)` over an unbounded full frame verifies correctness for a non-spillable aggregate window and asserts that the spillable window build is not used.
- A test-only aggregate records the largest `addSingleGroupRawInput` batch and verifies that full-frame aggregation is chunked by `max_output_batch_rows` instead of loading the whole partition at once.

### Performance Impact
- [ ] No Impact: This change does not affect the critical path (e.g., build system, doc, error handling).
- [x] Positive Impact: I have run targeted validation.
    <details>
    <summary>Click to view validation results</summary>

    ```text
    Regression evidence from SpillableWindowTest.nonSpillableSameFrameAggregationBatchesInput:
    - Before the fix, the test-only aggregate observed a max addSingleGroupRawInput batch of 512 rows.
    - After the fix, the max observed batch is 50 rows, matching max_output_batch_rows.

    Unit tests:
    - SpillableWindowTest.nonSpillableLastAggUnboundedFrame: passed
    - SpillableWindowTest.nonSpillableSameFrameAggregationBatchesInput: passed
    - WindowTest.* + SpillableWindowTest.* + RowStreamingWindowTest.* + SortAndWindowTest.* + SortWindowTest.*: 35 tests passed
    ```
    </details>
- [ ] Negative Impact: Explained below (e.g., trade-off for correctness).

### Release Note

Release Note:
```text
Release Note:
- Reduced memory spikes for non-spillable full-frame aggregate window functions by batching input loading and reusing the computed frame result.
```

### Checklist (For Author)

- [x] I have added/updated unit tests (ctest).
- [x] I have verified the code with local build (Release/Debug).
- [x] I have run clang-format / linters.
- [ ] (Optional) I have run Sanitizers (ASAN/TSAN) locally for complex C++ changes.
- [ ] No need to test or manual test.

### Breaking Changes

- [x] No
- [ ] Yes (Description: ...)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@zhli1142015 zhli1142015 force-pushed the zhli/non-spillable-last-window-test branch from 6d7d1c3 to ab156c7 Compare June 16, 2026 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants