Skip to content
Open
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
72 changes: 66 additions & 6 deletions src/reader/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,7 @@ pub fn read_metadata<R: ChunkReader>(reader: &mut R) -> Result<FileMetadata> {
let footer_length = postscript.footer_length.context(error::OutOfSpecSnafu {
msg: "Footer length is empty",
})?;
let metadata_length = postscript.metadata_length.context(error::OutOfSpecSnafu {
msg: "Metadata length is empty",
})?;
let metadata_length = postscript.metadata_length.unwrap_or(0);

// Ensure we have enough bytes for Footer and Metadata
let mut tail_bytes = if footer_length + metadata_length > tail_bytes.len() as u64 {
Expand Down Expand Up @@ -284,9 +282,7 @@ pub async fn read_metadata_async<R: super::AsyncChunkReader>(
let footer_length = postscript.footer_length.context(error::OutOfSpecSnafu {
msg: "Footer length is empty",
})?;
let metadata_length = postscript.metadata_length.context(error::OutOfSpecSnafu {
msg: "Metadata length is empty",
})?;
let metadata_length = postscript.metadata_length.unwrap_or(0);

// Ensure we have enough bytes for Footer and Metadata
let mut tail_bytes = if footer_length + metadata_length > tail_bytes.len() as u64 {
Expand Down Expand Up @@ -335,3 +331,67 @@ fn deserialize_footer_metadata(bytes: Bytes, compression: Option<Compression>) -
.context(error::IoSnafu)?;
Metadata::decode(buffer.as_slice()).context(error::DecodeProtoSnafu)
}

#[cfg(test)]
mod tests {
use std::sync::Arc;

use arrow::array::Int64Array;
use arrow::datatypes::{DataType, Field, Schema};
use arrow::record_batch::RecordBatch;

use super::*;
use crate::{ArrowReaderBuilder, ArrowWriterBuilder};

fn two_rows() -> RecordBatch {
let schema = Arc::new(Schema::new(vec![Field::new("id", DataType::Int64, false)]));
RecordBatch::try_new(schema, vec![Arc::new(Int64Array::from(vec![7, 11]))]).unwrap()
}

fn without_metadata_length(batch: &RecordBatch) -> Bytes {
let mut file = Vec::new();
let mut writer = ArrowWriterBuilder::new(&mut file, batch.schema())
.try_build()
.unwrap();
writer.write(batch).unwrap();
writer.close().unwrap();

let postscript_start = file.len() - 1 - *file.last().unwrap() as usize;
let mut postscript = PostScript::decode(&file[postscript_start..file.len() - 1]).unwrap();
assert_eq!(postscript.metadata_length, Some(0));
postscript.metadata_length = None;
let postscript = postscript.encode_to_vec();
file.truncate(postscript_start);
file.extend_from_slice(&postscript);
file.push(postscript.len() as u8);
Bytes::from(file)
}

#[test]
fn an_absent_metadata_length_reads_as_an_empty_metadata_section() {
let batch = two_rows();
let rows = ArrowReaderBuilder::try_new(without_metadata_length(&batch))
.unwrap()
.build()
.collect::<Result<Vec<_>, _>>()
.unwrap();
assert_eq!(rows, [batch]);
}

#[cfg(feature = "async")]
#[tokio::test]
async fn an_absent_metadata_length_reads_as_an_empty_metadata_section_async() {
use futures_util::TryStreamExt;

let batch = two_rows();
let file = std::io::Cursor::new(without_metadata_length(&batch));
let rows = ArrowReaderBuilder::try_new_async(file)
.await
.unwrap()
.build_async()
.try_collect::<Vec<_>>()
.await
.unwrap();
assert_eq!(rows, [batch]);
}
}
Loading