Repository navigation
Conversation
…introduces
ALTER TABLE mv (MODIFY QUERY SELECT id, id + 1 AS id2 FROM src), (COMMENT COLUMN
id2 'c') on a materialized view was rejected with
Code: 10. Wrong column name. Cannot find column `id2` to comment.
Maybe you meant: ['id']. (NOT_FOUND_COLUMN_IN_BLOCK)
although writing the two commands as two separate ALTER statements makes both
succeed, so the requested end state was already reachable. The hint was wrong too:
it offered the pre-alter column, while id2 is exactly what the view has after the
MODIFY QUERY. Written with IF EXISTS the statement instead returned success and
applied no comment at all.
A materialized view's columns are its query's output, so MODIFY QUERY replaces the
whole set. AlterCommands::validate() advances its working snapshot for ADD, DROP and
RENAME COLUMN but has no MODIFY_QUERY case at all, and AlterCommands::prepare() keeps
a second copy of the pre-alter columns with the same gap. Because validate() runs
first (InterpreterAlterQuery.cpp:373 validate, :379 prepare, :380
checkAlterIsPossible, :381 alter), that stale screen pre-empts the sequential model in
AlterCommand::apply that would have been right, and prepare()'s stale copy marks an
IF EXISTS comment command ignore, so apply() skips it outright.
validate() cannot be made right here. Deriving a view's new columns means analysing
the query, which reads the SOURCE tables' metadata under their own short-lived locks
(Analyzer/TableNode.cpp:91-93) while the ALTER holds locks only on the view itself
(InterpreterAlterQuery.cpp:362-364), so two analyses can see two different source
schemas; and validate() is documented as the opposite kind of work, "This check is
lightweight and base only on metadata" (AlterCommands.h:246). Its one existing
analyzer use runs over a StorageDummy built from the column list, never over another
table.
So both pre-apply screens now stop screening column names once a MODIFY QUERY earlier
in the same statement has replaced the set, and AlterCommand::apply, which walks the
commands in order against an accumulating StorageInMemoryMetadata, owns the check. It
needs no second analysis and no context reconstruction, and its error draws hints from
the columns the new query really produces: for a name close enough to one of them the
message now offers the new query's column where the pre-alter snapshot could offer
nothing. The apply-side user error this relies on arrived in 711f560; before it
an absent column reached ColumnsDescription::modify and threw LOGICAL_ERROR, so the
two changes belong together, and the new test's negative control asserts
NOT_FOUND_COLUMN_IN_BLOCK to keep it that way.
The latch is on MODIFY QUERY alone. ADD COLUMN followed by COMMENT COLUMN IF EXISTS
on a plain table keeps its existing behaviour, which is a separate compatibility
question, and ADD ENUM VALUES keeps its explicit throw because merging enum values
needs the column present. The DROP and RENAME COLUMN throws are untouched:
StorageMaterializedView::checkAlterIsPossible never admits those commands, and it
rejects them before apply() either way, measured.
Non-materialized-view storages are unaffected. MergeTreeData::checkAlterIsPossible
applies the commands itself (MergeTreeData.cpp:5626) before rejecting MODIFY QUERY by
type, and the apply-side check reuses validate()'s wording, so such a statement
reports the identical error as before.
Measured on a debug build: the new test reddens on 711f560 and passes with this
change; 250 randomized runs of it and of 05237, 04046, 02931 and 03022 pass; and
reverting either half of the change alone leaves the other half's spellings broken,
so both screens needed fixing.
Co-Authored-By: Claude Opus 5 <[email protected]>
Cut the design narration and the refuted alternative from the validate() latch comment, keeping the column-set property and the pointer to where the check now runs.
Internal second-model review (two independent passes; 1 blocker refuted, 2 majors actioned)Round 1 sent two wording corrections back for a fix; the code was not changed. A second cold review and a second gate pass over the corrected tree returned no findings.
Also checked and found clean: the latch's placement in both |
|
Workflow [PR], commit [dca2656] Summary: ❌
AI ReviewSummaryThis PR moves the column-existence check for Missing context / blind spots
Final Verdict✅ No actionable findings on the current PR head. LLVM Coverage ReportMeasured on commit dca2656.
Changed lines: Changed C/C++ lines covered: 34/35 (97.14%) · Uncovered code |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-6:20260925-090600 |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 16 s compile time in total, 7 of them have a recent master baseline. |
CI finish ledger - dca2656Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task whose
All 176 check runs completed (131 pass, 41 not selected, 4 red) with the last completion at 15:38:22Z, No master merge is pushed for this, and that is measured rather than a preference: the one Session id: cron:our-pr-ci-monitor:20260925-160221 |
|
We don't have to implement this right now |
|
Understood, I won't push anything further here. Closing it is fine with me. |
Related: #121469
Related: #119469
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Now a
COMMENT COLUMNorMODIFY COLUMN ... COMMENTwritten in the sameALTER TABLEstatement as aMODIFY QUERYon a materialized view can name a column the new query introduces. Previously the statement was rejected withNOT_FOUND_COLUMN_IN_BLOCK, or silently applied nothing when written withIF EXISTS, although writing the two commands as two separateALTERstatements worked.Description
Provenance: #121469 (comment) (automated review).
A comment command in the same
ALTERas aMODIFY QUERYcannot name a column the new query adds:Two separate
ALTERs both succeed, so the end state is legitimate; withIF EXISTSthe statement returns success and applies nothing.A materialized view's columns are its query's output, so
MODIFY QUERYreplaces the whole set, but neitherAlterCommands::validate()norAlterCommands::prepare()has aMODIFY_QUERYcase: both screen names against a pre-alter snapshot. So both stop screening once aMODIFY QUERYprecedes them, andAlterCommand::apply, which walks the commands in order against the real metadata, owns the check.An absent column is still rejected, one layer later, drawing any hint from the new query's columns. Nothing changes unless a
MODIFY QUERYprecedes a column-named command in the same statement, and a materialized view is the only storage that admitsMODIFY QUERY. The test's negative control assertsNOT_FOUND_COLUMN_IN_BLOCK, so a regression there reds CI.The new test reddens on the base commit; 250 randomized runs of it and four neighbours are green; per-half controls show each screen needs fixing.
Left alone:
ADD COLUMNwithCOMMENT COLUMN IF EXISTSon a plain table; aMODIFY QUERYwritten last still discards a preceding comment; inner-table persistence is #119469. In aReplicateddatabase the combination stays refused, for mixing replicated and non-replicated commands in oneALTER.Improvement, not a bug fix: this pairing was never a supported spelling, so no release misbehaves on one.Workflow [PR]
Sync PR [sync-upstream/pr/122213]