Repository navigation
Conversation
…r number
`ReadFromMerge` handled the `ALIAS` columns a child is asked for one at a
time. `getModifiedQueryInfo` ran a separate `QueryAnalysisPass` per alias
(each one rebuilds the analysis data of all the child's columns) and a
separate `PlannerActionsVisitor` visit per column (each one indexes the
whole growing DAG). `convertAndFilterSourceStream` then ran another pass and
added a separate `ExpressionStep` per alias, a chain of N steps with growing
headers that `QueryPlan::optimize` merges one by one. All of it is quadratic
in the number of aliases, and runs inside one child iteration of
`createChildrenPlans`, where the time limit is not checked.
Since `system.metric_log` has about 2,480 `ALIAS` columns over one map and
`system.all_metric_log` declares the same columns `MATERIALIZED`, they are
ordinary columns of `merge('system', 'metric_log')`, and `*` reads all of them
from `metric_log` as aliases. The AST fuzzer's variants of that query in the
stress job's log export spent 64-72 minutes planning that child on amd_tsan
with `max_execution_time = 10`; on a debug build it takes about 100 s.
Resolve all aliases with one `QueryAnalysisPass` over a list, as
`replaceTableExpressionAndRemoveJoin` already does since ClickHouse#113140, visit the
columns once, and compute all aliases with one `ExpressionStep`. Its outputs
are assembled in one pass to be the same as one `addOrReplaceInOutputs` call
per alias, in order (every output of a name is replaced by the last alias of
that name, a new name is appended once), without rescanning the outputs for
each alias. One shared analyzer scope is safe for the aliases of one table:
`CREATE TABLE` and `ALTER` already validate all its column defaults in one
scope.
Debug build, default server config: the CI queries went from 98-107 s
(`TIMEOUT_EXCEEDED` with a 10 s limit) to 2.6-3.8 s. A child with N aliases:
N = 1000 3.65 s -> 0.15 s, N = 2000 14.2 s -> 0.31 s, N = 2500 22.3 s -> 0.40 s,
peak memory 519 MB -> 39 MB, identical results.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
… plan `convertAndFilterSourceStream` rebuilt every alias from the AST of its inlined expression. That AST qualifies physical columns with the generated table alias (`__table1.x`) and carries `AS <name>` for every inlined alias, so the analyzer can bind `__table1.x` to something named `__table1` instead of the table: with one pass over all aliases, another alias's `AS __table1` sits in the shared scope, and with one pass per alias (master) the child's own `__table1` column wins. Through `merge`, a child with `__table1 Tuple(x UInt64) ALIAS tuple(100)` and `y UInt64 ALIAS x + 2` returned 102 for `y` instead of 3 (also on 26.3, 26.8 and 26.10). Resolve the alias names against the child table expression instead, as `getModifiedQueryInfo` does, and inline their expressions with `ApplyAliasColumnExpressionsVisitor`: one pass over plain identifiers, no alias in the list, no AST round trip. `AliasData::expression` is then unused and removed. The regression test gains the `__table1` case. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ed one by one The wide query of 05872 now runs with max_memory_usage = 150000000. Planning the 2,500 aliases of the t_alias child one at a time peaks at 511 MB on a debug build (minimum of 10 randomized runs), so the query fails with MEMORY_LIMIT_EXCEEDED on such a tree (10/10 runs), while the single pass peaks at 36 MB (maximum of 50 randomized runs, 4x below the cap). Before, the test only caught the per-alias planning through its runtime on sanitizer builds. t_plain is now a Memory table: as a MergeTree table, random settings made it a Wide part in some runs, and reading its 2,500 column streams alone took up to 391 MB, which hid the planning cost. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ad of capping it 05872 now runs the wide query with a fixed query_id and checks its memory_usage in system.query_log (< 150000000), instead of running it under max_memory_usage = 150000000. max_memory_usage is not enforced for allocations through operator new, so a tree that plans the aliases one by one could pass the cap: without random settings the capped query on an unfixed debug build finished with 510,736,328 bytes logged and no exception. The logged peak is deterministic: 510-513 MB before the change and 36 MB after, in every run. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review (4 rounds)
Last round: no findings. |
|
cc @yakov-olkhovskiy, could you review this? ReadFromMerge ran a QueryAnalysisPass, an actions visit and an ExpressionStep per ALIAS column of a child, quadratic in their number (merge('system', 'metric_log') reads about 2,500 of them and planned for minutes, ignoring max_execution_time); it now resolves all alias names against the child in one pass and computes them in one step. |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261124-082900 |
|
Workflow [PR], commit [8862281] Summary: ❌
AI ReviewSummaryThe PR batches analysis and computation of a Final VerdictStatus: ✅ Approve |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes3 object files changed (+32.18 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 units21 translation units recompiled, 166 s compile time in total, 21 of them have a recent master baseline. |
…er_alias_over_subcolumn off A child's required column names are resolved as identifiers in the scope of the child's table expression, which carries the generated table alias (`__table1`). With `analyzer_compatibility_prefer_alias_over_subcolumn = 1` the analyzer prefers the alias-qualified reading of a multi-part identifier, so `__table1.x`, a subcolumn of a source table's ALIAS column named `__table1`, resolved to the column `x` of the table: `getModifiedQueryInfo` threw the LOGICAL_ERROR "Alias column __table1.x is not resolved" (on master too), and `convertAndFilterSourceStream` alone would have returned `x` instead of the subcolumn. The outer query had already resolved the name as the subcolumn, since its table alias is generated after resolution. The stress jobs hit it with 05872's capture case and the random setting. Both resolutions now use a copy of the context with the setting off (only when it is on); the copy is kept alive by the child plan, since functions resolved with it may refer to it while the plan runs. 05872 runs the capture query with the setting as well. Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI finish ledger - 8862281Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20261124-160028 |
Related: #114734
Related: #128155
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Planning a query over a
Mergetable or themergetable function is now linear, instead of quadratic, in the number of independentALIAScolumns it reads from a source table. For example,SELECT * FROM merge('system', 'metric_log')reads about 2,500ALIAScolumns ofsystem.metric_logand could plan for a long time, ignoringmax_execution_time.Description
In the stress job's log export, the AST fuzzer's
SELECT * FROM merge(REGEXP('(?i)SYSTEM'), 'metric_log')(and two variants) spent 64-72 min planning thesystem.metric_logchild onamd_tsanwithmax_execution_time = 10: 1, 2, 3.metric_loghas about 2,480ALIAScolumns thatsystem.all_metric_logdeclaresMATERIALIZED, somerge()makes them ordinary and*reads all of them frommetric_logas aliases.ReadFromMergehandled a child's aliases one at a time: aQueryAnalysisPass(each rebuilding the analysis data of all columns) and an actions visit per column ingetModifiedQueryInfo, then another pass and anExpressionStepper alias inconvertAndFilterSourceStream. Each part is quadratic, and the time limit is only checked between children.Both places now resolve the alias names with one
QueryAnalysisPassover a list, asreplaceTableExpressionAndRemoveJoindoes since #113140, and oneExpressionStepcomputes the aliases. The JOIN path is already one pass; direct reads of many aliases are #128634.Resolving by name also fixes a wrong result (present in 26.3-26.10): re-analyzing each inlined alias's AST let a source table's
ALIAScolumn named__table1, the generated table alias, capture__table1.x:y UInt64 ALIAS x + 2returned 102, not 3, throughmerge. The names are resolved withanalyzer_compatibility_prefer_alias_over_subcolumnoff, otherwise__table1.x, a subcolumn of that column, meant the columnxof the table (aLOGICAL_ERRORon master).Debug build, default server config: the three CI queries took 98-107 s (
TIMEOUT_EXCEEDED) and now 1.9-3.2 s. A child with 2,500 aliases: 22.3 s -> 0.48 s, same results. Merge-related stateless tests: same results. The.shtest generates its 2,500-column tables and checks the peak memory insystem.query_log(510 MB -> 36 MB).🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/134077]