Skip to content

Do not recompute the parameterized view query hash on TableNode clone - #121042

Merged
alexey-milovidov merged 4 commits into
masterfrom
followup-119517-tablenode-clone-hash
Oct 7, 2026
Merged

alexey-milovidov merged 4 commits into
masterfrom
followup-119517-tablenode-clone-hash

Conversation

@alexey-milovidov

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

Copy link
Copy Markdown
Member

Follow-up to #119517, which cached the hash of the substituted inner query of a parameterized view in TableNode so that isEqualImpl and updateTreeHashImpl are O(1).

TableNode::cloneImpl created the copy through the public constructor, which eagerly computes parameterized_view_query_hash by traversing storage_snapshot->metadata->getSelectQuery().inner_query, and only then overwrote the field with the already cached value of the source node. A private constructor used only by cloneImpl now carries the cached hash over from the source node, so cloning does not look at the view query at all. This was pointed out in review of #119517: #119517 (comment)

This is a consistency cleanup, not a measurable speedup: on current master a parameterized view call such as pv(p = ...) is resolved by QueryAnalyzer::resolveTableFunction into a TableFunctionNode, not a TableNode, so the ordinary query paths (including tests/performance/parameterized_view_analysis.xml) never clone a TableNode that holds a parameterized StorageView. Verified with a debugger on IN (SELECT ... FROM pv(...)) and pv(...) JOIN pv(...) queries: the view's select query is only read by resolveTableFunction and StorageView::readImpl, never from TableNode. Behavior is unchanged: the resulting node holds the same hash as before.

Related: #119517
Related: #118736

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.10.1.1779-master (included in 26.10 and later)

`TableNode::cloneImpl` created the copy through the public constructor,
which eagerly computes `parameterized_view_query_hash` by traversing
`storage_snapshot->metadata->getSelectQuery().inner_query`, and only then
overwrote the field with the already cached value of the source node. So
every clone of a parameterized-view table expression still paid one full
walk of the substituted inner query - exactly the cost that the cache
added in #119517 is meant to avoid - and the analyzer clones subtrees
repeatedly.

Add a private constructor used only by `cloneImpl` that takes the hash
from the source node, so cloning is O(1) with respect to the size of the
view query.

Related: #119517
Related: #118736

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8ade3dc]

Summary: ✅


AI Review

Summary

This PR changes TableNode::cloneImpl to carry over the already-cached parameterized_view_query_hash through a clone-only constructor instead of recomputing it via the public constructor, while preserving the cloned node's observable state. I reviewed the current diff, the surrounding TableNode / TableFunctionNode analyzer code, and the prior discussion; the earlier metadata concern has been addressed, and I do not see a remaining correctness, compatibility, or safety issue in the current code.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Measured on commit 8ade3dc.

Metric Baseline Current Δ
Lines 88.20% 88.20% +0.00%
Functions 92.10% 92.20% +0.10%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 18/18 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Sep 19, 2026
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 19, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

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

This PR found no performance change, and none should be expected. The only change is that TableNode::cloneImpl now copies the cached parameterized view query hash instead of working it out again. That runs during query analysis and has no effect on ClickBench or TPC-H execution. The two flagged ClickBench queries, Q31 (+11.2%) and Q34 (+5.1%), both stay not_sure. Their CPU time barely moved, and Q31 is within master's current range. Some PR benchmark runs also shared hosts with each other, which can push up wall-clock times without adding CPU work.

clickbench

⚠️ 2 inconclusive

Flagged queries (2 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 31 not_sure 264 294 +11.2% <0.0001 aggregation: The +11.2% sits inside master's current range and CPU time is flat. This PR only changes how the analyzer clones a TableNode, which costs microseconds per query, so it cannot explain this delta.
⚠️ 34 not_sure 1122 1179 +5.1% 0.0318 aggregation: The +5.1% is borderline, the two tests disagree, and CPU time is flat. This GROUP BY URL scan does not run the changed clone path in any way that could take measurable time.

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: ffa26b04-8eac-4294-a126-3760253fcf15
  • MIRAI run: 6240c601-fd54-4d4c-b4a1-e8b777c94faf
  • PR check IDs:
    • clickbench_3290045_1791171782
    • clickbench_3290051_1791171782
    • clickbench_3290062_1791171782
    • tpch_adapted_1_official_3290069_1791171782
    • tpch_adapted_1_official_3290102_1791171782
    • tpch_adapted_1_official_3290116_1791171783

@clickhouse-gh

clickhouse-gh Bot commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Commit 8ade3dc07ed2bf4f6daff8175eb9e5d3f2c5c323 was not compared: the CI logs cluster did not answer (every POST attempt failed with an exception; stopped retrying after the 1250 s read budget).

See the job log for details.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Status: the AI-review request is addressed — I confirmed on trunk that parameterized view calls resolve to TableFunctionNode, so this TableNode::cloneImpl path is not reached by ordinary queries. I downgraded the PR to Not for changelog as a consistency cleanup and replied on the thread. The only red check, Performance Comparison (amd_release, master_head, 3/6) → order_by_read_in_order #1, is a known false positive tracked in #121456 (this change touches only analyzer cloning of parameterized-view TableNodes). No code change was needed.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Merged master (the branch was from 2026-09-22). The only red check was Performance Comparison (amd_release, master_head, 3/6) with order_by_read_in_order query #1 at +25% (0.018 s -> 0.023 s). This PR only changes TableNode::cloneImpl to skip the parameterized view hash computation, i.e. it removes work, and that test reads ordinary MergeTree tables, so this regression is not caused by the change; the perf job will re-run on the new head.

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Oct 3, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Merged master (8ade3dc). The red stateless checks on d0a4707 are unrelated to this change (it only touches TableNode::cloneImpl):

@groeneai, investigate the failures of 04819_dynamic_aggregate_function_state_version (https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=121042&sha=d0a47072989e6026ce02ef386f0176720e2dbbe5&name_0=PR&name_1=Stateless%20tests%20%28arm_binary%2C%20ParallelReplicas%2C%20s3%20storage%2C%20parallel%29) and 05237_with_expression_alias_scope_descendant (https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=121042&sha=d0a47072989e6026ce02ef386f0176720e2dbbe5&name_0=PR&name_1=Stateless%20tests%20%28arm_binary%2C%20s3%20storage%2C%20DBReplicated%2C%20parallel%2C%201%2F2%29) and provide a fix in a separate PR. If the fix is already in progress, link it here.

@groeneai

groeneai commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator

Both have fixes in progress, so I am not opening a new PR:

The new head 8ade3dc07ed includes #123789, so these arm_binary jobs do not run on it. The amd_llvm_coverage jobs report both tests as OK because their reruns pass, but the failure is still there: all 609 OK rows of 05237 in amd_llvm_coverage, s3 storage, DBReplicated over the last 3 days carry the view 10 diff.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Thanks @groeneai — noted: 04819_dynamic_aggregate_function_state_version is tracked by #123649 and 05237_with_expression_alias_scope_descendant by #123790. CI on the current head 8ade3dc07ed is green, there are no unresolved review threads, and the branch was merged with master on 2026-10-05, so no further change is needed here.

@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.

LGTM

@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.

LGTM

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 7, 2026
@alexey-milovidov alexey-milovidov self-assigned this Oct 7, 2026
Merged via the queue into master with commit 428d207 Oct 7, 2026
149 checks passed
@alexey-milovidov
alexey-milovidov deleted the followup-119517-tablenode-clone-hash branch October 7, 2026 22:14
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 7, 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). pr-not-for-changelog This PR should not be mentioned in the changelog pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants