Repository navigation
Plan based parallel replicas. Part 1 (aggregation) - #108504
Conversation
Add experimental setting `parallel_replicas_exchange_plan`. When enabled, the planner builds a plain local plan (PR construction in `PlannerJoinTree` is skipped) and a post-build phase `ClusterProxy::applyParallelReplicasSplit` replaces the `ReadFromMergeTree` with a `UNION` of a local read and a remote read of the fragment (`ReadFromParallelRemoteReplicasStep`), sharing one `ParallelReplicasReadingCoordinator`. Applicability is decided on the plan. Results are correct and `EXPLAIN` shows the union; remote-replica range coordination for the shipped fragment is not wired yet (WIP). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…pped fragments coordinate ranges Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Workflow [PR], commit [0a1d198] Summary: ❌
AI ReviewSummaryThis PR introduces experimental plan-based parallel replicas by inserting a split marker into the query plan and lowering it into local and remote fragment execution. The core idea is reasonable, but the current implementation still drops several correctness and safety invariants from the existing task-based path: the aggregation rewrite is not gated for all supported modes, and the new remote step no longer preserves replica freshness checks, initiator read limits, or query scalars. I would block merge until those gaps are fixed or explicitly rejected. Findings❌ Blockers
Final VerdictStatus: ❌ Block |
| const bool original_step_was_final | ||
| = aggregating_step->getFinal(); /// Save whether the original AggregatingStep was final or partial | ||
|
|
||
| /// Convert Aggregation step to partial aggregation |
There was a problem hiding this comment.
❌ This rewrites every AggregatingStep above the split into partial aggregation below the split plus MergingAggregatedStep, but it does not carry the safety gates that make that rewrite semantics-preserving. Modes such as max_rows_to_group_by with group_by_overflow_mode = 'any', aggregation-in-order (inOrder / explicitSortingRequired), and grouping sets cannot simply be applied independently on each replica and then merged as if the coordinator had seen one global stream. Once the split is lowered, replicas can apply overflow/order logic locally and the merge can return different keys or ordering than the non-parallel query. Please either reject/skip this rewrite for unsupported aggregation modes, at least matching the guards in tryMakeDistributedAggregation, or leave the aggregation above the split.
…ConvertToDistributedVisitor
…lly catch N x duplication Wrap the correctness queries with SYSTEM ENABLE/DISABLE FAILPOINT slowdown_parallel_replicas_local_plan_read so the remote replicas emit rows before the local read completes, turning the previously flaky "each replica reads everything" bug into a deterministic failure. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
… is deterministic Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
| connection_pool_with_failover, | ||
| query_plan); | ||
|
|
||
| remote_query_executor->setLogger(log); |
There was a problem hiding this comment.
❌ The old parallel-replicas remote step immediately calls setMainTable(storage_id) here; this new path never does. Without a main_table, ConnectionEstablisher skips getTablesStatus, so max_replica_delay_for_distributed_queries and missing/read-only table checks are bypassed for plan-based parallel replicas. A lagging replica can still be picked and read an older part set than the coordinator/local branch. Please carry StorageID into this step and call remote_query_executor->setMainTable(storage_id) before building the pipe.
| // for (const auto & processor : pipe.getProcessors()) | ||
| // processor->setStorageLimits(storage_limits); | ||
|
|
||
| pipeline.init(std::move(pipe)); |
There was a problem hiding this comment.
❌ This new source no longer propagates storage_limits to its RemoteSources. The existing ReadFromParallelRemoteReplicasStep::initializePipeline path calls processor->setStorageLimits(storage_limits) for every remote pipe, and RemoteSource::setStorageLimits is what preserves the initiator-side max_rows_to_read / max_bytes_to_read checks while stripping leaf-only limits. Dropping that wiring means plan-based parallel replicas can read past the query's global read limits as long as each replica stays under its own leaf limit. Please thread storage_limits through ReadFromParallelReplicasStep and set them on the created processors before pipeline.init.
Plan-based parallel replicas requires the analyzer, so under the old-analyzer CI config the split/remote read do not engage and the EXPLAIN checks fail. Force the analyzer in the tests so they are deterministic in every config. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108504&sha=7ae799f7c6a6d4f16ac05617039d076db4ce7e0a&name_0=PR&name_1=Stateless%20tests%20%28amd_llvm_coverage%2C%20old%20analyzer%2C%20s3%20storage%2C%20DBReplicated%2C%20WasmEdge%2C%20parallel%2C%202%2F3%29 PR: #108504 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
@clickhouse-gh The comments you have are valid, but they will be addressed separately. This PR introduces only the first part of the experimental feature. The fixes will land with consequent pull requests. |
There was a problem hiding this comment.
Pull request overview
Adds an experimental “plan-based” parallel replicas execution mode for MergeTree queries, where the planner produces a normal local plan and a later optimization phase inserts/moves a split marker and replaces it with a local+remote execution plan (including shipping a serialized plan fragment to replicas).
Changes:
- Introduces
ParallelReplicasSplitStepand a new optimization (applyParallelReplicas) that transforms plans into local+remote parallel-replicas execution (including aggregation handling). - Adds a new distributed read step (
ReadFromParallelReplicasStep) and extends query-plan serialization to support parallel-replicas reads and safe “skip/drain” deserialization. - Adds multiple stateless regression tests covering remote-only mode, UNION ALL/view branching fragments, constant folding consistency across replicas, and split header correctness.
Reviewed changes
Copilot reviewed 40 out of 40 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/queries/0_stateless/04546_pr_plan_based_remote_only.sql | Regression test for remote-only plan-based parallel replicas execution. |
| tests/queries/0_stateless/04546_pr_plan_based_remote_only.reference | Expected output for remote-only regression test. |
| tests/queries/0_stateless/04545_pr_view_union_local_reads.sql | Regression test for coordinating all UNION ALL branches in local fragment builder. |
| tests/queries/0_stateless/04545_pr_view_union_local_reads.reference | Expected output for UNION-local-reads regression test. |
| tests/queries/0_stateless/04538_pr_plan_based_constants.sql | Ensures query-level constants are consistent across replicas via shipped plan fragment. |
| tests/queries/0_stateless/04538_pr_plan_based_constants.reference | Expected output for constants regression test. |
| tests/queries/0_stateless/04511_pr_view_union_all.sql | Regression test for shipping branching fragments (UNION ALL) and required cloning support. |
| tests/queries/0_stateless/04511_pr_view_union_all.reference | Expected output for branching-fragment regression test. |
| tests/queries/0_stateless/04507_pr_plan_based_split_header.sql | Regression test for split-step header updates when pushed above header-changing steps. |
| tests/queries/0_stateless/04507_pr_plan_based_split_header.reference | Expected output for split-header regression test. |
| tests/queries/0_stateless/04409_pr_plan_based.sql | Base plan-based parallel replicas correctness + EXPLAIN shape assertions. |
| tests/queries/0_stateless/04409_pr_plan_based.reference | Expected output for base plan-based test. |
| src/Server/TCPHandler.cpp | Adds “skip/drain” query plan deserialization path to avoid building runnable plans when skipping/unexpected. |
| src/QueryPipeline/RemoteQueryExecutor.h | Exposes getThrottler for reuse. |
| src/QueryPipeline/RemoteQueryExecutor.cpp | Adds getThrottler and gates parallel-replicas usage on query-plan serialization version support. |
| src/Processors/QueryPlan/Serialization.h | Adds skipping flag to step deserialization context. |
| src/Processors/QueryPlan/Serialization.cpp | Plumbs skip_data through query-plan deserialization into step deserialization context. |
| src/Processors/QueryPlan/ReadFromParallelReplicas.h | New plan step to execute a shipped plan fragment across parallel replicas. |
| src/Processors/QueryPlan/ReadFromParallelReplicas.cpp | Implements remote-parallel-replicas read via RemoteQueryExecutor and coordinator callbacks. |
| src/Processors/QueryPlan/ReadFromMergeTree.h | Adds a serialization-only marker to encode “parallel replicas read” intent into shipped plans. |
| src/Processors/QueryPlan/ReadFromMergeTree.cpp | Serializes parallel-replicas flag; supports lightweight placeholder on skip/drain; rebuilds reads in parallel mode on replicas. |
| src/Processors/QueryPlan/QueryPlanVisitor.h | Adjusts logging include/behavior for visitor debug logging. |
| src/Processors/QueryPlan/QueryPlan.h | Extends deserialize with skip_data; adds cloneSubtree for branching fragments. |
| src/Processors/QueryPlan/QueryPlan.cpp | Implements cloneSubtree using structural cloning. |
| src/Processors/QueryPlan/ParallelReplicasSplitStep.h | New marker step to represent local/remote split boundary in query plans. |
| src/Processors/QueryPlan/ParallelReplicasSplitStep.cpp | Implements pass-through behavior when executed without transformation. |
| src/Processors/QueryPlan/ParallelReplicasLocalPlan.h | Adds helpers to build local/remote plan fragments from a shipped fragment. |
| src/Processors/QueryPlan/ParallelReplicasLocalPlan.cpp | Implements fragment-specific local/remote plan construction and correct read collection under UNION roots. |
| src/Processors/QueryPlan/Optimizations/QueryPlanOptimizationSettings.h | Adds enable_parallel_replicas optimization flag. |
| src/Processors/QueryPlan/Optimizations/QueryPlanOptimizationSettings.cpp | Enables parallel replicas optimization only when initiator-capable + plan-based setting is on. |
| src/Processors/QueryPlan/Optimizations/optimizeTree.cpp | Invokes applyParallelReplicas during second pass; disables it in certain subplans. |
| src/Processors/QueryPlan/Optimizations/applyParallelReplicas.cpp | New optimization: moves split marker upward; rewrites aggregation; converts split into distributed plan. |
| src/Planner/PlannerJoinTree.cpp | Adds plan-based split marker insertion path in planner (instead of old PR plan construction). |
| src/Planner/Planner.cpp | Adds includes/setting plumbing related to plan-based mode. |
| src/Interpreters/ClusterProxy/executeQuery.h | Declares createParallelReplicasPlan for plan-fragment → PR execution plan conversion. |
| src/Interpreters/ClusterProxy/executeQuery.cpp | Implements createParallelReplicasPlan (local+remote union or remote-only), and refactors throttler helper usage. |
| src/Core/SettingsChangesHistory.cpp | Registers new setting in settings history. |
| src/Core/Settings.cpp | Introduces parallel_replicas_plan_based setting definition and description. |
| src/Core/ProtocolDefines.h | Bumps query-plan serialization version and defines minimum version for PR-flag support. |
| src/Client/Connection.h | Exposes server query-plan serialization version on connections. |
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 402/422 (95.26%) · Uncovered code |
Plan-based parallel replicas requires the analyzer, so under the old-analyzer CI config the split/remote read do not engage and the EXPLAIN checks fail. Force the analyzer in the tests so they are deterministic in every config. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108504&sha=7ae799f7c6a6d4f16ac05617039d076db4ce7e0a&name_0=PR&name_1=Stateless%20tests%20%28amd_llvm_coverage%2C%20old%20analyzer%2C%20s3%20storage%2C%20DBReplicated%2C%20WasmEdge%2C%20parallel%2C%202%2F3%29 PR: ClickHouse#108504 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…s-exchange-plan Plan based parallel replicas. Part 1 (aggregation)
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Add experimental plan-based parallel replicas execution for MergeTree queries.