Skip to content

Add the query fragment graph model for a fragment-aware EXPLAIN of distributed plans - #124600

Merged
diegomestre2 merged 25 commits into
ClickHouse:masterfrom
diegomestre2:explain_distributed
Oct 20, 2026
Merged

diegomestre2 merged 25 commits into
ClickHouse:masterfrom
diegomestre2:explain_distributed

Conversation

@diegomestre2

@diegomestre2 diegomestre2 commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Related: #112738
Related: #124179

First of a series of pull requests that teach EXPLAIN about the stages of a query planned with make_distributed_plan and executed on stateless workers. The design is in the RFC above.

For now we only create the FragmentGraph and output in the format below for test and validation only:

LevelsWithSetFragment: an IN (subquery) set fragment on the initiator, between two worker stages:
query fragments: 4
  F0 stage_0  worker     distributed  tasks=8
  F1 main     initiator  single node  tasks=1  needs F0 via exchange_0 (streaming)  description: k IN (SELECT k FROM small)
  F2 stage_2  worker     distributed  tasks=8  needs F1 via exchange_1 (blocking: set must be complete)
  F3 main     initiator  distributed  tasks=1  needs F2 via exchange_2 (streaming)
exchanges: 3
  exchange_0  F0 -> F1  gather      pipelined  1 -> 1
  exchange_1  F1 -> F2  set values  pipelined  1 -> 1
  exchange_2  F2 -> F3  gather      pipelined  1 -> 1
levels: [F0] [F1] [F2] [F3]

Dump: a hand-built graph numbered the way a real plan is (workers first, initiator last), with a broadcast join build side and an aggregation-merge gather that has a row estimate:
query fragments: 3
  F0 stage_0  worker     distributed  tasks=1
  F1 stage_1  worker     distributed  tasks=8  needs F0 via exchange_0 (blocking: build side of JoinLogical)
  F2 main     initiator  distributed  tasks=1  needs F1 via exchange_1 (blocking: merg
exchanges: 2
  exchange_0  F0 -> F1  broadcast  pipelined  1 -> 8
  exchange_1  F1 -> F2  gather     pipelined  8 -> 1  ~5000 rows
levels: [F0] [F1] [F2]

DumpExchangeKinds: covers every way an exchange kind is rendered (hash keys, any + materialised, gather with merge sort, set values):
query fragments: 5
  F0 stage_0  worker     distributed  tasks=1
  F1 stage_1  worker     distributed  tasks=1
  F2 stage_2  worker     distributed  tasks=1
  F3 stage_3  worker     distributed  tasks=1
  F4 main     initiator  distributed  tasks=1  needs F0 via exchange_0 (streaming), F1 via exchange_1 (streaming), F2 via exchange_2 (streaming), F3 via exchange_3 (blocking: set must be complete)
exchanges: 4
  exchange_0  F0 -> F4  hash(a, b)            pipelined     1 -> 1
  exchange_1  F1 -> F4  any                   materialised  1 -> 1
  exchange_2  F2 -> F4  gather(merge: k ASC)  pipelined     1 -> 1
  exchange_3  F3 -> F4  set values            pipelined     1 -> 1
levels: [F0, F1, F2, F3] [F4]

BuildSingleFragment: the fallback when the query isn't distributed:
query fragments: 1
  F0 main  initiator  single node  tasks=1  not distributed: no exchanges
exchanges: 0
levels: [F0]

Motivation

EXPLAIN PLAN today prints the single logical tree with ShuffleExchange, BroadcastExchange and GatherExchange steps inside it.

The model

src/Processors/QueryPlan/Fragments/FragmentGraph.h defines a value object built from a DistributedQueryPlan:

  • QueryFragment: one stage, with its id, the executor's stage name for joins with logs and traces, the role (Initiator or Worker), the execution location (Distributed for a stage run as tasks, SingleNode for a plan that was not cut and runs as one pipeline on the initiator, with its fallback reason), an optional description (e.g. the set the fragment fills), the number of tasks, its children (the ids of the exchanges it reads) and a pointer to the stage's plan.
  • FragmentExchange: the edge between two fragments. Every dependency goes through exactly one exchange, so the exchange carries the dependency too: the executor's exchange_N name, the producer and consumer fragments, the kind (Hash, Broadcast, Gather, Any, and SetValues for a built set shipped inside the reader's tasks), the keys or the merge order, the mode (Pipelined or Materialised), the bucket counts, whether it is Streaming or Blocking with the reason (e.g. "build side of JoinLogical" or "set must be complete"), and ExchangeStatistics with the estimated rows (actual values come with EXPLAIN ANALYZE later).
  • FragmentGraph: fragments and exchanges sorted by id. A plan that was not distributed is the degenerate graph of one SingleNode fragment carrying the fallback reason.
  • validateFragmentGraph throws LOGICAL_ERROR unless every child of a fragment is an exchange into it from a known fragment, every exchange is read by exactly one fragment, the graph is acyclic, and its root (the one fragment whose output nobody reads) is an initiator fragment. Several fragments may have the initiator role, since a set fragment runs on the initiator too.
  • computeLevels returns the topological levels for the renderers; they are not stored in the model.

