Repository navigation
Evaluate a DEFAULT column from the patched value of the column it reads - #118630
alexey-milovidov wants to merge 56 commits into
Conversation
With `a UInt32 DEFAULT 0` and `z UInt32 DEFAULT a + 1000` both added by `ALTER TABLE ... ADD COLUMN` after the part was written, `UPDATE t SET a = 5` followed by `SELECT a, z` returned `5, 1000`, while the equivalent `ALTER TABLE ... UPDATE a = 5` returns `5, 1005`. A column the part does not store comes into existence only when the missing defaults are evaluated, so a patch part can be applied to it only after that pass. Its dependent `DEFAULT` columns were computed during that pass, from the value the default gave rather than from the patched one. `MergeTreeReadersChain` now evaluates the defaults once more after applying the patches, for the columns that pass produced and the patches did not. The same query without the updated column in the select list (`SELECT z`) read no column at all: `injectRequiredColumnsRecursively` adds the columns a `DEFAULT` expression reads only when the part stores them, so the patch that supplies the value was not even selected for the read task, and the expression was evaluated with the column's own default substituted into it. A column that a patch updates is now read even when the part does not store it. Closes: #117023 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Workflow [PR], commit [7a583f3] Summary: ❌
AI ReviewSummaryThe PR corrects missing Findings❌ Blockers
Tests
Missing context / blind spots
Final VerdictStatus: ❌ Block Keep shared initial defaults consistent when the reevaluation gate is false, and add focused regression coverage for reads and merges. |
|
Confirmed and reproduced, and it needs CREATE TABLE t (x UInt32) ENGINE = MergeTree ORDER BY x
SETTINGS enable_block_number_column = 1, enable_block_offset_column = 1;
INSERT INTO t SELECT number FROM numbers(6);
ALTER TABLE t ADD COLUMN a UInt32 DEFAULT 0, ADD COLUMN w UInt32 DEFAULT 7;
ALTER TABLE t ADD COLUMN z UInt32 DEFAULT a + 1000;
SYSTEM STOP MERGES t;
ALTER TABLE t UPDATE w = 8 WHERE 1 SETTINGS alter_sync = 0; -- stays pending
UPDATE t SET a = 5 WHERE 1; -- a later patch
SELECT a, z, w FROM t; -- 5, 1005, 7 (the mutation is not applied)
SELECT a, z, w FROM t SETTINGS apply_mutations_on_fly = 1; -- 5, 1000, 8 (z is stale)So the patch for I would rather not fold the fix into this pull request. Closing that chain means re-evaluating the dependents after Filtering I will send the follow-up as a separate pull request unless you would rather have it here. |
…at a step boundary A lightweight `UPDATE` issued after a mutation that is still pending is applied only at the boundary of the reader step that applies the mutation on the fly (`applyPatchesAfterReader`), after that step has already evaluated its missing `DEFAULT` columns. A `DEFAULT` that reads the patched column therefore kept the value the default gave: for `a DEFAULT 0, z DEFAULT a + 1000, w DEFAULT 7`, a pending `ALTER TABLE ... UPDATE w = 8` and a later `UPDATE t SET a = 5`, `SELECT a, z, w SETTINGS apply_mutations_on_fly = 1` returned `5, 1000, 8`. `ReadResult` now carries the names of the columns whose value is, so far, the one their `DEFAULT` expression gave (`columns_filled_by_defaults`). A column leaves the set when a patch is applied to it (`applyPatches` reports the columns it did patch, within the step's version window, which also answers the review remark about `columns_patched_after_defaults`) or when a step's action recomputes it (an on-fly `UPDATE z = ...` must keep the value it assigned, as it does once the mutation has materialized the column). After every `applyPatchesAfterReader` pass that patched something, the remaining columns of the set are evaluated again from the accumulated block (`IMergeTreeReader::evaluateDefaults`). An on-fly mutation step projects the columns the query did not ask for out of the result into `additional_columns`; the patches are applied to them there as well, and the last step keeps them, so that `SELECT z, w` sees the patched `a` through them. Test `05233_lwu_default_pending_mutation_later_patch` covers both shapes and the overwriting mutation, and compares the on-the-fly result against the one read after the mutation has materialized. Co-Authored-By: Claude Fable 5.1 <[email protected]>
… has patches Without patch parts nothing is ever applied at a step boundary, so there is no pass to evaluate the columns again and the set would only be filled and dropped. Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
🕵 The follow-up I mentioned above (dependents of a column patched at a step boundary, i.e. a lightweight |
|
@alexey-milovidov This PR reworks the functions where the abort below happens, so I would rather ask than open something that collides with it. Both tests here use a pending CREATE TABLE r2d (a UInt64, v String, w String) ENGINE = MergeTree ORDER BY a
SETTINGS enable_block_number_column = 1, enable_block_offset_column = 1;
INSERT INTO r2d SELECT number, 'foo', 'w0' FROM numbers(10);
ALTER TABLE r2d ADD COLUMN b UInt64, MODIFY ORDER BY (a, b);
ALTER TABLE r2d MODIFY COLUMN b UInt64 DEFAULT length(w);
SYSTEM STOP MERGES r2d;
SET mutations_sync = 0, alter_sync = 0;
UPDATE r2d SET v = 'bar' WHERE a >= 5; -- v2 (MergeOnKey) patch part
ALTER TABLE r2d MODIFY COLUMN w FixedString(4); -- left pending
SELECT a, v, w, b FROM r2d ORDER BY a;
Describing that block truthfully removes the abort, but it also fixes which schema epoch a v2 patch part's stored sorting key is computed in, and the two epochs give different key values for the same row. Why I read that as a policy choice rather than a local fixA patch written before the pending
Separately, the same fixture drops a patch with no type change at all: with Which of these do you want?
I have the repro, the probe matrix (both patch orderings, mixed reads, the mutation path, the v1 control) and a plan for routes 1 to 3; my preference is 3. Related: #119540, whose documented residual points here. |
…hen needed After the last reader step, `executePrewhereActionsAndFilterColumns` stored every column the step projected out in `additional_columns` for all queries, which `ReadResult::applyFilter` then filtered again. They are needed there only by `MergeTreeReadersChain::applyPatchesAfterReader` to evaluate a `DEFAULT` column once more, i.e. when there are patch parts and some column of the result still holds its `DEFAULT` value. Keep them only in that case, restoring the previous behavior for all other queries. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The "overwriting mutation materialized" step issued `UPDATE z = z WHERE 0` to wait for the pending `UPDATE z = 3`. `StorageMergeTree` may squash both into one mutation whose version is above the lightweight `UPDATE`, so the patched `a` was applied before `z` was materialized (`1007`/`1009` instead of `1000`). Drop that step; the on-the-fly check above it is the regression test. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118630&sha=431703659e536c627f6140a3683ad2ab23e19377&name_0=PR&name_1=Stateless%20tests%20%28amd_tsan%2C%20flaky%20check%29 #118630 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
🕵 @groeneai This is not in scope of this PR (so not option 4), and fixing it separately will not collide with it. This PR only re-evaluates |
|
Filed as #121943 with the repro, the probe matrix and the three routes, and asked the patch parts owner there for the epoch call. Thanks for the scope ruling; I will keep it out of this PR. |
…tched column After a patch part overwrote a column, every `DEFAULT` column the step had filled was evaluated again, including ones unrelated to the patched column. That evaluated a non-deterministic `DEFAULT` such as `rand()` twice and did needless work. Add `IMergeTreeReader::getDefaultsDependingOn`, which returns the candidates whose `DEFAULT` expression reads a changed column, directly or through another `DEFAULT` column the part does not store, and use it in both re-evaluation passes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 Merged master and pushed 5f2b9d2, which addresses the last open AI review thread (only @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118630&sha=0ebcb3fdc7d6e72f7e16c93a32c4bf7ecbe675aa&name_0=PR&name_1=Upgrade%20check%20%28amd_release%29 and provide a fix in a separate PR. If the fix is already in progress, link it here. |
|
Same failure as on #119307, and it is already fixed, so there is no new PR from me. The run on |
…shared intermediate `evaluateDefaultsInRowsFilledByDefaults` added every requested candidate reading a missing intermediate evaluated again, bypassing the sort-key exclusion of `getDefaultsDependingOn`. With `k DEFAULT b`, `b DEFAULT a + 100` and `z DEFAULT a + b`, a lightweight `UPDATE` of `a` changed `k` when `z` was requested, breaking the order of ordered reads. Add `IMergeTreeReader::isSortingKeyColumn` and skip sort-key columns in that expansion. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ULT` reads A sort-key column the part does not store keeps the value its `DEFAULT` gave before patches. When it was not requested, a `DEFAULT` column reading it was evaluated again after a lightweight `UPDATE` through an intermediate computed anew from the patched input, so `SELECT z` gave a value different from `SELECT a, k, z`. Inject such a column into the read columns when the part has patches, so it is filled once, before the patches, as if it was requested. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…olumn When the sorting key reads a subcolumn (`ORDER BY (id, k.x)`), the list of columns required for the sorting key holds `k.x`, while the injection of a missing sort-key column read by a `DEFAULT` compared it with the storage name `k`, so `k` was not retained and a re-evaluated `z DEFAULT a + k.1` depended on whether `k` was selected. Both the injection and the re-evaluation exclusion now use one set of sort-key storage names from `getSortingKeyColumnsInStorage`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… evaluation `k UInt64 DEFAULT b` in the sorting key keeps the value its `DEFAULT` gave, while `z DEFAULT a + b` is evaluated again after a patch to `a`. With `b DEFAULT rand64()` not stored and not requested, the evaluation after the patch synthesized a new `b`, so `z = k + a` no longer held in a row. `ReadResult::default_intermediates` now keeps the missing columns evaluated from their `DEFAULT` while patch parts are read. A later evaluation, the one after patches or the one of a later step (including the sort-key columns evaluated before the patches of format `v2`), reads these values instead of synthesizing new ones. A column leaves the set when a column its `DEFAULT` reads is patched or recomputed by a step, as its value is then stale and it is evaluated anew. New test `05617_lwu_default_sorting_key_consumer_of_intermediate` covers patch formats `v1` and `v2` and the pending-mutation step boundary. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 Merged the latest State on
|
…ion step as read An on-fly mutation step projects its inputs: after it, the block holds only the outputs of its actions. `getReadTaskColumns` still recorded every column read in earlier steps as available, so a subcolumn `b.x` injected to evaluate `z DEFAULT b.x + 1` (because a patch part updates `b`) was not read again by the last step, while the mutation step kept only its parent `b`. A query selecting `b.x` then threw `NOT_FOUND_COLUMN_IN_BLOCK`. Mark `PrewhereExprStep::project_inputs` for such steps and, after one, keep as available exactly the outputs of its actions. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118630&sha=5079c89beb126a043700a021209509d83c3cf0f7&name_0=PR&name_1=La%20Casa%20Del%20Dolor%20%28amd_tsan%29 and provide a fix in a separate PR. If the fix is already in progress, link it here. It is a ThreadSanitizer data race reported in the BuzzHouse client process ( |
|
This is the client-side race fixed by #131177 (merged 2026-11-13 09:14 UTC), so no new PR is needed. Symbolized with this run's binary (BuildId This run's tree predates the fix; the current head |
…mutation step A subcolumn `b.x` read to evaluate `z DEFAULT b.x + 1` is patched in the first step and then projected out by an on-fly mutation step that keeps only its parent `b`. The last step reads `b.x` from the disk again, but `ReadResult::patch_versions_block` still held the versions of the earlier, already patched instance, so the patch skipped its rows and the query returned the old value of `b.x` next to the patched `b`. Forget the recorded versions of the columns read in a step: they are new instances of the data, just read from the disk. Add a case with `b` stored in the part to `05768_lwu_default_injected_subcolumn_mutation_on_fly`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 State on |
Ergus
left a comment
There was a problem hiding this comment.
Two comments here:
-
the change looks extremely long and complicated. Are we sure that we are not overenginering here?
-
I see some red in the CI for perf test. DO we really need to accept that?
… may change their inputs The state kept to evaluate a `DEFAULT` column again after patch parts are applied (`columns_filled_by_defaults`, `rows_filled_by_defaults`, `default_intermediates` and the columns projected out by the last step) was kept whenever the part had patches, even if no `DEFAULT` expression reads a patched column. `ReadResult::applyFilter` and `shrink` then filtered the missing `DEFAULT` columns a second time in every `PREWHERE` step. `MergeTreeReadersChain::mayReevaluateDefaultsAfterPatches` now checks, once per chain and only when the part lacks a `DEFAULT` column, whether a patch part may overwrite a column that the `DEFAULT` expression of a missing column reads. If not, `getDefaultsDependingOn` can return nothing, so none of that state is kept and the patches are applied as before this change. Also skip the loop erasing stale patch versions when `patch_versions_block` is empty: it built a version column name for every column of every step and block, including in queries without patches. Add the performance test `apply_patch_parts_unrelated_defaults` for a `PREWHERE` query reading many missing `DEFAULT` columns that do not read the patched column. Related: #118630 Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
🕵 @Ergus, on the two points of your review of 2026-11-20 (both inline threads are answered and fixed in 7a583f3, which also merges the latest
|
|
🕵 @Ergus, I resolved the thread about the bookkeeping cost for unrelated |
| addDummyColumnWithRowCount(additional_columns, result.num_rows); | ||
| range_reader.getReader()->evaluateMissingDefaults(additional_columns, columns); | ||
| range_reader.getReader()->evaluateMissingDefaults( | ||
| additional_columns, columns, mayReevaluateDefaultsAfterPatches() ? &result.default_intermediates : nullptr); |
There was a problem hiding this comment.
[Blocker] Preserve first-pass default intermediates when reevaluation is unnecessary
A false result from mayReevaluateDefaultsAfterPatches now also disables retaining the initial default values. Those values are needed even when no default reads a patched column: executeSortingKeyExpressions evaluates missing defaults to obtain the key, retains only the key, and the ordinary default pass subsequently evaluates the other columns. With this gate disabled, the two passes generate different values for the same missing intermediate.
Reproduced with the 7a583f3089f8 Fast test binary:
SET allow_experimental_lightweight_update = 1;
CREATE TABLE t (id UInt32) ENGINE = MergeTree ORDER BY id
SETTINGS enable_block_number_column = 1, enable_block_offset_column = 1,
apply_patches_on_merge = 0, patch_parts_version = 'v2';
INSERT INTO t VALUES (0), (1);
SYSTEM STOP MERGES t;
ALTER TABLE t ADD COLUMN a UInt64 DEFAULT 5;
ALTER TABLE t ADD COLUMN b UInt64 DEFAULT rand64();
ALTER TABLE t ADD COLUMN k UInt64, MODIFY ORDER BY (id, k);
ALTER TABLE t MODIFY COLUMN k UInt64 DEFAULT b;
ALTER TABLE t ADD COLUMN z UInt64 DEFAULT b;
UPDATE t SET a = 5 WHERE 1;
SELECT id, z = k FROM t ORDER BY id;
SELECT id, a, z = k FROM t ORDER BY id;
ALTER TABLE t MODIFY SETTING apply_patches_on_merge = 1;
SYSTEM START MERGES t;
OPTIMIZE TABLE t FINAL;
SELECT id, z = k, b = k FROM t ORDER BY id SETTINGS apply_patch_parts = 0;The first query returns equality 1 for both rows; adding the unrelated patched a makes it 0. The final query returns both equalities as 0, so the merge persists the inconsistency. The previous PR head, 1b820eb47d69, returns 1 throughout, as does the current binary before the update.
Please separate retention needed across initial default passes from the reevaluation bookkeeping, retaining shared inputs when a key is filled early even if the predicate is false. Add this gate-disabled case to the regression coverage, including the merged result.
AI review of a later commit does not approve the PR, see the AI Review comment.
Closes: #117023
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a stale read after a lightweight
UPDATEof a column that aDEFAULTcolumn depends on, when neither column is stored in the data part.Description
With
a UInt32 DEFAULT 0andz UInt32 DEFAULT a + 1000both added byALTER TABLE ... ADD COLUMNafter the part was written,UPDATE t SET a = 5followed bySELECT a, zreturned5, 1000, while the equivalentALTER TABLE ... UPDATE a = 5returns5, 1005.A column the part does not store comes into existence only when the missing defaults are evaluated, so a patch part can be applied to it only after that pass (
ColumnForPatch::Order::AfterEvaluatingDefaults). Its dependentDEFAULTcolumns were computed during that pass, from the value the default gave rather than from the patched one.MergeTreeReadersChainnow evaluates the defaults once more after applying the patches, for the columns that pass produced and the patches did not; the columns a patch produced keep their patched values.The same query without the updated column in the select list (
SELECT z) read no column at all:injectRequiredColumnsRecursivelyadds the columns aDEFAULTexpression reads only when the part stores them, so the patch that supplies the value was not even selected for the read task and the expression was evaluated with the column's own default substituted into it. A column that a patch updates is now read even when the part does not store it.Both shapes, and a chain of dependent columns, are covered by
05136_lwu_default_depends_on_missing_updated_column, which compares the lightweightUPDATEagainst the heavyALTER TABLE ... UPDATEon the same data.A mutation that is still pending is applied on the fly by a reader step of its own, and a lightweight
UPDATEissued after it is applied only at the boundary of that step (applyPatchesAfterReader), after the step has evaluated itsDEFAULTcolumns.ReadResultnow carries the names of the columns whose value is still the one theirDEFAULTgave; a column leaves the set when a patch overwrites it or a step's action recomputes it, and after every boundary pass that patched something the rest are evaluated again from the accumulated block (IMergeTreeReader::evaluateDefaults). The patches are also applied to the columns a mutation step projected out intoadditional_columns, which the last step keeps for that purpose.05233_lwu_default_pending_mutation_later_patchcovers a pendingALTER TABLE ... UPDATEplus a later lightweightUPDATEread withapply_mutations_on_fly = 1, in both select-list shapes and with a mutation that overwrites the dependent itself, and compares each on-the-fly result with the one read after the mutation has materialized.Which rows of a
DEFAULTcolumn a patch overwrote is tracked row by row (ReadResult::rows_filled_by_defaults), so the result does not depend on the read block size: only the rows that still hold the value of theDEFAULTare evaluated again, and a dependent column with patched rows of its own is evaluated again from the combined value of the column it reads. A missing sort-key column is the exception: the rows of the part are sorted by the value itsDEFAULTgave, so it keeps that value (evaluating it again would change the key without reordering the rows and break ordered reads and merges, see #129600). Whether the part stores a column follows the reader's rules for pendingRENAME COLUMNandDROP COLUMN, so a column dropped by a pending mutation and added again with a newDEFAULTcounts as missing. These cases are covered by05332_lwu_default_sorting_key_column,05394_lwu_default_partially_patched_rows,05395_lwu_default_dependent_with_patched_rowsand05396_lwu_default_pending_drop_readd.Workflow [PR]
Sync PR [sync-upstream/pr/118630]