Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-arrow/src/legacy/conversion.rs
6939 views
1
use crate::array::{ArrayRef, PrimitiveArray, StructArray};
2
use crate::datatypes::{ArrowDataType, Field};
3
use crate::record_batch::RecordBatchT;
4
use crate::types::NativeType;
5
6
pub 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
19
pub 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
}
25
26