Repository navigation
Materialize a reused MATERIALIZED CTE before a parallel replicas read with a local plan sends it to the replicas - #123121
Conversation
…the replicas A MATERIALIZED CTE read more than once is an external table of the query context, and the query sent to the other replicas names its temporary table, so `RemoteQueryExecutor::sendExternalTables` must find it built. It skips a CTE that is not built yet. `executeQueryWithParallelReplicas` builds the initiator's local plan with its own (non-subquery) planner over a clone of the shipped query tree, so the local plan plants its own `DelayedMaterializingCTEsStep`. That plan is hidden in `ReadFromLocalParallelReplicaStep` and optimized before the outer plan's `resolveMaterializingCTEs`, so it claims the writer and gates only the local read. The remote read is the other input of the `Union`, starts immediately, the CTE is skipped and every other replica fails with `Unknown table expression identifier '_materialized_cte_...'`. The query only survives when the local replica finishes first and the unused replicas are cancelled, which is why `05182` and `05230` fail on ~1-2% of ParallelReplicas-lane runs and the query from ClickHouse#112642 fails every time on a 1M-row table. Strip the delayed steps of every CTE of the shipped tree from the local plan, the same fix `ReadFromMerge` applies to its child plans (ClickHouse#113558), so the outer plan's gate owns the writer and orders both reads. The set is transitive: a CTE read only inside another CTE's body must be stripped too, otherwise that CTE's writer, now under the outer gate, reads a CTE whose writer stays in the local branch. The collector moves from `StorageMerge.cpp` to `MaterializingCTEStep` so both callers share it. Closes ClickHouse#112642 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop the call-site line that narrated why the local plan would claim the CTE writer. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 1 finding).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-10:20260930-172300 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-7:20260930-153900 |
|
Workflow [PR], commit [eb9385e] Summary: ❌
AI ReviewSummaryThis PR fixes the parallel-replicas/local-plan race for Findings
Final Verdict
|
Build profile diff (arm_release)Commit See the job log for details. |
CI finish ledger - abc4f98Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
146 of 146 check runs completed (104 passed, 41 not selected, 1 red: the shard above), Session id: cron:our-pr-ci-monitor:20260930-223102 |
The last assertion checked `RuntimeFilterRowsChecked >= 2000` to prove the runtime filter was applied to the t1 side (2001 rows) rather than to t2 (5 rows). Every t1 row passes the filter, so after a block whose pass ratio exceeds join_runtime_filter_pass_ratio_threshold_for_disabling (0.7) the filter skips later blocks within its budget (rows checked times join_runtime_filter_blocks_to_skip_before_reenabling) and books them in RuntimeFilterRowsSkipped, not in RuntimeFilterRowsChecked. With the default layout t1 arrives in one block, so at most one row is skipped. With optimize_on_insert = 0 the part has level 0 and FINAL reads it through the merging path; with a wide part and index_granularity = 1493 that read yields a 1493-row and a 507-row block, and RowsChecked is 1494. This is what failed in Stateless tests (arm_binary, parallel) on PR ClickHouse#123121, where the runner's diagnosis narrowed it to these three settings (208/208 failing). In the failing job's query_log, Checked + Skipped was 2001 on all 470 executions of the probe, across every layout the runner tried. Sum the two counters. The threshold still separates t1 from the reversed join orientation (5) and from a filter that is planted but never applied (0). Co-Authored-By: Claude Opus 5.5 <[email protected]>
74ed031
Closes: #112642
Related: #120022
Related: #120179
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
A query that reads a
MATERIALIZEDCTE more than once, for example inside anINsubquery, now works with parallel replicas when the initiator also reads locally (parallel_replicas_local_plan = 1). The other replicas no longer fail withUNKNOWN_TABLEon the CTE's temporary table. Closes #112642.Description
05182_lightweight_update_materialized_cte_in_subqueryand05230_materialized_cte_shared_by_two_ctes_in_set_subqueryfail withCode: 60 Unknown table expression identifier '_materialized_cte_c_...'in the ParallelReplicas lanes: FAIL on the arm_binary lane of #122853, and hidden asOKon master by the coverage flaky rule (example). With a 1M-row table the query from #112642 fails in 39 of 40 runs on master (the replicas fail in all 40).The query sent to the replicas reads the CTE by its temporary table name, so the CTE must be written before it is sent. The initiator's local plan is built by its own planner and optimized first, so it claims the CTE's writer and gates only the local read. The remote read starts at once,
sendExternalTablesskips the unbuilt CTE, and the replicas throw.The fix strips those CTEs from the local plan, so the outer plan's gate owns the writer and orders both reads. This is what
ReadFromMergealready does for its child plans (#113558); the helper moves next toremoveDelayedMaterializingCTEsStepForso both callers share it.Validation: the new test fails on master in all six shapes (two references, chain/diamond,
UNION ALL,serialize_query_plan, subquery inFROM, parallel read inside another CTE) and passes 50/50 with randomized settings in the default and ParallelReplicas configurations. Removing the change or stripping only the directly named CTEs makes it fail again.Not fixed here: #120022 (
parallel_distributed_insert_select = 2does not send external tables) and #120179.Workflow [PR]
Sync PR [sync-upstream/pr/123121]
Version info
26.10.1.2331-master(included in26.10and later)