Skip to content

Add a per-step serialization version to query plan steps - #119535

Merged
davenger merged 6 commits into
masterfrom
query_plan_per_step_version
Sep 16, 2026
Merged

davenger merged 6 commits into
masterfrom
query_plan_per_step_version

Conversation

@davenger

@davenger davenger commented Sep 11, 2026 •

Copy link
Copy Markdown
Member

Query plan serialization is used to send a plan to other servers for distributed execution and parallel replicas. Today every change to how a step is serialized bumps the single global DBMS_QUERY_PLAN_SERIALIZATION_VERSION. This ties all steps to one counter. It also gives no protection when a step's bytes change without a bump: a reader built from different code misparses them instead of failing with an error.

This change gives each step its own serialization version.

  • A step bumps its version when the bytes its serialize and deserialize handle change.
  • The version is written to the stream next to the step name. A reader that does not know the version throws INCORRECT_DATA instead of misparsing.
  • The registry keeps, per step, a list of supported versions with the global version when each was introduced. The writer picks the newest version the peer's global version allows; the reader accepts only a listed version.

Versioning rules:

  • The global version is bumped once per release, even if many steps changed their serialization in that release. On master a step may change several times between releases; each change bumps only the step's version.
  • When a release branch is cut, each step keeps only the versions needed for compatibility with the few latest supported releases.
  • Within a release, step versions do not change, so two servers with the same global version agree on every step. The exception is a critical bugfix that has to change a step's bytes: it bumps the step version without a global bump. During its rollout a server without the fix rejects the new version, and the affected queries fail with an error until the rollout completes. That is accepted: a fix worth backporting matters more than mixed-version compatibility while it rolls out. The step version only makes sure the failure is an error, not misparsed data.
  • The writer picks the step version from the global version chosen for the stream: negotiated with the peer for the native QueryPlan packet, or the server's own version for the stateless-worker task path, which does not negotiate (as before).
  • The step version covers only the step's own payload. Step settings (serializeSettings) are still versioned by the global version, so a change to them still needs a global bump.

Why the list lives in the registry: the global version is the only number peers negotiate, so something must map it to a step version. Keeping that map as one list per step means pruning at release time is an edit in one place, a version the step never implemented is rejected instead of accepted by a range, and the selection logic has one unit test. An entry changes only when the step's own bytes change, never when the global version moves for another reason.

Compatibility: the step version is written only from plan serialization version 19 on; below it the format is unchanged, and mixed-version clusters keep working through the existing negotiation. A step that never changed its bytes stays at version 0 and declares nothing.

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Changelog entry:

Not for changelog. Internal change to query plan serialization: each step now has its own serialization version. A reader rejects an unknown step version instead of misparsing it, and the global plan version is bumped once per release instead of on every step change.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.9.1.1569 (included in 26.9 and later)

davenger and others added 2 commits September 11, 2026 19:05
Each step owns a version that travels on the wire and is bumped on any change
to the step's bytes. A reader refuses a step version it does not know rather
than misparsing it, so the global plan version only has to move once per
release. The registry picks the version to write for the stable global version.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added the comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... label Sep 11, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [30f31ec]

Summary: ❌

job_name test_name status info comment
Build profile diff ERROR

AI Review

Summary

This PR adds per-step query-plan serialization versions, threads them through QueryPlan::serialize / QueryPlan::deserialize, updates the Auto-PR hash-key path to hash the current step encoding, and initializes the registry in clickhouse-local. The runtime changes look coherent, but the protocol-spec update required for a wire-format change is still missing.

Findings

⚠️ Majors

  • [src/Core/ProtocolDefines.h:123] Bumping DBMS_QUERY_PLAN_SERIALIZATION_VERSION to 19 changes the on-wire QueryPlan payload by inserting a per-step step_version, but docs/reference/interfaces/specs/NativeProtocol.mdx still documents the old layout. The Native Protocol spec is the published source of truth for TCP message bodies; leaving it stale makes the new packet shape discoverable only from code and invites incompatible reimplementations.
    Suggested fix: update the QUERY_PLAN_SERIALIZATION feature row and the QueryPlan message section to describe the per-step step_version field and its serialization_version >= 19 gate.
Final Verdict

Changes requested.

LLVM Coverage Report

Measured on commit 30f31ec.

Metric Baseline Current Δ
Lines 88.40% 88.40% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 80.50% 80.50% +0.00%

