Skip to content

Announce the byte ranges MergeTree will read to the read buffers - #122250

Merged
CheSema merged 21 commits into
masterfrom
reader-executor-read-geometry
Oct 3, 2026
Merged

CheSema merged 21 commits into
masterfrom
reader-executor-read-geometry

Conversation

@CheSema

@CheSema CheSema commented Sep 25, 2026 •

Copy link
Copy Markdown
Member

The experimental ReaderExecutor knows only the read_until of the current task, and today that bound also caps what it fetches. The next PR bounds a fetch by the window and by the bytes MergeTree will read instead. This PR gives the executor that input; read behaviour does not change.

  • ReadBuffer::setRequestMap(ByteRangeSet): the byte ranges the caller will read until it announces another map; empty = nothing, no map = the whole file; advisory. The compressed buffers forward it, ReadBufferFromFileView moves it into its slice of the archive, ReaderExecutor stores it.
  • MergeTree announces the mark ranges each reader reads, and each stream converts them to byte ranges of its own file: all ranges the query reads from the part; with parallel replicas, the coordinator's assignment; minus the ranges a ranges refiner has dropped. Patch readers get the matching patch ranges.
  • Maps exist only with use_reader_executor.
  • IntervalSet in src/IO is renamed to ByteRangeSet: ReadBuffer.h now includes it, and the old name clashes with IntervalSet in Common/IntervalTree.h.

Tests: 05257-05261 (wide, compact, packed and patch parts, parallel replicas, refiner), gtest_byte_range_set, gtest_read_buffer_from_file_view.

Related: #115816
Related: #103706

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.10.1.1408-master (included in 26.10 and later)

Add `ReadBuffer::setRequestMap`: the sorted byte ranges the caller will
read, and nothing outside them. An empty map, the default, is the whole
file. The compressed buffers and `PipelineReadBuffer` forward it.
`ReadBufferFromFileView` shifts it into its slice of the archive and
announces the slice by default. `ReaderExecutor` stores the map; nothing
reads it yet.

MergeTree announces the mark ranges each reader reads over all its tasks:
the thread's stripe in `MergeTreeReadPool` and `MergeTreePrefetchedReadPool`,
all ranges of the part in the other pools, and the given ranges in merges.
A reader reused for the next task gets that task's map. Each stream converts
the marks to byte ranges of its file, only when `use_reader_executor` is on.

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

clickhouse-gh Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8091dba]

Summary: ❌

job_name test_name status info comment
Stateless tests (arm_asan_ubsan, azure, sequential) ERROR

AI Review

Summary
  • The new request_map plumbing for data, patch, parallel-replica, refined-range, and dynamic-subcolumn reads looks consistent, but the settings-to-reader refactor still misses the skip-index reader path.
  • MergeTreeIndexReader still creates MergeTreeReaderStream objects with exact index_ranges available, yet nothing now forwards those ranges into setRequestMap, so index-file reads keep the implicit whole-file map under use_reader_executor=1.
Findings
  • ⚠️ Majors
    • [src/Storages/MergeTree/IMergeTreeReader.h:105] The new propagation mechanism only covers IMergeTreeReader subclasses. MergeTreeIndexReader is not one of them, and its call sites in src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp:2816, src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp:3176, and src/Storages/MergeTree/PatchParts/RangesInPatchParts.cpp:424 already know the exact index_ranges they will read. After moving the map out of MergeTreeReaderSettings, those index streams now announce no map at all, so .idx / .mrk reads still look like whole-file reads to ReaderExecutor.
    • Suggested fix: store a MarkRangesPtr on MergeTreeIndexReader (or derive one from all_mark_ranges) and call stream->updateReadRequestMap(...) when each index stream is created, plus keep it refreshed if the reader ever changes ranges.
Tests
  • ⚠️ Add one focused test for an index-file read under use_reader_executor=1 (for example a minmax or text-index query) and assert that the logged Request map of ... lines for the index stream are bounded, not implicit whole-file reads. The current new tests only exercise data / patch / dynamic-column readers.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions: propagate the request map into MergeTreeIndexReader streams and cover that path with a focused test.

LLVM Coverage Report

⚠️ No coverage measurement for commit 8091dba: incomplete coverage measurement: 1 of 21 shard profiles are missing: LLVM_COVERAGE_FILE_it_8.profdata.

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Sep 25, 2026
The map of a part does not change during the query, so the readers get
it once, when they are created, and a reader reused for the next task
needs no update. This removes the stripes from `MergeTreeReadPool` and
`MergeTreePrefetchedReadPool` and the update path through the readers
and streams. With a cache, bytes a thread reads past its stripe serve the
neighbour thread. Without a cache, a stripe would save at most one window
per stream at each stripe boundary.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@clickhouse-gh clickhouse-gh Bot added the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Sep 25, 2026
`ReadBuffer::setRequestMap` takes a `ByteRangeSet` instead of a vector of
ranges, so the map merges overlapping ranges itself and can carry the
methods the executor needs.

