Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-arrow/src/array/struct_/ffi.rs
6939 views
1
use polars_error::PolarsResult;
2
3
use super::super::ffi::ToFfi;
4
use super::super::{Array, FromFfi};
5
use super::StructArray;
6
use crate::ffi;
7
8
unsafe impl ToFfi for StructArray {
9
fn buffers(&self) -> Vec<Option<*const u8>> {
10
vec![self.validity.as_ref().map(|x| x.as_ptr())]
11
}
12
13
fn children(&self) -> Vec<Box<dyn Array>> {
14
self.values.clone()
15
}
16
17
fn offset(&self) -> Option<usize> {
18
Some(
19
self.validity
20
.as_ref()
21
.map(|bitmap| bitmap.offset())
22
.unwrap_or_default(),
23
)
24
}
25
26
fn to_ffi_aligned(&self) -> Self {
27
self.clone()
28
}
29
}
30
31
impl<A: ffi::ArrowArrayRef> FromFfi<A> for StructArray {
32
unsafe fn try_from_ffi(array: A) -> PolarsResult<Self> {
33
let dtype = array.dtype().clone();
34
let fields = Self::get_fields(&dtype);
35
36
let arrow_array = array.array();
37
let validity = unsafe { array.validity() }?;
38
let len = arrow_array.len();
39
let offset = arrow_array.offset();
40
let values = (0..fields.len())
41
.map(|index| {
42
let child = array.child(index)?;
43
ffi::try_from(child).map(|arr| {
44
// there is a discrepancy with how polars_arrow exports sliced
45
// struct array and how pyarrow does it.
46
// # Pyarrow
47
// ## struct array len 3
48
// * slice 1 by with len 2
49
// offset on struct array: 1
50
// length on struct array: 2
51
// offset on value array: 0
52
// length on value array: 3
53
// # Arrow2
54
// ## struct array len 3
55
// * slice 1 by with len 2
56
// offset on struct array: 0
57
// length on struct array: 3
58
// offset on value array: 1
59
// length on value array: 2
60
//
61
// this branch will ensure both can round trip
62
if arr.len() >= (len + offset) {
63
arr.sliced(offset, len)
64
} else {
65
arr
66
}
67
})
68
})
69
.collect::<PolarsResult<Vec<Box<dyn Array>>>>()?;
70
71
Self::try_new(dtype, len, values, validity)
72
}
73
}
74
75