arrow-avro: sync `Reader` hangs on a block with bytes after its last record, and repeats errors forever

Aperta
#12,492 1 commento 0 reazioni 1 assegnatario Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@dayan02dev ci sta già lavorando.

Dal 28/11/2026.

Valutazione

Questa issue non è ancora stata valutata.

Descrizione

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

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

Lingua principale
Rust
Stelle
3.7k
Fork
1.5k
Merge medio
2g 16h
PR unite (30g)
183

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di apache/arrow-rs

Tutte le issue di apache/arrow-rs

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.