Skip to content

Fix LOGICAL_ERROR in on-fly mutation read when an UPDATE keep-old fallback is read under a non-constant condition - #108128

Merged
Algunenano merged 3 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-on-fly-mutation-if-return-type-keep-old-fallback
Jun 24, 2026
Merged

Algunenano merged 3 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-on-fly-mutation-if-return-type-keep-old-fallback

Conversation

@groeneai

@groeneai groeneai commented Jun 22, 2026 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fix a LOGICAL_ERROR ("Unexpected return type from if") when reading a column under apply_mutations_on_fly = 1 after an ALTER UPDATE col = ... WHERE <cond> with a non-constant or false condition followed by ALTER MODIFY COLUMN col <new type>.

Description

Follow-up to #107475, which fixed the materialize manifestation of this on-fly mutation read bug but introduced an if-return-type manifestation. After #107475 merged (2026-06-17), serverfuzz started hitting Unexpected return type from if. Expected UInt64. Got String on master (first occurrence on the merge commit itself; report e.g. https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108105&sha=06c39fa48d73772406026a2c33f5978583080896&name_0=PR for Stress test (amd_msan)).

Root cause: MutationsInterpreter lowers UPDATE col = expr WHERE cond to _CAST(if(cond, _CAST(expr, type), col), type), where type is the post-MODIFY type. The if node is declared at type and its third argument is the bare on-disk pre-MODIFY column (the value kept for unmatched rows). collectColumnsConsumedByChainActions excluded that keep-old fallback edge from forced conversion unconditionally. That is correct only when cond is constant-true: FunctionIf then returns the then-branch and never reads the fallback. For a non-constant or constant-false cond, FunctionIf does read the fallback, but the on-disk col is left in its pre-MODIFY type while the if expects the post-MODIFY type, so the executor aborts.

Minimal repro (crashes on-fly, returns correct results synchronously):

CREATE TABLE t (id UInt64, v String, b UInt8) ENGINE = MergeTree ORDER BY id;
INSERT INTO t SELECT number, toString(number * 10), number % 2 FROM numbers(10);
SYSTEM STOP MERGES t;
ALTER TABLE t UPDATE v = 100 WHERE id > 5 SETTINGS mutations_sync = 0, alter_sync = 0;
ALTER TABLE t MODIFY COLUMN v UInt64 SETTINGS mutations_sync = 0, alter_sync = 0;
SELECT v, b FROM t SETTINGS apply_mutations_on_fly = 1;

The fix ignores the keep-old fallback edge only when the if condition is a compile-time constant-true; for a non-constant, constant-false or NULL condition the fallback counts as a genuine consume and the column is converted to the post-MODIFY type. The cross-step ordering and the query PREWHERE skip are unchanged, so the cases that must stay skipped (an unconvertible on-disk value an UPDATE replaces under a constant-true condition) remain skipped. Extends 04338_on_fly_mutation_read_overwritten_lc_source with a regression case that aborts before this change and matches a synchronous UPDATE + MODIFY after it.

Version info

  • Merged into: 26.7.1.26
  • Backported to: 26.6.1.1188

…s not constant-true

MutationsInterpreter lowers UPDATE col = expr WHERE cond to
_CAST(if(cond, _CAST(expr, type), col), type), where type is the post-MODIFY
column type. The if node is declared at that type, and its third argument is the
bare on-disk pre-MODIFY column (the value kept for unmatched rows).

collectColumnsConsumedByChainActions excluded that fallback edge from forced
conversion unconditionally, on the premise that the kept-old value is discarded
and never read. That holds only when cond is constant-true: FunctionIf then
returns the then-branch and never touches the fallback. For a non-constant or
constant-false cond, FunctionIf does read the fallback, but the on-disk col is
left in its pre-MODIFY type while the if node expects the post-MODIFY type, so
the executor aborts with LOGICAL_ERROR: Unexpected return type from if (or a
FunctionIf type-equality assertion). A minimal repro:

  CREATE TABLE t (id UInt64, v String, b UInt8) ENGINE = MergeTree ORDER BY id;
  INSERT INTO t SELECT number, toString(number * 10), number % 2 FROM numbers(10);
  SYSTEM STOP MERGES t;
  ALTER TABLE t UPDATE v = 100 WHERE id > 5 SETTINGS mutations_sync = 0, alter_sync = 0;
  ALTER TABLE t MODIFY COLUMN v UInt64 SETTINGS mutations_sync = 0, alter_sync = 0;
  SELECT v, b FROM t SETTINGS apply_mutations_on_fly = 1;

