Skip to content

Cherry pick #115371 to 26.8: Warn when CLEAR COLUMN leaves an EPHEMERAL-derived MATERIALIZED column stale, and cover it with a test - #122428

Merged
robot-ch-test-poll merged 15 commits into
backport/26.8/115371from
cherrypick/26.8/115371
Oct 3, 2026
Merged

robot-ch-test-poll merged 15 commits into
backport/26.8/115371from
cherrypick/26.8/115371

Conversation

@robot-ch-test-poll4

Copy link
Copy Markdown
Contributor

Original pull-request #115371

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Before you resolve anything

Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

tiandiwonder and others added 14 commits August 18, 2026 12:43
…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]>
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]>
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]>
`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.
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.
…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`.
…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`.
…-materialized

Warn when CLEAR COLUMN leaves an EPHEMERAL-derived MATERIALIZED column stale, and cover it with a test
@robot-ch-test-poll4 robot-ch-test-poll4 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request labels Sep 26, 2026
@robot-clickhouse-ci-1

Copy link
Copy Markdown
Contributor

Dear @tiandiwonder, @alexey-milovidov, this cherry-pick PR has not been updated for 3d0h2m35s. Please resolve the conflicts to backport #115371, or close this PR if the backport is no longer needed. This PR will be automatically closed after 7 days of inactivity.

Resolve the conflict in `MutationsInterpreter.cpp`: 26.8 does not have
`MaterializedColumnDependencies` nor `apply_on_fly_for_read`, so the
EPHEMERAL-reading MATERIALIZED columns are collected with an owned copy of
their readable (non-EPHEMERAL) dependencies inside the existing
`TreeRewriter`-based loop, and the staleness warning is emitted after
`affected_materialized_closure` is defined, as in the original change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Resolved the conflict in src/Interpreters/MutationsInterpreter.cpp (522537c): 26.8 has no MaterializedColumnDependencies / apply_on_fly_for_read, so the EPHEMERAL-reading MATERIALIZED columns are collected in the existing TreeRewriter-based loop with an owned copy of their non-EPHEMERAL dependencies, and the staleness warning is emitted after affected_materialized_closure, as in #115371. The resulting diff against the backport branch is the same five files as the original PR.

@robot-ch-test-poll
robot-ch-test-poll merged commit accbcdd into backport/26.8/115371 Oct 3, 2026
1 check passed
@robot-ch-test-poll
robot-ch-test-poll deleted the cherrypick/26.8/115371 branch October 3, 2026 16:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants