Skip to content

Read the footers of upcoming Parquet objects in the background when reading a glob from object storage - #124950

Open
groeneai wants to merge 13 commits into
ClickHouse:masterfrom
groeneai:groeneai/object-storage-parquet-footer-prefetch
Open

groeneai wants to merge 13 commits into
ClickHouse:masterfrom
groeneai:groeneai/object-storage-parquet-footer-prefetch

Conversation

@groeneai

@groeneai groeneai commented Oct 9, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #124671

Changelog category (leave one):

  • Performance Improvement

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

A query over a glob of Parquet objects in object storage (for example s3('.../*.parquet')) now reads the footers of the next objects in the background instead of one after another. With 20 ms of latency to the storage, count() over 100 objects takes about a third of the time. The read-ahead follows remote_filesystem_read_prefetch and needs s3_validate_etag_on_read / azure_validate_etag_on_read (on by default).

Description

A source opens its next object only after the previous one, and the Parquet reader reads the footer on its first read(), so each stream waits for one footer round trip per object (25 in a row for 100 objects over 4 streams), even for count() or a statistics-pruned filter. Analysis and original patch: @ snelheidai, #124671.

A wrapper over GlobIterator hands out the same objects in order and reads the next footers on the IO pool: none for each stream's first two objects (LIMIT 1 reads nothing extra), at most clamp(4 * streams, 16, 32) ahead, each kept parsed until its object is opened, and wasted if a larger LIMIT stops the query. Each read makes the reader's own ParquetMetadataCache::getOrSetMetadata call and reaches the reader through the ObjectInfo: handing it over through the cache would access every entry twice, and SLRU promotes an entry on its first hit. A failed read is rethrown by the reader that needs it, else not counted in system.errors.

Key lists, data lakes, archives, *Cluster, non-seekable reads, objects whose query condition cache entry is read and reads without ETag pinning keep the old path. I reused remote_filesystem_read_prefetch as the switch; I can add a dedicated setting instead if you prefer.

Release binaries, MinIO at 20 ms round trip, 100 objects, 4 threads, 9 runs per side (A/A noise 0.97-1.00):

query fix / master
count() WHERE CounterID = -5 (pruned) 0.36
count() 0.35
count() WHERE AdvEngineID <> 0 0.87
sum(length(URL)) 0.64
SELECT * LIMIT 1 1.00

GETs, bytes and cache hits/misses are equal in every row, also for two concurrent cold queries; memory +2 to +21 MiB with these 105-column footers.

Tests: 05340_object_storage_parquet_footer_prefetch and object_storage_parquet_footer_prefetch.xml.

🤖 Generated with Claude Code


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

groeneai and others added 8 commits October 9, 2026 09:55
…b from object storage

A source opens its next object only after the previous one, and the Parquet reader reads an
object's footer on its first `read()`, so a query over many objects waits for one footer request
after another in every stream (100 objects over 4 streams = 25 serial round trips per stream, even
for `count()` or a statistics-pruned filter). The asynchronous creation of the next reader does not
help: it does not read the footer, and it starts only when the previous reader is taken.

`createIteratorWithParquetFooterPrefetch` wraps the `GlobIterator` of a plain glob over a remote
object storage. It hands out the same objects in the same order and reads the footers of the next
few objects on the IO thread pool. Nothing is read ahead while the streams take their first two
objects each (so `LIMIT 1` reads nothing extra); then the depth grows by 4 per object up to
`clamp(4 * num_streams, 16, 32)`, which is also the number of reads in flight per query.

Each read does exactly what the reader of that object would do, only earlier: with the metadata
cache it calls the same `ParquetMetadataCache::getOrSetMetadata` with the same key and load, so the
cross-query single flight, the SLRU admission and the hit/miss events are unchanged (counted in the
query's thread group). The result is attached to the `ObjectInfo` as a `ParquetPrefetchedFooter`
and handed to the reader through `IInputFormat::setPrefetchedFooter`; the reader does no cache
access of its own then. Handing it over through the cache instead would make two accesses per
object, and SLRU promotes an entry on its first hit, so every footer read once by a scan would land
in the protected segment. If the read has not started when the reader needs the footer, the reader
cancels it and reads the footer itself; if it is running, the reader waits for it. An error of the
read is rethrown by the reader that takes it; the read runs under `SuppressErrorCodesScope`, so a
failed read of an object nobody opens does not appear in `system.errors`.

Objects whose footer is not needed are skipped: archives, compressed objects, objects not larger
than the remote read buffer (read whole), objects without a usable ETag, objects already in the
metadata cache, objects with a valid cached row count for a count-only read, and objects with a
query condition cache entry. The probes of those caches are new non-mutating `contains`-style
lookups (`SchemaCache::hasValidNumRows`, `QueryConditionCache::contains`), so they neither reorder
nor invalidate entries nor count lookups. The row-count cache key is computed by one helper
(`getKeyForNumRowsCache`) for the source and the prefetch.

Key lists, data lakes, archives, `*Cluster` reads, local object storage, `_headers` and non-seekable
reads (`input_format_allow_seeks = 0`) keep the old path. The read-ahead is turned off by the
existing `remote_filesystem_read_prefetch`.

Analysis and the original patch: issue ClickHouse#124671.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Pass the structure, so that no query infers the schema: a schema cache entry registered in the
same second as the objects were written is invalid, and the next query reads a footer more.

Do not check that footers were read ahead: the failpoint is global, so a parallel run of the test
can turn the read-ahead off for this one, or on for its rejected run.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
It enables a failpoint, so the style check requires `no-parallel`. Running alone, no other run can
change the failpoint for it, so check again that footers are read ahead.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ds nothing ahead

Drop the failpoint test arm, so the test runs in parallel; check that `LIMIT 1`
schedules no footer read, with the new `ParquetFooterPrefetchRequests`. Also turn
the page cache off in the measured queries.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…replicas are enabled, and read the footer through the shared helper in the bucket splitter.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ads, so the query does not wait forever for a read that never runs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…d iterator is destroyed

The destructor waited for every queued footer read, so when the IO pool did not run them (after a reload
of `max_io_thread_pool_size` to 0, or while a busy pool kept them queued) the query's teardown waited for
them. It now waits only for reads that have started; a queued read checks that the iterator is still alive
before it starts, and references the query's resources weakly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Remove two comments that only restate the function name.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-issue PR origin: picked up an external GitHub issue labels Oct 9, 2026
@groeneai

groeneai commented Oct 9, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 5 rounds, 7 findings) plus my own review. The last round, on bdf29bd, found nothing.

