Skip to content

Test alternative for #114561 - #115638

Closed
Avogar wants to merge 4 commits into
masterfrom
on-fly-mutation-read-set-closure
Closed

Avogar wants to merge 4 commits into
masterfrom
on-fly-mutation-read-set-closure

Conversation

@Avogar

@Avogar Avogar commented Aug 20, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

...


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

Avogar and others added 4 commits August 20, 2026 13:33
…ation is pending

Applied from #114561 by Shaohua Wang
(tiandiwonder) as the base for follow-up work.

Reading a MATERIALIZED column that reaches an updated column only through another
MATERIALIZED column returned the stale on-disk value, while reading the whole chain
returned the recomputed one, so the result depended on the select list. Selecting a
MATERIALIZED column together with an updated one could also fail with
`UNKNOWN_IDENTIFIER`.

`AlterConversions::addColumnsRequiredForMaterialized` now closes the read set over the
MATERIALIZED columns it contains instead of looking one hop deep, normalises defaults
over subcolumns with `replaceSubcolumnsToGetSubcolumnFunctionInQuery`, and skips columns
whose expression reads an `EPHEMERAL` one. `MutationsInterpreter::prepare` resolves
`MATERIALIZED` defaults against every column of the table rather than only the ones the
read task reads.

Co-Authored-By: Shaohua Wang <[email protected]>
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…ncy graph

Two independent problems in the same plumbing.

The read set of an on-fly read was closed over MATERIALIZED chains once, before
`filterMutationCommands` ran. But that function is itself what puts the columns the
surviving commands read into the set, so anything it added was never closed over: a
`DELETE WHERE m2 > 100` or an `UPDATE y = m2` kept a command alive while the chain
behind `m2` stayed out of the read set, and the pending `UPDATE` of its base column was
dropped as irrelevant. The result depended on the select list, exactly as before the
chain fix. The closure and the filter now iterate until the read set stops growing.

`filterMutationCommands` also pushed identifiers into the read set as written, so a
command naming a subcolumn in dot syntax put `t.a` there while the set is keyed on
storage columns. That reached `MutationsInterpreter` as an available column and made
every read of the table fail with `NO_SUCH_COLUMN_IN_TABLE` while the mutation was
pending, with no MATERIALIZED column involved at all. Identifiers are now mapped through
`getNameInStorage`.

The analysis of MATERIALIZED defaults was open-coded at four sites with different source
column lists, and the read path and the interpreter have to agree on it or a dependency
they spell differently leaves the mutation unapplied. It moves into
`MaterializedColumnDependencies`, which owns the subcolumn rewrite, the `TreeRewriter`
analysis and the `EPHEMERAL` policy, and answers the reachability question both sides
ask. The five lambdas and two memo maps in `addColumnsRequiredForMaterialized` collapse
into a walk over that graph.

`getMutationSteps` no longer repeats the closure and the filter that `getMutationActions`
already performed; both consume one `MutationChainForRead`.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…n a proxy flag

Ported from #114561 by Shaohua Wang
(tiandiwonder), which added it after the version this branch started from.

The warning about a MATERIALIZED column derived from an EPHEMERAL one keeping a stale
on-disk value was keyed on `recalculate_dependencies_of_updated_columns`. That flag is
cleared on two paths, not one: the on-fly read, which writes nothing and should stay
quiet, and `MergeTreeData::updateLightweightImpl`, which builds a patch part and does
write. Keying on it silenced the one diagnostic a lightweight `UPDATE` gives.

Say what is meant with `Settings::apply_on_fly_for_read`, set only where
`AlterConversions` builds the actions for reading a part.

`05023_materialized_ephemeral_stale_warning` is taken unchanged from that pull request
and asserts all three directions at once. Measured against this build: `ALTER TABLE ...
UPDATE` warns, a lightweight `UPDATE` warns, an on-fly read does not and still returns
the stale value the warning is about.

Co-Authored-By: Shaohua Wang <[email protected]>
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@Avogar
Avogar marked this pull request as draft August 20, 2026 15:29
@clickhouse-gh

clickhouse-gh Bot commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [9222457]

Summary: ✅


AI Review

Summary

This PR fixes several apply_mutations_on_fly cases around MATERIALIZED columns: transitive chains, subcolumn references, EPHEMERAL inputs, and command predicates/expressions that grow the read set indirectly. The functional direction looks right and the new stateless coverage is strong, but the new dependency helper is currently eager enough to add avoidable TreeRewriter work to the read path on wide schemas.

