Repository navigation
Conversation
`createComputeAliasColumnsStep` merged one DAG per ALIAS column into a DAG
over the storage header with `ActionsDAG::mergeNodes`, and every call
rebuilt a `std::string`-keyed name index over all nodes accumulated so far,
so reading N aliases cost O(N^2). The output list was then assembled with
`addOrReplaceInOutputs` per node, which rescans all outputs on each call,
a second O((H + N)^2) term.
Add a `mergeNodes` overload that takes a caller-held `string_view` name
index: it is built once if empty and extended with the nodes each merge
moves in. `mergeNodes` only appends nodes, so the extended index equals the
one a rebuild would produce (first node per name wins). The old signature
is a wrapper over the overload. The output list is assembled in one pass
with the same result as the sequential `addOrReplaceInOutputs` calls:
existing outputs keep their positions and take the last node of the same
name, new names are appended in first-occurrence order holding the last
node with that name.
Since 26.10 `system.metric_log` has about 2,300 ALIAS columns, so
`DESCRIBE (SELECT hostname, event_time, COLUMNS('^(ProfileEvent_|CurrentMetric_)') FROM system.metric_log)`
took about 1.4 s of planning.
Closes ClickHouse#128155
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Extend `wide_table_nested_select.xml` with a copy of its 500-column table that also has 500 ALIAS columns, and time `DESCRIBE` and `SELECT` of all of them. Locally (release build, 7 runs, perf.py): DESCRIBE 29.8 -> 12.2 ms, SELECT 37.5 -> 19.5 ms; the three existing queries are within 2 %. Related to ClickHouse#128155 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; 0 findings) plus my own review (3 nits).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-11:20261102-123700 |
|
cc @yariks5s @novikd, could you review this and add the |
|
Workflow [PR], commit [3445e17] Summary: ✅
AI ReviewSummaryPR #128634 removes quadratic name-index rebuilding and output scans when planning reads of many Final VerdictStatus: ✅ Approve. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units1288 translation units recompiled, 10493 s compile time in total, 1288 of them have a recent master baseline. Median compile-time ratio to the baselines is ×1.10 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
Closes: #128155
Related: #115380
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Planning a query that reads many
ALIAScolumns is now linear in their number instead of quadratic.DESCRIBE (SELECT hostname, event_time, COLUMNS('^(ProfileEvent_|CurrentMetric_)') FROM system.metric_log), which reads about 2,300ALIAScolumns since 26.10, now takes 0.2 s instead of 1.4 s. Closes #128155.Description
Reading N
ALIAScolumns cost O(N^2) in query planning (#128155).createComputeAliasColumnsStepmerges one DAG per alias withActionsDAG::mergeNodes, and every call rebuilt astd::string-keyed name index over all nodes merged so far. The outputs were then assembled with oneaddOrReplaceInOutputscall per node, each scanning all outputs.The change:
mergeNodesoverload that takes a caller-heldstring_viewname index, built once and extended with the merged nodes.mergeNodesonly appends nodes, so this equals the rebuilt index. The old signature is a wrapper.createComputeAliasColumnsStepkeeps the index across the loop and assembles the outputs in one pass, with the same result as theaddOrReplaceInOutputsloop.Validation on release builds, with the issue's synthetic tables (min of 5 runs):
system.metric_log(2375)Query results and
EXPLAIN actions = 1, header = 1, compact = 0(also withoptimize = 0) are byte-identical before and after, for alias chains, shared subexpressions, constant aliases,Nullable,LowCardinality,Array,MapandTuplealiases,remote()andsystem.metric_log. The alias-related stateless tests pass.tests/performance/wide_table_nested_select.xmlgets a table with 500ALIAScolumns: locally, DESCRIBE goes from 29.8 to 12.2 ms and SELECT from 37.5 to 19.5 ms, and the existing queries are unchanged.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/128634]