Repository navigation
Description
Describe what's wrong
- One ALTER that adds a column named like an existing subcolumn and then modifies it fails with
LOGICAL_ERROR. - Trigger: table with
p Tuple(a UInt8, b UInt8), then one ALTER doingADD COLUMNandMODIFY COLUMNonp.b. - Expected the new
p.bcolumn modified toMap(String, UInt16); actual is aLOGICAL_ERRORthat aborts debug and sanitizer builds. - Not gated by
enable_subcolumn_codecs; introduced by Different codecs for elements of Tuple (simplified version) #123966, which is only on master, and splitting into two ALTERs works around it.
Root cause: AlterCommands::prepare (AlterCommands.cpp:2026-2031) rewrites MODIFY COLUMN on p.b to column p, subcolumn b using a snapshot that ignores earlier ADD COLUMN commands of the same ALTER. AlterCommand::apply then trusts the stale subcolumn_name and throws at AlterCommands.cpp:899.
Analysis details (evidence, affected locations, impact)
Why we believe this is a bug: InterpreterAlterQuery::executeToTable -> AlterCommands::validate sees the column added by the preceding ADD COLUMN and validates MODIFY COLUMN p.b`` as a column command. AlterCommands::prepare (`AlterCommands.cpp:2026-2031`) resolves the name against the pre-ALTER snapshot, which has no `p.b` column, so it rewrites the command to column `p`, subcolumn `b`. `MergeTreeData::checkAlterIsPossible` -> `checkAlterEligibility` -> `AlterCommands::apply` -> `AlterCommand::apply` takes the `target_subcolumn` branch and throws at `AlterCommands.cpp:899`.
Affected locations:
src/Storages/AlterCommands.cpp:2030— prepare rewrites MODIFY_COLUMN to parent column + subcolumn using the pre-ALTER snapshotsrc/Storages/AlterCommands.cpp:899— AlterCommand::apply throws LOGICAL_ERROR for a non-codec command on the stale subcolumn targetsrc/Storages/AlterCommands.cpp:878— apply-time resolution is skipped because prepare already set subcolumn_name
Impact: A valid ALTER fails with LOGICAL_ERROR (aborts debug/sanitizer builds and logs via ForcedCriticalErrorsLogger); the codec variant is rejected. Not gated by enable_subcolumn_codecs, so it reaches every table with a Tuple/Array/Nullable/Map column. Workaround: split into two ALTERs.
Does it reproduce on most recent release?
No — only master (4feb890db6ce). The bug starts with #123966 (merged as df03aff190d3), which is in no release yet, so a release build (and ClickHouse Fiddle) still passes.
How to reproduce
The same happens with arr.size0 next to arr Array(UInt8) and with MODIFY COLUMN ... COMMENT.
-- Test: one ALTER adds a column named like an existing subcolumn and modifies the new column.
DROP TABLE IF EXISTS t_add_modify_subcolumn_name;
CREATE TABLE t_add_modify_subcolumn_name (id UInt64, p Tuple(a UInt8, b UInt8), arr Array(UInt8)) ENGINE = MergeTree ORDER BY id;
ALTER TABLE t_add_modify_subcolumn_name ADD COLUMN `p.b` Map(String, UInt8), MODIFY COLUMN `p.b` Map(String, UInt16);
ALTER TABLE t_add_modify_subcolumn_name ADD COLUMN `arr.size0` UInt64, MODIFY COLUMN `arr.size0` UInt32 COMMENT 'c';
SELECT name, type, comment FROM system.columns
WHERE database = currentDatabase() AND table = 't_add_modify_subcolumn_name' AND name IN ('p.b', 'arr.size0')
ORDER BY position;
DROP TABLE t_add_modify_subcolumn_name;Verified: this block reproduces on current master (4feb890db6ce) when pasted as is.
Expected behavior
The ALTER succeeds and modifies the newly added p.b (or arr.size0) column.
Expected output of the reproducer above:
p.b Map(String, UInt16)
arr.size0 UInt32 c
Error message and/or stacktrace
The ALTER fails with LOGICAL_ERROR saying only CODEC can be modified for subcolumn b of column p, and the CODEC variant is rejected with BAD_ARGUMENTS.
Actual output of the reproducer above on master (4feb890db6ce):
Received exception from server (version 26.11.1):
Code: 49. DB::Exception: Received from 127.0.0.1:19030. DB::Exception: Only CODEC can be modified or removed for subcolumn `b` of column `p`. (LOGICAL_ERROR)
(query: ALTER TABLE t_add_modify_subcolumn_name ADD COLUMN `p.b` Map(String, UInt8), MODIFY COLUMN `p.b` Map(String, UInt16);)
(second ALTER: ... subcolumn `size0` of column `arr`. (LOGICAL_ER
Suggested fix
In prepare, skip the subcolumn rewrite when an earlier ADD_COLUMN of the same ALTER adds command.column_name (track added names), or drop the prepare-time rewrite and rely on the resolution apply already does against the columns so far. Trade-off: anything reading command.column_name between prepare and apply (e.g. key-column checks in checkAlterIsPossible) would then see the dotted name.
Additional context
Same pattern as #122321.
Introduced by PR #123966, found during its automated review: the reproducer passes on the master build just before it merged and fails on the build with it. Severity P1 · Finding h_pr123966_001
Activity
Prepare each ALTER command against the columns left by the previous ones