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/utf8.rs
6940 views
1
use std::io::{Read, Seek};
2
3
use polars_error::polars_err;
4
5
use super::super::read_basic::*;
6
use super::*;
7
use crate::array::Utf8Array;
8
use crate::buffer::Buffer;
9
use crate::offset::Offset;
10
11
#[allow(clippy::too_many_arguments)]
12
pub fn read_utf8<O: Offset, R: Read + Seek>(
13
field_nodes: &mut VecDeque<Node>,
14
dtype: ArrowDataType,
15
buffers: &mut VecDeque<IpcBuffer>,
16
reader: &mut R,
17
block_offset: u64,
18
is_little_endian: bool,
19
compression: Option<Compression>,
20
limit: Option<usize>,
21
scratch: &mut Vec<u8>,
22
) -> PolarsResult<Utf8Array<O>> {
23
let field_node = try_get_field_node(field_nodes, &dtype)?;
24
25
let validity = read_validity(
26
buffers,
27
field_node,
28
reader,
29
block_offset,
30
is_little_endian,
31
compression,
32
limit,
33
scratch,
34
)?;
35
36
let length = try_get_array_length(field_node, limit)?;
37
38
let offsets: Buffer<O> = read_buffer(
39
buffers,
40
1 + length,
41
reader,
42
block_offset,
43
is_little_endian,
44
compression,
45
scratch,
46
)
47
// Older versions of the IPC format sometimes do not report an offset
48
.or_else(|_| PolarsResult::Ok(Buffer::<O>::from(vec![O::default()])))?;
49
50
let last_offset = offsets.last().unwrap().to_usize();
51
let values = read_buffer(
52
buffers,
53
last_offset,
54
reader,
55
block_offset,
56
is_little_endian,
57
compression,
58
scratch,
59
)?;
60
61
Utf8Array::<O>::try_new(dtype, offsets.try_into()?, values, validity)
62
}
63
64
pub fn skip_utf8(
65
field_nodes: &mut VecDeque<Node>,
66
buffers: &mut VecDeque<IpcBuffer>,
67
) -> PolarsResult<()> {
68
let _ = field_nodes.pop_front().ok_or_else(|| {
69
polars_err!(
70
oos = "IPC: unable to fetch the field for utf8. The file or stream is corrupted."
71
)
72
})?;
73
74
let _ = buffers
75
.pop_front()
76
.ok_or_else(|| polars_err!(oos = "IPC: missing validity buffer."))?;
77
let _ = buffers
78
.pop_front()
79
.ok_or_else(|| polars_err!(oos = "IPC: missing offsets buffer."))?;
80
let _ = buffers
81
.pop_front()
82
.ok_or_else(|| polars_err!(oos = "IPC: missing values buffer."))?;
83
Ok(())
84
}
85
86