Skip to content

Send only the shard scalars with a plan-based parallel replicas fragment - #133844

Open
devcrafter wants to merge 1 commit into
masterfrom
pr-plan-based-ship-only-special-scalars
Open

devcrafter wants to merge 1 commit into
masterfrom
pr-plan-based-ship-only-special-scalars

Conversation

@devcrafter

@devcrafter devcrafter commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

ReadFromParallelReplicas sent every scalar of the query context with a plan-based fragment, although scalar subquery values are already constants in the plan. A query with a large scalar subquery could send megabytes to each replica: for a 1M-element array, the initiator sent 10 MB instead of 15 KB. Now only _shard_num and _shard_count are sent, which shardNum / shardCount and the follower's index analysis need.

Related: #127014 (review)

Changelog category (leave one):

  • Performance Improvement

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

Plan-based parallel replicas no longer send the results of scalar subqueries to the replicas, as they are already part of the shipped query plan.

🤖 Generated with Claude Code


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

Scalar subquery values are constants in the plan; shipping all scalars could send megabytes per replica.
#127014 (review)

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

clickhouse-gh Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [83d79b7]

Summary: ✅


AI Review

Summary

The PR avoids resending scalar-subquery results with plan-based parallel replica fragments while preserving _shard_num and _shard_count. No actionable findings. The new scalar-shipping regression and the existing follower index-analysis regression both passed in Fast test at 83d79b74. The linked review provides before/after network measurements supporting the performance improvement.

Final Verdict

Status: ✅ Approve

@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 Nov 23, 2026
@devcrafter
devcrafter requested a review from groeneai November 23, 2026 18:16
@clickhouse-gh

clickhouse-gh Bot commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 83d79b74e with master cd61bfb0c (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, 15 s compile time in total, 7 of them have a recent master baseline.

Job report

@devcrafter
devcrafter requested a review from nickitat November 23, 2026 19:12

@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.

LGTM. Checked with the CI release builds of this PR (83d79b7, merged with master cd61bfb) and of master cd61bfb itself, on a local 1-shard x 3-replica cluster with parallel_replicas_plan_based = 1, parallel_replicas_prefer_local_replica = 0:

  • 05871 fails on master (initiator NetworkSendBytes 10,133,908, last line 0) and passes here (19,483), so the oracle can fail, with a wide margin on both sides of 1 MB.
  • Scalar subquery values reach the replicas as constants of the fragment: 26 queries with an Array / Tuple / LowCardinality / AggregateFunction scalar subquery in SELECT, WHERE, PREWHERE, GROUP BY, ORDER BY, lambdas, ARRAY JOIN, IN, JOIN, and through a Distributed table over the parallel replicas cluster (the shard gets __getScalar calls plus the scalars, then ships fragments), return the same results on both builds, with fragments executed on the remote replicas.

One note on the test, not blocking: the shardNum() query over test_cluster_two_shards ships no plan fragment (the shard queries spawn no replica queries), and it returns the same 1 100000 / 2 100000 on a build from before #127014 (96b7402) that sends Scalars{}, so it cannot fail for this change. With a fragment (Distributed over test_cluster_one_shard_three_replicas_localhost), shardNum() / shardCount() are still folded on the parallel replicas initiator, whose table is not remote (isSuitableForConstantFolding returns !is_distributed, src/Functions/getScalar.cpp:157): that old build returns 1 1 there too. So the follower only reads _shard_num in canUseLocalPlanForParallelReplicas, which 05428 (plan_based_distributed) covers; sending _shard_count as well is harmless.

This branch has not been deployed

No deployments
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants