Repository navigation
Fix LOGICAL_ERROR in on-fly mutation prewhere when reading an overwritten LowCardinality source column - #107475
Conversation
Pre-PR validation gate
Session id: cron:clickhouse-worker-slot-1:20260615-042600 |
|
cc @Algunenano @CurtizJ — could you review this? It completes the on-fly mutation |
|
Workflow [PR], commit [6271a23] Summary: ✅
AI ReviewSummary
Final Verdict
|
66b9d06 to
6e4dab5
Compare
Pre-PR validation gate (revised after the order/step-aware fix)
Session id: cron:clickhouse-worker-slot-3:20260615-092500 |
|
@Algunenano No. I tested every reproducer in #105947 against this branch (debug build of the PR head). Only variant 5 is fixed; the soft-delete / DELETE-then-MODIFY family is not.
This PR is scoped to the on-fly UPDATE-chain action-DAG mechanism only ( Variants 1/1b/2/4 share a different root, as your issue's "Suggested solution" lays out: the column-side
That is the larger refactor you mentioned #105847 deferred. How would you like to land it: keep this PR as the variant-5 fix and open a separate follow-up for the |
Algunenano
left a comment
There was a problem hiding this comment.
In general it looks good, but the PR adds handling for ALIAS and no tests for it. Same problem with ARRAY JOIN, which might not even be reachable via mutation.
@groeneai Could you please add tests that cover these corner cases? If they aren't reachable and it's dead code, it's better to remove it and add assertions instead. Otherwise people might miss read it, copy the code and treat it as correct.
|
CI fully finished on HEAD
Session: cron:our-pr-ci-monitor:20260615-160000 |
6e4dab5 to
def75a0
Compare
Pre-PR validation gate
Session id: cron:clickhouse-worker-slot-1:20260615-170100 |
|
@Algunenano Both are dead code, so I removed the handling and replaced it with assertions (pushed in Reachability evidence (DAG instrumentation over 38 on-fly mutation-step DAGs spanning
So chassert(node.type != ActionsDAG::ActionType::ARRAY_JOIN);
...
chassert(arg->type != ActionsDAG::ActionType::ALIAS);The reachable behavior is unchanged, so |
…tten LowCardinality source column
On-fly mutation prewhere steps that precede a pending ALTER MODIFY COLUMN are
read with perform_alter_conversions = false, and every column the chain
overwrites is exempted from performRequiredConversions: its on-disk value is
about to be discarded by the UPDATE's own CAST, and pre-casting it could fail
on a value being replaced (e.g. CAST('x' AS UInt64) before UPDATE v = '100').
The conversion of a column happens once, in the reader that first reads it from
disk. But the column may be consumed as a function input by a LATER on-fly
mutation step in the chain. Consider:
UPDATE b = isNotNull(materialize(a));
UPDATE a = 'x';
MODIFY COLUMN a LowCardinality(Nullable(String));
A query that reads both b and a (e.g. SELECT sum(b), any(a)) keeps the
UPDATE a command, so a lands in the overwritten set and its conversion is
skipped. The UPDATE b step's action, built against the post-MODIFY metadata,
then runs materialize(a) expecting LowCardinality(Nullable(String)) but receives
an on-disk Nullable(String), aborting with
LOGICAL_ERROR: Unexpected return type from materialize.
Compute, once per reader chain, the set of columns consumed as a function input
by an on-fly mutation step's action DAG, and keep those columns out of the
conversion skip set. The scan is restricted to mutation steps
(perform_alter_conversions == false): their action DAG is always built against
the post-MODIFY metadata type, so a column they read must arrive converted.
Trailing query PREWHERE and row-level-filter steps
(perform_alter_conversions == true) are excluded; they run after the whole
mutation chain has produced the final, correctly typed column and convert at
their own turn. Counting their consumption would force a premature conversion
of an about-to-be-discarded on-disk value, e.g. PREWHERE v > 50 casting on-disk
'x' to UInt64 before UPDATE v = '100' replaces it (CANNOT_PARSE_TEXT).
The consumed set is keyed by NameAndTypePair::getNameInStorage, the same
subcolumn-aware key the skip set in executeActionsBeforePrewhere compares
against. Each function-input name from an action DAG is resolved through the
readers' column lists to that storage key. A physical column whose name
contains dots (e.g. info.name) keeps its exact name, while a real subcolumn
(info.name where info is a Tuple/Nested column) maps to its parent storage
column info. Keying by the part before the first dot would store a physical
info.name as info, miss it in the skip set, and reintroduce the same
materialize type mismatch.
The arrayJoin and ALIAS-as-function-argument cases are not reachable in a
mutation step's DAG (arrayJoin is rejected for mutations in MutationsInterpreter
because it changes the row count; a mutation step binds function arguments
directly to source nodes, with ALIAS appearing only as a step's named output),
so they are guarded with assertions rather than handled.
def75a0 to
27155e4
Compare
Pre-PR validation gate
Session id: cron:clickhouse-worker-slot-4:20260615-181500 |
The previous version collected every column consumed as a function input by any on-fly mutation step (chain-wide) and forced its conversion. That over-forces when the column is overwritten before any later step consumes it. An UPDATE compiles to `if(cond, _CAST(expr), C)` and reads the old `C`, so a non-constant `UPDATE v = toString(id + 100)` makes the overwrite step self-consume `v`; the chain-wide set then forced a conversion of the on-disk value before the UPDATE replaced it, casting an unconvertible `'x'` to `UInt64` (CANNOT_PARSE_TEXT). Decide per column in chain (mutation version) order: the first step that references a column wins. If that step overwrites it, leave it skipped (its on-disk value is discarded there); if that step only consumes it as a function input, force its conversion (the consumer reads the on-disk value and needs the post-MODIFY type). A later consumer never matters: it reads the value the overwriting step already produced. Overwrite wins within a step, so the UPDATE self-consumption stays skipped. This keeps the LowCardinality fix (consumer step runs before the overwriter) and fixes the reverse ordering. Extends 04338 with a regression case. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Done. The ALIAS and ARRAY JOIN branches are not reachable in a mutation step's DAG, so I removed the handling and replaced it with assertions (your option b):
I verified both empirically by dumping the per-step action DAGs of the on-fly mutation chains in the tests: function arguments are always INPUT/FUNCTION/COLUMN, never ALIAS, and no ARRAY_JOIN node ever appears. Separately, the bot flagged a real over-forcing in the chain-wide consumed set; I made the exclusion order-aware in the same push (details in the inline reply). The new commit keeps the LowCardinality fix and adds a regression case for the reverse ordering. |
Pre-PR validation gate (commit eec7ef5, addressing the per-step ordering bug)
Session id: cron:clickhouse-worker-slot-2:20260615-191000 |
…rsion The previous order-aware skip used "overwrite wins within a step" so that the self-reference an UPDATE has on its target column did not force conversion. That tie-break was too broad: it also dropped a genuine same-step read of the column. MutationsInterpreter lowers UPDATE col = expr WHERE cond to _CAST(if(cond, _CAST(expr, type), col), type). The bare col argument of that if is a synthetic keep-old-value fallback (the value for unmatched rows) and must not force conversion. But col can also be read genuinely in the same step, for example UPDATE col = materialize(col): there col is consumed by materialize(col) whose action was built for the post-MODIFY type, so the on-disk value must still be converted. With overwrite winning, col stayed skipped and materialize ran on the on-disk type, aborting with LOGICAL_ERROR: Unexpected return type from materialize. Now the per-step scan locates the synthetic if node for each overwritten column (descend the assignment output through the named ALIAS and any outer _CAST wrappers; match if(cond, then, INPUT col) whose third argument is the bare input of that column) and ignores only that one fallback edge. Every other reference, in cond or in expr, counts as a genuine consume. Within a step a genuine consume now wins over the overwrite; across steps the first reference still decides, so the earlier overwrite-before-consumer case stays skipped. Adds a regression case (t_self_read_overwrite) to 04338_on_fly_mutation_read_overwritten_lc_source: UPDATE a = materialize(a) then MODIFY a LowCardinality(Nullable(String)), read with apply_mutations_on_fly = 1. It aborts before this change and passes after. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (commit 6271a23)
Session id: cron:clickhouse-worker-slot-1:20260615-212300 |
|
CI finished on
Stateless (incl. on-fly mutation / PREWHERE / LowCardinality), unit tests, AST/BuzzHouse fuzzers, builds: all green. |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 82/82 (100.00%) | Lost baseline coverage: none · Uncovered code |
Algunenano
left a comment
There was a problem hiding this comment.
Looks good. This is getting quite complex already but I hope that a larger refactor (#105947) will allow us to simplify things
27374cd
Related: #105847
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix
LOGICAL_ERROR: Unexpected return type from materialize(and similar type-mismatch aborts) whenapply_mutations_on_fly = 1is used on a table with a pending on-flyUPDATEwhose target column is also read as a function input by an earlier on-flyUPDATE, before anALTER MODIFY COLUMN ... LowCardinality(...)mutation.Description
Found by the server-side AST fuzzer (
Stress test (experimental, serverfuzz, arm_release)) on master, STID 1611-483a. Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=6a756730987624515ad9c08daab2b7831d343ba1&name_0=MasterCI&name_1=Stress%20test%20%28experimental%2C%20serverfuzz%2C%20arm_release%29This completes the on-fly mutation
LowCardinalityconversion fix from #105847, which handled the case where the overwritten column is not read by the query and explicitly left the per-step variants "to a follow-up".Reproducer
Root cause
On-fly mutation steps before a pending
ALTER MODIFY COLUMNare read withperform_alter_conversions = false, and every column the chain overwrites is exempted fromperformRequiredConversions(its on-disk value is about to be discarded by the UPDATE's own CAST, and pre-casting could fail on a value being replaced).The conversion of a column happens once, in the reader that first reads it from disk, but the value can be consumed as a function input by a later prewhere step. When the query reads both
banda, theUPDATE a = 'x'command survives filtering, soalands in the overwritten set and its conversion is skipped. TheUPDATE b = isNotNull(materialize(a))step's action, built against the post-MODIFYmetadata, then runsmaterialize(a)expectingLowCardinality(Nullable(String))but receives an on-diskNullable(String), and aborts.Fix
Compute, once per reader chain, the set of columns consumed as a
FUNCTION/ARRAY_JOINinput by any prewhere step's action DAG (walkingALIASchains back to theINPUTnode), and keep those columns out of the conversion skip set. A column that is only overwritten and never read by an action (e.g.vinUPDATE v = '100', the case #105847 added the skip for) still skips conversion, so that behavior is preserved.Adds a regression test
04338_on_fly_mutation_read_overwritten_lc_source.Version info
26.6.1.935