Skip to content

Size merge/mutation index granularity of JSON shared data by read size - #128157

Merged
nihalzp merged 5 commits into
masterfrom
shared-data-read-size-granularity
Nov 20, 2026
Merged

nihalzp merged 5 commits into
masterfrom
shared-data-read-size-granularity

Conversation

@Avogar

@Avogar Avogar commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

When a JSON column's shared data is written with the ADVANCED serialization, the data part keeps a second on-disk copy of the shared-data values (the ObjectSharedDataCopy* streams) for fast whole-column reads, in addition to the per-bucket representation. The constant adaptive index granularity of a merged part (and of a full-rewrite mutation) was sized by the source parts' uncompressed size on disk, which counts both copies. The per-row byte estimate was therefore too large, so the merged part got a smaller granularity and more marks than the data warrants.

This sizes the granularity by the data a read of the source parts actually decompresses instead. A new IMergeTreeDataPart::getTotalColumnsReadSize sums, for a Wide part, the streams a whole-column read touches (reusing the existing per-stream size hints and de-duplicating shared files), so a second representation of the same data is not counted; merges and full mutations use it. The accounting is shared by typed and dynamic shared data because it works off the reader's actual stream list rather than per-serialization logic. The bytes argument of createMergeTreeIndexGranularity is now computed lazily, since reading a part's prefixes for the read size is only needed on the constant-granularity path.

Changelog category (leave one):

  • Performance Improvement

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

Fixed the index granularity of merged parts of a JSON column being smaller than necessary (too many marks), caused by double-counting the shared data's second on-disk copy when sizing adaptive granularity.


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

Version info

  • Merged into: 26.12.1.8-master (included in 26.12 and later)

The constant adaptive index granularity of a merged or fully-rewritten
mutated part was sized by the uncompressed bytes of the source parts on
disk. For a JSON column the dynamic shared data keeps a second on-disk
copy of its values (the `ObjectSharedDataCopy*` streams of the `ADVANCED`
serialization, used for whole-column reads) in addition to the per-bucket
representation. Counting both copies made the per-row byte estimate too
large, so the merged part got a smaller granularity and too many marks
than the data warrants.

Size the granularity by the data a read of the source parts actually
decompresses instead. Add `IMergeTreeDataPart::getTotalColumnsReadSize`,
which for a Wide part sums the streams a whole-column read touches (using
the existing per-stream size hints and de-duplicating shared files), so a
second representation of the same data is not counted. Merge and full
mutation granularity use it; the default falls back to the on-disk size
for other part types. The bytes argument of `createMergeTreeIndexGranularity`
is now computed lazily, since reading a part's prefixes to get the read
size is only needed on the constant-granularity path.

This accounting is shared by typed and dynamic shared data because it
works off the reader's actual stream list rather than per-serialization
logic.

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

clickhouse-gh Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [4b5dcdb]

Summary: ✅


AI Review

Summary

The PR sizes constant adaptive granularity for merges and full-rewrite mutations using the streams actually read, avoiding duplicate accounting of JSON shared data. Reviewed head 4b5dcdbc3d36299df62863c96481c58cf155fd23, the current implementation, and every prior discussion thread. All four earlier findings are addressed; no new blockers or major findings remain. The regression test covers Wide merges, full mutations, and Compact-to-Wide merges. It passed locally with the PR-head fast-test binary, matching all nine reference lines, and passed in CI.

Final Verdict

✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-performance Pull request with some performance improvements comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Oct 29, 2026
Comment thread src/Storages/MergeTree/MergeTask.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 4b5dcdbc3 with master 8bcbbbe48 (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

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

Job report

Make the read-size accounting cheaper and bound its memory per the PR
review:

- Compute the read size per column and cache it per column, and size only
  a column with dynamic subcolumns through a reader; any other column's
  read size is its already-cached on-disk size. A reader over a single
  column holds only that column's prefixes, so a vertical merge's
  preparation no longer deserializes every column's prefixes (and, for a
  table with a JSON column, no longer holds all LowCardinality dictionaries)
  at once.
- A sample read (read_only_column_sample) lists streams and their sizes
  without reading data, so it no longer installs the uniform-marks
  callback that makes a LowCardinality prefix load the single-part
  dictionary eagerly.
- createMergeTreeIndexGranularity calls the byte callback only on the
  adaptive path; a non-adaptive part gets the fixed row granularity and no
  longer reads source prefixes for a size it ignores.
- A fully expired TTLDrop merge keeps the cheap on-disk size instead of
  reading source prefixes for a granularity its empty output will not use.

Also cover a full-rewrite mutation in the test.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Avogar and others added 3 commits November 12, 2026 18:30
A `Compact` part records only the total size of its columns; its
per-column size map is empty. Summing per-column read sizes therefore
gave zero for a `Compact` source, so a merge of `Compact` parts into a
`Wide` part with constant adaptive granularity sized its granules by no
bytes and wrote one huge granule (2 marks instead of about 500 for
10000 rows of 500-byte strings).

Move the per-column read size and its cache into `MergeTreeDataPartWide`,
which overrides `calculateTotalColumnsReadSize` with their sum. Other
part types keep `getTotalColumnsSize` as their read size.

Cover a `Compact`-to-`Wide` merge in the test.

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

# Conflicts:
#	src/Storages/MergeTree/MergeTreeReaderWide.cpp
The test inserted 10000-row parts with ~20 rows per granule, so each part
had about 500 granules. The `advanced` shared-data serialization writes a
per-granule structure for every granule, and under ASan a single run took
up to ~200s and timed out in the flaky check.

Shrink the data tenfold (1000-row parts, so about 50 granules) while
keeping the same granule-size regime and every pinned setting, so the
byte accounting still binds and the mark-count ratios -- which are
scale-invariant -- still tell the fixed layout from the buggy one.

Flaky check before the change:
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=128157&sha=fbd00224aaa739c6e2db340bd4a1d6bb2dbf3ec9&name_0=PR&name_1=Stateless%20tests%20%28amd_asan_ubsan%2C%20flaky%20check%29

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@nihalzp nihalzp self-assigned this Nov 18, 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 19, 2026
@nihalzp
nihalzp added this pull request to the merge queue Nov 20, 2026
Merged via the queue into master with commit 2941424 Nov 20, 2026
196 checks passed
@nihalzp
nihalzp deleted the shared-data-read-size-granularity branch November 20, 2026 05:13
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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 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.

3 participants