Repository navigation
Conversation
`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]>
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.
Bounce cap reached: published as-is after the final fix round (the fix of #8 was not re-gated). No residual findings. Severity: ❌ blocker / |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261026-133900 |
|
cc @Michicosun @Algunenano, could you review this and add the |
|
Workflow [PR], commit [c39e9f4] Summary: ✅
AI ReviewSummary
Final VerdictStatus: ✅ Approve. |
…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]>
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]>
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]>
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]>
CI finish ledger - 030244bEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
No job was dropped ( Session id: cron:our-pr-ci-monitor:20261028-153005 |
…previous-commands # Conflicts: # src/Storages/MergeTree/MutateTask.cpp
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes11 object files changed (+50.72 KiB total), 0 added.
711 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units1153 translation units recompiled, 8319 s compile time in total, 1153 of them have a recent master baseline. |
Closes: #127444
Closes: #117824
Related: #127497
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Each command of an
ALTER TABLEnow resolves its column, includingIF EXISTSandIF NOT EXISTS, against the columns the previous commands of the same statement added, dropped or renamed, soDROP COLUMN b, ADD COLUMN IF NOT EXISTS b Stringno longer losesb. A column that oneALTERrenames 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 Stringlosesb, and on a wide partRENAME COLUMN q TO r, DROP COLUMN r, ADD COLUMN r UInt32 DEFAULT 7returns the dropped42instead of7.This PR carries the five commits of #127497 by @ vitlibar (authorship kept):
AlterCommands::preparedecides IF EXISTS / IF NOT EXISTS against the columns the earlier commands leave, andDROP COLUMN ndrops a Nested group member by member. On top of them:splitAndModifyMutationCommandsnames 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.aandCLEAR n.a, DROP IF EXISTS n.ano longer emptyn.b, both broken on master too). TheNOT_IMPLEMENTEDrejection of [WIP] ALTER command sees previous commands #127497 is removed.skip_empty_columns_on_insert) follow the same renames and drops;DROP q, RENAME x TO qwith a markerxno longer leaves a never-finishing mutation (also on 26.9 and 26.10).AlterConversionsno longer treatsDROP qafterRENAME q TO r, ADD qas a drop ofr.preparebuilds its snapshot with the codeapplyuses;validatefollows theALIASkind and keeps a cleared column. Renaming a column the statement cleared is rejected.Unchanged, as on master:
RENAMEthenCLEAR;MODIFY ... REMOVE <property>sees the properties from before the statement (apart from the default kind); a column re-added under a name aRENAMEfreed 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_commandsand05452_alter_rename_drop_readd_replicatedfail on master and pass with the fix in 50 randomized runs.Workflow [PR]
Sync PR [sync-upstream/pr/127897]