Skip to content

Route mixed settings/comment ALTER ON CLUSTER to every replica - #107142

Merged
alexey-milovidov merged 5 commits into
ClickHouse:masterfrom
groeneai:fix-oncluster-mixed-settings-comment-divergence
Jun 14, 2026
Merged

alexey-milovidov merged 5 commits into
ClickHouse:masterfrom
groeneai:fix-oncluster-mixed-settings-comment-divergence

Conversation

@groeneai

@groeneai groeneai commented Jun 11, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #43843 (ALTER TABLE ... ON CLUSTER ... RESET SETTING not applied on every replica) - same metadata-divergence class.

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 ALTER TABLE ... ON CLUSTER batches that mix MODIFY SETTING/RESET SETTING with a comment change (for example MODIFY COMMENT 'x', MODIFY SETTING old_parts_lifetime = 123) being applied only on the leader replica, leaving the other replicas diverged. Also fix a positional or per-column-SETTINGS MODIFY COLUMN ... COMMENT being misclassified as a local comment-only metadata change, which left the column reorder out of the replicated metadata and could cause INCOMPATIBLE_COLUMNS on replica restart.

Description

DDLWorker::taskShouldBeExecutedOnLeader recognized only single-type settings or comment ALTERs (via isSettingsAlter/isCommentAlter, which require every command in the batch to be the same type). A mixed batch matched neither, so it was routed leader-only. Since settings and comments are applied as local metadata and never written to the replicated log, leader-only routing left the followers without the new setting/comment.

The fix adds ASTAlterQuery::isSettingsOrCommentAlter (the AST-side equivalent of AlterCommands::areNonReplicatedAlterCommands): it returns true when every command is MODIFY SETTING, RESET SETTING, COMMENT COLUMN, MODIFY COMMENT, or a comment-only MODIFY COLUMN, and wires it into the routing so such mixed batches run on every replica. It also switches the immutable-disk gate in MergeTreeData::checkAlterIsPossible to areNonReplicatedAlterCommands so the same mixed metadata-only batches are accepted there instead of throwing SUPPORT_IS_DISABLED.

StorageReplicatedMergeTree::alter also gets a matching fast path. It only short-circuited the replicated log for a pure settings batch or a pure comment batch; a mixed batch fell through to the versioned ALTER_METADATA path. With the router now sending mixed batches to every replica, each replica raced for the same /metadata version, one winning and the rest retrying with CANNOT_ASSIGN_ALTER. A third path gated on commands.areNonReplicatedAlterCommands() now applies these batches locally (settings then comments) and returns before any replicated log entry, keeping the storage classification in lockstep with the router predicate.

This is the same bug class as #43843 (RESET SETTING ON CLUSTER not applied on every replica), fixed the same way.

That storage fast path runs changeSettings before committing the comment metadata. changeSettings is the sole writer of the setting-derived fields escape_index_filenames (and the per-index escape_filenames flag), which commands.apply never sets, so committing the comment metadata afterwards reverted the index filename policy until restart. The fix carries those fields from the snapshot changeSettings just committed into the metadata before the comment commit, so a mixed MODIFY COMMENT 'x', MODIFY SETTING escape_index_filenames = 0 keeps escaping off on every replica.

Finally, a comment-only MODIFY COLUMN is local-only only when it changes nothing but the comment. A positional modifier (FIRST/AFTER) reorders the column and per-column SETTINGS change column metadata, both of which are serialized into the replicated /columns (ColumnsDescription::operator== compares toString(false), which includes column order and settings and excludes only the comment). AlterCommand::isCommentAlter previously ignored placement and per-column settings, so MODIFY COLUMN x COMMENT 'c' FIRST took the local comment fast path: the leader reordered its columns without writing the replicated /columns, and on restart checkTableStructureAttempt compared the locally reordered columns against the stale Keeper /columns and threw INCOMPATIBLE_COLUMNS. This is a pre-existing replicated-storage bug, independent of ON CLUSTER. The fix tightens both AlterCommand::isCommentAlter and the mirroring ASTAlterQuery::isCommentOnlyModifyColumn to require no placement and no per-column settings; such shapes now take the full replicated ALTER_METADATA path (versioned /metadata + /columns rewrite), while a pure comment-only MODIFY COLUMN stays local.

