Repository navigation
Fix memory usage degradation of window functions with the analyzer - #126880
yakov-olkhovskiy wants to merge 7 commits into
Conversation
…query plan
With the analyzer, the projection is computed after the window functions, so the `Window` step
buffers the columns it reads: for `SELECT toUInt64OrZero(splitByString('@', s)[2]), count() OVER ()`
it buffers the `String` column `s` instead of a `UInt64`. The old query analysis computed such
expressions before the window functions.
The new second-pass optimization `pushDownExpressionsBelowWindow` (setting
`query_plan_push_down_expressions_below_window`) moves the expressions with a small fixed-size
result of a `String` or `FixedString` column below the `Window` steps and the `Sorting` steps for
them, if nothing else reads the column after them. Stateful, non-deterministic and side-effect
functions, `arrayJoin` and lazily executed arguments are not moved, so the results are the same.
Co-authored-by: yakov <[email protected]>
Co-authored-by: yakov <[email protected]>
|
|
|
Workflow [PR], commit [cdf703d] Summary: ✅
AI ReviewSummaryMoves eligible fixed-size expressions of Final VerdictStatus: ✅ Approve |
…rt-circuit evaluation disabled A `Filter` applies the arguments of an `and` condition one after another even with `short_circuit_function_evaluation = 'disable'`, so an argument is not executed for the rows removed by the previous ones. Computed in an `Expression` step below the window functions, `s != 'x' AND toUInt64(s) > 0` threw on `'x'`. Related: #126880 (comment) Co-authored-by: yakov <[email protected]>
…rt-circuit evaluation disabled The same fix as in #126880: a `Filter` applies the arguments of an `and` condition one after another even with `short_circuit_function_evaluation = 'disable'`, so `s != 'x' AND toUInt64(s) > 0` computed in an `Expression` step below the window functions threw on `'x'`. Related: #126880 (comment) Co-authored-by: yakov <[email protected]>
…hort-circuit mode With `short_circuit_function_evaluation = 'enable'`, the functions not suitable for short-circuit evaluation, like `toBool`, are executed for all rows, so only the sequential filtering of the `Filter` protected `toBool(s)` in `s != '' AND toBool(s)` from the empty string. Related: #126880 (comment) Co-authored-by: yakov <[email protected]>
…n `and` condition Such a `Filter` applies the arguments of the condition one after another and computes the rest of its expressions, like a merged projection with `query_plan_split_filter = 0`, only for the rows that passed the previous arguments. Moved below the window functions, `toUInt64(s)` was computed for all rows and threw. Related: #126880 (comment) Co-authored-by: yakov <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes48 object files changed (+107.25 KiB total), 1 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units155 translation units recompiled, 1363 s compile time in total, 154 of them have a recent master baseline. |
…ke the old query analysis With the old query analysis, `liftUpFunctions` moved the expressions computed before the window functions above the sorting for them. The sorting materializes sparse columns, so functions that see the representation, like `byteSize`, returned other values when the expressions were computed below it. Related: #126880 (comment) Co-authored-by: yakov <[email protected]>
`03322_view_over_parameterized_view` leaves the parameterized view `test_1.v` and an interrupted `02703_row_policy_for_database` leaks `ON test_1.* USING 1`. A parameterized view has no columns until its parameters are given, so `SELECT 1 FROM v` fails the access check for any user (Code 497), and a filter naming a column fails with an unknown identifier, which dropped a valid policy. Red on ClickHouse#126880 and ClickHouse#127094. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ssions-degradation-fix-380d Co-authored-by: yakov <[email protected]>
Closes: https://github.com/ClickHouse/clickhouse-private/issues/78134
Related: #126558
Related: #126882
This fixes a memory usage degradation of the analyzer compared with the old query analysis.
With the analyzer, the projection is computed after the window functions, so the
Windowstep buffers the columns the projection reads. ForSELECT toUInt64OrZero(splitByString('@', c2)[2]) AS v, count() OVER () AS n FROM t1, the window step buffers theStringcolumnc2instead of aUInt64. The old query analysis computed such expressions before the window functions (appendSelectSkipWindowExpressionsinExpressionAnalyzer), and since 26.9 it cannot be selected anymore.This adds the second-pass query plan optimization
pushDownExpressionsBelowWindow, with the settingquery_plan_push_down_expressions_below_window(enabled by default). It looks at theExpressionorFilterstep directly above aWindowstep. AFilterwith anandcondition is skipped: it applies the arguments one after another and computes its other expressions only for the rows that passed the previous ones. It moves the expressions there below theWindowsteps, and above the lowestSortingstep for them, where the old query analysis computed them (liftUpFunctionsmoved them there), if all of these hold:String/FixedStringcolumn;FixedString(alsoNullable) no larger than the smallest value of the column (the 8-byte offset of aString,NforFixedString(N));The rewrite is the same as in
pushDownVolumeReducingFunctions:ActionsDAG::splitof the parent, and a new step below that is merged into theExpressionstep there. It runs in the second pass, so that it sees the plan after the first pass moved and merged the expressions.Unlike the old query analysis, it keeps the results of the analyzer: stateful (
runningDifference), non-deterministic in the scope of the query (blockSize,rand) and side-effect (sleepEachRow) functions,arrayJoin, and lazily executed arguments of short-circuit functions are not moved. Otherwise it follows the old query analysis. The corner cases where moving an expression can use more memory or CPU than computing it after the window functions are not handled here: sparse or shared columns, aLIMITabove the window functions, and others. Computing the expressions below the window sorting as well, which would also reduce the memory of the sorting, is left for the improvements. They are tracked in #126882, and #126558 is the source of truth for the follow-up PRs, with the checks and a test for each case.The original query over a
MergeTreetable of 10 million rows of about 160-byte strings, withmax_threads = 1, peaks at 935 MiB (most of it theDISTINCTset) instead of 2.38 GiB. Withmax_memory_usage = 1e9it fails without the optimization.The test
05407_window_functions_push_down_expressions_below_windowchecks the plan, the memory usage and the results. It passed 40 runs with randomized settings, and all 307 stateless tests that use window functions orQUALIFYpass locally.Unrelated CI failure:
Stress test (amd_tsan)aborted withReceived signal 6 (internal) (STID: 4362-110a)inReplicatedMergeTreeQueue::pullLogsToQueue, after the test05230_merge_entry_for_local_part_missing_in_zookeeperwrote a merge entry with an empty part name into ZooKeeper (Unexpected part name: for format version: 1,BAD_DATA_PART_NAME) when fault injection had failed itsINSERT. The same abort happened on master and four other PRs on 2026-10-21. The fix is #127123.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix the memory usage of queries with window functions with the analyzer: an expression of a
StringorFixedStringcolumn with a small fixed-size result, such astoUInt64OrZero(splitByString('@', s)[2])orlength(s), is computed before the window functions again, as with the old query analysis, so the window functions buffer the result instead of the column. Controlled by the new settingquery_plan_push_down_expressions_below_window.Workflow [PR]
Sync PR [sync-upstream/pr/126880]