Path: blob/main/crates/polars-arrow/src/io/ipc/read/array/binary.rs
8393 views
use std::collections::VecDeque;1use std::io::{Read, Seek};23use polars_buffer::Buffer;4use polars_error::{PolarsResult, polars_err};5use polars_utils::bool::UnsafeBool;67use super::super::read_basic::*;8use super::super::{Compression, IpcBuffer, Node};9use crate::array::BinaryArray;10use crate::datatypes::ArrowDataType;11use crate::io::ipc::read::array::{try_get_array_length, try_get_field_node};12use crate::offset::{Offset, OffsetsBuffer};1314#[allow(clippy::too_many_arguments)]15pub fn read_binary<O: Offset, R: Read + Seek>(16field_nodes: &mut VecDeque<Node>,17dtype: ArrowDataType,18buffers: &mut VecDeque<IpcBuffer>,19reader: &mut R,20block_offset: u64,21is_little_endian: bool,22compression: Option<Compression>,23limit: Option<usize>,24scratch: &mut Vec<u8>,25checked: UnsafeBool,26) -> PolarsResult<BinaryArray<O>> {27let field_node = try_get_field_node(field_nodes, &dtype)?;2829let validity = read_validity(30buffers,31field_node,32reader,33block_offset,34is_little_endian,35compression,36limit,37scratch,38)?;3940let length = try_get_array_length(field_node, limit)?;4142let offsets: Buffer<O> = read_buffer(43buffers,441 + length,45reader,46block_offset,47is_little_endian,48compression,49scratch,50)51// Older versions of the IPC format sometimes do not report an offset52.or_else(|_| PolarsResult::Ok(Buffer::<O>::from(vec![O::default()])))?;5354let last_offset = offsets.last().unwrap().to_usize();55let values = read_buffer(56buffers,57last_offset,58reader,59block_offset,60is_little_endian,61compression,62scratch,63)?;6465if *checked {66BinaryArray::<O>::try_new(dtype, offsets.try_into()?, values, validity)67} else {68// SAFETY:69// Invariant of the `checked` state that this is valid.70unsafe {71let offsets = OffsetsBuffer::new_unchecked(offsets);72Ok(BinaryArray::<O>::new_unchecked(73dtype, offsets, values, validity,74))75}76}77}7879pub fn skip_binary(80field_nodes: &mut VecDeque<Node>,81buffers: &mut VecDeque<IpcBuffer>,82) -> PolarsResult<()> {83let _ = field_nodes.pop_front().ok_or_else(|| {84polars_err!(85oos = "IPC: unable to fetch the field for binary. The file or stream is corrupted."86)87})?;8889let _ = buffers90.pop_front()91.ok_or_else(|| polars_err!(oos = "IPC: missing validity buffer."))?;92let _ = buffers93.pop_front()94.ok_or_else(|| polars_err!(oos = "IPC: missing offsets buffer."))?;95let _ = buffers96.pop_front()97.ok_or_else(|| polars_err!(oos = "IPC: missing values buffer."))?;98Ok(())99}100101102