Path: blob/main/crates/polars-arrow/src/array/struct_/ffi.rs
6939 views
use polars_error::PolarsResult;12use super::super::ffi::ToFfi;3use super::super::{Array, FromFfi};4use super::StructArray;5use crate::ffi;67unsafe impl ToFfi for StructArray {8fn buffers(&self) -> Vec<Option<*const u8>> {9vec![self.validity.as_ref().map(|x| x.as_ptr())]10}1112fn children(&self) -> Vec<Box<dyn Array>> {13self.values.clone()14}1516fn offset(&self) -> Option<usize> {17Some(18self.validity19.as_ref()20.map(|bitmap| bitmap.offset())21.unwrap_or_default(),22)23}2425fn to_ffi_aligned(&self) -> Self {26self.clone()27}28}2930impl<A: ffi::ArrowArrayRef> FromFfi<A> for StructArray {31unsafe fn try_from_ffi(array: A) -> PolarsResult<Self> {32let dtype = array.dtype().clone();33let fields = Self::get_fields(&dtype);3435let arrow_array = array.array();36let validity = unsafe { array.validity() }?;37let len = arrow_array.len();38let offset = arrow_array.offset();39let values = (0..fields.len())40.map(|index| {41let child = array.child(index)?;42ffi::try_from(child).map(|arr| {43// there is a discrepancy with how polars_arrow exports sliced44// struct array and how pyarrow does it.45// # Pyarrow46// ## struct array len 347// * slice 1 by with len 248// offset on struct array: 149// length on struct array: 250// offset on value array: 051// length on value array: 352// # Arrow253// ## struct array len 354// * slice 1 by with len 255// offset on struct array: 056// length on struct array: 357// offset on value array: 158// length on value array: 259//60// this branch will ensure both can round trip61if arr.len() >= (len + offset) {62arr.sliced(offset, len)63} else {64arr65}66})67})68.collect::<PolarsResult<Vec<Box<dyn Array>>>>()?;6970Self::try_new(dtype, len, values, validity)71}72}737475