Skip to content

perf(iceberg): a Parquet page header is read from the window already fetched - #2365

Merged
aaj3f merged 7 commits into
mainfrom
perf/iceberg-page-header-window
Nov 17, 2026
Merged

aaj3f merged 7 commits into
mainfrom
perf/iceberg-page-header-window

Conversation

@aaj3f

@aaj3f aaj3f commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Partially addresses #2363: this fixes the first of its three items, the 8 MiB fetch per Parquet page header. #2366 takes the third (file reads that outlive their scan), and the second (a LIMIT bounding how much of each file a scan reads) stays open on the issue.

RangeBackedChunkReader reads every Iceberg data file over 64 MiB, and parquet's page reader calls its get_read(start) once per page, just to read the page header (the body then comes through get_bytes). get_read fetched min(8 MiB, file_size - start) every time and only took a cached window when that window held all 8 MiB past start. So once the reader was a couple hundred KiB into a window, every page header fetched a fresh 8 MiB, and a column chunk of 1 MiB pages moved about 8 MiB per page from storage. Claude found this in the synthetic repro described on the issue; I've traced it against SerializedPageReader::get_next_page in parquet 58.

What changes

All of it is in fluree-db-iceberg/src/io/chunk_reader.rs.

  • get_read is now served from the newest cached window that covers start and still holds at least 1 MiB past it (GET_READ_MIN_TAIL), or that runs to the end of the file. Only when none does is a fresh 8 MiB window (GET_READ_WINDOW) fetched. The 1 MiB isn't for the header itself (a few bytes, a few KiB with statistics); it's room for the page bodies that follow, so we don't serve a header from a window we'd miss on right afterwards. A page-by-page read now costs about one fetch per 7 MiB of column.
  • The reader get_read returns (RangeRead, replacing Cursor<Bytes>) reads up to 8 MiB from its offset, the same reach the old cursor had, and fetches whatever part of that a served window lacks only if it's read that far. With a 1 MiB minimum tail I didn't want a page header longer than a window's remainder to turn into a decode error where it used to be fine. That continuation is one uncached range, and no valid file should reach it.
    • The first version of this branch let the reader run on to the end of the file. A Claude review pass caught that a corrupt page header declaring a long field would then have parquet copy that length through the reader, pulling 8 MiB windows to the end of the file before the header was refused; on main it hit the end of the 8 MiB cursor and errored. 1d3b1381c puts the 8 MiB bound back.
  • An offset past the end of the file is now an EOF error, as parquet's own Bytes reader gives, rather than a fetch of an inverted range (or, on main, a wrapped subtraction).
  • fetch_range pulls the sync-to-async storage read out of fetch_bytes, so both paths share it.

Query results don't change, only how many bytes the reader asks storage for. Memory per open file doesn't change either: the cache still holds up to 64 windows of up to 8 MiB plus alignment (about 528 MiB), but those windows are now mostly distinct and fill about 8x more slowly, where on main a page-by-page read filled it with overlapping copies.

A page longer than what its header's window has left (Iceberg's default page size is 1 MiB, and real pages vary) still costs one extra GET at that window's edge: the body is fetched on its own, and the next header then starts a fresh window that re-reads the overlap. It's pinned in a test below. A 2 MiB GET_READ_MIN_TAIL would make that rarer at the cost of starting fresh windows sooner, and I don't think either trade is clearly better without real page-size data, so I've left it at 1 MiB.

Measured

Same box, same synthetic tables, main (467f0da4d) against this branch at a6fc12339 (1d3b1381c only changes what a reader does past a served window or past the end of the file, which no valid file reaches, so I didn't re-run the box for it): LocalStack as S3 with 20 ms injected on its egress, the server (dev-fast, default features) pinned to 16 vCPUs on a c7i.8xlarge, a fresh server and cache per build. Each cell is cold / warm (the same query run twice).

query build wall S3 GETs MB from S3 server CPU-s
2-column LIMIT 5, 8 files of ~136 MB (int64 keys) main 21.2 / 20.8 s 1,310 / 1,295 10,437 / 10,478 16.9 / 15.0
this PR 1.7 / 1.5 s 118 / 106 597 / 648 2.7 / 2.3
2-column LIMIT 5, 8 files of ~166 MB (string keys, 45 columns) main 20.9 / 19.3 s 1,374 / 1,356 10,931 / 10,913 17.5 / 16.1
this PR 2.7 / 1.7 s 115 / 97 530 / 530 4.8 / 4.4
full-table SUM over one int32 column of the first table main 73.8 / 69.6 s 1,359 / 1,359 11,021 / 11,021 349 / 342
this PR 46.0 / 45.1 s 130 / 130 861 / 861 311 / 312
LIMIT 5 over 576 files of ~12 MB (under 64 MiB, so not this reader) main 0.70 / 0.22 s 26 / 1 142 / 0 0.2 / 0.1
this PR 0.57 / 0.10 s 26 / 1 43 / 0 0.0 / 0.1

main run again at the end as a drift check: 21.7 / 21.9 s on the first query, and the SUM at 63.2 / 61.1 s with 259 / 260 CPU-s. So the SUM's CPU swings quite a bit run to run (it's aggregation-bound once the bytes are in), and I'd read its wall-time win as roughly 15-30 s rather than the exact figure; its bytes and GETs are exact. The 576-file row moves fewer bytes inside the query window only because more of its abandoned reads land after the response (#2363's third item, not this PR).

Results: the SUM and a COUNT control are identical on every build. The unordered LIMIT 5 answers vary from run to run on main as well (which file finishes first), so for those I checked that every row returned, in every run on every build, is a row of its table.

Tests

The new unit tests count the ranges a mock store is asked for:

  • page_by_page_read_fetches_each_window_once: 16 MiB of 64 KiB pages, read header-then-body, take 3 fetches (64 fetches and about 520 MiB before).
  • a_page_longer_than_its_windows_remainder_costs_one_extra_fetch: ten 1.75 MiB pages, two of whose bodies run past their window's edge, take exactly four fetches: a window, that body, a fresh window, the next such body (ten 8 MiB fetches before).
  • get_read_near_a_window_end_fetches_a_fresh_window: a window with exactly 1 MiB left serves; 1 byte further, a fresh window is fetched from the offset.
  • get_read_serves_a_window_that_runs_to_eof: a window that reaches the end of the file serves any offset in it, including the end itself.
  • get_read_reads_up_to_its_reach_and_no_further: a get_read reader returns exactly 8 MiB from its offset whether it was served (with one fetch for what its window lacked) or fresh.
  • get_read_past_the_end_of_the_file_is_an_eof_error: an EOF error, and no fetch.
  • small_page_parquet_decodes_with_about_one_pass_over_the_file: an Arrow decode of a 19 MB file of 64 KiB pages, in one row group, through the range-backed reader returns the same rows as decoding it from memory, in exactly 5 fetches and under 2x the file's bytes (29 MB; 43 fetches and 363 MB before).

With get_read put back the way it was, the fetch-count tests fail; the reach test fails with the reader left uncapped, and the EOF test with the past-the-end check taken out. The end-of-file one passes either way: it pins the end-of-file exception to the 1 MiB rule rather than a regression.

Verification

On a c7i.8xlarge, at a6fc12339:

  • cargo fmt --all -- --check, cargo clippy --all --all-features --all-targets --locked -- -D warnings and cargo check --workspace --all-targets --locked (CI's flags): clean.
  • cargo test -p fluree-db-iceberg --all-features: 510 passed, 0 failed (9 ignored).
  • cargo test -p fluree-db-api --all-features: 99 test binaries, 5,687 passed, 0 failed (137 ignored).

At 1d3b1381c, which only touches chunk_reader.rs: cargo fmt --all -- --check, cargo clippy -p fluree-db-iceberg --all-features --all-targets --locked -- -D warnings, and cargo test -p fluree-db-iceberg --features aws (512 passed, 0 failed), plus CI.

  • The box's Amazon Linux gcc 11 and clang 15 can't build usearch's AVX-512 FP16 kernels, so those runs used gcc 14 as the C/C++ compiler and linker. CI's ubuntu-24.04 doesn't need that, and CI is still the source of truth here.

After #2315 landed, the branch merges main (d56ed5467) rather than being rebased. Nothing conflicted in git, but #2315 gave decode_batches_arrow a row_group_subset argument, so small_page_parquet_decodes_with_about_one_pass_over_the_file now passes None (the whole file) for it in both of its decodes; without that the test module didn't compile on the merge. On a Mac at d56ed5467: cargo fmt --all -- --check and cargo clippy -p fluree-db-iceberg --all-targets --all-features -- -D warnings are clean, and cargo test -p fluree-db-iceberg --all-features passes 533, fails 0 (9 ignored); CI covers the rest.

testsuite-sparql/ isn't touched.

One thing worth flagging for embedders: RangeBackedChunkReader is public, so its ChunkReader::T changing from Cursor<Bytes> to RangeRead<S> is visible. I'd be surprised if anything names that associated type, but it's there.

After review

Brian's notes are in 612e1f698 (the docs say what the window does rather than what it replaced), b8f8bef08 (Debug for RangeRead, and expect_err in the EOF test now that it can be) and 3f7ed50e7 (small_page_parquet_decodes_with_about_one_pass_over_the_file writes one row group, and asserts it, rather than relying on parquet's 1,048,576-row default; still 5 fetches), on top of a merge of main (f5056f289). At 3f7ed50e7, on a Mac: cargo fmt -p fluree-db-iceberg -- --check and cargo clippy -p fluree-db-iceberg --features aws --all-targets -- -D warnings are clean and the 11 chunk_reader tests pass; CI is green.

🤖 Generated with Claude Code

…fetched

Parquet's page reader calls `ChunkReader::get_read` once per page to read
the page header, then reads the body through `get_bytes`.
`RangeBackedChunkReader::get_read` fetched `min(8 MiB, file_size - start)`
on every call and took a cached window only when it held all 8 MiB past
`start`. A page at least a few hundred KiB past the start of the last
window therefore fetched a fresh 8 MiB, so a page-by-page read of a
column chunk moved about 8 MiB per page from storage. On a file of 1 MiB
pages that is about 8x the column's bytes, one GET per page.

`get_read` now serves the newest cached window that covers `start` and
holds at least 1 MiB past it, or runs to the end of the file. Only when
none does is a fresh 8 MiB window fetched. A page-by-page read now costs
about one fetch per 7 MiB of column. The reader `get_read` returns also
runs to the end of the file, as the trait describes, fetching past its
window only if read that far, so a header longer than a window's
remainder is still read whole (the old 8 MiB cursor stopped short).

Tests count the ranges a mock store is asked for: pages served from one
window, a fresh window near a window's end, a window that reaches the end
of the file, a read past the first window, and an Arrow decode of a
19 MB file of 64 KiB pages, which took 43 fetches and 363 MB before and
5 fetches and 29 MB now, with the same rows.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@aaj3f aaj3f added area:iceberg Iceberg catalogs/credentials, R2RML, virtual datasets, materialize performance Performance improvement (release notes: Performance) labels Nov 12, 2026
The reader `get_read` returns ran on to the end of the file. No valid
file needs more than the old cursor's 8 MiB from the offset: a served
window has at least 1 MiB left past it and a fresh one has 8 MiB. A
corrupt page header that declares a long field, though, has parquet copy
that length through the reader, and it would have pulled uncached 8 MiB
windows to the end of the file before the header was refused. The reader
now stops at `start + GET_READ_WINDOW`, as the old cursor did, and still
fetches what a served window lacks of that reach, in one range.

An offset past the end of the file is now an EOF error, as parquet's
`Bytes` reader gives, rather than a fetch of an inverted range or a
slice past the end of the fetched bytes.

Tests: a `get_read` reader, served or fresh, returns exactly 8 MiB from
its offset, the served one with one fetch for what its window lacked; an
offset past the end is an EOF error with no fetch; ten 1.75 MiB pages,
two of whose bodies run past their window's edge, take exactly four
fetches (ten 8 MiB fetches before); and the Arrow end-to-end test now
pins its five fetches.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@aaj3f
aaj3f marked this pull request as ready for review November 12, 2026 19:58
@aaj3f
aaj3f requested a review from bplatz November 12, 2026 19:58
…ndow branch

No textual conflicts. #2315 gave `decode_batches_arrow` a
`row_group_subset` argument, so the ranged-vs-in-memory decode test in
`chunk_reader.rs` passes `None` (the whole file) for it in both calls.

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

@bplatz bplatz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving. The fix checks out against parquet 58's page reader, and the fetch-count tests pin it well. A few small comments inline, none blocking.

One follow-up I couldn't anchor inline: the row-group-parallel path in decode_large_file (send_parquet.rs) builds a separate RangeBackedChunkReader per task, plus one for the footer, each with its own cache. A task's last window reads up to 8 MiB into the next task's row groups, and worst-case memory is tasks × ~528 MiB. Not from this PR (which makes the cache fill much more slowly); a cache shared across the tasks would probably cover both.

Comment on lines +350 to +352
/// about one fetch per window. Requiring a cached window to hold a whole
/// `GET_READ_WINDOW` past the offset instead meant a fresh 8 MiB fetch for
/// nearly every page: 8 MiB moved per 1 MiB page.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this sentence describes the old behaviour. It's already in the PR body and goes stale once this merges, so I'd drop it. Same for the "before" figures in the test docs (L647–648, L847–848, L888–889).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, dropped in 612e1f6. Along with the get_read sentence and the three "before" figures in the test docs, I also took out the other comparisons to the reader this replaces that I found on the same pass (the module doc's and RangeRead's "as the old 8 MiB cursor did", and the same phrase in get_read_reads_up_to_its_reach_and_no_further's doc), since they'd go stale the same way. The history stays in the PR description.

