Skip to content

Introduce Window Aggregate Function Execution Strategy - #129257

Merged
Michicosun merged 13 commits into
masterfrom
window_functions_aggregates
Nov 6, 2026
Merged

Michicosun merged 13 commits into
masterfrom
window_functions_aggregates

Conversation

@Michicosun

@Michicosun Michicosun commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

This patch adds a window aggregate function interface execution strategy and implements prefix and recalculate strategies with dispatch based on the used frame type.


Workflow [PR]
Sync PR [sync-upstream/pr/129257]

Version info

  • Merged into: 26.11.1.1544-master (included in 26.11 and later)

@clickhouse-gh

clickhouse-gh Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [1acc33b]

Summary: ✅


AI Review

Summary

The PR moves aggregate window evaluation into prefix and recalculation strategies and changes input-block retention. Review of 1acc33bca18ca8ddd57e180754cdcf8c4e10fd5c found a reproduced memory regression at partition boundaries. The previously dismissed cancellation concern also remains in the current code.

Findings

⚠️ Majors

  • Completed partitions retain dead arena allocations. AggregateState.cpp:39–40: both strategies use recreate for partition resets, but it now retains the arena until allocated memory exceeds 16 MiB. Previously, every partition boundary released the arena. A query whose memory limit is below that threshold can exhaust memory while processing tiny independent partitions:

    SELECT any(repeat('x', 1000)) OVER (PARTITION BY number)
    FROM numbers(50000)
    SETTINGS max_threads = 1, max_block_size = 1000, max_memory_usage = 10000000
    FORMAT Null;

    The identical query passes on official 26.10.2.103 and fails on the current PR fast-test binary with MEMORY_LIMIT_EXCEEDED in WindowTransform: 14.57 MiB against a 9.54 MiB limit. Separate partition resets from frame rebuilding so partition boundaries release arena memory, and add this stateless regression case.

  • Aggregate batches do not observe cancellation. AggregateWindowFunctionRecalculate.cpp:37 [dismissed by author -- https://github.com/Introduce Window Aggregate Function Execution Strategy #129257#discussion_r4470343092]. Still real: the current evaluate loop can recalculate every expensive frame in a batch without observing cancellation; computeReadyRows checks cancellation only while collecting bounds. The earlier uniqExact reproducer with ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING still reaches this unchanged path. Check cancellation between aggregate frame evaluations, including in the prefix strategy. The dismissed thread was left resolved without another reply.

Missing context / blind spots
  • ⚠️ The current workflow is still running; full stateless and performance jobs were pending when inspected. Successful jobs for the reviewed head would close this validation gap. Local checks used the current fast-test binary: mixed sum, sumState, and groupArrayState results matched expectations at block sizes 1, 7, and 1000; repeated-frame state results across partitions also matched. These checks do not cover the full suite.
Final Verdict

⚠️ Request changes. Restore arena cleanup at partition boundaries and add the focused regression test. The cancellation concern remains outstanding as an author-dismissed finding; no duplicate inline comment was posted.

@clickhouse-gh clickhouse-gh Bot added pr-not-for-changelog This PR should not be mentioned in the changelog comp-window-functions Window function execution + frame handling (ROW_NUMBER/RANK/LAG/LEAD, frames, partitions, order). labels Nov 4, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 1acc33bca with master 57ca89d38 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of b5f808662; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

22 object files changed (+61.16 KiB total), 4 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/WindowFunctions/IAggregateWindowFunction.cpp.o new 20.32 KiB +20.32 KiB

719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

34 translation units recompiled, 83 s compile time in total, 30 of them have a recent master baseline.

Job report

@Michicosun Michicosun added the ci-performance performance only label Nov 5, 2026
@Michicosun
Michicosun enabled auto-merge November 5, 2026 11:21
@Michicosun
Michicosun disabled auto-merge November 5, 2026 11:21
@Michicosun Michicosun removed the ci-performance performance only label Nov 5, 2026
Comment on lines +39 to +40
if (arena && arena->allocatedBytes() > arena_replace_bytes)
arena = std::make_unique<Arena>();

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.

[P2] Clear the arena when resetting a partition

Both aggregate strategies call recreate from resetPartition, so this condition also changes partition cleanup: allocations from completed partitions now survive until the arena exceeds 16 MiB. Small partitions containing long strings accumulate dead state, and a query with a lower memory limit fails before reaching that cleanup threshold.

SELECT any(repeat('x', 1000)) OVER (PARTITION BY number)
FROM numbers(50000)
SETTINGS max_threads = 1, max_block_size = 1000, max_memory_usage = 10000000
FORMAT Null;

This passes on official 26.10.2.103, but the fast-test binary for 1acc33bca18ca8ddd57e180754cdcf8c4e10fd5c throws MEMORY_LIMIT_EXCEEDED in WindowTransform: it would use 14.57 MiB against a 9.54 MiB limit. The base implementation replaces its arena at every partition boundary. Preserve that cleanup separately from the threshold used for rebuilding frames within a partition, and cover this case with a stateless regression test.

@Michicosun
Michicosun added this pull request to the merge queue Nov 6, 2026
Merged via the queue into master with commit d55bffe Nov 6, 2026
186 checks passed
@Michicosun
Michicosun deleted the window_functions_aggregates branch November 6, 2026 17:26
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 6, 2026
subkanthi pushed a commit to subkanthi/ClickHouse that referenced this pull request Nov 12, 2026
An aggregate over only-NULL arguments collapses to `nothing`, whose state
has `sizeOfData() == 0`. A zero-byte arena allocation does not advance the
arena, so distinct zero-size states allocated back to back share one
address. Copying such a state, e.g. the same-frame copy of both window
strategies (`AggregateWindowFunctionPrefix`, `AggregateWindowFunctionRecalculate`)
through `ColumnAggregateFunction::insertFromWithOwnership`, which is
`insertDefault()` followed by `merge(new, previous)`, then trips
`chassert(place != rhs)` although the two states are distinct and have no
bytes for `mergeImpl` to read or write.

Every CI firing of this assertion family since it was added (ClickHouse#103543) is
such a zero-size state. The window site is reached for `-State` results
since ClickHouse#129257 and, before that, for `any`/`groupArray` over a state column.
Refine the predicate once (`IAggregateFunction::isSelfMerge`) and use it at
all six assertion sites instead of patching call sites one by one.
`mergeImpl` is still called, non-empty states are checked as before, and
release builds are unaffected (`chassert` is compiled out there).

STID: 2508-3fb0

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-window-functions Window function execution + frame handling (ROW_NUMBER/RANK/LAG/LEAD, frames, partitions, order). pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants