Repository navigation
Compact parts: write stripes and read them with a buffer per column - #118498
alexey-milovidov wants to merge 64 commits into
Conversation
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]>
|
Workflow [PR], commit [613e387] Summary: ❌
AI ReviewSummaryThe PR enables striped Consumers traced: query, 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
Final VerdictStatus: ❌ Block Resolve the existing default-layout rollout decision before approval. No duplicate inline comment or thread reply was posted; the existing thread remains open. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes89 object files changed (+177.78 KiB total), 1 added.
712 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units312 translation units recompiled, 3297 s compile time in total, 311 of them have a recent master baseline. Translation units without a recent master baseline:
|
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]>
|
🕵 Merged
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, |
…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]>
|
🕵 Merged
Whether stripes should be the open-source default is still a design decision for the reviewer (see the note in the description). |
…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]>
…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]>
|
🕵 Pushed 9bf0c6c: merged
The design question about the open-source default of |
…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]>
|
🕵 Pushed 3f80e08: merged The only open review thread (whether the open-source default of |
`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]>
…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]>
|
🕵 Pushed 95a3b17: fixed the
The only open review thread (whether the open-source default of |
# 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]>
|
🕵 Merged master (it had two trivial
The open thread about the default |
…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
Related: #118475
The
MergeTreesettingscompact_parts_max_bytes_to_bufferandcompact_parts_max_granules_to_buffer, and the query settingmerge_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_buffergranules (128 by default) orcompact_parts_max_bytes_to_bufferbytes (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. Withcompress_per_column_in_compact_partsdisabled every granule remains its own stripe.fillIndexGranularityassumed 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 ingetGranulesToWrite).Reader. Parts with at least
merge_tree_compact_parts_min_granules_to_multibuffer_readgranules (16 by default), or at least as many granules as columns to read, are read by the newMergeTreeReaderCompactMultipleBuffers: 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
webdisk behind an HTTP server with 30 ms latency per request; "single" is the previous single-buffer reader, "multi" the new one):SELECT sum(c1)SELECT sum(c1)SELECT sum(c1), sum(c10), sum(length(s))SELECT sum(c1), sum(c10), sum(length(s))SELECT * ... ORDER BY key DESC LIMIT 1SELECT * ... ORDER BY key DESC LIMIT 1So 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 = 1as the open-source default (the previous layout) and let users opt in; the code is the same either way.Notes:
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_*) setcompact_parts_max_granules_to_buffer = 1and are unchanged.02345_striped_compact_partsnow actually tests stripes.Changelog category (leave one):
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
MergeTreesettingscompact_parts_max_granules_to_buffer(default 128) andcompact_parts_max_bytes_to_buffer(default 128 MiB), and the reader bymerge_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]