vortex_array/builders/
null.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::any::Any;
5
6use vortex_dtype::DType;
7use vortex_mask::Mask;
8
9use crate::arrays::NullArray;
10use crate::builders::ArrayBuilder;
11use crate::canonical::Canonical;
12use crate::{Array, ArrayRef, IntoArray};
13
14/// The builder for building a [`NullArray`].
15pub struct NullBuilder {
16    length: usize,
17}
18
19impl Default for NullBuilder {
20    fn default() -> Self {
21        Self::new()
22    }
23}
24
25impl NullBuilder {
26    pub fn new() -> Self {
27        Self { length: 0 }
28    }
29}
30
31impl ArrayBuilder for NullBuilder {
32    fn as_any(&self) -> &dyn Any {
33        self
34    }
35
36    fn as_any_mut(&mut self) -> &mut dyn Any {
37        self
38    }
39
40    fn dtype(&self) -> &DType {
41        &DType::Null
42    }
43
44    fn len(&self) -> usize {
45        self.length
46    }
47
48    fn append_zeros(&mut self, n: usize) {
49        self.length += n;
50    }
51
52    unsafe fn append_nulls_unchecked(&mut self, n: usize) {
53        self.length += n;
54    }
55
56    unsafe fn extend_from_array_unchecked(&mut self, array: &dyn Array) {
57        self.append_nulls(array.len());
58    }
59
60    fn ensure_capacity(&mut self, _capacity: usize) {}
61
62    fn set_validity(&mut self, validity: Mask) {
63        self.length = validity.len();
64    }
65
66    fn finish(&mut self) -> ArrayRef {
67        NullArray::new(self.length).into_array()
68    }
69
70    fn finish_into_canonical(&mut self) -> Canonical {
71        Canonical::Null(NullArray::new(self.length))
72    }
73}