Path: blob/main/crates/polars-arrow/src/array/dictionary/ffi.rs
6939 views
use polars_error::{PolarsResult, polars_err};12use super::{DictionaryArray, DictionaryKey};3use crate::array::{FromFfi, PrimitiveArray, ToFfi};4use crate::ffi;56unsafe impl<K: DictionaryKey> ToFfi for DictionaryArray<K> {7fn buffers(&self) -> Vec<Option<*const u8>> {8self.keys.buffers()9}1011fn offset(&self) -> Option<usize> {12self.keys.offset()13}1415fn to_ffi_aligned(&self) -> Self {16Self {17dtype: self.dtype.clone(),18keys: self.keys.to_ffi_aligned(),19values: self.values.clone(),20}21}22}2324impl<K: DictionaryKey, A: ffi::ArrowArrayRef> FromFfi<A> for DictionaryArray<K> {25unsafe fn try_from_ffi(array: A) -> PolarsResult<Self> {26// keys: similar to PrimitiveArray, but the datatype is the inner one27let validity = unsafe { array.validity() }?;28let values = unsafe { array.buffer::<K>(1) }?;2930let dtype = array.dtype().clone();3132let keys = PrimitiveArray::<K>::try_new(K::PRIMITIVE.into(), values, validity)?;33let values = array.dictionary()?.ok_or_else(34|| polars_err!(ComputeError: "Dictionary Array must contain a dictionary in ffi"),35)?;36let values = ffi::try_from(values)?;3738// the assumption of this trait39DictionaryArray::<K>::try_new_unchecked(dtype, keys, values)40}41}424344