Skip to content

Keep the window state layout of contingency, cramersV and theilsU out of -State results - #128685

Merged
davenger merged 10 commits into
masterfrom
crosstab-window-state-stays-in-window
Nov 4, 2026
Merged

davenger merged 10 commits into
masterfrom
crosstab-window-state-stays-in-window

Conversation

@davenger

@davenger davenger commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

contingency, cramersV, cramersVBiasCorrected and theilsU keep a window function's state in a second memory layout (#93384). The type name and the binary type encoding do not show this layout. So when a -State result of such a window function gets into a column, any code that makes the type again from its name (a spill to disk, a CAST, a distributed query plan) reads the state with the wrong layout. Earlier fixes corrected this at each place where it occurred.

This PR makes -State always build its nested function with the ordinary layout, so a window-layout state never leaves the window function. Finished results such as cramersV(...) OVER (...) keep the window layout.

A -Merge window function keeps the window layout and merges ordinary states into it, so cramersVMerge(s) OVER (...) stays fast. -Map now supports this merge too (the change is taken from #119401), so contingencyMapMerge(s) OVER (...) works.

ColumnAggregateFunction now throws a LOGICAL_ERROR exception if it gets a function with the window layout, so a new path that breaks this rule fails in tests instead of reading wrong memory.

A window -State result and an ordinary -State result now have the same type. Before, their types printed the same but were not equal, so UNION ALL of the two made a Variant that -Merge could not read, and only its `AggregateFunction(...)` subcolumn could be merged. Now UNION ALL gives the state type, and -Merge reads the column directly.

Related: #93384
Related: #104277
Related: #109200
Related: #114020
Related: #119401
Related: #127800

Changelog category (leave one):

  • Critical Bug Fix (crash, data loss, RBAC)

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

Fix a crash and wrong TYPE_MISMATCH or CANNOT_CONVERT_TYPE errors when contingencyState, cramersVState, cramersVBiasCorrectedState or theilsUState is used as a window function and its result goes through a spill to disk, a CAST, or a distributed query plan. contingencyMapMerge and the other -MapMerge functions of this family now work as window functions.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.11.1.1302-master (included in 26.11 and later)
  • Backported to: 26.9.29.5, 26.8.38.8

@clickhouse-gh

clickhouse-gh Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ebc4bf2]

Summary: ✅


AI Review

Summary

The PR keeps states returned by window -State functions in the ordinary aggregation layout, preserves window -Merge support through combinators, and rejects window layouts in ColumnAggregateFunction. No remaining actionable findings; the two earlier review findings are addressed in the current code and covered by regression tests.

Missing context / blind spots
  • ⚠️ CI for head ebc4bf2c930060aaba46070cc347df9bd62f5b44 is still running. The three new regression tests and existing related tests passed under ASan/UBSan on preceding commit d38e88857a2dbb07a5510cffdf4fd8c2ebbb8b97; the subsequent change adds an assertion. Completing the current CI run will close this validation gap. No local build or tests were run.
Final Verdict

Status: ✅ Approve. No additional code changes requested.

@clickhouse-gh clickhouse-gh Bot added pr-not-for-changelog This PR should not be mentioned in the changelog comp-aggregate-functions Aggregate function implementations (sum, avg, count, quantile, combinators, etc.). labels Nov 2, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing ebc4bf2c9 with master e49720075 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 0fce80382; 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 (+235.59 KiB total), 0 added.

Object file Master PR Δ
src/AggregateFunctions/CMakeFiles/clickhouse_aggregate_functions.dir/Combinators/AggregateFunctionM… 4.05 MiB 4.26 MiB +214.22 KiB (+5.16%)

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, 127 s compile time in total, 34 of them have a recent master baseline.

Job report

@nickitat

nickitat commented Nov 2, 2026

Copy link
Copy Markdown
Member
💡 Use Bug Fix rather than Not for changelog: the affected window implementations shipped in 26.5 via #93384. Add a user-facing entry:

Fix handling of states returned by  contingencyState ,  cramersVState ,  cramersVBiasCorrectedState , and  theilsUState  window functions when spilling sorts to disk, casting, or executing distributed queries.

@clickhouse-gh clickhouse-gh Bot added pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! and removed pr-not-for-changelog This PR should not be mentioned in the changelog labels Nov 2, 2026
@davenger
davenger marked this pull request as ready for review November 3, 2026 08:09
{
auto rhs_nested = rhs.getNestedFunction();
chassert(rhs_nested != nullptr);

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.

maybe also chassert(canMergeStateFromDifferentVariant(rhs));

}
}

public:

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.

is it needed?

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.

@davenger
davenger added this pull request to the merge queue Nov 4, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 4, 2026
@davenger
davenger added this pull request to the merge queue Nov 4, 2026
Merged via the queue into master with commit c300af7 Nov 4, 2026
185 checks passed
@davenger
davenger deleted the crosstab-window-state-stays-in-window branch November 4, 2026 09:17
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 4, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 4, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 4, 2026
Backport #128685 to 26.10: Keep the window state layout of `contingency`, `cramersV` and `theilsU` out of `-State` results
PedroTadim added a commit that referenced this pull request Nov 9, 2026
Cherry pick #128685 to 26.8: Keep the window state layout of `contingency`, `cramersV` and `theilsU` out of `-State` results
PedroTadim added a commit that referenced this pull request Nov 9, 2026
Cherry pick #128685 to 26.9: Keep the window state layout of `contingency`, `cramersV` and `theilsU` out of `-State` results
robot-clickhouse added a commit that referenced this pull request Nov 9, 2026
…y`, `cramersV` and `theilsU` out of `-State` results
robot-clickhouse added a commit that referenced this pull request Nov 9, 2026
…y`, `cramersV` and `theilsU` out of `-State` results
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 9, 2026
leshikus pushed a commit that referenced this pull request Nov 13, 2026
…cy`, `cramersV` and `theilsU` out of `-State` results
davenger pushed a commit that referenced this pull request Nov 15, 2026
…y`, `cramersV` and `theilsU` out of `-State` results
davenger pushed a commit that referenced this pull request Nov 15, 2026
…y`, `cramersV` and `theilsU` out of `-State` results
clickhouse-gh Bot added a commit that referenced this pull request Nov 15, 2026
Backport #128685 to 26.9: Keep the window state layout of `contingency`, `cramersV` and `theilsU` out of `-State` results
clickhouse-gh Bot added a commit that referenced this pull request Nov 15, 2026
Backport #128685 to 26.8: Keep the window state layout of `contingency`, `cramersV` and `theilsU` out of `-State` results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-aggregate-functions Aggregate function implementations (sum, avg, count, quantile, combinators, etc.). pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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.

5 participants