Skip to content

Implement Improved arrow-avro Reader Zero-Byte Record Handling #7966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions arrow-avro/src/reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,10 @@ impl Decoder {
let mut total_consumed = 0usize;
while total_consumed < data.len() && self.decoded_rows < self.batch_size {
let consumed = self.record_decoder.decode(&data[total_consumed..], 1)?;
if consumed == 0 {
break;
}
// A successful call to record_decoder.decode means one row was decoded.
// If `consumed` is 0 on a non-empty buffer, it implies a valid zero-byte record.
// We increment `decoded_rows` to mark progress and avoid an infinite loop.
// We add `consumed` (which can be 0) to `total_consumed`.
total_consumed += consumed;
self.decoded_rows += 1;
}
Expand Down Expand Up @@ -365,11 +366,7 @@ impl<R: BufRead> Reader<R> {
}
// Try to decode more rows from the current block.
let consumed = self.decoder.decode(&self.block_data[self.block_cursor..])?;
if consumed == 0 && self.block_cursor < self.block_data.len() {
self.block_cursor = self.block_data.len();
Comment on lines -368 to -369
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this check was never needed? Because eventually the outer loop's self.decoder.batch_is_full() check would fail and break the loop?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@scovich Yeah it was an over-aggressive guardrail and since this crate isn't public I probably should have gone with your original recommendation. I was just unsure of what work needed to be done to support zero-byte encodings and wanted time to do the research and an integration test to confirm it.

This PR ended up being much smaller than I anticipated.

} else {
self.block_cursor += consumed;
}
self.block_cursor += consumed;
}
self.decoder.flush()
}
Expand Down Expand Up @@ -481,6 +478,29 @@ mod test {
assert!(batch.column(0).as_any().is::<StringViewArray>());
}

#[test]
fn test_read_zero_byte_avro_file() {
let batch = read_file("test/data/zero_byte.avro", 3, false);
let schema = batch.schema();
assert_eq!(schema.fields().len(), 1);
let field = schema.field(0);
assert_eq!(field.name(), "data");
assert_eq!(field.data_type(), &DataType::Binary);
assert!(field.is_nullable());
assert_eq!(batch.num_rows(), 3);
assert_eq!(batch.num_columns(), 1);
let binary_array = batch
.column(0)
.as_any()
.downcast_ref::<BinaryArray>()
.unwrap();
assert!(binary_array.is_null(0));
assert!(binary_array.is_valid(1));
assert_eq!(binary_array.value(1), b"");
assert!(binary_array.is_valid(2));
assert_eq!(binary_array.value(2), b"some bytes");
}

#[test]
fn test_alltypes() {
let files = [
Expand Down
Binary file added arrow-avro/test/data/zero_byte.avro
Binary file not shown.
Loading