Path: blob/main/crates/polars-arrow/src/io/avro/read/mod.rs
7884 views
//! APIs to read from Avro format to arrow.1use std::io::Read;23use avro_schema::file::FileMetadata;4use avro_schema::read::fallible_streaming_iterator::FallibleStreamingIterator;5use avro_schema::read::{BlockStreamingIterator, block_iterator};6use avro_schema::schema::Field as AvroField;78mod deserialize;9pub use deserialize::deserialize;10use polars_error::PolarsResult;1112mod nested;13mod schema;14mod util;1516pub use schema::infer_schema;1718use crate::array::Array;19use crate::datatypes::ArrowSchema;20use crate::record_batch::RecordBatchT;2122/// Single threaded, blocking reader of Avro; [`Iterator`] of [`RecordBatchT`].23pub struct Reader<R: Read> {24iter: BlockStreamingIterator<R>,25avro_fields: Vec<AvroField>,26fields: ArrowSchema,27projection: Vec<bool>,28}2930impl<R: Read> Reader<R> {31/// Creates a new [`Reader`].32pub fn new(33reader: R,34metadata: FileMetadata,35fields: ArrowSchema,36projection: Option<Vec<bool>>,37) -> Self {38let projection = projection.unwrap_or_else(|| fields.iter().map(|_| true).collect());3940Self {41iter: block_iterator(reader, metadata.compression, metadata.marker),42avro_fields: metadata.record.fields,43fields,44projection,45}46}4748/// Deconstructs itself into its internal reader49pub fn into_inner(self) -> R {50self.iter.into_inner()51}52}5354impl<R: Read> Iterator for Reader<R> {55type Item = PolarsResult<RecordBatchT<Box<dyn Array>>>;5657fn next(&mut self) -> Option<Self::Item> {58let fields = &self.fields;59let avro_fields = &self.avro_fields;60let projection = &self.projection;6162self.iter63.next()64.transpose()65.map(|maybe_block| deserialize(maybe_block?, fields, avro_fields, projection))66}67}686970