Skip to content

Support GROUPS frame mode for window functions - #108653

Merged
nihalzp merged 21 commits into
ClickHouse:masterfrom
nihalzp:groups-window-frame
Aug 13, 2026
Merged

nihalzp merged 21 commits into
ClickHouse:masterfrom
nihalzp:groups-window-frame

Conversation

@nihalzp

@nihalzp nihalzp commented Jun 26, 2026 •

Copy link
Copy Markdown
Member

The query below applies the same 1 PRECEDING AND 1 FOLLOWING bounds as a ROWS, a RANGE, and a GROUPS (this PR) frame. The order column contains duplicate and non-consecutive values, so the three modes cover different rows:

CREATE TABLE wf_frame_groups (`order` UInt64, value UInt64) ENGINE = Memory;
INSERT INTO wf_frame_groups FORMAT Values (10, 1), (10, 2), (20, 3), (30, 4), (30, 5);

SELECT
    order,
    value,
    groupArray(value) OVER (ORDER BY order ROWS   BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS rows_frame,
    groupArray(value) OVER (ORDER BY order RANGE  BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS range_frame,
    groupArray(value) OVER (ORDER BY order GROUPS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS groups_frame
FROM wf_frame_groups
ORDER BY order, value;
┌─order─┬─value─┬─rows_frame─┬─range_frame─┬─groups_frame─┐
│    10 │     1 │ [1,2]      │ [1,2]       │ [1,2,3]      │
│    10 │     2 │ [1,2,3]    │ [1,2]       │ [1,2,3]      │
│    20 │     3 │ [2,3,4]    │ [3]         │ [1,2,3,4,5]  │
│    30 │     4 │ [3,4,5]    │ [4,5]       │ [3,4,5]      │
│    30 │     5 │ [4,5]      │ [4,5]       │ [3,4,5]      │
└───────┴───────┴────────────┴─────────────┴──────────────┘

Each mode interprets the bounds differently:

  • ROWS counts physical rows, so the frame is at most three adjacent rows: the current row plus one on each side.
  • RANGE counts order values, so 1 PRECEDING and 1 FOLLOWING cover rows whose order is within 1 of the current row's. With gaps of 10, no neighbouring row qualifies, so the frame holds only the rows that share the current order.
  • GROUPS (added by this PR) counts peer groups, so 1 PRECEDING and 1 FOLLOWING always include the adjacent groups in full, whatever the gaps between order values.

cc: @cwurm

Changelog category (leave one):

  • New Feature

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

Support the GROUPS frame mode for window functions (SQL:2011), e.g. any(price) OVER (PARTITION BY symbol ORDER BY ts GROUPS BETWEEN CURRENT ROW AND 1 FOLLOWING). In a GROUPS frame the boundaries count whole peer groups — sets of rows that are equal on the ORDER BY key — so N PRECEDING/N FOLLOWING mean N peer groups before/after the current row's peer group, rather than physical rows (ROWS) or ORDER BY value distances (RANGE).

Version info

  • Merged into: 26.8.1.1354 (included in 26.8 and later)

@clickhouse-gh

clickhouse-gh Bot commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a893d21]

Summary: ✅


AI Review

Summary

This PR finishes user-facing GROUPS window-frame support by enabling the planner/executor path, adding focused stateless coverage, and updating the window-function docs. The executor-side regressions from earlier review rounds look addressed, but I still do not think this is safe to merge because distributed query-plan serialization can now send GROUPS frames to peers that do not implement them under the existing protocol version.

Findings

❌ Blockers

  • [src/Planner/PlannerWindowFunctions.cpp:85, src/Processors/QueryPlan/WindowStep.cpp:197, src/Core/ProtocolDefines.h:87] [dismissed by author -- https://github.com/Support GROUPS frame mode for window functions #108653#discussion_r3698372835] Enabling GROUPS here makes it a valid WindowStep payload for make_distributed_plan, but the query-plan protocol still does not fail closed for that new execution capability. DBMS_QUERY_PLAN_SERIALIZATION_VERSION is still 6, WindowStep::serialize only gates on version >= 4, and deserializeWindowFrame already accepts the GROUPS enum, so a mixed-version cluster can ship a GROUPS plan to a worker that still lacks the executor changes from this PR. make_distributed_plan is already present in stable releases, so this is not limited to unreleased code. Suggested fix: bump the query-plan serialization version for GROUPS support (with the matching ProtocolDefines.h comment/guard updates), or add an equivalent sender-side capability check that refuses to serialize WindowStep with GROUPS until every peer advertises support.
Final Verdict

Request changes.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.70% 86.70% +0.00%
Functions 92.00% 92.00% +0.00%
Branches 79.10% 79.10% +0.00%

Changed lines: Changed C/C++ lines covered: 172/172 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-feature Pull request with new product feature label Jun 26, 2026
Comment thread src/Interpreters/ExpressionAnalyzer.cpp Resolved
@antaljanosbenjamin antaljanosbenjamin self-assigned this Jul 27, 2026
Comment thread src/Interpreters/ExpressionAnalyzer.cpp Resolved
@nihalzp
nihalzp requested a review from a team as a code owner August 2, 2026 07:11
@@ -89,10 +84,6 @@ extractWindowDescriptions(const QueryTreeNodes & window_function_nodes, const Pl

auto function_window_description = extractWindowDescriptionFromWindowNode(window_function_node, planner_context);

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.

Making GROUPS reachable here also makes it a legitimate WindowStep payload in serialized query plans, but the plan protocol still has no way to distinguish peers that implement this frame type from peers that only speak query-plan serialization version 4.

serializeWindowFrame writes frame.type as a raw UInt8, deserializeWindowFrame accepts any value up to RANGE, and DBMS_QUERY_PLAN_SERIALIZATION_VERSION is still 4, so an older v4 peer will accept GROUPS at decode time even though its pre-PR executor still hits NOT_IMPLEMENTED when it reaches WindowTransform. That turns a protocol incompatibility into a late execution failure instead of failing closed up front.

Please bump the query-plan serialization version for this feature (or add an equivalent sender-side capability guard) before removing the planner-side rejection.

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.

Not a concern: WindowStep serialization is not in any release yet, and this lands in the same release, so no released peer can accept the step but lack GROUPS.

@clickhouse-gh

clickhouse-gh Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.50% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.70% 78.70% +0.00%

Changed lines: Changed C/C++ lines covered: 119/119 (100.00%) · Uncovered code

Full report · Diff report

`master` moved the documentation from the Docusaurus tree under
`docs/en/**/*.md` to the Mintlify tree at `docs/**/*.mdx`, deleting every
file this branch edits. The edits are replayed at the new paths:

- `docs/en/sql-reference/window-functions/dense_rank.md` -> `docs/reference/functions/window-functions/dense_rank.mdx`
- `docs/en/sql-reference/window-functions/first_value.md` -> `docs/reference/functions/window-functions/first_value.mdx`
- `docs/en/sql-reference/window-functions/lagInFrame.md` -> `docs/reference/functions/window-functions/lagInFrame.mdx`
- `docs/en/sql-reference/window-functions/last_value.md` -> `docs/reference/functions/window-functions/last_value.mdx`
- `docs/en/sql-reference/window-functions/leadInFrame.md` -> `docs/reference/functions/window-functions/leadInFrame.mdx`
- `docs/en/sql-reference/window-functions/nonNegativeDerivative.md` -> `docs/reference/functions/window-functions/nonNegativeDerivative.mdx`
- `docs/en/sql-reference/window-functions/nth_value.md` -> `docs/reference/functions/window-functions/nth_value.mdx`
- `docs/en/sql-reference/window-functions/rank.md` -> `docs/reference/functions/window-functions/rank.mdx`
- `docs/en/sql-reference/window-functions/row_number.md` -> `docs/reference/functions/window-functions/row_number.mdx`

Accepted the deletion for pages the migration did not carry over:

- `docs/en/sql-reference/window-functions/index.md` (no counterpart in the new tree)
# Conflicts:
#	docs/en/sql-reference/window-functions/dense_rank.md
#	docs/en/sql-reference/window-functions/first_value.md
#	docs/en/sql-reference/window-functions/index.md
#	docs/en/sql-reference/window-functions/lagInFrame.md
#	docs/en/sql-reference/window-functions/last_value.md
#	docs/en/sql-reference/window-functions/leadInFrame.md
#	docs/en/sql-reference/window-functions/nonNegativeDerivative.md
#	docs/en/sql-reference/window-functions/nth_value.md
#	docs/en/sql-reference/window-functions/rank.md
#	docs/en/sql-reference/window-functions/row_number.md
@clickhouse-gh

clickhouse-gh Bot commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing a893d2174 with master f656ba35b (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
Binary Master PR Δ
programs/clickhouse-stripped 691.73 MiB 688.71 MiB -3.02 MiB (-0.44%)

Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.

Object file sizes

6 object files changed (-89.74 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Planner/PlannerWindowFunctions.cpp.o 197.49 KiB 154.28 KiB -43.21 KiB (-21.88%)
src/CMakeFiles/dbms.dir/Processors/Transforms/WindowTransform.cpp.o 1.04 MiB 1023.86 KiB -38.99 KiB (-3.67%)

737 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

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

Job report

@nihalzp
nihalzp added this pull request to the merge queue Aug 13, 2026
Merged via the queue into ClickHouse:master with commit fb565a7 Aug 13, 2026
181 checks passed
@nihalzp
nihalzp deleted the groups-window-frame branch August 13, 2026 17:43
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-feature Pull request with new product feature 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