Skip to content

Fix mutations failing with CANNOT_READ_ALL_DATA when they read a column and its LowCardinality subcolumn - #123160

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-lc-single-dict-lazy-stream-mutation
Oct 4, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-lc-single-dict-lazy-stream-mutation

Conversation

@groeneai

@groeneai groeneai commented Oct 1, 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 mutations such as ALTER TABLE ... UPDATE failing with CANNOT_READ_ALL_DATA on every attempt, and never completing, when they read a column together with its LowCardinality subcolumn in a Wide part: a Dynamic column and its LowCardinality(String) subcolumn, or a Map with map_serialization_version = 'with_buckets' and its keys or values subcolumn. The problem appeared in 26.6.

Description

On 26.9, an ALTER TABLE ... UPDATE that reads a Dynamic column together with its LowCardinality(String) subcolumn (or a bucketed Map together with m.keys/m.values) fails with CANNOT_READ_ALL_DATA on every attempt and never completes:

CREATE TABLE t (id UInt64, d Dynamic, n UInt64 DEFAULT 0) ENGINE = MergeTree ORDER BY id SETTINGS min_bytes_for_wide_part = 0;
INSERT INTO t (id, d) SELECT number, toLowCardinality(toString(number % 10)) FROM numbers(10000);
ALTER TABLE t UPDATE n = length(d.`LowCardinality(String)`) + length(toString(d)) WHERE 1 SETTINGS mutations_sync = 2;
-- system.mutations: is_done = 0, Code: 33 ... Cannot read all data ... (while reading column d.LowCardinality(String))

Measured on the official 26.6, 26.7, 26.8 and 26.9 images; 26.3 and 26.5 complete the mutation. It came with the single-dictionary optimization of #103662.

SerializationLowCardinality asks the Wide reader's has_uniform_marks_callback whether the part has a single dictionary. The callback answered false for a stream the reader had not opened yet, and Map bucket and Dynamic variant streams are opened lazily. So the subcolumn was treated as multi-dictionary while the full column, deserialized next, read the shared dictionary stream to EOF. Mutations read without seeking to marks, so the subcolumn's first dictionary read then hit EOF.

The callback now opens the stream, like the neighbouring callbacks, so both columns get the same answer.

The new test fails on master (4 errors) and passes with the change (50 randomized runs).

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.10.1.1474-master (included in 26.10 and later)

…subcolumn

`SerializationLowCardinality::deserializeBinaryBulkStatePrefix` asks the
Wide reader's `has_uniform_marks_callback` whether the part may have a single
dictionary. The callback looked the stream up with `streams.find()` and
answered `false` when the reader had not opened it yet. Map bucket and Dynamic
variant streams are opened lazily by the prefix getter, which runs after the
callback, so the answer depended on which column sharing the stream was
deserialized first: `m.keys` was treated as multi-dictionary, `m` as single
dictionary, and `m`'s prefix read the shared dictionary stream to EOF.
Mutations read without seeking to marks, so the data read of `m.keys` then
called `read_dictionary()` on the EOF stream and failed with
`CANNOT_READ_ALL_DATA` on every attempt.

Open the stream in the callback, as the neighbouring callbacks already do.

Regression from ClickHouse#103662 (first released in 26.6).

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 1, 2026
@groeneai

groeneai commented Oct 1, 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; 0 findings) plus my own cold review (1 nit).

# Sev Finding Verdict Evidence / action
1 💡 The test header comment mentions reader internals rather than only what is tested (05315_low_cardinality_subcolumn_mutation_lazy_stream.sql:1-2) AGREE noted, not blocking

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-self-slot-58:20261001-000014

@groeneai

groeneai commented Oct 1, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. The new test run through clickhouse local gives 4 x Code: 33 Cannot read all data on master every time; through the runner it failed 20 of 20 randomized runs on master.
b Root cause explained? Yes. has_uniform_marks_callback in MergeTreeReaderWide::deserializePrefix returned false for a stream the reader had not opened yet. Map bucket and Dynamic variant streams are opened lazily, so a subcolumn got "multi-dictionary" while its full column, deserialized next, got "single dictionary" and read the shared dictionary stream to EOF. Mutation reads do not seek to marks, so the subcolumn's read_dictionary() hit EOF.
c Fix matches root cause? Yes. The callback now opens the stream (getOrAddStream, as its sibling callbacks do), so the answer depends on the part's marks, not on column order. No guard, no setting.
d Test intent preserved / new tests added? New test 05315_low_cardinality_subcolumn_mutation_lazy_stream: bucketed Map keys and values arms and a Dynamic LowCardinality arm, with value oracles (m.keys != mapKeys(m), subcolumn vs toString(d)).
e Both directions demonstrated? Yes. Master: 4 errors, runner FAIL (20/20 randomized). With the change: exact reference, runner 50/50 randomized OK.
f Fix is general across code paths? Yes. MergeTreeReaderWide is the only setter of the callback; MergeTreeReaderCompact sets none. Map buckets, Dynamic variants and JSON dynamic paths all go through this callback.
g Fix generalizes across inputs (params/datatypes/wrappers)? Checked LowCardinality keys, values, LowCardinality(Nullable) values, Array(LowCardinality(Nullable)) values and the Dynamic variant; results match basic Map serialization. Multi-dictionary parts are unchanged.
h Backward compatible? (maintainer-approved exception only) Yes. No format, setting or protocol change.
i Invariants and contracts preserved? Columns sharing a dictionary stream now reach the same verdict. The stream is the one the getter opens right after, on the same thread, so the one-creator-per-name rule of the reader's stream map holds. The EOF check in SerializationLowCardinality is unchanged.

Session id: cron:clickhouse-impl-self-slot-57:20260930-225800

@groeneai
groeneai requested review from Avogar and CheSema October 1, 2026 00:21
@clickhouse-gh

clickhouse-gh Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [75dcc35]

Summary: ✅


AI Review

Summary

This PR fixes MergeTreeReaderWide so the LowCardinality single-dictionary heuristic no longer depends on a lazily discovered Wide-part stream having been opened earlier. That matches the root cause described in the PR, and the new stateless regression test covers both affected surfaces (Map bucket keys/values and Dynamic with LowCardinality(String)) by exercising the mutation pattern that previously failed with CANNOT_READ_ALL_DATA. I did not find a correctness, compatibility, or test-gap issue in the current patch.

Missing context / blind spots
  • ⚠️ CI was still in progress at review time; Fast test, Style check, Build (arm_tidy), and Config Workflow had passed, while the remaining build/test jobs were still pending. Final confidence closes once the full PR workflow finishes.
Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Measured on commit 75dcc35.

Metric Baseline Current Δ
Lines 88.30% 88.20% -0.10%
Functions 91.80% 91.80% +0.00%
Branches 79.70% 79.60% -0.10%

Changed lines: Changed C/C++ lines covered: 5/5 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Oct 1, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 75dcc35dc with master aa370dbfd (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 253f06071; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 12 s compile time in total, 7 of them have a recent master baseline.

Job report

return false;

/// Bucket and dynamic streams are opened lazily; the verdict must not depend on which column opened them first.
auto * stream = getOrAddStream(substream_path, *stream_name);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@groeneai are you sure it won't ever return a null pointer? Maybe you should add that defensive check

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes, it cannot return null. FileStreams::getOrCreate returns either the existing map entry or the one it has just inserted, it is the only place that inserts into the map, and the factory in getOrAddStream always returns a std::make_unique<...> result (it throws on failure, never returns null). A part without a file for this substream is handled just above by if (!stream_name) return false;, before getOrAddStream is reached. The other two callers in deserializePrefix (getStream at L424 and seek_to_start_callback at L473) already dereference its result unchecked.

I'd rather not bring back return false here: false for a stream whose file exists is exactly the order-dependent verdict this PR removes. If you still want an explicit guard, I can add chassert(stream).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We prefer references over pointers. So if a pointer can't be non-null, it should be a reference.
Wait for this PR to be merged, then send a follow-up.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Acknowledged. After this PR is merged I will send a follow-up that makes getOrAddStream return a reference instead of a never-null pointer, and link it here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The follow-up is #123810.

@alexey-milovidov alexey-milovidov self-assigned this Oct 3, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 3, 2026
Merged via the queue into ClickHouse:master with commit ff91f2f Oct 4, 2026
149 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 4, 2026
azat pushed a commit to azat/ClickHouse that referenced this pull request Oct 5, 2026
`FileStreams::getOrCreate` returns either the existing map entry or the
one it just inserted, and the only factory (`getOrAddStream`'s lambda)
always returns `std::make_unique` (throws on failure, never null);
`release` moves the entry out and erases it under the same lock, so the
map never holds a null stream. Every caller either dereferenced the
pointer unchecked or discarded it, so return `MergeTreeReaderStream &`
from both functions instead.

`FileStreams::find` stays a pointer because it returns null for a
missing stream, and `getAll` stays a vector of pointers.

Follow-up to ClickHouse#123160, requested in
ClickHouse#123160 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added comp-part-formats Formats of MergeTree data parts in the public repository, that is how a part is laid out on disk ... and removed comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Nov 22, 2026
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-part-formats Formats of MergeTree data parts in the public repository, that is how a part is laid out on disk ... groeneai-origin-self PR origin: self-found defect (impact-bar validated) pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants