Skip to content

Resolve each command of an ALTER against the columns the previous commands left - #127897

Open
groeneai wants to merge 26 commits into
ClickHouse:masterfrom
groeneai:alter-command-sees-previous-commands
Open

groeneai wants to merge 26 commits into
ClickHouse:masterfrom
groeneai:alter-command-sees-previous-commands

Conversation

@groeneai

@groeneai groeneai commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #127444
Closes: #117824
Related: #127497

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

Each command of an ALTER TABLE now resolves its column, including IF EXISTS and IF NOT EXISTS, against the columns the previous commands of the same statement added, dropped or renamed, so DROP COLUMN b, ADD COLUMN IF NOT EXISTS b String no longer loses b. A column that one ALTER renames or drops and then adds again no longer reads the dropped data back from a wide part, loses the data of the renamed column on a compact part, or leaves a broken part. Closes #127444, #117824.

Description

On 26.8.18.2 and 26.3.42.3, ALTER TABLE t DROP COLUMN b, ADD COLUMN IF NOT EXISTS b String loses b, and on a wide part RENAME COLUMN q TO r, DROP COLUMN r, ADD COLUMN r UInt32 DEFAULT 7 returns the dropped 42 instead of 7.

This PR carries the five commits of #127497 by @ vitlibar (authorship kept): AlterCommands::prepare decides IF EXISTS / IF NOT EXISTS against the columns the earlier commands leave, and DROP COLUMN n drops a Nested group member by member. On top of them:

  • splitAndModifyMutationCommands names columns as the part stores them: a DROP of a column renamed earlier replaces that RENAME, and a later command on a dropped and re-added name does not touch the stored column, and a column's files are removed once (DROP n.a, ADD n.a, DROP n.a and CLEAR n.a, DROP IF EXISTS n.a no longer empty n.b, both broken on master too). The NOT_IMPLEMENTED rejection of [WIP] ALTER command sees previous commands #127497 is removed.
  • Missing-column markers (skip_empty_columns_on_insert) follow the same renames and drops; DROP q, RENAME x TO q with a marker x no longer leaves a never-finishing mutation (also on 26.9 and 26.10).
  • AlterConversions no longer treats DROP q after RENAME q TO r, ADD q as a drop of r.
  • prepare builds its snapshot with the code apply uses; validate follows the ALIAS kind and keeps a cleared column. Renaming a column the statement cleared is rejected.
  • The "empty parts are not allowed" check follows the renames of the ALTER.

Unchanged, as on master: RENAME then CLEAR; MODIFY ... REMOVE <property> sees the properties from before the statement (apart from the default kind); a column re-added under a name a RENAME freed reads the old data until the mutation finishes. Supersedes #117593. The open #117825 also closes #117824 and changes the same function.

Validated: 05444_alter_command_sees_previous_commands and 05452_alter_rename_drop_readd_replicated fail on master and pass with the fix in 50 randomized runs.


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

vitlibar and others added 16 commits October 26, 2026 13:40
`AlterCommands::prepare` used the columns before the ALTER, so a MODIFY COLUMN
of a column added by the same statement was bound to a subcolumn with the same
name, and IF EXISTS skipped columns added or dropped by a previous command.

ClickHouse#127444
(cherry picked from commit f57deb8)
The snapshots appended every added column at the end, so DROP COLUMN of a
Nested group could remove other columns than `apply` does and a later
IF NOT EXISTS was decided against the wrong columns. The snapshots now insert
the added columns and move modified columns the way `apply` does.

(cherry picked from commit 97e17f3)
`apply` skipped `DROP COLUMN IF EXISTS n` when only `n.a`, `n.b` existed,
while `prepare`, `validate` and the mutation planning treated `n` as the
Nested group. The mutation then deleted the data of `n.*` and the columns
stayed in the table, reading as empty.

(cherry picked from commit d0bd1fe)
`DROP COLUMN n` of the Nested group `n.a`, `n.b` became one mutation command
`DROP COLUMN n`, but a mutation matches the columns of a part by their exact
names. When the table had a column named like a member again by the time the
mutation ran, after `DROP COLUMN n, ADD COLUMN IF NOT EXISTS n.b` or a
separate `ADD COLUMN n.b`, a wide part kept the data of the dropped member
and the new column showed it. The mutation now drops each member by name.

(cherry picked from commit 0fd5386)
`DROP COLUMN q, ADD COLUMN q ..., RENAME COLUMN q TO r` put `DROP COLUMN q`
and `RENAME COLUMN q TO r` into one mutation. The mutation finds `q` in a
part by its name, so it renamed the data of the dropped column to `r` while
the drop removed the files, and the part listed `r` without any data for it.
Separate ALTER queries are refused while the drop's mutation is pending, so
the rename is rejected in the same ALTER too.

(cherry picked from commit 616bcf5)
…lumns

`AlterCommands::prepare` advanced its snapshot with a column built from the
name and the type only, and modified it only for the type, the merged enum
type and the placement. An ALIAS added by the statement therefore got storage
subcolumns in the snapshot, so `ADD COLUMN p Tuple(a UInt32) ALIAS ...,
MODIFY COLUMN IF EXISTS p.a CODEC(LZ4)` (with `enable_subcolumn_codecs`)
was not skipped and failed with "Column `p` is ALIAS, so it cannot have codecs",
while master skips the MODIFY.

`prepare` now builds the added column and modifies a column with the code
`AlterCommand::apply` uses (`getAddedColumnDescription`,
`modifyColumnDescription`), so the snapshot carries the default kind, comment,
codec, TTL, settings and statistics exactly as apply leaves them. The implicit
indices are refreshed only by apply, at the same point as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
One ALTER is one mutation entry whose DROP_COLUMN and RENAME_COLUMN commands
name the columns as the statement does at that point. For a wide part with
per-file storage, `splitAndModifyMutationCommands` pushed a DROP under the
current name, but `getColumnsForNewDataPart` and `collectFilesForRenames`
resolve it against the names the source part stores, while still applying
the earlier RENAME. So `RENAME COLUMN q TO r, DROP COLUMN r, ADD COLUMN r
UInt32 DEFAULT 7` kept the files of `q` as `r` and read 42 instead of 7 after
the mutation, also after OPTIMIZE FINAL (the same for the IF EXISTS spelling
and for `RENAME n.a TO n.c, DROP n, ADD n.c`).

The per-file branch now follows each column through the earlier commands of
the batch: a DROP of a column renamed earlier replaces that RENAME in place by
a DROP of the stored name, a RENAME (from the commands or the rename map) of a
stored column dropped earlier is skipped, because its name belongs to a column
added again, and a drop of a whole Nested group, as entries written by older
versions have it, drops each column of the group. The interpreter branch skips
replaying a rename of a column of such a group drop, which otherwise failed
the mutation on a compact part. The stored entry, AlterConversions and the
command stream given to the interpreter are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…column

`MergeTreeData::checkAlterEligibility` refuses an ALTER that would drop every
stored column of a part, looking the dropped names up in the metadata from
before the ALTER. A drop of a column renamed earlier in the same ALTER was
therefore not seen: on a table whose part stores only `q`, `RENAME COLUMN q TO
r, DROP COLUMN r, ADD COLUMN r UInt32 DEFAULT 7` was accepted, and the
mutation then failed forever with "none of its columns (q) would remain" (the
table being unreadable on a compact part), while `DROP COLUMN q, ADD COLUMN q
...` is refused before any change. Conversely, `RENAME COLUMN q TO r, ADD
COLUMN q UInt32 DEFAULT 7, DROP COLUMN q` was refused although the data stays
as `r`.

A non-CLEAR drop of a column the ALTER has at that point now resolves through
the lineage model this function already has (`old_names`, shared through two
small helpers): it records the pre-ALTER column the dropped one was renamed
from, and nothing for a column the ALTER added. A drop of a whole Nested group
and every CLEAR COLUMN are collected as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
With the stored names followed in `splitAndModifyMutationCommands`, the
mutation of `DROP COLUMN q, ADD COLUMN q ..., RENAME COLUMN q TO r` removes
the stored `q` and skips the rename of it, so the part no longer lists `r`
without a stream. The rejection of that statement in `AlterCommands::validate`
is therefore removed. It also applied to every engine, so `ENGINE = Memory`,
which master accepts, threw NOT_IMPLEMENTED.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…olumn

The statement of ClickHouse#117824, with
the IF forms on both sides and a stored row: the column is added back with
the new type, and the row reads the default.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`min_bytes_for_full_part_storage` is randomized by the test runner, and a
packed part takes the interpreter branch of the mutation, which never had the
defect, so a case on such a part passes without the fix. Measured on master:
the first case of 05452 read `42` (the defect) in 10 of 10 randomized runs
with the setting pinned to 0, and passed in a randomized run without it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
After `RENAME COLUMN q TO r`, a later `DROP COLUMN IF EXISTS q` of the same
statement is ignored (`q` no longer exists), so it removes nothing. The
empty-part check still recorded the pre-ALTER column `q` as dropped and
refused the ALTER on a table whose parts store only `q`:
`Cannot drop or clear column 'q' ... Empty parts are not allowed`.

An ignored command now contributes nothing to the set of dropped columns.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…act part

`DROP COLUMN n, ADD COLUMN n Nested(a UInt32), RENAME COLUMN n.a TO n.c` is
stored as `[DROP COLUMN n.a, RENAME COLUMN n.a TO n.c]`. The per-file branch
of splitAndModifyMutationCommands already skips that rename, but the
interpreter branch (compact parts, packed storage, dynamic columns) replayed
the composed `n.c <- n.a` pair: on a compact part the reader looked the
dropped `n.a` up in the table and the mutation failed forever with
NO_SUCH_COLUMN_IN_TABLE after the metadata was committed, and on a packed
part `n.c` read back the dropped data.

Skip a rename-map pair in that branch when a non-CLEAR drop of the same name
comes earlier in the batch. The rule is ordered on purpose: in
`RENAME q TO r, ADD COLUMN q, DROP COLUMN q` (stored `[RENAME q->r, DROP q]`)
the drop names the new `q`, and `r <- q` must still be replayed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e same mutation

`RENAME COLUMN q TO r, ADD COLUMN q UInt32 DEFAULT 7, DROP COLUMN q` is stored
as `[RENAME COLUMN q TO r, DROP COLUMN q]`. The drop names the new `q`, which
no part stores, but AlterConversions::addMutationCommand recorded it as a
dropped `q`, and `q` is the name the part stores `r` under. Every reader that
asks isColumnDropped for the part-side name then treated `r` as dropped: on
compact and packed parts the mutation rewrote `r` with its default (the data
was lost), and on any part a read while the mutation was pending returned the
default or failed with NO_SUCH_COLUMN_IN_TABLE.

After a rename of A in the batch, the name A can only belong to a column added
again (a second rename of A is refused, and a rename into A is a pair whose
`rename_to` is A, handled first), so a non-CLEAR drop of such a name records
nothing. CLEAR COLUMN is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… an ALTER

With `flatten_nested = 0`, `ADD COLUMN n Nested(a UInt32)` adds the single
column `n`, but advanceColumnOldNames always inserted the flattened members
(`n.a`) as added columns. On a table with `share_nested_offsets = 0`,
`ADD COLUMN n Nested(a UInt32), RENAME COLUMN x TO n.a, DROP COLUMN n.a` then
lost the lineage of `x` at the rename (the synthetic `n.a` kept its key), so
the empty-part check saw no dropped stored column and let a one-column table
commit the new metadata. The mutation then failed forever because no column
of the part would remain, and SELECT threw NO_SUCH_COLUMN_IN_TABLE.

The lineage now flattens an added column only when `flatten_nested` is set,
as AlterCommand::apply does; both callers in checkAlterEligibility pass the
setting of the ALTER.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…t check of an ALTER

With flatten_nested = 1, AlterCommand::apply adds only the flattened members
of an added Nested column (n.a), not n itself. The rename lineage still
recorded a synthetic n, so `ADD COLUMN n Nested(a UInt32), RENAME COLUMN x TO
n, DROP COLUMN n` lost the lineage of x, the empty-part check was skipped and a
one-column table got a mutation that fails forever. The lineage now records
the flattened members in that case and the column itself otherwise.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@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; 8 findings over 5 rounds), plus my own review of the tree.

# Sev Finding Verdict Evidence / action
1 ❌ An ignored DROP COLUMN IF EXISTS after a rename still counted as dropping the stored column, so RENAME COLUMN q TO r, DROP COLUMN IF EXISTS q on a one-column table was refused as an empty part (getColumnsDroppedByAlter) AGREE, fixed @ 6b1341a the empty-part check skips ignored drops; new case in 05444
2 ❌ DROP COLUMN n, ADD COLUMN n Nested(a UInt32), RENAME COLUMN n.a TO n.c replayed the rename of the dropped n.a in the interpreter branch of splitAndModifyMutationCommands: the mutation failed forever on a compact part and read the dropped data back on a packed part AGREE, fixed @ 7710576 a rename of a column dropped earlier in the batch is skipped there too, as on wide parts; compact case in 05444
3 ❌ AlterConversions took the DROP q of RENAME q TO r, ADD q, DROP q for a drop of the column stored as q, so r lost its data on compact and packed parts and read 0 while the mutation was pending AGREE, fixed @ 96f3727 a drop of a name an earlier rename moved away records nothing; compact and pending-read cases in 05444. Not changed: during the mutation of #2's statement an on-the-fly read of the re-added Nested member on a compact part still throws, as on master (MergeTreeReaderCompact)
4 ⚠️ MODIFY COLUMN x <type>, DROP COLUMN x, ADD COLUMN x ..., MODIFY COLUMN x ADD ENUM VALUES (...) is refused DISAGREE master refuses it identically (AlterCommands::prepare, "Cannot combine MODIFY COLUMN with an explicit type and MODIFY COLUMN ... ADD ENUM VALUES in a single ALTER query"); not touched here
5 ⚠️ MODIFY COLUMN ... REMOVE <property> does not see a property set by an earlier command of the same ALTER DISAGREE master refuses all of these identically (validate's property checks are unchanged here); the PR description says so and claims only column resolution
6 ❌ ADD COLUMN n Nested(...), MODIFY COLUMN IF EXISTS n ... is refused instead of being a no-op DISAGREE master refuses MODIFY COLUMN IF EXISTS n on an existing Nested group the same way (prepare finds the group through hasNested, then There is no column n in table); a group added by an earlier command now resolves like an existing one
7 ❌ With flatten_nested = 0 the empty-part check modelled ADD COLUMN n Nested(a UInt32) as adding n.a, so on a share_nested_offsets = 0 table ADD COLUMN n Nested(a UInt32), RENAME COLUMN x TO n.a, DROP COLUMN n.a lost the lineage of x, committed the metadata and left the mutation failing forever AGREE, fixed @ f19d9aa the check flattens an added Nested only when flatten_nested is set, as apply does; new case in 05444
8 ❌ The other half of #7: with flatten_nested = 1 the check also recorded an unflattened n for ADD COLUMN n Nested(a UInt32), so ADD COLUMN n Nested(a UInt32), RENAME COLUMN x TO n, DROP COLUMN n on a one-column table lost the lineage of x, committed n.a alone and left the mutation failing forever and the table unreadable AGREE, fixed @ 96ac129 an added column contributes exactly the names apply adds; new case in 05444

Bounce cap reached: published as-is after the final fix round (the fix of #8 was not re-gated). No residual findings.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. One statement on a one-row table with a wide part, clickhouse local: RENAME COLUMN q TO r, DROP COLUMN r, ADD COLUMN r UInt32 DEFAULT 7 reads 42 (also after OPTIMIZE FINAL), RENAME n.a TO n.c, DROP n, ADD n.c reads [11], and DROP q, ADD q DEFAULT 7, RENAME q TO r leaves r without a stream (debug abort). Reproduced at the ported #127497 head and on master.
b Root cause explained? One ALTER is one mutation entry whose commands name columns as the statement does at that point. The per-file plan of a wide-part mutation pushed a DROP under the current name, while its consumers resolve it against the names the source part stores and still apply the earlier RENAME, so the dropped data survived under the new name or a rename of dropped data left an empty column. Separately, prepare built added columns from name and type only, and the empty-part check looked dropped names up in the pre-ALTER metadata.
c Fix matches root cause? Yes. splitAndModifyMutationCommands follows each column through the earlier commands of the batch and names stored columns (a DROP of a renamed column replaces that RENAME in place; a rename of a dropped stored column is skipped; an older-format group drop is expanded). prepare reuses apply's column construction; the empty-part check reuses the existing rename lineage. The NOT_IMPLEMENTED rejection is removed because the mutation is now correct.
d Test intent preserved / new tests added? Yes. 05444_alter_command_sees_previous_commands extended (rename/drop/re-add of scalars, Nested members and groups, IF forms, in-place order, Memory, ALIAS, single-column empty-part checks with flatten_nested 0 and 1, wide, compact and pending-mutation cases, #117824 IF forms); its two NOT_IMPLEMENTED cases become value checks. New 05452_alter_rename_drop_readd_replicated for ReplicatedMergeTree.
e Both directions demonstrated? Yes. Rebuilt with each change reverted; each revert brings back its symptom (dropped data read back as 42 / [11], a mutation failing forever with NO_SUCH_COLUMN_IN_TABLE, the empty-part check refusing a valid or accepting a breaking single-column ALTER, the ALIAS case throwing, a debug abort). The new cases fail on master. 50/50 randomized runs pass for both tests.
f Fix is general across code paths? Both branches of splitAndModifyMutationCommands (per-file and interpreter), MergeTree and ReplicatedMergeTree (shared MutateTask), entries written before the producer change (wide and compact parts give the right result), Memory (metadata-only), on-the-fly reads through AlterConversions, and the empty-part check and index rename lineage, which record an added Nested as apply adds it. CLEAR COLUMN is unchanged.
g Fix generalizes across inputs (params/datatypes/wrappers)? Scalar columns, Nested members and whole groups, IF EXISTS / IF NOT EXISTS forms, wide, compact and packed parts, single-column and multi-column tables, chained renames to a freed name.
h Backward compatible? (maintainer-approved exception only) Yes. No setting or format change; the stored mutation entry and system.mutations are unchanged. Behaviour changes only for statements that lost a column, read dropped data, left a broken part, or were rejected.
i Invariants and contracts preserved? The file-level plan drops and renames by stored names in batch order: the DROP takes the RENAME's position, so RENAME q TO r, RENAME p TO q, DROP r drops the old q before renaming p to q. The command stream given to the interpreter and the count of command-derived file entries are unchanged.

Session id: cron:clickhouse-impl-slot-4:20261026-133900

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @Michicosun @Algunenano, could you review this and add the can be tested label so CI runs? It carries the five commits of #127497 by @ vitlibar (each command of an ALTER is prepared against the columns the previous commands leave) and makes the mutation name the columns a part actually stores, so a column that one ALTER renames or drops and then adds again no longer reads the dropped data back or leaves a part the mutation cannot process.

@clickhouse-gh

clickhouse-gh Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c39e9f4]

Summary: ✅


AI Review

Summary

ALTER TABLE resolves column commands against the schema left by preceding commands, and mutation planning follows stored column names through renames, drops, and re-additions. All five earlier inline findings are addressed in c39e9f43c4ef53c808770be6caf6f8d64cae39ad. No new actionable findings.

Final Verdict

Status: ✅ Approve.

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Oct 27, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 27, 2026
Comment thread src/Storages/MergeTree/MutateTask.cpp Resolved
Comment thread src/Storages/AlterCommands.cpp Resolved
groeneai and others added 4 commits October 27, 2026 18:49
…aming it

A wide part written with `skip_empty_columns_on_insert` stores no data for
a column whose values are all defaults, only a marker in its serialization
infos. In the per-file branch of `splitAndModifyMutationCommands` a drop of
such a column did not reach `dropped_in_batch`, so for
`DROP COLUMN q, ADD COLUMN q UInt32 DEFAULT 7, RENAME COLUMN q TO r` the
rename map entry `r <- q` was still replayed and `getColumnsForNewDataPart`
carried the marker of the dropped `q` over to `r`: `r` read `0` instead of
`7`, also after `OPTIMIZE FINAL`. Compact and packed parts were already
right because the interpreter branch skips such renames with the ordered
`renamed_after_drop` set.

The set is now computed once for both branches and also used by the
per-file branch, so a rename whose source the batch dropped earlier is not
replayed whether the part stores data or only the marker for it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`prepare` advances its snapshot with the code `apply` uses, so after
`MODIFY COLUMN p DEFAULT ...` an ALIAS `p` is a stored column there and a
later `MODIFY COLUMN IF EXISTS p.a CODEC(...)` resolves to its subcolumn and
is applied. `validate` kept the ALIAS kind, found no subcolumn `p.a` and
skipped the command, so it never ran `validateSubcolumnCodecCommand`: the
codec was stored with `enable_subcolumn_codecs = 0`, on `ENGINE = Memory`,
and as `CODEC(NONE, LZ4)` with `allow_suspicious_codecs = 0`. The same
held for `MODIFY COLUMN p REMOVE ALIAS`.

`validate` now sets the default kind of the column it modifies (not the
expression, which the dependency check of DROP COLUMN must keep seeing as
before), so it resolves the same subcolumns as `prepare`. As a side effect
the checks of a later command see the kind too:
`MODIFY COLUMN p DEFAULT 1, MODIFY COLUMN p REMOVE DEFAULT` of an ALIAS is
accepted, and `MODIFY COLUMN p DEFAULT 1, MODIFY COLUMN p REMOVE ALIAS` is
rejected because `p` is no longer an ALIAS.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`AlterCommands::validate` removed a column on `CLEAR COLUMN` as if it
were dropped, while `prepare` and `apply` keep it. A later command of the
same statement was then validated against a table without the column and
executed against a table with it:

- `CLEAR COLUMN IF EXISTS n, ADD COLUMN IF NOT EXISTS n Nested(a UInt32),
  RENAME COLUMN n.a TO n.c` on a table with `n.c` passed validation (the
  `ADD` looked effective), `prepare` skipped the `ADD` (`n` exists) and the
  rename of the missing `n.a` aborted with LOGICAL_ERROR 'Cannot find
  column n.a in ColumnsDescription' (AST fuzzer, STID 2891-3984 and
  2891-2e07). Pre-existing on master.
- `CLEAR COLUMN q, RENAME COLUMN IF EXISTS q TO r` with an existing `r`
  aborted in the mutation; `CLEAR COLUMN q, RENAME COLUMN r TO q` replaced
  `q` by `r`; `CLEAR COLUMN p, MODIFY COLUMN IF EXISTS p.a CODEC(...)`
  skipped the subcolumn codec checks. Pre-existing on master.
- `CLEAR COLUMN q, ADD COLUMN s UInt32 AFTER q` was rejected since the
  validation snapshot follows FIRST / AFTER (master accepts it).

Now `validate` keeps a cleared column and records it, so "Cannot DROP or
CLEAR all columns" still fires when every remaining column is cleared.
Renaming a column cleared earlier in the same ALTER is rejected with
NOT_IMPLEMENTED, like renaming a modified one: its plain form was rejected
before ("Cannot find column"), and its IF EXISTS form reached a
LOGICAL_ERROR in the mutation. `DROP`, `MODIFY`, `COMMENT` and `CLEAR` of a
cleared column are now accepted without IF EXISTS, as they already were
with it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`validate` added a column to its snapshot without its default kind,
while `prepare` keeps it. An added `ALIAS` then had subcolumns in
`validate` and none in `prepare`, so `ADD COLUMN p Tuple(a UInt32) ALIAS
..., MODIFY COLUMN IF EXISTS p.a CODEC(LZ4)` failed the experimental
subcolumn codec check although it does nothing, as for an existing
`ALIAS` column. Only the kind is carried, not the expression: the
expression may refer to a column that a later command adds, which the
dependency check of a `DROP COLUMN` in between would reject. The test
now runs the statement with `enable_subcolumn_codecs = 0`, where master
rejects it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread src/Storages/MergeTree/MutateTask.cpp Resolved
A column a part stores only as a missing-column marker
(`skip_empty_columns_on_insert`) was renamed and then dropped in one
mutation (`RENAME COLUMN q TO r, DROP COLUMN r, ADD COLUMN IF NOT EXISTS
q UInt32 DEFAULT 7`): `AlterConversions` erases the `q -> r` pair at the
drop, so the marker branches found the stored name with `nameInPart`
no longer and emitted no drop. The marker `q` survived, and the re-added
`q` read `0` instead of `7` on wide, compact and packed parts, also after
`OPTIMIZE FINAL`. The unconditional `ADD COLUMN q` spelling is wrong the
same way on master.

`splitAndModifyMutationCommands` now follows the markers through the
batch's renames in order and emits each non-CLEAR marker drop under the
name the part stores, in both branches. `getColumnsForNewDataPart`
removes such a marker by that stored name instead of resolving it through
the renames of the whole mutation, which also confused a dropped marker
with another marker renamed into its name (`RENAME q TO r, DROP r, ADD r,
RENAME x TO q` failed the mutation with "Duplicate missing column 'q'",
on master too). CLEAR of a marker is unchanged.

05444 gets the wide and compact copies of the case.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread src/Storages/MergeTree/MutateTask.cpp Resolved
groeneai and others added 2 commits October 28, 2026 04:27
After the batch drops a stored column, a later command of the same mutation
with that name refers to the column added again, which the part does not
store. The per-file branch of splitAndModifyMutationCommands kept the dropped
column in part_columns, so a following DROP or CLEAR of the re-added name
emitted a second file operation against the stored column, and
collectFilesForRenames released its shared streams twice. For a Nested member
that deleted the offsets the other members still use:

    ALTER TABLE t DROP COLUMN `n.a`, ADD COLUMN `n.a` Array(UInt32), DROP COLUMN `n.a`

left `n.b` reading [] instead of [42] on a wide part with CHECK TABLE failing,
and OPTIMIZE FINAL made the loss permanent. The unconditional spelling does
this on master too; the IF EXISTS / IF NOT EXISTS spelling reaches it through
the previous commands of this PR. Compact and packed parts were not affected.

The dropped column is now removed from part_columns, so a later command with
that name takes the path of a column the part does not store, and the check of
dropped_in_batch for a RENAME in that branch is no longer needed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A column that holds only defaults is stored as a missing-column marker
(skip_empty_columns_on_insert with serialization_info_version =
'with_missing_columns'). getColumnsForNewDataPart keys its bookkeeping by
name, so the drop of a stored column also hit the marker that a later command
renames into that name, with x a marker and q stored:

    ALTER TABLE t DROP COLUMN q, RENAME COLUMN x TO q

On a wide per-file part the mutation threw LOGICAL_ERROR "column q was renamed
from x, but it doesn't exist in source columns" and never finished (also on
26.9 and 26.10). On compact and packed parts the marker of x was lost, so a
later MODIFY COLUMN q UInt32 DEFAULT 7 read 7 instead of the inserted 0. The
same for RENAME q TO r, RENAME x TO q and for
RENAME q TO r, DROP r, ADD r, RENAME x TO q.

A column renamed from a marker now leaves the list of columns the new part
stores, as for any other rename source the part does not store, and its marker
is carried over: a drop removes a marker only for a name the part does not
store. A marker dropped by a non-CLEAR command is already resolved by its
stored name (removed_markers), so this relies on the ordered marker lineage of
splitAndModifyMutationCommands.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread src/Storages/AlterCommands.cpp Resolved
groeneai and others added 2 commits October 28, 2026 05:56
In the file-level plan of a wide part, CLEAR COLUMN keeps the column in the
part, so a later DROP COLUMN or CLEAR COLUMN of the same column in the same
statement removes its files a second time. collectFilesForRenames decremented
the owner count of every shared stream on each removal, so the offsets of a
Nested group were deleted while another member still used them:
`CLEAR COLUMN n.a, DROP COLUMN n.a` (or `CLEAR COLUMN n.a, CLEAR COLUMN n.a`)
read `[]` for `n.b`, CHECK TABLE failed, and OPTIMIZE made the loss
permanent. Master reaches the same through `CLEAR COLUMN n.a, DROP COLUMN IF
EXISTS n.a` and `CLEAR COLUMN n.a, CLEAR COLUMN IF EXISTS n.a`. Compact and
packed parts were not affected.

`stream_counts` counts each stored column once, so a column now releases its
streams once per mutation however many commands remove it. The rename of a
column to a name the new part does not store had its own set for the same
purpose, which this replaces.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…previous-commands

Resolve the conflict in collectFilesForRenames with ClickHouse#127000: the
once-per-column guard of remove_column_streams stays in front of the new
remove_stream lambda that reads columns_substreams.txt.

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

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 030244b

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Build profile diff infra, not from this PR: the job ended with LogClusterNotReady: the endpoint never became ready before comparing anything #126200 (mine, open)
Stateless tests (amd_debug, sequential) / 03254_prewarm_mark_cache_rmt flaky, not from this PR (the test is not in its diff): one prewarm count reads 0 instead of 1, the runner's own rerun failed 13 of 92 times, and the same test failed on 12 other PRs since 2026-10-26 #127866 (mine, open)
Stress test (arm_asan_ubsan) / Logical error: ... getDataType() == rhs.getDataType() ... (STID: 2508-5a14) not from this PR: assertTypeEquality in SerializationDynamicElement::deserializeBinaryBulkWithMultipleStreams while a compact part reader reads a Dynamic path through Variant and Array. The same STID failed on master on 2026-10-12, and this PR does not touch serialization #116393 (external, open)
Mergeable Check, PR praktika rollups of the rows above #126200, #127866 and #116393 (the owners of the rows above)
Sync - CH Inc sync (private, not actionable by me)

No job was dropped (CH Inc sync is still pending). CIDB has no other FAIL or ERROR row on this head.

Session id: cron:our-pr-ci-monitor:20261028-153005

…previous-commands

# Conflicts:
#	src/Storages/MergeTree/MutateTask.cpp
@clickhouse-gh

clickhouse-gh Bot commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing c39e9f43c with master 0e42176d2 (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.

Object file sizes

11 object files changed (+50.72 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Storages/MergeTree/MutateTask.cpp.o 1.37 MiB 1.40 MiB +37.45 KiB (+2.68%)

711 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Slowest function optimization changes (ThinLTO)

Median per-function time ratio to the master baseline: programs/clickhouse ×0.68, programs/clickhouse-keeper ×0.63 (each binary is linked separately, on a different machine and with different build flags); deltas below are relative to it.

Binary Function Master PR Δ vs median
programs/clickhouse DB::registerFunctionConversion(DB::FunctionFactory&) 6.1 s 12.5 s +8.3 s (+200%)
Compile time of recompiled translation units

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

Job report

@clickhouse-gh clickhouse-gh Bot added comp-ddl DDL command coordination and execution (ON CLUSTER, DDL queue). and removed comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Nov 19, 2026

This branch has not been deployed

No deployments
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-ddl DDL command coordination and execution (ON CLUSTER, DDL queue). pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

3 participants