Skip to content

Introduce RelationStatsCache - #122259

Open
cv4g wants to merge 2 commits into
masterfrom
relation-stats-cache
Open

cv4g wants to merge 2 commits into
masterfrom
relation-stats-cache

Conversation

@cv4g

@cv4g cv4g commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Extracted from #121736.

This PR adds a pass-local RelationStatsCache and threads it through the join optimization pipeline. Join reordering, legacy join optimization, runtime-filter planning, and IEJoin physical planning can now reuse statistics for the same relation subtrees.

The cache:

  • Keeps entries separate for RelationStatsOptions::propagate_join_estimates modes (required to not regress on join order optimization Revert #97114 Move join step row estimation before check for 1 child" #99957).
  • RelationStats unaffected by propagate_join_estimates, such as table-read statistics, are cached for both modes.
  • Keeps cached RelationStats request-local when derived through mutable joins and common-subplan references, preventing their reuse after plan rewrites (RelationStatsCache::Entry::invocation).
  • Snapshots step and child addresses to detect missed invalidation:
    • Debug and sanitizer builds assert.
    • Release builds discard stale entries and recompute.
  • Invalidates entries when optimizer rewrites mutate nodes.
  • Rebinds entries when a subtree is moved below a new expression node.
  • Starts a fresh cache for delayed physical join conversion after filter, PREWHERE, parallel-replica, and
    distributed-plan rewrites, preventing reuse of stale pre-rewrite entries without requiring those rewrites to
    implement cache invalidation.
  • Threads the cache into physical join conversion so IEJoin key selection reuses input statistics already computed
    during join optimization.

Callers outside the the join-optimization phase of optimizeTreeSecondPass retain the previous behavior through a temporary local cache.

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

Not for changelog.


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

@clickhouse-gh

clickhouse-gh Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e12d8c4]

Summary: ❌

job_name test_name status info comment
Performance Comparison (arm_release, master_head, 6/6) FAIL Performance dashboard
Check Results FAIL

AI Review

Summary

This PR adds a pass-local RelationStatsCache and threads it through join reordering, runtime-filter planning, and logical-to-physical join conversion so repeated estimateReadRowsCount work can be reused while estimates that cross mutable JoinStepLogical / CommonSubplanReferenceStep boundaries stay invocation-local. I reviewed the current diff, the existing discussion, and the touched optimizer paths; I did not find a remaining correctness, compatibility, or test-coverage issue that merits a new inline comment.

Missing context / blind spots
  • ⚠️ I did not run the modified unit tests or a full build locally, so this review is based on static inspection and the coverage added in gtest_relation_stats_cache.cpp.
Final Verdict

✅ No new inline findings.

LLVM Coverage Report

Measured on commit e12d8c4.

Metric Baseline Current Δ
Lines 88.20% 88.20% +0.00%
Functions 91.70% 91.70% +0.00%
Branches 79.50% 79.50% +0.00%

Changed lines: Changed C/C++ lines covered: 257/272 (94.49%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Sep 25, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... label Sep 25, 2026
@cv4g
cv4g force-pushed the relation-stats-cache branch from 4176edc to c619a82 Compare September 25, 2026 16:01

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

comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... pr-not-for-changelog This PR should not be mentioned in the changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant