Skip to main content

reifydb_core/value/column/push/
none.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2// Copyright (c) 2025 ReifyDB
3
4use std::mem;
5
6use reifydb_type::{storage::DataBitVec, util::bitvec::BitVec};
7
8use crate::value::column::data::{ColumnData, with_container};
9
10impl ColumnData {
11	pub fn push_none(&mut self) {
12		match self {
13			ColumnData::Option {
14				inner,
15				bitvec,
16			} => {
17				// Push a default value to the inner container (not recursive promotion)
18				with_container!(inner.as_mut(), |c| c.push_default());
19				DataBitVec::push(bitvec, false);
20			}
21			_ => {
22				// Promote bare container to Option-wrapped, then push none
23				let len = self.len();
24				let mut bitvec = BitVec::repeat(len, true);
25				let mut inner = mem::replace(self, ColumnData::bool(vec![]));
26				// Push a default value to the inner container directly (avoid recursion)
27				with_container!(&mut inner, |c| c.push_default());
28				DataBitVec::push(&mut bitvec, false);
29				*self = ColumnData::Option {
30					inner: Box::new(inner),
31					bitvec,
32				};
33			}
34		}
35	}
36}
37
38#[cfg(test)]
39pub mod tests {
40	use reifydb_type::value::{dictionary::DictionaryEntryId, identity::IdentityId, r#type::Type};
41
42	use crate::value::column::ColumnData;
43
44	#[test]
45	fn test_bool() {
46		let mut col = ColumnData::bool(vec![true]);
47		col.push_none();
48		// push_none promotes a bare column to Option-wrapped
49		assert!(col.is_defined(0));
50		assert!(!col.is_defined(1));
51		assert_eq!(col.len(), 2);
52	}
53
54	#[test]
55	fn test_float4() {
56		let mut col = ColumnData::float4(vec![1.0]);
57		col.push_none();
58		assert!(col.is_defined(0));
59		assert!(!col.is_defined(1));
60		assert_eq!(col.len(), 2);
61	}
62
63	#[test]
64	fn test_float8() {
65		let mut col = ColumnData::float8(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_int1() {
74		let mut col = ColumnData::int1(vec![1]);
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_int2() {
83		let mut col = ColumnData::int2(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_int4() {
92		let mut col = ColumnData::int4(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_int8() {
101		let mut col = ColumnData::int8(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_int16() {
110		let mut col = ColumnData::int16(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_string() {
119		let mut col = ColumnData::utf8(vec!["a"]);
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_uint1() {
128		let mut col = ColumnData::uint1(vec![1]);
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_uint2() {
137		let mut col = ColumnData::uint2(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_uint4() {
146		let mut col = ColumnData::uint4(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_uint8() {
155		let mut col = ColumnData::uint8(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_uint16() {
164		let mut col = ColumnData::uint16(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_identity_id() {
173		let mut col = ColumnData::identity_id(vec![IdentityId::generate()]);
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_dictionary_id() {
182		let mut col = ColumnData::dictionary_id(vec![DictionaryEntryId::U4(10)]);
183		col.push_none();
184		assert!(col.is_defined(0));
185		assert!(!col.is_defined(1));
186		assert_eq!(col.len(), 2);
187	}
188
189	#[test]
190	fn test_none_on_option() {
191		let mut col = ColumnData::none_typed(Type::Boolean, 5);
192		col.push_none();
193		assert_eq!(col.len(), 6);
194		assert!(!col.is_defined(0));
195		assert!(!col.is_defined(5));
196	}
197}