`IntervalSet` in `src/IO` is renamed to `ByteRangeSet`. `ReadBuffer.h`
now includes it, and the old name clashes with the `IntervalSet` template
in `Common/IntervalTree.h`. `ByteRangeSet::add` finds its place by binary
search, so ranges added in order no longer scan the set from the front.
The new `intersect` and `shift` clip and move the map in
`ReadBufferFromFileView`.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Patch readers were created with reader settings that carry no request
map, so the streams of patch parts announced the whole file.
`MergeTreeReadTaskInfo::patch_request_maps` now holds, for each patch
part of a part, the patch ranges that the part's readers read:
`RangesInPatchParts::getRanges` for all ranges the query reads from the
part. The pools compute them after `RangesInPatchParts::optimize`,
because the ranges of `Join` patches depend on all parts of the query.
Merges use the patch ranges they already compute for their ranges. The
maps are fixed for the query, so reused patch readers need no update.

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

clickhouse-gh Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 8091dbacb with master c662045b0 (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

566 object files changed (+408.40 KiB total), 1 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Storages/MergeTree/MergeTreeReadPoolBase.cpp.o 338.34 KiB 406.06 KiB +67.72 KiB (+20.02%)
src/CMakeFiles/clickhouse_common_io.dir/IO/ByteRangeSet.cpp.o new 34.08 KiB +34.08 KiB

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

Compile time of recompiled translation units

3119 translation units recompiled, 18036 s compile time in total, 3118 of them have a recent master baseline.

Median compile-time ratio to the baselines is ×1.09 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost +1554.3 s (+9%) in total before that adjustment.

Job report

CheSema and others added 4 commits September 29, 2026 13:44
With parallel replicas, the map of a part holds the ranges of all
replicas, while the coordinator assigns each replica a part of them
during the query. `MergeTreeReadPoolParallelReplicas` and
`MergeTreeReadPoolParallelReplicasInOrder` now give each task the ranges
of the part in the current assignment, and a reader reused for the task
announces them again. The other pools pass no map, so their readers keep
the map of the whole part and get no updates.

Maps now exist only with the reader executor. `buildReadTaskInfo` and
`MergeTreeSequentialSource` create them only when it is on, and the rest
of the code acts only on a map that exists, so the other read paths copy
and convert nothing. The fields of `MergeTreeReadTaskInfo` are renamed to
`read_request_map` and `patch_read_request_maps`.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
`describe` lists all ranges of a set with fewer than 10, and the first 4
and the last 4 of a longer one. `ReaderExecutor` logs the request map
with it. The request map tests read the range count from the new log
line, and they pin the settings that change the layout of the test
parts: sparse serialization adds a small stream for each column, the
range-splitting injection reads one part as two, and `v1` patch parts
can use `Join` patches, which map to all patch ranges of the part.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
`ICacheProvider.h`, `DiskCacheProvider.h` and `PageCacheProvider.h`
included the header without using it. `ReaderExecutor.h`, which holds a
`ByteRangeSet`, now includes it itself. The comments of
`ReadBuffer::setRequestMap` and `MergeTreeReaderSettings::request_map`
no longer describe the order of the ranges.

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

The intervals are sorted and disjoint, so both skip to the first interval
that ends after the start of the range with `std::partition_point`, the
same way `add` does, and stop at the first interval past its end.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
CheSema and others added 6 commits September 29, 2026 16:32
A ranges refiner (`use_indexes_refiner_in_read_pools`) decides task by
task which ranges survive the skip indexes or the projection index, after
the map of the part was taken. `MergeTreeReadPoolBase::refineReadRanges`
now records the ranges it drops from each part, and the readers of a
task get the map without them (`mapWithoutDroppedRanges`), so the ranges
of later tasks stay in the map until the refiner drops them. The tasks of
a part share one map until the refiner drops more, so a reused reader
announces again only then. The prefetched pool passes the same map to the
readers it creates in its refining job. Patch readers keep the map of the
patch ranges for the whole part.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
With parallel replicas or a ranges refiner, the readers of a task get a
map narrower than the part's, but patch readers still announced the
patch ranges of the whole part. When the map is narrower, the pool now
derives the patch maps from it with `RangesInPatchParts::getRanges`, the
function the tasks use for their patch ranges, and caches them for the
part until the map changes. New patch readers get them, and
`Readers::updateRequestMap` gives them to reused patch readers too.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
`recordDroppedRanges` rebuilt the merged set of dropped ranges for every
cut, and a new map subtracted all of them from the base again. The dropped
ranges are now appended as the refiner reports them, and the map
remembers how many of them it already leaves out (`dropped_in_map`), so a
new map subtracts only the entries added since. A new base, as a new
assignment of parallel replicas, still starts from all entries.
`patch_maps_of` is renamed to `patch_maps_source`.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
On a case-insensitive disk (macOS), `replaceFileNameToHashIfNeeded` stores every
stream under the hash of its name, so `05259`, `05260` and `05261` did not find
`k.bin` or `v.bin` in the log. Take the file name from
`system.parts_columns.filenames` instead of the `replace_long_file_name_to_hash = 0`
pin.

Under the load of the flaky check, the insert of 1M rows took up to 157 s and
`05261` ran longer than 180 s. Use 100K rows with `index_granularity = 128`,
which keeps the portions of the coordinator at about 160 marks.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=122250&sha=8bc1cbdcbc905270e722425f492d0e3ca1f98cc5&name_0=PR
PR: #122250

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

`subtractMarkRanges` takes `what` as a `std::span`, so the entries of `dropped`
that the actual map does not include yet are sorted in place instead of copied.

Rename the helpers after what they return: `getActualReadRequestMap` (was
`mapWithoutDroppedRanges`), `getActualPatchReadRequestMaps` (was `patchMapsFor`),
`announceReadRequestMap` and `updateReadRequestMap`. `PartMaps` is now
`PartReadRequestMaps`, with `initial_map`, `actual_map` and `actual_patch_maps`.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@CheSema
CheSema marked this pull request as ready for review September 30, 2026 10:15
An empty set can reach the executor for a caller that reads nothing: a patch
part that the task's map does not touch, mark ranges of zero bytes, or a view
map outside the view's slice. Without a map, the caller may still read the
whole file, so the default does not change. `ReadBufferFromFileView` announces
its slice explicitly, and `ReaderExecutor` keeps `std::optional<ByteRangeSet>`.

Related: #122250 (comment)

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@kssenii kssenii self-assigned this Sep 30, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 33 queries analysed

This PR adds a byte-range request map that MergeTree read pools pass down to the read buffers. It is built only when the reader executor is enabled, and the executor only stores the map and logs it at test level. Nothing in the read path uses it to change read-ahead or I/O yet. ClickBench Q31 (+16.2%) and Q30 (+20.4%, inside master's variance band) are both heavy GROUP BY ClientIP aggregations whose wall time grew by about 45 ms while CPU time stayed flat (within about 1%). That pattern does not match extra work from this change, so Q31 is downgraded to not_sure and the Q30 suppression stands. The full diff was too long to review in one piece, and I could not confirm whether the reader executor is enabled in this benchmark setup, so attribution has medium confidence. No tpch_adapted_1_official queries were flagged.

clickbench

⚠️ 2 inconclusive

Flagged queries (2 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 30 not_sure 226 272 +20.4% <0.0001 aggregation: The +20.4% rise falls inside master's current variance band and CPU is flat (2349 vs 2375 ms). The diff has no code path that would explain it, so the suppression stands.
⚠️ 31 not_sure 278 323 +16.2% <0.0001 aggregation: The +16.2% wall-time rise has no matching CPU increase (2820 vs 2842 ms). The PR only passes an advisory read map that the reader executor stores and logs, so nothing on this aggregation path does more work.

Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 9dc261c6-a418-4379-a323-2050210d8b37
  • MIRAI run: a12fd235-26ff-4e5f-89f0-7893ce785ade
  • PR check IDs:
    • clickbench_2986745_1790986619
    • clickbench_2986848_1790986624
    • clickbench_2986960_1790986658
    • tpch_adapted_1_official_2987070_1790986715
    • tpch_adapted_1_official_2987177_1790986757
    • tpch_adapted_1_official_2987292_1790986823

CheSema and others added 2 commits October 2, 2026 16:46
The source creates its readers once and never updates their maps, so it does
not need a copy of `MergeTreeReadTaskInfo` to carry them.

Related: #122250 (comment)

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
`createReaders` and `Readers::updateReadRequestMap` index the patch maps by the
position of the patch part. A vector of another size would read past the
readers, so it is a `LOGICAL_ERROR`. An empty vector still means no patch maps.

Related: #122250 (comment)

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

Every reader built from a copy of the settings inherited the map, so the index
readers had to clear it, and `createReaders` copied the settings only to carry
it. Now `IMergeTreeReader` holds the map: `createReaders` sets it on each new
reader, and readers pass it to their streams. `MarkRangesPtr` moves next to
`MarkRanges`.

Related: #122250 (comment)

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
CheSema and others added 2 commits October 2, 2026 20:02
…nt request map

`MergeTreeReaderWide::getOrAddStream` also creates streams after the map was
announced, for the subcolumns of a `Dynamic` column that it finds in the prefix,
so `updateReadRequestMap` never reached them. Each new stream now takes the
reader's current map.

Related: #122250 (comment)

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

Related: #122250 (comment)

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@CheSema
CheSema enabled auto-merge October 2, 2026 19:20
@CheSema
CheSema disabled auto-merge October 2, 2026 21:42
…uest_map`

In ClickHouse Cloud a compact part keeps up to `compact_parts_max_granules_to_buffer`
granules of each column together. With a random size of 112 the whole part was
one stripe, the single-buffer reader announced the map of the stripe, and the
two key ranges merged into one. The setting has no effect in the public build.

CI report: https://d1k2gkhrlfqv31.cloudfront.net/clickhouse-test-reports-private/praktika.html?PR=78395&sha=f877070f763ac37ec47f5cbb8c1612117c72ab4b&name_0=PR
Related: ClickHouse/clickhouse-private#78395

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants