Skip to content

Introduce Peers Abstraction for WindowTransform - #125749

Merged
Michicosun merged 24 commits into
masterfrom
window_functions_model_peers
Oct 16, 2026
Merged

Michicosun merged 24 commits into
masterfrom
window_functions_model_peers

Conversation

@Michicosun

@Michicosun Michicosun commented Oct 14, 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 combines the current row and peer group into a single peers abstraction to clean up the window transform code and improve the performance of some pure window functions.


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

Version info

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

@clickhouse-gh

clickhouse-gh Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [41b42f5]

Summary: ✅


AI Review

Summary

The PR introduces Peers to track the current row and peer-group bounds in WindowTransform, and reuses those bounds in cume_dist. Reviewed head 41b42f5bb9dac7e693d576e9df9484fb25dfa96d. The partition-boundary correctness issue is fixed and its thread is resolved. ⚠️ Request changes remains appropriate because the shared hot-path performance tradeoff lacks measurements and the boundary fix lacks committed regression coverage. No new inline findings were identified.

Findings

⚠️ Majors

  • src/Processors/Transforms/WindowTransform.cpp:333: The claimed performance benefit remains unverified across the shared row loop. Peer-end discovery now runs for every window, including ROWS frames and streaming row_number, count, and sum that do not consume peer ends; direct current-row accesses also become calls to the separately defined Peers::current. This creates a plausible regression risk for other users of the same loop. Provide a base/head comparison of window_transform and window_functions_streaming, covering cume_dist and the simple streaming cases. The PR description contains no measurements; AMD comparison jobs are skipped without pr-performance, and ARM comparisons are pending. Existing inline finding.
Tests
  • ⚠️ Add a new stateless regression for the partition-bounded search at src/Processors/Transforms/Window/Peers.cpp:91. Use the reproduction in the resolved correctness thread: two 17-row partitions in one block, each with peer sizes 1 and 16. Assert that cume_dist for each final peer group is 1, with both a large block and a split-block case. The current Fast-test binary passes this reproduction and 24 additional boundary cases, but the PR adds no regression test. Existing small ties and max_block_size = 1 coverage did not prevent this bug.
  • ⚠️ Supply the base/head performance evidence requested above; correctness tests do not establish the optimization's benefit or exclude regressions.
Final Verdict

⚠️ Request changes. Minimum actions: provide focused before/after performance results and commit the partition-boundary regression test. No unresolved correctness blocker remains.

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Oct 14, 2026
if (current.peer_group_index_in_partition != peer_group_start.peer_group_index_in_partition)
peer_group_start = current;
checkInvariants();
peers.advancePeerGroupBounds(blocks, partition.bounds());

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.

⚠️ Major — validate the performance tradeoff in the shared row loop

This now computes peer-group ends for every window, including ROWS frames and functions such as row_number or streaming count/sum that never consume those ends. It also replaces direct row access throughout the loop with calls to Peers::current, whose definition is in a separate translation unit. The claimed improvement to pure window functions needs before/after evidence covering these other users of the same hot path.

The PR description has no measurements, the performance dashboard has no runs for this PR, and the AMD comparison jobs were skipped because pr-performance is absent. Please provide a base/head comparison using the existing window_transform and window_functions_streaming benchmarks, covering both cume_dist and the simple ROWS/streaming cases. This would establish that the peer-group optimization pays for the extra work across the affected paths.

@Michicosun Michicosun added the ci-performance performance only label Oct 14, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-window-functions Window function execution + frame handling (ROW_NUMBER/RANK/LAG/LEAD, frames, partitions, order). label Oct 14, 2026
@yariks5s yariks5s self-assigned this Oct 14, 2026
@Michicosun Michicosun added ci-performance performance only and removed ci-performance performance only labels Oct 14, 2026
@Michicosun
Michicosun force-pushed the window_functions_model_peers branch from 4e7160e to a69b5ed Compare October 15, 2026 08:59
@Michicosun Michicosun removed the ci-performance performance only label Oct 15, 2026

@yariks5s yariks5s left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

👍

// scanning forward to the last peer) only when we enter a new peer group; otherwise reuse the
// cached value. This turns the per-peer-group cost from O(k^2) into O(k).
if (state.cached_peer_group_number != transform->current.peer_group_index_in_partition + 1)
if (state.cached_peer_group_number != transform->peers.current().peer_group_index_in_partition + 1)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit

Peers already maintains the group end, so read peers.bounds().end.row_index_in_partition directly and remove cached_peer_group_number, cached_peer_group_end_row_number, and their reset logic

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good catch, I didn't refactor window functions yet, so in the current pr no logic changes.

let's do it in a follow-up to speed up the CI runs :)

@Michicosun
Michicosun added this pull request to the merge queue Oct 16, 2026
Merged via the queue into master with commit 97b6dab Oct 16, 2026
178 checks passed
@Michicosun
Michicosun deleted the window_functions_model_peers branch October 16, 2026 13:15
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 16, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Oct 29, 2026
…-rank-dense-rank-rows-frame-peers

Master's ClickHouse#125749 (Peers) and ClickHouse#127389 (general window functions batch
execution) removed IWindowFunction.h and made rank() and dense_rank()
read bounds.peers_start and bounds.peer_group_index.

Port of the same fix onto that interface:
- needsSeparateOrderByPeerGroups uses the function's getRequirements().peers
  (under ROWS only rank() and dense_rank() can require peers, cume_dist() and
  percent_rank() reject any frame but RANGE over the whole partition), so the
  needsOrderByPeerGroup virtual is dropped.
- The ORDER BY peer group start is tracked in WindowTransform as before and
  appendCurrentRow passes it as peers_start / peer_group_index, so the rank
  and dense_rank sources are master's again.
- SlidingIndexes is unchanged on master and merged cleanly.

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