Skip to content

Fix wrong results when reading several subcolumns of one column with the columns cache - #126298

Merged
alexey-milovidov merged 5 commits into
masterfrom
fix-columns-cache
Oct 19, 2026
Merged

alexey-milovidov merged 5 commits into
masterfrom
fix-columns-cache

Conversation

@Avogar

@Avogar Avogar commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

Related: #96844

The subcolumn readers of one column share the deserialize states of their common streams through SubstreamsDeserializeStatesCache: the Variant discriminators (also inside Dynamic), the structure of the JSON dynamic shared data, the sparse offsets. In each read call the first reader decodes such a stream and the others take the result from the substreams cache without advancing their own state, which is correct only while they all hold the same state object. DeserializationPrefixesCache::clonePrefixes cloned every top-level state separately, so each reader got a private copy of the shared state. LowCardinality looked its state up in the states cache but never inserted it, so its readers did not share it at all.

Without the columns cache the same reader always decodes, so the copies never diverge. With use_columns_cache = 1 the columns cache serves a subcolumn for some granules and not for others, so the decoding reader changes between calls and continues from a stale position. The result is wrong values of JSON dynamic paths and Variant subcolumns without any exception, Unexpected format of compact discriminators granule for Dynamic subcolumns, and Invalid type for SerializationLowCardinality index column for LowCardinality inside Tuple or a JSON typed path.

The fix:

  • DeserializeBinaryBulkState::clone is now non-virtual. It takes a DeserializeBinaryBulkStateCloneContext, returns the clone made earlier in the same context if there is one, and otherwise calls the protected virtual cloneImpl. clonePrefixes uses one context for the whole map, so a state shared before cloning stays shared after it. With clone as the only public entry point, new states cannot bypass the memoization.
  • SerializationLowCardinality::deserializeBinaryBulkStatePrefix adds its state to the states cache, as the other serializations with shared streams already do.

The new test puts a part of the granules of one subcolumn into the columns cache with two primary key range reads, then reads it together with a sibling subcolumn under PREWHERE. All 12 cases fail before the fix (4 with wrong values, 8 with exceptions) and pass with it.

The columns cache appeared in #96844, so 26.10 needs the backport. Older releases clone the prefixes the same way but are not affected in practice: without the columns cache the decoding reader does not change.

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):

Fix wrong values and the exceptions Unexpected format of compact discriminators granule and Invalid type for SerializationLowCardinality index column when reading several subcolumns of the same JSON, Variant, Dynamic or LowCardinality-containing column with use_columns_cache = 1.


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

Version info

  • Merged into: 26.11.1.193-master (included in 26.11 and later)
  • Backported to: 26.10.2.19

The subcolumn readers of one column share the deserialize states of
their common streams (`Variant` discriminators, the structure of the
`JSON` dynamic shared data, sparse offsets): the first reader in a call
decodes such a stream and the others take it from the substreams cache
without advancing their state. `DeserializationPrefixesCache` cloned
every top-level state separately, so each reader got its own copy of a
shared state, and `LowCardinality` never put its state into the states
cache at all. When the columns cache serves a subcolumn for some
granules and not for others, the decoding reader changes between calls
and continues from a stale position: wrong values of `JSON` and
`Variant` subcolumns, exceptions for `Dynamic` and `LowCardinality`.

`DeserializeBinaryBulkState::clone` now memoizes the clones in a context
shared by the whole prefixes map and calls the virtual `cloneImpl`, so a
state shared before cloning stays shared after it. `LowCardinality`
adds its state to the states cache in `deserializeBinaryBulkStatePrefix`.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@Avogar Avogar mentioned this pull request Oct 16, 2026
1 task done
@clickhouse-gh

clickhouse-gh Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [65fd3dd]

Summary: ✅


AI Review

Summary

Reviewed PR #126298 at 65fd3dd0a790f81fcf77a0c4edc768739e6b422e. The change preserves shared deserialization states across prefix-cache clones and registers LowCardinality states in the substreams state cache, fixing wrong results and exceptions when sibling subcolumns alternate between cached and disk reads. No actionable findings. The new 12-case stateless regression passes in the Fast test report.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 16, 2026
@Avogar Avogar changed the title Keep shared deserialize states shared when cloning deserialization prefixes Fix wrong results when reading several subcolumns of one column with the columns cache Oct 16, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-data-types Type system & core datatypes used across query planning/execution. label Oct 16, 2026
@alexey-milovidov alexey-milovidov added v26.10-must-backport blocker This issue / pr blocks a new release labels Oct 16, 2026

@alexey-milovidov alexey-milovidov left a comment

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.

Ok, this is clear.

@alexey-milovidov alexey-milovidov self-assigned this Oct 17, 2026
…states`

`SYSTEM DROP COLUMNS CACHE` clears the columns cache of every test, and the
test relies on its reads being admitted to the cache, which the adaptive
admission makes probabilistic while other tests churn the cache.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=126298&sha=9e3bb65ffe41a6341d7974629428332c2f6ff307&name_0=PR&name_1=Style%20check
#126298

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

clickhouse-gh Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Commit 65fd3dd0a790f81fcf77a0c4edc768739e6b422e was not compared: the CI logs cluster did not answer (exception 241, code 500: { "meta": [ { "name": "file", "type": "String" }, { "name…).

See the job log for details.

alexey-milovidov and others added 2 commits October 17, 2026 07:14
… thread fuzzer

The insert parsed 400000 JSON values from text, and the parser of every value is
taken from a pool under a mutex. With the thread fuzzer of the flaky check, which
sleeps around mutex operations, the insert took about 450 seconds. Cast the JSON
values from named tuples instead, which builds the same columns without parsing.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=126298&sha=51847fa0d23fd23805a387c1a1fcfe2fa26cb755&name_0=PR&name_1=Stateless%20tests%20(amd_binary%2C%20flaky%20check)
#126298

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…es` once

The cast from a named tuple to `JSON` goes through the text as well, so the insert
still parsed a value per row and took about 230 seconds under the thread fuzzer
of the flaky check. Parse a thousand values once and take them by index: the
insert takes a fraction of a second. The values repeat every 1000 rows, which is
not a multiple of the distance between the granules a stale decoder confuses, so
without the fix the test still finds the wrong values (18928 of them for each
`JSON` case).

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=126298&sha=eebb6efc3b9&name_0=PR&name_1=Stateless%20tests%20(amd_binary%2C%20flaky%20check)
#126298

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

Copy link
Copy Markdown
Member

🕵 Status: the public CI for 3b1448c has no failed tests so far; some jobs are still running. The only red check is CH Inc sync, which reports build failed. In the sync PR (ClickHouse/clickhouse-private#81992), the private code was already adapted to the new DeserializeBinaryBulkState::clone signature (5947ec38862). The only failing private job is Build (amd_msan), and that job also fails on unrelated private PRs (#82089, #82086). I could not read its log from here (the report needs private credentials). Can someone with private CI access check the amd_msan build log of the sync PR and rerun it if it is infrastructure-related?

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 19, 2026
Merged via the queue into master with commit bb53dd9 Oct 19, 2026
178 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-columns-cache branch October 19, 2026 11:44
@robot-clickhouse robot-clickhouse added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 19, 2026
@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 19, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 19, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 19, 2026
Backport #126298 to 26.10: Fix wrong results when reading several subcolumns of one column with the columns cache
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Nov 4, 2026
Conflicts, all context-only:
- SerializationVariant.cpp, SerializationVariantElement.cpp: 26.8 has
  `clone()` without the clone context (ClickHouse#126298 is not in 26.8); keep it and
  add only the copies of the new `variant(s)_absent_in_storage` flags.
- MergeTreeReaderWide.cpp: 26.8 has `addStream`, not
  `FileStreams::getOrCreate` (ClickHouse#114252 is not in 26.8); add only the new
  `isReadWithTypeInPart` before it.

Semantic fix: `IMergeTreeReader::hasSubcolumnInPart` (ClickHouse#120632) is not in
26.8, so `isReadWithTypeInPart` uses `type->hasSubcolumn`, as the 26.8
readers already do.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Nov 4, 2026
Conflicts, all context-only:
- SerializationVariant.cpp, SerializationVariantElement.cpp: 26.9 has
  `clone()` without the clone context (ClickHouse#126298 is not in 26.9); keep it and
  add only the copies of the new `variant(s)_absent_in_storage` flags.
- MergeTreeReaderWide.cpp: 26.9 has `addStream`, not
  `FileStreams::getOrCreate` (ClickHouse#114252 is not in 26.9); add only the new
  `isReadWithTypeInPart` before it.

Semantic fix: `IMergeTreeReader::hasSubcolumnInPart` (ClickHouse#120632) is not in
26.9, so `isReadWithTypeInPart` uses `type->hasSubcolumn`, as the 26.9
readers already do.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blocker This issue / pr blocks a new release comp-data-types Type system & core datatypes used across query planning/execution. pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.10-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants