vortex_array/builders/
mod.rs1mod bool;
29mod decimal;
30mod extension;
31mod lazy_validity_builder;
32mod list;
33mod null;
34mod primitive;
35mod struct_;
36mod varbinview;
37
38use std::any::Any;
39
40pub use bool::*;
41pub use decimal::*;
42pub use extension::*;
43pub use list::*;
44pub use null::*;
45pub use primitive::*;
46pub use struct_::*;
47pub use varbinview::*;
48use vortex_dtype::{DType, match_each_native_ptype};
49use vortex_error::{VortexResult, vortex_bail, vortex_err};
50use vortex_mask::Mask;
51use vortex_scalar::{
52 BinaryScalar, BoolScalar, ExtScalar, ListScalar, PrimitiveScalar, Scalar, ScalarValue,
53 StructScalar, Utf8Scalar, match_each_decimal_value_type,
54};
55
56use crate::arrays::precision_to_storage_size;
57use crate::{Array, ArrayRef};
58
59pub trait ArrayBuilder: Send {
60 fn as_any(&self) -> &dyn Any;
61
62 fn as_any_mut(&mut self) -> &mut dyn Any;
63
64 fn dtype(&self) -> &DType;
65
66 fn len(&self) -> usize;
67
68 fn is_empty(&self) -> bool {
69 self.len() == 0
70 }
71
72 fn append_zero(&mut self) {
74 self.append_zeros(1)
75 }
76
77 fn append_zeros(&mut self, n: usize);
79
80 fn append_null(&mut self) {
82 self.append_nulls(1)
83 }
84
85 fn append_nulls(&mut self, n: usize);
87
88 fn extend_from_array(&mut self, array: &dyn Array) -> VortexResult<()>;
90
91 fn ensure_capacity(&mut self, capacity: usize);
93
94 fn set_validity(&mut self, validity: Mask);
96
97 fn finish(&mut self) -> ArrayRef;
107}
108
109pub fn builder_with_capacity(dtype: &DType, capacity: usize) -> Box<dyn ArrayBuilder> {
134 match dtype {
135 DType::Null => Box::new(NullBuilder::new()),
136 DType::Bool(n) => Box::new(BoolBuilder::with_capacity(*n, capacity)),
137 DType::Primitive(ptype, n) => {
138 match_each_native_ptype!(ptype, |$P| {
139 Box::new(PrimitiveBuilder::<$P>::with_capacity(*n, capacity))
140 })
141 }
142 DType::Decimal(decimal_type, n) => {
143 match_each_decimal_value_type!(precision_to_storage_size(decimal_type), |$D| {
144 Box::new(DecimalBuilder::<$D>::with_capacity(capacity, decimal_type.clone(), *n))
145 })
146 }
147 DType::Utf8(n) => Box::new(VarBinViewBuilder::with_capacity(DType::Utf8(*n), capacity)),
148 DType::Binary(n) => Box::new(VarBinViewBuilder::with_capacity(
149 DType::Binary(*n),
150 capacity,
151 )),
152 DType::Struct(struct_dtype, n) => Box::new(StructBuilder::with_capacity(
153 struct_dtype.clone(),
154 *n,
155 capacity,
156 )),
157 DType::List(dtype, n) => Box::new(ListBuilder::<u64>::with_capacity(
158 dtype.clone(),
159 *n,
160 capacity,
161 )),
162 DType::Extension(ext_dtype) => {
163 Box::new(ExtensionBuilder::with_capacity(ext_dtype.clone(), capacity))
164 }
165 }
166}
167
168pub trait ArrayBuilderExt: ArrayBuilder {
169 fn append_scalar_value(&mut self, value: ScalarValue) -> VortexResult<()> {
171 if value.is_null() {
172 self.append_null();
173 Ok(())
174 } else {
175 self.append_scalar(&Scalar::new(self.dtype().clone(), value))
176 }
177 }
178
179 fn append_scalar(&mut self, scalar: &Scalar) -> VortexResult<()> {
181 if scalar.dtype() != self.dtype() {
182 vortex_bail!(
183 "Builder has dtype {:?}, scalar has {:?}",
184 self.dtype(),
185 scalar.dtype()
186 )
187 }
188 match scalar.dtype() {
189 DType::Null => self
190 .as_any_mut()
191 .downcast_mut::<NullBuilder>()
192 .ok_or_else(|| vortex_err!("Cannot append null scalar to non-null builder"))?
193 .append_null(),
194 DType::Bool(_) => self
195 .as_any_mut()
196 .downcast_mut::<BoolBuilder>()
197 .ok_or_else(|| vortex_err!("Cannot append bool scalar to non-bool builder"))?
198 .append_option(BoolScalar::try_from(scalar)?.value()),
199 DType::Primitive(ptype, ..) => {
200 match_each_native_ptype!(ptype, |$P| {
201 self
202 .as_any_mut()
203 .downcast_mut::<PrimitiveBuilder<$P>>()
204 .ok_or_else(|| {
205 vortex_err!("Cannot append primitive scalar to non-primitive builder")
206 })?
207 .append_option(PrimitiveScalar::try_from(scalar)?.typed_value::<$P>())
208 })
209 }
210 DType::Decimal(decimal_type, _) => {
211 match_each_decimal_value_type!(precision_to_storage_size(decimal_type), |$D| {
212 self.as_any_mut()
213 .downcast_mut::<DecimalBuilder<$D>>()
214 .ok_or_else(|| {
215 vortex_err!("Cannot append decimal scalar to non-decimal builder")
216 })?
217 .append_option(Option::<$D>::try_from(scalar.as_decimal()).unwrap())
218 })
219 }
220 DType::Utf8(_) => self
221 .as_any_mut()
222 .downcast_mut::<VarBinViewBuilder>()
223 .ok_or_else(|| vortex_err!("Cannot append utf8 scalar to non-utf8 builder"))?
224 .append_option(Utf8Scalar::try_from(scalar)?.value()),
225 DType::Binary(_) => self
226 .as_any_mut()
227 .downcast_mut::<VarBinViewBuilder>()
228 .ok_or_else(|| vortex_err!("Cannot append binary scalar to non-binary builder"))?
229 .append_option(BinaryScalar::try_from(scalar)?.value()),
230 DType::Struct(..) => self
231 .as_any_mut()
232 .downcast_mut::<StructBuilder>()
233 .ok_or_else(|| vortex_err!("Cannot append struct scalar to non-struct builder"))?
234 .append_value(StructScalar::try_from(scalar)?)?,
235 DType::List(..) => self
236 .as_any_mut()
237 .downcast_mut::<ListBuilder<u64>>()
238 .ok_or_else(|| vortex_err!("Cannot append list scalar to non-list builder"))?
239 .append_value(ListScalar::try_from(scalar)?)?,
240 DType::Extension(..) => self
241 .as_any_mut()
242 .downcast_mut::<ExtensionBuilder>()
243 .ok_or_else(|| {
244 vortex_err!("Cannot append extension scalar to non-extension builder")
245 })?
246 .append_value(ExtScalar::try_from(scalar)?)?,
247 }
248 Ok(())
249 }
250}
251
252impl<T: ?Sized + ArrayBuilder> ArrayBuilderExt for T {}