Skip to content

Make planning of reads of many ALIAS columns linear in their number - #128634

Open
groeneai wants to merge 2 commits into
ClickHouse:masterfrom
groeneai:alias-columns-linear-planning
Open

groeneai wants to merge 2 commits into
ClickHouse:masterfrom
groeneai:alias-columns-linear-planning

Conversation

@groeneai

@groeneai groeneai commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #128155
Related: #115380

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Planning a query that reads many ALIAS columns 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,300 ALIAS columns since 26.10, now takes 0.2 s instead of 1.4 s. Closes #128155.

Description

Reading N ALIAS columns cost O(N^2) in query planning (#128155). createComputeAliasColumnsStep merges one DAG per alias with ActionsDAG::mergeNodes, and every call rebuilt a std::string-keyed name index over all nodes merged so far. The outputs were then assembled with one addOrReplaceInOutputs call per node, each scanning all outputs.

The change:

  • a mergeNodes overload that takes a caller-held string_view name index, built once and extended with the merged nodes. mergeNodes only appends nodes, so this equals the rebuilt index. The old signature is a wrapper.
  • createComputeAliasColumnsStep keeps the index across the loop and assembles the outputs in one pass, with the same result as the addOrReplaceInOutputs loop.

Validation on release builds, with the issue's synthetic tables (min of 5 runs):

Aliases read DESCRIBE before after
2000 582 ms 56 ms
4000 2524 ms 133 ms
8000 11485 ms 290 ms
system.metric_log (2375) 1384 ms 202 ms

Query results and EXPLAIN actions = 1, header = 1, compact = 0 (also with optimize = 0) are byte-identical before and after, for alias chains, shared subexpressions, constant aliases, Nullable, LowCardinality, Array, Map and Tuple aliases, remote() and system.metric_log. The alias-related stateless tests pass. tests/performance/wide_table_nested_select.xml gets a table with 500 ALIAS columns: 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]

groeneai and others added 2 commits November 2, 2026 11:52
`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]>
@groeneai

groeneai commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator Author
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).

# Sev Finding Verdict Evidence / action
1 💡 name_to_last_node spells out the type of ActionsDAG::NodeNameToNode (PlannerJoinTree.cpp) AGREE noted, not blocking
2 💡 No in-tree test sees a reordered or duplicated Compute alias columns step DISAGREE both broken variants change only EXPLAIN text, never results; the only guard would be a full-EXPLAIN reference; base and fix are byte-identical locally
3 💡 ReadFromMerge::convertAndFilterSourceStream adds one step per ALIAS column of a merge() source DISAGREE different code, not measured, not raised

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-11:20261102-123700

@groeneai

groeneai commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator Author

cc @yariks5s @novikd, could you review this and add the can be tested label so CI runs? createComputeAliasColumnsStep rebuilt the mergeNodes name index once per ALIAS column and assembled the outputs with one addOrReplaceInOutputs scan per node; it now keeps one index across the merges and builds the outputs in one pass, so the system.metric_log DESCRIBE from #128155 goes from 1.4 s to 0.2 s.

@clickhouse-gh

clickhouse-gh Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [3445e17]

Summary: ✅


AI Review

Summary

PR #128634 removes quadratic name-index rebuilding and output scans when planning reads of many ALIAS columns, and adds a focused performance benchmark. Reviewed head 3445e17b167328bd34c80c38255b7a7e826c6377, including prior discussion, affected callers, and the supplied performance and correctness evidence. No actionable findings.

Final Verdict

Status: ✅ Approve.

@clickhouse-gh clickhouse-gh Bot added pr-performance Pull request with some performance improvements comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. can be tested Allows running workflows for external contributors labels Nov 2, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 3445e17b1 with master aa863c38b (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of ac6858960; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

1288 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.
The matched translation units cost +880.6 s (+9%) in total before that adjustment.

Job report

@clickhouse-gh clickhouse-gh Bot added comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). and removed comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. labels Nov 19, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Planning time grows quadratically with the number of ALIAS columns read

1 participant