Skip to content

Fix CREATE ... ON CLUSTER ... AS SELECT over a Distributed table - #109407

Merged
alexey-milovidov merged 10 commits into
masterfrom
fix-90651-ddl-distributed-client-version
Jul 16, 2026
Merged

alexey-milovidov merged 10 commits into
masterfrom
fix-90651-ddl-distributed-client-version

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jul 4, 2026 •

Copy link
Copy Markdown
Member

CREATE TABLE ... ON CLUSTER ... AS SELECT ... FROM <Distributed> ORDER BY ... could fail on a cluster with two or more shards with NOT_FOUND_COLUMN_IN_BLOCK (Not found column __table1.<col> in block. There are only columns: <col>: While executing Remote.), or with std::bad_function_call (STD_EXCEPTION) in 25.8.

The ON CLUSTER DDL query is executed by the DDL worker, whose query context did not carry a client version — it stayed 0.0.0. When that context reads the Distributed table and forwards the sub-query to a remote shard, the shard treats the initiator as a pre-23.3 server and disables the analyzer for compatibility (see TCPHandler), while the initiator keeps using the analyzer. The two sides then name columns differently (column identifier __table1.<col> vs result name <col>), so distributed execution fails. Non-cluster distributed reads are not affected because they propagate a real client version. The problem could look intermittent because the compatibility downgrade only fired when allow_experimental_analyzer was not explicitly marked as changed.

The fix fills the client version of the DDL query context with this server's version, so distributed sub-queries spawned during DDL execution report a modern initiator and remote shards do not apply the legacy compatibility downgrade.

Closes: #90651

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fixed an error (NOT_FOUND_COLUMN_IN_BLOCK, or std::bad_function_call in older versions) when running CREATE TABLE ... ON CLUSTER ... AS SELECT reading a Distributed table on a cluster with two or more shards.

Documentation entry for user-facing changes

  • Documentation is not required for this change

Version info

  • Merged into: 26.7.1.1050 (included in 26.7 and later)

`CREATE TABLE ... ON CLUSTER ... AS SELECT ... FROM <Distributed> ORDER BY ...`
could fail on a cluster with two or more shards with `NOT_FOUND_COLUMN_IN_BLOCK`
(`Not found column __table1.<col> in block. There are only columns: <col>`), or
with `std::bad_function_call` in 25.8.

The `ON CLUSTER` DDL query is executed by the DDL worker, whose query context did
not carry a client version - it stayed 0.0.0. When that context reads the
`Distributed` table and forwards the sub-query to a remote shard, the shard treats
the initiator as a pre-23.3 server and disables the analyzer for compatibility
(see `TCPHandler`), while the initiator keeps using the analyzer. The two sides
then name columns differently (identifier `__table1.<col>` vs result name
`<col>`), so distributed execution fails. Non-cluster distributed reads are not
affected because they propagate a real client version.

Fill the client version of the DDL query context with this server's version, so
distributed sub-queries spawned during DDL execution report a modern initiator
and remote shards do not apply the legacy compatibility downgrade.

Closes: #90651

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [4f7826f]

Summary: ✅


AI Review

Summary

This PR stamps the DDL worker query context with this server's own client version so remote shards no longer misclassify CREATE TABLE ... ON CLUSTER ... AS SELECT over Distributed as coming from a pre-23.3 initiator and disable the analyzer on only one side. I did not find any remaining correctness issues in the current code or the added regression coverage.

Missing context / blind spots
  • ⚠️ GitHub CI was still in progress for commit 4f7826f1b40f13b7be48beb5df3646b7e2e77c7d while I reviewed it. Style check, Fast test, and Build (arm_tidy) were green, but the full build/test matrix had not finished yet.
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 4, 2026
Comment thread src/Interpreters/DDLTask.cpp Resolved
alexey-milovidov added a commit that referenced this pull request Jul 4, 2026
The zero-version guard added to `RemoteQueryExecutor` also surfaced two
internal query-context constructors that synthesize an `INITIAL_QUERY`
context via `Context::createCopy` and never fill `client_version_*`, so
they forwarded a zero version to remote shards:

- `AsynchronousInsertQueue::processBatchDeadlines` - the async insert flush
  context. Reached when the flush spawns a distributed read, e.g. a
  materialized view triggered by the insert reads from a `Distributed`
  table.
- `StorageMaterializedView::createRefreshContext` - the refreshable
  materialized view refresh context. Reached when the refresh `SELECT`
  reads from a `Distributed` table.

