Repository navigation
Fix ALTER atomicity when combining UPDATE/DELETE with RENAME COLUMN - #96022
Conversation
Fixes #70678 When an ALTER statement combined UPDATE or DELETE with RENAME COLUMN on the same column, the RENAME would succeed even if the mutation validation failed, leaving the table in an inconsistent state. For example: ```sql CREATE TABLE t0 (c0 Int) ENGINE = Memory(); ALTER TABLE t0 UPDATE c0 = 1 WHERE TRUE, RENAME COLUMN c0 TO c1; -- Error occurred, but column was renamed anyway! INSERT INTO TABLE t0 (c0) VALUES (1); -- Fails: No such column c0 ``` Root cause: In `InterpreterAlterQuery::executeToTable`, alter commands (including RENAME) are executed BEFORE mutation commands (UPDATE/DELETE) are validated. If mutation validation fails after the rename is committed, the table schema is already changed. The fix adds early validation that detects conflicts between RENAME COLUMN and UPDATE/DELETE operations before any changes are made: 1. If an UPDATE references a column that is being renamed, reject early 2. If a DELETE/UPDATE predicate references a column being renamed, reject early 3. Operations on different columns continue to work normally Error messages are clear and actionable: - "Cannot UPDATE column `c0` and RENAME it in the same ALTER query. Please split into separate ALTER statements." - "Cannot use column `c0` in DELETE predicate and RENAME it in the same ALTER query. Please split into separate ALTER statements." Co-Authored-By: Claude Opus 4.5 <[email protected]>
|
Workflow [PR], commit [028c7be] Summary: ❌
|
| ALTER TABLE test_alter_atomic UPDATE c0 = 100 WHERE true, RENAME COLUMN c1 TO c2; | ||
|
|
||
| -- Wait for mutation to complete | ||
| SELECT sleepEachRow(0.1) FROM numbers(30) SETTINGS function_sleep_max_microseconds_per_block = 10000000 FORMAT Null; |
There was a problem hiding this comment.
I don't understand this. Why is it using sleep to wait for a mutation instead of mutations_sync?
| /// If a column is both renamed and used in UPDATE/DELETE in the same ALTER, we must fail early | ||
| /// to ensure atomicity - otherwise RENAME would succeed but UPDATE/DELETE would fail, | ||
| /// leaving the table in an unexpected state. See issue #70678. | ||
| if (!alter_commands.empty() && mutation_commands.hasNonEmptyMutationCommands()) |
There was a problem hiding this comment.
The fix is hacky and it is not fixing all possible combinations, for example this is stil an issue https://fiddle.clickhouse.com/86d0c5df-932f-4a65-acbd-c08e57e80cda
I would suggest forbidding the combination of the rename column command with anything else; anyway, we have barrier logic that will prevent it from merging with other commands during mutation execution.
|
Hi @alexey-milovidov — while reviewing this PR I found the following:
Happy to discuss — close anything that's wrong or already addressed. |
Fixes #70678
When an ALTER statement combined UPDATE or DELETE with RENAME COLUMN on the same column, the RENAME would succeed even if the mutation validation failed, leaving the table in an inconsistent state.
For example:
Root cause: In
InterpreterAlterQuery::executeToTable, alter commands (including RENAME) are executed BEFORE mutation commands (UPDATE/DELETE) are validated. If mutation validation fails after the rename is committed, the table schema is already changed.The fix adds early validation that detects conflicts between RENAME COLUMN and UPDATE/DELETE operations before any changes are made:
Error messages are clear and actionable:
c0and RENAME it in the same ALTER query. Please split into separate ALTER statements."c0in DELETE predicate and RENAME it in the same ALTER query. Please split into separate ALTER statements."Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
In previous versions, when combining conflicting ALTER expressions with UPDATE and RENAME COLUMN, a logical error was thrown instead of a proper exception. Closes #70678.
Version info
26.2.1.336