Skip to content

Plan-based parallel replicas: do not ship a read of a temporary table - #129223

Merged
devcrafter merged 8 commits into
masterfrom
pb-keep-temporary-table-reads-local
Nov 6, 2026
Merged

devcrafter merged 8 commits into
masterfrom
pb-keep-temporary-table-reads-local

Conversation

@devcrafter

@devcrafter devcrafter commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

With parallel_replicas_plan_based = 1, a read of a temporary table was shipped to the replicas. A temporary table exists only in the session of the initiator, so every replica rejects the fragment with DATABASE_ACCESS_DENIED (Direct access to _temporary_and_external_tables database is not allowed). The client sees the error only when a replica's failure arrives before the local replica finishes the read, so with parallel_replicas_local_plan = 1 it fails intermittently (almost always with max_threads = 1), and with parallel_replicas_local_plan = 0 it always fails.

mergeTreeReadCanBeShipped now rejects a read of the temporary database, so the read never gets into a fragment: no split marker is planted above it, and a join whose broadcast side reads it is not lifted into one. The other subtrees of the query are still distributed, e.g. an aggregation over a regular table that is then joined with a temporary table. A Memory (or Log, or external) temporary table was already kept out of fragments, because its read is not serializable. A temporary table used only through an IN set is left to #129273.

05497_pr_plan_based_temporary_table_read_stays_local runs with parallel_replicas_local_plan = 0, so a shipped read of a temporary table always fails: a read of a temporary table has no ReadFromParallelReplicas, an aggregation joined with a temporary table is distributed while the join stays local, and joins with MergeTree and Memory temporary tables return the right result. It fails on master every time. 00542_access_to_temporary_table_in_readonly_mode, 02525_different_engines_in_temporary_tables, 02766_prql, 02995_bad_formatting_union_intersect and 03156_nullable_number_tips pass with plan-based in both parallel_replicas_local_plan modes; they are intermittent failures in the plan-based job of #128413.

Related: #128413
Related: #129273

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

🤖 Generated with Claude Code


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

Version info

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

@clickhouse-gh

clickhouse-gh Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [189b47b]

Summary: ✅


AI Review

Summary

Reviewed head 189b47bc211777e14d7ad960f51f2044a52b4c35. The change keeps temporary MergeTree reads out of shipped fragments while preserving parallel execution of eligible subtrees. No unresolved findings in the scoped change. The updated regression test passes in the current head's Fast test report. The previously discussed temporary-table-backed IN sets are explicitly deferred to issue #129273.

Final Verdict

✅ Approve. No new inline findings.

@clickhouse-gh clickhouse-gh Bot added pr-not-for-changelog This PR should not be mentioned in the changelog comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). labels Nov 4, 2026
@devcrafter devcrafter changed the title Plan-based parallel replicas: keep a read of a temporary table local Plan-based parallel replicas: keep a query that reads a temporary table local Nov 4, 2026
@devcrafter
devcrafter requested a review from groeneai November 4, 2026 11:46

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

Reviewed at ef768bd: I built it (debug) and ran it against a 3-replica localhost cluster with process_query_plan_packet. 05497 passes with parallel_replicas_local_plan 0 and 1, and a regular Set or Join table is still read by the replicas.

  1. A temporary Set inside a lambda still ships and fails with DATABASE_ACCESS_DENIED, and not only in the folded form from the bot's comment. Same fixtures as 05497, local result in the comment:
-- the lambda also captures `v`, so it stays a `FunctionCapture` node instead of being folded
SELECT count() FROM t_pr_regular WHERE arrayExists(x -> (x IN t_pr_tmp_set) AND (v >= 0), [k]); -- 10
SELECT count() FROM t_pr_regular PREWHERE arrayExists(x -> x IN t_pr_tmp_set, [k]); -- 10
SELECT sumIf(v, arrayExists(x -> x IN t_pr_tmp_set, [k])) FROM t_pr_regular; -- 450
SELECT count() FROM t_pr_regular ARRAY JOIN arrayFilter(x -> x IN t_pr_tmp_set, [k]) AS a; -- 10

