How to downcast dyn Array
using as_any
and data_type
?
#1101
-
I'm reading a chunk from a file, then iterating the column array but I need to convert the array to a more specific array type. I've looked at the documentation but haven't found a way to do it. let f = File::open("array_of_arrays.parquet").unwrap();
let fr = FileReader::try_new(&f, None, None, None, None).unwrap();
for chunk in fr.into_iter() {
if let Ok(cols) = chunk {
for array in cols.arrays().iter() {
// QUESTION: array: &Arc<dyn Array>
// QUESTION: cast array to some array type say ListArray
}
}
} Documentation for data_type mentions using Any help is deeply appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey! So, because use arrow2::datatypes::{PhysicalType, PrimitiveType};
match array.data_type().to_physical_type() {
PhysicalType::Primitive(PrimitiveType::Float32) => {
let array = array.as_any().downcast_ref::<PrimitiveArray<f32>>().unwrap(); // guaranteed
// let array = f32-specific operator
let array = array.clone();
Ok(Box::new(array))
}
PhysicalType::Primitive(PrimitiveType::Float64) => {
let array = array.as_any().downcast_ref::<PrimitiveArray<f64>>().unwrap(); // guaranteed
// let array = f64-specific operator
let array = array.clone();
Ok(Box::new(array))
}
PhysicalType::List => {
let array = array.as_any().downcast_ref::<ListArray<i32>>().unwrap(); // guaranteed
// let array = list-specific operator
let array = array.clone();
Ok(Box::new(array))
}
} in other words, Hope this helps. |
Beta Was this translation helpful? Give feedback.
Hey! So, because
Array
is a trait object, we need to match the datatype as you mention. We tried to describe this in the guide. Something like this: