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/binview.rs
8415 views
1
use std::io::{Read, Seek};
2
3
use polars_buffer::Buffer;
4
use polars_error::polars_err;
5
use polars_utils::bool::UnsafeBool;
6
7
use super::super::read_basic::*;
8
use super::*;
9
use crate::array::{ArrayRef, BinaryViewArrayGeneric, View, ViewType};
10
11
#[allow(clippy::too_many_arguments)]
12
pub fn read_binview<T: ViewType + ?Sized, R: Read + Seek>(
13
field_nodes: &mut VecDeque<Node>,
14
variadic_buffer_counts: &mut VecDeque<usize>,
15
dtype: ArrowDataType,
16
buffers: &mut VecDeque<IpcBuffer>,
17
reader: &mut R,
18
block_offset: u64,
19
is_little_endian: bool,
20
compression: Option<Compression>,
21
limit: Option<usize>,
22
scratch: &mut Vec<u8>,
23
checked: UnsafeBool,
24
) -> PolarsResult<ArrayRef> {
25
let field_node = try_get_field_node(field_nodes, &dtype)?;
26
27
let validity = read_validity(
28
buffers,
29
field_node,
30
reader,
31
block_offset,
32
is_little_endian,
33
compression,
34
limit,
35
scratch,
36
)?;
37
38
let length = try_get_array_length(field_node, limit)?;
39
let views: Buffer<View> = read_buffer(
40
buffers,
41
length,
42
reader,
43
block_offset,
44
is_little_endian,
45
compression,
46
scratch,
47
)?;
48
49
let n_variadic = variadic_buffer_counts.pop_front().ok_or_else(
50
|| polars_err!(ComputeError: "IPC: unable to fetch the variadic buffers\n\nThe file or stream is corrupted.")
51
)?;
52
53
let variadic_buffers = (0..n_variadic)
54
.map(|_| {
55
read_bytes(
56
buffers,
57
reader,
58
block_offset,
59
is_little_endian,
60
compression,
61
scratch,
62
)
63
})
64
.collect::<PolarsResult<Vec<Buffer<u8>>>>()?;
65
66
if *checked {
67
BinaryViewArrayGeneric::<T>::try_new(dtype, views, Buffer::from(variadic_buffers), validity)
68
.map(|arr| arr.boxed())
69
} else {
70
unsafe {
71
Ok(BinaryViewArrayGeneric::<T>::new_unchecked_unknown_md(
72
dtype,
73
views,
74
Buffer::from(variadic_buffers),
75
validity,
76
None,
77
)
78
.boxed())
79
}
80
}
81
}
82
83
pub fn skip_binview(
84
field_nodes: &mut VecDeque<Node>,
85
buffers: &mut VecDeque<IpcBuffer>,
86
variadic_buffer_counts: &mut VecDeque<usize>,
87
) -> PolarsResult<()> {
88
let _ = field_nodes.pop_front().ok_or_else(|| {
89
polars_err!(
90
oos = "IPC: unable to fetch the field for utf8. The file or stream is corrupted."
91
)
92
})?;
93
94
let _ = buffers
95
.pop_front()
96
.ok_or_else(|| polars_err!(oos = "IPC: missing validity buffer."))?;
97
98
let _ = buffers
99
.pop_front()
100
.ok_or_else(|| polars_err!(oos = "IPC: missing views buffer."))?;
101
102
let n_variadic = variadic_buffer_counts.pop_front().ok_or_else(
103
|| polars_err!(ComputeError: "IPC: unable to fetch the variadic buffers\n\nThe file or stream is corrupted.")
104
)?;
105
106
for _ in 0..n_variadic {
107
let _ = buffers
108
.pop_front()
109
.ok_or_else(|| polars_err!(oos = "IPC: missing variadic buffer"))?;
110
}
111
Ok(())
112
}
113
114