Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-arrow/src/array/dictionary/ffi.rs
6939 views
1
use polars_error::{PolarsResult, polars_err};
2
3
use super::{DictionaryArray, DictionaryKey};
4
use crate::array::{FromFfi, PrimitiveArray, ToFfi};
5
use crate::ffi;
6
7
unsafe impl<K: DictionaryKey> ToFfi for DictionaryArray<K> {
8
fn buffers(&self) -> Vec<Option<*const u8>> {
9
self.keys.buffers()
10
}
11
12
fn offset(&self) -> Option<usize> {
13
self.keys.offset()
14
}
15
16
fn to_ffi_aligned(&self) -> Self {
17
Self {
18
dtype: self.dtype.clone(),
19
keys: self.keys.to_ffi_aligned(),
20
values: self.values.clone(),
21
}
22
}
23
}
24
25
impl<K: DictionaryKey, A: ffi::ArrowArrayRef> FromFfi<A> for DictionaryArray<K> {
26
unsafe fn try_from_ffi(array: A) -> PolarsResult<Self> {
27
// keys: similar to PrimitiveArray, but the datatype is the inner one
28
let validity = unsafe { array.validity() }?;
29
let values = unsafe { array.buffer::<K>(1) }?;
30
31
let dtype = array.dtype().clone();
32
33
let keys = PrimitiveArray::<K>::try_new(K::PRIMITIVE.into(), values, validity)?;
34
let values = array.dictionary()?.ok_or_else(
35
|| polars_err!(ComputeError: "Dictionary Array must contain a dictionary in ffi"),
36
)?;
37
let values = ffi::try_from(values)?;
38
39
// the assumption of this trait
40
DictionaryArray::<K>::try_new_unchecked(dtype, keys, values)
41
}
42
}
43
44