Skip to main content

arrow_array/builder/
null_builder.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5use crate::builder::ArrayBuilder;
6use crate::{ArrayRef, NullArray};
7use std::any::Any;
8use std::sync::Arc;
9
10/// Builder for [`NullArray`]
11///
12/// # Example
13///
14/// Create a `NullArray` from a `NullBuilder`
15///
16/// ```
17///
18/// # use arrow_array::{Array, NullArray, builder::NullBuilder};
19///
20/// let mut b = NullBuilder::new();
21/// b.append_empty_value();
22/// b.append_null();
23/// b.append_nulls(3);
24/// b.append_empty_values(3);
25/// let arr = b.finish();
26///
27/// assert_eq!(8, arr.len());
28/// assert_eq!(0, arr.null_count());
29/// ```
30#[derive(Debug)]
31pub struct NullBuilder {
32    len: usize,
33}
34
35impl Default for NullBuilder {
36    fn default() -> Self {
37        Self::new()
38    }
39}
40
41impl NullBuilder {
42    /// Creates a new null builder
43    pub fn new() -> Self {
44        Self { len: 0 }
45    }
46
47    /// Appends a null slot into the builder
48    #[inline]
49    pub fn append_null(&mut self) {
50        self.len += 1;
51    }
52
53    /// Appends `n` `null`s into the builder.
54    #[inline]
55    pub fn append_nulls(&mut self, n: usize) {
56        self.len += n;
57    }
58
59    /// Appends a null slot into the builder
60    #[inline]
61    pub fn append_empty_value(&mut self) {
62        self.append_null();
63    }
64
65    /// Appends `n` `null`s into the builder.
66    #[inline]
67    pub fn append_empty_values(&mut self, n: usize) {
68        self.append_nulls(n);
69    }
70
71    /// Builds the [NullArray] and reset this builder.
72    pub fn finish(&mut self) -> NullArray {
73        NullArray::new(std::mem::take(&mut self.len))
74    }
75
76    /// Builds the [NullArray] without resetting the builder.
77    pub fn finish_cloned(&self) -> NullArray {
78        NullArray::new(self.len)
79    }
80}
81
82impl ArrayBuilder for NullBuilder {
83    /// Returns the builder as a non-mutable `Any` reference.
84    fn as_any(&self) -> &dyn Any {
85        self
86    }
87
88    /// Returns the builder as a mutable `Any` reference.
89    fn as_any_mut(&mut self) -> &mut dyn Any {
90        self
91    }
92
93    /// Returns the boxed builder as a box of `Any`.
94    fn into_box_any(self: Box<Self>) -> Box<dyn Any> {
95        self
96    }
97
98    /// Returns the number of array slots in the builder
99    fn len(&self) -> usize {
100        self.len
101    }
102
103    /// Builds the array and reset this builder.
104    fn finish(&mut self) -> ArrayRef {
105        Arc::new(self.finish())
106    }
107
108    /// Builds the array without resetting the builder.
109    fn finish_cloned(&self) -> ArrayRef {
110        Arc::new(self.finish_cloned())
111    }
112}
113
114#[cfg(test)]
115mod tests {
116    use super::*;
117    use crate::Array;
118
119    #[test]
120    fn test_null_array_builder() {
121        let mut builder = NullArray::builder(10);
122        builder.append_null();
123        builder.append_nulls(4);
124        builder.append_empty_value();
125        builder.append_empty_values(4);
126
127        let arr = builder.finish();
128        assert_eq!(10, arr.len());
129        assert_eq!(0, arr.offset());
130        assert_eq!(0, arr.null_count());
131        assert!(arr.is_nullable());
132    }
133
134    #[test]
135    fn test_null_array_builder_finish_cloned() {
136        let mut builder = NullArray::builder(16);
137        builder.append_null();
138        builder.append_empty_value();
139        builder.append_empty_values(3);
140        let mut array = builder.finish_cloned();
141        assert_eq!(5, array.len());
142
143        builder.append_empty_values(5);
144        array = builder.finish();
145        assert_eq!(10, array.len());
146    }
147
148    #[test]
149    fn test_null_array_builder_finish_resets() {
150        let mut builder = NullBuilder::new();
151        builder.append_nulls(10);
152
153        let array = builder.finish();
154        assert_eq!(10, array.len());
155        assert_eq!(0, builder.len());
156        assert!(builder.is_empty());
157
158        builder.append_nulls(3);
159        let array = builder.finish();
160        assert_eq!(3, array.len());
161        assert_eq!(0, builder.len());
162    }
163}