/Users/andrewlamb/Software/arrow-rs/arrow-array/src/builder/null_builder.rs
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | use crate::builder::ArrayBuilder; |
19 | | use crate::{ArrayRef, NullArray}; |
20 | | use arrow_data::ArrayData; |
21 | | use arrow_schema::DataType; |
22 | | use std::any::Any; |
23 | | use std::sync::Arc; |
24 | | |
25 | | /// Builder for [`NullArray`] |
26 | | /// |
27 | | /// # Example |
28 | | /// |
29 | | /// Create a `NullArray` from a `NullBuilder` |
30 | | /// |
31 | | /// ``` |
32 | | /// |
33 | | /// # use arrow_array::{Array, NullArray, builder::NullBuilder}; |
34 | | /// |
35 | | /// let mut b = NullBuilder::new(); |
36 | | /// b.append_empty_value(); |
37 | | /// b.append_null(); |
38 | | /// b.append_nulls(3); |
39 | | /// b.append_empty_values(3); |
40 | | /// let arr = b.finish(); |
41 | | /// |
42 | | /// assert_eq!(8, arr.len()); |
43 | | /// assert_eq!(0, arr.null_count()); |
44 | | /// ``` |
45 | | #[derive(Debug)] |
46 | | pub struct NullBuilder { |
47 | | len: usize, |
48 | | } |
49 | | |
50 | | impl Default for NullBuilder { |
51 | 0 | fn default() -> Self { |
52 | 0 | Self::new() |
53 | 0 | } |
54 | | } |
55 | | |
56 | | impl NullBuilder { |
57 | | /// Creates a new null builder |
58 | 0 | pub fn new() -> Self { |
59 | 0 | Self { len: 0 } |
60 | 0 | } |
61 | | |
62 | | /// Appends a null slot into the builder |
63 | | #[inline] |
64 | | pub fn append_null(&mut self) { |
65 | | self.len += 1; |
66 | | } |
67 | | |
68 | | /// Appends `n` `null`s into the builder. |
69 | | #[inline] |
70 | | pub fn append_nulls(&mut self, n: usize) { |
71 | | self.len += n; |
72 | | } |
73 | | |
74 | | /// Appends a null slot into the builder |
75 | | #[inline] |
76 | | pub fn append_empty_value(&mut self) { |
77 | | self.append_null(); |
78 | | } |
79 | | |
80 | | /// Appends `n` `null`s into the builder. |
81 | | #[inline] |
82 | | pub fn append_empty_values(&mut self, n: usize) { |
83 | | self.append_nulls(n); |
84 | | } |
85 | | |
86 | | /// Builds the [NullArray] and reset this builder. |
87 | 0 | pub fn finish(&mut self) -> NullArray { |
88 | 0 | let len = self.len(); |
89 | 0 | let builder = ArrayData::new_null(&DataType::Null, len).into_builder(); |
90 | | |
91 | 0 | let array_data = unsafe { builder.build_unchecked() }; |
92 | 0 | NullArray::from(array_data) |
93 | 0 | } |
94 | | |
95 | | /// Builds the [NullArray] without resetting the builder. |
96 | 0 | pub fn finish_cloned(&self) -> NullArray { |
97 | 0 | let len = self.len(); |
98 | 0 | let builder = ArrayData::new_null(&DataType::Null, len).into_builder(); |
99 | | |
100 | 0 | let array_data = unsafe { builder.build_unchecked() }; |
101 | 0 | NullArray::from(array_data) |
102 | 0 | } |
103 | | } |
104 | | |
105 | | impl ArrayBuilder for NullBuilder { |
106 | | /// Returns the builder as a non-mutable `Any` reference. |
107 | 0 | fn as_any(&self) -> &dyn Any { |
108 | 0 | self |
109 | 0 | } |
110 | | |
111 | | /// Returns the builder as a mutable `Any` reference. |
112 | 0 | fn as_any_mut(&mut self) -> &mut dyn Any { |
113 | 0 | self |
114 | 0 | } |
115 | | |
116 | | /// Returns the boxed builder as a box of `Any`. |
117 | 0 | fn into_box_any(self: Box<Self>) -> Box<dyn Any> { |
118 | 0 | self |
119 | 0 | } |
120 | | |
121 | | /// Returns the number of array slots in the builder |
122 | 0 | fn len(&self) -> usize { |
123 | 0 | self.len |
124 | 0 | } |
125 | | |
126 | | /// Builds the array and reset this builder. |
127 | 0 | fn finish(&mut self) -> ArrayRef { |
128 | 0 | Arc::new(self.finish()) |
129 | 0 | } |
130 | | |
131 | | /// Builds the array without resetting the builder. |
132 | 0 | fn finish_cloned(&self) -> ArrayRef { |
133 | 0 | Arc::new(self.finish_cloned()) |
134 | 0 | } |
135 | | } |
136 | | |
137 | | #[cfg(test)] |
138 | | mod tests { |
139 | | use super::*; |
140 | | use crate::Array; |
141 | | |
142 | | #[test] |
143 | | fn test_null_array_builder() { |
144 | | let mut builder = NullArray::builder(10); |
145 | | builder.append_null(); |
146 | | builder.append_nulls(4); |
147 | | builder.append_empty_value(); |
148 | | builder.append_empty_values(4); |
149 | | |
150 | | let arr = builder.finish(); |
151 | | assert_eq!(10, arr.len()); |
152 | | assert_eq!(0, arr.offset()); |
153 | | assert_eq!(0, arr.null_count()); |
154 | | assert!(arr.is_nullable()); |
155 | | } |
156 | | |
157 | | #[test] |
158 | | fn test_null_array_builder_finish_cloned() { |
159 | | let mut builder = NullArray::builder(16); |
160 | | builder.append_null(); |
161 | | builder.append_empty_value(); |
162 | | builder.append_empty_values(3); |
163 | | let mut array = builder.finish_cloned(); |
164 | | assert_eq!(5, array.len()); |
165 | | |
166 | | builder.append_empty_values(5); |
167 | | array = builder.finish(); |
168 | | assert_eq!(10, array.len()); |
169 | | } |
170 | | } |