Skip to content

Add EXPLAIN PLAN fragments = 1 to print a stateless-worker plan per query fragment - #124893

Merged
diegomestre2 merged 22 commits into
ClickHouse:masterfrom
diegomestre2:explain_distributed_cut
Oct 23, 2026
Merged

diegomestre2 merged 22 commits into
ClickHouse:masterfrom
diegomestre2:explain_distributed_cut

Conversation

@diegomestre2

@diegomestre2 diegomestre2 commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Related: #124179
Related: #124600

PR 2 of the fragment-aware EXPLAIN series described in the RFC above. This PR makes FragmentGraphs visible to users for plans executed by stateless workers (make_distributed_plan = 1) using the new EXPLAIN PLAN fragments = 1.

Changes

  • QueryPlan::cutIntoDistributedPlan is extracted from convertToDistributed: the decision checks, the extraction of IN-subquery sets, makeDistributedPlan, the final_result gather and assertFragmentSerializable. It returns a DistributedPlanCut and consumes the plan. convertToDistributed calls it and then continues exactly as before, so execution is unchanged.
  • After optimization, a plan that stays distributed is cut, turned into a FragmentGraph and rendered by the new explainFragmentGraph. Each fragment is printed with the existing explainPlan under a header.
  • A graph with a single fragment prints exactly what EXPLAIN prints without the option.
  • The send and receive steps set their exchange name as step description.
  • The sets of IN subqueries are not shown yet: the plan names such a set (bid IN subquery1), but no fragment builds it. They come in PR 2b (see the RFC).
  • fragments with json or distributed throws NOT_IMPLEMENTED.

Example output for a broadcast join with aggregation:

Query fragment 3 (main) · tasks=1
GatherSend (final_result)
  Expression ((Project names + Projection))
    MergingAggregated (merge)
      GatherReceive (exchange_2)

Query fragment 2 (stage_2) · tasks=3
GatherSend (exchange_2)
  Aggregating (partial)
    Expression (Before GROUP BY)
      JoinLogical
        ShuffleReceive (exchange_0)
        BroadcastReceive (exchange_1)

Query fragment 1 (stage_1) · tasks=1
BroadcastSend (exchange_1)
  Expression (Change column names to column identifiers)
    ReadFromMergeTree (default.small)

Query fragment 0 (stage_0) · tasks=1
ShuffleSend (exchange_0)
  Expression (Change column names to column identifiers)
    ReadFromMergeTree (default.big)

Exchanges:
  exchange_0: any, streaming, F0 -> F2, buckets 1 -> 3
  exchange_1: broadcast, streaming, F1 -> F2, buckets 1 -> 3
  exchange_2: gather, streaming, F2 -> F3, buckets 3 -> 1
Levels: [F0, F1] -> [F2] -> [F3]

Changelog category (leave one):

  • Experimental Feature

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

Added EXPLAIN PLAN fragments = 1, which prints a plan executed by stateless workers (make_distributed_plan = 1) as one plan per query fragment, with the exchanges between the fragments and their levels. On a query that is not distributed it prints the usual plan, followed by the fallback reason when the planner fell back to local execution.


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

Version info

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

@clickhouse-gh

clickhouse-gh Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [fcf1c86]

Summary: ✅


AI Review

Summary

Adds EXPLAIN PLAN fragments = 1 to show stateless-worker plans by fragment, with exchanges, execution levels, and local fallback reasons. No actionable findings remain at fcf1c86337a9e6477ced5213eb1a2da2cc5bed25; the previous cross-fragment naming issue is addressed. All three new functional tests pass in the Fast test report. Validation used source review and CI results; no local build or tests were run.

Final Verdict

✅ Approve.

@clickhouse-gh clickhouse-gh Bot added the pr-experimental Experimental Feature label Oct 9, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... label Oct 9, 2026
@clickhouse-gh clickhouse-gh Bot added comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. and removed comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... labels Oct 16, 2026
@diegomestre2
diegomestre2 marked this pull request as ready for review October 19, 2026 13:08
@diegomestre2 diegomestre2 added the can be tested Allows running workflows for external contributors label Oct 19, 2026
@clickhouse-gh clickhouse-gh Bot added comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... and removed comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. labels Oct 19, 2026

@Manerone Manerone left a comment

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.

I went though the code and so far everything makes sense. We refactor a bit the code for printing, we split the makeDistributed method, we take care of printing it in the correct Interpreter. I also asked Claude to review, and it found some issues which might be relevant.

@diegomestre2
diegomestre2 requested a review from Manerone October 21, 2026 14:20
@clickhouse-gh

clickhouse-gh Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing fcf1c8633 with master 479496ca1 (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.

Object file sizes

119 object files changed (+153.27 KiB total), 1 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Processors/QueryPlan/Fragments/FragmentGraphExplain.cpp.o new 154.26 KiB +154.26 KiB
src/CMakeFiles/dbms.dir/Processors/QueryPlan/Fragments/FragmentGraph.cpp.o 229.85 KiB 165.89 KiB -63.96 KiB (-27.83%)
src/CMakeFiles/dbms.dir/Interpreters/InterpreterExplainQuery.cpp.o 562.20 KiB 583.98 KiB +21.78 KiB (+3.87%)
src/CMakeFiles/dbms.dir/Processors/QueryPlan/QueryPlanFormat.cpp.o 302.02 KiB 323.32 KiB +21.30 KiB (+7.05%)

719 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

325 translation units recompiled, 2629 s compile time in total, 324 of them have a recent master baseline.

Job report

Comment thread src/Processors/QueryPlan/QueryPlan.h Resolved
@diegomestre2
diegomestre2 added this pull request to the merge queue Oct 23, 2026
Merged via the queue into ClickHouse:master with commit 41b4713 Oct 23, 2026
178 checks passed
@diegomestre2
diegomestre2 deleted the explain_distributed_cut branch October 23, 2026 13:52
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 23, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Nov 2, 2026
…expression-length-bound

Resolve the conflict in QueryPlanFormat.cpp: master's cut-plan fragment walk (ClickHouse#124893) is ported into the iterative buildPrettyNamesForNode as a stage between the children and the child plans, the same order as master's recursive walk.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added comp-explain EXPLAIN and query profiling output: EXPLAIN PLAN / PIPELINE / AST / SYNTAX / ESTIMATE, pretty EXP... and removed comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... labels Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-explain EXPLAIN and query profiling output: EXPLAIN PLAN / PIPELINE / AST / SYNTAX / ESTIMATE, pretty EXP... 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