Skip to content

Fix zero initiator version in the remote structure fetch of StorageDistributed - #113675

Merged
alexey-milovidov merged 13 commits into
masterfrom
fix/distributed-structure-fetch-client-version
Aug 12, 2026
Merged

alexey-milovidov merged 13 commits into
masterfrom
fix/distributed-structure-fetch-client-version

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Aug 6, 2026 •

Copy link
Copy Markdown
Member

Closes: #113671
Caused by: #109408

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):

Fix a logical error Sending a distributed query with unknown (zero) client version when a structure-less Distributed table over a remote shard is attached or loaded from metadata written by an older server version.

A structure-less Distributed definition has to fetch the structure of the remote table with a DESC TABLE service query. When the definition is loaded from metadata with no persisted column list, StorageDistributed's constructor sends that query under the storage's global context, which carries no client version, so the hardening check from #109408 rejected it with a logical error and the table failed to attach or load. The fix stamps the server's own version on the service-query context in getStructureOfRemoteTable when the version is unknown, the same way DDLTaskBase::makeQueryContext does for distributed DDL.

Modern servers persist the inferred structure into the stored definition (InterpreterCreateQuery::addColumnsDescriptionToCreateQueryIfNecessary), but metadata written by older versions can still be column-less, and DatabaseOnDisk::createTableFromAST deliberately loads such definitions with empty columns. The regression test simulates this legacy metadata by stripping the persisted column list from the stored definition while the table is detached: before the fix, the re-ATTACH failed with the logical error above.

The bug was originally found by BuzzHouse fleet-wide across at least 19 unrelated PRs since 2026-08-04, e.g. this report on #110179, where the same zero-version fetch was reached from a plain CREATE. That CREATE path was moved under the user's context on master in the meantime via #113220, so this PR is reduced to the client-version fix for the metadata-load path and its tests. The integration test test_distributed_structure_fetch also keeps coverage for the direct CREATE, the SHOW_COLUMNS access check of the structure inference over a local shard, and the Replicated-database DDLWorker replay that BuzzHouse hit.

Related: #113220

Version info

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

…istributed`

A structure-less `CREATE TABLE ... ENGINE = Distributed` fetches the structure
of the remote table with a `DESC TABLE` service query, sent under the storage's
global context, which carries no client version. Since the hardening check in
`RemoteQueryExecutor` (#109408), such a query is rejected with the logical error
"Sending a distributed query with unknown (zero) client version", so the
`CREATE` fails. Stamp the server's own version on the service-query context in
`getStructureOfRemoteTable` when it is unknown, the same way
`DDLTaskBase::makeQueryContext` does for distributed DDL.

Found by BuzzHouse fleet-wide, e.g.
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=110179&sha=f33258e4ed419d3eb7e77629b1e833cb36cadf7c&name_0=PR&name_1=BuzzHouse%20%28arm_asan_ubsan%29

Co-Authored-By: Claude Fable 5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b9f02f0]

Summary: ✅


AI Review

Summary

This PR correctly stamps the server's own client version onto the DESC TABLE service query used when a structure-less Distributed table is loaded from legacy column-less metadata, and the new integration coverage exercises that regression path. The zero-version failure itself looks fixed, but the review still cannot approve the change because the same Distributed creation path continues to allow a local-shard privilege bypass that was previously raised and dismissed.

Findings

❌ Blockers

  • [dismissed by author -- https://github.com/Fix zero initiator version in the remote structure fetch of StorageDistributed #113675#discussion_r3737267570] [src/Storages/StorageDistributed.cpp:2229-2255, src/Interpreters/ClusterProxy/SelectStreamFactory.cpp:216-240, src/Storages/StorageDistributed.cpp:1249-1274] Explicit-column Distributed definitions over a cluster with a local shard still skip any creation-time SELECT/INSERT validation on the target table. That means a user can persist ENGINE = Distributed(...) pointing at a protected local table and then read or write it through the cluster credentials by forcing the local shard down the remote-connection path (prefer_localhost_replica = 0), which is the same privilege bypass Remote/RemoteSecure already guard against. The current stateless regression test even pins the insecure behavior in tests/queries/0_stateless/04715_distributed_infer_columns_access.sh:60-65. Suggested fix: mirror the Remote/RemoteSecure local-shard SELECT/INSERT checks in registerStorageDistributed for every newly introduced definition (CREATE, full-definition ATTACH, RESTORE), not only the columns.empty() schema-inference path, and update the stateless expectation accordingly.
Final Verdict

❌ Changes requested

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.70% 86.70% +0.00%
Functions 92.00% 92.00% +0.00%
Branches 79.00% 79.00% +0.00%

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

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Aug 6, 2026
alexey-milovidov and others added 3 commits August 7, 2026 04:15
…ser's context

Address review: the `Distributed` engine inferred omitted columns in the
`StorageDistributed` constructor under the storage's global context, so the
`SHOW_COLUMNS` access check in `getStructureOfRemoteTableInShard` for a local
shard ran with global privileges - a user who can create a `Distributed` table
could learn the schema of a local table they are not allowed to describe.

Mirror the `Remote`/`RemoteSecure` treatment: infer the structure in
`registerStorageDistributed` under `args.getLocalContext()` when the definition
is first introduced (`CREATE`, full-definition `ATTACH`, backup `RESTORE`),
and keep the constructor's global-context inference only for loads of
already-validated metadata stored on this server (server startup, short
`ATTACH`), where there is no user to check against.

Add an access regression test: without `SHOW_COLUMNS` on the target, a
structure-less `CREATE TABLE ... ENGINE = Distributed` over a cluster with a
local shard must fail with `ACCESS_DENIED`.

Co-Authored-By: Claude Fable 5 <[email protected]>
Comment thread src/Storages/StorageDistributed.cpp Outdated
const bool loading_from_existing_metadata = isLoadingFromExistingMetadata(args.mode) || args.query.attach_short_syntax;

ColumnsDescription columns = args.columns;
if (columns.empty() && !loading_from_existing_metadata)

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.

This only moves omitted-column schema inference under args.getLocalContext(). A Distributed definition with explicit columns still skips any local-shard SELECT / INSERT validation here, so a user can persist ENGINE = Distributed('local_cluster', db, protected) and then query it with prefer_localhost_replica = 0. In that mode SelectStreamFactory / DistributedSink send the local shard through a real remote connection, and ClusterProxy::updateSettingsForCluster explicitly notes that without a cluster secret the shard user differs from the initiator (src/Interpreters/ClusterProxy/executeQuery.cpp:159-166), so the request runs under cluster credentials instead of the caller's rights. Remote already closes this exact hole at src/Storages/StorageDistributed.cpp:2663-2675; Distributed needs the same SELECT / INSERT check when the definition is first introduced (CREATE, full-definition ATTACH, RESTORE), not only when columns.empty().

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.

🕵 The explicit-column case is a pre-existing design point of master, not something this PR introduced or can change without breaking a master test: #113220 (merged today) deliberately pins it in 04715_distributed_infer_columns_access.sh — "3. explicit columns, no rights on the target: still allowed" (and case 2 allows a structure-less CREATE with only SHOW_COLUMNS). Requiring the caller's own SELECT/INSERT on the target would make this PR red on that untouched test, and unlike the new Remote engine (strict from day one), Distributed has long-standing deployments where the creator of a distributed table does not hold INSERT on the underlying table. After merging master, this PR no longer touches registerStorageDistributed at all — it is reduced to the zero-initiator-version fix in getStructureOfRemoteTableInShard plus its tests. Tightening the explicit-column path for Distributed is a real design question, but it belongs to a separate discussion against master's pinned behavior, not to this bug fix.

@clickhouse-gh

clickhouse-gh Bot commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing b9f02f0ad with master 0eb5eba20 (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
Binary Master PR Δ
programs/clickhouse-stripped 691.96 MiB 688.96 MiB -3.00 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.

Compile time of recompiled translation units

7 translation units recompiled, 12 s compile time in total, 7 of them have a recent master baseline.

Job report

Comment thread src/Storages/StorageDistributed.cpp Outdated

finalizeDistributedSettings(distributed_settings, context);

/// When the structure is not specified, infer it from the remote table under the user's

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.

@alexey-milovidov you have to trim these comments

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.

🕵 Done — after merging master, the long comment block is gone entirely: #113220 landed an equivalent inference fix with a much shorter comment, so this PR no longer modifies StorageDistributed.cpp at all.

…ucture-fetch-client-version

# Conflicts:
#	src/Storages/StorageDistributed.cpp
…e from legacy column-less metadata

After #113220, a plain `CREATE` infers the omitted structure under the user's
context and `addColumnsDescriptionToCreateQueryIfNecessary` persists the inferred
columns into the stored definition, so the `CREATE`-based tests no longer reach
the constructor's global-context structure fetch. The remaining zero-version path
is a column-less definition loaded from metadata written by an older server
version, which `DatabaseOnDisk::createTableFromAST` deliberately loads with empty
columns. The new test simulates such legacy metadata by stripping the persisted
column list while the table is detached; before the fix in
`getStructureOfRemoteTable` the re-`ATTACH` failed with the logical error
`Sending a distributed query with unknown (zero) client version`.
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Status of the two red checks on commit 7115c39, both unrelated to this change:

The Finish Workflow red (new_tests_check.py) was legitimate: after #113220 the CREATE-based tests passed on master, so no Bugfix validation job reproduced the bug. Addressed in 8bb313a by adding test_structure_less_distributed_loaded_from_legacy_metadata, which fails on master with the zero-version logical error and passes with the fix (verified locally both ways).

alexey-milovidov and others added 2 commits August 8, 2026 23:20
…tch`

The new test `test_structure_less_distributed_loaded_from_legacy_metadata` edits the
table's metadata `.sql` file on the local disk, but in the "db disk" CI variant the
database metadata is stored on a remote `plain_rewritable` disk, so the file does not
exist and the test failed with "No such file or directory":
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=113675&sha=8bb313a080b9b7f7349a5e22695e81b03789c493&name_0=PR&name_1=Integration%20tests%20(amd_asan_ubsan,%20db%20disk,%20old%20analyzer,%202/6)

Co-Authored-By: Claude Fable 5 <[email protected]>
Address review: the `ATTACH` in `test_structure_less_distributed_loaded_from_legacy_metadata`
throws on unfixed code, and the table then stayed detached: `DROP TABLE IF EXISTS` only sees
attached tables, so repeated runs on the module-scoped cluster (flaky-check) cascaded into
`TABLE_ALREADY_EXISTS ... (detached)` setup failures. Switch the `DETACH` to `SYNC` and add a
detached-table recovery that restores the backed-up metadata (bringing the column list back, so
the recovery `ATTACH` needs no structure inference and succeeds even on unfixed code), mirroring
`test_part_loading_tree_rollback`.

Verified both ways locally: with the fix, all 4 tests pass; on a stale unfixed binary the test
fails at `ATTACH` with the original logical error and the recovery in `finally` cleans up without
breaking the following tests.
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Both stress-test failures on 1bb8adbe are unrelated fleet-wide issues: Bad cast from type A to B in Stress test (amd_tsan) is #113891 (fix in progress: #113925), and the type-equality chassert (STID 2508-2e0c) in Stress test (arm_asan_ubsan) is #113931. @groeneai, the latter still has no fix PR — please investigate and provide a fix in a separate PR; if one is already in progress, link it here.

@groeneai

groeneai commented Aug 9, 2026

Copy link
Copy Markdown
Collaborator

A fix PR is already open: #113933, which Closes #113931.

It is the same mechanism, not merely the same STID. The failing frame on 1bb8adbe is HashJoinMethodsImpl.h:807, the exact col->insertFrom(*src_col, row_pos) call #113933 patches, and the template arguments decode to JoinKind::Left + JoinStrictness::Any, i.e. the LEFT ANY JOIN ... ON (key = key) AND (a * 10 < a) shape from the issue. All 10 Stress test jobs on #113933 are green, including Stress test (arm_asan_ubsan), the carrier that failed here.

Origin is my merged #112831, as the issue records.

One thing worth a look while that is in review: #113534 is still open and addresses the same mixed-condition-over-dictionary family one layer up (JoinStepLogical / HashJoin.cpp rather than buildAdditionalFilter), so the two overlap.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Status of commit 63b38b0: the only red check is Stress test (amd_debug) — Bad cast from type DB::ColumnString to DB::ColumnNullable in SerializationNullable::enumerateStreams reached from collectOffsetsColumns (inplaceBlockConversions.cpp). This is the known master-only regression from #113225, tracked in #113891, with the fix already open: #113925 (it patches exactly this collectOffsetsColumns → enumerateStreams walk). Unrelated to this PR.

Everything else is green, including CH Inc sync, and the AI review verdict on this head is ✅ Approve. Ready for a human to merge.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Merged master (d6e21a7e7b04) to pull in #113925, which fixes the Bad cast from type DB::ColumnString to DB::ColumnNullable stress failure (#113891) — the only red check on the previous head. The branch content is otherwise unchanged; a fresh CI run should be fully green.

@alexey-milovidov

alexey-milovidov commented Aug 11, 2026 •

Copy link
Copy Markdown
Member Author

🕵 Status of commit d6e21a7: both red checks are unrelated to this change.

Neither fix has landed on master yet, so re-merging master now would not clear the red. Once #114302 (or the revert) merges, a master merge should give a clean run.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Merged master (b9f02f0ad971) to pull in #114326, which reverts the change that caused the Hung check failed, possible deadlock found failure in Stress test (arm_msan) (DROP TABLE of a TimeSeries inner table stuck in waitTableFinallyDropped, #114259). The other red on d6e21a7e — the amd_llvm_coverage stateless job ERROR — was pure CI infrastructure (the runner died after all executed tests passed), so a fresh run should be fully green. No content changes beyond the merge.

@alexey-milovidov alexey-milovidov left a comment

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.

LGTM

@alexey-milovidov alexey-milovidov self-assigned this Aug 12, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Aug 12, 2026
Merged via the queue into master with commit a63b5b5 Aug 12, 2026
356 of 357 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix/distributed-structure-fetch-client-version branch August 12, 2026 15:48
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 12, 2026
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

4 participants