This crashes on-fly while the same operation materialized synchronously returns
correct results.

The fix ignores the keep-old fallback edge only when the if condition is a
compile-time constant-true (helper ifConditionIsConstantTrue, which descends the
_CAST(..., UInt8) wrapper the mutation builder adds). For a non-constant,
constant-false or NULL condition the fallback counts as a genuine consume and the
column is converted to the post-MODIFY type. The cross-step ordering and the
query PREWHERE skip (perform_alter_conversions) are unchanged, so the
already-covered cases that must stay skipped (an unconvertible on-disk value an
UPDATE replaces under a constant-true condition) remain skipped.

Extends 04338_on_fly_mutation_read_overwritten_lc_source with a regression case
(t_keep_old_nonconst_cond): a partial UPDATE under a non-constant condition that
aborts before this change and matches a synchronous UPDATE + MODIFY after it.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate

# Question Answer
a Deterministic repro? Yes. CREATE TABLE t (id UInt64, v String, b UInt8) ENGINE=MergeTree ORDER BY id; INSERT SELECT number, toString(number*10), number%2 FROM numbers(10); SYSTEM STOP MERGES t; ALTER TABLE t UPDATE v = 100 WHERE id > 5 (mutations_sync=0); ALTER TABLE t MODIFY COLUMN v UInt64 (mutations_sync=0); SELECT v,b FROM t SETTINGS apply_mutations_on_fly=1; aborts every time. WHERE 0 reproduces it too.
b Root cause explained? The on-fly mutation step DAG is _CAST(if(cond, _CAST(expr, T), col), T), T = post-MODIFY type. The if is declared at T and its 3rd arg is the bare on-disk pre-MODIFY col. collectColumnsConsumedByChainActions excluded that fallback edge from forced conversion unconditionally. That holds only when cond is constant-true (FunctionIf returns the then-branch, never reads the fallback). For a non-constant or constant-false cond, FunctionIf reads the fallback, but col is left pre-MODIFY-typed while the if expects T, so the executor aborts (Unexpected return type from if, or a FunctionIf type-equality assertion).
c Fix matches root cause? Yes. The fallback edge is now ignored only when the if condition is a compile-time constant-true (ifConditionIsConstantTrue, descending the _CAST(..., UInt8) wrapper). For non-constant/constant-false/NULL conditions the fallback counts as a genuine consume, so the column is converted to T. This is exactly the mechanism from (b), not a symptom guard.
d Test intent preserved / new tests added? Yes. 04338_on_fly_mutation_read_overwritten_lc_source gains t_keep_old_nonconst_cond (partial UPDATE under a non-constant condition). All 6 existing sections still pass byte-for-byte, including the two that must stay skipped (t_overwrite_then_prewhere, t_overwrite_before_consume with unconvertible on-disk 'x').
e Both directions demonstrated? Yes (Build-ID verified). Pre-fix binary (678e02e3): the new section ABORTS (FunctionIf::executeGeneric type-equality assertion). Post-fix binary (2ad044f0): full test matches reference; the repro returns 0,10,20,30,40,50,100,100, identical to the same operation materialized synchronously (mutations_sync=2).
f Fix is general, not a narrow patch? Yes. The fix is at the consumed-set classification (the origin of the wrong skip decision), not a guard at the crash site. It covers every condition regime: constant-true (skip, preserved), constant-false / NULL / non-constant (convert). Verified across WHERE 0, WHERE NULL, WHERE id>5, and the materialize(a) self-read (still converted via its genuine edge). No sibling code path has the same skip logic; this function is the single place the decision is made.

Found by serverfuzz on master after #107475 merged (first hit on the merge commit). Follow-up to #107475; same on-fly mutation family as #105947.

Session id: cron:clickhouse-worker-slot-0:20260622-122600

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @Algunenano @CurtizJ - this is the follow-up to #107475: that PR fixed the materialize manifestation but its unconditional skip of the synthetic if keep-old fallback introduced an if-return-type manifestation that serverfuzz began hitting on master right after the merge (first hit on the merge commit). The fix only skips the fallback edge when the UPDATE condition is constant-true, so a non-constant or false condition now converts the on-disk column to the post-MODIFY type. Same on-fly mutation family as #105947.

@Algunenano Algunenano added the can be tested Allows running workflows for external contributors label Jun 22, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 22, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2f2e802]

Summary: ✅


AI Review

Summary

This PR fixes on-fly mutation reads after an UPDATE followed by ALTER MODIFY COLUMN by treating the if(cond, expr, col) keep-old fallback as a real consume unless cond is compile-time constant true. The current code and regression coverage match that contract, and the prior inline comment about the stale header invariant has been addressed.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 22, 2026
…tant-true-only skip

The keep-old fallback edge of an UPDATE's if(cond, expr, col) is no longer
ignored unconditionally. After the constant-true gate added in this PR, the
fallback counts as a genuine consume except when cond is a compile-time
constant-true, so the member declaration's header comment in
MergeTreeReadersChain.h stated a stale invariant. This aligns the comment with
the code in collectColumnsConsumedByChainActions. Comment-only, no behavior
change.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
The amd_tsan flaky check on ea60fcf was cancelled at the 6h GitHub
Actions wall after the test harness wedged during MinIO docker setup
(Restarting clickminio), before any test ran. The prior commit
63bb1e1 ran the same flaky check green in ~10 min (04338 50x OK,
avg 5.6s). This empty commit re-triggers a fresh CI run.
@clickhouse-gh

clickhouse-gh Bot commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.30% 85.40% +0.10%
Functions 92.60% 92.60% +0.00%
Branches 77.60% 77.60% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 16/17 (94.12%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI is finished green on 2f2e802: Finish Workflow, Mergeable Check, Style check, all builds, fuzzers, stateless (incl. the previously-canceled amd_tsan flaky check, now green), integration, and stress jobs all pass. No test failures. CH Inc sync is the fork-mirror search bot, not a real CI check. Ready for review.

@Algunenano Algunenano self-assigned this Jun 23, 2026
@Algunenano
Algunenano added this pull request to the merge queue Jun 24, 2026
Merged via the queue into ClickHouse:master with commit 69d2346 Jun 24, 2026
167 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 24, 2026
@robot-clickhouse robot-clickhouse added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Jun 24, 2026
robot-ch-test-poll3 added a commit that referenced this pull request Jun 24, 2026
Cherry pick #108128 to 26.6: Fix LOGICAL_ERROR in on-fly mutation read when an UPDATE keep-old fallback is read under a non-constant condition
robot-clickhouse added a commit that referenced this pull request Jun 24, 2026
…hen an UPDATE keep-old fallback is read under a non-constant condition
@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Jun 24, 2026
clickhouse-gh Bot added a commit that referenced this pull request Jun 24, 2026
Backport #108128 to 26.6: Fix LOGICAL_ERROR in on-fly mutation read when an UPDATE keep-old fallback is read under a non-constant condition
alexey-milovidov added a commit that referenced this pull request Jun 24, 2026
Add changelog entries for PRs that landed on the 26.6 release line after
the first pass: #82414, #108042 (New Feature), #107428 (Improvement),
and #108128, #108288, #100205, #108029 (Bug Fix).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…y-mutation-if-return-type-keep-old-fallback

Fix LOGICAL_ERROR in on-fly mutation read when an UPDATE keep-old fallback is read under a non-constant condition
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Add changelog entries for PRs that landed on the 26.6 release line after
the first pass: ClickHouse#82414, ClickHouse#108042 (New Feature), ClickHouse#107428 (Improvement),
and ClickHouse#108128, ClickHouse#108288, ClickHouse#100205, ClickHouse#108029 (Bug Fix).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.6-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants