polars_arrow/legacy/
conversion.rs

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