Skip to content

Fix reading v1 patch parts that both update a column added by ADD COLUMN - #126698

Open
groeneai wants to merge 2 commits into
ClickHouse:masterfrom
groeneai:fix-lwu-v1-patch-group-header
Open

groeneai wants to merge 2 commits into
ClickHouse:masterfrom
groeneai:fix-lwu-v1-patch-group-header

Conversation

@groeneai

@groeneai groeneai commented Oct 19, 2026 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fixed NOT_FOUND_COLUMN_IN_BLOCK in SELECT, merges and ALTER TABLE ... APPLY PATCHES on a table with two lightweight UPDATEs in the v1 patch part format (the only format before 26.8, or patch_parts_version = 'v1') that both set a column added by ALTER TABLE ... ADD COLUMN. Such tables written by 26.7 could not be read after an upgrade to 26.8. Closes #126612.

Description

On 26.8.28.14 (LTS) and 26.9.19.9, reading, merging or running APPLY PATCHES on a table with two v1 patch parts that both update a column added by ADD COLUMN fails with NOT_FOUND_COLUMN_IN_BLOCK. 26.7.22.4 returns the right rows, and a data directory written by 26.7 becomes unreadable after the upgrade. Closes #126612, a regression from #116007.

Root cause: a column the part does not store is patched after its default is evaluated (AfterEvaluatingDefaults). In that stage the patches (s, a) and (n, a) both update only [a], so applyPatchesToBlockLegacy puts them into one group, but since #116007 the group header was built from all columns of the first patch block, (a, n). The combined path (taken here because of the String column) then looks up n in every block of the group, and the (s, a) block throws.

Change: getUpdatedHeader keeps only the columns the group was formed by, and the legacy groups are keyed by the column names again, both as before #116007. The v2 (MergeOnKey) path is unchanged: its patch blocks already hold only the updated columns.

Validation: the new test fails on master (Not found column n in block, 20/20) and passes with the fix (50/50 with randomized settings). The lightweight update and patch part stateless tests show no new failures compared to master, also with patch_parts_version = 'v1' as the server default.

🤖 Generated with Claude Code


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

groeneai and others added 2 commits October 19, 2026 11:39
A column that the data part does not store (added by `ALTER TABLE ... ADD
COLUMN` after the part was written) is patched in the `AfterEvaluatingDefaults`
stage, after the column has got its default. Two v1 patch parts that update
different columns but both update such a column, e.g. `(s, a)` and `(n, a)`,
both have `updated_columns = [a]` in that stage, so `applyPatchesToBlockLegacy`
puts them into one group. Since ClickHouse#116007 the group header was built by
`getUpdatedHeader(patches)` from all columns of the first patch block, `(a, n)`,
instead of from the group's `[a]`. When the combined path is used (one of the
patches updates a `String` column), `CombinedPatchBuilder::createPatchForColumn`
then looks for `n` in every block of the group and the `(s, a)` block throws
`NOT_FOUND_COLUMN_IN_BLOCK`. Reads, merges and `APPLY PATCHES` of such tables
failed, including tables written by 26.7 and opened by 26.8+.

Restore the behaviour from before ClickHouse#116007: `getUpdatedHeader` keeps only the
columns the group was formed by, and the legacy groups are keyed by the column
names instead of their hash.

Closes ClickHouse#126612

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The read after OPTIMIZE FINAL applied patch parts on the fly, so it passed
even if the merge did not write the patched values. Read it with
apply_patch_parts = 0, as the APPLY PATCHES half of the test already does.

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

Copy link
Copy Markdown
Collaborator Author
Internal second-model review (click to expand)
Finding Verdict
⚠️ Test: the read after OPTIMIZE FINAL applied patches at read time, so it could not show whether the merge wrote the patched values AGREE, fixed: that read now uses apply_patch_parts = 0

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. The issue SQL (patch_parts_version = 'v1', ADD COLUMN a, UPDATE of (s, a) and (n, a)) fails every time on master with Not found column n in block; the new test fails 20/20 on master.
b Root cause explained? a is not stored in the part, so it is patched at AfterEvaluatingDefaults, where both patches update only [a] and form one group. Since #116007 the group header was all columns of the first patch block, (a, n); the combined path looks up n in the (s, a) block and throws.
c Fix matches root cause? Yes. The group header keeps only the columns the group was formed by and groups are keyed by the names, as before #116007. No guard at the throwing site.
d Test intent preserved / new tests added? New 05407_lwu_v1_patches_update_added_column: SELECT, SELECT *, PREWHERE, then OPTIMIZE FINAL and APPLY PATCHES, each followed by a read with apply_patch_parts = 0 so it proves the merge or mutation wrote the patched values. No existing test changed.
e Both directions demonstrated? Master: 20/20 FAIL (Code: 10). Fix: 50/50 OK with randomized settings and MergeTree settings.
f Fix is general across code paths? One loop serves both v1 modes (Merge, Join), both applyPatches callers and all readers (queries, merges, mutations). The v2 path builds patch blocks from the group's columns already and is unchanged.
g Fix generalizes across inputs (params/datatypes/wrappers)? Grouping and filtering are by column name, independent of type. The stateless lightweight update and patch part tests (163) show no new failures, with random settings and with v1 as the server default; rename tests included.
h Backward compatible? (maintainer-approved exception only) Yes. No setting or format change; reads behave as on 26.7. A 26.7 data directory that fails on 26.8.28.14 reads and merges correctly with the fix.
i Invariants and contracts preserved? A v1 patch group writes exactly the columns it was grouped by, so the headers of one group are compatible again. Every patch of a group has every group column in its block.

Session id: cron:clickhouse-impl-slot-7:20261019-111300

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @Michicosun @scanhex12, could you review this and add the can be tested label so CI runs? Since #116007 the header of a v1 patch group was built from all columns of its first patch block, so two patches (s, a) and (n, a) grouped by a column a added with ADD COLUMN failed with NOT_FOUND_COLUMN_IN_BLOCK; this restores the pre-#116007 filter of the header to the group's columns and keys the groups by column names again.

@clickhouse-gh

clickhouse-gh Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [bea12d4]

Summary: ✅


AI Review

Summary

The PR fixes NOT_FOUND_COLUMN_IN_BLOCK when legacy v1 patch parts update a column added after the base part was written. Filtering each group header to the columns selected for the current read stage addresses the cause across reads, merges, and ALTER TABLE ... APPLY PATCHES. Review of head bea12d4aed24570fb96f056cb272f244e562c177 found no actionable defects. The new regression test passed in Fast test.

Missing context / blind spots
  • ⚠️ Broader stateless jobs and automated bugfix validation were dropped. Finish Workflow consequently failed its bugfix-validation check. Rerunning those jobs would close this CI gap; no local build or test execution was performed during this review.
Final Verdict

Status: ✅ Approve. No code changes requested.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 19, 2026
@PedroTadim

Copy link
Copy Markdown
Member

cc @alexey-milovidov

@clickhouse-gh clickhouse-gh Bot added the comp-mutations ALTER UPDATE/DELETE and mutation execution over parts (including lightweight updates/deletes). label Oct 19, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 21, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Oct 24, 2026
…column

The readers created for patch parts also skipped a subcolumn whose
`Nullable` parent is converted with a `DEFAULT`, but nothing extracts it
from the parent there, so the patch was not applied to the subcolumn.
When the patched part lacks the column (added by `ALTER ADD COLUMN`
after the part was written), the subcolumn of an updated row then read
the `DEFAULT` instead of the updated value (`s.size` 3 for 'updated').

A patch part now provides such a subcolumn as stored. When the patched
part extracts the subcolumn from its own column, the patch reads only
that column, so its unconverted subcolumn does not overwrite the value
extracted after the patch is applied to the column.

With legacy (`v1`) patches, deferring the subcolumn to the last read
stage makes the Merge and Join modes re-apply the column there, after
its conversion, so a `NULL` the patch set throws
`CANNOT_INSERT_NULL_IN_ORDINARY_COLUMN` (master returns the type
default). ClickHouse#126698 fixes that re-application by restricting each stage to its
own columns; the new test pins `patch_parts_version = 'v2'`.

Reported in ClickHouse#125037 (comment)

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

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-mutations ALTER UPDATE/DELETE and mutation execution over parts (including lightweight updates/deletes). pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

3 participants