vortex_array/builders/
extension.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::any::Any;
5use std::sync::Arc;
6
7use vortex_dtype::{DType, ExtDType};
8use vortex_error::{VortexResult, vortex_bail};
9use vortex_mask::Mask;
10use vortex_scalar::ExtScalar;
11
12use crate::arrays::ExtensionArray;
13use crate::builders::{ArrayBuilder, ArrayBuilderExt, builder_with_capacity};
14use crate::{Array, ArrayRef, Canonical, IntoArray};
15
16pub struct ExtensionBuilder {
17    storage: Box<dyn ArrayBuilder>,
18    dtype: DType,
19}
20
21impl ExtensionBuilder {
22    pub fn new(ext_dtype: Arc<ExtDType>) -> Self {
23        Self::with_capacity(ext_dtype, 1024)
24    }
25
26    pub fn with_capacity(ext_dtype: Arc<ExtDType>, capacity: usize) -> Self {
27        Self {
28            storage: builder_with_capacity(ext_dtype.storage_dtype(), capacity),
29            dtype: DType::Extension(ext_dtype),
30        }
31    }
32
33    pub fn append_value(&mut self, value: ExtScalar) -> VortexResult<()> {
34        self.storage.append_scalar(&value.storage())
35    }
36
37    pub fn append_option(&mut self, value: Option<ExtScalar>) -> VortexResult<()> {
38        match value {
39            Some(value) => self.append_value(value),
40            None => {
41                self.append_nulls(1);
42                Ok(())
43            }
44        }
45    }
46
47    fn ext_dtype(&self) -> Arc<ExtDType> {
48        if let DType::Extension(ext_dtype) = &self.dtype {
49            ext_dtype.clone()
50        } else {
51            unreachable!()
52        }
53    }
54}
55
56impl ArrayBuilder for ExtensionBuilder {
57    fn as_any(&self) -> &dyn Any {
58        self
59    }
60
61    fn as_any_mut(&mut self) -> &mut dyn Any {
62        self
63    }
64
65    fn dtype(&self) -> &DType {
66        &self.dtype
67    }
68
69    fn len(&self) -> usize {
70        self.storage.len()
71    }
72
73    fn append_zeros(&mut self, n: usize) {
74        self.storage.append_zeros(n)
75    }
76
77    fn append_nulls(&mut self, n: usize) {
78        self.storage.append_nulls(n)
79    }
80
81    fn extend_from_array(&mut self, array: &dyn Array) -> VortexResult<()> {
82        let array = array.to_canonical()?;
83        let Canonical::Extension(array) = array else {
84            vortex_bail!("Expected Extension array, got {:?}", array);
85        };
86        array.storage().append_to_builder(self.storage.as_mut())
87    }
88
89    fn ensure_capacity(&mut self, capacity: usize) {
90        self.storage.ensure_capacity(capacity)
91    }
92
93    fn set_validity(&mut self, validity: Mask) {
94        self.storage.set_validity(validity);
95    }
96
97    fn finish(&mut self) -> ArrayRef {
98        let storage = self.storage.finish();
99        ExtensionArray::new(self.ext_dtype(), storage).into_array()
100    }
101}