PR Metadata
  • The current Changelog category is incorrect. This is a user-visible correctness fix, so it should be Bug Fix, not Not for changelog.
  • A Changelog entry is required for Bug Fix, and the current ... placeholder does not satisfy that requirement.
  • Suggested replacement:
    Changelog category: Bug Fix (user-visible misbehavior in an official stable release)
    Changelog entry: Fixed reading a table while a mutation is in progress: a MATERIALIZED column could return a stale value, and selecting one together with an updated column could fail with an error.
Missing context / blind spots
  • ⚠️ The current PR body does not explain the bug or intended invariant; I had to infer the contract from linked PR #114561 and the new tests. Copying that problem statement here would make the review auditable on its own.
Findings

⚠️ Majors

  • [src/Interpreters/MaterializedColumnDependencies.cpp:34] The new helper eagerly analyzes every MATERIALIZED default when it is constructed. Both new call sites instantiate it on hot paths (MutationsInterpreter::prepare per part and AlterConversions::getMutationSteps per read task), and prepare now does so even when need_materialized_analysis is false. On a wide table, even a DELETE-only pending mutation or a query that reads one derived column now pays O(all MATERIALIZED columns) TreeRewriter work before the read starts.
    Suggested fix: make the cache lazy per column, or at minimum defer construction until materialized analysis is actually needed and limit it to the columns reachable from the current read set.
Final Verdict

Needs changes before merge: the correctness fix looks good, but the dependency cache should not eagerly re-analyze every MATERIALIZED column on the read path.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 87.00% 87.00% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 79.30% 79.30% +0.00%

Changed lines: Changed C/C++ lines covered: 151/155 (97.42%) · Uncovered code

Full report · Diff report

source_columns.push_back(column);
}

for (const auto & column : columns)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This helper is eager right now: constructing it walks every MATERIALIZED default in the table and runs TreeRewriter on each one. The two new call sites build it on hot paths (MutationsInterpreter::prepare per part and AlterConversions::getMutationSteps per read task), and prepare does so even before need_materialized_analysis is known. On a wide table, even a DELETE-only pending mutation or a query that reads a single derived column now pays O(all MATERIALIZED columns) analyzer work before the read starts.

Can we make the entries lazy per column, or at least defer construction until materialized analysis is actually needed and limit it to the columns reachable from the current read set?

@clickhouse-gh

clickhouse-gh Bot commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 9222457bd with master c29afa34d (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes
Binary Master PR Δ
programs/clickhouse-stripped 704.41 MiB 701.37 MiB -3.04 MiB (-0.43%)

Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.

Object file sizes

14 object files changed (+58.72 KiB total), 1 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Interpreters/MaterializedColumnDependencies.cpp.o new 47.22 KiB +47.22 KiB

737 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

364 translation units recompiled, 3621 s compile time in total, 363 of them have a recent master baseline.

Job report

tiandiwonder added a commit that referenced this pull request Aug 21, 2026
…h from #115638

Avogar tried an alternative on top of this branch in
#115638 and asked for it to be moved
here. This is that change, applied verbatim: the four test files this branch already
carried are byte-identical in it, and `Settings::apply_on_fly_for_read` is kept.

What it adds over what was here:

- `MaterializedColumnDependencies` — the dependency graph over a table's `MATERIALIZED`
  columns, built once and shared by the read path and `MutationsInterpreter` instead of
  being derived separately in each. `getAffected` closes over the graph, so a column
  reachable only through another recomputed one is included.
- `filterMutationCommands` puts the columns a surviving command reads into the read set,
  and one of those can itself be a `MATERIALIZED` column of a chain (`DELETE WHERE
  m2 > 100`), which can in turn keep an older command alive. Closing the set and
  filtering now run in a loop until the set stops growing; it only grows and is bounded
  by the width of the table, so it terminates.
- A command may name a subcolumn as written (`t.a`) while the read set is keyed on
  storage columns, which made every read of the table fail with `NO_SUCH_COLUMN_IN_TABLE`.
  Names now go through `getNameInStorage` on the way in. This one reproduces on master.
- `getMutationSteps` and `getMutationActions` share one `buildMutationChainForRead`
  instead of each recomputing the read set and the command filter.

Test `04926_on_fly_mutation_read_set_closure` covers the chain reached from a pending
`DELETE` predicate, the subcolumn spelling, and the cases that must not move.

Co-authored-by: avogar <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
@Avogar Avogar closed this Aug 21, 2026
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant