Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

How to downcast dyn Array using as_any and data_type? #1101

Answered by jorgecarleitao
twitu asked this question in Q&A
Discussion options

You must be logged in to vote

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:

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

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@twitu
Comment options

Answer selected by twitu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants