Skip to main content

reifydb_core/value/column/push/
value.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use reifydb_type::{
5	storage::DataBitVec,
6	util::bitvec::BitVec,
7	value::{
8		Value,
9		blob::Blob,
10		date::Date,
11		datetime::DateTime,
12		decimal::Decimal,
13		dictionary::DictionaryEntryId,
14		duration::Duration,
15		identity::IdentityId,
16		int::Int,
17		time::Time,
18		uint::Uint,
19		uuid::{Uuid4, Uuid7},
20	},
21};
22
23use crate::value::column::data::{ColumnData, with_container};
24
25macro_rules! push_or_promote {
26	// Helper: wrap a column in Option if there are preceding none values
27	(@wrap_option $self:expr, $new_col:expr, $len:expr) => {
28		if $len > 0 {
29			let mut bitvec = BitVec::repeat($len, false);
30			DataBitVec::push(&mut bitvec, true);
31			*$self = ColumnData::Option {
32				inner: Box::new($new_col),
33				bitvec,
34			};
35		} else {
36			*$self = $new_col;
37		}
38	};
39	// Tuple variant, uses self.push(value)
40	($self:expr, $val:expr, $col_variant:ident, $factory_expr:expr) => {
41		match $self {
42			ColumnData::$col_variant(_) => $self.push($val),
43			_ => unimplemented!(),
44		}
45	};
46	// Struct variant, uses self.push(value)
47	(struct $self:expr, $val:expr, $col_variant:ident, $factory_expr:expr) => {
48		match $self {
49			ColumnData::$col_variant {
50				..
51			} => $self.push($val),
52			_ => unimplemented!(),
53		}
54	};
55	// Tuple variant, direct container push (no Push trait impl needed)
56	(direct $self:expr, $val:expr, $col_variant:ident, $factory_expr:expr) => {
57		match $self {
58			ColumnData::$col_variant(container) => container.push($val),
59			_ => unimplemented!(),
60		}
61	};
62	// Struct variant, direct container push
63	(struct_direct $self:expr, $val:expr, $col_variant:ident, $factory_expr:expr) => {
64		match $self {
65			ColumnData::$col_variant {
66				container,
67				..
68			} => container.push($val),
69			_ => unimplemented!(),
70		}
71	};
72}
73
74impl ColumnData {
75	pub fn push_value(&mut self, value: Value) {
76		// Handle Option wrapper: delegate to inner and update bitvec
77		if let ColumnData::Option {
78			inner,
79			bitvec,
80		} = self
81		{
82			if matches!(value, Value::None { .. }) {
83				with_container!(inner.as_mut(), |c| c.push_default());
84				DataBitVec::push(bitvec, false);
85			} else if DataBitVec::count_ones(bitvec) == 0 {
86				// All-none Option column - need to promote inner type to match the value
87				let len = inner.len();
88				// Create a new typed column with defaults for the none rows, then push the value
89				let mut new_inner = match &value {
90					Value::Boolean(_) => ColumnData::bool(vec![false; len]),
91					Value::Float4(_) => ColumnData::float4(vec![0.0f32; len]),
92					Value::Float8(_) => ColumnData::float8(vec![0.0f64; len]),
93					Value::Int1(_) => ColumnData::int1(vec![0i8; len]),
94					Value::Int2(_) => ColumnData::int2(vec![0i16; len]),
95					Value::Int4(_) => ColumnData::int4(vec![0i32; len]),
96					Value::Int8(_) => ColumnData::int8(vec![0i64; len]),
97					Value::Int16(_) => ColumnData::int16(vec![0i128; len]),
98					Value::Uint1(_) => ColumnData::uint1(vec![0u8; len]),
99					Value::Uint2(_) => ColumnData::uint2(vec![0u16; len]),
100					Value::Uint4(_) => ColumnData::uint4(vec![0u32; len]),
101					Value::Uint8(_) => ColumnData::uint8(vec![0u64; len]),
102					Value::Uint16(_) => ColumnData::uint16(vec![0u128; len]),
103					Value::Utf8(_) => ColumnData::utf8(vec![String::new(); len]),
104					Value::Date(_) => ColumnData::date(vec![Date::default(); len]),
105					Value::DateTime(_) => ColumnData::datetime(vec![DateTime::default(); len]),
106					Value::Time(_) => ColumnData::time(vec![Time::default(); len]),
107					Value::Duration(_) => ColumnData::duration(vec![Duration::default(); len]),
108					Value::Uuid4(_) => ColumnData::uuid4(vec![Uuid4::default(); len]),
109					Value::Uuid7(_) => ColumnData::uuid7(vec![Uuid7::default(); len]),
110					Value::IdentityId(_) => {
111						ColumnData::identity_id(vec![IdentityId::default(); len])
112					}
113					Value::DictionaryId(_) => {
114						ColumnData::dictionary_id(vec![DictionaryEntryId::default(); len])
115					}
116					Value::Blob(_) => ColumnData::blob(vec![Blob::default(); len]),
117					Value::Int(_) => ColumnData::int(vec![Int::default(); len]),
118					Value::Uint(_) => ColumnData::uint(vec![Uint::default(); len]),
119					Value::Decimal(_) => ColumnData::decimal(vec![Decimal::default(); len]),
120					Value::Any(_) => ColumnData::any(vec![Box::new(Value::none()); len]),
121					Value::Record(_) => ColumnData::any(vec![Box::new(Value::none()); len]),
122					Value::Tuple(_) => ColumnData::any(vec![Box::new(Value::none()); len]),
123					_ => unreachable!(),
124				};
125				new_inner.push_value(value);
126				if len > 0 {
127					let mut new_bitvec = BitVec::repeat(len, false);
128					DataBitVec::push(&mut new_bitvec, true);
129					*self = ColumnData::Option {
130						inner: Box::new(new_inner),
131						bitvec: new_bitvec,
132					};
133				} else {
134					*self = new_inner;
135				}
136			} else {
137				inner.push_value(value);
138				DataBitVec::push(bitvec, true);
139			}
140			return;
141		}
142		match value {
143			Value::Boolean(v) => push_or_promote!(self, v, Bool, ColumnData::bool(vec![])),
144			Value::Float4(v) => push_or_promote!(self, v.value(), Float4, ColumnData::float4(vec![])),
145			Value::Float8(v) => push_or_promote!(self, v.value(), Float8, ColumnData::float8(vec![])),
146			Value::Int1(v) => push_or_promote!(self, v, Int1, ColumnData::int1(vec![])),
147			Value::Int2(v) => push_or_promote!(self, v, Int2, ColumnData::int2(vec![])),
148			Value::Int4(v) => push_or_promote!(self, v, Int4, ColumnData::int4(vec![])),
149			Value::Int8(v) => push_or_promote!(self, v, Int8, ColumnData::int8(vec![])),
150			Value::Int16(v) => push_or_promote!(self, v, Int16, ColumnData::int16(vec![])),
151			Value::Uint1(v) => push_or_promote!(self, v, Uint1, ColumnData::uint1(vec![])),
152			Value::Uint2(v) => push_or_promote!(self, v, Uint2, ColumnData::uint2(vec![])),
153			Value::Uint4(v) => push_or_promote!(self, v, Uint4, ColumnData::uint4(vec![])),
154			Value::Uint8(v) => push_or_promote!(self, v, Uint8, ColumnData::uint8(vec![])),
155			Value::Uint16(v) => push_or_promote!(self, v, Uint16, ColumnData::uint16(vec![])),
156			Value::Utf8(v) => {
157				push_or_promote!(struct self, v, Utf8, ColumnData::utf8(Vec::<String>::new()))
158			}
159			Value::Date(v) => push_or_promote!(self, v, Date, ColumnData::date(vec![])),
160			Value::DateTime(v) => push_or_promote!(self, v, DateTime, ColumnData::datetime(vec![])),
161			Value::Time(v) => push_or_promote!(self, v, Time, ColumnData::time(vec![])),
162			Value::Duration(v) => push_or_promote!(self, v, Duration, ColumnData::duration(vec![])),
163			Value::Uuid4(v) => push_or_promote!(self, v, Uuid4, ColumnData::uuid4(vec![])),
164			Value::Uuid7(v) => push_or_promote!(self, v, Uuid7, ColumnData::uuid7(vec![])),
165			Value::IdentityId(v) => {
166				push_or_promote!(direct self, v, IdentityId, ColumnData::identity_id(vec![]))
167			}
168			Value::DictionaryId(v) => {
169				push_or_promote!(direct self, v, DictionaryId, ColumnData::dictionary_id(vec![]))
170			}
171			Value::Blob(v) => push_or_promote!(struct_direct self, v, Blob, ColumnData::blob(vec![])),
172			Value::Int(v) => push_or_promote!(struct_direct self, v, Int, ColumnData::int(vec![])),
173			Value::Uint(v) => push_or_promote!(struct_direct self, v, Uint, ColumnData::uint(vec![])),
174			Value::Decimal(v) => {
175				push_or_promote!(struct_direct self, v, Decimal, ColumnData::decimal(vec![]))
176			}
177			Value::None {
178				..
179			} => self.push_none(),
180			Value::Type(t) => self.push_value(Value::Any(Box::new(Value::Type(t)))),
181			Value::List(v) => self.push_value(Value::Any(Box::new(Value::List(v)))),
182			Value::Record(v) => self.push_value(Value::Any(Box::new(Value::Record(v)))),
183			Value::Tuple(v) => self.push_value(Value::Any(Box::new(Value::Tuple(v)))),
184			Value::Any(v) => match self {
185				ColumnData::Any(container) => container.push(v),
186				_ => unreachable!("Cannot push Any value to non-Any column"),
187			},
188		}
189	}
190}
191
192#[cfg(test)]
193#[allow(clippy::approx_constant)]
194pub mod tests {
195	use reifydb_runtime::context::{
196		clock::{Clock, MockClock},
197		rng::Rng,
198	};
199	use reifydb_type::value::{
200		Value,
201		date::Date,
202		datetime::DateTime,
203		dictionary::DictionaryEntryId,
204		duration::Duration,
205		identity::IdentityId,
206		ordered_f32::OrderedF32,
207		ordered_f64::OrderedF64,
208		time::Time,
209		r#type::Type,
210		uuid::{Uuid4, Uuid7},
211	};
212
213	use crate::value::column::ColumnData;
214
215	fn test_clock_and_rng() -> (MockClock, Clock, Rng) {
216		let mock = MockClock::from_millis(1000);
217		let clock = Clock::Mock(mock.clone());
218		let rng = Rng::seeded(42);
219		(mock, clock, rng)
220	}
221
222	#[test]
223	fn test_bool() {
224		let mut col = ColumnData::bool(vec![true]);
225		col.push_value(Value::Boolean(false));
226		let ColumnData::Bool(container) = col else {
227			panic!("Expected Bool");
228		};
229		assert_eq!(container.data().to_vec(), vec![true, false]);
230	}
231
232	#[test]
233	fn test_undefined_bool() {
234		let mut col = ColumnData::bool(vec![true]);
235		col.push_value(Value::none());
236		// push_value(None) promotes to Option-wrapped; check via ColumnData API
237		assert_eq!(col.len(), 2);
238		assert!(col.is_defined(0));
239		assert!(!col.is_defined(1));
240	}
241
242	#[test]
243	fn test_push_value_to_none_bool() {
244		let mut col = ColumnData::none_typed(Type::Boolean, 2);
245		col.push_value(Value::Boolean(true));
246		assert_eq!(col.len(), 3);
247		assert!(!col.is_defined(0));
248		assert!(!col.is_defined(1));
249		assert!(col.is_defined(2));
250		assert_eq!(col.get_value(2), Value::Boolean(true));
251	}
252
253	#[test]
254	fn test_float4() {
255		let mut col = ColumnData::float4(vec![1.0]);
256		col.push_value(Value::Float4(OrderedF32::try_from(2.0).unwrap()));
257		let ColumnData::Float4(container) = col else {
258			panic!("Expected Float4");
259		};
260		assert_eq!(container.data().as_slice(), &[1.0, 2.0]);
261	}
262
263	#[test]
264	fn test_undefined_float4() {
265		let mut col = ColumnData::float4(vec![1.0]);
266		col.push_value(Value::none());
267		assert_eq!(col.len(), 2);
268		assert!(col.is_defined(0));
269		assert!(!col.is_defined(1));
270	}
271
272	#[test]
273	fn test_push_value_to_none_float4() {
274		let mut col = ColumnData::none_typed(Type::Boolean, 1);
275		col.push_value(Value::Float4(OrderedF32::try_from(3.14).unwrap()));
276		assert_eq!(col.len(), 2);
277		assert!(!col.is_defined(0));
278		assert!(col.is_defined(1));
279	}
280
281	#[test]
282	fn test_float8() {
283		let mut col = ColumnData::float8(vec![1.0]);
284		col.push_value(Value::Float8(OrderedF64::try_from(2.0).unwrap()));
285		let ColumnData::Float8(container) = col else {
286			panic!("Expected Float8");
287		};
288		assert_eq!(container.data().as_slice(), &[1.0, 2.0]);
289	}
290
291	#[test]
292	fn test_undefined_float8() {
293		let mut col = ColumnData::float8(vec![1.0]);
294		col.push_value(Value::none());
295		assert_eq!(col.len(), 2);
296		assert!(col.is_defined(0));
297		assert!(!col.is_defined(1));
298	}
299
300	#[test]
301	fn test_push_value_to_none_float8() {
302		let mut col = ColumnData::none_typed(Type::Boolean, 1);
303		col.push_value(Value::Float8(OrderedF64::try_from(2.718).unwrap()));
304		assert_eq!(col.len(), 2);
305		assert!(!col.is_defined(0));
306		assert!(col.is_defined(1));
307	}
308
309	#[test]
310	fn test_int1() {
311		let mut col = ColumnData::int1(vec![1]);
312		col.push_value(Value::Int1(2));
313		let ColumnData::Int1(container) = col else {
314			panic!("Expected Int1");
315		};
316		assert_eq!(container.data().as_slice(), &[1, 2]);
317	}
318
319	#[test]
320	fn test_undefined_int1() {
321		let mut col = ColumnData::int1(vec![1]);
322		col.push_value(Value::none());
323		assert_eq!(col.len(), 2);
324		assert!(col.is_defined(0));
325		assert!(!col.is_defined(1));
326	}
327
328	#[test]
329	fn test_push_value_to_none_int1() {
330		let mut col = ColumnData::none_typed(Type::Boolean, 1);
331		col.push_value(Value::Int1(5));
332		assert_eq!(col.len(), 2);
333		assert!(!col.is_defined(0));
334		assert!(col.is_defined(1));
335		assert_eq!(col.get_value(1), Value::Int1(5));
336	}
337
338	#[test]
339	fn test_int2() {
340		let mut col = ColumnData::int2(vec![1]);
341		col.push_value(Value::Int2(3));
342		let ColumnData::Int2(container) = col else {
343			panic!("Expected Int2");
344		};
345		assert_eq!(container.data().as_slice(), &[1, 3]);
346	}
347
348	#[test]
349	fn test_undefined_int2() {
350		let mut col = ColumnData::int2(vec![1]);
351		col.push_value(Value::none());
352		assert_eq!(col.len(), 2);
353		assert!(col.is_defined(0));
354		assert!(!col.is_defined(1));
355	}
356
357	#[test]
358	fn test_push_value_to_none_int2() {
359		let mut col = ColumnData::none_typed(Type::Boolean, 1);
360		col.push_value(Value::Int2(10));
361		assert_eq!(col.len(), 2);
362		assert!(!col.is_defined(0));
363		assert!(col.is_defined(1));
364		assert_eq!(col.get_value(1), Value::Int2(10));
365	}
366
367	#[test]
368	fn test_int4() {
369		let mut col = ColumnData::int4(vec![10]);
370		col.push_value(Value::Int4(20));
371		let ColumnData::Int4(container) = col else {
372			panic!("Expected Int4");
373		};
374		assert_eq!(container.data().as_slice(), &[10, 20]);
375	}
376
377	#[test]
378	fn test_undefined_int4() {
379		let mut col = ColumnData::int4(vec![10]);
380		col.push_value(Value::none());
381		assert_eq!(col.len(), 2);
382		assert!(col.is_defined(0));
383		assert!(!col.is_defined(1));
384	}
385
386	#[test]
387	fn test_push_value_to_none_int4() {
388		let mut col = ColumnData::none_typed(Type::Boolean, 1);
389		col.push_value(Value::Int4(20));
390		assert_eq!(col.len(), 2);
391		assert!(!col.is_defined(0));
392		assert!(col.is_defined(1));
393		assert_eq!(col.get_value(1), Value::Int4(20));
394	}
395
396	#[test]
397	fn test_int8() {
398		let mut col = ColumnData::int8(vec![100]);
399		col.push_value(Value::Int8(200));
400		let ColumnData::Int8(container) = col else {
401			panic!("Expected Int8");
402		};
403		assert_eq!(container.data().as_slice(), &[100, 200]);
404	}
405
406	#[test]
407	fn test_undefined_int8() {
408		let mut col = ColumnData::int8(vec![100]);
409		col.push_value(Value::none());
410		assert_eq!(col.len(), 2);
411		assert!(col.is_defined(0));
412		assert!(!col.is_defined(1));
413	}
414
415	#[test]
416	fn test_push_value_to_none_int8() {
417		let mut col = ColumnData::none_typed(Type::Boolean, 1);
418		col.push_value(Value::Int8(30));
419		assert_eq!(col.len(), 2);
420		assert!(!col.is_defined(0));
421		assert!(col.is_defined(1));
422		assert_eq!(col.get_value(1), Value::Int8(30));
423	}
424
425	#[test]
426	fn test_int16() {
427		let mut col = ColumnData::int16(vec![1000]);
428		col.push_value(Value::Int16(2000));
429		let ColumnData::Int16(container) = col else {
430			panic!("Expected Int16");
431		};
432		assert_eq!(container.data().as_slice(), &[1000, 2000]);
433	}
434
435	#[test]
436	fn test_undefined_int16() {
437		let mut col = ColumnData::int16(vec![1000]);
438		col.push_value(Value::none());
439		assert_eq!(col.len(), 2);
440		assert!(col.is_defined(0));
441		assert!(!col.is_defined(1));
442	}
443
444	#[test]
445	fn test_push_value_to_none_int16() {
446		let mut col = ColumnData::none_typed(Type::Boolean, 1);
447		col.push_value(Value::Int16(40));
448		assert_eq!(col.len(), 2);
449		assert!(!col.is_defined(0));
450		assert!(col.is_defined(1));
451		assert_eq!(col.get_value(1), Value::Int16(40));
452	}
453
454	#[test]
455	fn test_uint1() {
456		let mut col = ColumnData::uint1(vec![1]);
457		col.push_value(Value::Uint1(2));
458		let ColumnData::Uint1(container) = col else {
459			panic!("Expected Uint1");
460		};
461		assert_eq!(container.data().as_slice(), &[1, 2]);
462	}
463
464	#[test]
465	fn test_undefined_uint1() {
466		let mut col = ColumnData::uint1(vec![1]);
467		col.push_value(Value::none());
468		assert_eq!(col.len(), 2);
469		assert!(col.is_defined(0));
470		assert!(!col.is_defined(1));
471	}
472
473	#[test]
474	fn test_push_value_to_none_uint1() {
475		let mut col = ColumnData::none_typed(Type::Boolean, 1);
476		col.push_value(Value::Uint1(1));
477		assert_eq!(col.len(), 2);
478		assert!(!col.is_defined(0));
479		assert!(col.is_defined(1));
480		assert_eq!(col.get_value(1), Value::Uint1(1));
481	}
482
483	#[test]
484	fn test_uint2() {
485		let mut col = ColumnData::uint2(vec![10]);
486		col.push_value(Value::Uint2(20));
487		let ColumnData::Uint2(container) = col else {
488			panic!("Expected Uint2");
489		};
490		assert_eq!(container.data().as_slice(), &[10, 20]);
491	}
492
493	#[test]
494	fn test_undefined_uint2() {
495		let mut col = ColumnData::uint2(vec![10]);
496		col.push_value(Value::none());
497		assert_eq!(col.len(), 2);
498		assert!(col.is_defined(0));
499		assert!(!col.is_defined(1));
500	}
501
502	#[test]
503	fn test_push_value_to_none_uint2() {
504		let mut col = ColumnData::none_typed(Type::Boolean, 1);
505		col.push_value(Value::Uint2(2));
506		assert_eq!(col.len(), 2);
507		assert!(!col.is_defined(0));
508		assert!(col.is_defined(1));
509		assert_eq!(col.get_value(1), Value::Uint2(2));
510	}
511
512	#[test]
513	fn test_uint4() {
514		let mut col = ColumnData::uint4(vec![100]);
515		col.push_value(Value::Uint4(200));
516		let ColumnData::Uint4(container) = col else {
517			panic!("Expected Uint4");
518		};
519		assert_eq!(container.data().as_slice(), &[100, 200]);
520	}
521
522	#[test]
523	fn test_undefined_uint4() {
524		let mut col = ColumnData::uint4(vec![100]);
525		col.push_value(Value::none());
526		assert_eq!(col.len(), 2);
527		assert!(col.is_defined(0));
528		assert!(!col.is_defined(1));
529	}
530
531	#[test]
532	fn test_push_value_to_none_uint4() {
533		let mut col = ColumnData::none_typed(Type::Boolean, 1);
534		col.push_value(Value::Uint4(3));
535		assert_eq!(col.len(), 2);
536		assert!(!col.is_defined(0));
537		assert!(col.is_defined(1));
538		assert_eq!(col.get_value(1), Value::Uint4(3));
539	}
540
541	#[test]
542	fn test_uint8() {
543		let mut col = ColumnData::uint8(vec![1000]);
544		col.push_value(Value::Uint8(2000));
545		let ColumnData::Uint8(container) = col else {
546			panic!("Expected Uint8");
547		};
548		assert_eq!(container.data().as_slice(), &[1000, 2000]);
549	}
550
551	#[test]
552	fn test_undefined_uint8() {
553		let mut col = ColumnData::uint8(vec![1000]);
554		col.push_value(Value::none());
555		assert_eq!(col.len(), 2);
556		assert!(col.is_defined(0));
557		assert!(!col.is_defined(1));
558	}
559
560	#[test]
561	fn test_push_value_to_none_uint8() {
562		let mut col = ColumnData::none_typed(Type::Boolean, 1);
563		col.push_value(Value::Uint8(4));
564		assert_eq!(col.len(), 2);
565		assert!(!col.is_defined(0));
566		assert!(col.is_defined(1));
567		assert_eq!(col.get_value(1), Value::Uint8(4));
568	}
569
570	#[test]
571	fn test_uint16() {
572		let mut col = ColumnData::uint16(vec![10000]);
573		col.push_value(Value::Uint16(20000));
574		let ColumnData::Uint16(container) = col else {
575			panic!("Expected Uint16");
576		};
577		assert_eq!(container.data().as_slice(), &[10000, 20000]);
578	}
579
580	#[test]
581	fn test_undefined_uint16() {
582		let mut col = ColumnData::uint16(vec![10000]);
583		col.push_value(Value::none());
584		assert_eq!(col.len(), 2);
585		assert!(col.is_defined(0));
586		assert!(!col.is_defined(1));
587	}
588
589	#[test]
590	fn test_push_value_to_none_uint16() {
591		let mut col = ColumnData::none_typed(Type::Boolean, 1);
592		col.push_value(Value::Uint16(5));
593		assert_eq!(col.len(), 2);
594		assert!(!col.is_defined(0));
595		assert!(col.is_defined(1));
596		assert_eq!(col.get_value(1), Value::Uint16(5));
597	}
598
599	#[test]
600	fn test_utf8() {
601		let mut col = ColumnData::utf8(vec!["hello".to_string()]);
602		col.push_value(Value::Utf8("world".to_string()));
603		let ColumnData::Utf8 {
604			container,
605			..
606		} = col
607		else {
608			panic!("Expected Utf8");
609		};
610		assert_eq!(container.data().as_slice(), &["hello".to_string(), "world".to_string()]);
611	}
612
613	#[test]
614	fn test_undefined_utf8() {
615		let mut col = ColumnData::utf8(vec!["hello".to_string()]);
616		col.push_value(Value::none());
617		assert_eq!(col.len(), 2);
618		assert!(col.is_defined(0));
619		assert!(!col.is_defined(1));
620	}
621
622	#[test]
623	fn test_push_value_to_none_utf8() {
624		let mut col = ColumnData::none_typed(Type::Boolean, 1);
625		col.push_value(Value::Utf8("ok".to_string()));
626		assert_eq!(col.len(), 2);
627		assert!(!col.is_defined(0));
628		assert!(col.is_defined(1));
629		assert_eq!(col.get_value(1), Value::Utf8("ok".to_string()));
630	}
631
632	#[test]
633	fn test_undefined() {
634		let mut col = ColumnData::int2(vec![1]);
635		col.push_value(Value::none());
636		assert_eq!(col.len(), 2);
637		assert!(col.is_defined(0));
638		assert!(!col.is_defined(1));
639	}
640
641	#[test]
642	fn test_date() {
643		let date1 = Date::from_ymd(2023, 1, 1).unwrap();
644		let date2 = Date::from_ymd(2023, 12, 31).unwrap();
645		let mut col = ColumnData::date(vec![date1]);
646		col.push_value(Value::Date(date2));
647		let ColumnData::Date(container) = col else {
648			panic!("Expected Date");
649		};
650		assert_eq!(container.data().as_slice(), &[date1, date2]);
651	}
652
653	#[test]
654	fn test_undefined_date() {
655		let date1 = Date::from_ymd(2023, 1, 1).unwrap();
656		let mut col = ColumnData::date(vec![date1]);
657		col.push_value(Value::none());
658		assert_eq!(col.len(), 2);
659		assert!(col.is_defined(0));
660		assert!(!col.is_defined(1));
661	}
662
663	#[test]
664	fn test_push_value_to_none_date() {
665		let date = Date::from_ymd(2023, 6, 15).unwrap();
666		let mut col = ColumnData::none_typed(Type::Boolean, 1);
667		col.push_value(Value::Date(date));
668		assert_eq!(col.len(), 2);
669		assert!(!col.is_defined(0));
670		assert!(col.is_defined(1));
671		assert_eq!(col.get_value(1), Value::Date(date));
672	}
673
674	#[test]
675	fn test_datetime() {
676		let dt1 = DateTime::from_timestamp(1672531200).unwrap(); // 2023-01-01 00:00:00 SVTC
677		let dt2 = DateTime::from_timestamp(1704067200).unwrap(); // 2024-01-01 00:00:00 SVTC
678		let mut col = ColumnData::datetime(vec![dt1]);
679		col.push_value(Value::DateTime(dt2));
680		let ColumnData::DateTime(container) = col else {
681			panic!("Expected DateTime");
682		};
683		assert_eq!(container.data().as_slice(), &[dt1, dt2]);
684	}
685
686	#[test]
687	fn test_undefined_datetime() {
688		let dt1 = DateTime::from_timestamp(1672531200).unwrap();
689		let mut col = ColumnData::datetime(vec![dt1]);
690		col.push_value(Value::none());
691		assert_eq!(col.len(), 2);
692		assert!(col.is_defined(0));
693		assert!(!col.is_defined(1));
694	}
695
696	#[test]
697	fn test_push_value_to_none_datetime() {
698		let dt = DateTime::from_timestamp(1672531200).unwrap();
699		let mut col = ColumnData::none_typed(Type::Boolean, 1);
700		col.push_value(Value::DateTime(dt));
701		assert_eq!(col.len(), 2);
702		assert!(!col.is_defined(0));
703		assert!(col.is_defined(1));
704		assert_eq!(col.get_value(1), Value::DateTime(dt));
705	}
706
707	#[test]
708	fn test_time() {
709		let time1 = Time::from_hms(12, 30, 0).unwrap();
710		let time2 = Time::from_hms(18, 45, 30).unwrap();
711		let mut col = ColumnData::time(vec![time1]);
712		col.push_value(Value::Time(time2));
713		let ColumnData::Time(container) = col else {
714			panic!("Expected Time");
715		};
716		assert_eq!(container.data().as_slice(), &[time1, time2]);
717	}
718
719	#[test]
720	fn test_undefined_time() {
721		let time1 = Time::from_hms(12, 30, 0).unwrap();
722		let mut col = ColumnData::time(vec![time1]);
723		col.push_value(Value::none());
724		assert_eq!(col.len(), 2);
725		assert!(col.is_defined(0));
726		assert!(!col.is_defined(1));
727	}
728
729	#[test]
730	fn test_push_value_to_none_time() {
731		let time = Time::from_hms(15, 20, 10).unwrap();
732		let mut col = ColumnData::none_typed(Type::Boolean, 1);
733		col.push_value(Value::Time(time));
734		assert_eq!(col.len(), 2);
735		assert!(!col.is_defined(0));
736		assert!(col.is_defined(1));
737		assert_eq!(col.get_value(1), Value::Time(time));
738	}
739
740	#[test]
741	fn test_duration() {
742		let duration1 = Duration::from_days(30).unwrap();
743		let duration2 = Duration::from_hours(24).unwrap();
744		let mut col = ColumnData::duration(vec![duration1]);
745		col.push_value(Value::Duration(duration2));
746		let ColumnData::Duration(container) = col else {
747			panic!("Expected Duration");
748		};
749		assert_eq!(container.data().as_slice(), &[duration1, duration2]);
750	}
751
752	#[test]
753	fn test_undefined_duration() {
754		let duration1 = Duration::from_days(30).unwrap();
755		let mut col = ColumnData::duration(vec![duration1]);
756		col.push_value(Value::none());
757		assert_eq!(col.len(), 2);
758		assert!(col.is_defined(0));
759		assert!(!col.is_defined(1));
760	}
761
762	#[test]
763	fn test_push_value_to_none_duration() {
764		let duration = Duration::from_minutes(90).unwrap();
765		let mut col = ColumnData::none_typed(Type::Boolean, 1);
766		col.push_value(Value::Duration(duration));
767		assert_eq!(col.len(), 2);
768		assert!(!col.is_defined(0));
769		assert!(col.is_defined(1));
770		assert_eq!(col.get_value(1), Value::Duration(duration));
771	}
772
773	#[test]
774	fn test_identity_id() {
775		let (mock, clock, rng) = test_clock_and_rng();
776		let id1 = IdentityId::generate(&clock, &rng);
777		mock.advance_millis(1);
778		let id2 = IdentityId::generate(&clock, &rng);
779		let mut col = ColumnData::identity_id(vec![id1]);
780		col.push_value(Value::IdentityId(id2));
781		let ColumnData::IdentityId(container) = col else {
782			panic!("Expected IdentityId");
783		};
784		assert_eq!(container.data().as_slice(), &[id1, id2]);
785	}
786
787	#[test]
788	fn test_undefined_identity_id() {
789		let (_, clock, rng) = test_clock_and_rng();
790		let id1 = IdentityId::generate(&clock, &rng);
791		let mut col = ColumnData::identity_id(vec![id1]);
792		col.push_value(Value::none());
793		assert_eq!(col.len(), 2);
794		assert!(col.is_defined(0));
795		assert!(!col.is_defined(1));
796	}
797
798	#[test]
799	fn test_push_value_to_none_identity_id() {
800		let (_, clock, rng) = test_clock_and_rng();
801		let id = IdentityId::generate(&clock, &rng);
802		let mut col = ColumnData::none_typed(Type::Boolean, 1);
803		col.push_value(Value::IdentityId(id));
804		assert_eq!(col.len(), 2);
805		assert!(!col.is_defined(0));
806		assert!(col.is_defined(1));
807		assert_eq!(col.get_value(1), Value::IdentityId(id));
808	}
809
810	#[test]
811	fn test_uuid4() {
812		let uuid1 = Uuid4::generate();
813		let uuid2 = Uuid4::generate();
814		let mut col = ColumnData::uuid4(vec![uuid1]);
815		col.push_value(Value::Uuid4(uuid2));
816		let ColumnData::Uuid4(container) = col else {
817			panic!("Expected Uuid4");
818		};
819		assert_eq!(container.data().as_slice(), &[uuid1, uuid2]);
820	}
821
822	#[test]
823	fn test_undefined_uuid4() {
824		let uuid1 = Uuid4::generate();
825		let mut col = ColumnData::uuid4(vec![uuid1]);
826		col.push_value(Value::none());
827		assert_eq!(col.len(), 2);
828		assert!(col.is_defined(0));
829		assert!(!col.is_defined(1));
830	}
831
832	#[test]
833	fn test_push_value_to_none_uuid4() {
834		let uuid = Uuid4::generate();
835		let mut col = ColumnData::none_typed(Type::Boolean, 1);
836		col.push_value(Value::Uuid4(uuid));
837		assert_eq!(col.len(), 2);
838		assert!(!col.is_defined(0));
839		assert!(col.is_defined(1));
840		assert_eq!(col.get_value(1), Value::Uuid4(uuid));
841	}
842
843	#[test]
844	fn test_uuid7() {
845		let (mock, clock, rng) = test_clock_and_rng();
846		let uuid1 = Uuid7::generate(&clock, &rng);
847		mock.advance_millis(1);
848		let uuid2 = Uuid7::generate(&clock, &rng);
849		let mut col = ColumnData::uuid7(vec![uuid1]);
850		col.push_value(Value::Uuid7(uuid2));
851		let ColumnData::Uuid7(container) = col else {
852			panic!("Expected Uuid7");
853		};
854		assert_eq!(container.data().as_slice(), &[uuid1, uuid2]);
855	}
856
857	#[test]
858	fn test_undefined_uuid7() {
859		let (_, clock, rng) = test_clock_and_rng();
860		let uuid1 = Uuid7::generate(&clock, &rng);
861		let mut col = ColumnData::uuid7(vec![uuid1]);
862		col.push_value(Value::none());
863		assert_eq!(col.len(), 2);
864		assert!(col.is_defined(0));
865		assert!(!col.is_defined(1));
866	}
867
868	#[test]
869	fn test_push_value_to_none_uuid7() {
870		let (_, clock, rng) = test_clock_and_rng();
871		let uuid = Uuid7::generate(&clock, &rng);
872		let mut col = ColumnData::none_typed(Type::Boolean, 1);
873		col.push_value(Value::Uuid7(uuid));
874		assert_eq!(col.len(), 2);
875		assert!(!col.is_defined(0));
876		assert!(col.is_defined(1));
877		assert_eq!(col.get_value(1), Value::Uuid7(uuid));
878	}
879
880	#[test]
881	fn test_dictionary_id() {
882		let e1 = DictionaryEntryId::U4(10);
883		let e2 = DictionaryEntryId::U4(20);
884		let mut col = ColumnData::dictionary_id(vec![e1]);
885		col.push_value(Value::DictionaryId(e2));
886		let ColumnData::DictionaryId(container) = col else {
887			panic!("Expected DictionaryId");
888		};
889		assert_eq!(container.data().as_slice(), &[e1, e2]);
890	}
891
892	#[test]
893	fn test_undefined_dictionary_id() {
894		let e1 = DictionaryEntryId::U4(10);
895		let mut col = ColumnData::dictionary_id(vec![e1]);
896		col.push_value(Value::none());
897		assert_eq!(col.len(), 2);
898		assert!(col.is_defined(0));
899		assert!(!col.is_defined(1));
900	}
901
902	#[test]
903	fn test_push_value_to_none_dictionary_id() {
904		let e = DictionaryEntryId::U4(42);
905		let mut col = ColumnData::none_typed(Type::Boolean, 1);
906		col.push_value(Value::DictionaryId(e));
907		assert_eq!(col.len(), 2);
908		assert!(!col.is_defined(0));
909		assert!(col.is_defined(1));
910		assert_eq!(col.get_value(1), Value::DictionaryId(e));
911	}
912}