You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "alamb (via GitHub)" <gi...@apache.org> on 2026/04/13 18:54:58 UTC

[I] Support iterator-based Morsel output for Parquet without blocking [datafusion]

alamb opened a new issue, #21598:
URL: https://github.com/apache/datafusion/issues/21598

   ## Is your feature request related to a problem or challenge?
   
   Follow-on from [#20529](https://github.com/apache/datafusion/issues/20529) and the morsel API work in [#21342](https://github.com/apache/datafusion/pull/21342).
   
   In [this review comment](https://github.com/apache/datafusion/pull/21342#discussion_r3070184104), Adrian raised a good question about whether [`Morsel`](https://github.com/apache/datafusion/blob/04dbbbf6694a4b162f76aee0091fdc3a47d2f9f0/datafusion/datasource/src/morsel/mod.rs#L52) should return an iterator rather than a stream. We tried that change locally and found that the API is straightforward for mock and fully CPU-ready morsels, and it fits well with the current [`ScanState`](https://github.com/apache/datafusion/blob/04dbbbf6694a4b162f76aee0091fdc3a47d2f9f0/datafusion/datasource/src/file_stream/scan_state.rs#L65) / `FileStream` scheduler. However, Parquet is structurally harder because [`ParquetPushDecoder`](https://docs.rs/parquet/latest/parquet/arrow/push_decoder/struct.ParquetPushDecoder.html) can discover `NeedsData` only after some batches have already been produced from the current decode state, which makes a synchronous morsel boundary difficult to express cleanly in [
 `ParquetOpenState`](https://github.com/apache/datafusion/blob/04dbbbf6694a4b162f76aee0091fdc3a47d2f9f0/datafusion/datasource-parquet/src/opener.rs#L202), [`PushDecoderState`](https://github.com/apache/datafusion/blob/04dbbbf6694a4b162f76aee0091fdc3a47d2f9f0/datafusion/datasource-parquet/src/opener.rs#L411), and [`ParquetStreamMorsel`](https://github.com/apache/datafusion/blob/04dbbbf6694a4b162f76aee0091fdc3a47d2f9f0/datafusion/datasource-parquet/src/opener.rs#L507).
   
   Three-sentence summary of what we found in testing: switching `Morsel` from `Stream` to `Iterator` worked cleanly for `FileStream` and the mock morsel infrastructure. For Parquet, a simple iterator wrapper required blocking because the decoder can hit `NeedsData` mid-iteration, after already yielding some batches. A handoff-based design removed the blocking bridge in principle, but the current `ParquetPushDecoder` / reader ownership model made it easy to introduce hangs in Parquet reverse-scan tests.
   
   ## Describe the solution you'd like
   
   Add Parquet-side support that makes an iterator-based morsel contract practical without blocking.
   
   Two likely enablers are:
   - an API on [`ParquetPushDecoder`](https://docs.rs/parquet/latest/parquet/arrow/push_decoder/struct.ParquetPushDecoder.html) that can tell the caller how much already-buffered output remains, or otherwise expose a clearer “this many rows/batches are ready before the next `NeedsData`” boundary
   - an efficient way to hand off or clone the [`AsyncFileReader`](https://docs.rs/parquet/latest/parquet/arrow/async_reader/trait.AsyncFileReader.html) state needed to start the next async read without coupling the active morsel iterator and the pending planner so tightly
   
   More concretely, it would be useful if Parquet exposed one or both of:
   - a way to ask the decoder whether more output can be produced synchronously before the next I/O boundary
   - a cheap way to split or clone the reader / read context so the next planner I/O can be prepared independently of the currently active morsel iterator
   
   That would let DataFusion produce a [`Morsel`](https://github.com/apache/datafusion/blob/04dbbbf6694a4b162f76aee0091fdc3a47d2f9f0/datafusion/datasource/src/morsel/mod.rs#L52) that is honestly CPU-only, while keeping future I/O in [`PendingMorselPlanner`](https://github.com/apache/datafusion/blob/04dbbbf6694a4b162f76aee0091fdc3a47d2f9f0/datafusion/datasource/src/morsel/mod.rs#L188) rather than hiding async work behind a blocking iterator bridge.
   
   ## Describe alternatives you've considered
   
   - Keep [`Morsel`](https://github.com/apache/datafusion/blob/04dbbbf6694a4b162f76aee0091fdc3a47d2f9f0/datafusion/datasource/src/morsel/mod.rs#L52) as a stream for now. This is the cleanest fit for current Parquet behavior, but it gives up the simpler synchronous morsel contract.
   - Use `futures::executor::block_on` or `block_on_stream` inside Parquet morsels. This works mechanically but blocks executor threads and is not a good long-term design.
   - Use a channel handoff where the morsel iterator returns batches until `NeedsData`, then sends decoder state to a waiting planner future. This is conceptually closer to the desired model, but our prototype still had lifecycle/handoff complexity and hung in some Parquet tests.
   
   ## Additional context
   
   Relevant links:
   - [#20529](https://github.com/apache/datafusion/issues/20529)
   - [#21342](https://github.com/apache/datafusion/pull/21342)
   - Adrian’s review question: [#21342 comment](https://github.com/apache/datafusion/pull/21342#discussion_r3070184104)
   - [`ParquetPushDecoder` source in arrow-rs](https://github.com/apache/arrow-rs/blob/master/parquet/src/arrow/push_decoder/mod.rs)
   
   If helpful, this issue could explicitly track the Parquet-specific API work needed before revisiting an iterator-based `Morsel` contract in DataFusion.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] Support Morsel output for Parquet known to be non blocking [datafusion]

Posted by "zhuqi-lucas (via GitHub)" <gi...@apache.org>.
zhuqi-lucas commented on issue #21598:
URL: https://github.com/apache/datafusion/issues/21598#issuecomment-4295247616

   Thanks @alamb! The `try_next_reader` / `peek_next_row_group` / `skip_next_row_group` APIs in arrow-rs are exactly what we need for dynamic RG pruning.
   
   In #21580 we've built the statistics-driven TopK optimization chain (file reorder + RG reorder + stats init + cumulative prune) which gives 17-60x improvement on sorted data by skipping RGs at planning time. But for the WHERE case and overlapping RGs, we need runtime dynamic pruning — checking the TopK threshold against the next RG's statistics between reads.
   
   The morsel split via `try_next_reader` would let us do this naturally: when a morsel planner calls `try_next_reader`, it can check `peek_next_row_group()` statistics against the current `DynamicFilterPhysicalExpr` threshold and `skip_next_row_group()` if prunable.
   
   This is tracked in #21399. Happy to help implement the dynamic pruning logic on top of the morsel split once the API is wired up.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] Support Morsel output for Parquet known to be non blocking [datafusion]

Posted by "Dandandan (via GitHub)" <gi...@apache.org>.
Dandandan commented on issue #21598:
URL: https://github.com/apache/datafusion/issues/21598#issuecomment-4295379544

   Cool @alamb . 
   
   I've a PoC / bench to show the impact of going from file -> row group morsel using the similar approach earlier of using 2 queues (files / morsels).
   
   https://github.com/apache/datafusion/pull/21766
   
   * On partitioned clickbench it looks slightly better overall, but current approach seems to create small bit of overhead (not sure yet if it is due to doing things multiple times or...) on some queries
   * On clickbench_1 (single file) it yields big gains!
   * On TPC-DS it is the same still as everything is a single row group.
   
   Using `try_next_reader` looks great, I think it might simplify the implementation a bit 
   
   I didn't try to delay morsel splitting to the query tail, just split always into row groups.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] Support Morsel output for Parquet known to be non blocking [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on issue #21598:
URL: https://github.com/apache/datafusion/issues/21598#issuecomment-4260640449

   Actually, I was poking around the API in the context of https://github.com/apache/arrow-rs/pull/9697 and I actually think we have the API we needed for this:
   
   https://docs.rs/parquet/58.0.0/parquet/arrow/push_decoder/struct.ParquetPushDecoder.html#method.try_next_reader
   
   So the idea is that a morsel planner could call try_next_reader and retrn that as the morsel 🤔 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] Support Morsel output for Parquet known to be non blocking [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on issue #21598:
URL: https://github.com/apache/datafusion/issues/21598#issuecomment-4281403375

   FYI I started messing around with this locally -- hopefully I will have something to share soon


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org