Repository navigation
Keep the column default when another MODIFY COLUMN restates the type - #114807
alexey-milovidov merged 4 commits into
Conversation
A single ALTER with two MODIFY COLUMN subcommands on the same column silently
corrupted that column's default, in two opposite directions, and both are
reachable from plain DDL on a default MergeTree:
ALTER TABLE t MODIFY COLUMN c UInt32 MATERIALIZED JSONExtractUInt(event, 'x'),
MODIFY COLUMN c UInt32 CODEC(T64, LZ4);
dropped the MATERIALIZED expression, so the column read the type's zero value
instead of the computed one, and
ALTER TABLE t MODIFY COLUMN c REMOVE MATERIALIZED,
MODIFY COLUMN c UInt32 CODEC(T64, LZ4);
restored the default the REMOVE had just deleted, so a later explicit INSERT was
rejected with ILLEGAL_COLUMN where it succeeds after a plain REMOVE. Both
returned success and logged nothing. Omitting the redundant type in the second
subcommand was already correct, so one token decided whether the statement
worked.
AlterCommand::apply replaced the column default whenever the subcommand carried
a data type, writing an empty default over what a preceding subcommand of the
same ALTER had written into the shared metadata copy. AlterCommands::prepare
compensated by back-filling the default from the table, but it reads the
pre-ALTER column, and that one fact produced both directions: nothing to
back-fill for a default just set, and a stale default to resurrect for one just
removed.
Replace the default only when the subcommand carries a default expression, which
is an exact test because both parser sites set default_kind only inside
`if (ast_col_decl.getDefaultExpression())`, and removals are handled by the
earlier to_remove branches. That makes the MODIFY_COLUMN path match the
ADD_COLUMN path, which already guards on default_expression alone, and lets
composition fall out of the sequencing apply() already performs, so the
compensating back-fill in prepare() is removed with it.
One currently accepted statement starts being rejected: dropping a MATERIALIZED
that made the column nullable and restating the type as non-nullable now reports
the conversion it cannot perform, which is what the same two statements issued
separately already do.
Both state the local invariant only. Net comment count over the change is unchanged: each replaces a comment this change makes wrong.
Internal second-model review (click to expand)Reviewed independently before publishing: an author-blind read of the resulting code, then a
No blocking or major finding. Notes on scope, stated plainly rather than buried: Session id: cron:clickhouse-review-self-slot-58:20260814-110600 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-self-slot-57:20260814-091600 |
|
Workflow [PR], commit [b4bc975] Summary: ✅
AI ReviewSummaryThis PR fixes the Findings
Final Verdict
LLVM Coverage ReportMeasured on commit b4bc975.
Changed lines: Changed C/C++ lines covered: 9/9 (100.00%) · Uncovered code |
|
cc @alexey-milovidov @SmitaRKulkarni, could you review this? A |
| CREATE TABLE t_mod_def (event String, c UInt32 MATERIALIZED JSONExtractUInt(event, 'x')) | ||
| ENGINE = MergeTree() ORDER BY tuple(); | ||
| ALTER TABLE t_mod_def MODIFY COLUMN c UInt64 CODEC(T64, LZ4), | ||
| MODIFY COLUMN c UInt64 COMMENT 'z'; -- { serverError MULTIPLE_EXPRESSIONS_FOR_ALIAS } |
There was a problem hiding this comment.
AlterCommand::apply now composes these subcommands correctly, but AlterCommands::validate still does not: the else if (all_columns.has(column_name) && command.data_type) branch in src/Storages/AlterCommands.cpp:2197-2211 re-injects the pre-ALTER default once for every type-carrying MODIFY COLUMN that omits an explicit default. That means a column which already had a default still cannot compose
ALTER TABLE t
MODIFY COLUMN c UInt64 CODEC(T64, LZ4),
MODIFY COLUMN c UInt64 COMMENT 'z';even though the two statements succeed separately and the final metadata is unambiguous. The same stale pre-ALTER default is also why case 11 still rejects REMOVE MATERIALIZED, MODIFY COLUMN c Int8. I think this needs a real fix in the validation path rather than a test that pins the broken behavior.
There was a problem hiding this comment.
The residual is real, but it is pre-existing, and the case 11 half of it is reachable with a single subcommand, so neither half is composition and neither is this PR's defect.
Measured on the reviewed head and on its parent commit, same runner and flags, only the binary differs. Your example fails identically on both with Code: 179 MULTIPLE_EXPRESSIONS_FOR_ALIAS. Case 11 is that same rejection, and it needs neither a second subcommand nor REMOVE:
CREATE TABLE t (event String, c Enum8('x' = 1) MATERIALIZED 'x') ENGINE = MergeTree() ORDER BY tuple();
ALTER TABLE t MODIFY COLUMN c Int8; -- Code: 6 CANNOT_PARSE_TEXT on both binariesThat single-subcommand form is pinned on line 168, right next to the combined one. The test records that combining the subcommands changes nothing, which is what makes it a control rather than a pin on broken composition.
Advancing the validation snapshot would break the shape this PR fixes. The branch, at src/Storages/AlterCommands.cpp:2147-2166 on this head rather than 2197-2211, adds the column's stored default to default_expr_list, and a subcommand carrying an explicit default already added its own at 2141. Give the second subcommand the advanced state and both entries alias c. That is measurable today, by pre-setting the default so validate() sees what an advance would show it:
CREATE TABLE t (event String, c UInt32 MATERIALIZED JSONExtractUInt(event, 'x')) ENGINE = MergeTree() ORDER BY tuple();
ALTER TABLE t MODIFY COLUMN c UInt32 MATERIALIZED JSONExtractUInt(event, 'x'),
MODIFY COLUMN c UInt32 CODEC(T64, LZ4); -- Code: 179 on both binariesThe primary repro would report that error instead of returning 42.
Validating each default once against the final post-ALTER state, your other suggestion, is a genuine change but a larger one: it has to define what two colliding expressions for one column mean. That belongs in its own PR rather than bundled with a wrong-results fix.
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 - 79b6371Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
CI is fully finished on this head: 177 leaves, 0 dropped, 158 OK / 18 skipped / 1 failure; The single red is mechanically unrelated to this change: this PR touches only Session id: cron:our-pr-ci-monitor:20260815-003000 |
|
@groeneai please update the branch on this PR to the latest master |
|
Updated: merged master I rebuilt and re-ran the test on the merged tree: |
CI finish ledger - 2c97894Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
I am not merging master to pick up #117701 yet: Session id: cron:our-pr-ci-monitor:20260902-173000 |
|
@groeneai update to the latest master, and ensure green ci on this PR |
|
Done: merged master Both blocking failures at the previous head had the same cause and neither is reachable from this diff: the S3 artifact-prefix cutover in #110081 broke two baseline lookups, and the previous base (2026-09-02T10:35Z) predated both fixes.
Both fixes are on the branch now. I will report back here when CI finishes, with an owner for anything still red. |
| if (command.codec) | ||
| { | ||
| /// `default_kind` is what the parser set: `validate` runs before `prepare`, which back-fills it from the table. | ||
| /// `default_kind` holds its enumerator's zero value unless `default_expression` is set. |
There was a problem hiding this comment.
apply() now preserves the running default across a later type restate, but validate() still checks REMOVE DEFAULT / MATERIALIZED / ALIAS against all_columns.getDefault(column_name) from the table's original metadata here. A minimal repro is:
CREATE TABLE t (c UInt32) ENGINE = MergeTree() ORDER BY tuple();
ALTER TABLE t
MODIFY COLUMN c UInt32 DEFAULT 7,
MODIFY COLUMN c REMOVE DEFAULT;The two statements succeed separately, but the combined form still throws Column c doesn't have DEFAULT, because the second subcommand never sees the default the first one just wrote. So same-column MODIFY COLUMN composition is still broken for removals unless validate() advances the per-column default state before these checks.
There was a problem hiding this comment.
Confirmed, and wider than stated: the same refusal fires with the two subcommands swapped, and with MATERIALIZED in place of DEFAULT. It is also pre-existing, and it cannot ride in this PR.
Measured on three binaries, same flags, arms separated behaviourally by the primary repro (master returns c = 0 with an empty default_kind, this head returns c = 5 / MATERIALIZED):
- Your repro gives
Code: 36. Column 'c' doesn't have DEFAULT, cannot remove itbyte-identically on master2f4969d6and on this headb4bc9754, so it is not introduced here. - The two statements issued separately are accepted on both, so the asymmetry you point at is real.
- A single
ALTER TABLE t MODIFY COLUMN c REMOVE DEFAULTon a column with no default gives the sameCode: 36. That is the mechanism:validate()never advances the per-column state, so an entitled removal and an unentitled one are indistinguishable to it.
I implemented your suggested remedy rather than argue it: after each MODIFY_COLUMN is checked, write its default into all_columns and clear it for a REMOVE, mirroring what ADD_COLUMN already does at :2117-2122 on this head. It does accept your repro, and it breaks the case this PR exists to fix:
Code: 179. Multiple expressions _CAST(c_tmp_alter..., 'UInt64') AS c and
_CAST(c_tmp_alter..., 'UInt32') AS c for alias c ... default expression and
column type are incompatible. (MULTIPLE_EXPRESSIONS_FOR_ALIAS)
Advancing the state makes one column carry two default expressions inside a single ALTER, one per restated type, and the default-versus-type compatibility analysis then sees both. A correct fix has to advance the state for the removal checks at :2199-2217 without feeding it to that analysis, which is a separate change in a function this PR does not modify. What this PR does change is unaffected: a pre-existing DEFAULT or MATERIALIZED, plus a type restate, plus the matching REMOVE, is accepted identically on both binaries.
One correction to my earlier reply on the test file: I declined that finding partly because it was reachable with a single subcommand. That reason does not hold for this shape, since the single-subcommand form is correctly refused, so composition is the whole defect here. I have widened the disclosure in the description to cover it.
ReproducerCREATE TABLE t (event String, c UInt32) ENGINE = MergeTree() ORDER BY tuple();
ALTER TABLE t MODIFY COLUMN c UInt32 MATERIALIZED JSONExtractUInt(event, 'x'),
MODIFY COLUMN c UInt32 CODEC(T64, LZ4);
INSERT INTO t (event) VALUES ('{"x":42}');
SELECT c FROM t; -- 0 on 26.8/26.7/26.6/26.3/25.8, expected 42
SELECT default_kind FROM system.columns WHERE table = 't' AND name = 'c'; -- empty, expected MATERIALIZEDResults:
CC component owner: @tuanpach @tavplubix Analysis metadata
Introducing change: predates `v22.3.20.29` (oldest testable release)
|
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a single
ALTERwith twoMODIFY COLUMNsubcommands on the same column silently corrupting that column's default: a default set by one subcommand was dropped, and aREMOVE DEFAULT/MATERIALIZED/ALIASwas undone, whenever a following subcommand restated the column type.Description
Two
MODIFY COLUMNsubcommands on one column silently corrupted its default in two opposite directions, from plain DDL, returning success and logging nothing.Wrong results:
Valid input rejected:
MODIFY COLUMN c REMOVE MATERIALIZED, MODIFY COLUMN c UInt32 CODEC(...)left theMATERIALIZEDin place, so a laterINSERT INTO t (event, c)failed withILLEGAL_COLUMNwhere it succeeds after a plainREMOVE.AlterCommand::applyreplaced the column default whenever the subcommand carried a data type, writing an empty default over what a preceding subcommand had written into the shared metadata copy.AlterCommands::preparecompensated by back-filling from the table, but it reads the pre-ALTER column: nothing to back-fill for a default just set, and a stale default to resurrect for one just removed.Related: #113853 (comment)
The default is now replaced only when the subcommand carries a default expression, and the back-fill is gone, making
MODIFY_COLUMNmatchADD_COLUMN.One accepted statement starts being rejected: dropping a
MATERIALIZEDthat made the column nullable while restating the type as non-nullable now reports the conversion it cannot perform, as those statements issued separately already do.New test
04904fails on the parent commit and passes with the fix; 50/50 randomized runs and a 55-test ALTER neighbour set are green.Also noticed, not addressed here:
validate()checks removals and type compatibility against the pre-ALTERdefault, so an existing default unreadable as the new type is rejected with a singleMODIFY COLUMN, and aREMOVE DEFAULTbeside a subcommand that sets one is rejected.Workflow [PR]
Sync PR [sync-upstream/pr/114807]