Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-arrow/src/array/map/ffi.rs
6939 views
1
use polars_error::PolarsResult;
2
3
use super::super::Array;
4
use super::super::ffi::ToFfi;
5
use super::MapArray;
6
use crate::array::FromFfi;
7
use crate::bitmap::align;
8
use crate::ffi;
9
use crate::offset::OffsetsBuffer;
10
11
unsafe impl ToFfi for MapArray {
12
fn buffers(&self) -> Vec<Option<*const u8>> {
13
vec![
14
self.validity.as_ref().map(|x| x.as_ptr()),
15
Some(self.offsets.buffer().storage_ptr().cast::<u8>()),
16
]
17
}
18
19
fn children(&self) -> Vec<Box<dyn Array>> {
20
vec![self.field.clone()]
21
}
22
23
fn offset(&self) -> Option<usize> {
24
let offset = self.offsets.buffer().offset();
25
if let Some(bitmap) = self.validity.as_ref() {
26
if bitmap.offset() == offset {
27
Some(offset)
28
} else {
29
None
30
}
31
} else {
32
Some(offset)
33
}
34
}
35
36
fn to_ffi_aligned(&self) -> Self {
37
let offset = self.offsets.buffer().offset();
38
39
let validity = self.validity.as_ref().map(|bitmap| {
40
if bitmap.offset() == offset {
41
bitmap.clone()
42
} else {
43
align(bitmap, offset)
44
}
45
});
46
47
Self {
48
dtype: self.dtype.clone(),
49
validity,
50
offsets: self.offsets.clone(),
51
field: self.field.clone(),
52
}
53
}
54
}
55
56
impl<A: ffi::ArrowArrayRef> FromFfi<A> for MapArray {
57
unsafe fn try_from_ffi(array: A) -> PolarsResult<Self> {
58
let dtype = array.dtype().clone();
59
let validity = unsafe { array.validity() }?;
60
let offsets = unsafe { array.buffer::<i32>(1) }?;
61
let child = array.child(0)?;
62
let values = ffi::try_from(child)?;
63
64
// assumption that data from FFI is well constructed
65
let offsets = unsafe { OffsetsBuffer::new_unchecked(offsets) };
66
67
Self::try_new(dtype, offsets, values, validity)
68
}
69
}
70
71