1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use std::any::Any;
use std::sync::Arc;

use arrow_array::builder::{ArrayBuilder, PrimitiveBuilder};
use arrow_array::{ArrayRef, ArrowPrimitiveType};
use arrow_schema::DataType;

use crate::traits::{ArrowBuilder, ToArrow};

macro_rules! make_impl {
    ($native_ty:ty, $data_ty:expr, $array_ty:ty) => {
        impl ToArrow for $native_ty {
            type Item = $native_ty;
            type Builder = PrimitiveArrowBuilder<$array_ty>;

            fn to_datatype() -> DataType {
                return $data_ty;
            }
        }
    };
}

make_impl!(i32, DataType::Int32, arrow_array::types::Int32Type);
make_impl!(i64, DataType::Int64, arrow_array::types::Int64Type);
make_impl!(u32, DataType::UInt32, arrow_array::types::UInt32Type);
make_impl!(u64, DataType::UInt64, arrow_array::types::UInt64Type);
make_impl!(f32, DataType::Float32, arrow_array::types::Float32Type);
make_impl!(f64, DataType::Float64, arrow_array::types::Float64Type);

pub struct PrimitiveArrowBuilder<T: ArrowPrimitiveType> {
    builder: PrimitiveBuilder<T>,
}

impl<T: ArrowPrimitiveType> ArrowBuilder<T::Native> for PrimitiveArrowBuilder<T> {
    fn new_with_capacity(capacity: usize) -> Self {
        Self {
            builder: PrimitiveBuilder::with_capacity(capacity),
        }
    }

    fn append(&mut self, value: Option<T::Native>) {
        self.builder.append_option(value)
    }
}

impl<T: ArrowPrimitiveType> ArrayBuilder for PrimitiveArrowBuilder<T> {
    fn len(&self) -> usize {
        self.builder.len()
    }

    fn finish(&mut self) -> ArrayRef {
        Arc::new(self.builder.finish())
    }

    fn finish_cloned(&self) -> ArrayRef {
        Arc::new(self.builder.finish_cloned())
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }

    fn into_box_any(self: Box<Self>) -> Box<dyn Any> {
        self
    }
}