dora_arrow_convert/
lib.rs

1use arrow::array::{Array, Float32Array, Float64Array, Int32Array, Int64Array, UInt32Array};
2use arrow::datatypes::DataType;
3use eyre::{eyre, ContextCompat, Result};
4use num::NumCast;
5use std::ops::{Deref, DerefMut};
6
7mod from_impls;
8mod into_impls;
9
10pub trait IntoArrow {
11    type A: Array;
12
13    fn into_arrow(self) -> Self::A;
14}
15
16#[derive(Debug)]
17pub struct ArrowData(pub arrow::array::ArrayRef);
18
19impl Deref for ArrowData {
20    type Target = arrow::array::ArrayRef;
21
22    fn deref(&self) -> &Self::Target {
23        &self.0
24    }
25}
26
27impl DerefMut for ArrowData {
28    fn deref_mut(&mut self) -> &mut Self::Target {
29        &mut self.0
30    }
31}
32
33macro_rules! register_array_handlers {
34    ($(($variant:path, $array_type:ty, $type_name:expr)),* $(,)?) => {
35        pub fn into_vec<T>(data: &ArrowData) -> Result<Vec<T>>
36        where
37            T: Copy + NumCast + 'static,
38        {
39            match data.data_type() {
40                $(
41                    $variant => {
42                        let buffer: &$array_type = data
43                            .as_any()
44                            .downcast_ref()
45                            .context(concat!("series is not ", $type_name))?;
46
47                        let mut result = Vec::with_capacity(buffer.len());
48                        for &v in buffer.values() {
49                            let converted = NumCast::from(v).context(format!("Failed to cast value from {} to target type",$type_name))?;
50                            result.push(converted);
51                        }
52                        Ok(result)
53                    }
54                ),*
55                // Error handling for unsupported types
56                unsupported_type => Err(eyre!("Unsupported data type for conversion: {:?}", unsupported_type))
57            }
58        }
59    };
60}
61
62// Register all supported array types in one place
63register_array_handlers! {
64    (DataType::Float32, Float32Array, "float32"),
65    (DataType::Float64, Float64Array, "float64"),
66    (DataType::Int32, Int32Array, "int32"),
67    (DataType::Int64, Int64Array, "int64"),
68    (DataType::UInt32, UInt32Array, "uint32"),
69}