Both are `setQueryKind(INITIAL_QUERY)` (not `setInitialQuery`), which does
not fill the version. This server is the real initiator of these queries
and of any distributed sub-query they spawn, so fill the version with this
server's own version when it is zero - the same fix already applied to
`Session::makeQueryContext`. Otherwise remote shards treat the initiator as
a pre-23.3 server and apply legacy compatibility downgrades (see
`TCPHandler`), and `RemoteQueryExecutor` now rejects the zero version
outright (a logical error that aborts a debug server from the background
refresh/flush threads).

Adds a regression test covering both paths (verified locally: the guard
fires twice without this change and not at all with it).

Related: #109407
Related: #90651

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
alexey-milovidov and others added 2 commits July 11, 2026 23:50
…ery_log

The end-to-end test only asserted that CREATE TABLE ... ON CLUSTER ... AS
SELECT ... FROM a Distributed table succeeds. Whether an unfixed server
actually misbehaves depends on whether allow_experimental_analyzer happens
to be marked as explicitly changed on the wire; in the CI harness it always
is, so the bug did not reproduce on the master binary and the Bugfix
validation job reported "Bug does not reproduce on this arch", failing
new_tests_check:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=109407&sha=1dc2ba99ad23e6142be0dfa167045fbe3af94d32&name_0=PR&name_1=Finish%20Workflow

Add test_ddl_worker_context_carries_client_version: the query executed by
the DDL worker (identified by the "/* ddl_entry=... */" prefix it prepends)
is recorded in system.query_log with the client version of its query
context - client_version_major is 0 without the fix and the server's own
version with it, independently of whether the analyzer downgrade fires.
Verified against real binaries in the integration harness: the new test
fails on the current master binary (26.7.1.802) with client_version_major
= 0 and passes with this PR's binary, while the four end-to-end variants
pass on both. Also add a GROUP BY variant to the parametrized test.

PR: #109407

Co-Authored-By: Claude Fable 5 <[email protected]>
alexey-milovidov and others added 2 commits July 12, 2026 02:29
…r test

`test_ddl_worker_context_carries_client_version` asserted only
`client_version_major > 0`. The compatibility branch the fix avoids is
keyed off `client_version < 23.3.0` in `TCPHandler`, so a broken
implementation stamping e.g. `1.0.0` or `22.8.0` would pass a
major-greater-than-zero check yet still leave the pre-23.3 analyzer
downgrade reachable.

Assert the full logged `(client_version_major, client_version_minor,
client_version_patch)` tuple against the server's own `version`. The fix
stamps exactly `VERSION_MAJOR`, `VERSION_MINOR`, `VERSION_PATCH`, whose
first three components are what `version` reports, so on a fixed server
the logged tuple equals the server version (far above `23.3.0`) and on an
unfixed server it is `0.0.0` and the assertion fails - the bug still
reproduces on master, keeping the Bugfix validation deterministic.

Addresses the AI review on 75ef0a7:
#109407

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

The only red check is BuzzHouse (amd_debug), which hit a pre-existing formatter round-trip bug — Logical error: 'Inconsistent AST formatting between 'ExpressionList' and 'ExpressionList'' — on a random fuzzer query (ifNull(sum((['cut'] AS a1) IN [...] AS c3 = TRUE), 0)). This is unrelated to this PR (which only sets the client version in DDLTask::makeQueryContext): over the last 30 days the same failure has appeared on ~10 unrelated PRs and does not reproduce from anything this change touches.

I reduced it to a minimal, deterministic reproducer and filed it as #110223. The previous flaky red (test_replicated_table_structure_alter, #110036) is now green after merging master.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

The reds on 47a7f32dffb were both unrelated to this change:

  1. All six failed stateless jobs were the same test, 02346_text_index_bug108519_qcc_skip_index, failing only under the randomized setting materialize_statistics_on_insert = 1 (the CI minimizer confirmed it passes without randomization). The fix landed on master today in 389a832 ("Fix flaky 02346_text_index_bug108519_qcc_skip_index under randomized statistics") — after the previous merge of master into this branch — so I merged master again to pull it in.

  2. AST fuzzer (amd_debug): Logical error: Too large size (A) passed to allocator (STID: 1367-1ab3) is the pre-existing debug-build bug in MergeTreeDataPartWriterWide::validateColumnOfFixedSize with a huge index_granularity, hit during an INSERT pipeline. A fix is already in progress: Fix allocator abort in validateColumnOfFixedSize for huge index_granularity #108650.

The diff versus master is still exactly the intended change: DDLTask.cpp (+12) plus the integration test.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

The CH Inc sync failure on 2ac7e2e was unrelated to this PR: all 54 failed tests were the private-only test_storage_iceberg_with_spark/test_compaction.py (Spark failing to read back compaction-written Parquet with Unsupported type: UTF8String), broken by #106952, which has since been reverted on master by #110354 (merged after this branch's previous master merge). Merged master again to pull in the revert; the next sync run should be clean. Public CI on 2ac7e2e was fully green.

