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/dictionary.rs
6940 views
1
use std::collections::VecDeque;
2
use std::io::{Read, Seek};
3
4
use polars_error::{PolarsResult, polars_bail, polars_err};
5
use polars_utils::aliases::PlHashSet;
6
7
use super::super::{Compression, Dictionaries, IpcBuffer, Node};
8
use super::{read_primitive, skip_primitive};
9
use crate::array::{DictionaryArray, DictionaryKey};
10
use crate::datatypes::ArrowDataType;
11
12
#[allow(clippy::too_many_arguments)]
13
pub fn read_dictionary<T: DictionaryKey, R: Read + Seek>(
14
field_nodes: &mut VecDeque<Node>,
15
dtype: ArrowDataType,
16
id: Option<i64>,
17
buffers: &mut VecDeque<IpcBuffer>,
18
reader: &mut R,
19
dictionaries: &Dictionaries,
20
block_offset: u64,
21
compression: Option<Compression>,
22
limit: Option<usize>,
23
is_little_endian: bool,
24
scratch: &mut Vec<u8>,
25
) -> PolarsResult<DictionaryArray<T>>
26
where
27
Vec<u8>: TryInto<T::Bytes>,
28
{
29
let id = if let Some(id) = id {
30
id
31
} else {
32
polars_bail!(oos = "Dictionary has no id.");
33
};
34
let values = dictionaries
35
.get(&id)
36
.ok_or_else(|| {
37
let valid_ids = dictionaries.keys().collect::<PlHashSet<_>>();
38
polars_err!(ComputeError:
39
"Dictionary id {id} not found. Valid ids: {valid_ids:?}"
40
)
41
})?
42
.clone();
43
44
let keys = read_primitive(
45
field_nodes,
46
T::PRIMITIVE.into(),
47
buffers,
48
reader,
49
block_offset,
50
is_little_endian,
51
compression,
52
limit,
53
scratch,
54
)?;
55
56
DictionaryArray::<T>::try_new(dtype, keys, values)
57
}
58
59
pub fn skip_dictionary(
60
field_nodes: &mut VecDeque<Node>,
61
buffers: &mut VecDeque<IpcBuffer>,
62
) -> PolarsResult<()> {
63
skip_primitive(field_nodes, buffers)
64
}
65
66