Repository navigation
Decide parallel replicas eligibility with the subquery's own context - #110020
alexey-milovidov merged 12 commits into
Conversation
…a view over MergeTree In a UNION whose subqueries read a view over MergeTree, the parallel-replicas decision (getSupportingParallelReplicasQueries / findQueryForParallelReplicas) evaluated view eligibility with the top query context, while the plan build (findTableForParallelReplicas) re-evaluated it while descending into the chosen subquery. When a subquery overrode parallel_replicas_allow_view_over_mergetree via per-query SETTINGS, the two disagreed: the decision picked a candidate but the build found no eligible table, throwing LOGICAL_ERROR (server abort in debug/stress builds). Track the enclosing (sub)query context while descending in both traversals so per-subquery SETTINGS are honored consistently in the decision and the build. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-3:20260710-153800 |
|
cc @nickitat @devcrafter — could you review this? It fixes a LOGICAL_ERROR |
|
Workflow [PR], commit [2e7e50b] Summary: ✅
AI ReviewSummaryThis PR fixes the parallel-replicas context mismatch in the planner and the matching view-eligibility bug in Final Verdict✅ Approve. No new inline findings. LLVM Coverage ReportMeasured on commit 2e7e50b.
Changed lines: Changed C/C++ lines covered: 77/79 (97.47%) · Uncovered code |
Tag the test long and shrink the fixture from 1000 to 10 rows. The crash this test reproduces is triggered by the per-query parallel_replicas_allow_view_over_mergetree mismatch across UNION subqueries over a view, which is independent of row count, so a tiny fixture still deterministically hits it. The long tag exempts the test from the 180s flaky-check limit and reduces its repeat count, fixing the TSan flaky-check timeout. Co-Authored-By: Claude Opus 4.8 <[email protected]>
CI finish ledger — 9550ec3Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens. Only
Session id: cron:our-pr-ci-monitor:20260711-040000 |
CI finish ledger - 11b9d3eEvery failure below has an owner: a fixing PR, or a full-effort fix task whose
Session id: cron:our-pr-ci-monitor:20260729-083000 |
# Conflicts: # src/Planner/findParallelReplicasQuery.cpp
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 51/52 (98.08%) · Uncovered code |
CI finish ledger - 2982e4cEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task CI is fully finished at this head: 174 check-runs, 156 success, 18 skipped, zero failures.
Session id: cron:our-pr-ci-monitor:20260801-233000 |
There was a problem hiding this comment.
Pull request overview
Fixes a LOGICAL_ERROR (“Can't determine table for parallel replicas”) that could occur when parallel-replicas eligibility for a view-over-MergeTree is affected by per-subquery SETTINGS inside UNION/set-operation queries, by ensuring both the candidate-selection and table-finding traversals evaluate eligibility with the correct enclosing (sub)query context.
Changes:
- Track and propagate the current (sub)query
Contextwhile descending query-tree nodes when selecting parallel-replicas candidate subqueries. - Track and propagate the current (sub)query
Contextwhile descending query-tree nodes when locating the eligible table for parallel replicas (including JOIN branch traversal). - Add a stateless regression test covering per-subquery overrides of
parallel_replicas_allow_view_over_mergetreeacross set operations.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/Planner/findParallelReplicasQuery.cpp |
Keep per-subquery Context aligned between candidate selection and build-time table discovery to avoid mismatched eligibility decisions. |
tests/queries/0_stateless/04401_parallel_replicas_view_over_mergetree_per_query_setting.sql |
New regression test reproducer for per-subquery SETTINGS divergence in set operations over a view on MergeTree. |
tests/queries/0_stateless/04401_parallel_replicas_view_over_mergetree_per_query_setting.reference |
Expected output for the new regression test. |
Address the Copilot review on this PR.
Drop the `no-parallel-replicas` and `long` tags from
04401_parallel_replicas_view_over_mergetree_per_query_setting.
`no-parallel-replicas` made the test vacuous in exactly the configuration it
exists for. tests/clickhouse-test:2788 skips a test carrying that tag when the
runner is given `--no-parallel-replicas`, and that flag is what the
`ParallelReplicas` job passes (ci/jobs/functional_tests.py:173). So the two
`amd_llvm_coverage, ParallelReplicas, s3 storage, {parallel,sequential}` jobs
were the only ones exercising the parallel-replicas server profile, and the tag
excluded the test from both. CIDB confirms it: on head 2982e4c the test is
SKIPPED there with "smth is not supported with parallel replicas".
`long` was not needed either. The tag was added when the fixture was
numbers(1000), together with the shrink to numbers(10) that actually fixed the
180s flaky-check failure. The threshold at :3453 compares a single run's
duration, not the sum over reruns, and the test now takes about 3s. The tag also
cost coverage of its own: it reduces the flaky-check repeat count to 10% (:4270,
--long-test-runs-ratio 0.1) and makes every `--no-long` job skip the test
(:2837), which is what the SKIPPED "not running long tests" rows on
Fast test and several coverage jobs show.
Both tags had to go together: the two ParallelReplicas jobs are also
`--no-long` jobs, since `amd_llvm_coverage` sets is_llvm_coverage and the
non-per-test-coverage branch appends `--no-long`
(ci/jobs/functional_tests.py:532). Removing only one tag would have left the
test skipped there for the other reason.
Also reword two comments that described the failure as a crash. It is a
LOGICAL_ERROR exception, handled in release builds, and the abort only happens
in debug and sanitizer builds. Trim the two comments in
findParallelReplicasQuery.cpp to the local invariant, since the motivation and
the root cause belong in the commit message rather than in the source.
Now that the test is no longer tagged `no-parallel-replicas`, it is subject to setting randomization, and two gaps let it pass without exercising the fix. Pin `automatic_parallel_replicas_mode = 0`. The randomizer picks mode 2 with probability 0.25 (tests/clickhouse-test:1615), and that mode only collects statistics instead of using parallel replicas (:1699-1702). Measured on a master binary without this fix: with mode 2 injected the test returned 0 and passed, so roughly a quarter of runs would have silently tested nothing. With the setting pinned the same binary fails again (the in-test SET takes precedence over runner injection). Pin `parallel_replicas_local_plan` too, which is randomized at :1554. Assert the plan actually keeps `ReadFromRemoteParallelReplicas` for the eligible-view query. The previous assertions were all on result values, which hold just as well when the planner declines parallel replicas and runs locally, so they could not distinguish the fix from a silent fallback. Checked that the new assertion discriminates: it yields 1 with parallel replicas enabled and 0 with them disabled. Verified against the pre-fix binary with both randomized values of `automatic_parallel_replicas_mode` injected: the test fails in both cases.
Add a plan assertion for `parallel_replicas_allow_view_over_mergetree = 0`, so a guard that silently stopped honoring the setting would be caught. The discriminator is not whether parallel replicas run at all: measured, the remote step is present for both values of the setting, so an assertion requiring no `ReadFromRemoteParallelReplicas` step on the disabled path is not satisfiable. What the setting changes is which relation the remote step reads. Enabled it sends `SELECT count() ... FROM default.v_pr_view`, disabled it sends the rewritten inner query over `default.mt_pr_view`. This matches how 04077_pr_view_union_ineligible distinguishes eligible from ineligible views. Checked that the new assertion can fail: it yields 0 as written and 1 when the view is sent for a query that disabled the setting.
StorageView::getUnderlyingMergeTreeStorageForParallelReplicas walked the view's resolved inner query tree with a single captured outer context, so a SETTINGS clause written inside the view definition was ignored when deciding whether the underlying MergeTree table is eligible for parallel replicas. A view whose body sets parallel_replicas_for_non_replicated_merge_tree = 0 was therefore still read through the coordinated remote path, while each follower evaluated StorageMergeTree::read with its own view-body context, found the setting disabled, and read the whole table locally. The initiator concatenated those full results, so the query silently returned max_parallel_replicas times too many rows. Thread the effective context through the find_storage lambda: take it as a parameter, refresh it from QueryNode::getContext at QUERY and UnionNode::getContext at UNION, and forward it to the UNION branch recursion, the nested-view recursion and isTableNodeEligibleForParallelReplicas. The parameter form is used rather than a mutated local so that one UNION branch's context cannot leak into the next. This mirrors the rule already applied to the outer planner traversals in findParallelReplicasQuery.cpp.
The block restated the motivation that already lives in the previous commit message. Keep only what a reader needs at that spot.
Covers a single-level view, a nested view, three UNION shapes and FINAL, with both the returned values and the relation that is shipped to the replicas. parallel_replicas_local_plan is deliberately left to the runner's randomization rather than pinned: every assertion was measured identical at both values, so not pinning is strictly stronger coverage than the sibling 04066 form. The structural assertions are the fails-without-fix oracle: they differ on master for all four opt-out shapes at both values of that setting. The value rows are a correctness check, not the oracle, because the inflation a broken build produces is not deterministic across runs.
CI finish ledger - f2edec9Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
156 success, 18 skipped, 1 failure out of 175 check-runs. Session id: cron:our-pr-ci-monitor:20260803-200000 |
Internal second-model review (2 findings, both disagreed with evidence)An independent model reviewed this diff cold against the PR contract. Both findings are about the
|
| view | lp=0 |
lp=1 |
expected |
|---|---|---|---|
v_plain |
v_plain, 300 |
v_plain, 300 |
v_plain, 300 |
v_set |
empty, 300 | empty, 300 | empty, 300 |
v_nested |
empty, 300 | empty, 300 | empty, 300 |
v_union |
mt_g2, 600 |
mt_g2, 600 |
mt_g2, 600 |
v_union_both |
empty, 600 | empty, 600 | empty, 600 |
v_final |
empty, 300 | empty, 300 | empty, 300 |
Zero variance per cell and no difference between the two values, so there is nothing for the
randomization to destabilize. The pre-fix arm is red at both values as well (v_set and
v_nested return 900 instead of 300 and name the view instead of nothing), so no path is left
untested at either value. End to end: 100 of 100 randomized runs pass, and system.query_log
confirms the randomizer really did exercise both values rather than silently taking one (1727
queries at 0, 4804 at 1).
Pinning it would remove randomization coverage that is currently green rather than add determinism
that is currently missing. Note 04401 in this same PR does pin it: there the oracle was measured
to be sensitive to the value, so the pin is load-bearing. Same reasoning, opposite conclusion, both
driven by measurement rather than by a blanket rule.
⚠️ 04401's disabled-setting probe could pass via a fully local fallback
The claim: the disabled arm counts only remote-plan rows naming v_pr_view, and a completely local
plan would also yield zero, so the probe does not prove the promised underlying-table path.
Disagree on the premise. That exact question was measured while this assertion was being
written: ReadFromRemoteParallelReplicas is present for both values of
parallel_replicas_allow_view_over_mergetree. The setting changes what is shipped (enabled sends
FROM default.v_pr_view; disabled sends the rewritten inner query over default.mt_pr_view), not
whether parallel replicas run. A fully local fallback is not a state this query reaches, so it
cannot be the reason the count is zero. Both arms were also mutation-tested in the round that added
them: the enabled arm goes 1 to 0 with parallel replicas disabled, and the disabled arm goes 0 to 1
if the guard stops honoring the setting.
The stronger form the review suggests, asserting mt_pr_view by name on the disabled arm, is a fair
increment and matches the shape 04077_pr_view_union_ineligible uses. It is not a correctness gap
in what is here, so I have not changed that file in this round.
Also checked and clean
Context threading is a parameter rather than a mutated capture, so a UNION branch cannot leak its
context into a sibling; union_node.getContext() is safe to seed from because
QueryTreeBuilder.cpp:201 builds it from a copy of the enclosing context, so no outer override is
lost. The join_contexts stack is in strict lockstep with join_nodes (two pushes, one paired pop).
All three external callers of the helper already pass their own outer context, so none needed
changing. automatic_parallel_replicas_mode = 0 is load-bearing, not boilerplate: mode 2 is picked
in 25% of runs and only collects statistics, under which every assertion would pass without
exercising the paths under test.
Session id: cron:clickhouse-review-slot-9:20260803-221800
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units8 translation units recompiled, 21 s compile time in total, 8 of them have a recent master baseline. |
|
Dear @devcrafter, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
|
New occurrence of the logical error this PR fixes, on #119846 at Measured today on master |
|
New occurrence of the logical error this PR fixes, Measured on master |
|
Dear @devcrafter, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
Related: #100815
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Parallel replicas now honor a
SETTINGSclause that changes table eligibility when it is written inside a subquery or a view body. Such a clause was ignored, so a query over a view whose body setsparallel_replicas_for_non_replicated_merge_tree = 0, or setsparallel_replicas_allow_view_over_mergetree = 0and reads a further view, returnedmax_parallel_replicastimes too many rows, and aUNIONover a view overridingparallel_replicas_allow_view_over_mergetreeper subquery could throwCan't determine table for parallel replicas.Description
One root cause in three traversals: parallel-replicas table eligibility was decided with the OUTER query's context, while the read executes with the per-(sub)query context a
SETTINGSclause applies to.Wrong results.
StorageView::getUnderlyingMergeTreeStorageForParallelReplicaswalked a view's resolved inner tree with a single captured outer context, so aSETTINGSclause in the view body was ignored and the view was still judged eligible. Each follower then read the whole table locally and the initiator concatenated those results, soSELECT count()silently returnedmax_parallel_replicastimes too many rows. Two settings carry that opt-out:parallel_replicas_for_non_replicated_merge_tree, andparallel_replicas_allow_view_over_mergetreeonce the body reads a further view.Logical error. In a
UNIONwhose subqueries read a view over MergeTree,getSupportingParallelReplicasQueriesdecided eligibility with the outer context whilefindTableForParallelReplicasre-decided it while descending. A per-subquery override made the two disagree: a subquery was chosen but no eligible table found. Found by the AST fuzzer,Stress test (amd_debug)on master, STID 3634-4123: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=06136dac55ba4fb8544dc1697cd28b2ef769eb17&name_0=MasterCI&name_1=Stress%20test%20%28amd_debug%29The fix tracks the enclosing (sub)query context in all three traversals, and refuses a nested view whose enclosing body disallows a view over MergeTree. Neither half works alone: the planner half removes the inflated counts, the storage half the
UNION-in-a-view logical error that the buggy helper had been suppressing.No new setting or default change, so no
SettingsChangesHistory.cppentry. The nested-view cases keep using parallel replicas, coordinated on the underlyingMergeTreetable instead of the view. Validated on a 3-replica cluster:04728fails without the fix and passes with it, over 100 randomized runs.Workflow [PR]
Sync PR [sync-upstream/pr/110020]
Version info
26.10.1.2097-master(included in26.10and later)