@evillique evillique self-assigned this Jul 15, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Merged master (this branch was 335 commits behind) to keep it current; rebuilt locally and confirmed it still compiles cleanly.

The sole CI red on the previous head (7e6a8e8fdc8) was AST fuzzer (amd_debug) — Segmentation fault (STID: 3294-4af5), crashing in DirectKeyValueJoin::joinBlock → DirectJoinMergeTreeEntity::getByKeys → ActionsDAG::removeUnusedActions (query used join_algorithm = 'direct'). This is unrelated to this PR's change (DDLTask.cpp client-version stamping). It matches the already-diagnosed bug in #110241 (concurrent per-lookup ReadFromMergeTree::clone() in DirectJoinMergeTreeEntity::getByKeys shares prewhere_info/row_level_filter via shared_ptr, and column pruning corrupts it across threads), whose fix is in progress at #109932 (not yet merged). Since the crash is concurrency-dependent, a fresh CI run on the merged head is expected to not reproduce it.

@clickhouse-gh

clickhouse-gh Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.80% 85.90% +0.10%
Functions 92.70% 92.70% +0.00%
Branches 78.00% 78.00% +0.00%

Changed lines: Changed C/C++ lines covered: 5/5 (100.00%) · Uncovered code

Full report · Diff report

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jul 16, 2026
Merged via the queue into master with commit d243605 Jul 16, 2026
179 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-90651-ddl-distributed-client-version branch July 16, 2026 18:14
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 16, 2026
josemimbre pushed a commit to josemimbre/ClickHouse that referenced this pull request Aug 4, 2026
A distributed query forwarded to a remote shard must carry a known
initiator version: the receiving server uses it for version-gated
compatibility decisions (e.g. whether to enable the analyzer, see
`TCPHandler`). A zero version means the initiating query context was not
populated as an initial query - a real client always reports its version,
and a server that (re-)initiates a query fills it with its own version.
Sending a zero version silently triggers wrong compatibility downgrades on
the remote and can break distributed execution (see the issue below). Fail
with a logical error instead of sending a zero version, so such context
initialization bugs are caught loudly.

Related: ClickHouse#109407
Related: ClickHouse#90651

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…ery_log

The end-to-end test only asserted that CREATE TABLE ... ON CLUSTER ... AS
SELECT ... FROM a Distributed table succeeds. Whether an unfixed server
actually misbehaves depends on whether allow_experimental_analyzer happens
to be marked as explicitly changed on the wire; in the CI harness it always
is, so the bug did not reproduce on the master binary and the Bugfix
validation job reported "Bug does not reproduce on this arch", failing
new_tests_check:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=109407&sha=1dc2ba99ad23e6142be0dfa167045fbe3af94d32&name_0=PR&name_1=Finish%20Workflow

Add test_ddl_worker_context_carries_client_version: the query executed by
the DDL worker (identified by the "/* ddl_entry=... */" prefix it prepends)
is recorded in system.query_log with the client version of its query
context - client_version_major is 0 without the fix and the server's own
version with it, independently of whether the analyzer downgrade fires.
Verified against real binaries in the integration harness: the new test
fails on the current master binary (26.7.1.802) with client_version_major
= 0 and passes with this PR's binary, while the four end-to-end variants
pass on both. Also add a GROUP BY variant to the parametrized test.

PR: ClickHouse#109407

Co-Authored-By: Claude Fable 5 <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…r test

`test_ddl_worker_context_carries_client_version` asserted only
`client_version_major > 0`. The compatibility branch the fix avoids is
keyed off `client_version < 23.3.0` in `TCPHandler`, so a broken
implementation stamping e.g. `1.0.0` or `22.8.0` would pass a
major-greater-than-zero check yet still leave the pre-23.3 analyzer
downgrade reachable.

Assert the full logged `(client_version_major, client_version_minor,
client_version_patch)` tuple against the server's own `version`. The fix
stamps exactly `VERSION_MAJOR`, `VERSION_MINOR`, `VERSION_PATCH`, whose
first three components are what `version` reports, so on a fixed server
the logged tuple equals the server version (far above `23.3.0`) and on an
unfixed server it is `0.0.0` and the assertion fails - the bug still
reproduces on master, keeping the Bugfix validation deterministic.

Addresses the AI review on 75ef0a7:
ClickHouse#109407

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…stributed-client-version

Fix CREATE ... ON CLUSTER ... AS SELECT over a Distributed table
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default 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.

Query Failures on a Sharded ClickHouse Cluster

3 participants