Skip to content

Introduce Peer-Group Start Border Sliding Index - #123737

Merged
Michicosun merged 12 commits into
masterfrom
window_functions_model_peer_group_starts_index
Oct 6, 2026
Merged

Michicosun merged 12 commits into
masterfrom
window_functions_model_peer_group_starts_index

Conversation

@Michicosun

@Michicosun Michicosun commented Oct 3, 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 new sliding index that will precalculate where, within the partition, peer groups start. This way we can remove the constraint on block retention that we had before - keep blocks in memory while the peer group start is still needed because it was used to compare key columns via the arePeers predicate.


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

Version info

  • Merged into: 26.10.1.1628-master (included in 26.10 and later)

@clickhouse-gh

clickhouse-gh Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [5d250f3]

Summary: ✅


AI Review

Summary

This PR introduces a precomputed peer_group_starts sliding index and moves the window executor onto partition-local row/group counters so it can stop retaining old blocks just to detect peer-group boundaries. The core refactor looks coherent and CI is green, but I found one remaining block-boundary dependency in a touched window function consumer that still produces wrong results when a partition spans multiple input blocks.

Findings

❌ Blockers

  • [src/WindowFunctions/WindowFunctionNonNegativeDerivative.cpp:148] nonNegativeDerivative still decides whether the current row is the partition's first row from current.location.row, which is only the row number inside the current block. When a partition continues into a new block, that value resets to 0, so the first row of every later block is incorrectly forced to 0 again. Use current.row_index_in_partition == 0 (or WindowRowAccess::isPartitionFirstRow) so the function keys off the partition-global row index the refactor just introduced.
Final Verdict

Needs changes before merge: the new partition-local row bookkeeping is not applied consistently yet, and nonNegativeDerivative still returns incorrect results on multi-block partitions.

LLVM Coverage Report

Measured on commit 5d250f3.

Metric Baseline Current Δ
Lines 88.20% 88.20% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 230/231 (99.57%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Oct 3, 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 3, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 5d250f3b1 with master 6b5ed923f (stripped binary size, per-symbol sizes and ThinLTO time; 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.

Compile time of recompiled translation units

23 translation units recompiled, 50 s compile time in total, 23 of them have a recent master baseline.

Job report

@yariks5s yariks5s self-assigned this Oct 5, 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.

👍

below are non-blockers to me

@Michicosun
Michicosun enabled auto-merge October 5, 2026 22:20
@Michicosun
Michicosun added this pull request to the merge queue Oct 6, 2026
Merged via the queue into master with commit b5446bf Oct 6, 2026
149 checks passed
@Michicosun
Michicosun deleted the window_functions_model_peer_group_starts_index branch October 6, 2026 03:33
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 6, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

@Michicosun @yariks5s — ClickGap found the following in this PR:

Close anything that's wrong or already addressed.

groeneai added a commit to groeneai/ClickHouse that referenced this pull request Oct 7, 2026
…Params peer check

Master's window refactor (ClickHouse#122762, ClickHouse#123260, ClickHouse#123737) moved the peer comparison into
WindowTransformParams::arePeers and added SlidingIndexes, which marks peer-group starts for every
RANGE and GROUPS frame with raw compareAt / getEqualRangeEndAssumeSorted. The input is sorted with
the ORDER BY collator, so collation-equal values that differ in bytes (for example 'a' and 'A'
under 'en-u-ks-level2') are interleaved, and the raw scan either cuts a peer group short or trips
the debug check "Equal values are not contiguous within the range assumed to be sorted". Since
ClickHouse#123737 this aborts deterministically for an ordinary collated rank():

    SELECT rank() OVER (ORDER BY x COLLATE 'en-u-ks-level2')
    FROM (SELECT if(number % 2, 'a', 'A') AS x FROM numbers(1000))

The previous version of this fix lived in WindowTransform methods that the refactor removed, so
it is re-done on the new structure. WindowTransformParams gets the collation flag and two helpers,
haveEqualOrderByValues and getOrderByEqualRangeEnd, and every ORDER BY peer comparison goes
through them: arePeers (cume_dist and the GROUPS cross-block check), the SlidingIndexes block
boundary and in-block scan, the RANGE frame-end check and scan, and the GROUPS peer-group scan.
PARTITION BY keys stay raw, as PARTITION BY cannot carry COLLATE.

The helpers are inline in the header with the collated comparison in out-of-line cold functions,
so an uncollated window keeps the inlined raw comparison of master: the earlier out-of-line
version of these calls cost 15-19% on window_function_with_order in the arm performance check.

The test gains the deterministic 1000-row case above (also for GROUPS), a cume_dist case and a
one-row-per-block rank() case, so each of the six comparison sites has an assertion that fails
when only that site compares raw.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Oct 8, 2026
Master's window refactor (ClickHouse#123737) replaced the runtime arePeers transition in
computeReadyRows with a per-block SlidingIndex::peer_group_starts, which still
marks every row as a peer group start under ROWS, so rank/dense_rank still
degenerate into row_number there.

Port the fix onto that model instead of the runtime prev-row comparison:
- SlidingIndexes computes order_by_peer_group_starts with the same
  equal-range scan RANGE/GROUPS use (crossing blocks via last_order_key), only
  when WindowTransformParams::separate_order_by_peer_groups is set, i.e. a ROWS
  frame with rank or dense_rank.
- WindowTransform tracks order_by_peer_group_start from that index;
  orderByPeerGroupStart() returns it, or peer_group_start otherwise, and
  rank/dense_rank read it.
- No block needs to be kept alive for the check any more, and
  haveEqualOrderByValues is gone.

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.

4 participants