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