Repository navigation
Fix reading subcolumns of an ALIAS column after ALTER TABLE ... COMMENT COLUMN - #121449
PedroTadim merged 2 commits into
Conversation
…fied
On 26.8.2.7 and 26.3.33.10, SELECT arr.size0 FROM t fails with
NO_SUCH_COLUMN_IN_TABLE after ALTER TABLE t COMMENT COLUMN arr 'c', although
it returned 2 immediately before that statement:
CREATE TABLE t (id UInt64, arr Array(UInt64) ALIAS [id, id])
ENGINE = MergeTree ORDER BY id;
INSERT INTO t VALUES (7);
SELECT arr.size0 FROM t; -- 2
ALTER TABLE t COMMENT COLUMN arr 'c';
SELECT arr.size0 FROM t; -- Code: 16 NO_SUCH_COLUMN_IN_TABLE
ColumnsDescription::add() deliberately registers no static subcolumns for an
ALIAS column (ColumnsDescription.cpp:455-458), because an alias has none of its
own: they are derived from its expression. ColumnsDescription::modify() did it
unconditionally, so every ALTER that reaches it on an ALIAS column inserted
arr.size0 into the subcolumns index with an Alias parent. The chain from there:
1. get(All + withSubcolumns) emits arr.size0, since addSubcolumnsToList skips
subcolumns of Ephemeral parents only, so it enters the analyzer's
column_names_and_types.
2. getDefault("arr.size0") consults columns only, finds nothing, and the
node-map populator emplaces a plain expression-less ColumnNode
(QueryAnalyzer.cpp:5026-5031).
3. Identifier resolution therefore hits node_map.find and skips the
alias-expression rewrite wrapExpressionNodeInSubcolumn
(IdentifierResolver.cpp:768-776) that would have produced
getSubcolumn([id, id], 'size0').
4. With no expression the planner hands "arr.size0" to storage->read, and the
MergeTree read re-asks with AllPhysical, where
defaultKindToGetKind(Alias) == Aliases(4) and Aliases & AllPhysical(3) == 0
(ColumnsDescription.cpp:876-878): the entry is found, discarded, and the
lookup throws NO_SUCH_COLUMN_IN_TABLE.
The asymmetry dates to dcd7312 (2022-02-09), so every supported release
carries it. The subcolumns container is derived in-memory state (toString()
iterates columns only), so nothing is persisted, a DETACH/ATTACH heals the
table, and the ALTER and the failing SELECT must run in the same process for
the failure to be visible.
ALTER TABLE ... RENAME COLUMN is a second trigger: AlterCommand::apply
(AlterCommands.cpp:1276) re-applies modify() to every column of the table, so
renaming any column re-registers the subcolumns of every ALIAS column. Measured
as Code: 16 before this change and 2 after it.
The kind is read AFTER the mutation so that the guard describes the resulting
column: MODIFY COLUMN arr REMOVE ALIAS and MODIFY COLUMN arr <T> DEFAULT <expr>
turn an ALIAS into a physical column and must then register subcolumns, while
the opposite direction must stop registering them (the removeSubcolumns call
already handles the removal).
The explicit invalidateGetCache() keeps modify()'s cache invalidation
unconditional. Today the only guaranteed invalidation on the alias path is a
side effect of addSubcolumns (ColumnsDescription.cpp:1139, which invalidates
whether or not the type yields any subcolumn), while removeSubcolumns
invalidates only when it erased something and modifyColumnOrder only when
first/after_column is set. Skipping addSubcolumns for an alias without this call
would have silently removed that guarantee. Placing it before the new branch
makes the behavioural delta of this change exactly "an ALIAS column is no longer
registered". No staleness is observable today, so this preserves the existing
invalidation rather than fixing an observed bug.
Alternatives rejected:
- Moving the guard into addSubcolumns() is not implementable: add() calls it
before inserting the column into columns, so the callee cannot look the kind
up. The guard has to live at the call site.
- A comment-only ColumnsDescription::setComment was rejected on ClickHouse#119469, and
it would fix only COMMENT COLUMN, leaving MODIFY COLUMN and RENAME COLUMN
broken.
- Fixing AlterCommand::apply is the wrong layer: ColumnsDescription owns the
subcolumn index, and there are seven modify() call sites.
- Teaching the read path to accept an ALIAS parent under AllPhysical would
make a non-materialised column look physically readable, and the alias
expression is already lost by then, so the read would have no data to
compute from. The defect is the registration, not the lookup.
- Widening addSubcolumnsToList to skip Alias parents papers over one consumer
of a corrupt index and leaves tryGetColumn, hasSubcolumn and
InterpreterDescribeQuery still answering for a phantom subcolumn.
No setting, no serialization and no stored metadata change, so no
SettingsChangesHistory.cpp entry is owed and a mixed-version cluster sees
identical metadata.
Closes: ClickHouse#121421
Related: ClickHouse#119469
Co-Authored-By: Claude Opus 5 <[email protected]>
Test-only follow-up to the previous commit; src/ is unchanged. Three additions to 05235_alter_comment_column_alias_subcolumn. 1. ALTER TABLE ... RENAME COLUMN. RENAME_COLUMN is its own branch of AlterCommands::apply (AlterCommands.cpp:1268-1283), calling columns.rename() and then modify() over every column of the table, so renaming any column re-registers the subcolumns of every ALIAS column. That trigger was measured but not covered by the suite. Renaming the unrelated physical column and reading arr.size0 next to it returns "2 3" with the fix and Code: 16 NO_SUCH_COLUMN_IN_TABLE without it, measured on a fresh table so that the arm fails on its own rather than after an earlier arm. 2. The REMOVE ALIAS arm reads two rows now. Its single asserted value was 0, which is the Array(UInt64) default for a part written before the column became physical, so a read that silently answered the type default would have passed it as well. Inserting a row after the ALTER makes the expected output 0 then 4. 3. ALTER TABLE ... MODIFY COLUMN <physical> Array(UInt64) ALIAS <expr>, the direction in which a column stops being physical. This is what pins the guard on the kind AFTER the mutation: reading the kind before the mutation still registers subcolumns for a column that has just become an alias, and the read then fails with Code: 16, while reading it after the mutation answers 2. The opposite direction cannot distinguish the two, because MODIFY COLUMN ... REMOVE ALIAS re-enters modify() with the column already physical, and the entry is registered either way. The reference is regenerated from the patched build's own output. Co-Authored-By: Claude Opus 5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review in two independent parts: my own cold read of the resulting code, and a cold
Validation behind the change rather than the review: the new test fails on unpatched master and One negative is reported as a negative: dropping the explicit |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20260921-212200 |
|
Workflow [PR], commit [9456cdd] Summary: ✅
AI ReviewSummaryThis PR fixes the Final VerdictStatus: ✅ Approve LLVM Coverage ReportMeasured on commit 9456cdd.
Changed lines: Changed C/C++ lines covered: 5/5 (100.00%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units1100 translation units recompiled, 7357 s compile time in total, 1100 of them have a recent master baseline. |
0f47f73
The branch was 14 days behind master and GitHub reported it as conflicting. One conflict, in the `ColumnsDescription::modify` template: master's ClickHouse#121449 added an `invalidateGetCache()` and a guard that skips `addSubcolumns` for ALIAS columns, while this branch threads a `CancellationBudget *` into the same call. Both are kept - the guard and the cache invalidation from master, with the budget passed to the `addSubcolumns` call that survives the guard. Everything else merged without conflicts. The per-table cancellation check in `StorageMerge::getColumnsDescriptionFromSourceTablesImpl` still sits between the SHOW_TABLES visibility filter and the SHOW_COLUMNS check, so the property from the previous round holds: a table the user may not see behaves alike whether or not it exists. Master's new StorageAlias target-privilege check lands after it, unchanged. Master has no cancellation checkpoint in that function and its `forEachSubcolumn` still takes no budget, so nothing in this branch has been superseded.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
ALTER TABLE ... COMMENT COLUMNandALTER TABLE ... RENAME COLUMNmaking reads of anALIAScolumn's subcolumns (for examplearr.size0) fail withNO_SUCH_COLUMN_IN_TABLEuntil the table was reloaded. Closes #121421.Description
Closes #121421. Related: #119469, where @ PedroTadim named this asymmetry and @ evillique asked for it as its own PR with an issue.
On 26.8.2.7 and 26.3.33.10,
SELECT arr.size0 FROM tfails withNO_SUCH_COLUMN_IN_TABLEafterALTER TABLE t COMMENT COLUMN arr 'c', though it returned2immediately before:ColumnsDescription::add()registers no static subcolumns for anALIAScolumn, since an alias's subcolumns are derived from its expression.modify()registered them unconditionally, so anyALTERreaching it putarr.size0into the subcolumn index with anAliasparent. The read then resolves that entry as a plain physical column, and the MergeTree lookup, which matches it againstAllPhysical, discards it and throws. The index is in-memory derived state, so a reload heals the table, and theALTERand theSELECTmust share one process to see the bug.The fix gives
modify()the guardadd()already has, reading the kind after the mutation, so a column that stops being an alias stays registered and one that becomes an alias does not. The addedinvalidateGetCache()keepsmodify()'s cache invalidation unconditional instead of leaving it a side effect ofaddSubcolumns; it preserves today's behaviour rather than fixing an observed staleness.RENAME COLUMNof any column is a second trigger, since it re-appliesmodify()to every column of the table. Fixed as well.Validation: the new test fails on master and passes with the fix; 50 randomized runs green; the alias-subcolumn and comment-column suites stay green, including
04738_merge_subcolumn_of_alias_parent, which asserts the opposite behaviour through aMergetable.cc @evillique
Workflow [PR]
Sync PR [sync-upstream/pr/121449]
Version info
26.10.1.601-master(included in26.10and later)