Building the graph

  • buildFragmentGraph walks the stages and maps the executor's exchange and stage descriptions onto the model. For every receive step it walks towards the output of the fragment to the first step that needs all of its input on that way, keeping the input position at every parent.
  • classifyDependency is a pure function over that step: the build side of a join, a hash aggregation (not in order), a full sort and a MergingAggregated that does not merge bucket by bucket are blocking; when no such step exists the exchange streams. A materialised exchange and a SetValues exchange are always blocking.
  • makeDistributedPlan now copies the cost estimate of every original node onto the node it rebuilds, so the producer's estimate reaches the exchange.

Tests

Unit tests for the pure algorithms on hand-built graphs, and for buildFragmentGraph on hand-built cut plans (blocking consumers behind Union, hash and in-order aggregation).

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

Version info

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

@clickhouse-gh

clickhouse-gh Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6dfdaae]

Summary: ✅


AI Review

Summary

This PR introduces the internal FragmentGraph model, graph validation, topological levels, debug rendering, and metadata carried by the distributed-plan cutter for a future fragment-aware EXPLAIN. Earlier correctness findings are addressed or fall outside the deliberately narrowed scope described in the RFC. No new correctness defect warrants an inline comment; estimate propagation still lacks focused regression evidence.

Missing context / blind spots
  • ⚠️ This checkout has no configured C++ build, and the fetched CI report contains no results for the added unit-test suites. Successful execution of FragmentGraph.* and RuntimeFilterExchangeWiring.FragmentGraphLeavesOutMergeStages would close this execution gap.
Tests
  • ⚠️ src/Processors/QueryPlan/Optimizations/makeDistributed.cpp:1764 now preserves cost_estimation, but no test proves that an estimate survives the real cutter and reaches FragmentExchange::statistics. BuildFromDistributedPlan constructs its stages and exchange description manually, while Dump sets estimated_rows directly. Both still pass if the estimate-copying change is removed. Add one focused regression that constructs a logical exchange over a producer with a known estimate, calls makeDistributedPlan followed by buildFragmentGraph, and asserts the resulting exchange's estimated_rows. This is the remaining coverage gap from the earlier estimate-propagation finding; the current copy itself addresses the original implementation defect.
Final Verdict

Status: ⚠️ Request changes. Add the focused cutter-to-graph estimate regression and obtain passing results for the affected unit tests.

@clickhouse-gh clickhouse-gh Bot added pr-not-for-changelog This PR should not be mentioned in the changelog comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... labels Oct 8, 2026
@diegomestre2
diegomestre2 marked this pull request as ready for review October 12, 2026 14:42
@diegomestre2 diegomestre2 added the can be tested Allows running workflows for external contributors label Oct 12, 2026
Comment thread src/Processors/QueryPlan/QueryPlan.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Commit 6dfdaae32cfcacb9b4237c90e45fc65cfe6a60bf was not compared: the CI logs cluster did not answer (exception 241, code 500: { "meta": [ { "name": "file", "type": "LowCardinality(String)" …).

See the job log for details.

Comment thread src/Processors/QueryPlan/WindowStep.cpp Resolved
@antoniofilipovic antoniofilipovic self-assigned this Oct 15, 2026
@Manerone Manerone self-assigned this Oct 15, 2026

@antoniofilipovic antoniofilipovic left a comment

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.

LGTM, thanks! As I understand this is a machinery for later steps to create fragment graph from real distributed plan, right?

@diegomestre2
diegomestre2 added this pull request to the merge queue Oct 20, 2026
Merged via the queue into ClickHouse:master with commit 2ad32ae Oct 20, 2026
525 of 528 checks passed
@diegomestre2
diegomestre2 deleted the explain_distributed branch October 20, 2026 09:28
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 20, 2026
@clickhouse-gh clickhouse-gh Bot added comp-distributed-query-exchange Distributed Query Execution with Exchange operators: cloud-native query distribution using stream... 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-distributed-query-exchange Distributed Query Execution with Exchange operators: cloud-native query distribution using stream... 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