# Sev Finding Verdict Evidence / action
1 ⚠️ The test armed a failpoint only to cover the rejected-scheduling path, which forced no-parallel AGREE, fixed @ c2c25aa failpoint and arm removed, test runs in parallel
2 ⚠️ No test proves that LIMIT 1 schedules no footer read AGREE, fixed @ c2c25aa new ParquetFooterPrefetchRequests; LIMIT 1 arm asserts 0
3 💡 GET equality depends on the object-storage page cache being off AGREE, fixed @ c2c25aa pinned off
4 ⚠️ A partial query condition cache entry suppresses the read-ahead DISAGREE such an object takes the master path; dropping the check would add GETs for objects the source skips, and a precise check needs a non-promoting cache read that ICachePolicy lacks. Disclosed in the description
5 ⚠️ Under the ParallelReplicas CI configuration s3() is read as s3Cluster, so the test sees no read-ahead AGREE, fixed @ 31cc2ac test pins enable_parallel_replicas = 0
6 💡 ParquetBucketSplitter repeats the footer read the new helper wraps AGREE, fixed @ 31cc2ac uses readParquetFileMetaData
7 ❌ With max_io_thread_pool_size = 0 a footer read is queued but never runs, so the query waits forever at teardown AGREE, fixed @ c139095 no read is queued when the IO pool has no threads
8 ❌ A reload of max_io_thread_pool_size to 0 can still strand a queued read, and teardown waits for it AGREE, fixed @ bdf29bd teardown waits only for reads that started; a queued read never starts after it and holds nothing of the query

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

@groeneai

groeneai commented Oct 9, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. Release build, MinIO behind a 20 ms round trip, 100 Parquet objects, max_threads = 4, metadata cache off: master spends 608 ms on count() ... WHERE CounterID = -5, about one footer round trip per object per stream.
b Root cause explained? A stream opens its next object only after the previous one; the next reader is created asynchronously but does not read the footer, and the Parquet reader reads it on its first read(). So each stream waits for one footer round trip per object in a row.
c Fix matches root cause? Yes. The footers of the next objects are read on the IO pool while earlier objects are read, with the same bytes, the same GET requests and the same metadata cache access the reader would make.
d Test intent preserved / new tests added? New 05340_object_storage_parquet_footer_prefetch (results, requests and cache accounting with and without the read-ahead, a query stopped with reads in flight, LIMIT 1 scheduling no read) and the performance test object_storage_parquet_footer_prefetch.xml.
e Both directions demonstrated? 608 -> 218 ms (0.36) and count() 605 -> 213 ms (0.35). The test fails 5/5 when the reader ignores the prefetched footer or never receives it and 5/5 when reads are scheduled from the first object, and passes 100/100 in parallel with randomized settings, and 10/10 in the ParallelReplicas configuration.
f Fix is general across code paths? The only other overrider of the Parquet input-format hooks (AggregateFunctionStatesFromValuesInputFormat) forwards the new one. Key lists, data lakes, archives, *Cluster reads, local object storage and _headers keep the old path; StorageFile globs are a separate path.
g Fix generalizes across inputs (params/datatypes/wrappers)? Checked: a non-Parquet object inside a Parquet glob (same error and message, system.errors unchanged when no reader opens it), compressed and small objects and objects without a strong ETag (not read ahead), metadata cache on and off, count with and without a cached row count, input_format_allow_seeks = 0, 1, 4 and 16 streams, LIMIT, KILL QUERY, max_execution_time.
h Backward compatible? (maintainer-approved exception only) Yes. No setting or format change; remote_filesystem_read_prefetch = 0 gives the old path. Two new profile events (ParquetPrefetchedFooters, ParquetFooterPrefetchRequests).
i Invariants and contracts preserved? One metadata cache access per object per query (two concurrent cold queries: 100 misses and 100 GETs, as on master); the iterator's destructor waits only for reads that have started; no exception leaves a job into the IO pool. TSan: 80 runs of the test and a 36-query stress with KILL, 0 reports.

Session id: cron:clickhouse-impl-slot-4:20261009-134800

@clickhouse-gh

clickhouse-gh Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f5f9f9f]

Summary: ❌

job_name test_name status info comment
Stress test (arm_release) FAIL
Cannot start clickhouse-server FAIL cidb
Check failed FAIL cidb

AI Review

Summary

The PR adds bounded background footer reads for Parquet globs in object storage, controlled by remote_filesystem_read_prefetch and requiring object-generation pinning. No actionable findings remain at f5f9f9f7ec6ddb78f7a5bce71454ac7fe1108a36; the previously reported issues no longer hold in the current code. The performance claim has before/after measurements and focused stateless and performance coverage.

Missing context / blind spots
  • ⚠️ No built ClickHouse binary or build directory is available locally, so runtime tests and benchmarks were not independently rerun. Runtime evidence comes from the PR's recorded validation and existing CI reports.
Final Verdict

Status: ✅ Approve. No new inline comments or thread changes are needed.

@clickhouse-gh clickhouse-gh Bot added the comp-parquet-reader-v3 Issues related to the native Parquet reader. label Oct 9, 2026
…ed generation

The footer read ahead and the later data read are separate requests. Without
`s3_validate_etag_on_read` / `azure_validate_etag_on_read` neither is pinned to
the ETag seen at listing time, so an in-place overwrite between them could pair
a footer of one generation with data of another. Use the same predicate as the
lazy materialization reread, now shared in `isReadPinnedToObjectGeneration`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added comp-object-storage Object storage connectivity (S3/GCS/Azure) including credentials, retries, multipart, etc. and removed comp-parquet-reader-v3 Issues related to the native Parquet reader. labels Oct 9, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

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

This PR reads Parquet footers ahead of time for multi-file glob reads from S3/Azure object storage. All the other changes are refactors or read-only cache lookups that only that new prefetch path uses, and neither ClickBench nor TPC-H runs it, because both read local MergeTree tables. The flagged Q20 improvement (-41.8%, with CPU time roughly halved) cannot come from this diff, so it is downgraded to not_sure; the likely cause is a difference between the PR build and the master baseline. Q28 is borderline, and Q33/Q34 fall within master's normal variation, which fits a diff with no effect on these benchmarks.

clickbench

⚠️ 2 inconclusive

Flagged queries (2 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 20 not_sure 220 128 -41.8% <0.0001 The PR only adds Parquet footer prefetch for glob reads from S3/Azure, and Q20 scans a local MergeTree table. The -41.8% delta and halved CPU must come from a baseline or build difference, not from this diff.
⚠️ 28 not_sure 1058 1000 -5.5% <0.0001 The two tests disagree and run-to-run spread is wide. This query does not reach the object-storage Parquet path the PR changes.

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: 283dd80e-d7c0-4328-ab41-aa18186b43da
  • MIRAI run: 3a8532b8-133a-4243-a0f9-4d8a361d2d89
  • PR check IDs:
    • clickbench_84923_1791938413
    • clickbench_85200_1791938661
    • clickbench_85309_1791938667
    • tpch_adapted_1_official_85455_1791938713
    • tpch_adapted_1_official_85715_1791938760
    • tpch_adapted_1_official_85868_1791938828

…ping its object

When the read-ahead window was full (`Slot::tryAcquire` returned null) or the IO pool
refused the job, the object was still counted as handled, so its footer was never read
ahead and the reader opened it with a synchronous footer request. The window fills in the
normal footer-bound case, because the requests of objects already handed out keep their
slots until their reads finish.

Now the loop stops at the first object whose read cannot be scheduled and tries it again
on the next call. Measured with 100 Parquet objects behind a 20 ms RTT (release builds,
9 runs per arm): `count() WHERE CounterID = -5` schedules 91 of 91 eligible footers
instead of 76-79 with 4 threads, 81 of 81 instead of 64-69 with 16 threads (median
170 -> 147 ms); other queries within A/A noise, S3 GETs and bytes unchanged.

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

With `use_query_condition_cache_for_top_k = 0` a read with TopN dynamic filtering
does not consult the query condition cache, but the read-ahead iterator skipped
every object that had an entry in it, so an earlier plain read of the same table
and predicate turned the read-ahead off for such reads. The iterator now probes the
cache only when the source would, through the source's own predicate, which is
moved to `StorageObjectStorageSource::isTopKFilterAllowed`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rage-parquet-footer-prefetch

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rage-parquet-footer-prefetch

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

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

can be tested Allows running workflows for external contributors comp-object-storage Object storage connectivity (S3/GCS/Azure) including credentials, retries, multipart, etc. groeneai-origin-issue PR origin: picked up an external GitHub issue pr-performance Pull request with some performance improvements

Projects

None yet

2 participants