Repository navigation
Bound the expansion of WITH elements by max_expanded_ast_elements - #122362
Conversation
`ApplyWithAliasVisitor` copies every element of a `WITH` list into the subqueries of the elements after it, together with the copies those carry already, so the query doubles with each element. `ApplyWithSubqueryVisitor` then pastes a common table expression into every place that reads it, so a chain of CTEs each reading the previous one twice doubles as well. Neither had a bound: the AST fuzzer's `EXPLAIN AST optimize = 1` over 13 scalar subqueries grew the server to 46 GB RSS within a minute, ignoring the 10 s timeout, until the OOM killer stopped it. `CREATE VIEW` with a doubling CTE chain exhausts the memory the same way. Count the elements both visitors copy and throw `TOO_BIG_AST` past `max_expanded_ast_elements`, the limit the analyzer applies to the query tree. Definitions loaded from metadata stay unbounded, so a server never refuses to start over a view it accepted before. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=711f560311e8150e0898a4dad1ef6af19e8cc77a&name_0=MasterCI&name_1=AST%20fuzzer%20%28amd_debug%29 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
Workflow [PR], commit [5363321] Summary: ✅
AI ReviewSummaryThis PR threads Final Verdict✅ No new blockers or majors. The previously reported review issues appear addressed in the current code. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes23 object files changed (+8.43 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 units29 translation units recompiled, 245 s compile time in total, 28 of them have a recent master baseline. |
|
📊 Cloud Performance Report ✅ AI verdict: no significant changes detected. K_source=6 K_base=30 flagged=0/65 clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
Resolved the conflict in the `Setting` declarations of `InterpreterInsertQuery.cpp` by keeping both sides. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…xt in `DDLDependencyVisitor` `checkTableCanBeAddedWithNoCyclicDependencies` expands the query of a materialized view to collect its dependencies. The limit was read from the global context, so a session override of `max_expanded_ast_elements` was ignored there. Pass the limit of the current query down instead. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…lements` With `enable_global_with_statement`, the `WITH` list of the first branch of a `UNION` is cloned into every other branch, which allocates O(with_list * branches) AST nodes before any limit is checked. Count these copies with `ExpandedASTBudget` as well. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… the fix, cover new cases The bugfix validation jobs ran into their 20-minute timeout: a server without the bound never finishes expanding a chain of 30 elements. Use shorter chains with a lower `max_expanded_ast_elements`, so that the old server finishes quickly and fails the test (it reports `BAD_ARGUMENTS` or no error instead of `TOO_BIG_AST`). Also cover the `UNION` propagation and the dependency collection of a materialized view. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=122362&sha=873da41dc76a2aa020eb86d6aec484183dbbcc6f&name_0=PR&name_1=Bugfix%20validation%20%28functional%20tests%2C%20amd64%29 #122362 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…y `max_expanded_ast_elements` A full user-supplied `ATTACH VIEW ... AS SELECT` runs under `LoadingStrictnessLevel::ATTACH`, so `mode <= CREATE` left it unbounded. Use `isFreshTableDefinition`, so only replayed metadata stays unbounded. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…nsion_bound` Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Both already have fixes:
|
04401_http_query_parameters_not_leaked_to_logs bounded its six system.text_log reads only by `event_time >= now() - INTERVAL 600 SECOND`, so every read scanned the last ten minutes of the whole server's log. On the parallel `Stateless tests (arm_binary, ParallelReplicas, s3 storage, parallel)` server that is ~4.7M rows / ~120 MB of `message` per scan, and with `remote_filesystem_read_method = 'read'`, `enable_filesystem_cache = 0` and 1 MiB remote read tasks the five `message LIKE` scans took 97-222 s each, so the test hit the 600 s timeout (ClickHouse#123752, ClickHouse#122362). Take `toUnixTimestamp(now())` before the first request and bound all six reads with `event_time >= toDateTime(test_start)`, as ClickHouse#123652 did for 05061. Every asserted line is logged after that point, so the reference is unchanged; text_log is ordered by (event_date, event_time), so the bound prunes the read to the test's own granules. Locally, with 681K rows in the ten-minute window, a scan reads 381 rows instead of 681,982. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…st `max_expanded_ast_elements` `ExpandedASTBudget` counted only the subtrees copied within one pass, and every pass created its own budget, so `max_expanded_ast_elements` acted as a per-pass delta: a large query could still receive a full limit of copies, and the alias propagation and the common table expression substitution could each spend the whole limit. The budget now also charges the query being expanded, on its first copy, so the limit bounds the size of the whole expanded query. A pass that copies nothing does not walk the query at all. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…lements_expansion_bound` Co-Authored-By: Claude Opus 5.5 <[email protected]>
…he fragments of a query `InterpreterUpdateQuery` expands the predicate and the assignments in separate calls, and `InterpreterAlterQuery` does the same for `MODIFY QUERY` and every mutation expression. Each call had its own `ExpandedASTBudget`, so splitting a doubling `WITH` chain across sibling fragments let one query grow past the limit. Now one budget, rooted at the whole query, is threaded through all of these calls. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=122362&sha=5e1f5a70f6816d5d397a77706d2e8f4316cc8e02&name_0=PR ( |
|
This job ran on The fix is #113605: it runs the free-space check before every job on macOS too, so a host this full refuses the job and reboots instead of taking it. It is waiting for review and deploy by @ leshikus. #124316 is the diagnostic that prints what fills the disk on such a host. |
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/PRs/122362/0ca1a4051cf860aa7dfef2f0016e68efe859ead5/pr/ast_fuzzer_amd_release_oracle/job.log ( |
|
Acknowledged. This is a real wrong result, not an oracle false positive, and it does not need SELECT * FROM (SELECT concat(initializeAggregation('groupArrayState', 'x'), toString(number)) AS r, concat(initializeAggregation('groupArrayState', 'y'), toString(number)) AS r FROM numbers(2))returns |
… is attached `ExpandedASTBudget::replace` refunds the elements of the node being replaced, assuming they belong to the counted query. The table-position substitution in `ApplyWithSubqueryVisitor` detached `database_and_table_name` before calling it, so when that call was the first one, the root was counted without the table name and the table name was then refunded a second time, undercounting the expanded AST. Call `replace` before detaching the table name. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 The |
|
The fix is in #124867: the analyzer constant comparison ( |
# Conflicts: # src/Interpreters/InterpreterCreateQuery.cpp
…Visitor` When a `SELECT` has no `WITH` clause, `ApplyWithAliasVisitor` creates a new `ASTExpressionList` for the propagated aliases. That node is part of the expanded query too, so it is now charged against `max_expanded_ast_elements` together with the copies it holds. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 Merged master to resolve the conflict in |
… `WITH` expansion The setting now also limits the substitution of `WITH` elements in views, `ATTACH`, `MODIFY QUERY`, mutations, distributed `INSERT ... SELECT` and `UNION` propagation, so mention it in the setting description (the docs page is generated from it). Co-Authored-By: Claude Opus 5.5 <[email protected]>
… `Replicated` database A follower replica executes a `CREATE VIEW` under `SECONDARY_CREATE`, so `isFreshTableDefinition` returned false there and the expansion of `WITH` elements was not bounded by `max_expanded_ast_elements`. For an ordinary view there is no earlier bounded pass on that replica, so a doubling CTE chain could still exhaust the memory of every follower. Keep the exemption only for a restore from a backup and for the recovery from stored metadata. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…nt-subquery-loading-dependency" This reverts commit 659cc1c, reversing changes made to f75df10. Conflicts resolved by removing only what ClickHouse#114923 added: the narrow qualification pass in AddDefaultDatabaseVisitor.h is removed together with the parts ClickHouse#114908 later added inside it; InterpreterCreateQuery.cpp keeps the expansion bound of ClickHouse#122362 on the neighbouring call; the string overload of qualifyDictionaryNameWithDatabase in ExternalDictionariesLoader is kept, because the dictionary usage record of ClickHouse#121567 is built on it. 04908_constraint_subquery_loading_dependency.sh (edited by ClickHouse#124408) is deleted with the rest of the test. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The AST fuzzer on master killed the server with the OOM killer (46 GB RSS within a minute) on
EXPLAIN AST optimize = 1over aWITHlist of 13 scalar subqueries: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=711f560311e8150e0898a4dad1ef6af19e8cc77a&name_0=MasterCI&name_1=AST%20fuzzer%20%28amd_debug%29ApplyWithAliasVisitorcopies every element of aWITHlist into the subqueries of the elements after it, together with the copies those already carry, so the query doubles with each element.ApplyWithSubqueryVisitorthen pastes a common table expression into every place that reads it, so a chain of CTEs each reading the previous one twice doubles as well. Neither visitor had a bound, and neither checks for cancellation, so the query ignored its 10 s timeout. The same happens outsideEXPLAIN:CREATE VIEWwith a doubling CTE chain exhausts the memory inInterpreterCreateQuerybefore the analyzer's own "Query tree is too big" check can run.Both visitors now count the elements they copy and throw
TOO_BIG_ASTpastmax_expanded_ast_elements, the limit the analyzer applies to the query tree. The check runs before each copy, so the work stays proportional to the limit. Definitions replayed from metadata stay unbounded (a fullATTACH ... AS SELECTis user input, so it is bounded), so a server never refuses to start over a view it accepted before. On the fuzzer's query and on both new test cases, the release build of master runs out of an 8 GB address-space cap after 12 to 28 s; with this change they fail in 0.3 s withTOO_BIG_AST.Related: #118756 (an earlier, closed attempt that bounded only
ApplyWithAliasVisitor; this query also needs the CTE substitution bounded)Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
A
WITHlist whose elements refer to the preceding ones, or a chain of common table expressions that read the previous one more than once, now fails withTOO_BIG_ASTpastmax_expanded_ast_elementsinstead of exhausting the server's memory inEXPLAIN AST optimize = 1,CREATE VIEW, a fullATTACH VIEW ... AS SELECTandALTER TABLE ... MODIFY QUERY.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/122362]
Version info
26.10.1.2342-master(included in26.10and later)