@@ -35,6 +35,17 @@ pub struct LimitedBatchCoalescer {
3535 finished : bool ,
3636}
3737
38+ /// Status returned by [`LimitedBatchCoalescer::push_batch`]
39+ #[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
40+ pub enum PushBatchStatus {
41+ /// The limit has **not** been reached, and more batches can be pushed
42+ Continue ,
43+ /// The limit **has** been reached after processing this batch
44+ /// The caller should call [`LimitedBatchCoalescer::finish`]
45+ /// to flush any buffered rows and stop pushing more batches.
46+ LimitReached ,
47+ }
48+
3849impl LimitedBatchCoalescer {
3950 /// Create a new `BatchCoalescer`
4051 ///
@@ -61,11 +72,21 @@ impl LimitedBatchCoalescer {
6172 self . inner . schema ( )
6273 }
6374
64- /// Push next batch, and returns [`true`] indicating if the limit is hit
75+ /// Pushes the next [`RecordBatch`] into the coalescer and returns its status.
76+ ///
77+ /// # Arguments
78+ /// * `batch` - The [`RecordBatch`] to append.
79+ ///
80+ /// # Returns
81+ /// * [`PushBatchStatus::Continue`] - More batches can still be pushed.
82+ /// * [`PushBatchStatus::LimitReached`] - The row limit was reached after processing
83+ /// this batch. The caller should call [`Self::finish`] before retrieving the
84+ /// remaining buffered batches.
6585 ///
66- /// If the limit is reached, the caller must call [`Self::finish()`] to
67- /// complete the buffered results as a batch and finish the query.
68- pub fn push_batch ( & mut self , batch : RecordBatch ) -> Result < bool > {
86+ /// # Errors
87+ /// Returns an error if called after [`Self::finish`] or if the internal push
88+ /// operation fails.
89+ pub fn push_batch ( & mut self , batch : RecordBatch ) -> Result < PushBatchStatus > {
6990 if self . finished {
7091 return internal_err ! (
7192 "LimitedBatchCoalescer: cannot push batch after finish"
@@ -76,7 +97,7 @@ impl LimitedBatchCoalescer {
7697 if let Some ( fetch) = self . fetch {
7798 // limit previously reached
7899 if self . total_rows >= fetch {
79- return Ok ( true ) ;
100+ return Ok ( PushBatchStatus :: LimitReached ) ;
80101 }
81102
82103 // limit now reached
@@ -88,14 +109,14 @@ impl LimitedBatchCoalescer {
88109 let batch_head = batch. slice ( 0 , remaining_rows) ;
89110 self . total_rows += batch_head. num_rows ( ) ;
90111 self . inner . push_batch ( batch_head) ?;
91- return Ok ( true ) ;
112+ return Ok ( PushBatchStatus :: LimitReached ) ;
92113 }
93114 }
94115
116+ // Limit not reached, push the entire batch
95117 self . total_rows += batch. num_rows ( ) ;
96118 self . inner . push_batch ( batch) ?;
97-
98- Ok ( false ) // not at limit
119+ Ok ( PushBatchStatus :: Continue )
99120 }
100121
101122 /// Return true if there is no data buffered
@@ -276,9 +297,13 @@ mod tests {
276297
277298 let mut output_batches = vec ! [ ] ;
278299 for batch in input_batches {
279- if coalescer. push_batch ( batch) . unwrap ( ) {
280- // at limit, finish the coalescer
281- break ;
300+ match coalescer. push_batch ( batch) . unwrap ( ) {
301+ PushBatchStatus :: Continue => {
302+ // continue pushing batches
303+ }
304+ PushBatchStatus :: LimitReached => {
305+ break ;
306+ }
282307 }
283308 }
284309 coalescer. finish ( ) . unwrap ( ) ;
0 commit comments