Changed lines: Changed C/C++ lines covered: 157/163 (96.32%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Sep 11, 2026
Comment thread src/Core/ProtocolDefines.h Outdated
static constexpr auto DBMS_QUERY_PLAN_SERIALIZATION_VERSION = 16;
/// Version 17 writes a per-step serialization version next to every step, so a step can change its
/// own bytes without moving this global version (see the constant below).
static constexpr auto DBMS_QUERY_PLAN_SERIALIZATION_VERSION = 17;

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.

Version 17 changes the wire layout of the QueryPlan packet's plan payload, but docs/reference/interfaces/specs/NativeProtocol.mdx still describes QueryPlan as just {serialization_version, max_threads, concurrency_control, plan} and never mentions the per-step step_version field or its >= 17 gate.

The review rules require protocol/spec sync for native TCP changes. Please update the QueryPlan section in docs/reference/interfaces/specs/NativeProtocol.mdx in the same PR so the inter-server contract stays documented.

@clickhouse-gh

clickhouse-gh Bot commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing commit 30f31ec4acdee88b84e271a00d25a1ab294f86f3 with master failed: RuntimeError: CI logs cluster query failed: SELECT count() AS c FROM binary_sizes W….

See the job log for details.

@KochetovNicolai KochetovNicolai self-assigned this Sep 14, 2026
Comment on lines +13 to +48
/// The serialization versions of one step.
///
/// A step owns its version. The author bumps it when the bytes the step's own `serialize` and
/// `deserialize` write and read change. The version travels on the wire, so a reader refuses a
/// version it does not know instead of misparsing the bytes.
///
/// The contract this relies on:
/// - Several steps may change in one release cycle, each bumping its own version. One bump of
/// the global plan version per release covers all of them.
/// - On master a step may change several times between releases; each change bumps its version.
/// When a release branch is cut, each step keeps the previous release's version plus the one
/// new version, and the global plan version moves once.
/// - Within a released line step versions are frozen, so two servers that advertise the same
/// global version agree on every step. Only critical bugfixes are backported. When one has to
/// change a step's bytes, it bumps the step version without a global bump; during that fix's
/// rolling upgrade a peer that does not know the new version refuses it, and the affected
/// queries fail with an error until the rollout completes. That is accepted: a fix critical
/// enough to backport matters more than mixed-version compatibility while it rolls out. The
/// on-wire version only makes sure the failure is a clean error, not misparsed bytes.
/// - `write` is keyed by the global plan version: the writer serializes at the version the peers
/// agreed on (the lowest both support, or one pinned in config) and picks the step version
/// stored for it, so an older-release peer gets the encoding it knows.
/// - This covers the step's own payload only. The step settings channel (`serializeSettings` and
/// the changed-settings blob) is versioned by the global plan version, so a change to a step's
/// settings schema also needs a global plan-version bump.
///
/// `readable` lists every step version this binary can deserialize. `write` maps a global plan
/// version to the step version to write at it: the writer takes the entry with the highest plan
/// version not above the one it serializes with. A step that has never changed its bytes stays at
/// version 0 and needs none of this.
struct Versions
{
std::vector<UInt64> readable = {0};
/// (global plan version, step version), sorted by the plan version ascending.
std::vector<std::pair<UInt64, UInt64>> write = {{0, 0}};
};

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.

This looks somewhat too complicated to me.

  1. The comment above is not human-readable.
  2. Idk why we need to split read and write. Usually, code is symmetric. Technically, we can support a wider range of reading versions. However, I can hardly imagine the usecase when sending plan old -> new is allowed, but new -> old is not.
  3. Do we need a vector of versions, not just min and max? Do we want to have gups?
  4. Idk if connecting step version to plan version make sense. There are cases when they are orthogonal (e.g. we change a format of plan header serialization, or a packet structure). It could be that we want to change plan version and step version at the same time (e.g. if we switch to protobuf, want json, or something) - but this looks more like a format setting. Anyway, I don't want to have a new copy of the versions vector every time the global plan version changes (which has increased 10 times for the last month!)

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.

Thanks — you're right on 0–2; reworked in afcadea.

0. Simplified the comment.

1. There is no read/write split any more. Each step has one list of {version, since_plan_version}: its supported serialization versions and the global version when each was introduced. The reader accepts only a listed version. The writer picks the newest version the peer's global version allows. So both directions work.

2. It is an explicit list, not [min, max], on purpose. It stays short: only the versions needed for the few latest supported releases are kept. And a range does not always fit: after a backport a release can support versions 1 and 3 but not 2, and a range would accept 2.

3. The dependency on the global version cannot be avoided: it is the only number peers negotiate, so something has to map it to a step version to send an older peer the bytes it knows. Today that mapping is the ctx.version >= WITH_X checks inside each step. In the registry it is in one place, visible and easy to prune, instead of spread over every step. An entry changes only when the step's own bytes change, never when the global version moves for another reason. The eleven global-version bumps of the last month would have touched zero entries; UnrelatedPlanVersionBumpsDoNotTouchTheStep checks this.

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

Discussed offline, LGTM.
Let's merge this version.

@davenger
davenger added this pull request to the merge queue Sep 16, 2026
Merged via the queue into master with commit eb77c64 Sep 16, 2026
174 of 176 checks passed
@davenger
davenger deleted the query_plan_per_step_version branch September 16, 2026 03:24
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 16, 2026
thevar1able added a commit that referenced this pull request Oct 3, 2026
Master moved to per-step serialization versions
(#119535) and documented that a
change to a step's bytes must bump the step's own version rather than the
global `DBMS_QUERY_PLAN_SERIALIZATION_VERSION`
(#123241). Flag bit 128 of
`AggregatingStep` is now taken by `only_merge`.

Resolve accordingly: drop the global version bump and the bit-128 top-K
flag, and carry the top-K parameters as a trailing presence byte plus
payload in version 1 of the `Aggregating` step, introduced at plan version
20 (the 26.10 value). Towards a peer that reads step version 0 the
parameters are omitted, as before.

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-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... 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.

3 participants