Repository navigation
Fix lightweight updates lost and wrong values when reading with apply_mutations_on_fly over a pending MODIFY COLUMN - #127372
Conversation
…pending MODIFY COLUMN With `apply_mutations_on_fly = 1` and a pending `ALTER MODIFY COLUMN`, a column the on-fly chain overwrites stays in its on-disk type until the step that overwrites it, unless a step reads it first (`collectColumnsConsumedByChainActions`). Two defects in `MergeTreeReadersChain`: - The consume scan skipped every step with `perform_alter_conversions`, which includes the on-fly steps queued after the `MODIFY`. Their `if(cond, expr, col)` keep-old fallback then read `col` unconverted: a debug abort in `FunctionIf`, wrong values on release. - `applyPatchesAfterReader` applied only `AfterConversions` patches between steps, so every lightweight update older than the `MODIFY` (`BeforeConversions`) was dropped there, and it wrote newer patches into still-unconverted columns (abort in `updateFrom`, SIGSEGV on release). Now the scan covers every on-fly mutation step (`mutation_version` set) with the chain-wide overwritten set, so a skip-set column stays unconverted only when its first reference in the whole chain is a constant-true overwrite. The readers chain precomputes, per step, the columns still unconverted after it; `applyPatchesAfterReader` drops their patch entries (a later step overwrites them on every row) and applies all orders for the rest, casting an older patch to the requested type like a newer one. The v1 legacy path applies only the columns each patch group was selected for, as the v2 `MergeOnKey` path already does; without it a v1 patch of a converted and an unconverted column would write the unconverted one too. This is the change of ClickHouse#126698, taken verbatim so that the two PRs merge in either order. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Fold the scanned-steps note into one line of the collectColumnsConsumedByChainActions doc and drop the collectColumnsUnconvertedAfterStep doc that repeats the member comment. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 2 rounds, 1 finding) and an own cold review (2 rounds, 3 findings, 1 withdrawn).
Re-review of the fixed tree: 0 findings from either review. Severity: ❌ blocker / Session id: cron:clickhouse-review-self-slot-58:20261022-100400 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-self-slot-57:20261022-064600 |
|
cc @Algunenano @CurtizJ, could you review this and add the |
|
Workflow [PR], commit [9c98b41] Summary: ✅
AI ReviewSummaryReviewed head Final VerdictStatus: ✅ Approve |
…ding it After an on-fly UPDATE overwrites a column, the column has its post-MODIFY type, so a lightweight UPDATE older than the pending MODIFY that falls between this step and a later step was cast to that type before it was applied. When the later step overwrites the column on every row before reading it, the patched value is discarded anyway, and materializing the mutations never converts it; the on-fly read failed with CANNOT_PARSE_TEXT instead (for example `UPDATE c = 'bad'` between `ALTER UPDATE c = '100'` and `ALTER UPDATE c = '200'` before `MODIFY COLUMN c UInt64`). The consume scan now returns, for every step, the columns it consumes and the columns it overwrites. From the same first-reference rule that decides whether an on-disk value is converted, a backward pass finds the columns whose next referencing step overwrites them without reading them, and the after-reader application drops their patches, as it already does for columns still in their on-disk type. A later partial overwrite reads the old value for unmatched rows, so such a patch is still converted, as the on-disk value is. Reported by the automated review on the PR. Co-Authored-By: Claude Opus 5.5 <[email protected]>
An on-fly ALTER UPDATE also emits a stage that recomputes every MATERIALIZED column depending on its targets, but only the explicit UPDATE targets were treated as overwritten when deciding which patches a later step discards. So a lightweight UPDATE older than a pending MODIFY of such a column, falling between two on-fly UPDATEs of its source, was still cast to the post-MODIFY type after the first one, and the read failed with CANNOT_PARSE_TEXT although the second UPDATE recomputes the value before the MODIFY needs it (for example `m String MATERIALIZED c`, `UPDATE c = 'bad'` between `ALTER UPDATE c = '100'` and `ALTER UPDATE c = '200'`, then `MODIFY COLUMN m UInt64`). The consume scan now also records, for every step, the non-passthrough outputs that are not UPDATE targets, and the backward pass treats them like overwritten columns. The explicit-target set used to decide on-disk conversions is unchanged. Reported by the automated review on the PR. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes14 object files changed (+25.95 KiB total), 0 added.
716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units149 translation units recompiled, 1357 s compile time in total, 149 of them have a recent master baseline. |
CI finish ledger - 4ea1d73Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20261023-050052 |
…ches-pending-modify Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ches-pending-modify # Conflicts: # src/Storages/MergeTree/MergeTreeReadersChain.cpp # src/Storages/MergeTree/MergeTreeReadersChain.h
Related: #126698
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
SELECTwithapply_mutations_on_fly = 1on a table with a pendingALTER MODIFY COLUMN: lightweightUPDATEs made before theMODIFYwere not applied, a lightweightUPDATEmade after it could crash the server when a pendingALTER UPDATEoverwrites the same column, and anALTER UPDATEqueued after theMODIFYreturned wrong values for the rows it does not update.Description
On 26.8.21.12 and 26.3.33.24, a
SELECTwithapply_mutations_on_fly = 1over a pendingALTER MODIFY COLUMNdrops lightweightUPDATEs made before theMODIFY, crashes (SIGSEGV) when one made after it targets a column a pendingALTER UPDATEoverwrites, and returns wrong values when anALTER UPDATEqueued after theMODIFYreads the modified column:Root cause, in
MergeTreeReadersChain: a column the on-fly chain overwrites stays in its on-disk type until the step that overwrites it, unless a step reads it first. That check skipped the steps queued after theMODIFY, so theirALTER UPDATEread the unconverted column. Patches whose version falls between two steps were applied only if newer than theMODIFY, and also into still-unconverted columns.Change: the check covers every on-fly mutation step. Between steps, patches are applied to the columns in their requested type and skipped for the unconverted ones and for those the next step that references them overwrites or recomputes without reading them. The v1 patch path applies only the columns each patch group was selected for, as the v2 path does; this is the change of #126698, included verbatim.
Validation: the new test compares every on-fly read with the same read after the mutations are materialized. It fails on master (wrong rows, then an abort) and passes 50/50 with randomized settings; related tests show no new failures.
Workflow [PR]
Sync PR [sync-upstream/pr/127372]