Skip to content

Cherry pick #130516 to 26.10: Estimate the join inputs of the automatic parallel replicas probe from the single-node analysis - #133081

Merged
robot-clickhouse-ci-2 merged 17 commits into
backport/26.10/130516from
cherrypick/26.10/130516
Nov 20, 2026
Merged

robot-clickhouse-ci-2 merged 17 commits into
backport/26.10/130516from
cherrypick/26.10/130516

Conversation

@robot-ch-test-poll2

Copy link
Copy Markdown
Contributor

Original pull-request #130516

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Before you resolve anything

Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

nickitat and others added 16 commits November 10, 2026 14:08
The probe plan of automatic parallel replicas is optimized without
`query_plan_optimize_primary_key`, so its reads have no index conditions and
no analysis of their own. Join optimization estimates every join input with
`estimateReadRowsCount`, which analyzed such a read on the spot: a second
index analysis of every read under a join, and an unpruned one, so the probe
also sized its joins from whole-table row counts.

Pass `find_single_node_read` to `estimateReadRowsCount` from join
optimization (`optimizeJoinLegacy`, `optimizeJoinLogical`, and the IEJoin
statistics in `buildPhysicalJoinImpl`). A read without an analysis is
estimated from the analysis of its single-node read, which is the one the
probe's reads take if that plan is chosen. Nothing is stored on the probe's
read.

In the new test, the probe of a two-table join used to add one round of
index analysis per table and check every mark against the primary key
again; it now adds none.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The previous commit took the estimate of a probe read from the read that
`find_single_node_read` pairs it with. That pairing goes by the name the
analyzer gave the table expression, and the two plans do not agree on these
names: the replicas plan's local plan is built from the tree
`buildQueryTreeForShard` returns, which renumbers `__tableN`. With a
three-table join written big table last, or a table read twice, the
pairing finds nothing and the estimate analyzed the probe's read in full,
unpruned, as before.

An estimate does not need to know which read is which. The probe now also
gets `find_single_node_reads_of_table`, every read of the single-node plan
of the same table, and `estimateReadRowsCount` takes the largest of their
analyses: the analysis itself when the table is read once or several times
alike (a CTE used twice), and an upper bound when its reads are filtered
differently. `find_single_node_read` stays as it is for direct read from a
text index, which needs the very read.

`makeDistributed`, `RuntimeFilterExchangeWiring` and the Cascades statistics
also call the estimate, but only under `make_distributed_plan`, which
rejects automatic parallel replicas, so they never see a probe plan.

`05508` covers the two shapes, and a CTE used twice; with the previous
commit the first two still added rounds of index analysis.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This is the shape of the B28 residual queries (q39-like: a CTE over a
four-table join, read twice with a different filter on each copy). The
plan with parallel replicas numbers the table expressions of each copy
from 1, so both copies of a table share one name there and have two
different names in the single-node plan. Pairing by name then finds
nothing, and with only the first fix the probe doubled the index
analysis (8 rounds against 16 locally, as in the replays).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A read of the probe plan of automatic parallel replicas now takes the
analysis of the single-node read that `find_single_node_read` pairs it
with, if that read already has one, and is left without an estimate
otherwise. It is never analyzed for the estimate: not the probe's own read,
which would prune nothing, and not the single-node one.

This replaces the previous commit's grouping of single-node reads by table,
which took the largest of their analyses when a read could not be paired.
A read that cannot be paired cannot take an analysis in
`transplantAnalysisToAllReads` either, so that estimate only helped a probe
the transplant rejects afterwards anyway.

Under a throwing read limit the single-node join estimates use
`selectRangesToReadForEstimation`, which keeps no result, so the probe finds
none to take. It used to analyze its reads then (6 index analysis rounds
against 4 without automatic parallel replicas in the new `05508` case); now
it adds none. In every `05508` case the outcome of the optimization is the
same as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…n `05508`

With a throwing `max_rows_to_read`, the single-node join estimates use
`selectRangesToReadForEstimation`, which keeps no result, so the probe of
automatic parallel replicas finds no analysis to estimate from. Without
column statistics (`use_statistics = 0`) its join inputs then have no row
estimate at all. The single-node plan swaps a join whose small table is
written first, the probe does not, and the two plans cannot be matched:
`AutoParallelReplicasPlansNotComparable` is 1 and no statistics are
collected.

The case fails on this branch. Before the previous commit it matched, but
the probe analyzed its reads again.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Under a throwing read limit, a single-node join estimate analyzes its read
with `selectRangesToReadForEstimation`, which must not become the read's
analysis, so the probe plan of automatic parallel replicas found none to
estimate from. Without column statistics its join inputs then had no row
estimate, it did not swap the join the single-node plan swapped, and the
plans could not be matched.

Keep that result on the read as well, in a field of its own that only
`getAnalyzedResultForEstimation` returns, and let the probe estimate from it
when the paired single-node read has no analysis. The probe now estimates
from the numbers the single-node plan chose by, without analyzing anything
itself; the executed read still analyzes again as before.

`05508_throwing_limit_swap` passes: the plans match, and the probe adds no
index analysis.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…alysis-again

# Conflicts:
#	src/Processors/QueryPlan/ReadFromMergeTree.cpp
…lysis

The row store for `String` keys (#125979),
merged from master, estimates the right input of a hash join for the average
bytes of its columns with `estimateReadRowsCount`, without
`find_single_node_read`. In the probe plan of automatic parallel replicas that
analyzed the read again, which `05508_throwing_limit_swap` caught after the
merge: 6 index analysis rounds against 5 without automatic parallel replicas.
Pass `find_single_node_read` there as well, like the other join estimates.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The comment still described the first version, where a read without an
analysis was estimated from the single-node read however it got one. Now
the estimate only takes an analysis that read already has, or its last
estimation-only one, and otherwise gives none.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…alysis-again

# Conflicts:
#	src/Processors/QueryPlan/Optimizations/RelationStatisticsEstimator.cpp
#	src/Processors/QueryPlan/Optimizations/RelationStatisticsEstimator.h
#	src/Processors/QueryPlan/ReadFromMergeTree.cpp
…again

Estimate the join inputs of the automatic parallel replicas probe from the single-node analysis
@robot-ch-test-poll2 robot-ch-test-poll2 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request labels Nov 19, 2026
@nickitat

Copy link
Copy Markdown
Member

@copilot resolve the merge conflicts in this pull request

# Conflicts:
#	src/Processors/QueryPlan/JoinStepLogical.cpp
#	src/Processors/QueryPlan/Optimizations/RelationStatisticsEstimator.h
#	src/Processors/QueryPlan/ReadFromMergeTree.cpp
@robot-clickhouse-ci-2
robot-clickhouse-ci-2 merged commit 5bbde21 into backport/26.10/130516 Nov 20, 2026
2 checks passed
@robot-clickhouse-ci-2
robot-clickhouse-ci-2 deleted the cherrypick/26.10/130516 branch November 20, 2026 12:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants