Skip to content

Ship _shard_num with plan-based parallel replicas fragments; test follower analysis without a local plan - #127014

Merged
nickitat merged 4 commits into
masterfrom
pr-follower-analysis-without-local-plan
Nov 4, 2026
Merged

nickitat merged 4 commits into
masterfrom
pr-follower-analysis-without-local-plan

Conversation

@nickitat

@nickitat nickitat commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Performance Improvement

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

Plan-based parallel replicas (parallel_replicas_plan_based = 1) used through a Distributed table now apply primary key and skip index analysis on the followers instead of reading whole parts.


Related: #126792
Related: #126954
Related: #126955

With parallel_replicas_index_analysis_only_on_coordinator (the default), a follower skips its own index analysis and relies on the analysis of the initiator's local plan. Without a local plan - parallel replicas inside a Distributed sub-query, or parallel_replicas_prefer_local_replica = 0 - nobody analyzed, and every replica read whole parts. #126792 fixed this in master by checking ClusterProxy::canUseLocalPlanForParallelReplicas on the follower; this PR adds a test for it.

The test also found a case that fix misses. A follower recognizes the Distributed case by the _shard_num scalar the initiator ships, but with parallel_replicas_plan_based = 1 the plan fragment was sent with Scalars{} (ReadFromParallelReplicasStep::createPipeForSingeReplica). The followers then assumed a local plan, skipped the analysis, and read whole parts again. The fragment now carries the query context's scalars, as the query-based path does.


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

Version info

  • Merged into: 26.11.1.1285-master (included in 26.11 and later)

…l plan

With `parallel_replicas_index_analysis_only_on_coordinator`, a follower skipped
its own index analysis whenever `parallel_replicas_local_plan` was set, relying
on the analysis of the initiator's local plan. Inside a `Distributed` subquery
and with `parallel_replicas_prefer_local_replica = 0` there is no local plan,
so nobody analyzed and every replica read whole parts: 1280000 rows instead of
384 for a primary key range of 3 granules through `Distributed`. The check now
uses `ClusterProxy::canUseLocalPlanForParallelReplicas`, which covers both.

Review: #126954 (comment)
Related: #126954

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [9d26358]

Summary: ✅


AI Review

Summary

The PR forwards query scalars, including _shard_num, with plan-based parallel-replica fragments so followers perform index analysis when their coordinator has no local plan. Regression coverage exercises both query-based and plan-based execution through Distributed and with parallel_replicas_prefer_local_replica = 0. Both previous findings are addressed and their bot-authored threads are resolved. No new actionable findings remain.

Final Verdict

✅ Approve for head 9d263588d9cb473a67d06ef0f8e2e801433ed540. The new regression test passed in Fast test. Full CI is still running.

@clickhouse-gh clickhouse-gh Bot added pr-performance Pull request with some performance improvements comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). labels Oct 20, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 9d263588d with master 8e3eb7adf (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 17 s compile time in total, 7 of them have a recent master baseline.

Job report

@clickhouse-gh clickhouse-gh Bot added comp-testing Test content only (no production code). and removed comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). labels Oct 23, 2026
`ReadFromParallelReplicasStep` sent `Scalars{}` to the followers, so inside a `Distributed` sub-query
they did not see `_shard_num`, assumed the initiator runs a local plan, and skipped their own index
analysis: every follower read whole parts. Send the query context's scalars, as the query-based path
does. The test now runs both cases with `parallel_replicas_plan_based` = 0 and 1.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nickitat nickitat changed the title Analyze on parallel replicas followers when the initiator has no local plan Ship _shard_num with plan-based parallel replicas fragments; test follower analysis without a local plan Oct 25, 2026
@clickhouse-gh clickhouse-gh Bot added comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). and removed comp-testing Test content only (no production code). labels Oct 25, 2026
@nickitat
nickitat requested a review from devcrafter November 2, 2026 13:35
@nickitat
nickitat marked this pull request as ready for review November 2, 2026 13:40
@devcrafter devcrafter self-assigned this Nov 3, 2026
@devcrafter
devcrafter requested a review from groeneai November 3, 2026 21:54
@nickitat
nickitat added this pull request to the merge queue Nov 3, 2026

@groeneai groeneai left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I ran the CI amd_debug binaries of this head (9d263588) and of its master base (8e3eb7ad) on a local 1-shard x 3-replica cluster. 05428 gives plan_based_distributed 0 on the base and matches the reference on the head; the other three modes pass on both.

One non-blocking suggestion: getScalars() also ships every scalar-subquery result, which a plan fragment does not need, since the value is already folded into the plan (on the base, followers evaluate has((SELECT groupArray(number * 10) FROM numbers(100)), k) correctly with Scalars{}). For SELECT sum(k) FROM t WHERE k < length((SELECT groupArray(number) FROM numbers(1000000))) with parallel_replicas_plan_based = 1, parallel_replicas_prefer_local_replica = 0, the initiator's NetworkSendBytes goes from 15 KB on the base to 10.1 MB on this head. Shipping only _shard_num avoids that:

Scalars scalars;
if (context->hasQueryContext() && context->getQueryContext()->hasScalar("_shard_num"))
    scalars.emplace("_shard_num", context->getQueryContext()->getScalar("_shard_num"));

I built that on master ebff85ed: 05428 passes, that query sends 19 KB, and the array scalar subquery and shardNum()/shardCount() through Distributed return the same results as on the base.

LGTM otherwise.

Merged via the queue into master with commit 5969a38 Nov 4, 2026
352 of 353 checks passed
@nickitat
nickitat deleted the pr-follower-analysis-without-local-plan branch November 4, 2026 02:06
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 4, 2026
@devcrafter

Copy link
Copy Markdown
Member

@groeneai thanks for the suggestion in #127014 (review): #133844 sends only _shard_num and _shard_count with a plan-based fragment, with a test that checks the initiator's NetworkSendBytes.

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

Labels

comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). pr-performance Pull request with some performance 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