Skip to main content

typed_arrow/bridge/
column.rs

1//! Column-level helpers: `data_type_of<R, I>()` and `ColumnBuilder<R, I>`.
2
3use std::marker::PhantomData;
4
5use arrow_schema::DataType;
6
7use super::ArrowBinding;
8use crate::schema::{ColAt, Record};
9
10/// Returns the Arrow `DataType` for column `I` of record `R`.
11#[must_use]
12pub fn data_type_of<R: Record + ColAt<I>, const I: usize>() -> DataType
13where
14    <R as ColAt<I>>::Native: ArrowBinding,
15{
16    <<R as ColAt<I>>::Native as ArrowBinding>::data_type()
17}
18
19/// A typed column builder for column `I` of record `R`.
20pub struct ColumnBuilder<R: Record + ColAt<I>, const I: usize>
21where
22    <R as ColAt<I>>::Native: ArrowBinding,
23{
24    inner: <<R as ColAt<I>>::Native as ArrowBinding>::Builder,
25    _pd: PhantomData<R>,
26}
27
28impl<R: Record + ColAt<I>, const I: usize> ColumnBuilder<R, I>
29where
30    <R as ColAt<I>>::Native: ArrowBinding,
31{
32    /// Create a builder with `capacity`.
33    #[must_use]
34    pub fn with_capacity(capacity: usize) -> Self {
35        Self {
36            inner: <<R as ColAt<I>>::Native as ArrowBinding>::new_builder(capacity),
37            _pd: PhantomData,
38        }
39    }
40
41    /// Append a value.
42    pub fn append_value(&mut self, v: &<R as ColAt<I>>::Native) {
43        <<R as ColAt<I>>::Native as ArrowBinding>::append_value(&mut self.inner, v);
44    }
45
46    /// Append an optional value; `None` appends a null.
47    pub fn append_option(&mut self, v: Option<&<R as ColAt<I>>::Native>) {
48        match v {
49            Some(x) => self.append_value(x),
50            None => <<R as ColAt<I>>::Native as ArrowBinding>::append_null(&mut self.inner),
51        }
52    }
53
54    /// Finish and produce the typed Arrow array for this column.
55    #[must_use]
56    pub fn finish(self) -> <<R as ColAt<I>>::Native as ArrowBinding>::Array {
57        <<R as ColAt<I>>::Native as ArrowBinding>::finish(self.inner)
58    }
59}