Skip to main content

reifydb_core/value/column/push/
value.rs

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