Skip to content

Revert #97114 Move join step row estimation before check for 1 child" - #99957

Merged
alexey-milovidov merged 2 commits into
masterfrom
revert_97114
Mar 20, 2026
Merged

alexey-milovidov merged 2 commits into
masterfrom
revert_97114

Conversation

@davenger

@davenger davenger commented Mar 18, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Revert #97114 "Move join step row estimation before check for 1 child" due to suspected performance regression.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.4.1.40
  • Backported to: 26.3.10.16, 26.2.17.11

@clickhouse-gh

clickhouse-gh Bot commented Mar 18, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0f398a9]

Summary: ✅


AI Review

Summary

This PR reverts #97114 by moving optimized JoinStepLogical row-estimation handling below a single-child guard and updating test references accordingly. That reintroduces the original semantic issue: optimized logical joins (with two children) return empty stats, which can disable cardinality-based join optimization and produce worse plans. High-confidence correctness/performance regression risk remains.

Findings

❌ Blockers

  • [src/Processors/QueryPlan/Optimizations/optimizeJoin.cpp:308-349] The JoinStepLogical optimized branch is now unreachable for normal joins because if (node.children.size() != 1) return {}; runs first. Optimized joins have two children, so estimateReadRowsCount returns empty estimates instead of getResultRowsEstimation/getResultColumnStats.
    • Impact: loss of row/cardinality stats for optimized joins, degraded join ordering/algorithm selection, and behavior regression relative to #97114.
    • Suggested fix: move the optimized JoinStepLogical branch back before the node.children.size() != 1 guard.

ClickHouse Rules

Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ❌ Query-plan core logic regresses row-estimation semantics for optimized joins.
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ⚠️ Reverts previously fixed optimizer behavior, causing plan-quality regression risk.
SettingsChangesHistory.cpp ➖
Safe rollout ⚠️ Regression risk affects stable optimizer behavior without a guard.
Compilation time ✅

Performance & Safety

  • Missing row/cardinality estimates on optimized joins can force weaker join-order decisions and less suitable join algorithms, causing real query performance regressions under multi-join workloads.

Final Verdict

  • Status: ⚠️ Request changes
  • Minimum required actions:
    1. Restore optimized JoinStepLogical estimation handling before the node.children.size() != 1 early return.
    2. Keep/restore reference outputs that reflect non-empty optimized-join stats instead of unknown propagation introduced by this revert.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Mar 18, 2026
.table_name = join_step->getReadableRelationName()};
}

if (node.children.size() != 1)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

JoinStepLogical nodes should have exactly 2 children, so this early guard returns before the optimized-join branch below can run.

With this ordering, estimateReadRowsCount for optimized logical joins becomes effectively unreachable and falls back to {}/nullopt row estimates (the behavior fixed in #97114). That can disable join-size-based decisions in callers.

Can we keep the JoinStepLogical case before the node.children.size() != 1 check, and address the performance regression separately?

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 exactly the point of this revert. It was like this before the change that supposedly caused a regression. So just restoring the old code.

@clickhouse-gh

clickhouse-gh Bot commented Mar 19, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 83.80% 83.80% +0.00%
Functions 23.90% 23.90% +0.00%
Branches 76.30% 76.40% +0.10%

PR changed lines: PR changed-lines coverage: 62.50% (10/16, 0 noise lines excluded)
Diff coverage report
Uncovered code

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Mar 20, 2026
@alexey-milovidov alexey-milovidov self-assigned this Mar 20, 2026
Merged via the queue into master with commit 3ac032b Mar 20, 2026
159 checks passed
@alexey-milovidov
alexey-milovidov deleted the revert_97114 branch March 20, 2026 07:52
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 20, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Apr 19, 2026
robot-clickhouse-ci-2 added a commit that referenced this pull request Apr 19, 2026
Cherry pick #99957 to 26.3: Revert #97114 Move join step row estimation before check for 1 child"
robot-clickhouse added a commit that referenced this pull request Apr 19, 2026
robot-ch-test-poll4 added a commit that referenced this pull request Apr 19, 2026
Cherry pick #99957 to 26.2: Revert #97114 Move join step row estimation before check for 1 child"
robot-clickhouse added a commit that referenced this pull request Apr 19, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Apr 19, 2026
clickhouse-gh Bot added a commit that referenced this pull request Apr 19, 2026
Backport #99957 to 26.2: Revert #97114 Move join step row estimation before check for 1 child"
fm4v added a commit that referenced this pull request Apr 19, 2026
Backport #99957 to 26.3: Revert #97114 Move join step row estimation before check for 1 child"
fkastrati added a commit that referenced this pull request Sep 22, 2026
A join the optimizer has already settled enters its parent's graph as a single
relation, and the row count it arrived at is the only estimate that relation
has. `estimateReadRowsCount` returned early for any node with two children, so
the parent saw nothing and `computeJoinCost` read the missing count as one row
through `value_or(1)` - the largest input in the query looked like the cheapest
thing to join. Read the estimate before that check.

On TPC-DS q72 the boundary is real: the non-equi predicate
`inv_quantity_on_hand < cs_quantity` splits `inventory` and `catalog_sales` into
their own graph, which estimates itself at 945,072,914 rows against a true
944,916,882 - 0.02% off - and the parent then planned eight relations around it
as if it returned one row. With column statistics the query goes from 11.6s to
0.8s, and under `dpsub` with the CD-C conflict detector from 72.7s to 1.0s: the
detector widens the search space, so a wrong estimate buys more wrong plans.

Measured over the full 99-query TPC-DS suite at scale factor 1 with column
statistics, no other query changes.

The relation name of such a sub-join is a chain that already carries its own
inputs' estimates, so it is marked composite and `EXPLAIN` no longer appends a
second estimate to it.

This restores #97114, reverted in #99957 for a suspected performance
regression. On tables without column statistics the estimate it propagates is
itself unreliable, and there the change is a wash across TPC-DS - 18 queries
faster, 11 slower. #101398 proposes gating propagation on a trust flag.

Related: #97114
Related: #99957
Related: #101398
cv4g added a commit that referenced this pull request Oct 22, 2026
The option kept the row estimate and column statistics of an already
optimized `JoinStepLogical` from reaching its parents unless a caller
opted in. It was added because moving the sub-join case into
`estimateDirectRelationStats` made it reachable for the first time;
before, it sat behind the single-child check and never ran, so the
option preserved the state after #99957 reverted #97114.

#120110 then deliberately restored that propagation for every caller of
`estimateReadRowsCount`. The merge with master kept the option and only
opted in for join reordering, so this branch still diverged from master
for all other consumers. For example, the distributed plan shuffled the
right side of a join that was a small optimized sub-join instead of
broadcasting it, because its row count was unknown.

Drop the option so sub-join estimates propagate as on master. The
`Unsupported` (and for semi/anti joins `RowSubset`) provenance stays,
so consumers that gate on `ColumnStatsProvenance` still fail closed on
a sub-join's column statistics.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.2-must-backport v26.3-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants