Skip to content

Support distributed execution of plans with a CreatingSets step - #113826

Merged
davenger merged 13 commits into
masterfrom
creating-sets-distributed
Aug 10, 2026
Merged

davenger merged 13 commits into
masterfrom
creating-sets-distributed

Conversation

@davenger

@davenger davenger commented Aug 7, 2026 •

Copy link
Copy Markdown
Member

Related: #109476, #83991

Support distributed execution of plans with a CreatingSets step. Until now make_distributed_plan worked around the missing support by force-enabling rewrite_in_to_join, overriding the user's explicit setting.

The set subquery now executes once on the initiator, the same way local execution and GLOBAL IN build sets: during planning when index analysis requests the set, otherwise in the initiator pipeline at query start, gated before the distributed source sends the worker tasks. The subquery itself runs as a distributed plan when it can be serialized, with deduplication on the workers; otherwise it builds locally on the initiator. The initiator uses the set for index analysis, and the set values ship with the worker tasks as the existing TupleValues record, so the workers can evaluate the IN filter. The GLOBAL IN transfer limits (max_rows_to_transfer/max_bytes_to_transfer, unlimited by default) also bound the shipped set. An over-limit set and a set backed by an external table fail the query with a clear error.

The forced rewrite_in_to_join override is removed. The rewrite remains available as an explicit setting: converting IN to a join lets the optimizer reorder joins across the whole query, which can produce more optimal plans for complex queries. The opposite rewrite, query_plan_convert_join_to_in, is not applied under make_distributed_plan: the set it creates uses its own NULL-matching policy (transform_null_in = false), which the shipped set record does not carry, so a worker would rebuild the set with different membership.

Changelog category (leave one):

  • Experimental Feature

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

Support distributed execution of query plans with IN (subquery) without rewriting it to a JOIN. The set is built once on the initiator and its values are shipped with the worker tasks. The forced rewrite_in_to_join override is removed; the rewrite remains available as an explicit setting.

Version info

  • Merged into: 26.8.1.1119 (included in 26.8 and later)

@clickhouse-gh

clickhouse-gh Bot commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [263a1a1]

Summary: ✅


AI Review

Summary

This PR switches make_distributed_plan from the forced rewrite_in_to_join workaround to shipping ready IN (subquery) sets with worker tasks, and it also tightens distributed-exchange cancellation. The set-shipping path looks coherent after the follow-up fixes, but the PR still regresses multi-stage make_distributed_plan queries that use WITH ... AS MATERIALIZED: their DelayedMaterializingCTEsStep nodes are stripped during task serialization without any replacement MaterializingCTEsStep, so the query can reach a fail-fast ReadFromMemoryStorageStep instead of a materialized CTE.

Findings

❌ Blockers

  • [src/Processors/QueryPlan/QueryPlan.cpp:820-838, 997-1021; src/Processors/QueryPlan/Serialization.cpp:103-107; src/Processors/QueryPlan/ReadFromMemoryStorageStep.cpp:76-90] QueryPlan::optimize now returns early under make_distributed_plan, which skips resolveMaterializingCTEs. The collapsed single-stage branch restores that pass, but the multi-stage branch only re-adds delayed sets. During task serialization every DelayedMaterializingCTEsStep is skipped, so any distributed fragment that reads a materialized CTE loses the writer/gate that ReadFromMemoryStorageStep now requires and hits Reading from materialized CTE ... before its materialization completed. This regresses multi-stage distributed plans that reference WITH ... AS MATERIALIZED in the main query.
    Suggested fix: preserve delayed materialized CTEs alongside delayed sets and resolve them on the initiator-side stub before ReadFromDistributedPlanSource starts worker tasks, or resolve them before fragment cutting and keep the resulting materialization step on the initiator side.
Tests
  • ⚠️ Add a focused stateless test that runs a multi-stage make_distributed_plan query over a WITH ... AS MATERIALIZED source, so this regression is caught by execution rather than by review.
Final Verdict

Request changes.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.60% 86.60% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.80% 78.80% +0.00%

Changed lines: Changed C/C++ lines covered: 290/330 (87.88%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-experimental Experimental Feature label Aug 7, 2026
Comment thread src/Interpreters/PreparedSets.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 37 queries analysed

no significant changes detected. K_source=5 K_base=30 flagged=0/65

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 120eccf1-76d2-47f8-9426-b66070c6b192
  • MIRAI run: 9281b5a7-49f0-46dc-99ac-62943ec10e99
  • PR check IDs:
    • clickbench_327109_1786200281
    • clickbench_327122_1786200281
    • clickbench_327128_1786200281
    • tpch_adapted_1_official_327146_1786200281
    • tpch_adapted_1_official_327165_1786200281

@clickhouse-gh

clickhouse-gh Bot commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 263a1a14c with master c8cea90a1 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 2999ffda7; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes
Binary Master PR Δ
programs/clickhouse-stripped 690.38 MiB 687.40 MiB -2.97 MiB (-0.43%)

Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.

Object file sizes

509 object files changed (+396.08 KiB total), 2 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Processors/Formats/Impl/HiveTextRowOutputFormat.cpp.o new 148.12 KiB +148.12 KiB
src/CMakeFiles/dbms.dir/Processors/QueryPlan/DistributedPlanSets.cpp.o new 52.35 KiB +52.35 KiB
src/CMakeFiles/dbms.dir/DataTypes/Serializations/SerializationMap.cpp.o 2.16 MiB 2.18 MiB +21.09 KiB (+0.96%)
src/CMakeFiles/dbms.dir/DataTypes/Serializations/SerializationArray.cpp.o 228.85 KiB 249.85 KiB +21.00 KiB (+9.18%)
src/CMakeFiles/dbms.dir/DataTypes/Serializations/SerializationTuple.cpp.o 271.66 KiB 292.33 KiB +20.66 KiB (+7.61%)
src/CMakeFiles/dbms.dir/Processors/QueryPlan/SetsSerialization.cpp.o 145.91 KiB 165.55 KiB +19.64 KiB (+13.46%)

737 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

766 translation units recompiled, 2668 s compile time in total, 295 of them have a recent master baseline.

Translation units without a recent master baseline:

  • contrib/llvm-project-cmake/__/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp: 10.5 s
  • contrib/vectorscan-cmake/__/vectorscan/src/nfagraph/ng_violet.cpp: 6.5 s
  • contrib/llvm-project-cmake/__/llvm-project/llvm/lib/CodeGen/AssignmentTrackingAnalysis.cpp: 5.8 s
  • contrib/wasmedge-cmake/__/wasmedge/lib/executor/instantiate/component/component.cpp: 5.8 s
  • contrib/ai-sdk-cpp-cmake/__/ai-sdk-cpp/src/providers/openai/openai_factory.cpp: 5.5 s
  • contrib/s2geometry-cmake/__/s2geometry/src/s2/s2builder.cc: 5.5 s
  • contrib/llvm-project-cmake/__/llvm-project/llvm/utils/TableGen/GlobalISelEmitter.cpp: 5.5 s
  • contrib/wasmedge-cmake/__/wasmedge/lib/executor/engine/threadInstr.cpp: 5.3 s
  • contrib/wasmedge-cmake/__/wasmedge/lib/executor/instantiate/table.cpp: 5.2 s

Job report

/// added here and expanded the ordinary way, so this pipeline builds them before the
/// distributed source sends the worker tasks with their values. The cache is skipped:
/// a cached set has no values.
bool has_sets_to_build = false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optimize() now returns before resolveMaterializingCTEs(), but the multi-stage make_distributed_plan path never adds an initiator-side replacement for those delayed CTE writers the way it does for delayed sets here. That leaves any WITH ... AS MATERIALIZED referenced by the main query or by one of these shipped IN subqueries without a MaterializingCTEsStep: fragment serialization strips DelayedMaterializingCTEsStep entirely, and the first ReadFromMemoryStorageStep then hits the fail-fast "Reading from materialized CTE ... before its materialization completed" exception instead of seeing populated storage.

We need to preserve and resolve delayed materialized CTEs alongside the delayed sets before the distributed source starts sending worker tasks.

rhs_type = std::make_shared<DataTypeTuple>(rhs_tuple->getElements());

if (!lhs_type->equals(*rhs_type))
throw Exception(ErrorCodes::INCORRECT_DATA,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be happy to avoid this change, it possible.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created a follow-up PR to test an alternative fix #114229

return plan;
}

void FutureSetFromSubquery::prepareForDistributedPlan(const ContextPtr & context)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can consider adding a setting: fill sets on the initiator always, or send as a plan, when possible.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be tricky in case of non-deterministic sub-queries that might return different results on for execution on different workers. Also the decision to when to execute the sub-query ideally should be driven by cost based optimizer. So I would prefer to support simple logic for now and extend it later in follow-ups

@davenger
davenger added this pull request to the merge queue Aug 10, 2026
Merged via the queue into master with commit 74afa81 Aug 10, 2026
355 of 356 checks passed
@davenger
davenger deleted the creating-sets-distributed branch August 10, 2026 17:33
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-experimental Experimental Feature 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.

3 participants