dora_arrow_convert/
into_impls.rs

1use crate::IntoArrow;
2use arrow::array::{PrimitiveArray, StringArray, TimestampNanosecondArray};
3use arrow::datatypes::{
4    ArrowPrimitiveType, ArrowTimestampType, Float16Type, Float32Type, Float64Type, Int8Type,
5    Int16Type, Int32Type, Int64Type, UInt8Type, UInt16Type, UInt32Type, UInt64Type,
6};
7use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
8use half::f16;
9
10impl IntoArrow for bool {
11    type A = arrow::array::BooleanArray;
12    fn into_arrow(self) -> Self::A {
13        std::iter::once(Some(self)).collect()
14    }
15}
16
17macro_rules! impl_into_arrow {
18    ($($t:ty => $arrow_type:ty),*) => {
19        $(
20            impl IntoArrow for $t {
21                type A = PrimitiveArray<$arrow_type>;
22                fn into_arrow(self) -> Self::A {
23                    std::iter::once(self).collect()
24                }
25            }
26        )*
27
28        $(
29            impl IntoArrow for Vec<$t> {
30                type A = PrimitiveArray<$arrow_type>;
31                fn into_arrow(self) -> Self::A {
32                    self.into()
33                }
34            }
35        )*
36    };
37}
38
39impl_into_arrow!(
40    u8 => UInt8Type,
41    u16 => UInt16Type,
42    u32 => UInt32Type,
43    u64 => UInt64Type,
44    i8 => Int8Type,
45    i16 => Int16Type,
46    i32 => Int32Type,
47    i64 => Int64Type,
48    f16 => Float16Type,
49    f32 => Float32Type,
50    f64 => Float64Type
51);
52
53impl IntoArrow for &str {
54    type A = StringArray;
55    fn into_arrow(self) -> Self::A {
56        std::iter::once(Some(self)).collect()
57    }
58}
59
60impl IntoArrow for () {
61    type A = arrow::array::NullArray;
62
63    fn into_arrow(self) -> Self::A {
64        arrow::array::NullArray::new(0)
65    }
66}
67
68impl IntoArrow for NaiveDate {
69    type A = arrow::array::Date64Array;
70    fn into_arrow(self) -> Self::A {
71        arrow::array::Date64Array::from(vec![arrow::datatypes::Date64Type::from_naive_date(self)])
72    }
73}
74
75impl IntoArrow for NaiveTime {
76    type A = arrow::array::Time64NanosecondArray;
77    fn into_arrow(self) -> Self::A {
78        arrow::array::Time64NanosecondArray::from(vec![
79            arrow::array::temporal_conversions::time_to_time64ns(self),
80        ])
81    }
82}
83
84impl IntoArrow for String {
85    type A = StringArray;
86    fn into_arrow(self) -> Self::A {
87        std::iter::once(Some(self)).collect()
88    }
89}
90
91impl IntoArrow for Vec<String> {
92    type A = StringArray;
93    fn into_arrow(self) -> Self::A {
94        StringArray::from(self)
95    }
96}
97
98impl IntoArrow for NaiveDateTime {
99    type A = arrow::array::TimestampNanosecondArray;
100    fn into_arrow(self) -> Self::A {
101        let timestamp = match arrow::datatypes::TimestampNanosecondType::make_value(self) {
102            Some(timestamp) => timestamp,
103            None => arrow::datatypes::TimestampNanosecondType::default_value(),
104        };
105        TimestampNanosecondArray::from(vec![timestamp])
106    }
107}