Skip to content

Properly return column stats from sub-queries with joins so that parent query can use them to join reordering - #99096

Merged
davenger merged 4 commits into
masterfrom
fix_stats_from_join_steps
Mar 12, 2026
Merged

davenger merged 4 commits into
masterfrom
fix_stats_from_join_steps

Conversation

@davenger

@davenger davenger commented Mar 9, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Improvement

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

Properly return column stats from sub-queries with joins so that parent query can use them for join reordering.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Note

Medium Risk
Changes join-order optimization to carry and update per-column NDV statistics across joined subplans, which can alter chosen join orders/algorithms and impact performance characteristics.

Overview
Join-order optimization now propagates column statistics. JoinStepLogical stores and exposes result_column_stats, and optimizeJoin returns these stats when an optimized logical join is used as an input to higher-level planning.

DP join enumeration carries NDVs through join trees. DPJoinEntry now tracks column_stats, merges child stats when building join nodes, normalizes NDVs for equi-join keys, and caps NDVs by estimated output rows; selectivity estimation for multi-relation components now consults these propagated stats.

Subquery joins are optimized for stats even when not flattened into the query graph. addChildQueryGraph additionally optimizes nested JoinStepLogical nodes hidden under single-child wrappers to ensure their stats feed parent join reordering, with updated/added stateless tests covering the new behavior.

Written by Cursor Bugbot for commit 6070702. This will update automatically on new commits. Configure here.

Version info

  • Merged into: 26.3.1.622

@clickhouse-gh

clickhouse-gh Bot commented Mar 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6070702]

Summary: ❌

job_name test_name status info comment
AST fuzzer (amd_tsan) failure
Logical error: Unexpected return type from A. Expected B. Got C. Action: (STID: 1611-3d11) FAIL cidb #99278
LLVM Coverage failure
Generate LLVM Coverage Diff Report failure cidb IGNORED

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Mar 9, 2026
@clickhouse-gh

clickhouse-gh Bot commented Mar 10, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 83.20% 83.70% +0.50%
Functions 23.60% 23.80% +0.20%
Branches 75.30% 76.20% +0.90%

PR changed lines: PR changed-lines coverage: 95.16% (118/124)
Diff coverage report
Uncovered code

@davenger
davenger force-pushed the fix_stats_from_join_steps branch from ee88120 to 6070702 Compare March 10, 2026 11:17
@vdimir vdimir self-assigned this Mar 12, 2026
@davenger
davenger added this pull request to the merge queue Mar 12, 2026
Merged via the queue into master with commit 06faf28 Mar 12, 2026
157 of 159 checks passed
@davenger
davenger deleted the fix_stats_from_join_steps branch March 12, 2026 19:30
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 12, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Hi @davenger @vdimir — while reviewing this PR I found the following:

Happy to discuss — close anything that's wrong or already addressed.

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

Labels

pr-improvement Pull request with some product 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.

4 participants