@@ -309,22 +309,10 @@ pub(crate) struct PushDecoderStreamState {
309309 /// group at a time as they are decoded or skipped, and topped up to the
310310 /// full range when the stream is dropped.
311311 pub ( crate ) byte_progress : ByteProgress ,
312- /// Emit each row group's rows in reverse order. The active reader is
313- /// drained into [`Self::reversed_batches`] before anything is returned,
314- /// so one decoded row group is held in memory at a time.
315- pub ( crate ) reverse_rows : bool ,
316- /// The scan's row limit, if any, when `reverse_rows` is set. No more than
317- /// this many rows of a row group can reach the output, so only its last
318- /// `limit` rows are kept.
319- pub ( crate ) reverse_limit : Option < usize > ,
320- /// The current row group's batches, each already row-reversed and kept
321- /// in decode order, so popping from the end yields the row group back to
322- /// front.
323- pub ( crate ) reversed_batches : Vec < RecordBatch > ,
324- /// Charges the memory pool for the batches held while a row group is
325- /// reversed. `None` when the scan does not reverse rows or was opened
326- /// without a pool.
327- pub ( crate ) reversed_reservation : Option < MemoryReservation > ,
312+ /// Set when the scan emits each row group's rows in reverse order: the
313+ /// row group is decoded in full before any of it is returned, so one
314+ /// decoded row group is held in memory at a time.
315+ pub ( crate ) reverser : Option < RowGroupReverser > ,
328316}
329317
330318/// A reusable, `Arc`-shared list of prebuilt row-filter candidates.
@@ -450,32 +438,30 @@ impl PushDecoderStreamState {
450438 let elapsed_compute = self . baseline_metrics . elapsed_compute ( ) . clone ( ) ;
451439 let mut timer = elapsed_compute. timer ( ) ;
452440 loop {
453- // Step 1a: a reversed row group still being emitted.
454- if let Some ( batch) = self . reversed_batches . pop ( ) {
455- if let Some ( reservation) = & self . reversed_reservation {
456- reservation. shrink ( batch. get_array_memory_size ( ) ) ;
457- }
441+ // Step 0: a reversed row group still being emitted.
442+ if let Some ( batch) = self . reverser . as_mut ( ) . and_then ( RowGroupReverser :: pop) {
458443 return Some ( ( Ok ( batch) , self ) ) ;
459444 }
460445
461- // Step 1b: in reverse mode, drain the whole row group, reverse it
462- // into `reversed_batches`, and emit it from step 1a.
463- if self . reverse_rows
464- && let Some ( reader) = self . active_reader . take ( )
465- {
466- if let Err ( e) = self . buffer_reversed_row_group ( reader) {
467- return Some ( ( Err ( e) , self ) ) ;
468- }
469- continue ;
470- }
471-
472446 // Step 1: drain a batch from the active reader if any.
473447 if let Some ( reader) = self . active_reader . as_mut ( ) {
474448 match reader. next ( ) {
475449 Some ( Ok ( batch) ) => {
476450 self . copy_arrow_reader_metrics ( ) ;
477451 let result = self . project_batch ( & batch) ;
478- return Some ( ( result, self ) ) ;
452+ let Some ( reverser) = self . reverser . as_mut ( ) else {
453+ return Some ( ( result, self ) ) ;
454+ } ;
455+ // Reverse mode holds the row group until it is fully
456+ // decoded. Yield between its batches so that decoding
457+ // a large row group does not block cancellation.
458+ if let Err ( e) = result. and_then ( |batch| reverser. push ( batch) ) {
459+ return Some ( ( Err ( e) , self ) ) ;
460+ }
461+ timer. stop ( ) ;
462+ tokio:: task:: yield_now ( ) . await ;
463+ timer. restart ( ) ;
464+ continue ;
479465 }
480466 Some ( Err ( e) ) => {
481467 return Some ( ( Err ( DataFusionError :: from ( e) ) , self ) ) ;
@@ -484,6 +470,14 @@ impl PushDecoderStreamState {
484470 // Reader exhausted: drop and fall through to per-RG
485471 // boundary handling, then try_next_reader.
486472 self . active_reader = None ;
473+ // In reverse mode, emit the row group from step 0
474+ // first.
475+ if let Some ( reverser) = self . reverser . as_mut ( ) {
476+ if let Err ( e) = reverser. finish ( ) {
477+ return Some ( ( Err ( e) , self ) ) ;
478+ }
479+ continue ;
480+ }
487481 }
488482 }
489483 }
@@ -743,63 +737,98 @@ impl PushDecoderStreamState {
743737 fn project_batch ( & self , batch : & RecordBatch ) -> Result < RecordBatch > {
744738 self . decoder_projection . map ( batch)
745739 }
740+ }
746741
747- /// Decodes every remaining batch of `reader`'s row group, projects it,
748- /// reverses its rows, and stores the results in
749- /// [`Self::reversed_batches`]. Batches stay in decode order there, so
750- /// popping them yields the row group's rows last to first.
751- ///
752- /// Under [`Self::reverse_limit`] only the row group's last `limit` rows
753- /// are kept: batches that fall entirely before them are dropped as soon
754- /// as later batches cover the limit, so at most `limit` rows plus one
755- /// batch are held while decoding.
756- fn buffer_reversed_row_group (
757- & mut self ,
758- reader : ParquetRecordBatchReader ,
759- ) -> Result < ( ) > {
760- let mut kept: VecDeque < RecordBatch > = VecDeque :: new ( ) ;
761- let mut kept_rows = 0 ;
762- for batch in reader {
763- let batch = batch. map_err ( DataFusionError :: from) ?;
764- self . copy_arrow_reader_metrics ( ) ;
765- let batch = self . project_batch ( & batch) ?;
766- if batch. num_rows ( ) == 0 {
767- continue ;
768- }
769- if let Some ( reservation) = & self . reversed_reservation {
770- reservation. try_grow ( batch. get_array_memory_size ( ) ) ?;
771- }
772- kept_rows += batch. num_rows ( ) ;
773- kept. push_back ( batch) ;
774- if let Some ( limit) = self . reverse_limit {
775- while let Some ( front) = kept. front ( )
776- && kept_rows - front. num_rows ( ) >= limit
742+ /// Buffers one row group's decoded batches and returns them with the row
743+ /// group's rows in reverse order.
744+ pub ( crate ) struct RowGroupReverser {
745+ /// The scan's row limit, if any. No more than this many rows of a row
746+ /// group can reach the output, so only its last `limit` rows are kept.
747+ limit : Option < usize > ,
748+ /// The row group's batches as decoded so far.
749+ pending : VecDeque < RecordBatch > ,
750+ /// Rows in [`Self::pending`].
751+ pending_rows : usize ,
752+ /// The finished row group's batches, each already row-reversed and kept
753+ /// in decode order, so popping from the end yields the row group back to
754+ /// front.
755+ reversed : Vec < RecordBatch > ,
756+ /// Charges the memory pool for the batches held. `None` when the scan was
757+ /// opened without a pool.
758+ reservation : Option < MemoryReservation > ,
759+ }
760+
761+ impl RowGroupReverser {
762+ pub ( crate ) fn new (
763+ limit : Option < usize > ,
764+ reservation : Option < MemoryReservation > ,
765+ ) -> Self {
766+ Self {
767+ limit,
768+ pending : VecDeque :: new ( ) ,
769+ pending_rows : 0 ,
770+ reversed : Vec :: new ( ) ,
771+ reservation,
772+ }
773+ }
774+
775+ /// Returns the next batch of the finished row group, last rows first.
776+ fn pop ( & mut self ) -> Option < RecordBatch > {
777+ let batch = self . reversed . pop ( ) ?;
778+ if let Some ( reservation) = & self . reservation {
779+ reservation. shrink ( batch. get_array_memory_size ( ) ) ;
780+ }
781+ Some ( batch)
782+ }
783+
784+ /// Adds the next decoded batch of the current row group. Under
785+ /// [`Self::limit`], batches that fall entirely before the row group's last
786+ /// `limit` rows are dropped as soon as later batches cover the limit, so
787+ /// at most `limit` rows plus one batch are held.
788+ fn push ( & mut self , batch : RecordBatch ) -> Result < ( ) > {
789+ if batch. num_rows ( ) == 0 {
790+ return Ok ( ( ) ) ;
791+ }
792+ if let Some ( reservation) = & self . reservation {
793+ reservation. try_grow ( batch. get_array_memory_size ( ) ) ?;
794+ }
795+ self . pending_rows += batch. num_rows ( ) ;
796+ self . pending . push_back ( batch) ;
797+ if let Some ( limit) = self . limit {
798+ while let Some ( front) = self . pending . front ( )
799+ && self . pending_rows - front. num_rows ( ) >= limit
800+ {
801+ self . pending_rows -= front. num_rows ( ) ;
802+ if let Some ( dropped) = self . pending . pop_front ( )
803+ && let Some ( reservation) = & self . reservation
777804 {
778- kept_rows -= front. num_rows ( ) ;
779- if let Some ( dropped) = kept. pop_front ( )
780- && let Some ( reservation) = & self . reversed_reservation
781- {
782- reservation. shrink ( dropped. get_array_memory_size ( ) ) ;
783- }
805+ reservation. shrink ( dropped. get_array_memory_size ( ) ) ;
784806 }
785807 }
786808 }
787- if let Some ( limit) = self . reverse_limit
788- && kept_rows > limit
789- && let Some ( front) = kept. front_mut ( )
809+ Ok ( ( ) )
810+ }
811+
812+ /// Ends the current row group: trims it to its last `limit` rows and
813+ /// reverses it, ready for [`Self::pop`].
814+ fn finish ( & mut self ) -> Result < ( ) > {
815+ if let Some ( limit) = self . limit
816+ && self . pending_rows > limit
817+ && let Some ( front) = self . pending . front_mut ( )
790818 {
791- let excess = kept_rows - limit;
819+ let excess = self . pending_rows - limit;
792820 * front = front. slice ( excess, front. num_rows ( ) - excess) ;
793821 }
822+ self . pending_rows = 0 ;
794823 // The reversed copies replace the decoded batches, so the
795- // reservation is resized to exactly what is stored, which is what
796- // step 1a releases batch by batch.
797- for batch in kept {
798- self . reversed_batches . push ( reverse_batch_rows ( & batch) ?) ;
824+ // reservation is resized to exactly what is stored, which `pop`
825+ // releases batch by batch.
826+ for batch in self . pending . drain ( .. ) {
827+ self . reversed . push ( reverse_batch_rows ( & batch) ?) ;
799828 }
800- if let Some ( reservation) = & self . reversed_reservation {
829+ if let Some ( reservation) = & self . reservation {
801830 let stored = self
802- . reversed_batches
831+ . reversed
803832 . iter ( )
804833 . map ( RecordBatch :: get_array_memory_size)
805834 . sum ( ) ;
0 commit comments