Skip to content

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

Merged
davenger merged 1 commit into
masterfrom
fix_estimate_rows_for_join_step
Feb 17, 2026
Merged

davenger merged 1 commit into
masterfrom
fix_estimate_rows_for_join_step

Conversation

@davenger

@davenger davenger commented Feb 16, 2026 •

Copy link
Copy Markdown
Member

Previously estimateReadRowsCount for join step always returned nullopt because of if (node.children.size() != 1) return; before it. It didn't seem to affect join reordering because there the row count estimation was stored in DP table.

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):

...

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.2.1.831

@clickhouse-gh

clickhouse-gh Bot commented Feb 16, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [fff94d2]

Summary: ❌

job_name test_name status info comment
Integration tests (amd_asan, db disk, old analyzer, 3/6) failure
test_refreshable_mv/test.py::test_refreshable_mv_in_read_only_node FAIL cidb IGNORED
test_refreshable_mv/test.py::test_refreshable_mv_in_read_only_node_no_ddl FAIL cidb IGNORED
Integration tests (amd_tsan, 3/6) failure
test_refreshable_mv/test.py::test_replicated_db_startup_race FAIL cidb IGNORED
Integration tests (amd_msan, 3/6) failure
test_refreshable_mv/test.py::test_backup_inner_table FAIL cidb, issue ISSUE EXISTS
test_refreshable_mv/test.py::test_adding_replica FAIL cidb IGNORED

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Feb 16, 2026
@nickitat nickitat self-assigned this Feb 16, 2026
@davenger
davenger added this pull request to the merge queue Feb 17, 2026
Merged via the queue into master with commit 9284c45 Feb 17, 2026
140 of 143 checks passed
@davenger
davenger deleted the fix_estimate_rows_for_join_step branch February 17, 2026 10:21
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Feb 17, 2026
davenger added a commit that referenced this pull request Mar 18, 2026
github-merge-queue Bot pushed a commit that referenced this pull request Mar 20, 2026
Revert #97114 Move join step row estimation before check for 1 child"
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
Copilot AI added a commit that referenced this pull request Apr 19, 2026
…icts

- Resolved conflict in `optimizeJoin.cpp`: keep the `JoinStepLogical` check after `aggregating_step` (as per the revert of #97114), using `column_stats = {}` since `getResultColumnStats()` was removed in the backport
- Resolved conflict in `03837_subquery_ndv_propagation_join_order.reference`: deleted (both .sql and .reference deleted in backport branch)

Co-authored-by: fm4v <[email protected]>
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
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-not-for-changelog This PR should not be mentioned in the changelog 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