Repository navigation
Conversation
…n would exceed max_expanded_ast_elements A column that each nested subquery level builds as `x + x` from the level below has a linear DAG but 2^depth paths. Each scope is small, so the analyzer's `max_expanded_ast_elements` never fires, but the first plan pass merges the levels into the filter that reads the column, and the condition then expands to 2^(depth + 2) + 1 nodes. Filter push-down over JOIN (`splitActionsForJOINFilterPushDown`), index analysis (`buildFilterActionsDAG` in `applyFilters`, `cloneDAGWithInversionPushDown`) and projection analysis (`resolveMatchedInputs`) walk it once per path, so at depth 36 planning allocates past `max_server_memory_usage` and the process is OOM-killed (master and 26.10.2.103; also hit by the stress fuzzer on a 05317 variant). Option A of the three alternatives requested in ClickHouse#123724: the places that build a filter condition from the steps below it refuse to do so when the result would expand beyond `max_expanded_ast_elements`: - `tryMergeExpressions` (Filter + Expression) and `tryMergeFilters` keep the steps separate; - `optimizePrimaryKeyConditionAndLimit` does not add such a composed filter for index analysis (the FilterStep still runs); - `QueryDAG::build` does not build the projection DAG, so no projection is used for the query. The size is computed by a memoized post-order walk (`getExpandedTreeSize[AfterMerge]` on `visitNodesPostOrder`); the after-merge variant binds the inputs of the second DAG to the outputs of the first exactly as `ActionsDAG::mergeInplace` does, so nothing is merged to measure. A limit of 0 means no limit, which keeps the default-constructed `ExtraSettings{}` used by lazy materialization unchanged. Below the limit plans are unchanged. The JOIN-side compositions and the read-side conjunction of filters that were each bounded are left as they are; the measured JOIN shapes are fixed because their join filters reach the chain through the same Filter + Expression merge. Related: ClickHouse#123724 Co-Authored-By: Claude Opus 5.5 <[email protected]>
05872 observed only the Filter + Expression merge check. It now also covers, at depth 18: - two stacked filters (`query_plan_split_filter = 0`) stay separate when their merged condition would exceed the limit; - index analysis on a MergeTree table gets no condition for such a filter (`Condition: true` instead of `(val in [0, 0])`); - projection analysis does not use the aggregate projection for it. Each of these fails when its check alone is removed; all fail on master. `query_plan_merge_filters` is pinned to 1 on the stacked-filter query, since with 0 the filters are not merged with or without the check. Also reword the doc line of `getExpandedTreeSize`: it counts DAG nodes, not the analyzer's query tree nodes. Related: ClickHouse#123724 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ze limit in the projection reject reason With parallel replicas the initiator's plan nests the local MergeTree read under the parallel-replicas step, or (with `parallel_replicas_local_plan = 0`) has no local read at all, so the primary key condition and the projection choice that the two MergeTree EXPLAINs inspect are not there. This happens with query-based and plan-based parallel replicas alike, so both statements set `enable_parallel_replicas = 0`. `QueryDAG::build` now also fails when the filter condition exceeds `max_expanded_ast_elements`, so the reject reason reported for the projection (visible in `PROJECTION_NOT_USED` under `force_optimize_projection = 1`) names that case too. Related: ClickHouse#123724 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 3 rounds, 4 findings), plus my own review (3 nits, 1 major).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261124-014700 |
|
cc @CurtizJ, could you review this? Option A for issue 123724: query plan optimizations skip merging a filter with the expressions below it, and skip it for index and projection analysis, when its condition would exceed max_expanded_ast_elements. |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-40:20261124-003800 |
|
Workflow [PR], commit [76c8f90] Summary: ✅
AI ReviewSummaryBounds filter expansion during expression merging, index analysis and projection matching using Missing context / blind spots
Final VerdictStatus: ✅ Approve |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes72 object files changed (+37.89 KiB total), 0 added.
716 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 units174 translation units recompiled, 1213 s compile time in total, 174 of them have a recent master baseline. |
Sources take the filters they are given as all the filters below the next step of the walk. When a filter was skipped for its size, the walk went on: - a `LIMIT` above it still reached the source, and `numbers`, `primes` and `generate_series` (which push a limit only when the filter is absent or exactly ranged) stopped after `limit + offset` rows before the skipped filter ran, returning too few rows; - `updateQueryConditionCacheForMemory` tagged the nearest `FilterStep` (the skipped one) with the hash of the filters above it, so its rejections were stored under another condition's key and later queries lost rows. Now the walk stops there, as it does at any step it cannot see through. Only conditions above `max_expanded_ast_elements` are affected. Co-Authored-By: Claude Opus 5.5 <[email protected]>
An `indexHint` keeps its condition in its own DAG, not in its children, and `ActionsDAG::mergeInplace` rebinds the inputs of an implied hint to the expressions of the DAG below. Index analysis (`buildFilterActionsDAGImpl`, `cloneDAGWithInversionPushDown`) walks that condition, so a hint counted as one node let the expansion through. `HAVING a36 < max(v) AND max(v) < 5` over the 36-level `a + a` chain: with `optimize_and_compare_chain` an implied `indexHint(a36 < 5)` is pushed below `GROUP BY`, passes the merge check, and the read then spends 11 GiB in `cloneDAGWithInversionPushDown` (`MEMORY_LIMIT_EXCEEDED`, as on master). With the hint counted it takes 0.2 s over `numbers()` and MergeTree, with the same result as master at depth 12. `getExpandedTreeSize` now adds the expanded size of the outputs of a hint's DAG, and `getExpandedTreeSizeAfterMerge` binds the inputs of implied hints to the outputs of `first` by name, as `mergeInplace` does. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #123724
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed an out-of-memory kill while planning a query whose nested subqueries reuse a column more than once per level, e.g. 36 nested levels of
SELECT a + a AS b FROM (...)under aWHERE. Query plan optimizations no longer merge a filter with the expressions below it, and do not use it for index or projection analysis, when its condition would then exceedmax_expanded_ast_elements.Description
Option A of the three alternatives requested in #123724 (comment). Found by Stress test (amd_msan): https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=100173&sha=b00d774e18fa8a5be47f1f2c8fcc26290d8fc13a&name_0=PR&name_1=Stress%20test%20%28amd_msan%29
On 26.10.2.103,
SELECT count() FROM (<36 nested SELECT a + a>) WHERE a36 <= 0is killed by the OOM killer during planning.Each nested scope passes
max_expanded_ast_elements, but merging the levels into the filter builds a 2^38-node condition, and filter push-down, index analysis and projection analysis walk it once per path; a 12 GiBmax_server_memory_usagedoes not stop them.A memoized
getExpandedTreeSize(onvisitNodesPostOrder) gates the four places that build a filter condition from the steps below it:tryMergeExpressions(Filter + Expression),tryMergeFilters,composeinoptimizePrimaryKeyConditionAndLimit, andQueryDAG::buildfor projections.Master vs this PR (debug, 12 GiB cap):
WHEREovernumbers()query_plan_merge_expressions = 0MEMORY_LIMIT_EXCEEDEDPARTITION BY(minmax_count projection)JOIN ONconjunct over the chainindexHintfromHAVING, pushed belowGROUP BYMEMORY_LIMIT_EXCEEDEDJOIN filters and runtime filters reach the chain through the same Filter + Expression merge, so they stay above it. The JOIN-side compositions themselves (
fix_predicate_for_join_logical_step, join graph absorption) and the read-side conjunction inapplyFiltersare unchanged.Price: below the limit plans are unchanged (1192 related stateless tests match master; planning a 300-level nested
WHEREtakes the same time). Above it the filter stays put: no push-down, index analysis orPREWHEREfor it (its small conjuncts included), no index analysis orLIMITpush-down above it, and no projection for the query.Workflow [PR]
Sync PR [sync-upstream/pr/134010]