Skip to content

Compact parts: write stripes and read them with a buffer per column - #118498

Open
alexey-milovidov wants to merge 64 commits into
masterfrom
compact-parts-stripes-pr
Open

alexey-milovidov wants to merge 64 commits into
masterfrom
compact-parts-stripes-pr

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 7, 2026 •

Copy link
Copy Markdown
Member

Related: #118475

The MergeTree settings compact_parts_max_bytes_to_buffer and compact_parts_max_granules_to_buffer, and the query setting merge_tree_compact_parts_min_granules_to_multibuffer_read, were declared in open source but documented as "Only available in ClickHouse Cloud" and had no effect: the Compact part writer flushed every granule as soon as it was complete with the columns of the granule adjacent, and the only reader used one buffer for all columns. This makes them work the way the stripe-aware reader stream code (MergeTreeReaderStreamMultipleColumns, in open source since the compact reading refactoring of March 2024) already expects.

Writer. A Compact part is written in stripes. Rows are buffered until they form compact_parts_max_granules_to_buffer granules (128 by default) or compact_parts_max_bytes_to_buffer bytes (128 MiB by default); a block that already exceeds the limits is written directly, split into stripes. Inside a stripe the columns are written one after another: all granules of the first column, then all granules of the second column, and so on, and every granule of every column still starts a new compressed block, so the meaning of marks does not change. With compress_per_column_in_compact_parts disabled every granule remains its own stripe. fillIndexGranularity assumed the buffer holds at most one incomplete granule; it now skips the complete buffered granules (without this, merges into Compact parts failed with a logical error in getGranulesToWrite).

Reader. Parts with at least merge_tree_compact_parts_min_granules_to_multibuffer_read granules (16 by default), or at least as many granules as columns to read, are read by the new MergeTreeReaderCompactMultipleBuffers: a buffer per column, so every buffer reads its column sequentially inside a stripe and the columns are prefetched in parallel by the prefetched read pool. A part written without stripes (detected from the first two marks) is read through one shared buffer, because separate buffers would read the whole file each.

Measurements (a 2M-row Compact part with 22 columns, 178 MB, 245 granules = 2 stripes, read from a web disk behind an HTTP server with 30 ms latency per request; "single" is the previous single-buffer reader, "multi" the new one):

query layout, reader requests bytes
SELECT sum(c1) granule-major, single (previous behavior) 42 28.6 MB
SELECT sum(c1) stripes, multi 44 7.9 MB
SELECT sum(c1), sum(c10), sum(length(s)) granule-major, single 42 113.8 MB
SELECT sum(c1), sum(c10), sum(length(s)) stripes, multi 108 24.8 MB
SELECT * ... ORDER BY key DESC LIMIT 1 granule-major, single 14 0.87 MB
SELECT * ... ORDER BY key DESC LIMIT 1 stripes, multi 35 0.86 MB
full scan of all columns granule-major, single 65 200 MB
full scan of all columns stripes, multi 739 175 MB

So reading a subset of columns transfers 3.6x to 4.6x fewer bytes, while reading all columns of one granule costs one range request per column instead of one, and a full scan of all columns makes one request per column per read task instead of one per task. This is the trade-off ClickHouse Cloud has been running with (with the filesystem cache in front of object storage); for tables with few wide reads and many narrow ones it is a clear win, for "read everything" workloads on object storage it increases the number of requests. The reviewer may prefer to keep compact_parts_max_granules_to_buffer = 1 as the open-source default (the previous layout) and let users opt in; the code is the same either way.

Notes:

  • The test harness already randomizes both writer settings (tests/clickhouse-test), so randomized stripe layouts get coverage in all Compact-part tests; tests that pin the physical layout (02947_merge_tree_index_table_*) set compact_parts_max_granules_to_buffer = 1 and are unchanged. 02345_striped_compact_parts now actually tests stripes.
  • Parts written in stripes are readable by versions with the stripe-aware reader (since 24.4), not by older ones.

Changelog category (leave one):

  • Performance Improvement

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

Compact parts are written in stripes, inside which the data of each column is contiguous, and are read with a buffer per column, so reading a subset of columns from a Compact part transfers several times fewer bytes from object storage. The stripe size is controlled by the MergeTree settings compact_parts_max_granules_to_buffer (default 128) and compact_parts_max_bytes_to_buffer (default 128 MiB), and the reader by merge_tree_compact_parts_min_granules_to_multibuffer_read; all three previously had an effect only in ClickHouse Cloud.


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

alexey-milovidov and others added 3 commits September 7, 2026 01:06
The `MergeTree` settings `compact_parts_max_bytes_to_buffer` and
`compact_parts_max_granules_to_buffer`, and the query setting
`merge_tree_compact_parts_min_granules_to_multibuffer_read`, were declared in
open source but documented as available only in ClickHouse Cloud and had no
effect. This makes them work the way the reader already expects.

A Compact part is now written in stripes. Rows are buffered until they form
`compact_parts_max_granules_to_buffer` granules or
`compact_parts_max_bytes_to_buffer` bytes; a block that already exceeds the
limits is written directly, split into stripes. Inside a stripe the columns are
written one after another: all granules of the first column, then all granules
of the second column, and so on. Every granule of every column still starts a
new compressed block. With `compress_per_column_in_compact_parts` disabled every
granule remains its own stripe, because all its columns must share a compressed
block. `fillIndexGranularity` assumed that the buffer holds at most one
incomplete granule; it now skips the complete buffered granules.

Compact parts with at least `merge_tree_compact_parts_min_granules_to_multibuffer_read`
granules (or at least as many granules as columns to read) are read by the new
`MergeTreeReaderCompactMultipleBuffers`: a buffer per column, so that every
buffer reads its column sequentially inside a stripe and the columns are
prefetched in parallel. If a part was written without stripes, all columns
share one buffer, because separate buffers would read the whole file each.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Co-Authored-By: Claude Fable 5.1 <[email protected]>
…t filter in the new test

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [613e387]

Summary: ❌

job_name test_name status info comment
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/can_be_merged.py --review-threads FAIL

AI Review

Summary

The PR enables striped Compact parts and a reader that chooses shared or per-column buffers for each task. Reviewed head: 613e387ddc9ad8a77a1239d8277d720ecffdee82. Previous implementation findings are addressed in the current code; no new inline findings were identified. The existing default-layout rollout concern remains unresolved.

Consumers traced: query, PREWHERE, merge and mutation readers use stripe-aware marks; reader reuse updates the buffer strategy; the reader executor receives per-stream request bounds; the columns cache serves granules independently of their physical layout; mergeTreeIndex exposes the unchanged marks representation; part consistency and checksum checks validate marks/file shape and compressed file contents.

The Praktika reports report 15,431 passed tests, zero failures and 4,591 skipped tests. No local build or tests were run because this review job has no ClickHouse binary.

Findings

❌ Blockers

  • src/Storages/MergeTree/MergeTreeSettings.cpp:294 — Activating the existing default changes every OSS table's layout without a compatibility rollout. [dismissed by author -- https://github.com/Compact parts: write stripes and read them with a buffer per column #118498#discussion_r4186299769] The author has left the default to a maintainer; the concern still holds because the writer now applies compact_parts_max_granules_to_buffer = 128, while previously OSS wrote one granule per stripe. The PR's measurements show an uncached full scan increasing from 65 to 739 requests, and the author's point-read measurements show a reproducible 10–16% regression. A reader heuristic cannot restore adjacency after the striped part is written. Setting the value to 1 changes future writes and rewrites, but existing tables do not receive that protection automatically. Keep the previous OSS layout by default and allow opt-in stripes, or provide a versioned rollout that restores the previous layout under compatibility settings. Ensure the settings-history handling cannot inadvertently enable stripes when restoring older compatibility.
Final Verdict

Status: ❌ Block

Resolve the existing default-layout rollout decision before approval. No duplicate inline comment or thread reply was posted; the existing thread remains open.

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Sep 7, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Sep 7, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 95a3b1709 with master cb2cbd899 (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.

Object file sizes

89 object files changed (+177.78 KiB total), 1 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Storages/MergeTree/MergeTreeReaderCompactMultipleBuffers.cpp.o new 160.03 KiB +160.03 KiB

712 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Slowest function optimization changes (ThinLTO)

Median per-function time ratio to the master baseline: programs/clickhouse ×0.60, programs/clickhouse-keeper ×0.68 (each binary is linked separately, on a different machine and with different build flags); deltas below are relative to it.

Binary Function Master PR Δ vs median
programs/clickhouse DB::registerFunctionConversion(DB::FunctionFactory&) 5.8 s 8.9 s +5.4 s (+154%)
programs/clickhouse _ZN6detail10static_forITk7is_enumN2DB23AttributeUnderlyingTypeEZNS1_29callOnDictionaryAttributeType… 3.3 s 7.2 s +5.2 s (+260%)
Compile time of recompiled translation units

312 translation units recompiled, 3297 s compile time in total, 311 of them have a recent master baseline.

Translation units without a recent master baseline:

  • src/Storages/MergeTree/MergeTreeReaderCompactMultipleBuffers.cpp: 8.1 s

Job report

alexey-milovidov and others added 4 commits September 11, 2026 12:34
The rows buffered by `MergeTreeDataPartWriterCompact` are written with
`getGranulesToWrite(last_block = false)`, which requires every granule of the
block to be complete. The buffer, however, could reach the byte limit of a
stripe while holding complete granules and the beginning of the next one: the
block that appended the incomplete granule to the index granularity does not
fill it (see `fillIndexGranularityImpl`), and the byte limit does not care
about granule boundaries. The write then failed with `Required to write A
rows, but only B rows was written for the non last granule`.

Now the beginning of an incomplete granule stays in the buffer until the next
blocks complete it (and the incomplete granule of the very last block is
written by `finalizeIndexGranularity`, as before).

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…ructure`

`ColumnObject::fixDynamicStructure` froze only the number of dynamic paths,
but not the dynamic structure of the path columns themselves, unlike
`ColumnObject::takeExactDynamicStructureFrom` and unlike the `Array`, `Map`,
`Nullable` and `Tuple` columns, which pass the call through to the nested
column.

`MergeTreeDataPartWriterCompact::ColumnsBuffer::add` relies on it: the
accumulated column of the buffer must be written in the same set of
substreams as `block_sample`. Without it, inserting a block into the buffer
could extend the set of variants of a `Dynamic` column of a dynamic path (by
extracting a type from the shared variant of the inserted column), and the
write then failed with `Mismatch of internal columns of Dynamic`.

Reproduced by the existing test `03532_json_dynamic_updates`, where a
mutation of a Compact part writes several blocks of a `JSON` column whose
path is `Int64` in the first block and `Array(Int64)` in a later one.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…s correctly

Two findings of the review:

- The choice between `MergeTreeReaderCompactSingleBuffer` and
  `MergeTreeReaderCompactMultipleBuffers` used the number of marks of the
  whole part, while the trade-off depends on the granules the reader will
  actually touch: a point read of a few granules from a large part is cheaper
  with the single buffer. It now uses the number of marks of the read ranges.

- The detection of the stripes compared the mark of the second granule of the
  first column with the mark at position `1`, which is the second substream of
  the first column when the marks are written for substreams (the default). A
  striped part whose first column has several substreams (`Array`, `Map`,
  `Tuple`, `JSON`, ...) was therefore read with a single shared buffer. The
  comparison is now against the first substream of the second column.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Merged master (the branch was 2510 commits behind) and fixed both CI reds, which were ours (no occurrences of either logical error on master in the last 30 days, only this PR):

  • Logical error: Required to write A rows, but only B rows was written for the non last granule - the buffer of MergeTreeDataPartWriterCompact could reach the byte limit of a stripe while holding complete granules and the beginning of the next one, and getGranulesToWrite(last_block = false) requires every granule to be complete. Fixed in 59580d5: only a whole number of granules is written, the rest waits for the next blocks. Regression test added to 05113_compact_parts_stripes (it fails with Required to write 7 rows, but only 5 rows was written on a control binary without the fix).

  • Logical error: Mismatch of internal columns of Dynamic - ColumnObject::fixDynamicStructure froze only the number of dynamic paths, not the dynamic structure of the path columns, so inserting a block into the writer buffer could extend the set of variants of a Dynamic column of a path and diverge from block_sample. Fixed in 23a14f8. This is reproduced by the existing test 03532_json_dynamic_updates (a mutation of a Compact part): it fails on a control binary and passes with the fix.

Both review findings are addressed in 0101c39, see the replies in the threads. 42 related tests pass locally (the 4 failures are environmental: no ZooKeeper, cluster, part_log or access storage in the local server).

alexey-milovidov and others added 6 commits September 18, 2026 11:26
…gle buffer reader

`createForMergeMutation` did not carry `merge_tree_compact_parts_min_granules_to_multibuffer_read`,
so merges and mutations, which read all columns of whole parts, switched to
`MergeTreeReaderCompactMultipleBuffers` and multiplied the number of read requests on remote
storage (visible in `03164_s3_settings_for_queries_and_merges`) and the memory of mutations
(`01200_mutations_memory_consumption`). They now always use the single buffer reader.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
… the single buffer reader"

This reverts commit 9fe1d66.
…r memory

`03164_s3_settings_for_queries_and_merges`, `02242_system_filesystem_cache_log_table` and
`01200_mutations_memory_consumption` pin exact S3 request counts, filesystem cache log rows,
and mutation memory, which were measured for the previous layout where every granule is its own
stripe. With stripes (the new default) a merge of the `03164` part makes 6 requests with the per
column reader, a query in `02242` reads through two buffers, and a mutation buffers a whole stripe.
Set `compact_parts_max_granules_to_buffer = 1` in these tests, as `02947_merge_tree_index_table_*` do.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118498&sha=58fdb327baaa2f5c392fd361bf8351ab5ccf3491&name_0=PR
PR: #118498

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…act part

A merge reads a Compact part written in stripes with a buffer per column: 6 requests for this
part. With the single buffer, as the AI review suggested for merges and mutations, it seeks for
every column of every granule, which measured 1272 requests for the same part.

PR: #118498

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Merged master (the branch was 4150 commits behind; no conflicts) and handled the AI review blocker and the CI reds.

  • Blocker (merges and mutations use the buffer-per-column reader): I disagree with this one, based on a measurement. For a striped part, forcing the single buffer on merges makes the single-buffer reader seek for every column of every granule. The OPTIMIZE ... FINAL of a 1280-granule striped part on s3_no_cache made 1272 S3 requests that way, against 6 with MergeTreeReaderCompactMultipleBuffers. I tried the change and then reverted it, and gave the details in the review thread. The new test 05255_compact_parts_stripes_merge_s3_requests covers the merge path on a remote disk: it fails if the merge goes above 100 requests.
  • CI reds 03164_s3_settings_for_queries_and_merges, 02242_system_filesystem_cache_log_table, 01200_mutations_memory_consumption: these tests pin exact request counts, cache-log rows, or mutation memory, measured for the old granule-major layout. They now set compact_parts_max_granules_to_buffer = 1, as 02947_merge_tree_index_table_* already did. All three pass locally against MinIO and Azurite, and so do 05113, 05255, 02345 and 02947_*.

Whether stripes should be the open-source default is still a design decision for the reviewer (see the note in the description).

alexey-milovidov and others added 3 commits October 2, 2026 17:58
…e than one granule

The stripes of one part can have different numbers of granules (e.g. a granule bigger than
`compact_parts_max_bytes_to_buffer` is written as a separate stripe), so checking only the
first stripe could choose the shared buffer for a striped part.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A merge writes blocks of one granule, and the first granule is bigger than
`compact_parts_max_bytes_to_buffer`, so it is a separate stripe and the next granules
form larger stripes. Such a part must still be read with a buffer per column.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
alexey-milovidov and others added 3 commits October 4, 2026 15:49
…s` to a local disk

With the `s3` and `azure` storage policies by default, `CreatedReadBufferOrdinary` also counts the buffers of the
filesystem cache, so the numbers differ from the reference.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118498&sha=eb1fb71e701a26c34a748fb2746a3bfa587ac727&name_0=PR
#118498

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…olumns to read

Subcolumns of one column share a buffer of `MergeTreeReaderCompactMultipleBuffers` and missing columns have none,
so compare the number of granules with the number of distinct columns of the part to read, not with the number of
requested columns and subcolumns.

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

Copy link
Copy Markdown
Member Author

🕵 Pushed 9bf0c6c: merged master and fixed the CI failures of 098766e.

  • 05453_compact_parts_stripes_point_read_last_granule failed in all flaky checks and in the parallel runs. The test runner sets log_comment to the test file name, which matched LIKE '05453_%', so the queries of other runs were counted too. The test now filters by the exact comments; 8 repeated runs pass locally.
  • Addressed the review about the columns cache: a Compact range that the columns cache can serve as a whole is no longer prefetched. New test 05454_compact_parts_columns_cache_prefetch_skip.
  • Stateless tests (arm_asan_ubsan, targeted): Logical error: Trying to deserialize data into Variant column with not global variants order in 05298_json_shared_typed_paths_ranges. The stack passes through the new multi-buffer reader, but this error is in the columns-cache path for JSON and appeared in about 17 other PRs between 2026-10-26 and 2026-10-27. It is being fixed in Fix LOGICAL_ERROR reading a JSON or Dynamic column with its subcolumn from a compact part with the columns cache #127881 (reading a JSON column with its subcolumn from a Compact part with the columns cache, the same query shape) and Fix LOGICAL_ERROR reading SHARED paths of dynamic types of a JSON column with the columns cache #127867.
  • Build profile diff: an infrastructure error (LogClusterNotReady: the endpoint never became ready).

The design question about the open-source default of compact_parts_max_granules_to_buffer remains for a maintainer.

alexey-milovidov and others added 4 commits October 30, 2026 01:55
…e byte limit

`IColumn::byteSize` of `ColumnAggregateFunction` does not include the states
(they live in arenas or are allocated by the aggregate function), so
`compact_parts_max_bytes_to_buffer` did not bound the buffered data of
`AggregateFunction` columns: a stripe could hold 128 granules of about 8 KiB
`groupArray` states each with a 64 KiB limit.

The Compact writer now estimates the size of every incoming block once with
`getBlockSizeForGranularity` (which already accounts for nested aggregate
states, and is reused for the index granularity), tracks the size of the
buffer incrementally, and uses the same estimate to split blocks into stripes.

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

Copy link
Copy Markdown
Member Author

🕵 Pushed 3f80e08: merged master (clean, no conflicts). The PR's translation units and the clickhouse binary build; the stripe tests that can run in clickhouse-local match their references (the S3 and system.query_log parts are left to CI). The previous CI run had no failures.

The only open review thread (whether the open-source default of compact_parts_max_granules_to_buffer should stay 128 or go back to 1) is a design decision for a maintainer; the trade-offs are in the reply on that thread.

alexey-milovidov and others added 2 commits November 9, 2026 19:06
`MergeTreeReaderCompactMultipleBuffers` checked only `isRetryableException`
before calling `reportBroken`, unlike `MergeTreeReaderCompactSingleBuffer` and
`MergeTreeReaderWide`. A killed or timed-out query would then enqueue a part
check of a healthy part. Add `isQueryCancellation` to all three guards.

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

`writeStripe` called `setVectorDimensionsIfNeeded` on the first granule of
every stripe, and each call scans the full column of the block. With many small
stripes (e.g. `compact_parts_max_granules_to_buffer = 1`) this made `SZ3`
writes quadratic again. Pass an explicit flag so only the first stripe of a
block does it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
alexey-milovidov and others added 4 commits November 10, 2026 21:32
…f a reused reader

A reader is reused for the later read tasks of the same part through
`updateAllMarkRanges`, but both the choice between one buffer for all columns
and a buffer per column (made in `createMergeTreeReaderCompact` from the first
task's ranges) and the stripe detection in
`MergeTreeReaderCompactMultipleBuffers::init` (guarded by `initialized`) were
made only once. Reading in order starts with small tasks, so a reader created
for one granule, or for an isolated stripe of one granule, kept reading all
later multi-granule stripes with one buffer and read the data of columns that
were not requested.

Now `createMergeTreeReaderCompact` always creates
`MergeTreeReaderCompactMultipleBuffers`, which makes the choice in
`shouldUseSharedStream` for the current mark ranges, and its
`updateAllMarkRanges` drops the streams when the choice for the new ranges
differs, so they are created again on the next read.
`MergeTreeReaderCompactSingleBuffer` is no longer used and is removed: its
behavior is the shared-stream mode of the multibuffer reader.

The new test reproduces the scenario from the review: 32 MB and 21.5 MB of
file reads before the fix, 125 KB and 321 KB after.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…_log` by exact `log_comment`

The test runner sets `log_comment` to the test file name, which matched
`LIKE '05618_%'`, so the test's own queries were counted too.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118498&sha=latest&name_0=PR&name_1=Fast%20test
#118498

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

Copy link
Copy Markdown
Member Author

🕵 Pushed 95a3b17: fixed the Fast test failure of 8624590 and merged master (clean).

  • 05618_compact_parts_reused_reader_buffers counted the test's own queries: the test runner sets log_comment to the test file name, which matched LIKE '05618_%'. The test now filters by the exact comments, the same fix as in 05453 earlier.
  • The clickhouse binary builds after the merge, and the test matches its reference on a local server.

The only open review thread (whether the open-source default of compact_parts_max_granules_to_buffer stays 128) is still a design decision for a maintainer.

alexey-milovidov and others added 4 commits November 18, 2026 03:11
# Conflicts:
#	src/Storages/MergeTree/MergeTreeDataPartWriterCompact.h
#	src/Storages/MergeTree/MergeTreeDataPartWriterOnDisk.h
…at the first column to read

The buffer shared by all columns of a Compact part was prefetched from the
first column of the part, even when the reader reads only later columns.
`readGranule` then seeks to the requested column and discards the prefetched
data, so the prefetch read the data of an unrequested leading column.

Start the shared stream at the smallest position among the columns to read:
the columns of a granule are written in the order of their positions.
Add a test with a large unrequested leading column.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Master now answers `sum` over integer columns from the per-part column
statistics (`use_statistics_for_sum_avg_aggregation`), so the queries of these
tests did not read the part at all, and there was nothing to prefetch or to
put into the columns cache. Disable it in these tests.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118498&sha=95a3b17090af685e58a35b3c2a35409dc0c05dc2&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20parallel%29
#118498

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…uffer is prefetched

Since `createMergeTreeReaderCompact` always creates
`MergeTreeReaderCompactMultipleBuffers`, a Compact part read with one buffer
for all columns prefetches this buffer once, if the prefetch limit allows all
its columns (the previous single-buffer reader did not prefetch at all). The
test pinned the old behavior, and its result depended on the randomized
`MergeTree` settings, so it failed in some runs. Disable the randomization
and expect one prefetch.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118498&sha=95a3b17090af685e58a35b3c2a35409dc0c05dc2&name_0=PR&name_1=Stateless%20tests%20%28arm_binary%2C%20parallel%29
#118498

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

Copy link
Copy Markdown
Member Author

🕵 Merged master (it had two trivial [[nodiscard]] conflicts) and fixed the CI failures of 95a3b17:

  • 05404_compact_parts_prefetch_memory_budget and 05454_compact_parts_columns_cache_prefetch_skip: master now answers sum over integer columns from per-part column statistics, so these queries no longer read the part. The tests now disable use_statistics_for_sum_avg_aggregation.
  • 03394_filesystem_prefetches_limit: since 8624590, every Compact part is read by MergeTreeReaderCompactMultipleBuffers. When the limit covers all columns, it prefetches the shared buffer once; the old single-buffer reader never prefetched. The result also depended on the randomized MergeTree settings. The test now has the no-random-merge-tree-settings tag and expects one prefetch.

The open thread about the default compact_parts_max_granules_to_buffer = 128 is still a design decision for a human reviewer.

@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
alexey-milovidov and others added 3 commits November 22, 2026 09:50
…es-pr

Conflict: `master` changed `MergeTreeReaderCompactSingleBuffer` (reading the columns of
a Compact part in the order of the file, to avoid backward seeks), which this branch
replaced with `MergeTreeReaderCompactMultipleBuffers`. The files stay deleted, and the
change is ported to the new reader: `fillReadOrder` and the `read_order` used by
`readGranule`, which matters for the buffer shared by all columns.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…uired substream of a subcolumn

A query that reads only a subcolumn seeks to the marks of the substreams of this subcolumn,
but `MergeTreeReaderCompactMultipleBuffers::prefetchBeginOfRange` started the prefetch at the
first substream of the physical column. For `t Tuple(s String, b UInt64)` and
`SELECT sum(t.b)` it read `t.s`, and the read then discarded these bytes. It affected both
the buffer shared by all columns and the buffer per column.

The first position to read is now computed for every column to read: for a subcolumn in a
part with substream marks it is the smallest position among the substreams of its serialization
(for the types with a dynamic structure these are the substreams of the prefix, which are read
first and precede the data). The stream is prefetched at the minimum over its columns, and is
not prefetched if the position cannot be established.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
# Conflicts:
#	src/Storages/MergeTree/MergeTreeIOSettings.cpp
#	src/Storages/MergeTree/MergeTreeIOSettings.h

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

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