check.bytes_fetched(),
check.fetches().len()
);
// The footer, then a window per ~7 MiB of the two column chunks.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1.2M rows is over parquet's default limit of 1,048,576 rows per row group, so this file has two row groups (four column chunks), not the two chunks this comment describes. The == 5 still holds, but it depends on that default. Suggest setting .set_max_row_group_row_count(Some(..)) in the writer props and updating the comment.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, thanks for catching this. Parquet 58.4's DEFAULT_MAX_ROW_GROUP_ROW_COUNT is 1,048,576, so the file had two row groups and the five-fetch count only held by way of that default. 3f7ed50 sets .set_max_row_group_row_count(Some(ROWS as usize)) and rewords the comment, and the test now also asserts num_row_groups() == 1, so the comment can't drift from the file again (I checked that dropping the setter fails it with left: 2). With one row group it's still five fetches.

/// remainder still reads whole, while a corrupt header that declares a longer
/// field than that runs into the end of the reader, as it did before, instead
/// of pulling the rest of the file through it.
pub struct RangeRead<S: SendIcebergStorage + 'static> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: RangeRead is a new public type without Debug. RangeBackedChunkReader has a hand-written one, so one here would match.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in b8f8bef, matching RangeBackedChunkReader's hand-written one: path, bytes still buffered, next and limit, with finish_non_exhaustive() for the storage and runtime it leaves out. One small knock-on: with Debug in place, clippy's err_expect started flagging the EOF test's .err().expect(..) (it only fires once T: Debug), so that's expect_err now, in the same commit.

Comment on lines +64 to +70
const GET_READ_WINDOW: u64 = 8 * 1024 * 1024;

/// The least a cached window must hold past a `get_read` offset to serve it,
/// unless it runs to the end of the file. A page header needs a few bytes; the
/// rest is room for the page bodies that follow, so a window this close to its
/// end is passed over for a fresh one rather than served and soon missed.
const GET_READ_MIN_TAIL: u64 = 1024 * 1024;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up idea, not for this PR: a window doesn't know where column chunks end. A narrow projection over a wide file still pulls 8 MiB for each projected chunk smaller than that, and a chunk whose last pages fall in a window's last MiB triggers a fresh 8 MiB fetch. Prefetching each row group's projected chunk ranges, as parquet's async reader does, would fix that. The min-tail rule would need another look then, since a chunk's end isn't EOF.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, @bplatz, I think that's the right shape. A window has no idea where a column chunk ends, so a narrow projection over a wide file pays 8 MiB for each small projected chunk, and a chunk whose last pages land in a window's final MiB buys a whole fresh window just for them. Planning each row group's fetches from the footer's projected chunk ranges, the way parquet's async reader does, would fix both.

I'm not planning it in this PR, though. It's a change to the fetch planner rather than to how a page header is read, and it brings its own question about the min-tail rule: once a chunk's end, not EOF, bounds a fetch, that rule is answering a different question and probably wants re-deriving rather than carrying over. It seems worth doing if remote Iceberg scans stay a hot path, and I'd rather it get its own before/after numbers than ride along here.

aaj3f and others added 4 commits November 16, 2026 13:10
…ot what it replaced

The get_read doc, RangeRead's doc, the module doc and the fetch-count
test docs compared against the reader this PR replaced ("the old 8 MiB
cursor", "before" figures). The PR description carries that history;
in the code it goes stale once the change lands.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
RangeRead is public and RangeBackedChunkReader already has a
hand-written Debug; this one matches it, showing the path, the bytes
still buffered, and the reader's next offset and limit. With Debug in
place the EOF test uses expect_err, which clippy's err_expect now asks
for.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Its 1.2M rows ran past parquet's default of 1,048,576 rows per row
group, so the file had two row groups (four column chunks), not the two
column chunks its comment describes; the five-fetch count held, but
only by way of that default. The writer now sets
max_row_group_row_count, and the test asserts the file has one row
group.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@aaj3f
aaj3f merged commit a44e16e into main Nov 17, 2026
19 checks passed
@aaj3f
aaj3f deleted the perf/iceberg-page-header-window branch November 17, 2026 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:iceberg Iceberg catalogs/credentials, R2RML, virtual datasets, materialize performance Performance improvement (release notes: Performance)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants