Repository navigation
Resolve ALIAS columns lazily in the analyzer - #123926
Conversation
The analyzer built and resolved the expressions of all `ALIAS` columns of a table on the first lookup of any column. Since the `bucketed` schema of `system.metric_log` became the default (it has about 2000 `ALIAS` columns), every query over `metric_log` spent most of its time in the analysis, and `03540_system_dashboards` began to time out. Now the expression of an `ALIAS` column is built and resolved on the first request of this column (`AnalysisTableExpressionData::tryGetColumnNode`). Also calculate the hash of the serialization of `Enum` once, in the constructor of `DataTypeEnum`, instead of on every request of the serialization. The `Enum16` key of the `metrics` map in `metric_log` has thousands of values, and the serialization is requested per part and per block while reading. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=3ead9595c864fca894685b3d6290c45187fd4332&name_0=MasterCI&name_1=Stateless%20tests%20%28amd_tsan%2C%20s3%20storage%2C%20sequential%2C%201%2F2%29 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Workflow [PR], commit [c9d8b9f] Summary: ✅
AI ReviewSummaryThis PR defers analysis of table Final Verdict
LLVM Coverage ReportMeasured on commit c9d8b9f.
Changed lines: Changed C/C++ lines covered: 95/99 (95.96%) · Uncovered code |
|
📊 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
|
…esolution The analyzer now resolves the expression of an `ALIAS` column only when the query uses this column, so a query over `t_alias` that does not read the dictionary alias no longer falls back to local execution; it distributes. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=123926&sha=dd088d1afb6b33972a58670e59722cb1269c9ed7&name_0=PR&name_1=Integration%20tests%20%28amd_llvm_coverage%2C%202%2F8%29 PR: #123926 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units265 translation units recompiled, 1903 s compile time in total, 265 of them have a recent master baseline. |
…or `REPLACE` `getMatchedColumnNodesWithNames` resolved every matched column, so `SELECT * EXCEPT (alias_column)` or `SELECT * REPLACE (1 AS alias_column)` still analyzed the expression of the ALIAS column, although the projection discards or substitutes it. Now a column that `resolveMatcher` will remove by `EXCEPT` or substitute by `REPLACE` (with no `APPLY` before them) gets a plain `ColumnNode` instead of `AnalysisTableExpressionData::tryGetColumnNode`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Resolve the conflict in `QueryAnalyzer::initializeTableExpressionData`: master (ClickHouse#123926) builds `ALIAS` column expressions lazily in the `alias_column_resolver`, so the column matcher expansion and the late cycle check of this PR moved there. Co-Authored-By: Claude Opus 5.5 <[email protected]>
ClickHouse#123926 resolves the expression of each ALIAS column lazily, in its own scope, on the first lookup of that column, so a later-declared ALIAS column is no longer resolved in the scope of the column that reads it. That fixes the wrong values this PR was fixing, and the source change of this PR is dropped in favor of the master version. The test still covers the case: it fails on master with the `src/Analyzer` part of ClickHouse#123926 reverted and passes on master. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@alexey-milovidov — ClickGap found the following in this PR:
Close anything that's wrong or already addressed. |
…projection alias With analyzer_compatibility_join_using_top_level_identifier = 1, try_resolve_identifier_from_query_projection copies the AnalysisTableExpressionData of the left table expression. When the column map already exists (an earlier USING key such as `id` built it), the copy shares the ColumnNodes but owns a separate unresolved_alias_columns set. The ALIAS column is then resolved through the copy, and resolved again through the original: the second call replaces the resolved expression with a fresh unresolved one, and getResultType() throws "Function node with name 'multiply' is not resolved" (UNSUPPORTED_METHOD). The populator creates ALIAS column nodes without an expression and the resolver is the only code that sets one, so resolve only nodes that have no expression yet. The per-object set still decides whether this object has requested the column. Regression from ClickHouse#123926; master only. Closes ClickHouse#124842 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…expression is resolved An ALIAS column whose expression reads the column itself through a subcolumn, e.g. `c0 Nullable(IPv6) ALIAS c0.null`, made a SELECT of it run until the server was killed. On 26.10 such a column is accepted by CREATE and ALTER; older releases reach it through ATTACH and table function structures (format, remote). It was found by La Casa Del Dolor on master: a read through clusterAllReplicas of a refreshable materialized view with such a column OOM-killed the server. ALIAS expressions are resolved lazily (ClickHouse#123926): tryGetColumnNode erases the column from unresolved_alias_columns and calls the resolver, which sets the node's expression and resolves it against the same table data. Resolving `c0.null` calls tryGetSubcolumnInfo -> tryGetColumnNode("c0"), which no longer finds the column unresolved and returns the node that is being resolved; wrapExpressionNodeInSubcolumn then makes getSubcolumn(<c0 node>, 'null') part of that node's own expression. The query tree is cyclic, and validateTreeSize, an iterative DFS that memoizes a node only after its children, grows its stack without bound. Analysis is not cancellable, so max_execution_time, max_memory_usage, max_server_memory_usage and KILL QUERY do not stop it. A direct cycle (`c0 ALIAS c0`) overflows the stack in a recursive walker instead (TOO_DEEP_RECURSION). The resolver now pushes the column name to AnalysisTableExpressionData::alias_columns_in_resolution before building the expression and pops it on scope exit, and tryGetColumnNode throws CYCLIC_ALIASES with the chain when a requested column is in that list. The resolver captures the original data, which is also the alias resolution data of every re-entrant lookup, so the check also works when a copy of the data started the resolution (the JOIN USING projection alias path); that is why it precedes the unresolved_alias_columns branch. Every cycle (through subcolumns, other ALIAS columns, functions) comes back through this lookup, so tables, Merge, remote/cluster*, format, materialized views and stored definitions are all covered. The definition-time check for such columns is ClickHouse#124850. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Since the
bucketedschema ofsystem.metric_logbecame the default, every query overmetric_logbecame several times slower. The test03540_system_dashboardswent from about 110 seconds to 300-600 seconds and began to time out inStateless tests (amd_tsan, s3 storage, sequential, 1/2): https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=3ead9595c864fca894685b3d6290c45187fd4332&name_0=MasterCI&name_1=Stateless%20tests%20%28amd_tsan%2C%20s3%20storage%2C%20sequential%2C%201%2F2%29There were two reasons:
ALIAScolumns of a table (about 2000 formetric_log) on the first lookup of any column. Now the expression of anALIAScolumn is built and resolved only when this column is used (AnalysisTableExpressionData::tryGetColumnNode).Enumhashed all values of theEnumon every request, and theEnum16key of themetricsmap has thousands of values, requested per part and per block while reading. Now the hash is calculated once in the constructor ofDataTypeEnum.Locally, a query reading one metric from
system.metric_logtakes 8 ms instead of 91 ms (the same as with thewideschema), and all queries ofsystem.dashboardstake 21.8 s instead of 53 s (16.4 s with thewideschema).Related: #118318
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Faster analysis of queries to tables with many
ALIAScolumns, such assystem.metric_log: the expression of anALIAScolumn is analyzed only if the query uses this column.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/123926]
Version info
26.10.1.1905-master(included in26.10and later)