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

   ### Describe the bug
   
   Since #9093 each column reader reserves its values buffer for a whole batch 
before it reads. The push decoder, and the async reader built on it, reads one 
row group at a time and builds each row group's readers for the configured 
batch size. A row group with fewer rows than the batch size therefore comes 
back in arrays whose buffers are allocated for `batch_size` rows.
   
   `ArrowReaderBuilder::with_batch_size` already caps the batch size at the 
file's row count to avoid this (#2511, #2537), but that does not help a file 
with many small row groups, where every batch holds one row group.
   
   We hit this with a wide table: about 2,000 columns in row groups of about 
1,400 rows, read at a batch size of 8,192. Every fixed-width column of every 
batch was allocated for 8,192 rows, 5.7 times what it held, and operators that 
buffer batches or account for them by memory size ran out of budget.
   
   ### To Reproduce
   
   ```rust
   use std::sync::Arc;
   
   use arrow_array::{ArrayRef, Int64Array, RecordBatch};
   use bytes::Bytes;
   use parquet::DecodeResult;
   use parquet::arrow::ArrowWriter;
   use parquet::arrow::push_decoder::ParquetPushDecoderBuilder;
   use parquet::file::metadata::ParquetMetaDataReader;
   use parquet::file::properties::WriterProperties;
   
   fn main() {
       // 10,000 rows of one Int64 column, in row groups of 100 rows
       let a: ArrayRef = Arc::new(Int64Array::from_iter_values(0..10_000));
       let input = RecordBatch::try_from_iter([("a", a)]).unwrap();
       let props = WriterProperties::builder()
           .set_max_row_group_row_count(Some(100))
           .build();
       let mut file = Vec::new();
       let mut writer = ArrowWriter::try_new(&mut file, input.schema(), 
Some(props)).unwrap();
       writer.write(&input).unwrap();
       writer.close().unwrap();
       let file = Bytes::from(file);
   
       let metadata = ParquetMetaDataReader::new()
           .parse_and_finish(&file)
           .unwrap();
       let mut decoder = 
ParquetPushDecoderBuilder::try_new_decoder(Arc::new(metadata))
           .unwrap()
           .with_batch_size(8192)
           .build()
           .unwrap();
       decoder.push_range(0..file.len() as u64, file).unwrap();
   
       let DecodeResult::Data(batch) = decoder.try_decode().unwrap() else {
           panic!("expected a batch");
       };
       println!(
           "{} rows, {} bytes",
           batch.num_rows(),
           batch.get_array_memory_size()
       );
   }
   ```
   
   On `main` (0f1fd354bb) this prints
   
   ```
   100 rows, 65632 bytes
   ```
   
   ### Expected behavior
   
   The batch's buffers are sized for the rows it holds:
   
   ```
   100 rows, 896 bytes
   ```
   
   ### Additional context
   
   The last batch of a row group larger than the batch size also reserves a 
full batch for the rows that remain. That is bounded by one ordinary batch and 
is not part of this report.
   
   ### Willingness to contribute
   
   I plan to contribute a fix for this bug independently
   


-- 
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