Added tests/integration/test_alter_settings_or_comment_on_cluster, a two-replica ReplicatedMergeTree regression test covering mixed settings/comment batches, comment-only MODIFY COLUMN, a positional MODIFY COLUMN ... COMMENT ... FIRST shape (asserting the ZooKeeper /metadata version moves and that both replicas survive a restart with the new order and no INCOMPATIBLE_COLUMNS), and a mixed MODIFY SETTING escape_index_filenames = 0 batch (asserting the on-disk index filename, not just SHOW CREATE). It also asserts the ZooKeeper /metadata version does not move for a pure settings/comment batch (no replicated log entry, no CANNOT_ASSIGN_ALTER churn). Verified on a debug build that the cases fail on master and pass with this change.

Version info

  • Merged into: 26.6.1.786

DDLWorker::taskShouldBeExecutedOnLeader uses ASTAlterQuery::isSettingsAlter
and isCommentAlter, both of which require every command in the batch to be a
single type. A mixed batch such as

    ALTER TABLE t ON CLUSTER 'c' MODIFY COMMENT 'x', MODIFY SETTING old_parts_lifetime = 123

matches neither, so the routing fell through to the default leader-only path.
StorageReplicatedMergeTree::alter applies settings and comments as local
metadata and never writes them to the replicated log (settings and comments
are not replicated), so leader-only routing leaves the followers diverged: the
leader has the new setting/comment, the followers do not. This is the same bug
class as issue ClickHouse#43843 (RESET SETTING ON CLUSTER), which was fixed by teaching
the routing predicate to recognise the shape.

Add ASTAlterQuery::isSettingsOrCommentAlter, the AST-side equivalent of
AlterCommands::areNonReplicatedAlterCommands: true when every command is
MODIFY SETTING, RESET SETTING, COMMENT COLUMN, MODIFY COMMENT, or a
comment-only MODIFY COLUMN. Wire it into the routing disjunction so mixed
batches execute on every replica. The comment-only MODIFY COLUMN recognition
matches the resolved AlterCommand::isCommentAlter, which the storage layer also
treats as metadata-only; it is intentionally stricter so any unexpected shape
just routes to all replicas (the safe default).

Also switch the immutable-disk gate in MergeTreeData::checkAlterIsPossible from
isSettingsAlter && isCommentAlter to areNonReplicatedAlterCommands, so the same
mixed metadata-only batches are accepted on immutable disks instead of throwing
SUPPORT_IS_DISABLED.

Add tests/integration/test_alter_settings_or_comment_on_cluster covering mixed
MODIFY COMMENT + MODIFY SETTING, MODIFY COMMENT + RESET SETTING, COMMENT COLUMN
+ MODIFY SETTING, comment-only MODIFY COLUMN (standalone and mixed), and a
negative MODIFY COLUMN-with-type-change case, asserting both replicas converge
after each ON CLUSTER ALTER. Verified both directions on a debug build: the
mixed test fails on master (follower and leader miss the setting) and passes
with this change.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate

# Question Answer
a Deterministic repro? Yes. On a debug master binary, ALTER TABLE mixed_alter ON CLUSTER 'cluster' MODIFY COMMENT 'mixed-on-cluster', MODIFY SETTING old_parts_lifetime = 123 followed by SHOW CREATE on both replicas reproduces the divergence every run (the setting/comment are missing). Captured by tests/integration/test_alter_settings_or_comment_on_cluster/test.py::test_mixed_settings_and_comment_alter_on_cluster.
b Root cause explained? DDLWorker::taskShouldBeExecutedOnLeader uses ASTAlterQuery::isSettingsAlter/isCommentAlter, both single-type (all_of) checks. A mixed MODIFY COMMENT + MODIFY SETTING batch matches neither, so it routes leader-only. StorageReplicatedMergeTree::alter applies settings and comments as local metadata and never writes them to the replicated log, so only the leader gets the change and the followers diverge. Same bug class as #43843.
c Fix matches root cause? Yes. Adds ASTAlterQuery::isSettingsOrCommentAlter (AST equivalent of AlterCommands::areNonReplicatedAlterCommands) and wires it into the routing disjunction so mixed metadata-only batches execute on all replicas. The companion immutable-disk gate is switched to areNonReplicatedAlterCommands for consistency. Targets the routing decision directly, not a symptom.
d Test intent preserved / new tests added? New tests/integration/test_alter_settings_or_comment_on_cluster (2 tests): mixed MODIFY COMMENT + MODIFY SETTING, MODIFY COMMENT + RESET SETTING, COMMENT COLUMN + MODIFY SETTING, comment-only MODIFY COLUMN (standalone and mixed), plus a negative MODIFY COLUMN-with-type-change case that must still take the full replicated path. No existing test weakened.
e Both directions demonstrated? Yes. Master baseline (Build ID 19d6788a...): mixed test FAILS (old_parts_lifetime = 123 missing on the replicas). With fix (Build ID e384557a...): both tests PASS, and 10/10 across 5 repeats each.
f Fix is general, not a narrow patch? Yes. The predicate covers all mixed shapes of the same metadata-only command kinds, for both StorageMergeTree and StorageReplicatedMergeTree via the single shared DDLWorker routing point. The comment-only MODIFY COLUMN recognition mirrors the resolved AlterCommand::isCommentAlter the storage layer uses, kept intentionally stricter so unexpected shapes safely route to all replicas. The sibling immutable-disk gate is fixed in the same way. No defensive symptom-guard.

Session id: cron:clickhouse-worker-slot-19:20260611-025200

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @tavplubix @alesapin — could you review this? It fixes ALTER ... ON CLUSTER batches that mix MODIFY SETTING/RESET SETTING with a comment change being routed leader-only in DDLWorker::taskShouldBeExecutedOnLeader (single-type isSettingsAlter/isCommentAlter miss the mixed shape), which left followers diverged since settings/comments are applied as local metadata. Same bug class as #43843. Adds an AST-side isSettingsOrCommentAlter predicate plus a two-replica integration test.

@nikitamikhaylov nikitamikhaylov added the can be tested Allows running workflows for external contributors label Jun 11, 2026
@nikitamikhaylov

Copy link
Copy Markdown
Member

@groeneai Link the issue you are working on. Do it every time for every PR.

@clickhouse-gh

clickhouse-gh Bot commented Jun 11, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8aefff0]

Summary: ✅


AI Review

Summary

This PR fixes ALTER TABLE ... ON CLUSTER routing for mixed settings/comment metadata changes, aligns ASTAlterQuery classification with resolved AlterCommand classification, and keeps StorageReplicatedMergeTree from writing replicated ALTER_METADATA entries for batches that are intentionally local on every replica. The current code also handles the reviewed edge cases around positional/per-column-SETTINGS MODIFY COLUMN ... COMMENT and preserves setting-derived escape_index_filenames metadata in the mixed fast path. I found no remaining blockers or major issues.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 11, 2026
Comment thread src/Parsers/ASTAlterQuery.cpp Resolved
Address review feedback on the routing predicate. The previous helper was
stricter than the storage-side classifier: it rejected positional modifiers
(AFTER/FIRST) and per-column settings. As a result a comment-only
MODIFY COLUMN x COMMENT 'c' AFTER y set command.column, so the helper
returned false and DDLWorker routed the ALTER leader-only. But
AlterCommand::isCommentAlter ignores after_column/first, so the storage layer
took the local comment-only branch and never wrote a replicated log entry,
reproducing the same metadata divergence this change fixes.

Mirror AlterCommand::isCommentAlter's MODIFY_COLUMN branch exactly: a comment
present and no type/codec/default/TTL change, regardless of placement or
per-column settings. This keeps the DDLWorker routing predicate in step with
the storage fast path for every shape the storage treats as non-replicated.

Add an ON CLUSTER regression case for comment-only MODIFY COLUMN ... FIRST
asserting both the column comment and the new column order converge on every
replica.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate (HEAD fe6f4de — review-feedback update)

# Question Answer
a Deterministic repro? Yes. Two-replica ReplicatedMergeTree, ALTER TABLE ... ON CLUSTER 'cluster' MODIFY COLUMN x COMMENT 'c' FIRST (and the mixed settings/comment batches). On the pre-fix predicate the change reaches only the leader; the queried replica's SHOW CREATE is missing the new comment and column order, deterministically.
b Root cause explained? The routing predicate ASTAlterQuery::isSettingsOrCommentAlter must match exactly the shapes the storage takes via its local comment/settings fast path (no replicated log). AlterCommand::isCommentAlter classifies MODIFY COLUMN ... COMMENT as comment-only ignoring AFTER/FIRST and per-column settings; the AST helper was stricter and rejected those, so DDLWorker::taskShouldBeExecutedOnLeader fell through to leader-only → followers diverge.
c Fix matches root cause? Yes. isCommentOnlyModifyColumn now mirrors AlterCommand::isCommentAlter's MODIFY_COLUMN branch exactly (comment present; no type/codec/default/TTL), so the AST routing predicate and the storage local-only path agree for every shape, including positional modifiers.
d Test intent preserved / new tests added? New ON CLUSTER regression case for MODIFY COLUMN x COMMENT 'c' FIRST added to test_modify_column_comment_only_on_cluster, asserting both the comment and the new column order converge on both replicas. Existing cases unchanged. Negative case (real type change → full replicated path) retained.
e Both directions demonstrated? Yes. Baseline binary (strict helper, Build ID e384557a): the FIRST case FAILS (replica missing the comment/order). Fixed binary (Build ID 4b3ff97d): both tests PASS (2 passed).
f Fix is general, not a narrow patch? Yes. The fix aligns the AST predicate with the single source of truth (AlterCommand::isCommentAlter / AlterCommands::areNonReplicatedAlterCommands) rather than enumerating shapes, so it covers AFTER/FIRST and any other shape the storage treats as comment-only. No symptom guard; the predicate is the root-cause site.

Session id: cron:clickhouse-worker-slot-8:20260611-102900

@groeneai

Copy link
Copy Markdown
Collaborator Author

@nikitamikhaylov Done — surfaced the issue visibly in the PR body: Related: #43843 (ALTER TABLE ... ON CLUSTER ... RESET SETTING not applied on every replica), the same metadata-divergence class this fixes. Will keep linking the worked-on issue visibly on every PR going forward.

Comment thread src/Interpreters/DDLWorker.cpp Resolved
The DDLWorker router change (ASTAlterQuery::isSettingsOrCommentAlter) now
sends a batch that mixes settings and comments, for example
ALTER TABLE t ON CLUSTER 'c' MODIFY COMMENT 'x', MODIFY SETTING s = v,
to every replica instead of the leader only. But StorageReplicatedMergeTree::alter
only short-circuited the replicated log for a pure settings batch
(commands.isSettingsAlter()) or a pure comment batch (commands.isCommentAlter()).
A mixed batch matches neither, so it fell through to the replicated ALTER_METADATA
path and did a versioned /metadata update.

With the router sending that batch to all replicas, every replica then attempted
the same versioned /metadata update: one succeeds, the others hit CANNOT_ASSIGN_ALTER,
retry, and append additional no-op ALTER_METADATA entries after applying the local
setting/comment. On a shard with several replicas a single ON CLUSTER metadata-only
alter became several serial replicated alters and could keep the distributed DDL task
retrying or timing out.

Add a third fast path gated on commands.areNonReplicatedAlterCommands(), which is the
resolved companion of the AST router predicate (all commands are settings or comments).
It applies the change locally exactly like the two existing single-type paths combined:
changeSettings for the live storage settings (and their side effects), then
setInMemoryMetadata for the table and column comments, then alterTable, and returns
before writing any replicated log entry. The DDLWorker router and the storage fast path
now classify the same shapes as non-replicated.

The regression test reads the ZooKeeper /metadata node version before and after the
mixed ON CLUSTER alter and asserts it does not change, which proves no replicated log
entry was written and no CANNOT_ASSIGN_ALTER churn occurs.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate (HEAD 812f81d, storage fast-path fixup)

# Question Answer
a Deterministic repro? Yes. On a 2-replica ReplicatedMergeTree, ALTER TABLE t ON CLUSTER 'cluster' MODIFY COMMENT 'x', MODIFY SETTING old_parts_lifetime = 123 makes the followers retry with CANNOT_ASSIGN_ALTER and bumps the ZooKeeper /metadata version. The regression test reads that version and fails deterministically (assert 2 == 0) without the fix.
b Root cause explained? The router (ASTAlterQuery::isSettingsOrCommentAlter) now sends mixed settings/comment batches to every replica, but StorageReplicatedMergeTree::alter only short-circuited the replicated log for a pure isSettingsAlter() or pure isCommentAlter() batch. A mixed batch matched neither and fell through to the versioned ALTER_METADATA path, so every replica attempted the same /metadata update: one wins, the rest hit CANNOT_ASSIGN_ALTER, retry, and append no-op log entries.
c Fix matches root cause? Yes. Adds a third storage fast path gated on commands.areNonReplicatedAlterCommands(), the resolved companion of the router predicate. It applies the batch locally (changeSettings + setInMemoryMetadata + alterTable) and returns before writing a replicated log entry, so router and storage classify the same shapes as non-replicated.
d Test intent preserved / new tests added? Added a churn assertion: read the ZK /metadata version before and after the mixed ON CLUSTER alter and assert it does not change (proves no replicated log entry, no CANNOT_ASSIGN_ALTER). The existing convergence and negative (real type change -> full replicated path) cases are unchanged.
e Both directions demonstrated? Yes. Baseline (no storage fast path, Build ID 4b3ff97d): test_mixed_settings_and_comment_alter_on_cluster FAILS with assert 2 == 0. Fixed (Build ID aede152b): both tests PASS (2 passed in 18.13s), ZK version stays 0.
f Fix is general, not a narrow patch? Yes. Uses the existing areNonReplicatedAlterCommands() predicate (all_of isSettingsAlter

Session id: cron:clickhouse-worker-slot-6:20260611-120300

…ALTER

The mixed settings/comment fast path in StorageReplicatedMergeTree::alter ran
changeSettings and then committed future_metadata via setInMemoryMetadata.
changeSettings is the sole writer of the setting-derived metadata fields
escape_index_filenames (StorageInMemoryMetadata) and the per-index
escape_filenames flag: when escape_index_filenames changes it recomputes both on
its own snapshot and commits them. commands.apply never touches those fields, so
future_metadata still carried the old values and the subsequent
setInMemoryMetadata reverted the index filename policy.

The visible effect: ALTER TABLE t ON CLUSTER ... MODIFY COMMENT 'x',
MODIFY SETTING escape_index_filenames = 0 left escape_index_filenames = 0 in
SHOW CREATE on every replica while the in-memory policy stayed escaped, so new
parts were written with escaped index filenames (skp_idx_<escaped>.idx2) until a
restart, diverging from the pure MODIFY SETTING path.

Carry the setting-derived escape fields from the snapshot changeSettings just
committed into future_metadata before the comment commit. changeSettings still
runs first, so its sanityCheck rejects bad settings before anything is committed.

The integration test writes a part on each replica after the mixed ON CLUSTER
alter and asserts the on-disk skip-index filename is unescaped on both, which
fails on the previous code (escaped name written) and passes now.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate (HEAD 22407b5)

# Question Answer
a Deterministic repro? Yes. ALTER TABLE escape_mixed ON CLUSTER 'cluster' MODIFY COMMENT 'escape-mixed', MODIFY SETTING escape_index_filenames = 0 then an INSERT: on the pre-fix binary the new part is written with the escaped index filename skp_idx_idx%2Desc.idx2 on every replica even though SHOW CREATE reports escape_index_filenames = 0. Encoded as integration test test_mixed_setting_escape_index_filenames_on_cluster.
b Root cause explained? changeSettings is the sole writer of the setting-derived metadata escape_index_filenames (and the per-index escape_filenames flag): when the setting changes it recomputes both on its own snapshot and commits them. commands.apply never sets those fields. The mixed fast path called changeSettings, then committed future_metadata via setInMemoryMetadata; future_metadata still held the old escape values, so the commit reverted the index filename policy until restart.
c Fix matches root cause? Yes. After changeSettings (which already committed the new values) the fix reads them back from the committed snapshot and carries escape_index_filenames + per-index escape_filenames into future_metadata before the comment commit, so setInMemoryMetadata no longer reverts them. changeSettings still runs first, preserving its sanityCheck-before-commit exception safety.
d Test intent preserved / new tests added? New test test_mixed_setting_escape_index_filenames_on_cluster added. It inspects the on-disk skip-index filename (the materialization of the in-memory escape_filenames), not just SHOW CREATE, since SHOW CREATE already reflected the setting. Existing 2 tests in the file unchanged and still pass.
e Both directions demonstrated? Yes. Pre-fix binary (Build ID aede152b): test FAILS, ch1 writes skp_idx_idx%2Desc.idx2 (escaped, reverted policy). Fixed binary (Build ID d13c8ec1): all 3 tests pass (3 passed in 32.30s), unescaped skp_idx_idx-esc.idx2 written on both replicas.
f Fix is general, not a narrow patch? The mixed fast path is now the only place that combined changeSettings + setInMemoryMetadata(future_metadata); the pure-settings path only calls setInMemoryMetadata conditionally (statistics_changed) and the pure-comment path never calls changeSettings, so neither reverts the escape fields. escape_index_filenames and refresh_statistics_interval are the only setting-derived metadata changeSettings recomputes; the latter is not part of StorageInMemoryMetadata, so escaping is the only field needing carry-over here. No sibling path has the same revert.

Session id: cron:clickhouse-worker-slot-1:20260611-134100

Comment thread src/Parsers/ASTAlterQuery.cpp Resolved
AlterCommand::isCommentAlter (and the matching DDL routing predicate
ASTAlterQuery::isCommentOnlyModifyColumn) classified a positional
MODIFY COLUMN x COMMENT 'c' FIRST/AFTER y, or one carrying per-column
SETTINGS, as comment-only. But a placement modifier reorders the column
and per-column settings change column metadata, both serialized into the
replicated /columns (ColumnsDescription::operator== compares
toString(false), which includes column order and settings, excluding only
the comment). The comment-only classification made the storage take the
local fast path: it applied the reorder/settings locally without writing a
replicated /columns log entry, so a replica restart compared the locally
reordered columns against the stale ZooKeeper /columns and threw
INCOMPATIBLE_COLUMNS. This was a pre-existing replicated-storage bug,
independent of ON CLUSTER.

Tighten both predicates so only a pure comment change (no placement, no
per-column settings) stays local; positional or settings MODIFY COLUMN now
takes the full replicated ALTER_METADATA path, which versions /metadata and
rewrites /columns in ZooKeeper, keeping every replica consistent across
restarts.

Extended the ON CLUSTER regression test: the positional case now asserts the
ZooKeeper /metadata version moves (replicated path, not the local fast path)
and that both replicas survive a restart with the new column order and no
INCOMPATIBLE_COLUMNS.

Related: ClickHouse#43843

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

You are right, and thanks. Verified the chain in source: a positional MODIFY COLUMN x COMMENT 'c' FIRST/AFTER y reorders the column via ColumnsDescription::modifyColumnOrder, and ColumnsDescription::operator== compares toString(false), which serializes column order (and per-column SETTINGS), excluding only the comment. So the reorder/settings change the replicated /columns, while AlterCommand::isCommentAlter was classifying it as comment-only, taking the local storage fast path that never writes /columns to Keeper. checkTableStructureAttempt then compares local columns against the stale /columns and throws INCOMPATIBLE_COLUMNS on restart. Pre-existing on master, not ON CLUSTER specific.

This pointed in the opposite direction of the earlier relaxation, so I went to the root cause: tightened both predicates to be comment-only ONLY for a pure comment change.

  • AlterCommand::isCommentAlter (storage classifier) now requires settings_changes.empty() && settings_resets.empty() && after_column.empty() && !first in addition to the comment/no-type/no-codec/no-default/no-ttl conditions.
  • ASTAlterQuery::isCommentOnlyModifyColumn (DDL routing) mirrors it exactly: no inline SETTINGS, no MODIFY/RESET SETTING, no placement (column/first).

Now a positional or per-column-settings MODIFY COLUMN takes the full replicated ALTER_METADATA path (versioned /metadata set + /columns rewrite), so the reorder is replicated and survives restart. A pure comment-only MODIFY COLUMN stays on the local fast path.

Regression added to test_modify_column_comment_only_on_cluster: the positional FIRST case now asserts the Keeper /metadata version moves (proving the replicated path, not the local fast path), and both replicas are restarted and verified to come back with the new order and no INCOMPATIBLE_COLUMNS.

Verified both directions on a debug build (the new positional/restart assertion):

  • Baseline (loose isCommentAlter, Build ID d13c8ec1): FAILS at assert get_zk_metadata_version() > version_before (assert 0 > 0) - the local fast path left /metadata at version 0. Matches your prediction.
  • Fixed (Build ID 4878bcd5): all 3 tests in the file pass (3 passed), positional case 3/3 on repeat.

@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate (HEAD 8aefff0)

# Question Answer
a Deterministic repro? Yes. test_modify_column_comment_only_on_cluster: ALTER TABLE t ON CLUSTER 'cluster' MODIFY COLUMN x COMMENT '...' FIRST, then read the Keeper /metadata version and restart both replicas. On the baseline binary it deterministically fails at assert get_zk_metadata_version() > version_before (assert 0 > 0): the local fast path left /metadata at version 0.
b Root cause explained? A positional (FIRST/AFTER) or per-column-SETTINGS MODIFY COLUMN reorders the column / changes per-column settings. ColumnsDescription::operator== compares toString(false), which serializes column order and per-column settings (excluding only the comment), so these shapes change the replicated /columns. AlterCommand::isCommentAlter classified them as comment-only, so StorageReplicatedMergeTree::alter took the local fast path and never wrote /columns to Keeper. On restart checkTableStructureAttempt compares the locally reordered columns against the stale Keeper /columns via operator== and throws INCOMPATIBLE_COLUMNS. Pre-existing on master, independent of ON CLUSTER.
c Fix matches root cause? Yes. Tightened the resolved AlterCommand::isCommentAlter to its documented contract ("only comment changed"): require settings_changes.empty() && settings_resets.empty() && after_column.empty() && !first. Mirrored the same condition in the DDL routing predicate ASTAlterQuery::isCommentOnlyModifyColumn. Positional/settings MODIFY COLUMN now takes the full replicated ALTER_METADATA path (versioned /metadata + /columns rewrite); pure comment-only stays local. Not a guard at the crash site.
d Test intent preserved / new tests added? Yes. Extended test_modify_column_comment_only_on_cluster: the positional FIRST case asserts the Keeper /metadata version moves (proving the replicated path, not the local fast path) and both replicas are restarted and verified to come back with the new column order and no INCOMPATIBLE_COLUMNS. The pure comment-only case still asserts the /metadata version does NOT move. The mixed and escape-index-filenames tests are unchanged and still pass.
e Both directions demonstrated? Yes. Baseline (loose isCommentAlter, Build ID d13c8ec1): FAILS (assert 0 > 0 at the positional case). Fixed (Build ID 4878bcd5, differs -> edits in binary): all 3 tests in the file pass (3 passed in 35.71s); positional case 3/3 on repeat (54.25s).
f Fix is general, not a narrow patch? Yes. The fix is at the shared classifier AlterCommand::isCommentAlter, so every caller (StorageReplicatedMergeTree::alter, StorageMergeTree::alter, AlterCommands::isCommentAlter/areNonReplicatedAlterCommands, and the checkAlterIsPossible of View/MaterializedView/TimeSeries/RocksDB/WindowView/ObjectStorage) now sees the same correct classification, plus the AST routing predicate that mirrors it. Grep confirms no existing test used MODIFY COLUMN ... COMMENT ... FIRST/AFTER or per-column SETTINGS, so the only behavior change for non-MergeTree storages is correctly rejecting (NOT_IMPLEMENTED) positional/settings comment-modifies they never supported (previously silently dropped). Traced the bad state to its origin (the misclassification), not the restart-time symptom.

Session id: cron:clickhouse-worker-slot-1:20260611-150400

@clickhouse-gh

clickhouse-gh Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.60% 84.60% +0.00%
Functions 92.30% 92.30% +0.00%
Branches 77.20% 77.20% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 58/63 (92.06%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 14 line(s) · Uncovered code

Full report · Diff report

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finished on 8aefff0. All checks green (144 success, 22 skipped, 0 failures). No PR-caused failures. Ready for review.

(cron:our-pr-ci-monitor:20260611-210000)

if (!col_decl)
return false;

return col_decl->getComment() != nullptr

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 code is correct but error-prone.

@alexey-milovidov alexey-milovidov 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

@alexey-milovidov alexey-milovidov self-assigned this Jun 14, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jun 14, 2026
Merged via the queue into ClickHouse:master with commit 0e19629 Jun 14, 2026
166 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 14, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Jun 15, 2026
…ttings-simple-aggregate

Resolve conflict in StorageReplicatedMergeTree::alter: master gained the
areNonReplicatedAlterCommands() local-apply block for mixed settings/comment
ALTERs (PR ClickHouse#107142). Keep it and place this PR's sortingKeyTypesChanged guard
on verifySortingKey after that block, so settings/comment-only ALTERs return
early and the suspicious-key check fires only when the sorting key types
actually change.
pull Bot pushed a commit to Haofei/ClickHouse that referenced this pull request Sep 6, 2026
…tabase

`DatabaseReplicated::shouldReplicateQuery` gated the replicated path for an
`ALTER` on a `KeeperMap` table on `ASTAlterQuery::isCommentAlter`, which
requires every command in the query to be the same single AST type. The
storage accepts a strictly larger set: `StorageKeeperMap` inherits the base
`alter`, so `IStorage::checkAlterIsPossible` accepts
`AlterCommand::isCommentAlter` per command, which also covers a comment-only
`MODIFY COLUMN`. Anything in that gap ran locally with no
`ZooKeeperMetadataTransaction` and still reached
`DatabaseReplicated::commitAlterTable`, whose first statement asserts a
transaction is present while the DDL worker is active.

Two user-reachable carriers, both against a `KeeperMap` table in a
`Replicated` database:

    ALTER TABLE t MODIFY COMMENT 'a', COMMENT COLUMN c 'b';
    ALTER TABLE t MODIFY COLUMN c COMMENT 'a';

The second is a single command, so the trigger is the type classification and
not the command count; a single-type comment `ALTER` was always routed
correctly.

`chassert` and `assertDigest` both compile away outside debug and sanitizer
builds, so on a release build the `ALTER` completes: local metadata and the
local digest advance while Keeper keeps the pre-`ALTER` `CREATE` statement and
digest. Other replicas therefore never receive the change, and at the next
start `local_digest != zk_digest` makes `DatabaseReplicatedWorker` declare the
replica lost (`check_consistency` defaults to true) and run
`recoverLostReplica`, which reverts the `ALTER`. Measured on a single-replica
database holding 50 rows, that recovery logged "Will DROP TABLE km, because it
does not store data on disk and can be safely dropped" and the table came back
empty with its Keeper data children going from 50 to 0, while a `MergeTree`
control table in the same database kept all 77 of its rows. That drop branch is
the pre-existing heuristic tracked in
ClickHouse#111957 and is unchanged here;
this commit removes one deterministic trigger of it.

The fix uses `ASTAlterQuery::isSettingsOrCommentAlter`, added in
ClickHouse#107142 for exactly this class and
already wired into `DDLWorker.cpp`, whose comment there names mixed batches
"that match neither single-type predicate". That routing site was fixed and
this one was missed. `isCommentAlter` implies `isSettingsOrCommentAlter`, so the
change can only widen the set of queries that take the replicated path: nothing
replicated before stops being replicated. The predicate also accepts
`MODIFY SETTING` and `RESET SETTING`, which `KeeperMap` has none of; those
still fail with an identical error code and message before and after, verified
by comparing both binaries, at the cost of one extra DDL log entry.

Found by the BuzzHouse fuzzer on the arm_asan_ubsan build:
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117591&sha=1520bd2704e60296068431f9f1144b2112ec03ff&name_0=PR&name_1=BuzzHouse%20%28arm_asan_ubsan%29
ClickHouse#117591

Co-Authored-By: Claude Opus 5 <[email protected]>
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 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.

4 participants