ActionsDAG::serialize registers the sets of a FunctionCapture body (ActionsDAG.cpp:5610) and of a folded ColumnFunction's body and captured columns (:5370, :5377), while dagReadsTemporaryTable looks only at top-level set nodes. The inline suggestion follows the same three places. With it, all of the above plus the bot's folded and nested-lambda queries stay local and return the local result, a regular Set (plain and inside a lambda) is still distributed, and 05497 passes in both modes. It needs #include <Columns/ColumnFunction.h> and #include <Functions/FunctionsMiscellaneous.h>. The first query above is worth adding to 05497.

  1. Not covered either: joinGet(t_pr_tmp_join, 'w', k) and joinGetOrNull over a temporary Join table fail on the replica with UNKNOWN_TABLE (Table default.t_pr_tmp_join does not exist). This fails the same way with parallel_replicas_plan_based = 0, so it may be out of scope here.

devcrafter and others added 2 commits November 4, 2026 13:49
This PR keeps only a query that reads a temporary table directly local.

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

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

Re-reviewed at c49f10c (debug build, 3-replica localhost cluster). Moving the IN cases to #129273 is fine with me.

05497 matches the reference with parallel_replicas_local_plan 0 and 1 (also with max_threads = 1) and fails on master 4f752f7 with DATABASE_ACCESS_DENIED in both modes. I also checked 23 direct-read shapes with a temporary MergeTree table: subquery in FROM, CTE, UNION ALL/DISTINCT, INTERSECT, EXCEPT, GLOBAL JOIN, LEFT/RIGHT/FULL/CROSS/PASTE JOIN, correlated EXISTS, aggregated subquery, 3-way join, view(), ReplacingMergeTree ... FINAL, parallel_hash/full_sorting_merge/grace_hash, window and ARRAY JOIN above the join, merge() joined with it. All stay local and return the same result as enable_parallel_replicas = 0 in both modes; 15 of them fail on master with local_plan = 0. A join of regular tables is still distributed.

One behaviour change, inline.

@clickhouse-gh

clickhouse-gh Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 189b47bc2 with master 08d8752ac (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, 18 s compile time in total, 7 of them have a recent master baseline.

Job report

@devcrafter devcrafter changed the title Plan-based parallel replicas: keep a query that reads a temporary table local Plan-based parallel replicas: do not ship a read of a temporary table Nov 4, 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.

Re-reviewed at 189b47b (debug build, 3-replica localhost cluster). 05497 matches the reference with parallel_replicas_local_plan 0 and 1 (max_threads 1 and 8) and fails on master 4f752f7 in every run of both modes.

The 23 shapes from my earlier review and 18 more (a temporary table on the coordinated or the broadcast side, nested and 3-way joins, runtime filters, ORDER BY ... LIMIT and GROUP BY above the join, SEMI/ANTI/ANY, join_use_nulls, query_plan_join_swap_table false/auto/true) return the same result as without parallel replicas in both modes, with the regular reads still going through ReadFromParallelReplicas (an aggregation, the coordinated side of a join, a UNION ALL branch). Joins with Memory, Log and --external temporary tables now get the same plan as on master and are correct, so my ReadFromMemoryStorageStep point is resolved.

@devcrafter
devcrafter requested a review from nickitat November 4, 2026 20:05
@nickitat nickitat self-assigned this Nov 6, 2026
@devcrafter
devcrafter added this pull request to the merge queue Nov 6, 2026
Merged via the queue into master with commit cdedc6a Nov 6, 2026
185 checks passed
@devcrafter
devcrafter deleted the pb-keep-temporary-table-reads-local branch November 6, 2026 18:14
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 6, 2026
devcrafter added a commit that referenced this pull request Nov 10, 2026
Add the subquery query result cache tests: the cache is not used with plan-based parallel replicas since #130376.
Add `03707_autopr_unsupported_cases` (the `LIMIT` is shipped since #129811) and `03401_..._no_sorting` (#128537).
Remove the tests that pass now: the temporary table ones (#129223), the `LIMIT` ones (#129811), and those fixed on master.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Nov 24, 2026
Since ClickHouse#129223 plan-based parallel replicas keep the read of a temporary table
on the initiator (`mergeTreeReadCanBeShipped`) and still distribute the other
subtrees of the query, e.g. an aggregation over a regular table joined with a
temporary table. Turning parallel replicas off for the whole query in
`DisableParallelReplicasPass` took that away and broke
`05497_pr_plan_based_temporary_table_read_stays_local` (10/10 runs). The
finder now fires only for task-based and custom-key parallel replicas, the
modes that send the query to the replicas as is.

`05345_parallel_replicas_temporary_table` covers the task-based mode, so it
sets `parallel_replicas_plan_based = 0` and keeps its result in the
plan-based CI configuration.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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-not-for-changelog This PR should not be mentioned in the changelog 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