Repository navigation
Conversation
|
Workflow [PR], commit [616bcf5] Summary: ❌
AI ReviewSummaryPR #127497, reviewed at PR Metadata💡 The required changelog entry, Findings❌ Blockers
Tests
Final Verdict❌ Block. Required changes: prevent deleted data from surviving rename/drop/re-add, scope the mutation-specific rejection to affected engines, and add the focused regressions above. |
`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
f68871a to
f57deb8
Compare
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.
| SELECT '======= ADD COLUMN THEN ADD ENUM VALUES IN ONE STATEMENT ==============='; | ||
| CREATE OR REPLACE TABLE enum_add_then_modify (y Int8) ENGINE = Memory; | ||
| ALTER TABLE enum_add_then_modify ADD COLUMN x Enum8('a' = 1), MODIFY COLUMN x ADD ENUM VALUES('b' = 2); -- { serverError NO_SUCH_COLUMN_IN_TABLE } | ||
| ALTER TABLE enum_add_then_modify ADD COLUMN x Enum8('a' = 1), MODIFY COLUMN x ADD ENUM VALUES('b' = 2); |
There was a problem hiding this comment.
Now the ALTER statement sees the changes made by itself, so ALTER TABLE enum_add_then_modify ADD COLUMN x Enum8('a' = 1), MODIFY COLUMN x ADD ENUM VALUES('b' = 2) gives a enum with two values a and b
| CREATE TABLE default.add_nested_exact_sno0\n(\n `key` UInt64,\n `n` String\n)\nENGINE = MergeTree\nORDER BY key\nSETTINGS share_nested_offsets = 0, index_granularity = 8192 | ||
| CREATE TABLE default.add_nested_rename_child\n(\n `n.a` Array(UInt32),\n `m` Array(String)\n)\nENGINE = MergeTree\nORDER BY tuple()\nSETTINGS share_nested_offsets = 0, index_granularity = 8192 | ||
| CREATE TABLE default.add_nested_cond_rename\n(\n `n.a` Array(UInt32),\n `n.b` Array(String)\n)\nENGINE = MergeTree\nORDER BY tuple()\nSETTINGS share_nested_offsets = 0, index_granularity = 8192 | ||
| CREATE TABLE default.add_nested_cond_rename\n(\n `n.a` Array(UInt32),\n `m` Array(String)\n)\nENGINE = MergeTree\nORDER BY tuple()\nSETTINGS share_nested_offsets = 0, index_granularity = 8192 |
There was a problem hiding this comment.
Since the ALTER statement now sees the changes made by itself, the last rename n.b to m works now:
CREATE TABLE add_nested_cond_rename
(
`n.a` Array(UInt32)
)
ENGINE = MergeTree()
ORDER BY tuple()
SETTINGS share_nested_offsets = 0;
ALTER TABLE add_nested_cond_rename ADD COLUMN IF NOT EXISTS n Nested(a UInt32, b String), RENAME COLUMN IF EXISTS `n.b` TO `m`;
`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.
Build profile diff (arm_release)Commit See the job log for details. |
`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.
6ff8b2e to
0fd5386
Compare
`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.
| auto member_ast = make_intrusive<ASTAlterCommand>(); | ||
| member_ast->type = ASTAlterCommand::DROP_COLUMN; | ||
| member_ast->if_exists = if_exists; | ||
| member_ast->set(member_ast->column, make_intrusive<ASTIdentifier>(nested_column.name)); |
There was a problem hiding this comment.
[P1] Handle drop/re-add after an earlier rename
Expanding the group into its current member names still loses the identity of a member renamed earlier in this statement. Reproduced with the fast-test binary for 616bcf54:
CREATE TABLE t (id UInt64, n Nested(a UInt32, b UInt32))
ENGINE = MergeTree ORDER BY id
SETTINGS min_rows_for_wide_part = 0, min_bytes_for_wide_part = 0;
INSERT INTO t VALUES (1, [11], [42]);
ALTER TABLE t
RENAME COLUMN `n.a` TO `n.c`,
DROP COLUMN n,
ADD COLUMN IF NOT EXISTS `n.c` Array(UInt32)
SETTINGS mutations_sync = 1;
SELECT id, `n.c` FROM t;This returns (1, [11]), although the replacement column should be empty. The generated mutations are RENAME COLUMN n.a TO n.c, DROP COLUMN n.c, and DROP COLUMN n.b; all report is_done = 1, and OPTIMIZE TABLE t FINAL preserves the old value. Compact parts and splitting the operations into separate statements return (1, []).
The scalar form also retains deleted data: starting with q = 42, RENAME COLUMN q TO r, DROP COLUMN IF EXISTS r, ADD COLUMN IF NOT EXISTS r UInt32 DEFAULT 7 returns 42 instead of 7 in a wide part. The new validation guard catches only a rename after a drop, so this opposite order remains accepted.
Preserve the source-part identity through preceding renames when executing these drops, or reject the unsafe combination on affected engines before committing metadata. Please cover this order with a stateless test that reads existing wide-part data after mutation completion.
There was a problem hiding this comment.
@groeneai There are always some gaps, can you make your own PR, move my work from this PR there and then try to correctly fix all the gaps?
There was a problem hiding this comment.
Acknowledged. I'll open my own PR from master carrying your commits (authorship kept), work through the remaining gaps, including the rename-then-drop/re-add case above and the Memory rename guard, and post the link here. It also covers the DROP b, ADD COLUMN IF NOT EXISTS b case of my open #117593, which I'll reconcile with the new PR.
There was a problem hiding this comment.
I opened #127897 with your five commits (authorship kept) and these fixes on top: the mutation names the columns a part stores, so the rename, drop and re-add cases from this thread read the new column (7, []) instead of the dropped data; the NOT_IMPLEMENTED rename guard is removed (it also hit ENGINE = Memory); prepare builds an added column with the code apply uses, so an ALIAS keeps its kind; and the empty-part check follows the renames of the ALTER. Not covered: CLEAR COLUMN combined with RENAME COLUMN, which the open #118500 and #117825 change.
| /// The column exists here only because it was added again after the drop. The mutation of the ALTER would | ||
| /// rename the data of the dropped column instead of removing it, because it finds the column in the parts | ||
| /// by its name. | ||
| if (dropped_columns.contains(command.column_name)) |
There was a problem hiding this comment.
[P2] Restrict the mutation-specific rename guard to affected engines
This validation runs for every storage, including Memory, whose schema alteration applies metadata directly and does not use the wide-part mutation path that motivates this restriction:
CREATE TABLE t (id UInt64, q UInt32) ENGINE = Memory;
ALTER TABLE t DROP COLUMN q, ADD COLUMN q UInt32, RENAME COLUMN q TO r;The preceding PR binary (0fd5386d4) accepts this and produces id UInt64, r UInt32. The current head throws NOT_IMPLEMENTED at this guard, even though the table is empty and there is no stored part to corrupt. StorageMemory::checkAlterIsPossible explicitly supports all three operations, and StorageMemory::alter applies them through AlterCommands::apply without mutation replay.
Keep this restriction in the affected engine's eligibility check, or condition it on the relevant mutation behavior, so existing Memory schema migrations remain supported. Add a small stateless regression for the sequence above.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
ALTER command sees previous commands
Inside a single
ALTERwith several commands, every command should see the table as the previous commands of the same statement left it, not as it was before the statement.Example. Take a table with a column
band run oneALTERthat drops it and adds it back with a new type:Before this branch, the
IF NOT EXISTScheck looked at the table as it was before theALTERstarted. Therebstill existed, so theADD COLUMN IF NOT EXISTSwas marked as "nothing to do". Then the commands ran in order:bwas dropped, theADD COLUMN IF NOT EXISTSwas skipped, and the column was silently gone.Closes #127444, also fixes scenarios #123966 (comment) and #123966 (comment).
Closes #117824
Workflow [PR]
Sync PR [sync-upstream/pr/127497]