Path: blob/main/crates/polars-arrow/src/legacy/conversion.rs
6939 views
use crate::array::{ArrayRef, PrimitiveArray, StructArray};1use crate::datatypes::{ArrowDataType, Field};2use crate::record_batch::RecordBatchT;3use crate::types::NativeType;45pub fn chunk_to_struct(chunk: RecordBatchT<ArrayRef>, fields: Vec<Field>) -> StructArray {6let dtype = ArrowDataType::Struct(fields);7StructArray::new(dtype, chunk.len(), chunk.into_arrays(), None)8}910/// Returns its underlying [`Vec`], if possible.11///12/// This operation returns [`Some`] iff this [`PrimitiveArray`]:13/// * has not been sliced with an offset14/// * has not been cloned (i.e. [`Arc::get_mut`][Arc::get_mut] yields [`Some`])15/// * has not been imported from the c data interface (FFI)16///17/// [Arc::get_mut]: std::sync::Arc::get_mut18pub fn primitive_to_vec<T: NativeType>(arr: ArrayRef) -> Option<Vec<T>> {19let arr_ref = arr.as_any().downcast_ref::<PrimitiveArray<T>>().unwrap();20let buffer = arr_ref.values().clone();21drop(arr); // Drop original reference so refcount becomes 1 if possible.22buffer.into_mut().right()23}242526