Skip to content

feat: account for native Parquet scan pages and decoder memory - #7840

Open
ningsiyuan1024 wants to merge 7 commits into
apache:mainfrom
ningsiyuan1024:feat/parquet-page-memory
Open

ningsiyuan1024 wants to merge 7 commits into
apache:mainfrom
ningsiyuan1024:feat/parquet-page-memory

Conversation

@ningsiyuan1024

@ningsiyuan1024 ningsiyuan1024 commented Dec 3, 2026 •

Copy link
Copy Markdown

Which issue does this PR close?

Closes #7688.

Rationale for this change

A one-byte slice can retain a 524,416-byte merged object-store response. The decoder also holds uncompressed pages and an output batch. Without a scan reservation other consumers see more free memory than the task has.

What changes are included in this PR?

Register a scan consumer on the reader factory. Hold compressed response reservations through the lifetime of their bytes. Estimate decode memory at the first data fetch using the overlapping row groups, fetched leaves, uncompressed chunk sizes, repetition/definition levels and batch size. Retain the reader's largest granted estimate until close. Metadata-only and fully pruned reads do not take a decode reservation. Scan memory accounting is disabled by default. Set spark.comet.parquet.pageMemory.enabled=true to enable page and decoder reservations. The metrics and memory guide describe refusals and what remains uncharged.

How are these changes tested?

On the original PR head, the three affected Rust test groups passed all 48 tests. Coverage includes a one-row, 500-column file, a refused reservation followed by recovery, an interrupted request, and the last slice of a merged response. With real Parquet dictionary pages and nested lists, exported Arrow C arrays remained alive after the scan pool returned to zero. Reimport verified nulls, empty lists and values. Cargo formatting also passed.

For the Spark comparison, write one Snappy Parquet partition from range(600000) with id and sha2(cast(id as string), 256) as payload, using 1 MiB row groups. On Spark 4.1.1 with a locally compiled JVM bridge and native library, run sortWithinPartitions("payload", "id") with fair_unified, 32 MiB off-heap and a Comet batch size of 2048. Alternate the switch and hash every output row in order. Both arms matched the Spark-only result in all ten measured runs.

Per run Off (5 runs) On (5 runs)
Native sort spills 5 6
Bytes spilled 43,484,960 43,462,344
Data bytes read 42,071,849 42,071,849
Decode reservation bytes granted, cumulative 0 1,274,327
Median wall time including digest, seconds 14.64 14.40

The enabled scan caused one additional spill per run. The timing ranges overlap, and these are debug builds. This single-partition sort does not establish the spill impact on SF100 TPC-H q9/q10. Fork Spark SQL CI is tracked here.

The review revision keeps a separate reservation for each response. With the Parquet 59.3.2 push decoder, a filter read two ranges in one request. After filtering, the output reader still held the first range while the second had already released 961,136 bytes. A shared reservation would keep charging the task for both. The Spark UI metric now describes bytes read without a reservation.

On Spark 4.1.1 with 128 MiB off-heap, the default, explicitly enabled and explicitly disabled settings all returned the same 1,000 rows as Spark. Cumulative decoder reservations were 0 B, 32,155 B and 0 B respectively. In the debug-build TPC-DS SF10 runs, Q3 and Q19 each had two measured pairs with zero spills and zero failed task attempts. Q29 had only a warmup pair. The release-build cost of JNI calls on a wide projection and the SF100 Q9/Q10 spill impact remain unmeasured.

We defined the requirements and acceptance criteria. OpenAI Codex was responsible for execution, including implementing the changes and running the tests.

Codex and others added 4 commits December 3, 2026 12:13
Opt in to memory-pool accounting before coalesced cloud responses are sliced. Share one consumer across the scan and propagate non-capacity pool errors. Track skipped bytes when accounting backs off after a refusal.

@comphead comphead 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.

Charging each storage response through a Bytes owner, so a surviving slice keeps the whole coalesced response reserved, is a sound way to follow real page lifetimes. DataFusion 55.2's Parquet opener reserves nothing for reads, and ParquetPushDecoder::buffered_bytes() in parquet 59.3.2 sits inside DataFusion's push_decoder.rs, out of Comet's reach, so hooking in at the reader level makes sense. What is missing is evidence for turning this on by default for every native scan, and a measurement of the per-response JNI round trips it adds. New public config spark.comet.parquet.pageMemory.enabled (CATEGORY_PARQUET, default true, not internal) and new metrics scan_decode_reserved_bytes_total, scan_decode_unreserved_bytes_total and scan_io_unreserved_page_bytes_total. The only failing check is the advisory CI (fork) run, and I could not tie it to the diff.

Comment on lines +255 to +263
val COMET_PARQUET_PAGE_MEMORY_ENABLED: ConfigEntry[Boolean] =
conf("spark.comet.parquet.pageMemory.enabled")
.category(CATEGORY_PARQUET)
.doc(
"Reserve task memory for compressed Parquet pages and estimate the working memory for " +
"decoding fetched columns and building one batch. Cloud reads include fetched gaps. " +
"A refused reservation allows the scan to continue.")
.booleanConf
.createWithDefault(true)

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.

This is on by default and adds unspillable bytes to every task that runs a native Parquet scan. Under fair_unified that lowers the limit of every consumer that can spill ((task_share - unspillable) / num_spill in fair_pool.rs) and the room left in pool_size for consumers that cannot. A sort's first request or a hash join build that fit before can now be refused, and ExternalSorter fails on a refused first request. Each page try_grow can also make Spark spill JVM consumers in the same task, such as a JVM shuffle writer fed by the scan. The only evidence is one single-partition sort on a debug build, and it already shows an extra spill per run. Can you post the SF100 TPC-H q9/q10 run the description mentions, or TPC-DS, with the flag on and off at a realistic off-heap size, with spill counts and any new task failures? Until that exists I would default this to false. I did not run this.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for pointing out the impact on spillable consumers, @comphead. In 80002c5 the feature defaults to false. On TPC-DS SF10 with 2 GiB off-heap, Q3 and Q19 each had two measured off/on pairs with zero spills or failed task attempts in either setting. Those queries did not put the pool under spill pressure. In the 32 MiB sort, every one of five measured pairs went from five spills to six when enabled. These are debug-build results; I have not run SF100 Q9/Q10.

Comment on lines +265 to +266
let reservation = self.reservation.new_empty();
match reservation.try_grow(bytes.len()) {

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.

Under fair_unified each try_grow here takes the pool lock, runs block_in_place and calls acquireMemory over JNI (ask_spark in spark_memory.rs). Dropping the Bytes then calls releaseMemory. For local and HDFS stores get_ranges returns one response per requested range (LocalFileSystem::get_ranges in object_store 0.13.2 reads each range separately), so a wide projection or a page-index-pruned read pays two JNI calls per column chunk or page range in each row group. Could this reserve once per get_byte_ranges call, for the sum of the response lengths, and share one Arc'd reservation owner across the responses? As far as I can tell from DataFusion 55.2's push_decoder.rs, those bytes are freed together when the row group's reader drops anyway. Either way, a release-build number for a wide file (hundreds of columns, several row groups) with the flag on and off would show the cost. I did not measure it.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I checked the lifetime assumption with the Parquet 59.3.2 push decoder. A filter read two ranges in one request, then projected only the first column. Once the output reader was returned, it still held the first buffer, but the second buffer (961,136 bytes) had already been released. A single shared reservation would keep charging for that buffer, so I kept the per-response owners. This tests lifetime only. The release-build JNI cost on a wide projection is still unmeasured.

Comment on lines +475 to +476
"scan_io_unreserved_page_bytes_total" ->
SQLMetrics.createSizeMetric(sc, "Cumulative page bytes skipped after memory refusal"),

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.

The bytes are not skipped. hold returns them unchanged and the scan decodes them, just without a reservation. Please match metrics.md, for example "Parquet page bytes read without a memory reservation".

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Updated the Spark UI label in 80002c5 to “Parquet page bytes read without a memory reservation” and clarified the retry interval in the metrics guide. On Spark 4.1.1 I checked the runtime label and compared all 1,000 output rows with the reference scan.

Comment on lines +618 to +627
With `spark.comet.parquet.pageMemory.enabled`, the Parquet scan also reserves compressed
page buffers. For cloud stores it charges the merged range before splitting it into pages.
For local and custom stores it charges each range the store returns. A separate reservation
estimates decoder memory on the first data fetch. For each fetched leaf in the current row
group it includes up to 1 MiB of uncompressed pages and one batch of values, scaled to the
smaller of the configured batch size and the group's row count. Nested estimates use the leaf
value count and repetition and definition levels. Later fetches of other leaves, including
predicate columns, can raise this estimate. The reader retains its highest granted estimate
until close. The estimate excludes footer, page index, and Bloom filter allocations.
Set `spark.comet.parquet.pageMemory.enabled=false` to disable both reservations.

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.

With this on by default, the example at line 383 of this page no longer holds. It describes a task that holds a native pool but no memory, "such as a native scan that feeds a JVM shuffle". That scan now reserves page and decode bytes, so Spark counts the task too. Please update or replace that example in this PR.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Replaced the scan-to-shuffle example in 80002c5 with the interval after a task opens its Comet pool and before its first Spark execution-memory acquisition. The guide also documents the opt-in. In the same local scan, cumulative decoder reservations were 0 B with the setting unset, 32,155 B with it enabled, and 0 B with it explicitly disabled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Charge native Parquet scan memory to the task memory pool

2 participants