Repository navigation
Warn when CLEAR COLUMN leaves an EPHEMERAL-derived MATERIALIZED column stale, and cover it with a test - #115371
Conversation
…ved MATERIALIZED column
`CLEAR COLUMN` recomputes the MATERIALIZED columns that read the cleared
column. The recompute set was built by inserting *every* MATERIALIZED
column of the table once the closure of the cleared columns was found to
be non-empty, so it also picked up columns whose default reads an
EPHEMERAL column. Those cannot be evaluated outside INSERT — the
dependency analysis above deliberately skips them — and recomputing one
fails the whole mutation:
Code: 341. Exception happened during execution of mutation
'mutation_2.txt' with part 'all_1_1_0' reason: 'Code: 47.
Missing columns: 'e' while processing: 'x + e'
The mutation then stays in the queue retrying, so a synchronous `ALTER`
either throws or waits forever, and the columns that should have been
recomputed keep their stale values.
Use the closure itself as the recompute set. It contains exactly the
MATERIALIZED columns reachable from a cleared column and, because the
analysis skips EPHEMERAL-derived ones, never contains a column that
cannot be recomputed.
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
|
Workflow [PR], commit [d163946] Summary: ✅
AI ReviewSummaryThis PR keeps the Findings
Final VerdictNeeds one test fix before merge: the new LLVM Coverage ReportMeasured on commit d163946.
Changed lines: Changed C/C++ lines covered: 25/25 (100.00%) · Uncovered code |
The first version narrowed the recompute set to the dependency closure of the cleared columns. That breaks an existing guarantee: a metadata-only `MODIFY COLUMN ... MATERIALIZED` schedules no mutation, so `CLEAR COLUMN` is what refreshes the stored values of a column whose expression changed, even when that column does not read the cleared one. `04546_ttl_delete_where_recalc_after_update` pins exactly that behaviour. Keep the original set and only drop the columns that cannot be recomputed outside `INSERT` — the ones whose expression reads an `EPHEMERAL` column, which the dependency analysis already skips for the same reason. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
`MaterializedColumnDependencies`, added by #114561, already computes for every MATERIALIZED column whether its expression reads an EPHEMERAL one, and the dependency analysis skips those columns through it. That replaced the loop this branch had been collecting the same information in, hence the conflict. Resolved by dropping this branch's own detection and asking the shared node instead, so the fix is one condition on `clear_affected_materialized`. Keeping it there rather than in `emit_materialized_recompute_stages` preserves the documented invariant that the set stays equal to what the recompute writes. The error the fix prevents also changed with #114561: `Missing columns: 'e' while processing 'x + e'` (`UNKNOWN_IDENTIFIER`) is now `There is no column or subcolumn e in table` (`NO_SUCH_COLUMN_IN_TABLE`). Test comment updated to match. Co-Authored-By: Claude Opus 5 <[email protected]>
`CLEAR COLUMN` was the only mutation not using the dependency closure. `UPDATE` and `APPLY PATCHES` recompute `affected_materialized_closure(changed_columns)`; `CLEAR COLUMN` used that closure only as a non-emptiness test and then took every recomputable MATERIALIZED column of the table. Use the closure as the set as well. Three problems go away with the cause rather than being compensated for: A column whose expression reads an EPHEMERAL one is absent from the graph the closure walks, because it cannot be recomputed outside INSERT. It can no longer reach the recompute stage, so the stage is never handed a name no part holds -- the exception this branch previously avoided with an explicit filter. The non-emptiness test walked that same graph, so a clear reaching only such a column looked like it reached nothing and skipped the refresh of every other column. Gate and set are now one expression and cannot disagree. Recomputing a column in the base part while its projection was hardlinked stale is what made the projection and skip-index rebuild necessary. An untouched column now keeps its stored value and its projection at the pre-clear value, so they cannot diverge. This reverses a deliberate choice: the wide set was there so that a metadata-only `MODIFY COLUMN ... MATERIALIZED`, which schedules no mutation, would have its stored values refreshed by an unrelated `CLEAR COLUMN`. `MATERIALIZE COLUMN` is the documented command for that, and it is explicit, per-column and `IN PARTITION`-capable. The wide behaviour also did not hold: with `enable_block_number_column` or `enable_block_offset_column` on it does not happen at all, and the two cases in `04546_ttl_delete_where_recalc_after_update` that pinned it had to set both to 0 to observe it. Those cases are deleted. Co-Authored-By: Claude Opus 5 <[email protected]>
…hemeral-materialized
The section "CLEAR COLUMN recalculating MATERIALIZED columns" relied on the old behavior in which `CLEAR COLUMN` rewrote every `MATERIALIZED` column of the table. With the recompute set narrowed to the dependency closure of the cleared columns, the statistics-carrying column `e` must read the cleared column `c` to be rewritten, so its new expression is `c + 2`, which yields the same values and statistics as before. The reference file is unchanged. Failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=115371&sha=49efb8148c43b6a57254ab8e6d1a45219086be86&name_0=PR&name_1=Fast%20test Co-Authored-By: Claude Fable 5 <[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, 21 s compile time in total, 7 of them have a recent master baseline. |
`04926_clear_column_ephemeral_materialized` failed on six lanes (all four `flaky check`
jobs, `amd_msan WasmEdge` and `amd_tsan selected tests`) with `mk` at `2` instead of `1`:
-0 8 108 1
+0 8 108 2
`mk MATERIALIZED x + 1` was not recomputed. The runner randomizes
`enable_block_number_column`, `enable_block_offset_column` and `min_bytes_for_wide_part`,
and on a Wide part with either block column on, a mutation does not recompute MATERIALIZED
columns at all - so the recompute this test is about never ran, and `mk` kept its
pre-clear value.
Measured on this branch: the failure needs BOTH a Wide part AND a block column. Wide
alone, or either block column alone on a Compact part, still recomputes `mk` to `1`;
`min_bytes_for_full_part_storage` makes no difference. Pinning both block columns off
reproduces `mk = 1` deterministically - 6 runs with randomization pass, and flipping the
same clause to `= 1` with `min_bytes_for_wide_part = 0` reproduces the CI diff byte for
byte.
Only the first table needs the pin: `t_clear_unrelated` asserts that nothing is
recomputed, which holds under either setting.
The deleted section of `04546_ttl_delete_where_recalc_after_update` pinned these same two
settings for the same reason.
| } | ||
| } | ||
| if (!clear_column_names.empty()) | ||
| clear_affected_materialized = affected_materialized_closure(clear_column_names); |
There was a problem hiding this comment.
CLEAR COLUMN now has a precise dependency-closure contract in the background mutation path, but the read-time apply_mutations_on_fly = 1 path still does not know that a cleared source column changed. AlterConversions::addMutationCommand records DROP_COLUMN/clear only in dropped_columns, so buildMutationChainForRead never seeds x into all_updated_columns and never pulls mk MATERIALIZED x + 1 into the on-fly chain. Concretely, with a pending CLEAR COLUMN x, SELECT x, mk ... SETTINGS apply_mutations_on_fly = 1 will return 0, 2 from the stale part even though the fully materialized part stores 0, 1.
Can we either teach AlterConversions to treat CLEAR COLUMN as an updated source column (mirroring this closure) or explicitly disable on-the-fly application for this case? A focused test with SYSTEM STOP MERGES would catch it.
There was a problem hiding this comment.
🕵 I verified this scenario locally, and the mismatch is real — but it is pre-existing and this PR does not change it byte-for-byte, so I tracked it separately in #117035 rather than growing this fix into a read-path feature.
Two corrections to the framing:
- It is not an
apply_mutations_on_flyissue: a pendingCLEAR COLUMNis applied eagerly for every read (the cleared column lands inAlterConversions::dropped_columns, and metadata mutations are always included into the mutations snapshot regardless of the setting). With merges stopped,SELECT x, mkreturns0, 2with the setting both on and off. - This PR does not touch that path and does not widen the window. For a column in the closure (
mk), master behaves identically during the pending window and after materialization. What the PR changes is only the background recompute set — and for columns it stops recomputing (outside the closure), read-time and materialized states now actually agree, where on master they diverged. So the divergence set strictly shrinks here.
Since the eager-read behavior (and its interaction with IN PARTITION command selection, see the issue) predates this PR — it appeared with the recompute in #110648 — fixing it deserves its own PR with its own tests; both directions you suggest are written up in the issue. I did not add a test pinning the current mid-window values, because they are exactly what the follow-up should change.
…hemeral-materialized
A MATERIALIZED column that reads an EPHEMERAL column cannot be recomputed by a mutation, so when `CLEAR COLUMN` clears one of its regular dependencies its stored value silently goes stale. The existing warning in `MutationsInterpreter::prepare` fired only for `UPDATE`-ed dependencies; now it also fires when the dependency is in `clear_column_names`, so the stale-data path is logged instead of silent. Addresses the review comment on #115371 Co-Authored-By: Claude Fable 5 <[email protected]>
…hemeral-materialized Master has independently landed the same design this pull request proposed: `clear_affected_materialized` is now the dependency closure of the cleared columns (`affected_materialized_closure(clear_column_names)`) instead of every `MATERIALIZED` column in the table, and master adapted `04546_ttl_delete_where_recalc_after_update` and `04648_mutation_statistics_type_mismatch` to the narrow semantics rather than deleting the cases. Master's side is taken for all of that, so the branch no longer carries a competing revert of #110648. What remains unique to this pull request and is preserved: - The stale-`MATERIALIZED` warning in `MutationsInterpreter::prepare` also fires for cleared columns, not only updated ones: a `MATERIALIZED` column reading an `EPHEMERAL` column is skipped by the recompute, so a `CLEAR COLUMN` of one of its regular dependencies silently leaves the on-disk value stale. - The regression test for the original bug, renumbered `04926_clear_column_ephemeral_materialized` -> `05055_...` because master took the 04926 prefix.
|
🕵 Merged master and reworked the scope. Master has independently landed the very design this pull request proposed: The diff against master is now 3 files / 64 insertions and only contains what master does not have:
Verified locally against the merged tree: The one open review thread (the read-time |
…ure as the recompute
The warning added for `CLEAR COLUMN` only looked at the direct readable inputs of the
skipped column, so a chain stayed silent: with `m1 MATERIALIZED x + 1` and
`m2 MATERIALIZED m1 + e` (`e` `EPHEMERAL`), `CLEAR COLUMN x` recomputes `m1`, skips `m2`,
and `m2`'s inputs are `{m1, e}` — neither is the cleared column, so nothing was reported
even though `m2` is left stale behind one `MATERIALIZED` hop.
Answer it over the same transitive closure the recompute uses, which means moving the
check out of the loop that builds the readable dependency graph (a later hop of a chain
may still be unvisited while the loop runs) to just after the graph is complete. The
changed base set is now `UPDATE` columns, `CLEAR COLUMN` columns and patch-updated
columns — exactly the three sets whose closures are recomputed.
Addresses the review finding on `src/Interpreters/MutationsInterpreter.cpp`.
…hemeral-materialized
…TERIALIZED` hop `m2 MATERIALIZED m1 + e` with `m1 MATERIALIZED x + 1` and `e` `EPHEMERAL`: `CLEAR COLUMN x` recomputes `m1`, cannot recompute `m2`, and so leaves `m2` stale. The warning is emitted inside the background mutate task, so it never reaches the client that issued the `ALTER` and the test reads it from `system.text_log`, keyed on the logger name that carries the table. Verified load-bearing: with the staleness decided from the directly changed columns only — the shape this replaces — the test reports `warned about m2 0`.
|
🕵 Addressed the review finding, merged Review finding on New test The one CI failure is unrelated and already tracked. Verified locally against the merged tree: The other open thread (the read-time |
| # that issued the ALTER. The logger name carries the table, which isolates the rows well enough. | ||
| ${CLICKHOUSE_CLIENT} -q " | ||
| SYSTEM FLUSH LOGS text_log; | ||
| SELECT 'warned about m2', count() >= 1 |
There was a problem hiding this comment.
system.text_log is append-only, and DROP TABLE IF EXISTS / DROP TABLE do not remove old rows, so this assertion is not tied to the mutation that just ran. After one successful run, a rerun in the same ${CLICKHOUSE_DATABASE} can regress the warning emission completely and still satisfy count() >= 1 from the historical MutationsInterpreter(${CLICKHOUSE_DATABASE}.t_stale_warning) row. Can we capture a per-run lower bound (now64(6), query_id, CLICKHOUSE_LOG_COMMENT, etc.) before the ALTER and filter the log query on it? That also avoids scanning the whole system.text_log.
|
@alexbakharew why are we backporting this to 26.8? |
Cherry pick #115371 to 26.8: Warn when CLEAR COLUMN leaves an EPHEMERAL-derived MATERIALIZED column stale, and cover it with a test
…derived MATERIALIZED column stale, and cover it with a test
Backport #115371 to 26.8: Warn when CLEAR COLUMN leaves an EPHEMERAL-derived MATERIALIZED column stale, and cover it with a test
Closes: #116875
Related: #110648
Related: #117035
What is left in this pull request
This pull request originally proposed narrowing the
CLEAR COLUMNrecompute set from everyrecomputable
MATERIALIZEDcolumn of the table to the dependency closure of the cleared columns.Master has since landed exactly that design independently —
clear_affected_materializedis nowaffected_materialized_closure(clear_column_names)— and master also adapted04546_ttl_delete_where_recalc_after_updateand04648_mutation_statistics_type_mismatchto thenarrow semantics instead of deleting the cases. Master's side was taken for all of it, so this branch
no longer carries a competing revert of #110648, and the design question that was raised for
@alexey-milovidov here is settled by what is already on master.
Two things remain, and they are all this pull request now changes:
1. The regression test for the original bug.
ALTER TABLE ... CLEAR COLUMNused to fail on atable with a
MATERIALIZEDcolumn whose expression reads anEPHEMERALcolumn:EPHEMERALcolumns exist only duringINSERT, so aMATERIALIZEDcolumn reading one is absent fromthe dependency graph the closure walks and can no longer reach the recompute stage. The behaviour is
correct on master;
05055_clear_column_ephemeral_materializedpins it, including thatmk(whichdoes read the cleared
x) is recomputed, and that a clear of a column noMATERIALIZEDcolumnreads starts no recompute at all.
2. A stale-value warning that also covers cleared columns, decided over the recompute closure.
MutationsInterpreter::preparewarns when a mutation changes a dependency of aMATERIALIZEDcolumnthat reads an
EPHEMERALcolumn, because that column is skipped by the recompute and its on-diskvalue silently goes stale. The warning only looked at
updated_columns, and only at the directinputs of the skipped column.
CLEAR COLUMNleaves the value stale in exactly the same way, and achain hides the case entirely: with
m1 MATERIALIZED x + 1andm2 MATERIALIZED m1 + e,CLEAR COLUMN xrecomputesm1, skipsm2, andm2's inputs are{m1, e}— neither is the clearedcolumn.
The staleness is now answered over the same transitive closure the recompute uses. The changed base
set is
UPDATEcolumns,CLEAR COLUMNcolumns and patch-updated columns — exactly the three setswhose closures are recomputed — and a skipped column is reported when any of its readable inputs is
in that set or in its closure. The check therefore had to move out of the loop that builds the
readable dependency graph (a later hop of a chain may still be unvisited while it runs) to just
after the graph is complete. The message says "updated or cleared".
05153_clear_column_stale_materialized_warningpins the chain case. It reads the warning out ofsystem.text_log, because the interpreter runs inside the background mutate task and its log neverreaches the client that issued the
ALTER.Verified locally: both tests pass,
MutationsInterpreter.cppbuilds clean andclickhouselinks.Changelog category (leave one):
Workflow [PR]
Sync PR [sync-upstream/pr/115371]
Version info
26.9.1.1416-master(included in26.9and later)26.8.18.2