reifydb_core/value/column/push/
none.rs1use std::mem;
5
6use reifydb_type::{storage::DataBitVec, util::bitvec::BitVec};
7
8use crate::value::column::buffer::{ColumnBuffer, with_container};
9
10impl ColumnBuffer {
11 pub fn push_none(&mut self) {
12 match self {
13 ColumnBuffer::Option {
14 inner,
15 bitvec,
16 } => {
17 with_container!(inner.as_mut(), |c| c.push_default());
18 DataBitVec::push(bitvec, false);
19 }
20 _ => {
21 let len = self.len();
22 let mut bitvec = BitVec::repeat(len, true);
23 let mut inner = mem::replace(self, ColumnBuffer::bool(vec![]));
24
25 with_container!(&mut inner, |c| c.push_default());
26 DataBitVec::push(&mut bitvec, false);
27 *self = ColumnBuffer::Option {
28 inner: Box::new(inner),
29 bitvec,
30 };
31 }
32 }
33 }
34}
35
36#[cfg(test)]
37pub mod tests {
38 use reifydb_runtime::context::{
39 clock::{Clock, MockClock},
40 rng::Rng,
41 };
42 use reifydb_type::value::{dictionary::DictionaryEntryId, identity::IdentityId, r#type::Type};
43
44 use crate::value::column::ColumnBuffer;
45
46 fn test_clock_and_rng() -> (MockClock, Clock, Rng) {
47 let mock = MockClock::from_millis(1000);
48 let clock = Clock::Mock(mock.clone());
49 let rng = Rng::seeded(42);
50 (mock, clock, rng)
51 }
52
53 #[test]
54 fn test_bool() {
55 let mut col = ColumnBuffer::bool(vec![true]);
56 col.push_none();
57 assert!(col.is_defined(0));
59 assert!(!col.is_defined(1));
60 assert_eq!(col.len(), 2);
61 }
62
63 #[test]
64 fn test_float4() {
65 let mut col = ColumnBuffer::float4(vec![1.0]);
66 col.push_none();
67 assert!(col.is_defined(0));
68 assert!(!col.is_defined(1));
69 assert_eq!(col.len(), 2);
70 }
71
72 #[test]
73 fn test_float8() {
74 let mut col = ColumnBuffer::float8(vec![1.0]);
75 col.push_none();
76 assert!(col.is_defined(0));
77 assert!(!col.is_defined(1));
78 assert_eq!(col.len(), 2);
79 }
80
81 #[test]
82 fn test_int1() {
83 let mut col = ColumnBuffer::int1(vec![1]);
84 col.push_none();
85 assert!(col.is_defined(0));
86 assert!(!col.is_defined(1));
87 assert_eq!(col.len(), 2);
88 }
89
90 #[test]
91 fn test_int2() {
92 let mut col = ColumnBuffer::int2(vec![1]);
93 col.push_none();
94 assert!(col.is_defined(0));
95 assert!(!col.is_defined(1));
96 assert_eq!(col.len(), 2);
97 }
98
99 #[test]
100 fn test_int4() {
101 let mut col = ColumnBuffer::int4(vec![1]);
102 col.push_none();
103 assert!(col.is_defined(0));
104 assert!(!col.is_defined(1));
105 assert_eq!(col.len(), 2);
106 }
107
108 #[test]
109 fn test_int8() {
110 let mut col = ColumnBuffer::int8(vec![1]);
111 col.push_none();
112 assert!(col.is_defined(0));
113 assert!(!col.is_defined(1));
114 assert_eq!(col.len(), 2);
115 }
116
117 #[test]
118 fn test_int16() {
119 let mut col = ColumnBuffer::int16(vec![1]);
120 col.push_none();
121 assert!(col.is_defined(0));
122 assert!(!col.is_defined(1));
123 assert_eq!(col.len(), 2);
124 }
125
126 #[test]
127 fn test_string() {
128 let mut col = ColumnBuffer::utf8(vec!["a"]);
129 col.push_none();
130 assert!(col.is_defined(0));
131 assert!(!col.is_defined(1));
132 assert_eq!(col.len(), 2);
133 }
134
135 #[test]
136 fn test_uint1() {
137 let mut col = ColumnBuffer::uint1(vec![1]);
138 col.push_none();
139 assert!(col.is_defined(0));
140 assert!(!col.is_defined(1));
141 assert_eq!(col.len(), 2);
142 }
143
144 #[test]
145 fn test_uint2() {
146 let mut col = ColumnBuffer::uint2(vec![1]);
147 col.push_none();
148 assert!(col.is_defined(0));
149 assert!(!col.is_defined(1));
150 assert_eq!(col.len(), 2);
151 }
152
153 #[test]
154 fn test_uint4() {
155 let mut col = ColumnBuffer::uint4(vec![1]);
156 col.push_none();
157 assert!(col.is_defined(0));
158 assert!(!col.is_defined(1));
159 assert_eq!(col.len(), 2);
160 }
161
162 #[test]
163 fn test_uint8() {
164 let mut col = ColumnBuffer::uint8(vec![1]);
165 col.push_none();
166 assert!(col.is_defined(0));
167 assert!(!col.is_defined(1));
168 assert_eq!(col.len(), 2);
169 }
170
171 #[test]
172 fn test_uint16() {
173 let mut col = ColumnBuffer::uint16(vec![1]);
174 col.push_none();
175 assert!(col.is_defined(0));
176 assert!(!col.is_defined(1));
177 assert_eq!(col.len(), 2);
178 }
179
180 #[test]
181 fn test_identity_id() {
182 let (_, clock, rng) = test_clock_and_rng();
183 let mut col = ColumnBuffer::identity_id(vec![IdentityId::generate(&clock, &rng)]);
184 col.push_none();
185 assert!(col.is_defined(0));
186 assert!(!col.is_defined(1));
187 assert_eq!(col.len(), 2);
188 }
189
190 #[test]
191 fn test_dictionary_id() {
192 let mut col = ColumnBuffer::dictionary_id(vec![DictionaryEntryId::U4(10)]);
193 col.push_none();
194 assert!(col.is_defined(0));
195 assert!(!col.is_defined(1));
196 assert_eq!(col.len(), 2);
197 }
198
199 #[test]
200 fn test_none_on_option() {
201 let mut col = ColumnBuffer::none_typed(Type::Boolean, 5);
202 col.push_none();
203 assert_eq!(col.len(), 6);
204 assert!(!col.is_defined(0));
205 assert!(!col.is_defined(5));
206 }
207}