RENAME COLUMN after MODIFY QUERY on MergeTree raises LOGICAL_ERROR
Describe what's wrong
- Combined ALTER
(MODIFY QUERY ...), (RENAME COLUMN a TO b)on a MergeTree table crashes debug/sanitizer builds and throws LOGICAL_ERROR on release. - Trigger: run MODIFY QUERY together with RENAME COLUMN in one ALTER on
MergeTree,ReplicatedMergeTree, orReplacingMergeTree. - Expected NOT_IMPLEMENTED or NOT_FOUND_COLUMN_IN_BLOCK, but the server throws
Cannot find column a in ColumnsDescription (LOGICAL_ERROR). - Any user with ALTER privilege on such a table can trigger the crash, affects 26.3, 26.7, 26.8, 26.9.
Root cause: MergeTreeData::checkAlterEligibility applies all commands including MODIFY_QUERY via commands.apply/getMutationCommands before its later loop rejects MODIFY_QUERY at MergeTreeData.cpp:5970, so RENAME_COLUMN in ColumnsDescription::rename hits a column already dropped by MODIFY_QUERY and throws LOGICAL_ERROR instead of being blocked upfront.
Why we believe this is a bug: MergeTreeData::checkAlterIsPossible (MergeTreeData.cpp:5363) calls checkAlterEligibility, which calls commands.apply (MergeTreeData.cpp:5626, or getMutationCommands at :5378 when allow_non_metadata_alters = 0). There the MODIFY_QUERY branch replaces metadata.columns (AlterCommands.cpp:1275), then RENAME_COLUMN calls ColumnsDescription::rename (AlterCommands.cpp:1327), which throws LOGICAL_ERROR (ColumnsDescription.cpp:490). The MODIFY_QUERY rejection only runs later, at MergeTreeData.cpp:5970.
Affected locations:
src/Storages/AlterCommands.cpp:1327—RENAME_COLUMNguards onlyIF EXISTSbeforeColumnsDescription::renamesrc/Storages/MergeTree/MergeTreeData.cpp:5626—commands.applyrunsMODIFY QUERYbefore it is rejectedsrc/Storages/MergeTree/MergeTreeData.cpp:5378— same viagetMutationCommandswhenallow_non_metadata_alters = 0src/Storages/MergeTree/MergeTreeData.cpp:5970—MODIFY_QUERYrejected only after applysrc/Storages/ColumnsDescription.cpp:490—LOGICAL_ERRORon missing column
Impact: Any user with ALTER on a MergeTree-family table can trigger a LOGICAL_ERROR. Debug and sanitizer builds abort. Release builds throw an internal-error exception; the statement is rejected and metadata is unchanged. Exercised on MergeTree, ReplicatedMergeTree and ReplacingMergeTree. The 26.3 and 26.9 branches have the same apply-before-reject order and the same RENAME guard; 26.7 and 26.8 have the same AlterCommands/MergeTreeData code (grep-confirmed).
Does it reproduce on most recent release?
Yes — confirmed on current master (commit fb2810634cfd38).
How to reproduce
Reproduce with CREATE TABLE alter_02834 (a UInt64) ENGINE=MergeTree ORDER BY a; then ALTER TABLE alter_02834 (MODIFY QUERY SELECT 1 AS x), (RENAME COLUMN a TO b);.
DROP TABLE IF EXISTS alter_02834;
CREATE TABLE alter_02834 (a UInt64) ENGINE=MergeTree() ORDER BY a;
ALTER TABLE alter_02834 MODIFY QUERY SELECT a FROM alter_02834; -- { serverError NOT_IMPLEMENTED }
-- Test: RENAME COLUMN after a MODIFY QUERY that drops the column must fail with a user error
ALTER TABLE alter_02834 (MODIFY QUERY SELECT 1 AS x), (RENAME COLUMN a TO b); -- { serverError NOT_IMPLEMENTED, NOT_FOUND_COLUMN_IN_BLOCK }
DROP TABLE alter_02834;Verified: this block reproduces on current master (fb2810634cfd) when pasted as is.
Expected behavior
The second ALTER should fail with NOT_IMPLEMENTED (or NOT_FOUND_COLUMN_IN_BLOCK) and leave metadata unchanged.
Expected output of the reproducer above:
[ OK ]: the second ALTER fails with `NOT_IMPLEMENTED` (ALTER MODIFY QUERY is not supported by MergeTree engines family) or `NOT_FOUND_COLUMN_IN_BLOCK`; the reference file stays empty.Error message and/or stacktrace
The ALTER applies MODIFY_QUERY first, then RENAME_COLUMN fails with Cannot find column a in ColumnsDescription (LOGICAL_ERROR), aborting debug/sanitizer builds.
Actual output of the reproducer above on master (fb2810634cfd38):
[1 / 1] 05257_alter_modify_query_rename_mergetree: [ FAIL ] 0.19 sec.
Reason: return code: 49
Expected server error code: [48, 10] but got: 49 (query: -- Test: RENAME COLUMN after a MODIFY QUERY that drops the column must fail with a user error
Code: 49. DB::Exception: Received from 127.0.0.1:19020. DB::Exception: Cannot find column a in ColumnsDescription. Maybe you Reject MODIFY_QUERY (and MODIFY_REFRESH) in MergeTreeData::checkAlterEligibility before the first apply, i.e. ahead of getMutationCommands at :5378 and commands.apply at :5626. MergeTree then reports the documented NOT_IMPLEMENTED for every MODIFY QUERY statement, instead of the current UNKNOWN_IDENTIFIER/NOT_FOUND_COLUMN_IN_BLOCK answers. Optionally give the RENAME_COLUMN branch a skip_absent_column_or_fail-style guard as defence in depth.
Follow-up to #100328 (closed as fixed by #100387; this reproducer still fails on master after that merge).
Open risks:
- Open follow-up #122213 is built on the premise that only materialized views admit
MODIFY QUERY. ItsMergeTreebehaviour should be re-checked against the apply-before-reject path.
Found during automated review of PR #121469; the bug predates that PR (it reproduces on the master build just before it merged), so the introducing change is not identified yet. Severity P2 · Finding h_pr121469_001
Source: ClickHouse/ClickHouse