Repository navigation
Optimize cramersV, cramersVBiasCorrected, theilsU, and contingency when used with window functions - #93384
Conversation
|
Workflow [PR], commit [31743b5] Summary: ❌
AI ReviewSummaryThis PR adds Findings
ClickHouse Rules
Final VerdictStatus: Minimum required actions:
|
There was a problem hiding this comment.
Pull request overview
This PR optimizes four statistical aggregate functions (cramersV, cramersVBiasCorrected, theilsU, and contingency) when used with window functions by introducing specialized *Window variants. The optimization achieves ~600x performance improvement (8.575s → 0.014s) by replacing O(n) getResult() complexity with O(1) or O(log n) alternatives, making them suitable for window contexts where getResult() is called frequently. The rewrite from standard to window variants happens automatically via rewriteAggregateFunctionNameForWindowIfNeeded in the query resolver.
Key Changes
- Added
CrossTabWindowPhiSquaredDatabase class that maintains an incrementally-updatedphi_term_sumusing adjacency lists for efficient updates - Implemented specialized window variants:
TheilsUWindowData,CramersVWindowData,CramersVBiasCorrectedWindowData, andContingencyWindowData - Added automatic function rewriting logic in both analyzer and expression analyzer paths
Reviewed changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
src/AggregateFunctions/CrossTab.h |
Adds CrossTabWindowPhiSquaredData class with incremental phi-squared calculation; optimizes phi calculation in CrossTabData |
src/AggregateFunctions/AggregateFunctionTheilsU.cpp |
Implements TheilsUWindowData with cached entropy sums for O(1) results; fixes documentation range description |
src/AggregateFunctions/AggregateFunctionCramersV.cpp |
Adds CramersVWindowData variant and updates test examples |
src/AggregateFunctions/AggregateFunctionCramersVBiasCorrected.cpp |
Adds CramersVBiasCorrectedWindowData variant with consistent computation logic |
src/AggregateFunctions/AggregateFunctionContingencyCoefficient.cpp |
Adds ContingencyWindowData variant |
src/Analyzer/Resolve/resolveFunction.cpp |
Implements rewriteAggregateFunctionNameForWindowIfNeeded to automatically select window variants |
src/Interpreters/ExpressionAnalyzer.cpp |
Adds same rewriting logic for non-analyzer code path |
tests/performance/contingency_functions.xml |
Adds performance benchmarks covering window and non-window cases |
tests/queries/0_stateless/03781_contingency_functions_resolve.sql |
Tests that resolver correctly rewrites to window variants |
tests/queries/0_stateless/03782_contingency_window_functions_state_serialize_deserialize.sql |
Tests state serialization/deserialization roundtrips |
tests/queries/0_stateless/03783_contingency_window_functions_distributed.sql |
Tests window variants work correctly in distributed mode |
|
Slightly different direction, but for that particular window It actually does not change aggregated state between calling of getResult ( as window is the same for all rows) Could we have flag and cached result value in function. But, it wouldn't work for windows if it's rolling or adding. |
|
Yes, you are right. For this format: SELECT cramersV(number, number) OVER () FROM numbers(20000);we can definitely use your idea; but that is a different problem and require changes in various other places. The PR also optimizes any window functions like rolling or partition. For example: SELECT cramersV(number, number) OVER (ORDER BY number) FROM numbers(20000); |
| @@ -1,3 +1,22 @@ | |||
| SELECT 'Sanity Check to ensure window variant is choosen. Non-window variant would take too long'; | |||
There was a problem hiding this comment.
Typo: choosen → chosen.
| </query> | ||
|
|
||
| <query> | ||
| -- Worst-case performance test for `CrossTabWindowPhiSquaredData` inherited aggregate functions |
There was a problem hiding this comment.
Typo in class name: CrossTabWindowPhiSquaredData should be CrossTabPhiSquaredWindowData.
azat
left a comment
There was a problem hiding this comment.
I have not looked at the implementation of optimized version for window states (there was already approve before), everything else looks good, also PTAL at some minor comments
LLVM Coverage Report
Changed lines: 95.63% (940/983) · Uncovered code |
6f79d40
Master: 4.198 sec
PR: 0.014 sec
These functions were running in quadratic time complexity when used in window context. This PR adds
windowvariants of these aggregate functions. When a query uses these aggregates in a window context,resolveFunctionchooses the window variants creators.We do not use these
Windowversions for non-window context such as GROUP BY because they will be slower.Windowversions are optimized for incremental updates so thatgetResultisO(1).Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Optimize
cramersV,cramersVBiasCorrected,theilsU, andcontingencywhen used with window functions. Closes #83521.Documentation entry for user-facing changes
Version info
26.5.1.27