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