Repository navigation
Report a user error when a comment command follows MODIFY QUERY on a materialized view - #121469
Conversation
…materialized view
AlterCommands::apply runs a statement's commands in written order against one
accumulating StorageInMemoryMetadata. On a materialized view, MODIFY QUERY replaces
metadata.columns wholesale with the new query's output columns. A COMMENT_COLUMN or
MODIFY_COLUMN command later in the same statement then called
ColumnsDescription::modify() with a name that is no longer present, and its
missing-column branch raises LOGICAL_ERROR ("Cannot find column id in
ColumnsDescription"). In a debug or sanitizer build abortOnFailedAssertion turns that
into a server abort; on a stable build the user gets Code 49 instead of a
missing-column error.
AlterCommands::validate already screens these names, but it models only
ADD/DROP/MODIFY/RENAME COLUMN effects on the column set: it never analyzes the SELECT,
so it cannot see MODIFY QUERY rewriting a view's derived columns. That is why the
plain-table analogue ((DROP COLUMN x), (COMMENT COLUMN x 'c')) is clean while the view
case aborted. The only existing runtime guard at these two branches was
should_skip_column_operation(), which covers the IF EXISTS case alone.
Complete that runtime check for the no-IF-EXISTS case at the two reachable branches and
report NOT_FOUND_COLUMN_IN_BLOCK, the error validate already produces for the same
commands, with its wording and its column-name hints. IF EXISTS behaviour is unchanged
(the if_exists arm is tested before the throw). The statistics branches in the same
function already guard columns.modify() this way with a user-facing error.
NOT_FOUND_COLUMN_IN_BLOCK is chosen over NO_SUCH_COLUMN_IN_TABLE so the view case is
indistinguishable from the plain-table case and the error class stops depending on which
storage the statement names. The reversed command order returns NO_SUCH_COLUMN_IN_TABLE
from checkTargetTableHasQueryOutputColumns, which is a different complaint about the new
output column, so making the two orders agree on one code is not the goal.
Carriers deliberately not changed: RENAME_COLUMN and DROP_COLUMN are rejected earlier
with NOT_IMPLEMENTED by StorageMaterializedView::checkAlterIsPossible (measured), and
MODIFY QUERY exists on no other storage, so neither is reachable with a stale column
name; ColumnsDescription::remove already throws a user-facing error in any case.
ColumnsDescription::modify keeps its LOGICAL_ERROR for every other caller, where a
missing column really is an internal invariant violation.
Found by the CI AST fuzzer on
https://github.com/ClickHouse/ClickHouse/actions/runs/35654611057/job/106546712698
Co-Authored-By: Claude Opus 5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex): 2 gate findings plus 3 from my own
Severity: ❌ blocker / The Session id: cron:clickhouse-review-slot-10:20260922-011100 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20260921-234800 |
|
Workflow [PR], commit [fdc62db] Summary: ❌
AI ReviewSummaryThis PR fixes the removed-column half of a materialized-view Findings
Final Verdict
|
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, 15 s compile time in total, 7 of them have a recent master baseline. |
CI finish ledger - 073dbc9Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task whose
Session id: cron:our-pr-ci-monitor:20260922-063421 |
| return if_exists && !metadata.columns.has(column_name); | ||
| }; | ||
|
|
||
| /// validate() screens these column names too, but against a model that tracks only ADD/DROP/MODIFY/RENAME |
There was a problem hiding this comment.
This only fixes the "column disappeared" half of the MODIFY QUERY mismatch. validate() still never advances all_columns for MODIFY QUERY, so a later comment on a column introduced by the new query is rejected before we ever reach this helper, and the IF EXISTS form is still silently dropped in prepare().
Concrete path on a TO view:
CREATE TABLE dst (id UInt64, id2 Nullable(UInt64)) ENGINE = MergeTree ORDER BY id;
CREATE MATERIALIZED VIEW mv TO dst AS SELECT id FROM src;
ALTER TABLE mv
(MODIFY QUERY SELECT id, id + 1 AS id2 FROM src),
(COMMENT COLUMN id2 'c');StorageMaterializedView::checkAlterIsPossible() already allows that command mix, and params.apply() would have new_metadata.columns = {id, id2} by the time it reaches the comment alter. But AlterCommands::validate() still throws from the COMMENT_COLUMN / MODIFY_COLUMN checks because its working snapshot is stuck on the pre-alter schema, and COMMENT COLUMN IF EXISTS id2 then gets marked ignore in prepare() for the same reason. Can we teach validate()/prepare() the post-MODIFY QUERY column set for later comment alters (or explicitly reject that mixed statement as unsupported)?
There was a problem hiding this comment.
Verified, both halves are real, and both are pre-existing. I ran the same arms on two binaries under clickhouse local: the master debug snapshot 26.10.1.1, which contains no code from this PR, and this PR's head b09555b1 (build id 7b8b3de8a000c444).
| # | Arm | master snapshot | this PR |
|---|---|---|---|
| 1 | TO view, (MODIFY QUERY SELECT id, id + 1 AS id2 FROM src), (COMMENT COLUMN id2 'c') |
Code: 10 ... Cannot find column 'id2' to comment. Maybe you meant: ['id'] |
identical |
| 2 | arm 1 with COMMENT COLUMN IF EXISTS id2 'c' |
succeeds, id2's comment stays empty |
identical |
| 3 | arm 1's two commands as two separate ALTERs |
both succeed, comment c persists on id2 |
identical |
| 4 | arm 1 on an inner-table view | same Code: 10 |
identical |
| 5 | (MODIFY QUERY ...), (MODIFY COLUMN id2 COMMENT 'c') |
Code: 10 ... to modify |
identical |
| 6 | the removed-column case this PR is about | abort, exit 134 | Code: 10 |
Arm 6 is the only row that differs, and that is structural: the diff sits entirely in AlterCommand::apply, whereas InterpreterAlterQuery calls validate() at :374 and prepare() at :380 before table->alter() at :382, and AlterCommands::apply skips ignored commands at :1832. Arm 1 throws inside validate(); in arm 2 the command is already marked ignore. The helper you commented on is unreachable in both.
One correction on the IF EXISTS half: it is not specific to MODIFY QUERY. prepare() computes has_column once per command from the unadvanced pre-alter snapshot (:1974), so any preceding command has the same effect. Same binary, plain MergeTree table:
ALTER TABLE t (ADD COLUMN y UInt8), (COMMENT COLUMN IF EXISTS y 'c')succeeds withy's comment empty.(ADD COLUMN y UInt8), (MODIFY COLUMN IF EXISTS y COMMENT 'c')behaves the same.
Without IF EXISTS that pair works, because validate() does advance all_columns for ADD COLUMN (:2226). That asymmetry is the gap, not MODIFY QUERY alone, and closing it also changes behaviour for existing ADD COLUMN pairs.
So I am keeping this PR at the removed-column fix and tracking the added-column case separately. It is worth fixing: arm 3 shows the end state arm 1 asks for is legitimate, so arm 1 rejects a request that is already expressible, and its hint points at the wrong column. Of your two options I would not take "reject the mixed statement as unsupported" unless a maintainer asks for it, since it has to reject arm 2, which succeeds today. I will post the fix PR link here when it opens.
There was a problem hiding this comment.
The added-column half turns out to depend on this PR, so I would rather not open it standalone.
The fix is to stop screening a column name once a MODIFY QUERY earlier in the same statement has replaced the set, in validate() and in prepare(), and let the sequential AlterCommands::apply decide. That is about 12 lines. It only works on top of skip_absent_column_or_fail: with those two screens relaxed, a genuine typo such as (MODIFY QUERY SELECT id FROM src), (COMMENT COLUMN typo 'c') reaches ColumnsDescription::modify and produces arm 6's exit 134 instead of a user error. So this PR is the prerequisite, and I am not going to duplicate its diff into a second one.
One correction to my previous message: closing the IF EXISTS gap does not have to change behaviour for ADD COLUMN pairs. Latching only on MODIFY QUERY in prepare() leaves (ADD COLUMN y UInt8), (COMMENT COLUMN IF EXISTS y 'c') exactly as it is today.
Do you want those 12 lines and their test pushed here, or as a follow-up PR once this merges? I will default to the follow-up if you would rather not grow this one.
There was a problem hiding this comment.
Please push it here. It is the remaining half of the same mixed MODIFY QUERY + comment-alter surface, and since it depends on this helper it is cleaner to land it in the same PR than as a follow-up.
There was a problem hiding this comment.
I have those 12 lines ready: stop screening the column name in validate() and in prepare() once a MODIFY QUERY earlier in the same statement has replaced the column set, and let the sequential AlterCommands::apply own the check. The prepare() half is what closes the IF EXISTS case, and the latch is on MODIFY QUERY alone, so (ADD COLUMN y UInt8), (COMMENT COLUMN IF EXISTS y 'c') keeps the behaviour it has today.
They only work on top of this PR. Master still does not carry skip_absent_column_or_fail (checked at 1be97a74), so with those two screens relaxed a plain typo such as (MODIFY QUERY SELECT id FROM src), (COMMENT COLUMN typo 'c') reaches ColumnsDescription::modify and gives an internal error that aborts a debug build, instead of Code: 10.
What I do not want to do is grow this PR on my own initiative. Two reviewers are requested on it, a master merge landed on the branch at 00:31 UTC and its CI is still in flight, and the diff here is a single guard in AlterCommand::apply, which is cheap to review as it stands.
@Michicosun you are one of the requested reviewers and have the recent history on this file, so I would rather have your call: those 12 lines plus their test pushed here, or a follow-up PR once this merges? I will default to the follow-up and post its link in this thread.
There was a problem hiding this comment.
@groeneai go with a follow-up PR. For this one, change the category for bugfix.
There was a problem hiding this comment.
Done on both.
Category: switched to Bug Fix (user-visible misbehavior in an official stable release) and re-voiced the changelog entry to match. I measured it first, since the repro had been filed as an improvement: it raises Code: 49. Cannot find column id in ColumnsDescription. (LOGICAL_ERROR) on official 26.3.33.10, 26.6.5.73, 26.7.6.57 and 26.8.2.7, and aborts a debug build, so it is a stable-release defect present in every one of those releases.
Follow-up: the validate()/prepare() half stays out of this PR and goes in its own. I will post the link in this thread when it opens.
There was a problem hiding this comment.
Follow-up is open: #122213
It is the validate()/prepare() half, on top of this one now that it has merged: both screens stop at a MODIFY QUERY earlier in the same statement, so a comment command can name a column the new query introduces, and AlterCommand::apply keeps the rejection for a genuinely absent name. Filed as Improvement, since the pairing was never a supported spelling, so no release misbehaves on it.
| /// COLUMN - not MODIFY QUERY, which replaces a materialized view's columns with its new query's output. | ||
| auto skip_absent_column_or_fail = [&](std::string_view action) -> bool | ||
| { | ||
| if (metadata.columns.has(column_name)) |
There was a problem hiding this comment.
@groeneai could you call should_skip_column_operation here?
There was a problem hiding this comment.
Done in fdc62db: the guard now calls should_skip_column_operation() for the skip decision and only decides between "the column is present, proceed" and "report the user error", so the if_exists predicate lives in one place.
Behaviour is unchanged, since the two predicates are mutually exclusive. Verified on a debug build: 05237_alter_modify_query_comment_removed_column passes 10/10, a missing column without IF EXISTS still reports NOT_FOUND_COLUMN_IN_BLOCK, with IF EXISTS the comment is still skipped and the statement fails on the inner table with NO_SUCH_COLUMN_IN_TABLE, and the control where the new query keeps the column still succeeds.
Review request: the new absent-column guard spelled the IF EXISTS test out a second time, so the two copies of that predicate could drift. It now calls should_skip_column_operation(), which already owns it, and decides only between "the column is present, proceed" and "report the user error". Behaviour is unchanged. should_skip_column_operation() is if_exists && !metadata.columns.has(column_name), so the three outcomes stay the same and stay mutually exclusive: a missing column under IF EXISTS skips the command, a present column proceeds, and a missing column without IF EXISTS reports NOT_FOUND_COLUMN_IN_BLOCK with the column-name hints.
CI finish ledger - fdc62dbEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task whose
Nothing is PR-caused: the diff is +16/-2 in Session id: cron:our-pr-ci-monitor:20260924-183043 |
711f560
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed the logical error
Cannot find column <name> in ColumnsDescription(a server abort in debug and sanitizer builds) raised when a singleALTERstatement on a materialized view appliesCOMMENT COLUMNorMODIFY COLUMN ... COMMENTafter aMODIFY QUERYwhose new query no longer produces that column. Such a statement now reports the missing column.Description
Found by CI: AST fuzzer (amd_debug, targeted), 2026-09-21. The defect is master's; #119469 only added the first in-tree statement combining these commands.
Official 26.3.33.10, 26.6.5.73, 26.7.6.57 and 26.8.2.7 all raise
Code: 49. Cannot find column id in ColumnsDescription. (LOGICAL_ERROR)there, and a debug or sanitizer build aborts.Root cause.
AlterCommands::applyapplies commands in written order against one accumulating metadata. On a materialized viewMODIFY QUERYreplacesmetadata.columnswith the new query's output, so a laterCOMMENT_COLUMN/MODIFY_COLUMNreachesColumnsDescription::modify()with a name that is gone, whose missing-column branch raisesLOGICAL_ERROR.AlterCommands::validatescreens these names, but models onlyADD/DROP/MODIFY/RENAME COLUMNand never analyzes the SELECT, so it cannot seeMODIFY QUERYrewriting a view's derived columns. The plain-table analogue is therefore clean.Change. +16/-2. The runtime guard at those two branches covered only
IF EXISTS; I complete it for the no-IF EXISTScase, reportingNOT_FOUND_COLUMN_IN_BLOCKwithvalidate's wording and hints. The statistics branches beside it already guardcolumns.modify()this way.IF EXISTSis unchanged, andColumnsDescription::modifykeeps itsLOGICAL_ERRORfor other callers.Siblings.
RENAME COLUMNandDROP COLUMNare rejected earlier withNOT_IMPLEMENTEDon a materialized view (measured), andMODIFY QUERYexists on no other storage, so these two are the whole reachable set.Validation. New test
05237_alter_modify_query_comment_removed_columncovers the defect, the sibling command, aTO-target view and two controls. Two servers from one tree: the unpatched aborts, the patched passes. 50/50 randomized and not; the 54 stateless tests naming these commands stay green.Workflow [PR]
Sync PR [sync-upstream/pr/121469]
Version info
26.10.1.655-master(included in26.10and later)