dayan02dev opened a new issue, #12492:
URL: https://github.com/apache/arrow-rs/issues/12492
### Describe the bug
The OCF `Reader` (`ReaderBuilder::build`) tracks progress through a block by
byte position instead of by the block's record count, and it keeps going after
an error:
1. If a block's data has bytes left after its declared records, `next()`
loops forever at 100% CPU.
2. After a decode error, every later `next()` returns the same error again,
so the iterator never ends.
3. A block whose records take zero bytes (for example a record whose only
field is `null`) is skipped, so its rows are lost.
### To Reproduce
Dependencies: `arrow-avro`, `arrow-schema` (60.2.0 or main).
```rust
use std::io::Cursor;
use arrow_avro::reader::ReaderBuilder;
use arrow_avro::writer::AvroWriter;
use arrow_schema::{DataType, Field, Schema};
/// An OCF file with one column of `data_type` and one data block holding
`count`
/// records encoded in `payload`.
fn ocf(data_type: DataType, count: u8, payload: &[u8]) -> Vec<u8> {
let schema = Schema::new(vec![Field::new("a", data_type, false)]);
let mut writer = AvroWriter::new(Vec::new(), schema).unwrap();
writer.finish().unwrap();
let mut file = writer.into_inner();
let sync = file[file.len() - 16..].to_vec();
// zig-zag encoded block count and block size
file.extend([count * 2, payload.len() as u8 * 2]);
file.extend_from_slice(payload);
file.extend_from_slice(&sync);
file
}
fn main() {
// Two `null` records, which take zero bytes each.
let reader = ReaderBuilder::new()
.build(Cursor::new(ocf(DataType::Null, 2, &[])))
.unwrap();
let rows: usize = reader.map(|batch| batch.unwrap().num_rows()).sum();
println!("zero-byte records: {rows} rows");
// A block that declares two int records but holds one.
let reader = ReaderBuilder::new()
.build(Cursor::new(ocf(DataType::Int32, 2, &[0x02])))
.unwrap();
for item in reader.take(3) {
println!("missing record: {:?}", item.map(|batch| batch.num_rows()));
}
// A block that declares one int record (the value 1) followed by one
extra byte.
let reader = ReaderBuilder::new()
.build(Cursor::new(ocf(DataType::Int32, 1, &[0x02, 0x00])))
.unwrap();
for item in reader {
println!("trailing byte: {:?}", item.map(|batch| batch.num_rows()));
}
println!("done");
}
```
Output on main, after which the program spins until killed:
```
zero-byte records: 0 rows
missing record: Err(AvroError("Parser error: bad varint"))
missing record: Err(AvroError("Parser error: bad varint"))
missing record: Err(AvroError("Parser error: bad varint"))
```
### Expected behavior
```
zero-byte records: 2 rows
missing record: Err(AvroError("Parser error: bad varint"))
trailing byte: Ok(1)
done
```
This is what `AsyncAvroFileReader` returns for the same bytes: it moves to
the next block once the block's record count is used up, and stops after the
first error (`arrow-avro/src/reader/async_reader/mod.rs:472-493`).
### Additional context
Affects every published release from 56.2.1 through 60.2.0, and main (the
loop came in with #8100).
Cause: `Reader::read` (`arrow-avro/src/reader/mod.rs:1401-1439`) treats a
block as done only when its byte cursor reaches the end of the block data, so
leftover bytes leave it calling `decode_block` with a record count of 0
forever, and a block with no bytes is skipped; `Reader::next` (`:1448-1450`)
never sets `finished` after an error.
The Avro spec doesn't say what a reader should do with bytes left in a
block. Java's `DataFileStream` fails with "Block read partially, the data may
be corrupt" (AVRO-4006); matching the async reader instead keeps the two
arrow-avro readers consistent.
### 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]