Skip to content

Rebuild broken and missing commit-order projections from the part's data in a partial mutation - #124912

Open
groeneai wants to merge 5 commits into
ClickHouse:masterfrom
groeneai:groeneai/commit-order-projection-partial-mutation
Open

groeneai wants to merge 5 commits into
ClickHouse:masterfrom
groeneai:groeneai/commit-order-projection-partial-mutation

Conversation

@groeneai

@groeneai groeneai commented Oct 9, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #111809

Changelog category (leave one):

  • Improvement

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

A mutation that rewrites only some columns of a Wide part now builds a missing commit-order projection (allow_commit_order_projection) and a broken projection from the part's real values instead of type defaults, and no longer fails on every retry with NO_SUCH_COLUMN_IN_TABLE for _block_number.

Description

A part has no commit-order projection after an insert, so MutationsInterpreter rebuilds it in every mutation, but, unlike MATERIALIZE PROJECTION, without adding the projection's columns to the dependencies. A partial mutation of a Wide part did not read them, and PartMergerWriter::calculateProjection filled them with type defaults:

  • the projection stored zeros, and a read in insertion order returned them;
  • when the part already stored _block_number (after a merge, a lightweight DELETE or ATTACH PARTITION ... FROM), getPhysical threw NO_SUCH_COLUMN_IN_TABLE and the mutation was retried forever.
CREATE TABLE t (id UInt64, g UInt64, w UInt64,
    PROJECTION p2 (SELECT *, _block_number, _block_offset ORDER BY _block_number, _block_offset))
ENGINE = MergeTree ORDER BY id
SETTINGS min_bytes_for_wide_part = 0, enable_block_number_column = 1, enable_block_offset_column = 1, allow_commit_order_projection = 1;
INSERT INTO t SELECT number, number % 7, number FROM numbers(10);
ALTER TABLE t UPDATE w = w + 1 WHERE id = 5 SETTINGS mutations_sync = 2;
SELECT id, g, w FROM t ORDER BY _block_number, _block_offset; -- id = g = 0 in every row

A broken projection is rebuilt by every mutation the same way: a plain one got the defaults and returned wrong rows, a commit-order one hit the same error, one with none of its columns read was dropped.

The fix registers the required columns of both as readonly PROJECTION dependencies, as MATERIALIZE PROJECTION does. The setting is experimental and broken projections come from damaged parts, hence Improvement.

A projection already written with defaults stays wrong until dropped and re-added: merges keep it. A rebuilt projection still keeps lightweight-deleted rows; #111809 fixes that.

05340 covers level-0, merged, attached and lightweight-deleted parts and MATERIALIZE PROJECTION of another projection; 05342 breaks a plain and a commit-order projection. All fail without the fix.

Server log on master (UPDATE after OPTIMIZE FINAL and ADD PROJECTION)
2026.10.09 11:10:12.333771 [ 1265574 ] {17f4cc25-abd0-48c6-bf0e-dbb7d9e1c511::all_1_1_1_2} <Trace> MutateTask: Mutating part all_1_1_1 to mutation version 2
2026.10.09 11:10:12.333911 [ 1265574 ] {17f4cc25-abd0-48c6-bf0e-dbb7d9e1c511::all_1_1_1_2} <Debug> MutationsInterpreter(rawlog57.t_c): Will rebuild commit-order projection p2
2026.10.09 11:10:12.335509 [ 1265574 ] {17f4cc25-abd0-48c6-bf0e-dbb7d9e1c511::all_1_1_1_2} <Error> MutatePlainMergeTreeTask: Code: 16. DB::Exception: There is no physical column _block_number in table. (NO_SUCH_COLUMN_IN_TABLE) (version 26.10.1.1)
5. src/Storages/ColumnsDescription.cpp:954:15: DB::ColumnsDescription::getPhysical(String const&) const
6. src/Storages/MergeTree/MutateTask.cpp:2609:69: DB::PartMergerWriter::calculateProjection(unsigned long, DB::Block const&, unsigned long)
7. src/Storages/MergeTree/MutateTask.cpp:2673:17: DB::PartMergerWriter::finalizeTempProjectionsAndIndexes()
2026.10.09 11:10:12.759646 [ 1265586 ] {17f4cc25-abd0-48c6-bf0e-dbb7d9e1c511::all_1_1_1_2} <Trace> MutateTask: Mutating part all_1_1_1 to mutation version 2
2026.10.09 11:10:12.761890 [ 1265586 ] {17f4cc25-abd0-48c6-bf0e-dbb7d9e1c511::all_1_1_1_2} <Error> MutatePlainMergeTreeTask: Code: 16. DB::Exception: There is no physical column _block_number in table. (NO_SUCH_COLUMN_IN_TABLE) (version 26.10.1.1)

With the fix the same mutation logs Will rebuild commit-order projection p2, writes p2 and finishes.

🤖 Generated with Claude Code


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

groeneai and others added 2 commits October 9, 2026 11:12
…tions

A part never carries a commit-order projection after an insert, so
`MutationsInterpreter` rebuilds it in every mutation of such a part. Unlike
`MATERIALIZE PROJECTION`, that branch did not register the projection's
required columns as dependencies, so a mutation that rewrites only some
columns of a Wide part did not read them. `calculateProjection` then filled
the missing columns with type defaults: the projection stored zeros, and a
read in insertion order returned them. When the part already stored
`_block_number` (after a merge, a lightweight delete, or `ATTACH PARTITION
FROM`), `getPhysical` threw `NO_SUCH_COLUMN_IN_TABLE` for the virtual column
and the mutation was retried forever.

Register the required columns as readonly `PROJECTION` dependencies before
the stages are built, as `MATERIALIZE PROJECTION` does. The readonly stage
reads them without rewriting them. On-the-fly read interpreters are excluded:
they never write the projection.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-self PR origin: self-found defect (impact-bar validated) labels Oct 9, 2026
@groeneai

groeneai commented Oct 9, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 2 rounds, 1 finding, 0 in the re-review) plus my own review (3 nits).

# Sev Finding Verdict Evidence / action
1 ⚠️ Case E checked only that the mutation finishes, not that p2 was rebuilt AGREE, fixed case E now checks that p2 holds every surviving row; a build that skips the rebuild prints E 0
2 💡 On a part with lightweight-deleted rows the rebuilt p2 still keeps those rows AGREE, noted same as any projection rebuilt on such a part today; #111809 fixes it
3 💡 A lightweight DELETE in drop mode reads the projection columns although the projection is then dropped AGREE, noted experimental table setting, read-only cost

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding. Findings on hunks unchanged by the fix round are auto-dropped.

Session id: cron:clickhouse-review-self-slot-58:20261009-124700

@groeneai

groeneai commented Oct 9, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. The SQL in the description on master: every row of the insertion-order read has id = g = 0; with OPTIMIZE FINAL (or a lightweight DELETE) before ADD PROJECTION, the UPDATE fails with NO_SUCH_COLUMN_IN_TABLE on every retry.
b Root cause explained? The missing commit-order projection was scheduled for rebuild without adding its required_columns to the dependencies, so a partial mutation of a Wide part did not read them and calculateProjection default-filled them (getPhysical throws for the virtual _block_number).
c Fix matches root cause? Yes. The interpreter now registers those columns as readonly PROJECTION dependencies, as MATERIALIZE PROJECTION does. No guard in calculateProjection.
d Test intent preserved / new tests added? New test with five part shapes: level-0 UPDATE, MATERIALIZE PROJECTION of another projection, merged part, ATTACH PARTITION FROM, lightweight delete.
e Both directions demonstrated? Yes. Unfixed build: all five cases red (20/20 with random settings). Fixed build: green, 50/50 with random settings, 10/10 + 10/10 with parallel replicas (parallel_replicas_local_plan 1 and 0). Removing only the dependency registration, or the whole rebuild branch, turns all five red again.
f Fix is general across code paths? Yes. The other rebuild sites (MATERIALIZE PROJECTION, key type change, CLEAR COLUMN) already register the columns; projections affected by an update get them from getAllColumnDependencies; whole-part rewrites read every column. Replicated and Shared MergeTree use the same interpreter.
g Fix generalizes across inputs (params/datatypes/wrappers)? No type-specific code. The test covers _block_number not stored, stored, and invalidated by partition adoption; Compact and packed parts rewrite every column and were already correct.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, default or format change; only mutations of parts that lack a commit-order projection read its columns once more.
i Invariants and contracts preserved? Yes. Readonly stage columns are not written back (execute keeps only write-stage columns), so the part's data is unchanged; on-the-fly read interpreters and ALTER validation interpreters are excluded by isMutatingDataPart() && !apply_on_fly_for_read.

Session id: cron:clickhouse-impl-self-slot-57:20261009-122300

@groeneai
groeneai requested review from CurtizJ and Michicosun October 9, 2026 13:09
@clickhouse-gh

clickhouse-gh Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0d3c721]

Summary: ✅


AI Review

Summary

The change registers the columns required to rebuild broken projections and missing commit-order projections before mutation read stages are prepared, preventing default-filled projection data and repeated NO_SUCH_COLUMN_IN_TABLE exceptions. The previous finding is addressed in the current code. No unresolved findings remain.

Final Verdict

✅ Approve — no new actionable issues found. Both focused regression tests pass in CI for commit 0d3c72111c412c7c7570da24f8faf884911407d4.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Oct 9, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-mutations ALTER UPDATE/DELETE and mutation execution over parts (including lightweight updates/deletes). label Oct 9, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Commit 0d3c72111c412c7c7570da24f8faf884911407d4 was not compared: the CI logs cluster did not answer (every POST attempt failed with an exception).

See the job log for details.

A mutation rebuilds every broken projection of the part it rewrites, but
MutationsInterpreter only added the projection to materialized_projections,
after the dependencies had already been turned into read stages. A mutation
that rewrites only some columns of a Wide part therefore did not read the
projection's columns, and PartMergerWriter::calculateProjection filled the
missing ones with type defaults:

- a plain projection was rebuilt with zeros and then returned wrong rows;
- a commit-order projection on a part that stores _block_number failed with
  NO_SUCH_COLUMN_IN_TABLE on every retry;
- when the block had none of the projection's columns, the projection was
  dropped from the new part.

Register the projection's required columns as readonly PROJECTION
dependencies in the same place as for a missing commit-order projection.
The test breaks the projection files in a private clickhouse-local path and
then updates a column the projection does not use.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai changed the title Build a missing commit-order projection from the part's data in a partial mutation Rebuild broken and missing commit-order projections from the part's data in a partial mutation Oct 9, 2026
@groeneai

groeneai commented Oct 9, 2026

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 3e5395a

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Stateless tests (arm_binary, ParallelReplicas, s3 storage, parallel) / 05210_lightweight_update_materialized_cte_in_ignore flaky, not from this PR: a MATERIALIZED CTE read twice in an IN subquery fails on the replicas with UNKNOWN_TABLE for its _materialized_cte_c_* table (#112642). 126/126 reruns pass; 33 PRs show it since 2026-10-08 #123121 (mine, open)
Stateless tests (arm_binary, s3 storage, DBReplicated, parallel, 1/2) / 05316_secret_arguments_declared_by_engines not from this PR: the test came with #123364, and under DatabaseReplicated its last query_log check counts the CREATE DICTIONARY row twice (0 2 instead of 0 1; the password stays hidden). 180 PRs show it in this lane since 2026-10-02 #124928 (external, merged 17:58Z). This run was dispatched at 15:52Z, before that merge, so I will merge master here to pick it up
Mergeable Check, PR praktika rollups of the two rows above (Failed: 2) #123121, #124928

Session id: cron:our-pr-ci-monitor:20261009-220010

groeneai added a commit to groeneai/ClickHouse that referenced this pull request Oct 15, 2026
05340 is also used by ClickHouse#124912 and by master's
05340_projection_lightweight_delete_rebuild_normal_projection (ClickHouse#118375).
05401 is free on master.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…er-projection-partial-mutation

In `MutationsInterpreter::prepare`, this branch moved the rebuild of broken and missing commit-order projections into an earlier block that also reads their columns. Master (ClickHouse#123185) added `hasUnloadedProjection` to the projection loop. The loop now skips a projection the part does not have unless it is on disk but not loaded and is not a commit-order projection. Commit-order projections are already handled by the earlier block.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added comp-projections Projections: precomputed layouts for accelerating queries (projection parts, maintenance). and removed comp-mutations ALTER UPDATE/DELETE and mutation execution over parts (including lightweight updates/deletes). labels Nov 19, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-projections Projections: precomputed layouts for accelerating queries (projection parts, maintenance). groeneai-origin-self PR origin: self-found defect (impact-bar validated) pr-improvement Pull request with some product improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants