Skip to content

Resolve the streams of dynamic subcolumns once in the wide part reader - #132877

Open
groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/wide-reader-resolve-dynamic-substreams-once
Open

groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/wide-reader-resolve-dynamic-substreams-once

Conversation

@groeneai

@groeneai groeneai commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #124792
Related: #131011

Changelog category (leave one):

  • Performance Improvement

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

Reading JSON and Dynamic columns from Wide parts is faster: the streams of their dynamic paths and types are resolved once per reader instead of for every block. For example, reading 32 dynamic paths of a JSON column takes about 11% less time.

Description

#131011 resolves the substreams that addStreams enumerates once per reader, but the substreams only the prefix of a column lists (dynamic paths of JSON, variants of Dynamic) are unknown in addStreams. For every block, the readData callbacks of each of them rebuilt the stream name (getStreamNameForColumn) and looked up its avg_value_size_hints slot again. #131011 left this as a follow-up because the prefix is deserialized by several threads.

readData now adds such a substream to the column's ResolvedSubstreams on first use (resolveAndAddSubstream, reusing resolveSubstream and avgValueSizeHintSlot). No lock is needed: these callbacks run on the reading thread after deserializePrefix has returned, and the prefixes pool is drained inside it. The prefix callbacks, which can run on that pool, and the prefetch callbacks still only look paths up.

Validation: local Release builds, both servers pinned to one NUMA node, 40 paired runs measured in both orientations, ratios PR / master:

Query Ratio [95% CI]
json_subcolumns_required_conversions q0 0.892 [0.883, 0.900]
SELECT json of that table (400 paths) 0.878 [0.847, 0.910]
wide_part_many_substreams q0 / q1 / q2 0.989 / 0.960 / 1.016
json_shared_data_wide q0 / q1 / q2, max_threads = 1 1.008 / 1.045 / 0.945

Peak memory is unchanged. A TSan build ran 180 reads, 6 at a time, of a 200-path JSON + Dynamic table with the prefixes pool active (PrefixReader threads in every SELECT json) and 43 JSON/Dynamic Wide-part tests, with no report. None of the 1057 json/dynamic/variant stateless tests fails only with this change.

The finding and the original patch are from @ snelheidai in #124792 (comment).


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

ClickHouse#131011 resolves the substreams `addStreams` enumerates once per reader, but
the substreams only the prefix lists (dynamic paths of `JSON`, variants of
`Dynamic`) missed its map, so the `readData` callbacks of every block
recomputed their file name (`getStreamNameForColumn`: name building,
escaping, checksums lookup) and their `avg_value_size_hints` slot.

`readData`'s callbacks now add such a path to the column's
`ResolvedSubstreams` on first use (`resolveAndAddSubstream`, which reuses
`resolveSubstream` and `avgValueSizeHintSlot`), so later blocks find it.

No synchronization is needed: the callbacks of `readData` run on the
reading thread, after `deserializePrefix` returned, and the only thread
pool in the serializations (the prefixes pool of `SerializationObject`) is
drained inside `deserializeBinaryBulkStatePrefix`. The prefix and prefetch
callbacks, which can run on that pool, keep only looking paths up.
Readers prefetched on the prefetch pool are handed to the reading thread
through `waitForAllToFinishAndRethrowFirstError`.

Taking the hint slot through `avgValueSizeHintSlot` keeps this call site
coupled at compile time to its signature, which ClickHouse#132369 changes.

Related: ClickHouse#124792
Related: ClickHouse#131011

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; 0 findings) plus my own cold review of the diff.

# Sev Finding Verdict Evidence / action
1 💡 Description said the prefetch callbacks run on the prefixes pool AGREE, fixed in the description prefetchForColumn runs on the reading thread or on the prefetch pool before hand-off; no code change

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

Session id: cron:clickhouse-review-slot-8:20261119-043700

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Performance change. Release A/B of json_subcolumns_required_conversions q0, both servers pinned to one NUMA node, 40 paired runs in both orientations: 0.892 [0.883, 0.900] PR / master; the sign is the same in each orientation.
b Root cause explained? addStreams runs before any prefix is read, so the dynamic paths of JSON and variants of Dynamic are missing from ResolvedSubstreams; every readData callback of every block then rebuilt their stream name and size-hint slot.
c Fix matches root cause? Yes: such a path is resolved once with the existing resolveSubstream and added to the same map under the same key on first use; no guard, no new structure.
d Test intent preserved / new tests added? Behaviour-preserving (cached value = recomputed value), so no functional test. The existing perf test json_subcolumns_required_conversions.xml q0 covers the path. None of the 1057 json/dynamic/variant stateless tests fails only with this change; 7 Wide-part tests 50/50 each.
e Both directions demonstrated? Master vs PR binaries on identical data: q0 155.2 to 137.9 ms, SELECT json 2347 to 2134 ms; guards wide_part_many_substreams and json_shared_data_wide within noise.
f Fix is general across code paths? Only readData's callbacks insert. The prefix callbacks, which can run on the prefixes thread pool, and the prefetch callbacks stay lookup-only; Compact readers have no such map.
g Fix generalizes across inputs (params/datatypes/wrappers)? Checked JSON dynamic paths (whole object and subcolumns, 32 to 400 paths), JSON shared data, Dynamic variants and statically enumerable Tuple/Array columns, with prefetch on and off and the prefixes cache off.
h Backward compatible? (maintainer-approved exception only) Yes: no format, setting or protocol change.
i Invariants and contracts preserved? The map is inserted only on the reading thread while no prefix task runs. TSan build: 180 reads, 6 at a time, with PrefixReader threads active, plus 43 tests, no report. References stay valid (no erase, unordered_map insertion invalidates none).

Session id: cron:clickhouse-impl-slot-5:20261119-002200

@clickhouse-gh

clickhouse-gh Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [09fd123]

Summary: ❌

job_name test_name status info comment
Stateless tests (arm_binary, ParallelReplicas, plan-based, s3 storage, parallel) FAIL
05829_autopr_skip_index_on_read_in_subquery FAIL cidb
05829_autopr_skip_index_on_read_in_subquery FAIL cidb
05829_autopr_skip_index_on_read_in_subquery FAIL cidb
05100_autopr_gate_lazy_read FAIL cidb
01710_projection_with_mixed_pipeline NOT_FAILED cidb
04353_text_index_explicit_tokenizer_argument NOT_FAILED cidb
05033_lazy_materialization_column_function_structure_equals NOT_FAILED cidb
05034_lazy_materialization_column_function_set_capture NOT_FAILED cidb

AI Review

Summary

Reviewed #132877 at 09fd123bc48cac1247441e3e0f5aa1c34616fae3. The change caches substream resolution for dynamically discovered JSON paths and Dynamic variants in Wide readers. No actionable findings. The reported paired benchmarks and functional/TSan validation support the change; no local build or tests were run during this review.

Final Verdict

✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Nov 19, 2026
@clickhouse-gh clickhouse-gh Bot added can be tested Allows running workflows for external contributors comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Nov 19, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 09fd123

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, plan-based, s3 storage, parallel) / 05829_autopr_skip_index_on_read_in_subquery not from this PR (it changes only MergeTreeReaderWide): a deterministic red of this test under the plan-based parallel replicas job, on 12 master commits since 2026-11-19 00:41Z (master report) and on 51 other PRs #132867 (mine, open)
Stateless tests (arm_binary, ParallelReplicas, plan-based, s3 storage, parallel) / 05100_autopr_gate_lazy_read not from this PR: the runner's diagnosis names the randomized query_plan_max_limit_for_top_k_optimization 100000 (44/44 red with the same settings, 40/40 green without); with it the plan-based job keeps the Top-K read local and collects no statistics. Same red in this job on 4 other PRs in 30 days #132873 (mine, open)
PR praktika rollup of the rows above #132867, #132873 (mine, open)

The other CIDB FAIL row on this head is Config Workflow / Pre Hooks of the run before the can be tested label. Mergeable Check is red only because of the rows above; CH Inc sync is green.

Session id: cron:our-pr-ci-monitor:20261119-080025

@Avogar Avogar self-assigned this Nov 19, 2026
@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 20, 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-part-formats Formats of MergeTree data parts in the public repository, that is how a part is laid out on disk ... pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants