Skip to content

Fence the MATERIALIZED recompute stages to the mutation version they belong to - #115372

Merged
tiandiwonder merged 3 commits into
masterfrom
fix-materialized-recompute-stage-version
Sep 4, 2026
Merged

tiandiwonder merged 3 commits into
masterfrom
fix-materialized-recompute-stage-version

Conversation

@tiandiwonder

@tiandiwonder tiandiwonder commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

Related: #103182

Problem

While a mutation is pending, the value of a MATERIALIZED column can depend on which columns the
query selects:

CREATE TABLE t (id UInt64, x Int32, z Int32, m1 Int32 MATERIALIZED x + z, m2 Int32 MATERIALIZED m1 + 1)
ENGINE = MergeTree ORDER BY id
SETTINGS enable_block_number_column = 1, enable_block_offset_column = 1;

INSERT INTO t (id, x, z) VALUES (1, 10, 100);
SYSTEM STOP MERGES t;

SET alter_sync = 0, mutations_sync = 0, enable_lightweight_update = 1, apply_patch_parts = 1;
ALTER TABLE t UPDATE x = 20 WHERE 1;   -- stays pending
SET apply_mutations_on_fly = 0;
UPDATE t SET z = 500 WHERE 1;          -- lightweight update, creates a patch part
SET apply_mutations_on_fly = 1;
SELECT x, z, m1, m2 FROM t;    -- 20 500 520 521
SELECT m1 FROM t;              -- 520
SELECT m2 FROM t;              -- 521

The expected result is 20 500 510 511: m1 in the patch part was computed from x = 10, the value
visible when the lightweight update ran, and the pending UPDATE x = 20 precedes the patch. Instead the
recompute takes the patched z = 500 into account and returns 20 + 500.

Measured on master 26bd12dc68f (which already contains the revert of #103182) on 2026-08-25.

The version-less recompute stages come from 5d164cab620 (#110648, 2026-07-16) and are absent from
26.7, the newest release branch, so no released version is affected. Note that
Stage::mutation_version itself is older and is in 26.7 (a5327c66fe8); what is new is the stages
that fail to carry it.

Root cause

After an UPDATE, MutationsInterpreter::prepare emits one stage per dependency level to recompute
the MATERIALIZED columns derived from the updated ones (emit_materialized_recompute_stages). Those
stages were created with a plain stages.emplace_back(context), leaving Stage::mutation_version
unset, while the UPDATE's own stage carries command.mutation_version (via addStageIfNeeded).

On-fly reads derive the patch-visibility window from consecutive stages' versions
(AlterConversions::getMutationSteps, MergeTreeReadersChain), so a version-less stage leaves that
window unbounded above: a patch part created after the pending UPDATE becomes visible to the
recompute stages, which then recompute m1 from the patched z and override the value the patch
carries itself.

Which reads are affected has shifted as the surrounding code moved: when this pull request was opened
only the full-row read was wrong while SELECT m2 alone still agreed with the patch, so the symptom
was a disagreement between two ways of reading the same data. On master today all three reads take the
patch into account and are wrong together. The defect and the fix are unchanged; only the shape of the
symptom is.

Solution

Pass the command's version into the recompute stages, so they are fenced exactly like the stage of the
command they belong to.

The patch-materialization and CLEAR COLUMN call sites keep the current behaviour (std::nullopt):
the stages they follow carry no version either, and choosing a version for them is a separate
question, out of scope here.

Regression test 04927_materialized_recompute_stage_mutation_version: all three of its reads return
the patched-in values on master (20 500 520 521 / 520 / 521) and the patch's own values after this
change (20 500 510 511 / 510 / 511). The negative half was measured on master; the positive half
is covered by this pull request's CI run, where the test passes in every stateless lane including five
50-run flaky checks.

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Workflow [PR]
Sync PR [sync-upstream/pr/115372]

Version info

  • Merged into: 26.9.1.682 (included in 26.9 and later)

The per-level stages that recompute MATERIALIZED columns after an
`UPDATE` were created without a mutation version. On-fly reads derive the
patch-visibility window from consecutive stages' versions, so a
version-less stage leaves that window unbounded above: a patch part
created after the pending `UPDATE` becomes visible to the recompute,
which then recomputes the derived column from the patched value and
overrides the value the patch itself carries.

The result is that the value of a MATERIALIZED column depends on which
columns the query selects. For `m1 MATERIALIZED x + z`,
`m2 MATERIALIZED m1 + 1` with a pending `UPDATE x` and a later
lightweight `UPDATE z`:

    SELECT m2 FROM t              -> 511   (m1 = 510, from the patch)
    SELECT x, z, m1, m2 FROM t    -> 20 500 520 521

Pass the command's version into the recompute stages, so they are fenced
exactly like the stage of the command they belong to. The patch and
`CLEAR COLUMN` call sites keep the current behaviour: the stages they
follow carry no version either, and giving them one is a separate
question.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6f166c8]

Summary: ❌

job_name test_name status info comment
Stress test (amd_tsan) FAIL
ThreadSanitizer (STID: 3538-42a8) FAIL cidb
LLVM Coverage FAIL
Generate LLVM Coverage Diff Report FAIL cidb
Print Uncovered Code FAIL cidb Uncovered code analysis did not run: bash ci/jobs/scripts/generate_diff_coverage_report.sh failed (its output is on the Generate LLVM Coverage Diff Report result).
Parser memory check ERROR
Resolve master binary ERROR

AI Review

Summary

This PR threads the owning mutation_version into the dependency-ordered MATERIALIZED recompute stages emitted for UPDATE, so on-fly reads fence patch visibility to the mutation they belong to instead of letting later patch parts leak into the recomputation. I did not find a correctness, compatibility, or test-coverage issue in the current diff, and the added stateless regression test exercises the full-row read plus the m1/m2 projections that previously diverged.

Missing context / blind spots
  • ⚠️ The linked Praktika report for https://github.com/ClickHouse/ClickHouse/pull/115372 is expired (403), so I could not independently confirm the cited CI reruns and reviewed this change from the current code, test, and GitHub discussion instead.
Final Verdict

✅ No new findings.

LLVM Coverage Report

⚠️ No coverage measurement for commit 6f166c8: bash ci/jobs/scripts/generate_diff_coverage_report.sh failed (its output is on the Generate LLVM Coverage Diff Report result).

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Aug 18, 2026
…by another replica

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 6f166c8e4 with master 84ac4b282 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of bb3c0a852; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 18 s compile time in total, 7 of them have a recent master baseline.

Job report

@tiandiwonder tiandiwonder changed the title Fix MATERIALIZED column value depending on the selected columns while a mutation is pending Fence the MATERIALIZED recompute stages to the mutation version they belong to Aug 25, 2026

@CurtizJ CurtizJ left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@tiandiwonder
tiandiwonder added this pull request to the merge queue Sep 4, 2026
Merged via the queue into master with commit 48a638b Sep 4, 2026
172 of 175 checks passed
@tiandiwonder
tiandiwonder deleted the fix-materialized-recompute-stage-version branch September 4, 2026 10:40
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 4, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

@tiandiwonder @CurtizJ — ClickGap found the following in this PR:

Close anything that's wrong or already addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants