Repository navigation
Conversation
Nested subqueries that each double a column (`SELECT a + a AS b FROM (...)`) compose into one filter DAG with O(depth) nodes but 2^depth root-to-leaf paths. The DAG itself and its execution are linear; the consumers below rebuild a name from the children's names or walk the predicate once per path, so planning needed exponential time or memory (OOM-killed at 36 levels with default settings). Each of them now does O(nodes) work: 1. `ActionsDAG::nameForRebuiltFunction` / `MAX_GENERATED_NAME_SIZE` (64 KiB): a rebuilt function keeps its generated `f(children)` name unless that name would exceed the bound, then it takes the source node's name. 2. `buildFilterActionsDAGImpl` names rebuilt functions that way. 3. `cloneDAGWithInversionPushDown` (KeyCondition) clones each node once per (need_inversion, boolean_context) and names the clones the same way. 4. `RPNBuilderTreeNode::tryGetColumnName(max_size)`: KeyCondition, the FINAL sorting-key check, the selectivity estimator and `isConditionGood` stop building a name once it is longer than every name it is compared with. 5. MergeTreeWhereOptimizer: `collectColumns`, `cannotBeMoved`, `isExpensiveExpression`, `conditionMayThrow` and the FINAL walk visit each DAG node once. 6. The JIT function name (`CompileDAG::dump`) is cut at the bound, with the DAG hash appended to keep it unique. 7. `findLazyExecutedNodes` remembers nodes found not lazily executed; this is the same change as ClickHouse#133893. 8. Partial JOIN filter push-down: `onlyDependsOnAvailableColumns` and `dependsOnSomeColumn` use `allReachableNodes` / `anyReachableNode`. 9. The PREWHERE condition log label (`Condition::toString`) uses the bounded name, falling back to the DAG node's name. Every name within the bound and every walk result is unchanged. Related: ClickHouse#123724 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Eleven queries whose filters have 24 or 40 doublings of one column: every memory-bound one fails with MEMORY_LIMIT_EXCEEDED on master under a 200 MB cap, and the two partial JOIN push-down ones plan for hours there. Related: ClickHouse#123724 Co-Authored-By: Claude Opus 5.5 <[email protected]>
`cloneDAGWithInversionPushDown` names a comparison it rebuilds (inverted or not) like every other rebuilt function. Related: ClickHouse#123724 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review (Gate B) - adjudication
Re-review after the fix: no findings. |
|
cc @Manerone, could you review this? Option C for issue #123724: each planner consumer of a filter DAG (filter push-down names, index analysis, PREWHERE, JIT, short-circuit analysis, partial JOIN push-down) processes the filter once per node instead of once per path; the JIT part cuts the CompileDAG::dump name at 64 KiB and appends the DAG hash. |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261124-065400 |
|
Workflow [PR], commit [fb3e0e5] Summary: ✅
AI ReviewSummaryThe PR bounds generated expression labels and memoizes planner walks over shared filter DAGs, preventing exponential planning work for the covered query shapes. Reviewed commit Consumers traced for the changed labels: source filtering and Missing context / blind spots
Final VerdictStatus: ✅ Approve |
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. Filter push-down, index analysis,PREWHERE, JIT compilation, short-circuit analysis and partialJOINfilter push-down now process such a filter once per node instead of once per path.Description
Option C of three alternatives requested in #123724 (comment) (A: #134010, B: #134034). 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.5.57 and 26.8.37.168,
SELECT count() FROM (<36 nested SELECT a + a>) WHERE a36 <= 0is OOM-killed during planning despite the memory limit.The filter has few nodes but 2^36 paths. Every optimization still runs; each consumer that walked paths or concatenated child names is now linear:
buildFilterActionsDAGandcloneDAGWithInversionPushDown(now memoized) generate names up to 64 KiB, then keep the source name.RPNBuilderstops a name once it exceeds every name it is compared with (KeyCondition,FINALkey check, estimator,isConditionGood), thePREWHERElog label at 64 KiB.PREWHEREanalysis visits each node once; the JIT name is cut at 64 KiB plus the DAG hash.findLazyExecutedNodes: the same change as Visit each node once when finding lazily executed short-circuit arguments #133893.JOINpush-down usesallReachableNodes/anyReachableNode.Nine places in 13 files, ~300 lines; reverting any one alone turns a repro red.
Debug build, depth 36:
numbers(), also merge expressions off / JIT / a comparison chainMEMORY_LIMIT_EXCEEDEDPREWHERE+ statistics,FINALJOIN: non-equiON, 05317 withjoin_use_nulls = 1, the fuzzed query (its ownILLEGAL_DIVISION)AND/ORJOINpush-downORNot fixed (no repro reaches them): skip-index name lookups, text-index direct read,
use_query_condition_cache_for_time_conditions = 1, Graphite / row-policyFINALhelpers, the Cascades estimator, EXPLAIN pretty (#115921),and/orover shared subtrees.Price: names within 64 KiB and all walk results are unchanged; 1845 related stateless tests match master.
Workflow [PR]
Sync PR [sync-upstream/pr/134059]