Repository navigation
Conversation
ADD COLUMN IF NOT EXISTS is resolved in AlterCommands::prepare against the prepare-time metadata snapshot (command.ignore is set only if the column already exists then). But checkAlterIsPossible re-applies the commands via AlterCommand::apply against a fresher snapshot, and AlterCommand::apply for ADD_COLUMN unconditionally called metadata.columns.add(), which throws ILLEGAL_COLUMN when the column now exists. This is the only ADD_*/column op whose apply() ignored its if_not_exists flag (DROP/MODIFY/COMMENT_COLUMN, ADD_INDEX/CONSTRAINT/PROJECTION all honor it). Two triggers hit it: (a) two ADD COLUMN IF NOT EXISTS of the same column in one statement (prepare marks neither ignore; validate adds it once then continues), and (b) a concurrent ALTER adds the column between prepare and apply on a ReplicatedMergeTree. Skip the add in apply() when if_not_exists is set and the column already exists, matching ColumnsDescription::add's own existence check. Plain ADD COLUMN still throws DUPLICATE_COLUMN on an existing column. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-1:20260711-062400 |
|
cc @tavplubix @alesapin — could you review this? |
|
Workflow [PR], commit [23d0027] Summary: ✅
AI ReviewSummaryThis PR fixes Final Verdict
|
flatten_nested (default on) stores n Nested(a ...) as n.a, so the apply-time has(column_name) guard is false for the nested group name and the flattened n.a add still throws ILLEGAL_COLUMN when the same nested group is added twice with IF NOT EXISTS in one statement. Guard with has() || hasNested() so IF NOT EXISTS is a consistent no-op for nested adds too. The collision is independent of share_nested_offsets, so the check is unconditional. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Good catch. Fixed in 616950c: the apply-time guard now checks On threading Regression test Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-4:20260711-122100 |
The previous guard used has(column_name) || hasNested(column_name). The hasNested() prefix match is broader than prepare()/validate(): a table with only n.a would wrongly skip a genuinely new, distinct top-level scalar n (ADD COLUMN IF NOT EXISTS n String silently dropped). Instead compute the exact set of column names this ADD would insert (the flattened n.a, n.b, ... under flatten_nested, or the single column) and skip only names that truly already exist. This keeps the double-add nested no-op correct while not dropping a real new column, and is consistent regardless of share_nested_offsets. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Good catch, and agreed. Fixed in 2aa3b7f by switching the apply-time guard to your second suggestion: compare the exact transformed names that would be inserted rather than an
Why not gate on Regression ( Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-0:20260711-133400 |
The apply-time IF NOT EXISTS existence check must match prepare()/validate(), which gate n/n.* equivalence on share_nested_offsets. With it enabled (default), any pre-existing n.* makes the whole nested/scalar add a no-op (via hasNested); with it disabled n and n.* are independent, so only exact transformed names that already exist are skipped. This keeps ADD COLUMN n.a, ADD COLUMN IF NOT EXISTS n Nested(a, b) consistent between validate() and apply() (no spurious n.b) while still adding a genuinely new distinct column under share_nested_offsets = 0. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Addressed in 1fe36fb. Root of the tension: finding #2 correctly required not treating any Fix: thread
Matrix verified (
Regression test Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-1:20260711-143700 |
The apply-time IF NOT EXISTS nested guard was share_nested_offsets-aware only on the explicit commands.apply() path. getMutationCommands -> tryConvertToMutationCommand -> AlterCommand::apply still replayed with the default share_nested_offsets = true, so for share_nested_offsets = 0 tables the mutation-planning snapshot diverged from the real apply. A table with only n.a then ADD COLUMN IF NOT EXISTS n String, RENAME COLUMN n TO n2 would skip the add (hasNested matched n.a) and RENAME threw from ColumnsDescription::rename. Thread the flag through tryConvertToMutationCommand / getMutationCommands from all MergeTree callers. Co-Authored-By: Claude Opus 4.8 <[email protected]>
The apply-time ADD COLUMN IF NOT EXISTS no-op gated the exact metadata.columns.has(column_name) match on share_nested_offsets, but prepare()/validate() treat an exact existing column as a whole-command no-op unconditionally. Only the n/n.* nested equivalence (hasNested) is share_nested_offsets-gated. Pull has(column_name) out of the gate so apply() matches validate()'s existence predicate; with share_nested_offsets=0, ADD COLUMN n String, ADD COLUMN IF NOT EXISTS n Nested(a,b) no longer diverges (second command stays a no-op). Co-Authored-By: Claude Opus 4.8 <[email protected]>
validate() advanced its working snapshot with a synthetic top-level n for an ADD COLUMN n Nested(...), while apply() materializes the flattened children n.a/n.b. A later command in the same ALTER targeting a real child (RENAME COLUMN n.b) was then rejected in validate() with Cannot find column n.b even though apply() would have created it. This divergence predates the IF NOT EXISTS work (a plain ADD COLUMN n Nested(a,b), RENAME COLUMN n.b hit the same wrong path). Factor the flatten_nested expansion + IF NOT EXISTS existence filter into a single columnsAddedByAlter helper used by both AlterCommand::apply and AlterCommands::validate, so the two can no longer disagree on the columns a (nested) ADD introduces. Co-Authored-By: Claude Opus 4.8 <[email protected]>
…ertToMutationCommand
The mutation branch of AlterCommand::tryConvertToMutationCommand called
apply(metadata, context) with the default share_nested_offsets, so the
mutation-planning intermediate metadata could diverge from the real
commands.apply for share_nested_offsets = 0 tables. Pass the threaded
share_nested_offsets so both replay paths agree.
Ignored commands cannot reach this branch: isRequireMutationStage returns
false when ignore is set, so a command marked ignore by prepare() (an unmet
IF [NOT] EXISTS precondition) returns {} earlier and queues no mutation.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
CI finish ledger — db3437fEvery failure below has an owner: a fixing PR (ours), or a full-effort fix task whose fixing-PR link will be posted here when it opens. Only
Session id: cron:our-pr-ci-monitor:20260712-000000 |
|
Fixing PR for the parallel_replicas_protocol OPTIMIZE FINAL flaky (backward-compat, arm_binary distributed plan): #110129 |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 81/81 (100.00%) · Uncovered code |
|
📊 Cloud Performance Report ✅ AI verdict: This PR only reworks ALTER TABLE ADD COLUMN IF NOT EXISTS handling for nested columns (the DDL apply/validate path); it does not touch SELECT query execution. Both flagged ClickBench queries (Q4 -23.1%, Q15 -14.5%) show faster medians but essentially unchanged CPU, and both land inside master's current variance band, so the deterministic within-envelope no_change verdicts are correct. There is no plausible mechanism for this change to affect either query — the deltas are run-to-run variance, not a real PR effect. clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
CI finish ledger - 23d0027CI is fully finished on this head (Finish Workflow success, 153 success / 18 skipped, zero failures). Session id: cron:our-pr-ci-monitor:20260726-163000 |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a spurious
ILLEGAL_COLUMNerror fromALTER TABLE ... ADD COLUMN IF NOT EXISTSwhen the column already exists at apply time. This happened when the same column was added twice withIF NOT EXISTSin one statement, or when a concurrentALTERadded the column between the prepare and apply phases.Description
ADD COLUMN IF NOT EXISTSis resolved inAlterCommands::prepareagainst the prepare-time metadata snapshot:command.ignoreis set only when the column already exists in that snapshot. ButcheckAlterIsPossiblere-applies the commands throughAlterCommand::applyagainst a fresher snapshot, and theADD_COLUMNbranch ofAlterCommand::applyunconditionally calledmetadata.columns.add(), which throwsILLEGAL_COLUMN(ColumnsDescription::add) when the column now exists.ADD_COLUMNwas the only ADD_*/column operation whoseapply()ignored itsif_[not_]existsflag.DROP/MODIFY/COMMENT_COLUMNhonor it via an early return, andADD_INDEX/ADD_CONSTRAINT/ADD_PROJECTIONall honorif_not_existsat apply time;validate()also already tolerates a pre-existing column. Soapply()was inconsistent with bothvalidate()and its siblings.Two triggers hit the same missing guard:
ADD COLUMN IF NOT EXISTS <same col>in one statement —preparemarks neitherignore;validateadds the column for the first command thencontinues the second;applyadds the first then throws on the second;ALTERadds the column betweenprepareandapplyon aReplicatedMergeTree.Deterministic repro:
The fix skips the add in
apply()whenif_not_existsis set and the column already exists, matchingColumnsDescription::add's own existence check. PlainADD COLUMNof an existing column still throwsDUPLICATE_COLUMN, so duplicate detection is untouched. Test01318_alter_add_column_existspreviously codified the buggy behavior (assertedserverError ILLEGAL_COLUMN); it is updated to assert the corrected behavior plus positive/negative cases.No related open issue found (searched ClickHouse/ClickHouse issues and PRs).
Version info
26.8.1.427(included in26.8and later)