1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use std::any::Any;
use std::fmt;
use crate::array::{Array, ArrayData, ArrayDataRef};
use crate::datatypes::*;
pub struct NullArray {
data: ArrayDataRef,
}
impl NullArray {
pub fn new(length: usize) -> Self {
let array_data = ArrayData::builder(DataType::Null).len(length).build();
NullArray::from(array_data)
}
}
impl Array for NullArray {
fn as_any(&self) -> &Any {
self
}
fn data(&self) -> ArrayDataRef {
self.data.clone()
}
fn data_ref(&self) -> &ArrayDataRef {
&self.data
}
fn is_null(&self, _index: usize) -> bool {
true
}
fn is_valid(&self, _index: usize) -> bool {
false
}
fn null_count(&self) -> usize {
self.data().len()
}
}
impl From<ArrayDataRef> for NullArray {
fn from(data: ArrayDataRef) -> Self {
assert_eq!(
data.buffers().len(),
0,
"NullArray data should contain 0 buffers"
);
assert!(
data.null_buffer().is_none(),
"NullArray data should not contain a null buffer, as no buffers are required"
);
Self { data }
}
}
impl fmt::Debug for NullArray {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "NullArray")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_null_array() {
let array1 = NullArray::new(32);
assert_eq!(array1.len(), 32);
assert_eq!(array1.null_count(), 32);
assert_eq!(array1.is_valid(0), false);
}
#[test]
fn test_null_array_slice() {
let array1 = NullArray::new(32);
let array2 = array1.slice(8, 16);
assert_eq!(array2.len(), 16);
assert_eq!(array2.null_count(), 16);
assert_eq!(array2.offset(), 8);
}
}