Skip to content

Fix LOGICAL_ERROR reading SHARED paths of dynamic types of a JSON column with the columns cache - #127867

Merged
Avogar merged 4 commits into
ClickHouse:masterfrom
groeneai:groeneai/json-shared-paths-cache-copy-structure
Nov 2, 2026
Merged

Avogar merged 4 commits into
ClickHouse:masterfrom
groeneai:groeneai/json-shared-paths-cache-copy-structure

Conversation

@groeneai

@groeneai groeneai commented Oct 26, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #127000

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

...

Description

Stateless tests (amd_debug, parallel) aborted with Logical error: 'Trying to deserialize data into Variant column with not global variants order' in 05296_json_shared_typed_paths_nested_types with randomized use_columns_cache = 1 (report). The code is new in #127000 and is master-only; its backport to 26.10 is #127829, still open.

The first reader of a JSON column in a read call decodes every requested SHARED path and puts it into the substreams cache; the other readers take the path from there in insertPathFromCache, a plain insertRangeFrom. For an empty Dynamic (or JSON) destination that gives a variant column whose variants are not in global order. With the columns cache the readers of one call can differ granule by granule: when the other reader is served from the cache, this reader becomes the first one, decodes the next granule straight into that column, and SerializationVariant throws.

insertPathFromCache now uses the file's insertRangeFromDecoded, which gives an empty destination the exact structure of the decoded column first, as the remainder path already does. It covers the three callers: the whole column, a path read as a subcolumn, and a sub-object.

05402_json_shared_typed_paths_columns_cache gets one block per caller plus one on a Compact part with substream marks. Each aborts the unfixed debug build and passes with the fix; 50/50 randomized runs pass, and so do the other json_shared_typed_paths and columns cache JSON tests.


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

Version info

  • Merged into: 26.11.1.1104-master (included in 26.11 and later)

…cache

The first reader of a JSON column in a read call decodes every requested
SHARED path and puts the decoded columns into the substreams cache; the
other readers of the same column take their paths from there through
SerializationObjectTypedSharedData::insertPathFromCache, which was a plain
insertRangeFrom. For an empty Dynamic destination (or a JSON one, whose
dynamic paths are Dynamic) that keeps SharedVariant at local discriminator 0
and appends the decoded variants after it, i.e. a Variant column whose
variants are not in global order.

Without the columns cache the readers of a call are the same in every call,
so that column is only ever appended to. With the columns cache the readers
of one call can differ granule by granule: when the other reader is served
from the cache, this reader becomes the first reader and decodes the next
granule straight into that column, and SerializationVariant throws
"Trying to deserialize data into Variant column with not global variants
order" (LOGICAL_ERROR, abort in debug builds). Seen in
05296_json_shared_typed_paths_nested_types with use_columns_cache = 1.

The remainder path of the same file already keeps the invariant that a
destination is either empty or has the part's exact dynamic structure:
insertRangeFromDecoded takes the exact structure into an empty column first.
Use it in insertPathFromCache too. It covers all three callers: the whole
JSON column, a SHARED path read as a subcolumn, and a sub-object.

The test extends 05402_json_shared_typed_paths_columns_cache, which covers
the same reader-order mechanism with non-dynamic path types only.

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

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

Pre-publication review by an independent model (engine: codex; 1 finding) plus my own review of the diff.

# Sev Finding Verdict Evidence / action
1 ⚠️ The new 05402 blocks stay green if the asserted reads bypass the columns cache; assert ColumnsCacheHits/ColumnsCacheMisses from system.query_log DISAGREE Reads and writes are enabled by SET, the premise lines prove the granule entries exist, and a SELECT never reads without marks (MergeTreeReaderWide.cpp:86-94); without the fix every block aborts at its asserted read (4/4 fresh process, 10/10 randomized server runs).

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

Also noticed, not this PR: a Compact part without substream marks reaches the same message through a different copy in MergeTreeReaderCompact, also with a plain JSON column (columns cache for Compact parts, #127287).

Session id: cron:clickhouse-review-slot-10:20261026-173100

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. Each new block of 05402_json_shared_typed_paths_columns_cache, run alone in a fresh clickhouse local (Atomic database), aborts the unfixed debug build with the CI message; on a server, 10/10 randomized runs of the test abort it.
b Root cause explained? Yes. insertPathFromCache copied a decoded SHARED path into an empty Dynamic/JSON destination with insertRangeFrom, which leaves the variants in non-global order. With the columns cache a later call, where the other reader is served from the cache, decodes straight into that column and SerializationVariant throws.
c Fix matches root cause? Yes. The copy now takes the decoded column's exact structure into an empty destination first (insertRangeFromDecoded, the helper the remainder path already uses). No guard at the throw site and no setting pin.
d Test intent preserved / new tests added? Yes. Four blocks appended to 05402 (additions only): whole column, path read as a subcolumn, sub-object, and a Compact part with substream marks.
e Both directions demonstrated? Yes. Per block: 4/4 abort before the fix, 4/4 pass after (fresh process each). Whole test: 50/50 randomized runs pass with the fix.
f Fix is general across code paths? Yes. All three callers of insertPathFromCache and both readers that reach it (Wide, Compact with substream marks) are covered.
g Fix generalizes across inputs (params/datatypes/wrappers)? Yes. Dynamic, nested JSON, Array(Dynamic) and Tuple(Dynamic) paths measured; for non-dynamic path types takeExactDynamicStructureFrom is a no-op, and the existing 05402 blocks plus the json_shared_typed_paths family pass.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, format or on-disk change.
i Invariants and contracts preserved? Yes. A destination is empty or has the part's exact dynamic structure for every producer of its rows; the self-insert chassert from insertDataFromCachedColumn is kept.

Session id: cron:clickhouse-impl-slot-5:20261026-164900

@clickhouse-gh

clickhouse-gh Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f23fd57]

Summary: ✅


AI Review

Summary

Preserves the decoded dynamic structure when copying JSON SHARED paths from the substreams cache, preventing a later granule's direct deserialization from raising LOGICAL_ERROR. The regression cases cover the whole column, path, sub-object, and Compact-part readers. No actionable findings.

Final Verdict

Status: ✅ Approve.

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Oct 26, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @Avogar @Algunenano, could you review this and add the can be tested label so CI runs? insertPathFromCache copied a decoded SHARED path into an empty Dynamic/JSON destination with a plain insertRangeFrom, which leaves the variants out of global order, so with the columns cache a later granule decoded into that column hit the SerializationVariant logical error; it now uses insertRangeFromDecoded, like the remainder path.

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - da91460

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_asan_ubsan, targeted) / Logical error: Trying to deserialize data into Variant column with not global variants order (STID: 6636-6d82) and Server liveness check failed trunk bug, not from this PR: 05298_json_shared_typed_paths_ranges read (json.c.size0, json) from a compact part through the columns cache, and the abort is in MergeTreeReaderCompact::readData, a path this PR does not change #127881 (mine, open)
Stateless tests (amd_debug, sequential) / 05218_text_index_substreams_opened_lazily flaky, not from this PR: the runner's diagnosis names the randomized use_columns_cache = 1, and the tested merge commit predates the fix #127781 (mine, merged)
Build profile diff infra, not from this PR: the CI logs cluster never passed the readiness probe, so the job ended with LogClusterNotReady: the endpoint never became ready before comparing anything. 206 of 210 runs since 2026-10-26 22:00 UTC are red, on 176 PRs #126200 (mine, open)
Mergeable Check, PR praktika rollups of the rows above #127881, #127781 and #126200 (the owners of the rows above)

CIDB has no other FAIL or ERROR row on this head.

Session id: cron:our-pr-ci-monitor:20261027-170011

@Avogar

Avogar commented Oct 28, 2026

Copy link
Copy Markdown
Member

@groeneai when #127881 is merged, update this PR with master

@groeneai

Copy link
Copy Markdown
Collaborator Author

Done: merged master, which now includes #127881, in f5a1947. The diff of this PR is unchanged. On the merged debug build, 05402, 05456, 05296 and 05298 match their references, and a compact part without substream marks read as (json.j.k, json) through the columns cache (which aborted with only this PR's fix) now returns the expected result.

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - f5a1947

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
Fast test (arm_darwin) / 05227_mutate_part_ahead_of_table_metadata_version_mergetree flaky, not from this PR: the rewrite dropped the column prints 1 instead of 0. This job failed on this test in 90 other PRs since 2026-10-27 16:41 UTC, and this PR does not touch mutations #127925 (mine, open)
Stress test (amd_tsan) / ThreadSanitizer (STID: 1938-332e), Hung check failed, possible deadlock found, Possible deadlock on shutdown (see gdb.log) crash, not from this PR: heap-use-after-free in WriteBuffer::next from IOutputFormat::finalize in ClientBase::resetOutput of the embedded client. The two hung-check rows are the cascade of the long TSan report #127912 (mine, open)
Build profile diff infra, not from this PR: the job ended with LogClusterNotReady: the endpoint never became ready before comparing anything #126200 (mine, open)
Mergeable Check, PR praktika rollups of the rows above #127925, #127912 and #126200 (the owners of the rows above)

CIDB has no other FAIL or ERROR row on this head.

Session id: cron:our-pr-ci-monitor:20261028-143006

@Avogar

Avogar commented Oct 29, 2026

Copy link
Copy Markdown
Member

@groeneai update with master

@groeneai

Copy link
Copy Markdown
Collaborator Author

Done: merged current master (6624a33) in 15d73b2. The diff of this PR is unchanged. On the merged debug build, 05402, 05456, 05296 and 05298 match their references.

@clickhouse-gh

clickhouse-gh Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing f23fd57a2 with master 1f2bde142 (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

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, 8 s compile time in total, 7 of them have a recent master baseline.

Job report

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 15d73b2

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
Fast test (arm_darwin) / 05397_clickhouse_local_locked_path_untouched fails on macOS for every PR, not from this PR (its diff changes only SerializationObjectTypedSharedData.cpp and test 05402): the expected Another server instance in same directory is already running line is missing. 168 failures in 160 PRs since 2026-10-28 19:35 UTC, all in this job, 0 on master #128088 (mine, open)
Mergeable Check, PR praktika rollups of the row above #128088 (the owner of the row above)

CIDB has no other FAIL or ERROR row on this head.

Session id: cron:our-pr-ci-monitor:20261029-160007

@Avogar
Avogar added this pull request to the merge queue Nov 2, 2026
Merged via the queue into ClickHouse:master with commit 20409c6 Nov 2, 2026
354 checks passed
@robot-clickhouse robot-clickhouse added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 2, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-synced-to-cloud The PR is synced to the cloud repo labels Nov 2, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 5, 2026
Backport #127867 to 26.10: Fix `LOGICAL_ERROR` reading `SHARED` paths of dynamic types of a `JSON` column with the columns cache
leshikus pushed a commit that referenced this pull request Nov 13, 2026
… of dynamic types of a `JSON` column with the columns cache
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-json-datatype JSON/Dynamic/Object datatype: semi-structured column storage, typed and dynamic paths, sub-object... pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-not-for-changelog This PR should not be mentioned in the changelog 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.

5 participants