Repository navigation
Fence the MATERIALIZED recompute stages to the mutation version they belong to - #115372
Conversation
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]>
|
Workflow [PR], commit [6f166c8] Summary: ❌
AI ReviewSummaryThis PR threads the owning Missing context / blind spots
Final Verdict✅ No new findings. LLVM Coverage Report |
…by another replica Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
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, 18 s compile time in total, 7 of them have a recent master baseline. |
|
@tiandiwonder @CurtizJ — ClickGap found the following in this PR:
Close anything that's wrong or already addressed. |
Related: #103182
Problem
While a mutation is pending, the value of a
MATERIALIZEDcolumn can depend on which columns thequery selects:
The expected result is
20 500 510 511:m1in the patch part was computed fromx = 10, the valuevisible when the lightweight update ran, and the pending
UPDATE x = 20precedes the patch. Instead therecompute takes the patched
z = 500into account and returns20 + 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 from26.7, the newest release branch, so no released version is affected. Note thatStage::mutation_versionitself is older and is in26.7(a5327c66fe8); what is new is the stagesthat fail to carry it.
Root cause
After an
UPDATE,MutationsInterpreter::prepareemits one stage per dependency level to recomputethe
MATERIALIZEDcolumns derived from the updated ones (emit_materialized_recompute_stages). Thosestages were created with a plain
stages.emplace_back(context), leavingStage::mutation_versionunset, while the
UPDATE's own stage carriescommand.mutation_version(viaaddStageIfNeeded).On-fly reads derive the patch-visibility window from consecutive stages' versions
(
AlterConversions::getMutationSteps,MergeTreeReadersChain), so a version-less stage leaves thatwindow unbounded above: a patch part created after the pending
UPDATEbecomes visible to therecompute stages, which then recompute
m1from the patchedzand override the value the patchcarries 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 m2alone still agreed with the patch, so the symptomwas 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 COLUMNcall 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 returnthe patched-in values on master (
20 500 520 521/520/521) and the patch's own values after thischange (
20 500 510 511/510/511). The negative half was measured on master; the positive halfis 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):
Workflow [PR]
Sync PR [sync-upstream/pr/115372]
Version info
26.9.1.682(included in26.9and later)