Skip to content

Fix unparseable metadata for a NATS table defined only by a named collection - #120025

Merged
evillique merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-nats-collection-only-metadata
Sep 15, 2026
Merged

evillique merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-nats-collection-only-metadata

Conversation

@groeneai

@groeneai groeneai commented Sep 14, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #119120

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 a NATS table whose settings all come from a named collection (ENGINE = NATS(collection) with no SETTINGS clause) being written to its metadata file with a dangling SETTINGS keyword. The stored definition could not be parsed again, so the table failed to load and aborted the load of its whole database, making unrelated tables in it inaccessible. Closes #119120

Description

registerStorageNATS called NATSSettings::loadFromQuery unconditionally. When the engine definition is only a named collection, args.storage_def->settings is null, and that function's else branch attaches an empty ASTSetQuery; ASTStorage::formatImpl then emits the keyword for any non-null settings member, so the definition serialized into the metadata file ends in a bare SETTINGS .

Reproduced on master:

CREATE NAMED COLLECTION c AS nats_url = '127.0.0.1:1', nats_subjects = 's',
    nats_format = 'JSONEachRow', nats_startup_connect_tries = 0, nats_reconnect_wait = 1;
ATTACH TABLE t UUID 'c6d2423a-9ab2-4a37-8e56-10e479542009' (key UInt64) ENGINE = NATS(c);
DETACH TABLE t;
ATTACH TABLE t;

The stored file ends ENGINE = NATS(c) / SETTINGS and the reload fails:

Code: 62. DB::Exception: Syntax error (in file .../t.sql): failed at position 117 (end of query)
... Cannot parse definition from metadata file .../t.sql. (SYNTAX_ERROR)

The fix guards the call, as the sibling registrars already do: StorageRabbitMQ.cpp has the same preceding else if (!args.storage_def->settings) throw plus the guard, and its RabbitMQSettings::loadFromQuery carries the identical empty-ASTSetQuery branch; StorageKafkaUtils.cpp and StorageDistributed.cpp likewise. That branch becomes unreachable; I left it so NATS and RabbitMQ stay the same shape.

Test: 05029_nats_tls_certificate_path_restriction and 04891_nats_credential_file_path_restriction both coded around this defect, carrying SETTINGS nats_num_consumers = 1 on a full-definition ATTACH only so the stored definition would parse on the DETACH/ATTACH replay that follows. Removing both crutches turns those blocks into the regression oracles: without this fix each short ATTACH fails SYNTAX_ERROR (62) where the test expects BAD_ARGUMENTS (36). The removed value equalled the setting's own default and reaches neither provenance path, so every assertion keeps its meaning.

Scope: this stops new corruption. A file an older server already wrote this way still needs the dangling keyword removed by hand, because the parse failure aborts the database load before the table can be dropped.


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

Version info

  • Merged into: 26.9.1.1525 (included in 26.9 and later)
  • Backported to: 26.8.7.9, 26.7.11.5, 26.6.9.5, 26.3.33.94

…lection

registerStorageNATS called NATSSettings::loadFromQuery unconditionally. With
ENGINE = NATS(collection) and no SETTINGS clause, args.storage_def->settings is
null, and loadFromQuery's else branch attaches an empty ASTSetQuery to the
ASTStorage. ASTStorage::formatImpl emits the keyword for any non-null settings
member, so the definition serialized into the table's metadata file ends in a
bare "SETTINGS ". Re-parsing that file fails with SYNTAX_ERROR, which makes the
table unloadable and aborts the whole database load, so unrelated healthy tables
in the same database become inaccessible too.

Guard the call, exactly as the sibling registrars already do: StorageRabbitMQ.cpp
has the same preceding "else if (!args.storage_def->settings) throw" together
with the guard, and its RabbitMQSettings::loadFromQuery carries the identical
empty-ASTSetQuery branch; StorageKafkaUtils.cpp and StorageDistributed.cpp guard
the same way. Nothing after the call needs settings to be non-null: the later
uses are already inside "if (args.storage_def->settings)". The empty-ASTSetQuery
branch of NATSSettings::loadFromQuery becomes unreachable and is left in place so
that NATS and RabbitMQ stay the same shape.

The shape became reachable when named collections were added in b3ca976;
the unguarded call dates from the original engine in 944a729.

05029_nats_tls_certificate_path_restriction already coded around this defect: its
full-definition ATTACH carried SETTINGS nats_num_consumers = 1 purely so that the
stored definition would parse on the DETACH/ATTACH replay that follows. Removing
that crutch turns the existing block into the regression oracle. The value equalled
the setting's own default and is read nowhere in the certificate provenance logic,
so the block's assertions keep their meaning. Without this fix the short ATTACH
fails with SYNTAX_ERROR (62) where the test expects BAD_ARGUMENTS (36).

Closes ClickHouse#119120
04891_nats_credential_file_path_restriction carried the same
SETTINGS nats_num_consumers = 1 crutch on the full-definition ATTACH whose
metadata the block replays, and a comment stating that a collection-only
definition is stored with an empty SETTINGS clause. The guard added in this
branch makes that comment false, so remove the crutch there too: the block then
covers the fixed shape on the credential-file path as well. The value equalled
the setting's own default and is read nowhere in the credential provenance
logic, so every assertion keeps its meaning. Without the guard the short ATTACH
of that block fails with SYNTAX_ERROR (62) where the test expects
BAD_ARGUMENTS (36).

Both replay blocks now state what the stored definition must carry instead of
describing what it happens to carry, so the property the block exists to check
does not read as an incidental remark.
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-request PR origin: a maintainer pinged or directed groeneai labels Sep 14, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 2 rounds, 1 finding in round 1 and 0 in
round 2) plus my own cold read of the resulting code.

# Sev Finding Verdict Evidence / action
1 ⚠️ A sibling test, 04891_nats_credential_file_path_restriction, carried the same SETTINGS nats_num_consumers = 1 workaround and a comment stating that a collection-only definition is stored with an empty SETTINGS clause. This change is what makes that comment false. AGREE, fixed @ c117d58c Crutch removed and the comment restated there too, so the credential-file replay covers the fixed shape as well. Measured both directions on the shipped text: without the guard that block's short ATTACH gives SYNTAX_ERROR (62) where it asserts BAD_ARGUMENTS (36); with the guard, [ OK ], and 50/50 randomized. The removed value equalled the setting's own default and is read nowhere in the credential provenance logic, so every assertion keeps its meaning.
2 💡 Both replay blocks described the settings-free stored definition rather than requiring it, so a future editor could re-add a filler setting and silently delete the only oracle for this fix. AGREE, fixed @ c117d58c One word in each file: "carries no" becomes "must carry no". No lines added.
3 💡 The PR body named only one of the two changed test files. AGREE The Test paragraph now names both oracles.
4 ⚠️ Declining to repair metadata that an older server already wrote was disclosed in the PR body but never actually put to the owner. AGREE Asked on the issue rather than settling it myself; the disclosure stays in the body. No repair path was added, since it would be a guard whose only precondition is already-corrupt state.
5 ⚠️ The else branch in NATSSettings::loadFromQuery is unreachable after the guard and could be deleted for a shorter diff. DISAGREE StorageNATS.cpp:1213 is its sole caller in the tree, so it is indeed dead, but RabbitMQSettings::loadFromQuery (RabbitMQSettings.cpp:74-95) is structurally identical including that branch, and its registrar pairs it with the same guard. Deleting one copy makes the two helpers diverge for no functional gain. Mutually exclusive with the shipped shape; a one-round change if the other shape is preferred.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding. Findings on hunks unchanged by the fix round are auto-dropped.

Session id: cron:clickhouse-review-slot-10:20260914-154400

@clickhouse-gh clickhouse-gh Bot closed this Sep 14, 2026
@clickhouse-gh clickhouse-gh Bot reopened this Sep 14, 2026
@clickhouse-gh clickhouse-gh Bot closed this Sep 14, 2026
@clickhouse-gh clickhouse-gh Bot reopened this Sep 14, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c117d58]

Summary: ✅


AI Review

Summary

This PR fixes the NATS metadata-corruption path for tables defined from a named collection without a SETTINGS clause by skipping NATSSettings::loadFromQuery when no query settings exist, and it turns two existing DETACH/short-ATTACH blocks into regression coverage for the reload failure. The code matches the established RabbitMQ/Kafka/Distributed registrar pattern, the changed tests now exercise the exact metadata replay path that used to fail with SYNTAX_ERROR, and the current PR discussion plus green CI do not reveal a remaining correctness or coverage gap in the shipped diff.

Final Verdict

Status: ✅ Approve

LLVM Coverage Report

Measured on commit c117d58.

Metric Baseline Current Δ
Lines 89.10% 89.10% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 81.50% 81.50% +0.00%

Changed lines: Changed C/C++ lines covered: 4/5 (80.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 Sep 14, 2026
@evillique evillique self-assigned this Sep 14, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-message-queues Message queue integrations (Kafka, RabbitMQ, NATS table engines for stream ingestion/egress). label Sep 14, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing c117d58c6 with master d90494138 (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.

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

@evillique
evillique added this pull request to the merge queue Sep 15, 2026
Merged via the queue into ClickHouse:master with commit a0a74ce Sep 15, 2026
176 of 348 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 15, 2026
@evillique

Copy link
Copy Markdown
Member

@groeneai Just to check, do we need to backport this?

@groeneai

Copy link
Copy Markdown
Collaborator Author

Yes, I would backport it. All four active release branches are affected, and I reproduced it on the latest official build of each rather than reading it off the source (clickhouse local --path=..., create then reload):

  • 26.3.33.24, 26.6.7.18, 26.7.8.15, 26.8.4.11: a collection-only definition (ENGINE = NATS(collection), no SETTINGS) is stored with SETTINGS and nothing after it, and the next load of that metadata file fails with Code: 62 ... Cannot parse definition from metadata file ... (SYNTAX_ERROR).
  • Control, on each of the four builds: the same definition plus an explicit SETTINGS nats_num_consumers = 1 stores a parseable clause and reloads fine.

Both halves of the mechanism are present on all four: the unguarded nats_settings->loadFromQuery(*args.storage_def) in registerStorageNATS (26.8 StorageNATS.cpp:1199, 26.7 :899, 26.6 :850, 26.3 :866) and the empty ASTSetQuery branch in NATSSettings::loadFromQuery. Nothing master-only is needed to reach it, a named collection carrying nats_url, nats_subjects and nats_format is enough, and the parse failure aborts the whole database load rather than just this table.

If you agree, v26.3-must-backport on this PR covers the set: the floor release plus every newer active branch, so 26.3, 26.6, 26.7 and 26.8.

One heads-up on the cherry-picks. Only the one-line guard applies everywhere; the two test hunks are deletions inside files the older branches do not have (05029_nats_tls_certificate_path_restriction.sql is master-only, 04891_nats_credential_file_path_restriction.sql exists on 26.8 only), so the robot will report a conflict on each branch. Keeping the guard and dropping the hunk for the absent file is the whole resolution, and I will do that when they open.

@groeneai

Copy link
Copy Markdown
Collaborator Author

The four robot cherry-picks of this PR opened CONFLICTING, so here are the resolutions I said I would provide. Each one carries its own comment with the scope, the verification and a two-command fast-forward:

Nothing in any of them is hand-authored: every byte is git's own 3-way merge plus dropping a test file the release branch does not have. src/Storages/NATS/StorageNATS.cpp auto-merges clean on all four, so 26.3, 26.6 and 26.7 receive only the guard (neither NATS test file exists there). 26.8 also receives 04891_nats_credential_file_path_restriction.sql at exactly the blob master carries after this PR, so it keeps the regression oracle. I built each merge with the compiler its branch pins and checked both directions against a binary differing only in that hunk.

robot-clickhouse-ci-1 added a commit that referenced this pull request Sep 16, 2026
Cherry pick #120025 to 26.3: Fix unparseable metadata for a NATS table defined only by a named collection
robot-clickhouse added a commit that referenced this pull request Sep 16, 2026
robot-clickhouse-ci-1 added a commit that referenced this pull request Sep 16, 2026
Cherry pick #120025 to 26.6: Fix unparseable metadata for a NATS table defined only by a named collection
robot-clickhouse added a commit that referenced this pull request Sep 16, 2026
robot-ch-test-poll2 added a commit that referenced this pull request Sep 16, 2026
Cherry pick #120025 to 26.7: Fix unparseable metadata for a NATS table defined only by a named collection
robot-clickhouse added a commit that referenced this pull request Sep 16, 2026
robot-ch-test-poll2 added a commit that referenced this pull request Sep 16, 2026
Cherry pick #120025 to 26.8: Fix unparseable metadata for a NATS table defined only by a named collection
robot-clickhouse added a commit that referenced this pull request Sep 16, 2026
@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 16, 2026
PedroTadim added a commit that referenced this pull request Sep 17, 2026
Backport #120025 to 26.3: Fix unparseable metadata for a NATS table defined only by a named collection
PedroTadim added a commit that referenced this pull request Sep 17, 2026
Backport #120025 to 26.7: Fix unparseable metadata for a NATS table defined only by a named collection
PedroTadim added a commit that referenced this pull request Sep 17, 2026
Backport #120025 to 26.8: Fix unparseable metadata for a NATS table defined only by a named collection
PedroTadim added a commit that referenced this pull request Sep 17, 2026
Backport #120025 to 26.6: Fix unparseable metadata for a NATS table defined only by a named collection
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-message-queues Message queue integrations (Kafka, RabbitMQ, NATS table engines for stream ingestion/egress). groeneai-origin-request PR origin: a maintainer pinged or directed groeneai pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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.

Unguarded NATS loadFromQuery: collection-only table writes unparseable metadata

5 participants