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 reifydb_type::{storage::DataBitVec, util::bitvec::BitVec};
5
6use crate::value::column::data::{ColumnData, with_container};
7
8impl ColumnData {
9	pub fn push_none(&mut self) {
10		match self {
11			ColumnData::Option {
12				inner,
13				bitvec,
14			} => {
15				// Push a default value to the inner container (not recursive promotion)
16				with_container!(inner.as_mut(), |c| c.push_default());
17				DataBitVec::push(bitvec, false);
18			}
19			_ => {
20				// Promote bare container to Option-wrapped, then push none
21				let len = self.len();
22				let mut bitvec = BitVec::repeat(len, true);
23				let mut inner = std::mem::replace(self, ColumnData::bool(vec![]));
24				// Push a default value to the inner container directly (avoid recursion)
25				with_container!(&mut inner, |c| c.push_default());
26				DataBitVec::push(&mut bitvec, false);
27				*self = ColumnData::Option {
28					inner: Box::new(inner),
29					bitvec,
30				};
31			}
32		}
33	}
34}
35
36#[cfg(test)]
37pub mod tests {
38	use reifydb_type::value::r#type::Type;
39
40	use crate::value::column::ColumnData;
41
42	#[test]
43	fn test_bool() {
44		let mut col = ColumnData::bool(vec![true]);
45		col.push_none();
46		// push_none promotes a bare column to Option-wrapped
47		assert!(col.is_defined(0));
48		assert!(!col.is_defined(1));
49		assert_eq!(col.len(), 2);
50	}
51
52	#[test]
53	fn test_float4() {
54		let mut col = ColumnData::float4(vec![1.0]);
55		col.push_none();
56		assert!(col.is_defined(0));
57		assert!(!col.is_defined(1));
58		assert_eq!(col.len(), 2);
59	}
60
61	#[test]
62	fn test_float8() {
63		let mut col = ColumnData::float8(vec![1.0]);
64		col.push_none();
65		assert!(col.is_defined(0));
66		assert!(!col.is_defined(1));
67		assert_eq!(col.len(), 2);
68	}
69
70	#[test]
71	fn test_int1() {
72		let mut col = ColumnData::int1(vec![1]);
73		col.push_none();
74		assert!(col.is_defined(0));
75		assert!(!col.is_defined(1));
76		assert_eq!(col.len(), 2);
77	}
78
79	#[test]
80	fn test_int2() {
81		let mut col = ColumnData::int2(vec![1]);
82		col.push_none();
83		assert!(col.is_defined(0));
84		assert!(!col.is_defined(1));
85		assert_eq!(col.len(), 2);
86	}
87
88	#[test]
89	fn test_int4() {
90		let mut col = ColumnData::int4(vec![1]);
91		col.push_none();
92		assert!(col.is_defined(0));
93		assert!(!col.is_defined(1));
94		assert_eq!(col.len(), 2);
95	}
96
97	#[test]
98	fn test_int8() {
99		let mut col = ColumnData::int8(vec![1]);
100		col.push_none();
101		assert!(col.is_defined(0));
102		assert!(!col.is_defined(1));
103		assert_eq!(col.len(), 2);
104	}
105
106	#[test]
107	fn test_int16() {
108		let mut col = ColumnData::int16(vec![1]);
109		col.push_none();
110		assert!(col.is_defined(0));
111		assert!(!col.is_defined(1));
112		assert_eq!(col.len(), 2);
113	}
114
115	#[test]
116	fn test_string() {
117		let mut col = ColumnData::utf8(vec!["a"]);
118		col.push_none();
119		assert!(col.is_defined(0));
120		assert!(!col.is_defined(1));
121		assert_eq!(col.len(), 2);
122	}
123
124	#[test]
125	fn test_uint1() {
126		let mut col = ColumnData::uint1(vec![1]);
127		col.push_none();
128		assert!(col.is_defined(0));
129		assert!(!col.is_defined(1));
130		assert_eq!(col.len(), 2);
131	}
132
133	#[test]
134	fn test_uint2() {
135		let mut col = ColumnData::uint2(vec![1]);
136		col.push_none();
137		assert!(col.is_defined(0));
138		assert!(!col.is_defined(1));
139		assert_eq!(col.len(), 2);
140	}
141
142	#[test]
143	fn test_uint4() {
144		let mut col = ColumnData::uint4(vec![1]);
145		col.push_none();
146		assert!(col.is_defined(0));
147		assert!(!col.is_defined(1));
148		assert_eq!(col.len(), 2);
149	}
150
151	#[test]
152	fn test_uint8() {
153		let mut col = ColumnData::uint8(vec![1]);
154		col.push_none();
155		assert!(col.is_defined(0));
156		assert!(!col.is_defined(1));
157		assert_eq!(col.len(), 2);
158	}
159
160	#[test]
161	fn test_uint16() {
162		let mut col = ColumnData::uint16(vec![1]);
163		col.push_none();
164		assert!(col.is_defined(0));
165		assert!(!col.is_defined(1));
166		assert_eq!(col.len(), 2);
167	}
168
169	#[test]
170	fn test_identity_id() {
171		let mut col = ColumnData::identity_id(vec![reifydb_type::value::identity::IdentityId::generate()]);
172		col.push_none();
173		assert!(col.is_defined(0));
174		assert!(!col.is_defined(1));
175		assert_eq!(col.len(), 2);
176	}
177
178	#[test]
179	fn test_dictionary_id() {
180		use reifydb_type::value::dictionary::DictionaryEntryId;
181
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}