Repository navigation
Read the next Parquet file's offset index and first row group ahead when a source reads many files from object storage - #129495
Conversation
…n object storage sources A ClickHouse-written Iceberg data file is one row group (ClickHouse#128471), and a source of object storage reads its files one after another. `createReaderAsync` prepares the next file's reader in the background, but since ClickHouse#128607 only its footer is read: `ReadManager::init`, which reads the offset index and then the column data, ran on the file's first `read`, so every file cost a stream two dependent round trips that overlapped nothing. From a source's third file on (a query with a small LIMIT often needs only the first file), the next file's format now runs `ReadManager::init` in the background with decoding deferred: only offset-index tasks run, and `readAheadFirstRowGroup` starts the first admission round of the first row group if `Prefetcher::startPrefetchWithinLimit` fits it within the size of one read task (`4 x remote_read_min_bytes_for_seek`) and in what the stream's ColumnData share leaves free. The whole column chunks of that round must fit as well, checked before any page is handed out: handing out the pages of a chunk shorter than `min_bytes_for_seek` starts reading it with the ranges less than `min_bytes_for_seek` away (`Prefetcher::splitRange`), so when the round has such a chunk, the gaps between its chunks count too. That read also takes the likely-to-be-used ranges near the chunk, so with a short chunk only the file's last row group is read ahead, and only if no column is left for a later PREWHERE step. The deferred offset-index reads coalesce only with page-index ranges (`Prefetcher::startPrefetch` with `page_indexes_only`), so they take no column data along. Both readers of a source charge one counter (`InputFormatStreamMemory`), so the next file's reader does not get a second per-stream budget, which is why ClickHouse#128607 was narrowed to the footer. Bloom filters, dictionaries, the column index and all decoding wait for the first `read`. `addColumnDataPrefetches` hands each page of an offset-index chunk out once, so pages of reads that do not fit are handed back (restoring `data_pages_prefetch_idx`), which is exact only when the row filter skipped no page; without that, admission decodes through a page that was never started. The test reaches the hand-back with two long chunks around an unread column: the chunks fit in one read task, the read task over the gap does not. A failure of the read-ahead is stored before `read` can schedule the row group. An error of the background initialization (and of the prefetched footer, which ClickHouse#128607 reported without the file name) is rethrown when the source switches to the file, with the file's name. Not used for local object storage, the page cache, TopN dynamic filtering (its threshold may still tighten), `max_parsing_threads = 1`, `remote_filesystem_read_prefetch = 0`, or files smaller than `remote_read_min_bytes_for_seek` (decoding the previous file is too short to hide their request). `AggregateFunctionStatesFromValuesInputFormat` forwards `prefetch`. Analysis and original patch: @ snelheidai in ClickHouse#129392. Closes ClickHouse#129392 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Condense the read-ahead comments in ReadManager.cpp, ParquetV3BlockInputFormat.cpp and IInputFormat.h; remove one that restated the code below it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex) and my own review; 17 findings over 6 rounds (1 full pass, 5 delta rechecks). Bounce cap reached - published as is; residual findings listed below. The last fix round (row 13) was not re-gated.
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-10:20261105-134100 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-7:20261105-130700 |
|
cc @scanhex12 @grantholly-clickhouse, could you review this and add the |
|
Workflow [PR], commit [749c67b] Summary: ✅
AI ReviewSummaryPR #129495 overlaps decoding the current Parquet file with bounded reads of the next file's offset index and first row group, sharing the stream's column-data memory allowance. Reviewed head Missing context / blind spots
Final VerdictStatus: ✅ Approve |
With `use_columns_cache = 1`, `Reader::init` (`initColumnsCache`) looks up and holds the cached columns of every selected row group of the file. Run for the next file in the background, it held them while the source was still on the current file, uncharged to the stream's memory share, and the cache parks such columns on eviction instead of freeing them; a query that stops early (LIMIT) also looked up a file it never read. Measured on six warm one-row-group files, `LIMIT` over three of them: `ColumnsCacheHits` 4 before, 3 after. Keep the footer-only prefetch whenever the columns cache is used. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes56 object files changed (+57.16 KiB total), 0 added.
716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units167 translation units recompiled, 1069 s compile time in total, 167 of them have a recent master baseline. |
The arm read two 625 KB files and then a small one, and expected the third file to be refused because the second file's chunk filled the shared 300 KB share. But the second file charges that chunk only on its first `read`, on the pipeline thread, while the third file decides in its background reader creation, about 3 ms after the switch here (footer and offset index reads). When the pipeline thread stalls longer, as on loaded CI runners, the third file sees an empty share and is read ahead: `g_shared_share_refused` printed 1 in 9 of 150 flaky-check runs (amd_debug, amd_binary, amd_asan_ubsan), reproduced 3 of 3 with a 20 ms sleep injected after `createReaderAsync`. Now share/ has two small files, then files of 240 KB and 80 KB without page index. The third file is read ahead within its reader creation, so its chunk is charged before the source switches to it, and the fourth file is refused for as long as the third holds that chunk, which slower rows stretch to about one second. The arm prints the number of files read ahead: 1 for g, 2 for h (default share). Passes with a 200 ms stall after the switch and with an 800 ms delay of the fourth file's creation; without the shared counter in `getColumnDataMemoryUsage` it prints 2. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e near it With a column chunk shorter than `min_bytes_for_seek`, `readAheadFirstRowGroup` read only a file's last row group ahead, and only without PREWHERE, because admission reads such a chunk together with the likely-to-be-used ranges near it: the next row groups' chunks, a later PREWHERE step's columns. So a file of several row groups was never read ahead once a queried chunk was short, e.g. the ClickBench `hits_*.parquet` objects (2 to 5 row groups, no page index). Refuse instead only when such a range is actually there: `Prefetcher::hasIncidentalRangesNear` looks in range set 0 for a range that may be read incidentally, has no read task yet, lies outside the round's chunks and less than `min_bytes_for_seek` from one of them. A read task grows only across gaps below `min_bytes_for_seek`, so without one it reads nothing outside the round. Reading the round ahead with such ranges near it would also split the single read admission makes, e.g. for files of small adjacent row groups. New arm `x_far_row_groups`: files of three row groups whose 40 KB chunks are kept apart by an unread column are read ahead (0 before). Without the new check, `t_row_groups_refused` and `v_prewhere_refused` print 1 and the `t` bytes oracle fails. Measured by @ snelheidai on the ClickBench objects (4 vCPU arm64, 20 ms per request): 100 objects, 4 threads, 1890 ms -> 1456 ms, with the same GETs and bytes. Proposed in ClickHouse#129392 (comment). Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI finish ledger - fb710e1Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Not a failure: the Session id: cron:our-pr-ci-monitor:20261106-000007 |
|
Fixing PR for the Iceberg rewrite error of prewhere_subcolumn_read_steps in Performance Comparison (iceberg_local/iceberg_s3, 1/3): #129658 |
…xt-file-read-ahead Master switched `src/Processors/Formats` to `-WithMemoryTracking` containers and added the `other_row_groups_are_read_by_other_readers` argument of `ReadManager::init`; the containers of the read-ahead of the next file follow it.
Since ClickHouse#132712 randomizes the Parquet reader and writer settings, 05507 failed the flaky check in the merge queue (78 of 186 runs on 2026-11-19). Each failing arm traced to one setting that changes a fixture's layout or what the reader reads, forced alone or in a pair on master + this PR: - input_format_parquet_use_offset_index = 0 reads no offset index, so the oracle of m/n counts no separate offset index request (2 -> 0). - output_format_parquet_data_page_size below a chunk splits it into pages; with reader blocks of 1000 or 8192 rows the first block's pages fit the read task and are read ahead, so the hand-back of s is not reached (0 -> 1). That read is within the bound: 970102 bytes with prefetch vs 971782 without. - output_format_parquet_row_group_size_bytes = 65536 with max_block_size below 25000 rows cuts good/ into several row groups with short chunks, which are not read ahead (a, l and the page cache control 1 -> 0). - compression_method = 'none' with no dictionary makes the small/ files larger than remote_read_min_bytes_for_seek, so they are read ahead (j 0 -> 1). The limits line clamps these five to their defaults. 160 of 160 randomized runs of the test pass with it, 28 of 50 with the line disabled. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`hasIncidentalRangesNear` compared every registered range of the file with every chunk of the round, so for a file whose columns are all short it took O(columns^2) per next file. It now sorts the round's chunks once, keeps the running maximum of their ends (chunks of a malformed file may overlap), and finds the neighbours of each range by binary search: O(ranges * log(chunks)). The result is the same as before: 0 differences in 3,000,000 random cases (overlapping and empty ranges, seek threshold 0) and in 624 calls made by 05507 on master + this PR, where both answers occur. The check also runs only after the cheaper size bound, which refuses the round the same way. Measured on a debug build, `SELECT *` over 12 files of 4000 UInt8 columns (1000 rows, one row group each), one stream, 10 files read ahead: median 589 / 623 ms before, 524 / 526 ms after; user CPU 903 / 948 ms before, 657 / 661 ms after (two alternating rounds of 7 runs). With 2000 columns one call took 5.2 ms before and 0.10 ms after. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
The merge queue removed this PR because 6eef247 also makes |
When reading the next file's first row group ahead fails after its pages were handed out (`addColumnDataPrefetches`) but before their read tasks exist, the exception is stored and that row group keeps its schedulable bit. The first `read` checked the exception only after clearing `decoding_deferred`, so an offset-index batch of another row group finishing in between could schedule the row group's `ColumnData` tasks, whose decoding then reached `Prefetcher::getRangeData` for a page without a read task. `read` now checks the exception under `column_data_prefetch_mutex`, under which `deferTasks` stores it, and clears the flag only when there is none, so the reader of a failed read-ahead never schedules anything and the exception is rethrown. Co-Authored-By: Claude Opus 5.5 <[email protected]>
05507 failed the flaky checks twice on 6eef247 (amd_tsan and amd_asan_ubsan, 1 of 50 each): the clickhouse-local control read without the page cache did not read the next file ahead. It read good/{0,1,2}, where only the third file is a candidate, and those files have a page index, so the decision waits for the offset index read in the background and is skipped when the source switches to the file first, as happens on a loaded sanitizer runner. The block now reads share/, whose files have no page index: their first row group is read ahead while the reader is created, which the source waits for before it switches, so the control does not depend on timing. The page cache case still prints 0 for those files. Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI finish ledger - 085e7f1Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20261120-110103 |
AI review of a later commit does not approve the PR, see the AI Review comment.
When `readAheadFirstRowGroup` throws in a pool thread (reached from an offset index batch through `flushMemoryUsageDiff`), `deferTasks` stores the exception and rethrew the same object. `runBatchOfTasks` then appends the read stage and the column to it with `addMessage` outside `delivery_mutex`, while `read`, once the source has switched to this file, copies the stored exception under `delivery_mutex`. That is a data race on the exception's message. Keep the stored exception immutable and rethrow a copy, as `FormatFilterInfo::initKeyConditionOnce` does. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
📊 Cloud Performance Report ✅ AI verdict: No query shows a performance change caused by this PR. The PR adds read-ahead of the next file's offset index and first row group for Parquet files in object storage, and only does this once a source is past its second file. The single flagged query is clickbench Q10, a COUNT(DISTINCT) GROUP BY aggregation that was 20.9% faster (87 ms vs 110 ms). That result was suppressed because 87 ms is inside master's normal range of 83-115 ms, and this query's history is noisy. The suppression is consistent with the code: this query doesn't run the changed Parquet path, so there is no basis to contest it. clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
…xt-file-read-ahead Resolves the conflict in `Reader::initializePrefetches` with ClickHouse#129430: an offset index kept with the cached footer is used as is, and the one still read is registered as a page index. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #129392
Related: #128607
Related: #128374
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Scans of many Parquet files in object storage (
s3, Iceberg, Delta Lake and other data lakes) are faster: while a stream decodes a file, the reader of its next file reads the offset index and starts reading the first row group's column data, within the size of one read task (4 x remote_read_min_bytes_for_seek) and the stream's memory share. A scan of a 100M-row Iceberg table of 101 files takes 34% less time with 20 ms of latency to S3 and 24% less without added latency.Description
A ClickHouse-written Iceberg data file is one row group (#128471); on switching to it, a stream waited for its offset index, then its column data. #128607 reads only the next file's footer ahead.
From a source's third file on,
createReaderAsyncalso has the next file's format start reading data.ReadManager::initruns with decoding deferred: the offset index is read without the column data next to it, and the first row group's first admission round starts only if its whole chunks fit in one read task and in the stream's free ColumnData share, checked before any page is handed out. A chunk shorter thanremote_read_min_bytes_for_seekis read with its neighbours: then the gaps count, and no other range to read may lie near it. Both readers of a source charge one counter (InputFormatStreamMemory), answering the per-stream budget concern that narrowed #128607. Off for local object storage, the page cache, the columns cache, TopN dynamic filtering,max_parsing_threads = 1,remote_filesystem_read_prefetch = 0and files smaller thanremote_read_min_bytes_for_seek. A failed background initialization is reported, naming the file, when the source reaches it.Release, MinIO at 20 ms RTT, 4 threads, 6 paired rounds, fix/master [95% CI]: Iceberg scan 0.661 [0.648, 0.676],
s3()glob 0.659, 16 threads 0.90-0.96,WHERE b = 50.992, 1000 small files 1.004; same GETs and bytes (one of 101 files: +1 GET, -2 MB). No added latency: 0.762. Peak query memory +12-14 MiB at 4 threads, up to +40 MiB at 16; server max RSS +140 / +440 MiB (no latency), decaying when idle.LIMIT 12000000: 0.86, 8 more GETs.05507_parquet_object_storage_next_file_read_aheadcovers the read-ahead, both bounds, the shared share, the offset index read alone, every exclusion above, theaggregate_function_input_formatwrapper and the error messages; the shared Iceberg read perf suite gains a 4-stream scan. Analysis and original patch: @ snelheidai in #129392.Workflow [PR]
Sync PR [sync-upstream/pr/129495]