adriangb opened a new issue, #12562:
URL: https://github.com/apache/arrow-rs/issues/12562

   ## Is your feature request related to a problem or challenge?
   
   DataFusion can return the rows of a Parquet scan in reverse order. It uses 
this for `ORDER BY ... DESC` on files sorted ascending. Today it decodes a 
whole row group, buffers the batches, and emits them in reverse 
([`RowGroupReverser`](https://github.com/apache/datafusion/blob/217984626eeb235deb5a9232b532897474f2e0d9/datafusion/datasource-parquet/src/push_decoder.rs#L755)).
 Peak memory is one decoded row group per partition, which can be hundreds of 
MiB.
   
   The batch-granular push decoder 
([#11238](https://github.com/apache/arrow-rs/pull/11238), epic 
[#11234](https://github.com/apache/arrow-rs/issues/11234)) bounds forward scans 
to about one batch window of pages per column. It cannot help a reversed scan, 
because it moves through windows from the start of the row group to the end.
   
   ## Describe the solution you'd like
   
   Let `ParquetPushDecoder` walk the batch windows of a row group from the last 
window to the first. For each window, it fetches the pages of each column that 
overlap the window, decodes them, and returns the batch. The caller reverses 
the rows of one batch instead of a whole row group.
   
   This needs an offset index. The offset index gives the location and first 
row of every page 
([`PageLocation`](https://github.com/apache/arrow-rs/blob/e4920d88ae760ee68eeb2604eb82e7f09536cd20/parquet/src/file/page_index/offset_index.rs#L22)).
 Data pages decode independently, given the dictionary page of their column 
chunk.
   
   ```
   row group rows:   0 ............................................ N
   column a pages:   | a0      | a1        | a2      | a3            |
   column b pages:   | b0  | b1    | b2  | b3     | b4    | b5        |
   windows (reverse):                               <- w1 | <- w0     |
   w0 needs a3, b5 (+ dictionaries). w1 needs a2, a3, b4, b5.
   ```
   
   Possible shape:
   
   - A builder option, for example a `RequestGranularity` variant or a separate 
`with_window_order(WindowOrder::Reverse)`. It requires the offset index for the 
read columns. Without the offset index, fall back to the whole column chunk.
   - `ScanPlan` lists pages in the reverse window order, so read-ahead follows 
it.
   - The decoder keeps the dictionary page of each column for the whole row 
group.
   
   ## Open questions
   
   - **Page boundaries differ by column.** A page that spans two windows is 
needed by both. A strict back-to-front walk decodes it twice, unless the 
decoder keeps the decoded tail of that page.
   - **Repeated columns.** Pages must start at record boundaries. The format 
requires this when an offset index is present, but older writers may not follow 
it. Should the decoder detect this and fall back?
   - **Row filters.** A `RowFilter` is evaluated one window at a time in batch 
mode. Is reverse window order compatible with the predicate cache?
   - **Limits.** With a limit, a reversed scan reads only the tail of a row 
group. DataFusion approximates this today with a row selection 
([`select_tail_for_limit`](https://github.com/apache/datafusion/blob/217984626eeb235deb5a9232b532897474f2e0d9/datafusion/datasource-parquet/src/access_plan.rs#L869)).
 Reverse window order would make this natural.
   
   ## Describe alternatives you've considered
   
   - Rebuild the decoder with a one-window row selection for each batch. This 
works with today's API, but each rebuild costs too much.
   - Keep the whole-row-group reversal and only add read-ahead. DataFusion is 
doing this now 
([apache/datafusion#24086](https://github.com/apache/datafusion/pull/24086)). 
It hides latency but does not reduce memory.
   
   ## Additional context
   
   Related: [#11238](https://github.com/apache/arrow-rs/pull/11238) 
(batch-granular push decoder), 
[#10555](https://github.com/apache/arrow-rs/pull/10555) (`scan_plan`).
   


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to