Skip to main content

reifydb_core/value/column/push/
value.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4use reifydb_value::value::{
5	Value,
6	container::{
7		any_array::push_any,
8		dictionary_array,
9		digest_array::push_digest,
10		temporal_array::{date_to_native, datetime_to_native, duration_to_native, time_to_native},
11	},
12};
13
14use crate::value::column::builder::{ColumnBuilder, TypedBuilder, append_fixed};
15
16macro_rules! push_or_promote {
17	(native $self:expr, $val:expr, $col_variant:ident) => {
18		match &mut $self.inner {
19			TypedBuilder::$col_variant(builder) => builder.append_value($val),
20			_ => unimplemented!(),
21		}
22	};
23
24	(temporal $self:expr, $val:expr, $col_variant:ident, $to_native:ident) => {
25		match &mut $self.inner {
26			TypedBuilder::$col_variant(builder) => builder.append_value($to_native($val)),
27			_ => unimplemented!(),
28		}
29	};
30
31	(fixed $self:expr, $val:expr, $col_variant:ident) => {
32		match &mut $self.inner {
33			TypedBuilder::$col_variant(builder) => append_fixed(builder, $val.as_bytes()),
34			_ => unimplemented!(),
35		}
36	};
37
38	(varlen $self:expr, $val:expr, $col_variant:ident) => {
39		match &mut $self.inner {
40			TypedBuilder::$col_variant {
41				builder,
42				..
43			} => builder.append_value($val),
44			_ => unimplemented!(),
45		}
46	};
47
48	(decimal $self:expr, $val:expr, $col_variant:ident) => {
49		match &mut $self.inner {
50			TypedBuilder::$col_variant(builder) => builder.push(&$val),
51			_ => unimplemented!(),
52		}
53	};
54}
55
56impl ColumnBuilder {
57	pub fn push_value(&mut self, value: Value) {
58		if matches!(self.inner, TypedBuilder::None(_)) && !matches!(value, Value::None { .. }) {
59			panic!("the untyped none column can not take a value of type {:?}", value.get_type());
60		}
61		match value {
62			Value::Boolean(v) => match &mut self.inner {
63				TypedBuilder::Bool(builder) => builder.append_value(v),
64				_ => unimplemented!(),
65			},
66			Value::Float4(v) => push_or_promote!(native self, v.value(), Float4),
67			Value::Float8(v) => push_or_promote!(native self, v.value(), Float8),
68			Value::Int1(v) => push_or_promote!(native self, v, Int1),
69			Value::Int2(v) => push_or_promote!(native self, v, Int2),
70			Value::Int4(v) => push_or_promote!(native self, v, Int4),
71			Value::Int8(v) => push_or_promote!(native self, v, Int8),
72			Value::Int16(v) => push_or_promote!(native self, v, Int16),
73			Value::Uint1(v) => push_or_promote!(native self, v, Uint1),
74			Value::Uint2(v) => push_or_promote!(native self, v, Uint2),
75			Value::Uint4(v) => push_or_promote!(native self, v, Uint4),
76			Value::Uint8(v) => push_or_promote!(native self, v, Uint8),
77			Value::Uint16(v) => push_or_promote!(native self, v, Uint16),
78			Value::Utf8(v) => push_or_promote!(varlen self, v, Utf8),
79			Value::Date(v) => push_or_promote!(temporal self, v, Date, date_to_native),
80			Value::DateTime(v) => push_or_promote!(temporal self, v, DateTime, datetime_to_native),
81			Value::Time(v) => push_or_promote!(temporal self, v, Time, time_to_native),
82			Value::Duration(v) => push_or_promote!(temporal self, v, Duration, duration_to_native),
83			Value::Uuid4(v) => push_or_promote!(fixed self, v, Uuid4),
84			Value::Uuid7(v) => push_or_promote!(fixed self, v, Uuid7),
85			Value::IdentityId(v) => push_or_promote!(fixed self, v, IdentityId),
86			Value::DictionaryId(v) => match &mut self.inner {
87				TypedBuilder::DictionaryId {
88					builder,
89					..
90				} => append_fixed(builder, &dictionary_array::encode(v)),
91				_ => unimplemented!(),
92			},
93			Value::Blob(v) => push_or_promote!(varlen self, v.as_bytes(), Blob),
94			Value::Decimal(v) => push_or_promote!(decimal self, v, Decimal),
95			Value::None {
96				..
97			} => self.push_none(),
98			Value::Type(t) => self.push_value(Value::Any(Box::new(Value::Type(t)))),
99			Value::List(v) => self.push_value(Value::Any(Box::new(Value::List(v)))),
100			Value::Record(v) => self.push_value(Value::Any(Box::new(Value::Record(v)))),
101			Value::Tuple(v) => self.push_value(Value::Any(Box::new(Value::Tuple(v)))),
102			Value::Any(v) => match &mut self.inner {
103				TypedBuilder::Any {
104					builder,
105					..
106				} => push_any(builder, &v),
107				_ => unreachable!("Cannot push Any value to non-Any column"),
108			},
109			Value::Digest(digest) => match &mut self.inner {
110				TypedBuilder::Digest {
111					builder,
112					inner,
113					accuracy,
114				} => {
115					if digest.inner() != inner || digest.accuracy() != *accuracy {
116						panic!(
117							"cannot push a Digest({}, {}) into a Digest({inner}, {accuracy}) column",
118							digest.inner(),
119							digest.accuracy()
120						);
121					}
122					push_digest(builder, &digest);
123				}
124				_ => unimplemented!(),
125			},
126		}
127	}
128}
129
130#[cfg(test)]
131#[allow(clippy::approx_constant)]
132pub mod tests {
133	use arrow_array::{Array, ArrayRef};
134	use arrow_schema::FieldRef;
135	use reifydb_runtime::context::{
136		clock::{Clock, MockClock},
137		rng::Rng,
138	};
139	use reifydb_value::value::{
140		Value,
141		column_view::{ColumnView, ViewData},
142		container::{
143			dictionary_array,
144			temporal_array::{dates, datetimes, durations, times},
145			uuid_array::{identity_ids, uuid4s, uuid7s},
146			wide_int_array::wides,
147		},
148		date::Date,
149		datetime::DateTime,
150		dictionary::DictionaryEntryId,
151		duration::Duration,
152		identity::IdentityId,
153		ordered_f32::OrderedF32,
154		ordered_f64::OrderedF64,
155		time::Time,
156		uuid::{Uuid4, Uuid7},
157		value_type::ValueType,
158	};
159
160	use crate::value::column::{builder::ColumnBuilder, factory};
161
162	fn builder_of(column: (FieldRef, ArrayRef)) -> ColumnBuilder {
163		ColumnBuilder::from_view(&ColumnView::try_from(&column).unwrap())
164	}
165
166	fn view(column: &(FieldRef, ArrayRef)) -> ColumnView<'_> {
167		ColumnView::try_from(column).unwrap()
168	}
169
170	fn test_clock_and_rng() -> (MockClock, Clock, Rng) {
171		let mock = MockClock::from_millis(1000);
172		let clock = Clock::Mock(mock.clone());
173		let rng = Rng::seeded(42);
174		(mock, clock, rng)
175	}
176
177	#[test]
178	fn test_bool() {
179		let mut col = builder_of(factory::bool("c", vec![true]));
180		col.push_value(Value::Boolean(false));
181		let col = col.finish("c");
182		let col = view(&col);
183		let ViewData::Bool(container) = col.data else {
184			panic!("Expected Bool");
185		};
186		assert_eq!(container.values().iter().collect::<Vec<_>>(), vec![true, false]);
187	}
188
189	#[test]
190	fn test_undefined_bool() {
191		let mut col = builder_of(factory::bool("c", vec![true]));
192		col.push_value(Value::none());
193		// Pushing none must add a null row and make the bare column optional, never a false.
194		let col = col.finish("c");
195		let col = view(&col);
196		assert_eq!(col.len(), 2);
197		assert!(col.is_defined(0));
198		assert!(!col.is_defined(1));
199	}
200
201	#[test]
202	fn test_push_value_to_none_bool() {
203		let mut col = builder_of(factory::none_typed("c", ValueType::Boolean, 2));
204		col.push_value(Value::Boolean(true));
205		let col = col.finish("c");
206		let col = view(&col);
207		assert_eq!(col.len(), 3);
208		assert!(!col.is_defined(0));
209		assert!(!col.is_defined(1));
210		assert!(col.is_defined(2));
211		assert_eq!(col.get_value(2), Value::Boolean(true));
212	}
213
214	#[test]
215	fn test_float4() {
216		let mut col = builder_of(factory::float4("c", vec![1.0]));
217		col.push_value(Value::Float4(OrderedF32::try_from(2.0).unwrap()));
218		let col = col.finish("c");
219		let col = view(&col);
220		let ViewData::Float4(container) = col.data else {
221			panic!("Expected Float4");
222		};
223		assert_eq!(&container.values()[..], &[1.0, 2.0]);
224	}
225
226	#[test]
227	fn test_undefined_float4() {
228		let mut col = builder_of(factory::float4("c", vec![1.0]));
229		col.push_value(Value::none());
230		let col = col.finish("c");
231		let col = view(&col);
232		assert_eq!(col.len(), 2);
233		assert!(col.is_defined(0));
234		assert!(!col.is_defined(1));
235	}
236
237	#[test]
238	fn test_push_value_to_none_float4() {
239		let mut col = builder_of(factory::none_typed("c", ValueType::Float4, 1));
240		col.push_value(Value::Float4(OrderedF32::try_from(3.14).unwrap()));
241		let col = col.finish("c");
242		let col = view(&col);
243		assert_eq!(col.len(), 2);
244		assert!(!col.is_defined(0));
245		assert!(col.is_defined(1));
246	}
247
248	#[test]
249	fn test_float8() {
250		let mut col = builder_of(factory::float8("c", vec![1.0]));
251		col.push_value(Value::Float8(OrderedF64::try_from(2.0).unwrap()));
252		let col = col.finish("c");
253		let col = view(&col);
254		let ViewData::Float8(container) = col.data else {
255			panic!("Expected Float8");
256		};
257		assert_eq!(&container.values()[..], &[1.0, 2.0]);
258	}
259
260	#[test]
261	fn test_undefined_float8() {
262		let mut col = builder_of(factory::float8("c", vec![1.0]));
263		col.push_value(Value::none());
264		let col = col.finish("c");
265		let col = view(&col);
266		assert_eq!(col.len(), 2);
267		assert!(col.is_defined(0));
268		assert!(!col.is_defined(1));
269	}
270
271	#[test]
272	fn test_push_value_to_none_float8() {
273		let mut col = builder_of(factory::none_typed("c", ValueType::Float8, 1));
274		col.push_value(Value::Float8(OrderedF64::try_from(2.718).unwrap()));
275		let col = col.finish("c");
276		let col = view(&col);
277		assert_eq!(col.len(), 2);
278		assert!(!col.is_defined(0));
279		assert!(col.is_defined(1));
280	}
281
282	#[test]
283	fn test_int1() {
284		let mut col = builder_of(factory::int1("c", vec![1]));
285		col.push_value(Value::Int1(2));
286		let col = col.finish("c");
287		let col = view(&col);
288		let ViewData::Int1(container) = col.data else {
289			panic!("Expected Int1");
290		};
291		assert_eq!(&container.values()[..], &[1, 2]);
292	}
293
294	#[test]
295	fn test_undefined_int1() {
296		let mut col = builder_of(factory::int1("c", vec![1]));
297		col.push_value(Value::none());
298		let col = col.finish("c");
299		let col = view(&col);
300		assert_eq!(col.len(), 2);
301		assert!(col.is_defined(0));
302		assert!(!col.is_defined(1));
303	}
304
305	#[test]
306	fn test_push_value_to_none_int1() {
307		let mut col = builder_of(factory::none_typed("c", ValueType::Int1, 1));
308		col.push_value(Value::Int1(5));
309		let col = col.finish("c");
310		let col = view(&col);
311		assert_eq!(col.len(), 2);
312		assert!(!col.is_defined(0));
313		assert!(col.is_defined(1));
314		assert_eq!(col.get_value(1), Value::Int1(5));
315	}
316
317	#[test]
318	fn test_int2() {
319		let mut col = builder_of(factory::int2("c", vec![1]));
320		col.push_value(Value::Int2(3));
321		let col = col.finish("c");
322		let col = view(&col);
323		let ViewData::Int2(container) = col.data else {
324			panic!("Expected Int2");
325		};
326		assert_eq!(&container.values()[..], &[1, 3]);
327	}
328
329	#[test]
330	fn test_undefined_int2() {
331		let mut col = builder_of(factory::int2("c", vec![1]));
332		col.push_value(Value::none());
333		let col = col.finish("c");
334		let col = view(&col);
335		assert_eq!(col.len(), 2);
336		assert!(col.is_defined(0));
337		assert!(!col.is_defined(1));
338	}
339
340	#[test]
341	fn test_push_value_to_none_int2() {
342		let mut col = builder_of(factory::none_typed("c", ValueType::Int2, 1));
343		col.push_value(Value::Int2(10));
344		let col = col.finish("c");
345		let col = view(&col);
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 = builder_of(factory::int4("c", vec![10]));
355		col.push_value(Value::Int4(20));
356		let col = col.finish("c");
357		let col = view(&col);
358		let ViewData::Int4(container) = col.data else {
359			panic!("Expected Int4");
360		};
361		assert_eq!(&container.values()[..], &[10, 20]);
362	}
363
364	#[test]
365	fn test_undefined_int4() {
366		let mut col = builder_of(factory::int4("c", vec![10]));
367		col.push_value(Value::none());
368		let col = col.finish("c");
369		let col = view(&col);
370		assert_eq!(col.len(), 2);
371		assert!(col.is_defined(0));
372		assert!(!col.is_defined(1));
373	}
374
375	#[test]
376	fn test_push_value_to_none_int4() {
377		let mut col = builder_of(factory::none_typed("c", ValueType::Int4, 1));
378		col.push_value(Value::Int4(20));
379		let col = col.finish("c");
380		let col = view(&col);
381		assert_eq!(col.len(), 2);
382		assert!(!col.is_defined(0));
383		assert!(col.is_defined(1));
384		assert_eq!(col.get_value(1), Value::Int4(20));
385	}
386
387	#[test]
388	fn test_int8() {
389		let mut col = builder_of(factory::int8("c", vec![100]));
390		col.push_value(Value::Int8(200));
391		let col = col.finish("c");
392		let col = view(&col);
393		let ViewData::Int8(container) = col.data else {
394			panic!("Expected Int8");
395		};
396		assert_eq!(&container.values()[..], &[100, 200]);
397	}
398
399	#[test]
400	fn test_undefined_int8() {
401		let mut col = builder_of(factory::int8("c", vec![100]));
402		col.push_value(Value::none());
403		let col = col.finish("c");
404		let col = view(&col);
405		assert_eq!(col.len(), 2);
406		assert!(col.is_defined(0));
407		assert!(!col.is_defined(1));
408	}
409
410	#[test]
411	fn test_push_value_to_none_int8() {
412		let mut col = builder_of(factory::none_typed("c", ValueType::Int8, 1));
413		col.push_value(Value::Int8(30));
414		let col = col.finish("c");
415		let col = view(&col);
416		assert_eq!(col.len(), 2);
417		assert!(!col.is_defined(0));
418		assert!(col.is_defined(1));
419		assert_eq!(col.get_value(1), Value::Int8(30));
420	}
421
422	#[test]
423	fn test_int16() {
424		let mut col = builder_of(factory::int16("c", vec![1000]));
425		col.push_value(Value::Int16(2000));
426		let col = col.finish("c");
427		let col = view(&col);
428		let ViewData::Int16(container) = col.data else {
429			panic!("Expected Int16");
430		};
431		assert_eq!(wides::<i128>(container), [1000, 2000]);
432	}
433
434	#[test]
435	fn test_undefined_int16() {
436		let mut col = builder_of(factory::int16("c", vec![1000]));
437		col.push_value(Value::none());
438		let col = col.finish("c");
439		let col = view(&col);
440		assert_eq!(col.len(), 2);
441		assert!(col.is_defined(0));
442		assert!(!col.is_defined(1));
443	}
444
445	#[test]
446	fn test_push_value_to_none_int16() {
447		let mut col = builder_of(factory::none_typed("c", ValueType::Int16, 1));
448		col.push_value(Value::Int16(40));
449		let col = col.finish("c");
450		let col = view(&col);
451		assert_eq!(col.len(), 2);
452		assert!(!col.is_defined(0));
453		assert!(col.is_defined(1));
454		assert_eq!(col.get_value(1), Value::Int16(40));
455	}
456
457	#[test]
458	fn test_uint1() {
459		let mut col = builder_of(factory::uint1("c", vec![1]));
460		col.push_value(Value::Uint1(2));
461		let col = col.finish("c");
462		let col = view(&col);
463		let ViewData::Uint1(container) = col.data else {
464			panic!("Expected Uint1");
465		};
466		assert_eq!(&container.values()[..], &[1, 2]);
467	}
468
469	#[test]
470	fn test_undefined_uint1() {
471		let mut col = builder_of(factory::uint1("c", vec![1]));
472		col.push_value(Value::none());
473		let col = col.finish("c");
474		let col = view(&col);
475		assert_eq!(col.len(), 2);
476		assert!(col.is_defined(0));
477		assert!(!col.is_defined(1));
478	}
479
480	#[test]
481	fn test_push_value_to_none_uint1() {
482		let mut col = builder_of(factory::none_typed("c", ValueType::Uint1, 1));
483		col.push_value(Value::Uint1(1));
484		let col = col.finish("c");
485		let col = view(&col);
486		assert_eq!(col.len(), 2);
487		assert!(!col.is_defined(0));
488		assert!(col.is_defined(1));
489		assert_eq!(col.get_value(1), Value::Uint1(1));
490	}
491
492	#[test]
493	fn test_uint2() {
494		let mut col = builder_of(factory::uint2("c", vec![10]));
495		col.push_value(Value::Uint2(20));
496		let col = col.finish("c");
497		let col = view(&col);
498		let ViewData::Uint2(container) = col.data else {
499			panic!("Expected Uint2");
500		};
501		assert_eq!(&container.values()[..], &[10, 20]);
502	}
503
504	#[test]
505	fn test_undefined_uint2() {
506		let mut col = builder_of(factory::uint2("c", vec![10]));
507		col.push_value(Value::none());
508		let col = col.finish("c");
509		let col = view(&col);
510		assert_eq!(col.len(), 2);
511		assert!(col.is_defined(0));
512		assert!(!col.is_defined(1));
513	}
514
515	#[test]
516	fn test_push_value_to_none_uint2() {
517		let mut col = builder_of(factory::none_typed("c", ValueType::Uint2, 1));
518		col.push_value(Value::Uint2(2));
519		let col = col.finish("c");
520		let col = view(&col);
521		assert_eq!(col.len(), 2);
522		assert!(!col.is_defined(0));
523		assert!(col.is_defined(1));
524		assert_eq!(col.get_value(1), Value::Uint2(2));
525	}
526
527	#[test]
528	fn test_uint4() {
529		let mut col = builder_of(factory::uint4("c", vec![100]));
530		col.push_value(Value::Uint4(200));
531		let col = col.finish("c");
532		let col = view(&col);
533		let ViewData::Uint4(container) = col.data else {
534			panic!("Expected Uint4");
535		};
536		assert_eq!(&container.values()[..], &[100, 200]);
537	}
538
539	#[test]
540	fn test_undefined_uint4() {
541		let mut col = builder_of(factory::uint4("c", vec![100]));
542		col.push_value(Value::none());
543		let col = col.finish("c");
544		let col = view(&col);
545		assert_eq!(col.len(), 2);
546		assert!(col.is_defined(0));
547		assert!(!col.is_defined(1));
548	}
549
550	#[test]
551	fn test_push_value_to_none_uint4() {
552		let mut col = builder_of(factory::none_typed("c", ValueType::Uint4, 1));
553		col.push_value(Value::Uint4(3));
554		let col = col.finish("c");
555		let col = view(&col);
556		assert_eq!(col.len(), 2);
557		assert!(!col.is_defined(0));
558		assert!(col.is_defined(1));
559		assert_eq!(col.get_value(1), Value::Uint4(3));
560	}
561
562	#[test]
563	fn test_uint8() {
564		let mut col = builder_of(factory::uint8("c", vec![1000]));
565		col.push_value(Value::Uint8(2000));
566		let col = col.finish("c");
567		let col = view(&col);
568		let ViewData::Uint8(container) = col.data else {
569			panic!("Expected Uint8");
570		};
571		assert_eq!(&container.values()[..], &[1000, 2000]);
572	}
573
574	#[test]
575	fn test_undefined_uint8() {
576		let mut col = builder_of(factory::uint8("c", vec![1000]));
577		col.push_value(Value::none());
578		let col = col.finish("c");
579		let col = view(&col);
580		assert_eq!(col.len(), 2);
581		assert!(col.is_defined(0));
582		assert!(!col.is_defined(1));
583	}
584
585	#[test]
586	fn test_push_value_to_none_uint8() {
587		let mut col = builder_of(factory::none_typed("c", ValueType::Uint8, 1));
588		col.push_value(Value::Uint8(4));
589		let col = col.finish("c");
590		let col = view(&col);
591		assert_eq!(col.len(), 2);
592		assert!(!col.is_defined(0));
593		assert!(col.is_defined(1));
594		assert_eq!(col.get_value(1), Value::Uint8(4));
595	}
596
597	#[test]
598	fn test_uint16() {
599		let mut col = builder_of(factory::uint16("c", vec![10000]));
600		col.push_value(Value::Uint16(20000));
601		let col = col.finish("c");
602		let col = view(&col);
603		let ViewData::Uint16(container) = col.data else {
604			panic!("Expected Uint16");
605		};
606		assert_eq!(wides::<u128>(container), [10000, 20000]);
607	}
608
609	#[test]
610	fn test_undefined_uint16() {
611		let mut col = builder_of(factory::uint16("c", vec![10000]));
612		col.push_value(Value::none());
613		let col = col.finish("c");
614		let col = view(&col);
615		assert_eq!(col.len(), 2);
616		assert!(col.is_defined(0));
617		assert!(!col.is_defined(1));
618	}
619
620	#[test]
621	fn test_push_value_to_none_uint16() {
622		let mut col = builder_of(factory::none_typed("c", ValueType::Uint16, 1));
623		col.push_value(Value::Uint16(5));
624		let col = col.finish("c");
625		let col = view(&col);
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::Uint16(5));
630	}
631
632	#[test]
633	fn test_utf8() {
634		let mut col = builder_of(factory::utf8("c", vec!["hello".to_string()]));
635		col.push_value(Value::Utf8("world".to_string()));
636		let col = col.finish("c");
637		let col = view(&col);
638		let ViewData::Utf8 {
639			container,
640			..
641		} = col.data
642		else {
643			panic!("Expected Utf8");
644		};
645		let collected: Vec<&str> = (0..container.len()).map(|i| container.value(i)).collect();
646		assert_eq!(collected, vec!["hello", "world"]);
647	}
648
649	#[test]
650	fn test_undefined_utf8() {
651		let mut col = builder_of(factory::utf8("c", vec!["hello".to_string()]));
652		col.push_value(Value::none());
653		let col = col.finish("c");
654		let col = view(&col);
655		assert_eq!(col.len(), 2);
656		assert!(col.is_defined(0));
657		assert!(!col.is_defined(1));
658	}
659
660	#[test]
661	fn test_push_value_to_none_utf8() {
662		let mut col = builder_of(factory::none_typed("c", ValueType::Utf8, 1));
663		col.push_value(Value::Utf8("ok".to_string()));
664		let col = col.finish("c");
665		let col = view(&col);
666		assert_eq!(col.len(), 2);
667		assert!(!col.is_defined(0));
668		assert!(col.is_defined(1));
669		assert_eq!(col.get_value(1), Value::Utf8("ok".to_string()));
670	}
671
672	#[test]
673	fn test_undefined() {
674		let mut col = builder_of(factory::int2("c", vec![1]));
675		col.push_value(Value::none());
676		let col = col.finish("c");
677		let col = view(&col);
678		assert_eq!(col.len(), 2);
679		assert!(col.is_defined(0));
680		assert!(!col.is_defined(1));
681	}
682
683	#[test]
684	fn test_date() {
685		let date1 = Date::from_ymd(2023, 1, 1).unwrap();
686		let date2 = Date::from_ymd(2023, 12, 31).unwrap();
687		let mut col = builder_of(factory::date("c", vec![date1]));
688		col.push_value(Value::Date(date2));
689		let col = col.finish("c");
690		let col = view(&col);
691		let ViewData::Date(container) = col.data else {
692			panic!("Expected Date");
693		};
694		assert_eq!(dates(container), &[date1, date2]);
695	}
696
697	#[test]
698	fn test_undefined_date() {
699		let date1 = Date::from_ymd(2023, 1, 1).unwrap();
700		let mut col = builder_of(factory::date("c", vec![date1]));
701		col.push_value(Value::none());
702		let col = col.finish("c");
703		let col = view(&col);
704		assert_eq!(col.len(), 2);
705		assert!(col.is_defined(0));
706		assert!(!col.is_defined(1));
707	}
708
709	#[test]
710	fn test_push_value_to_none_date() {
711		let date = Date::from_ymd(2023, 6, 15).unwrap();
712		let mut col = builder_of(factory::none_typed("c", ValueType::Date, 1));
713		col.push_value(Value::Date(date));
714		let col = col.finish("c");
715		let col = view(&col);
716		assert_eq!(col.len(), 2);
717		assert!(!col.is_defined(0));
718		assert!(col.is_defined(1));
719		assert_eq!(col.get_value(1), Value::Date(date));
720	}
721
722	#[test]
723	fn test_datetime() {
724		let dt1 = DateTime::from_epoch_secs(1672531200).unwrap(); // 2023-01-01 00:00:00 SVTC
725		let dt2 = DateTime::from_epoch_secs(1704067200).unwrap(); // 2024-01-01 00:00:00 SVTC
726		let mut col = builder_of(factory::datetime("c", vec![dt1]));
727		col.push_value(Value::DateTime(dt2));
728		let col = col.finish("c");
729		let col = view(&col);
730		let ViewData::DateTime(container) = col.data else {
731			panic!("Expected DateTime");
732		};
733		assert_eq!(datetimes(container), &[dt1, dt2]);
734	}
735
736	#[test]
737	fn test_undefined_datetime() {
738		let dt1 = DateTime::from_epoch_secs(1672531200).unwrap();
739		let mut col = builder_of(factory::datetime("c", vec![dt1]));
740		col.push_value(Value::none());
741		let col = col.finish("c");
742		let col = view(&col);
743		assert_eq!(col.len(), 2);
744		assert!(col.is_defined(0));
745		assert!(!col.is_defined(1));
746	}
747
748	#[test]
749	fn test_push_value_to_none_datetime() {
750		let dt = DateTime::from_epoch_secs(1672531200).unwrap();
751		let mut col = builder_of(factory::none_typed("c", ValueType::DateTime, 1));
752		col.push_value(Value::DateTime(dt));
753		let col = col.finish("c");
754		let col = view(&col);
755		assert_eq!(col.len(), 2);
756		assert!(!col.is_defined(0));
757		assert!(col.is_defined(1));
758		assert_eq!(col.get_value(1), Value::DateTime(dt));
759	}
760
761	#[test]
762	fn test_time() {
763		let time1 = Time::from_hms(12, 30, 0).unwrap();
764		let time2 = Time::from_hms(18, 45, 30).unwrap();
765		let mut col = builder_of(factory::time("c", vec![time1]));
766		col.push_value(Value::Time(time2));
767		let col = col.finish("c");
768		let col = view(&col);
769		let ViewData::Time(container) = col.data else {
770			panic!("Expected Time");
771		};
772		assert_eq!(times(container), &[time1, time2]);
773	}
774
775	#[test]
776	fn test_undefined_time() {
777		let time1 = Time::from_hms(12, 30, 0).unwrap();
778		let mut col = builder_of(factory::time("c", vec![time1]));
779		col.push_value(Value::none());
780		let col = col.finish("c");
781		let col = view(&col);
782		assert_eq!(col.len(), 2);
783		assert!(col.is_defined(0));
784		assert!(!col.is_defined(1));
785	}
786
787	#[test]
788	fn test_push_value_to_none_time() {
789		let time = Time::from_hms(15, 20, 10).unwrap();
790		let mut col = builder_of(factory::none_typed("c", ValueType::Time, 1));
791		col.push_value(Value::Time(time));
792		let col = col.finish("c");
793		let col = view(&col);
794		assert_eq!(col.len(), 2);
795		assert!(!col.is_defined(0));
796		assert!(col.is_defined(1));
797		assert_eq!(col.get_value(1), Value::Time(time));
798	}
799
800	#[test]
801	fn test_duration() {
802		let duration1 = Duration::from_days(30).unwrap();
803		let duration2 = Duration::from_hours(24).unwrap();
804		let mut col = builder_of(factory::duration("c", vec![duration1]));
805		col.push_value(Value::Duration(duration2));
806		let col = col.finish("c");
807		let col = view(&col);
808		let ViewData::Duration(container) = col.data else {
809			panic!("Expected Duration");
810		};
811		assert_eq!(durations(container), &[duration1, duration2]);
812	}
813
814	#[test]
815	fn test_undefined_duration() {
816		let duration1 = Duration::from_days(30).unwrap();
817		let mut col = builder_of(factory::duration("c", vec![duration1]));
818		col.push_value(Value::none());
819		let col = col.finish("c");
820		let col = view(&col);
821		assert_eq!(col.len(), 2);
822		assert!(col.is_defined(0));
823		assert!(!col.is_defined(1));
824	}
825
826	#[test]
827	fn test_push_value_to_none_duration() {
828		let duration = Duration::from_minutes(90).unwrap();
829		let mut col = builder_of(factory::none_typed("c", ValueType::Duration, 1));
830		col.push_value(Value::Duration(duration));
831		let col = col.finish("c");
832		let col = view(&col);
833		assert_eq!(col.len(), 2);
834		assert!(!col.is_defined(0));
835		assert!(col.is_defined(1));
836		assert_eq!(col.get_value(1), Value::Duration(duration));
837	}
838
839	#[test]
840	fn test_identity_id() {
841		let (mock, clock, rng) = test_clock_and_rng();
842		let id1 = IdentityId::generate(&clock, &rng);
843		mock.advance_millis(1);
844		let id2 = IdentityId::generate(&clock, &rng);
845		let mut col = builder_of(factory::identity_id("c", vec![id1]));
846		col.push_value(Value::IdentityId(id2));
847		let col = col.finish("c");
848		let col = view(&col);
849		let ViewData::IdentityId(container) = col.data else {
850			panic!("Expected IdentityId");
851		};
852		assert_eq!(identity_ids(container), &[id1, id2]);
853	}
854
855	#[test]
856	fn test_undefined_identity_id() {
857		let (_, clock, rng) = test_clock_and_rng();
858		let id1 = IdentityId::generate(&clock, &rng);
859		let mut col = builder_of(factory::identity_id("c", vec![id1]));
860		col.push_value(Value::none());
861		let col = col.finish("c");
862		let col = view(&col);
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_identity_id() {
870		let (_, clock, rng) = test_clock_and_rng();
871		let id = IdentityId::generate(&clock, &rng);
872		let mut col = builder_of(factory::none_typed("c", ValueType::IdentityId, 1));
873		col.push_value(Value::IdentityId(id));
874		let col = col.finish("c");
875		let col = view(&col);
876		assert_eq!(col.len(), 2);
877		assert!(!col.is_defined(0));
878		assert!(col.is_defined(1));
879		assert_eq!(col.get_value(1), Value::IdentityId(id));
880	}
881
882	#[test]
883	fn test_uuid4() {
884		let uuid1 = Uuid4::generate();
885		let uuid2 = Uuid4::generate();
886		let mut col = builder_of(factory::uuid4("c", vec![uuid1]));
887		col.push_value(Value::Uuid4(uuid2));
888		let col = col.finish("c");
889		let col = view(&col);
890		let ViewData::Uuid4(container) = col.data else {
891			panic!("Expected Uuid4");
892		};
893		assert_eq!(uuid4s(container), &[uuid1, uuid2]);
894	}
895
896	#[test]
897	fn test_undefined_uuid4() {
898		let uuid1 = Uuid4::generate();
899		let mut col = builder_of(factory::uuid4("c", vec![uuid1]));
900		col.push_value(Value::none());
901		let col = col.finish("c");
902		let col = view(&col);
903		assert_eq!(col.len(), 2);
904		assert!(col.is_defined(0));
905		assert!(!col.is_defined(1));
906	}
907
908	#[test]
909	fn test_push_value_to_none_uuid4() {
910		let uuid = Uuid4::generate();
911		let mut col = builder_of(factory::none_typed("c", ValueType::Uuid4, 1));
912		col.push_value(Value::Uuid4(uuid));
913		let col = col.finish("c");
914		let col = view(&col);
915		assert_eq!(col.len(), 2);
916		assert!(!col.is_defined(0));
917		assert!(col.is_defined(1));
918		assert_eq!(col.get_value(1), Value::Uuid4(uuid));
919	}
920
921	#[test]
922	fn test_uuid7() {
923		let (mock, clock, rng) = test_clock_and_rng();
924		let uuid1 = Uuid7::generate(&clock, &rng);
925		mock.advance_millis(1);
926		let uuid2 = Uuid7::generate(&clock, &rng);
927		let mut col = builder_of(factory::uuid7("c", vec![uuid1]));
928		col.push_value(Value::Uuid7(uuid2));
929		let col = col.finish("c");
930		let col = view(&col);
931		let ViewData::Uuid7(container) = col.data else {
932			panic!("Expected Uuid7");
933		};
934		assert_eq!(uuid7s(container), &[uuid1, uuid2]);
935	}
936
937	#[test]
938	fn test_undefined_uuid7() {
939		let (_, clock, rng) = test_clock_and_rng();
940		let uuid1 = Uuid7::generate(&clock, &rng);
941		let mut col = builder_of(factory::uuid7("c", vec![uuid1]));
942		col.push_value(Value::none());
943		let col = col.finish("c");
944		let col = view(&col);
945		assert_eq!(col.len(), 2);
946		assert!(col.is_defined(0));
947		assert!(!col.is_defined(1));
948	}
949
950	#[test]
951	fn test_push_value_to_none_uuid7() {
952		let (_, clock, rng) = test_clock_and_rng();
953		let uuid = Uuid7::generate(&clock, &rng);
954		let mut col = builder_of(factory::none_typed("c", ValueType::Uuid7, 1));
955		col.push_value(Value::Uuid7(uuid));
956		let col = col.finish("c");
957		let col = view(&col);
958		assert_eq!(col.len(), 2);
959		assert!(!col.is_defined(0));
960		assert!(col.is_defined(1));
961		assert_eq!(col.get_value(1), Value::Uuid7(uuid));
962	}
963
964	#[test]
965	fn test_dictionary_id() {
966		let e1 = DictionaryEntryId::U4(10);
967		let e2 = DictionaryEntryId::U4(20);
968		let mut col = builder_of(factory::dictionary_id("c", vec![e1]));
969		col.push_value(Value::DictionaryId(e2));
970		let col = col.finish("c");
971		let col = view(&col);
972		let ViewData::DictionaryId {
973			container,
974			..
975		} = col.data
976		else {
977			panic!("Expected DictionaryId");
978		};
979		assert_eq!(dictionary_array::iter(container).collect::<Vec<_>>(), &[e1, e2]);
980	}
981
982	#[test]
983	fn test_undefined_dictionary_id() {
984		let e1 = DictionaryEntryId::U4(10);
985		let mut col = builder_of(factory::dictionary_id("c", vec![e1]));
986		col.push_value(Value::none());
987		let col = col.finish("c");
988		let col = view(&col);
989		assert_eq!(col.len(), 2);
990		assert!(col.is_defined(0));
991		assert!(!col.is_defined(1));
992	}
993
994	#[test]
995	fn test_push_value_to_none_dictionary_id() {
996		let e = DictionaryEntryId::U4(42);
997		let mut col = builder_of(factory::none_typed("c", ValueType::DictionaryId, 1));
998		col.push_value(Value::DictionaryId(e));
999		let col = col.finish("c");
1000		let col = view(&col);
1001		assert_eq!(col.len(), 2);
1002		assert!(!col.is_defined(0));
1003		assert!(col.is_defined(1));
1004		assert_eq!(col.get_value(1), Value::DictionaryId(e));
1005	}
1006
1007	#[test]
1008	fn test_push_value_to_none_list() {
1009		// A list arriving after only none rows must widen the column like a record does, never hit an
1010		// unreachable arm.
1011		let list = Value::List(vec![Value::Int4(1), Value::Int4(2)]);
1012		let mut col = ColumnBuilder::with_capacity(ValueType::Option(Box::new(ValueType::Any)), 2);
1013		col.push_none();
1014		col.push_value(list.clone());
1015		let col = col.finish("c");
1016		let col = view(&col);
1017		assert_eq!(col.len(), 2);
1018		assert!(!col.is_defined(0));
1019		assert!(col.is_defined(1));
1020		assert_eq!(col.get_value(1), Value::Any(Box::new(list)));
1021	}
1022
1023	#[test]
1024	fn test_push_value_to_none_type() {
1025		// A type value arriving after only none rows must widen the column like a record does, never hit an
1026		// unreachable arm.
1027		let mut col = ColumnBuilder::with_capacity(ValueType::Option(Box::new(ValueType::Any)), 2);
1028		col.push_none();
1029		col.push_value(Value::Type(ValueType::Int4));
1030		let col = col.finish("c");
1031		let col = view(&col);
1032		assert_eq!(col.len(), 2);
1033		assert!(!col.is_defined(0));
1034		assert!(col.is_defined(1));
1035		assert_eq!(col.get_value(1), Value::Any(Box::new(Value::Type(ValueType::Int4))));
1036	}
1037}