Skip to content

Cache the parameterized view query hash in TableNode - #119517

Merged
fm4v merged 3 commits into
masterfrom
fix-118736-parameterized-view-hash
Sep 15, 2026
Merged

fm4v merged 3 commits into
masterfrom
fix-118736-parameterized-view-hash

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 11, 2026 •

Copy link
Copy Markdown
Member

TableNode::isEqualImpl and TableNode::updateTreeHashImpl traversed the whole substituted inner query of a parameterized view on every call (introduced by #105170 to tell apart two calls of the same view with different argument values). The tree hash of every expression that references a column of the view descends into its TableNode, and the analyzer computes such hashes hundreds of times while resolving a query, so planning of a large parameterized view tree became several times slower. The hash of the inner query is now computed once per TableNode (and copied on clone), so the equality check and the tree hash are O(1) again.

Measured on the fixture from the issue (clickhouse local, min of 5 alternating rounds, with the parameterized-view call resolved through a TableNode as in 26.5-26.7):

Query Before After
EXPLAIN SELECT * FROM synth_treatment... 2.54 s 0.47 s
SELECT * FROM synth_treatment... (empty tables) 2.80 s 0.69 s
EXPLAIN SELECT * FROM synth_control... 0.38 s 0.37 s

A performance test, tests/performance/parameterized_view_analysis.xml, models the shape of the issue: a parameterized view with a large body, called three times from an outer parameterized view, joined on several columns and filtered by a long AND chain of comparisons over empty tables. With the 26.6-style resolution the first query takes about 1.3 s before and 0.75 s after this change; on master (where a call is a TableFunctionNode) it takes about 1.1 s in both cases, so the test guards the analysis cost of such trees rather than this particular path.

Note that on master a parameterized-view call is resolved as a TableFunctionNode since #68978 (26.8), so the slow path is reached there only when a TableNode wraps a parameterized view through other routes; the regression as reported is present in 26.6 and 26.7, where the call still becomes a TableNode. This change is needed on those release branches.

Closes: #118736
Related: #105170
Related: #68978

Changelog category (leave one):

  • Performance Improvement

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

Fix a slowdown of query analysis for large parameterized views: the substituted view query was re-hashed on every comparison of the query tree. Closes #118736.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/119517]

Version info

  • Merged into: 26.9.1.1559 (included in 26.9 and later)
  • Backported to: 26.8.6.5

`TableNode::isEqualImpl` and `TableNode::updateTreeHashImpl` traversed the whole
substituted inner query of a parameterized view on every call. The tree hash of
every expression that references a column of the view descends into its
`TableNode`, so analysis of large parameterized view trees became several times
slower after #105170. Compute the hash once per node instead.

Closes: #118736

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@alexey-milovidov alexey-milovidov added pr-performance Pull request with some performance improvements v26.6-must-backport v26.7-must-backport labels Sep 11, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c461959]

Summary: ❌

job_name test_name status info comment
Stress test (arm_tsan) FAIL
Cannot start clickhouse-server FAIL cidb IGNORED
Check failed FAIL cidb IGNORED
Build profile diff ERROR IGNORED

AI Review

Summary

Caches the substituted parameterized-view inner-query hash on TableNode so isEqualImpl and updateTreeHashImpl stop rewalking the view AST, and adds a performance test for large parameterized-view trees. The main hot path improves, but the clone path still rebuilds that cache by re-hashing the whole inner query, so the PR does not yet satisfy its stated "copied on clone / O(1) again" contract.

Findings

⚠️ Majors

  • [src/Analyzer/TableNode.cpp:225] cloneImpl still constructs the clone through TableNode(storage, storage_id, storage_lock, storage_snapshot), and that constructor eagerly recomputes parameterized_view_query_hash from storage_snapshot->metadata->getSelectQuery().inner_query. The assignment at line 230 only overwrites the result after the O(size(inner query)) walk has already happened, so analyzer passes that clone parameterized-view subtrees still pay the expensive hash build once per clone. That leaves a substantial part of the reported slowdown in place and makes the "copied on clone" part of the contract false. Suggested fix: add a clone/copy path that initializes the new TableNode with the already-computed hash instead of calling the hashing constructor, for example via a private constructor or helper used only by cloneImpl.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required action: avoid recomputing parameterized_view_query_hash during TableNode::cloneImpl so clones actually reuse the cached hash.

LLVM Coverage Report

Measured on commit c461959.

Metric Baseline Current Δ
Lines 88.40% 88.40% +0.00%
Functions 91.70% 91.70% +0.00%
Branches 80.50% 80.50% +0.00%

Changed lines: Changed C/C++ lines covered: 26/30 (86.67%) · Uncovered code

Full report · Diff report

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is clear. Maybe add a perf test?

@alexey-milovidov alexey-milovidov self-assigned this Sep 11, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Added a performance test, tests/performance/parameterized_view_analysis.xml, modelled on the shape of the issue (a large parameterized view called three times from an outer parameterized view, joined on several columns and filtered by a long AND chain). Locally, with the call resolved through a TableNode as in 26.6/26.7, the first query goes from about 1.3 s to 0.75 s with this change. On master the call is a TableFunctionNode since #68978, so the test does not show a change for this PR there; it guards the analysis cost of such view trees and will show the improvement in the 26.6/26.7 backports.

Comment thread src/Analyzer/TableNode.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). label Sep 11, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 38 queries analysed

This PR narrows the analyzer cost of parameterized views: it caches the substituted inner-query hash on the TableNode instead of re-traversing the (potentially large) inner query on every isEqual/tree-hash call. That path is exercised only by parameterized-view analysis (covered by the new perf test), not by standard ClickBench SELECTs. The flagged ClickBench deltas (Q5 -12.8%, Q28, Q33, Q34) all sit on queries that use no parameterized views, so Q5's flagged improvement is downgraded to not_sure as run-to-run variance, and the Q33/Q34 envelope-suppressed no_change verdicts are correct and left untouched. No real per-query regression or improvement on the standard benchmarks is attributable to this change.

clickbench

⚠️ 2 inconclusive

Flagged queries (2 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 5 not_sure 257 224 -12.8% <0.0001 This PR only optimizes parameterized-view query-tree hashing in the analyzer; ClickBench Q5 has no parameterized views, so this -12.8% is run-to-run variance, not a PR effect.
⚠️ 28 not_sure 1124 1048 -6.8% <0.0001 Already not_sure due to mixed test signals on a noisy query. The PR touches only parameterized-view analyzer hashing, which Q28 does not use, so no attribution here.

Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 5fc0b73a-e07b-42a7-ae13-d9bb4694a6a9
  • MIRAI run: 344eb7d1-bab8-4852-bfb9-fc612b5205b4
  • PR check IDs:
    • clickbench_318587_1789490674
    • clickbench_318593_1789490675
    • clickbench_318599_1789490674
    • tpch_adapted_1_official_318605_1789490674
    • tpch_adapted_1_official_318617_1789490674
    • tpch_adapted_1_official_318629_1789490674

@clickhouse-gh

clickhouse-gh Bot commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing commit c46195915cd5f4e68bd4d4b6a14b94f044ccbecb with master failed: RuntimeError: CI logs cluster query failed: SELECT count() AS c FROM binary_sizes W….

See the job log for details.

perf.py expands {name} in every query as a substitution placeholder, so
{ts:DateTime64(3)} failed with KeyError: 'ts' in substitute_parameters and
the test errored out on both shards (8 errors in amd_release 4/6 and
arm_release 2/6). Double the braces so the parameterized-view syntax
reaches the server verbatim.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=119517&sha=b6fefdfb9112f36b080856ec9c58b4f4d4d67452&name_0=PR
PR: #119517
@fm4v
fm4v enabled auto-merge September 15, 2026 15:41
omar12eladawy pushed a commit to omar12eladawy/ClickHouse that referenced this pull request Sep 15, 2026
A performance test that uses literal braces in SQL (e.g. a parameterized
view parameter like {ts:DateTime64(3)}) failed in substitute_parameters
with a bare KeyError: 'ts' traceback, which does not explain that {name}
is expanded as a <substitution> placeholder or that the braces must be
doubled. Wrap the per-template substitution and rethrow with the failing
template, the available parameters and the {{...}} escaping hint,
keeping the original exception as the cause.

Seen in ClickHouse#119517:
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=119517&sha=b6fefdfb9112f36b080856ec9c58b4f4d4d67452&name_0=PR
@fm4v
fm4v added this pull request to the merge queue Sep 15, 2026
Merged via the queue into master with commit 6ef6af7 Sep 15, 2026
173 of 176 checks passed
@fm4v
fm4v deleted the fix-118736-parameterized-view-hash branch September 15, 2026 22:32
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 15, 2026
@robot-clickhouse robot-clickhouse added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Sep 15, 2026
clickhouse-gh Bot pushed a commit that referenced this pull request Sep 16, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 16, 2026
Backport #119517 to 26.8: Cache the parameterized view query hash in TableNode
@novikd novikd added the post-approved Approved, but after the PR is merged. label Sep 17, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). post-approved Approved, but after the PR is merged. pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo v26.6-must-backport v26.7-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SELECT latency regression after #105170 in large parameterized-view trees (2.2–2.4x; planning isolated with EXPLAIN)

6 participants