typed_arrow_unified/
lib.rs1#![deny(missing_docs)]
2use std::{marker::PhantomData, sync::Arc};
16
17use typed_arrow::{
18 arrow_array::RecordBatch,
19 arrow_schema::Schema,
20 schema::{BuildRows, IntoRecordBatch, RowBuilder, SchemaMeta},
21};
22use typed_arrow_dyn::{DynBuilders, DynError, DynRow, DynSchema};
23
24pub struct Typed<R> {
26 _phantom: PhantomData<R>,
27}
28
29impl<R> Default for Typed<R> {
30 fn default() -> Self {
31 Self {
32 _phantom: PhantomData,
33 }
34 }
35}
36
37pub trait BuildersLike {
39 type Row;
41
42 type Error: std::error::Error;
44
45 fn append_row(&mut self, row: Self::Row) -> Result<(), Self::Error>;
50
51 fn append_option_row(&mut self, row: Option<Self::Row>) -> Result<(), Self::Error>;
56
57 fn finish_into_batch(self) -> RecordBatch;
59
60 fn try_finish_into_batch(self) -> Result<RecordBatch, Self::Error>
66 where
67 Self: Sized,
68 {
69 Ok(self.finish_into_batch())
70 }
71}
72
73pub trait SchemaLike {
75 type Row;
77
78 type Builders: BuildersLike<Row = Self::Row>;
80
81 fn schema_ref(&self) -> Arc<Schema>;
83
84 fn new_builders(&self, capacity: usize) -> Self::Builders;
86
87 fn build_batch<I>(
95 &self,
96 rows: I,
97 ) -> Result<RecordBatch, <Self::Builders as BuildersLike>::Error>
98 where
99 I: IntoIterator<Item = Self::Row>,
100 {
101 let iter = rows.into_iter();
102 let (lb, ub) = iter.size_hint();
103 let capacity = ub.unwrap_or(lb);
104 let mut b = self.new_builders(capacity);
105 for r in iter {
106 b.append_row(r)?;
107 }
108 b.try_finish_into_batch()
109 }
110}
111
112pub struct TypedBuilders<R: BuildRows> {
114 inner: R::Builders,
115}
116
117impl<R: BuildRows> TypedBuilders<R> {
118 fn new(inner: R::Builders) -> Self {
119 Self { inner }
120 }
121}
122
123#[derive(Debug)]
124pub struct NoError;
126
127impl std::fmt::Display for NoError {
128 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
129 write!(f, "NoError")
130 }
131}
132
133impl std::error::Error for NoError {}
134
135impl<R> BuildersLike for TypedBuilders<R>
136where
137 R: BuildRows,
138{
139 type Row = R;
140
141 type Error = NoError;
142
143 fn append_row(&mut self, row: Self::Row) -> Result<(), NoError> {
144 <R::Builders as RowBuilder<R>>::append_row(&mut self.inner, row);
145 Ok(())
146 }
147
148 fn append_option_row(&mut self, row: Option<Self::Row>) -> Result<(), NoError> {
149 <R::Builders as RowBuilder<R>>::append_option_row(&mut self.inner, row);
150 Ok(())
151 }
152
153 fn finish_into_batch(self) -> RecordBatch {
154 <R::Builders as RowBuilder<R>>::finish(self.inner).into_record_batch()
155 }
156}
157
158impl<R> SchemaLike for Typed<R>
160where
161 R: SchemaMeta + BuildRows,
162{
163 type Row = R;
164
165 type Builders = TypedBuilders<R>;
166
167 fn schema_ref(&self) -> Arc<Schema> {
168 R::schema()
169 }
170
171 fn new_builders(&self, capacity: usize) -> Self::Builders {
172 TypedBuilders::new(R::new_builders(capacity))
173 }
174}
175
176impl SchemaLike for DynSchema {
178 type Row = DynRow;
179
180 type Builders = DynBuilders;
181
182 fn schema_ref(&self) -> Arc<Schema> {
183 self.schema.clone()
184 }
185
186 fn new_builders(&self, capacity: usize) -> Self::Builders {
187 DynBuilders::new(self.schema.clone(), capacity)
188 }
189}
190
191impl SchemaLike for Arc<Schema> {
193 type Row = DynRow;
194
195 type Builders = DynBuilders;
196
197 fn schema_ref(&self) -> Arc<Schema> {
198 self.clone()
199 }
200
201 fn new_builders(&self, capacity: usize) -> Self::Builders {
202 DynBuilders::new(self.clone(), capacity)
203 }
204}
205
206impl BuildersLike for DynBuilders {
208 type Row = DynRow;
209
210 type Error = DynError;
211
212 fn append_row(&mut self, row: Self::Row) -> Result<(), DynError> {
213 typed_arrow_dyn::DynBuilders::append_option_row(self, Some(row))
214 }
215
216 fn append_option_row(&mut self, row: Option<Self::Row>) -> Result<(), DynError> {
217 typed_arrow_dyn::DynBuilders::append_option_row(self, row)
218 }
219
220 fn finish_into_batch(self) -> RecordBatch {
221 typed_arrow_dyn::DynBuilders::finish_into_batch(self)
222 }
223
224 fn try_finish_into_batch(self) -> Result<RecordBatch, DynError> {
225 typed_arrow_dyn::DynBuilders::try_finish_into_batch(self)
226 }
227}