arrow_array/array/
null_array.rs1use crate::builder::NullBuilder;
21use crate::{Array, ArrayRef};
22use arrow_buffer::buffer::NullBuffer;
23use arrow_data::{ArrayData, ArrayDataBuilder};
24use arrow_schema::DataType;
25use std::any::Any;
26use std::sync::Arc;
27
28#[derive(Clone)]
46pub struct NullArray {
47 len: usize,
48}
49
50impl NullArray {
51 pub fn new(length: usize) -> Self {
57 Self { len: length }
58 }
59
60 pub fn slice(&self, offset: usize, len: usize) -> Self {
62 assert!(
63 offset.saturating_add(len) <= self.len,
64 "the length + offset of the sliced BooleanBuffer cannot exceed the existing length"
65 );
66
67 Self { len }
68 }
69
70 pub fn builder(_capacity: usize) -> NullBuilder {
75 NullBuilder::new()
76 }
77}
78
79unsafe impl Array for NullArray {
81 fn as_any(&self) -> &dyn Any {
82 self
83 }
84
85 fn to_data(&self) -> ArrayData {
86 self.clone().into()
87 }
88
89 fn into_data(self) -> ArrayData {
90 self.into()
91 }
92
93 fn data_type(&self) -> &DataType {
94 &DataType::Null
95 }
96
97 fn slice(&self, offset: usize, length: usize) -> ArrayRef {
98 Arc::new(self.slice(offset, length))
99 }
100
101 fn len(&self) -> usize {
102 self.len
103 }
104
105 fn is_empty(&self) -> bool {
106 self.len == 0
107 }
108
109 fn offset(&self) -> usize {
110 0
111 }
112
113 fn nulls(&self) -> Option<&NullBuffer> {
114 None
115 }
116
117 fn logical_nulls(&self) -> Option<NullBuffer> {
118 (self.len != 0).then(|| NullBuffer::new_null(self.len))
119 }
120
121 fn is_nullable(&self) -> bool {
122 !self.is_empty()
123 }
124
125 fn logical_null_count(&self) -> usize {
126 self.len
127 }
128
129 fn get_buffer_memory_size(&self) -> usize {
130 0
131 }
132
133 fn get_array_memory_size(&self) -> usize {
134 std::mem::size_of::<Self>()
135 }
136}
137
138impl From<ArrayData> for NullArray {
139 fn from(data: ArrayData) -> Self {
140 let (data_type, len, nulls, _offset, buffers, _child_data) = data.into_parts();
141
142 assert_eq!(
143 data_type,
144 DataType::Null,
145 "NullArray data type should be Null"
146 );
147 assert_eq!(buffers.len(), 0, "NullArray data should contain 0 buffers");
148 assert!(
149 nulls.is_none(),
150 "NullArray data should not contain a null buffer, as no buffers are required"
151 );
152 Self { len }
153 }
154}
155
156impl From<NullArray> for ArrayData {
157 fn from(array: NullArray) -> Self {
158 let builder = ArrayDataBuilder::new(DataType::Null).len(array.len);
159 unsafe { builder.build_unchecked() }
160 }
161}
162
163impl std::fmt::Debug for NullArray {
164 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
165 write!(f, "NullArray({})", self.len())
166 }
167}
168
169#[cfg(test)]
170mod tests {
171 use super::*;
172 use crate::{Int64Array, StructArray, make_array};
173 use arrow_data::transform::MutableArrayData;
174 use arrow_schema::Field;
175
176 #[test]
177 fn test_null_array() {
178 let null_arr = NullArray::new(32);
179
180 assert_eq!(null_arr.len(), 32);
181 assert_eq!(null_arr.null_count(), 0);
182 assert_eq!(null_arr.logical_null_count(), 32);
183 assert_eq!(null_arr.logical_nulls().unwrap().null_count(), 32);
184 assert!(null_arr.is_valid(0));
185 assert!(null_arr.is_nullable());
186 }
187
188 #[test]
189 fn test_null_array_slice() {
190 let array1 = NullArray::new(32);
191
192 let array2 = array1.slice(8, 16);
193 assert_eq!(array2.len(), 16);
194 assert_eq!(array2.null_count(), 0);
195 assert_eq!(array2.logical_null_count(), 16);
196 assert_eq!(array2.logical_nulls().unwrap().null_count(), 16);
197 assert!(array2.is_valid(0));
198 assert!(array2.is_nullable());
199 }
200
201 #[test]
202 fn test_debug_null_array() {
203 let array = NullArray::new(1024 * 1024);
204 assert_eq!(format!("{array:?}"), "NullArray(1048576)");
205 }
206
207 #[test]
208 fn test_null_array_with_parent_null_buffer() {
209 let null_array = NullArray::new(1);
210 let int_array = Int64Array::from(vec![42]);
211
212 let fields = vec![
213 Field::new("a", DataType::Int64, true),
214 Field::new("b", DataType::Null, true),
215 ];
216
217 let struct_array_data = ArrayData::builder(DataType::Struct(fields.into()))
218 .len(1)
219 .add_child_data(int_array.to_data())
220 .add_child_data(null_array.to_data())
221 .build()
222 .unwrap();
223
224 let mut mutable = MutableArrayData::new(vec![&struct_array_data], true, 1);
225
226 mutable.extend_nulls(1);
229 let data = mutable.freeze();
230
231 let struct_array = Arc::new(StructArray::from(data.clone()));
232 assert!(make_array(data) == struct_array);
233 }
234}