Skip to main content

reifydb_core/value/column/buffer/
factory.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2// Copyright (c) 2026 ReifyDB
3
4use reifydb_value::{
5	util::bitvec::BitVec,
6	value::{
7		Value,
8		blob::Blob,
9		constraint::{bytes::MaxBytes, precision::Precision, scale::Scale},
10		container::{
11			any::AnyContainer, blob::BlobContainer, bool::BoolContainer, dictionary::DictionaryContainer,
12			identity_id::IdentityIdContainer, number::NumberContainer, temporal::TemporalContainer,
13			utf8::Utf8Container, uuid::UuidContainer,
14		},
15		date::Date,
16		datetime::DateTime,
17		decimal::Decimal,
18		dictionary::DictionaryEntryId,
19		duration::Duration,
20		identity::IdentityId,
21		int::Int,
22		time::Time,
23		uint::Uint,
24		uuid::{Uuid4, Uuid7},
25		value_type::ValueType,
26	},
27};
28
29use crate::value::column::ColumnBuffer;
30
31macro_rules! impl_number_factory {
32	($name:ident, $name_opt:ident, $name_cap:ident, $name_bv:ident, $variant:ident, $t:ty, $default:expr) => {
33		pub fn $name(data: impl IntoIterator<Item = $t>) -> Self {
34			let data = data.into_iter().collect::<Vec<_>>();
35			ColumnBuffer::$variant(NumberContainer::from_vec(data))
36		}
37
38		pub fn $name_opt(data: impl IntoIterator<Item = Option<$t>>) -> Self {
39			let mut values = Vec::new();
40			let mut bitvec = Vec::new();
41			let mut has_none = false;
42			for opt in data {
43				match opt {
44					Some(value) => {
45						values.push(value);
46						bitvec.push(true);
47					}
48					None => {
49						values.push($default);
50						bitvec.push(false);
51						has_none = true;
52					}
53				}
54			}
55			let inner = ColumnBuffer::$variant(NumberContainer::from_vec(values));
56			if has_none {
57				ColumnBuffer::Option {
58					inner: Box::new(inner),
59					bitvec: BitVec::from(bitvec),
60				}
61			} else {
62				inner
63			}
64		}
65
66		pub fn $name_cap(capacity: usize) -> Self {
67			ColumnBuffer::$variant(NumberContainer::with_capacity(capacity))
68		}
69
70		pub fn $name_bv(data: impl IntoIterator<Item = $t>, bitvec: impl Into<BitVec>) -> Self {
71			let data = data.into_iter().collect::<Vec<_>>();
72			let bitvec = bitvec.into();
73			assert_eq!(bitvec.len(), data.len());
74			let inner = ColumnBuffer::$variant(NumberContainer::from_vec(data));
75			if bitvec.all_ones() {
76				inner
77			} else {
78				ColumnBuffer::Option {
79					inner: Box::new(inner),
80					bitvec,
81				}
82			}
83		}
84	};
85}
86
87macro_rules! impl_temporal_factory {
88	($name:ident, $name_opt:ident, $name_cap:ident, $name_bv:ident, $variant:ident, $t:ty) => {
89		pub fn $name(data: impl IntoIterator<Item = $t>) -> Self {
90			let data = data.into_iter().collect::<Vec<_>>();
91			ColumnBuffer::$variant(TemporalContainer::from_vec(data))
92		}
93
94		pub fn $name_opt(data: impl IntoIterator<Item = Option<$t>>) -> Self {
95			let mut values = Vec::new();
96			let mut bitvec = Vec::new();
97			let mut has_none = false;
98			for opt in data {
99				match opt {
100					Some(value) => {
101						values.push(value);
102						bitvec.push(true);
103					}
104					None => {
105						values.push(<$t>::default());
106						bitvec.push(false);
107						has_none = true;
108					}
109				}
110			}
111			let inner = ColumnBuffer::$variant(TemporalContainer::from_vec(values));
112			if has_none {
113				ColumnBuffer::Option {
114					inner: Box::new(inner),
115					bitvec: BitVec::from(bitvec),
116				}
117			} else {
118				inner
119			}
120		}
121
122		pub fn $name_cap(capacity: usize) -> Self {
123			ColumnBuffer::$variant(TemporalContainer::with_capacity(capacity))
124		}
125
126		pub fn $name_bv(data: impl IntoIterator<Item = $t>, bitvec: impl Into<BitVec>) -> Self {
127			let data = data.into_iter().collect::<Vec<_>>();
128			let bitvec = bitvec.into();
129			assert_eq!(bitvec.len(), data.len());
130			let inner = ColumnBuffer::$variant(TemporalContainer::from_vec(data));
131			if bitvec.all_ones() {
132				inner
133			} else {
134				ColumnBuffer::Option {
135					inner: Box::new(inner),
136					bitvec,
137				}
138			}
139		}
140	};
141}
142
143macro_rules! impl_uuid_factory {
144	($name:ident, $name_opt:ident, $name_cap:ident, $name_bv:ident, $variant:ident, $t:ty) => {
145		pub fn $name(data: impl IntoIterator<Item = $t>) -> Self {
146			let data = data.into_iter().collect::<Vec<_>>();
147			ColumnBuffer::$variant(UuidContainer::from_vec(data))
148		}
149
150		pub fn $name_opt(data: impl IntoIterator<Item = Option<$t>>) -> Self {
151			let mut values = Vec::new();
152			let mut bitvec = Vec::new();
153			let mut has_none = false;
154			for opt in data {
155				match opt {
156					Some(value) => {
157						values.push(value);
158						bitvec.push(true);
159					}
160					None => {
161						values.push(<$t>::default());
162						bitvec.push(false);
163						has_none = true;
164					}
165				}
166			}
167			let inner = ColumnBuffer::$variant(UuidContainer::from_vec(values));
168			if has_none {
169				ColumnBuffer::Option {
170					inner: Box::new(inner),
171					bitvec: BitVec::from(bitvec),
172				}
173			} else {
174				inner
175			}
176		}
177
178		pub fn $name_cap(capacity: usize) -> Self {
179			ColumnBuffer::$variant(UuidContainer::with_capacity(capacity))
180		}
181
182		pub fn $name_bv(data: impl IntoIterator<Item = $t>, bitvec: impl Into<BitVec>) -> Self {
183			let data = data.into_iter().collect::<Vec<_>>();
184			let bitvec = bitvec.into();
185			assert_eq!(bitvec.len(), data.len());
186			let inner = ColumnBuffer::$variant(UuidContainer::from_vec(data));
187			if bitvec.all_ones() {
188				inner
189			} else {
190				ColumnBuffer::Option {
191					inner: Box::new(inner),
192					bitvec,
193				}
194			}
195		}
196	};
197}
198
199impl ColumnBuffer {
200	pub fn bool(data: impl IntoIterator<Item = bool>) -> Self {
201		let data = data.into_iter().collect::<Vec<_>>();
202		ColumnBuffer::Bool(BoolContainer::from_vec(data))
203	}
204
205	pub fn bool_optional(data: impl IntoIterator<Item = Option<bool>>) -> Self {
206		let mut values = Vec::new();
207		let mut bitvec = Vec::new();
208		let mut has_none = false;
209
210		for opt in data {
211			match opt {
212				Some(value) => {
213					values.push(value);
214					bitvec.push(true);
215				}
216				None => {
217					values.push(false);
218					bitvec.push(false);
219					has_none = true;
220				}
221			}
222		}
223
224		let inner = ColumnBuffer::Bool(BoolContainer::from_vec(values));
225		if has_none {
226			ColumnBuffer::Option {
227				inner: Box::new(inner),
228				bitvec: BitVec::from(bitvec),
229			}
230		} else {
231			inner
232		}
233	}
234
235	pub fn bool_with_capacity(capacity: usize) -> Self {
236		ColumnBuffer::Bool(BoolContainer::with_capacity(capacity))
237	}
238
239	pub fn bool_with_bitvec(data: impl IntoIterator<Item = bool>, bitvec: impl Into<BitVec>) -> Self {
240		let data = data.into_iter().collect::<Vec<_>>();
241		let bitvec = bitvec.into();
242		assert_eq!(bitvec.len(), data.len());
243		let inner = ColumnBuffer::Bool(BoolContainer::from_vec(data));
244		if bitvec.all_ones() {
245			inner
246		} else {
247			ColumnBuffer::Option {
248				inner: Box::new(inner),
249				bitvec,
250			}
251		}
252	}
253
254	impl_number_factory!(float4, float4_optional, float4_with_capacity, float4_with_bitvec, Float4, f32, 0.0);
255	impl_number_factory!(float8, float8_optional, float8_with_capacity, float8_with_bitvec, Float8, f64, 0.0);
256	impl_number_factory!(int1, int1_optional, int1_with_capacity, int1_with_bitvec, Int1, i8, 0);
257	impl_number_factory!(int2, int2_optional, int2_with_capacity, int2_with_bitvec, Int2, i16, 0);
258	impl_number_factory!(int4, int4_optional, int4_with_capacity, int4_with_bitvec, Int4, i32, 0);
259	impl_number_factory!(int8, int8_optional, int8_with_capacity, int8_with_bitvec, Int8, i64, 0);
260	impl_number_factory!(int16, int16_optional, int16_with_capacity, int16_with_bitvec, Int16, i128, 0);
261	impl_number_factory!(uint1, uint1_optional, uint1_with_capacity, uint1_with_bitvec, Uint1, u8, 0);
262	impl_number_factory!(uint2, uint2_optional, uint2_with_capacity, uint2_with_bitvec, Uint2, u16, 0);
263	impl_number_factory!(uint4, uint4_optional, uint4_with_capacity, uint4_with_bitvec, Uint4, u32, 0);
264	impl_number_factory!(uint8, uint8_optional, uint8_with_capacity, uint8_with_bitvec, Uint8, u64, 0);
265	impl_number_factory!(uint16, uint16_optional, uint16_with_capacity, uint16_with_bitvec, Uint16, u128, 0);
266
267	pub fn utf8(data: impl IntoIterator<Item = impl Into<String>>) -> Self {
268		let data = data.into_iter().map(|c| c.into()).collect::<Vec<_>>();
269		ColumnBuffer::Utf8 {
270			container: Utf8Container::from_vec(data),
271			max_bytes: MaxBytes::MAX,
272		}
273	}
274
275	pub fn utf8_repeated(value: &str, count: usize) -> Self {
276		ColumnBuffer::Utf8 {
277			container: Utf8Container::from_repeated_str(value, count),
278			max_bytes: MaxBytes::MAX,
279		}
280	}
281
282	pub fn utf8_optional(data: impl IntoIterator<Item = Option<String>>) -> Self {
283		let mut values = Vec::new();
284		let mut bitvec = Vec::new();
285		let mut has_none = false;
286
287		for opt in data {
288			match opt {
289				Some(value) => {
290					values.push(value);
291					bitvec.push(true);
292				}
293				None => {
294					values.push(String::new());
295					bitvec.push(false);
296					has_none = true;
297				}
298			}
299		}
300
301		let inner = ColumnBuffer::Utf8 {
302			container: Utf8Container::from_vec(values),
303			max_bytes: MaxBytes::MAX,
304		};
305		if has_none {
306			ColumnBuffer::Option {
307				inner: Box::new(inner),
308				bitvec: BitVec::from(bitvec),
309			}
310		} else {
311			inner
312		}
313	}
314
315	pub fn utf8_with_capacity(capacity: usize) -> Self {
316		ColumnBuffer::Utf8 {
317			container: Utf8Container::with_capacity(capacity),
318			max_bytes: MaxBytes::MAX,
319		}
320	}
321
322	pub fn utf8_with_bitvec(data: impl IntoIterator<Item = impl Into<String>>, bitvec: impl Into<BitVec>) -> Self {
323		let data = data.into_iter().map(Into::into).collect::<Vec<_>>();
324		let bitvec = bitvec.into();
325		assert_eq!(bitvec.len(), data.len());
326		let inner = ColumnBuffer::Utf8 {
327			container: Utf8Container::from_vec(data),
328			max_bytes: MaxBytes::MAX,
329		};
330		if bitvec.all_ones() {
331			inner
332		} else {
333			ColumnBuffer::Option {
334				inner: Box::new(inner),
335				bitvec,
336			}
337		}
338	}
339
340	impl_temporal_factory!(date, date_optional, date_with_capacity, date_with_bitvec, Date, Date);
341	impl_temporal_factory!(
342		datetime,
343		datetime_optional,
344		datetime_with_capacity,
345		datetime_with_bitvec,
346		DateTime,
347		DateTime
348	);
349	impl_temporal_factory!(time, time_optional, time_with_capacity, time_with_bitvec, Time, Time);
350	impl_temporal_factory!(
351		duration,
352		duration_optional,
353		duration_with_capacity,
354		duration_with_bitvec,
355		Duration,
356		Duration
357	);
358
359	impl_uuid_factory!(uuid4, uuid4_optional, uuid4_with_capacity, uuid4_with_bitvec, Uuid4, Uuid4);
360	impl_uuid_factory!(uuid7, uuid7_optional, uuid7_with_capacity, uuid7_with_bitvec, Uuid7, Uuid7);
361
362	pub fn blob(data: impl IntoIterator<Item = Blob>) -> Self {
363		let data = data.into_iter().collect::<Vec<_>>();
364		ColumnBuffer::Blob {
365			container: BlobContainer::from_vec(data),
366			max_bytes: MaxBytes::MAX,
367		}
368	}
369
370	pub fn blob_optional(data: impl IntoIterator<Item = Option<Blob>>) -> Self {
371		let mut values = Vec::new();
372		let mut bitvec = Vec::new();
373		let mut has_none = false;
374
375		for opt in data {
376			match opt {
377				Some(value) => {
378					values.push(value);
379					bitvec.push(true);
380				}
381				None => {
382					values.push(Blob::default());
383					bitvec.push(false);
384					has_none = true;
385				}
386			}
387		}
388
389		let inner = ColumnBuffer::Blob {
390			container: BlobContainer::from_vec(values),
391			max_bytes: MaxBytes::MAX,
392		};
393		if has_none {
394			ColumnBuffer::Option {
395				inner: Box::new(inner),
396				bitvec: BitVec::from(bitvec),
397			}
398		} else {
399			inner
400		}
401	}
402
403	pub fn blob_with_capacity(capacity: usize) -> Self {
404		ColumnBuffer::Blob {
405			container: BlobContainer::with_capacity(capacity),
406			max_bytes: MaxBytes::MAX,
407		}
408	}
409
410	pub fn blob_with_bitvec(data: impl IntoIterator<Item = Blob>, bitvec: impl Into<BitVec>) -> Self {
411		let data = data.into_iter().collect::<Vec<_>>();
412		let bitvec = bitvec.into();
413		assert_eq!(bitvec.len(), data.len());
414		let inner = ColumnBuffer::Blob {
415			container: BlobContainer::from_vec(data),
416			max_bytes: MaxBytes::MAX,
417		};
418		if bitvec.all_ones() {
419			inner
420		} else {
421			ColumnBuffer::Option {
422				inner: Box::new(inner),
423				bitvec,
424			}
425		}
426	}
427
428	pub fn identity_id(identity_ids: impl IntoIterator<Item = IdentityId>) -> Self {
429		let data = identity_ids.into_iter().collect::<Vec<_>>();
430		ColumnBuffer::IdentityId(IdentityIdContainer::from_vec(data))
431	}
432
433	pub fn identity_id_optional(identity_ids: impl IntoIterator<Item = Option<IdentityId>>) -> Self {
434		let mut values = Vec::new();
435		let mut bitvec = Vec::new();
436		let mut has_none = false;
437
438		for opt in identity_ids {
439			match opt {
440				Some(value) => {
441					values.push(value);
442					bitvec.push(true);
443				}
444				None => {
445					values.push(IdentityId::default());
446					bitvec.push(false);
447					has_none = true;
448				}
449			}
450		}
451
452		let inner = ColumnBuffer::IdentityId(IdentityIdContainer::from_vec(values));
453		if has_none {
454			ColumnBuffer::Option {
455				inner: Box::new(inner),
456				bitvec: BitVec::from(bitvec),
457			}
458		} else {
459			inner
460		}
461	}
462
463	pub fn identity_id_with_capacity(capacity: usize) -> Self {
464		ColumnBuffer::IdentityId(IdentityIdContainer::with_capacity(capacity))
465	}
466
467	pub fn identity_id_with_bitvec(
468		identity_ids: impl IntoIterator<Item = IdentityId>,
469		bitvec: impl Into<BitVec>,
470	) -> Self {
471		let data = identity_ids.into_iter().collect::<Vec<_>>();
472		let bitvec = bitvec.into();
473		assert_eq!(bitvec.len(), data.len());
474		let inner = ColumnBuffer::IdentityId(IdentityIdContainer::from_vec(data));
475		if bitvec.all_ones() {
476			inner
477		} else {
478			ColumnBuffer::Option {
479				inner: Box::new(inner),
480				bitvec,
481			}
482		}
483	}
484
485	pub fn int(data: impl IntoIterator<Item = Int>) -> Self {
486		let data = data.into_iter().collect::<Vec<_>>();
487		ColumnBuffer::Int {
488			container: NumberContainer::from_vec(data),
489			max_bytes: MaxBytes::MAX,
490		}
491	}
492
493	pub fn int_optional(data: impl IntoIterator<Item = Option<Int>>) -> Self {
494		let mut values = Vec::new();
495		let mut bitvec = Vec::new();
496		let mut has_none = false;
497
498		for opt in data {
499			match opt {
500				Some(value) => {
501					values.push(value);
502					bitvec.push(true);
503				}
504				None => {
505					values.push(Int::default());
506					bitvec.push(false);
507					has_none = true;
508				}
509			}
510		}
511
512		let inner = ColumnBuffer::Int {
513			container: NumberContainer::from_vec(values),
514			max_bytes: MaxBytes::MAX,
515		};
516		if has_none {
517			ColumnBuffer::Option {
518				inner: Box::new(inner),
519				bitvec: BitVec::from(bitvec),
520			}
521		} else {
522			inner
523		}
524	}
525
526	pub fn uint(data: impl IntoIterator<Item = Uint>) -> Self {
527		let data = data.into_iter().collect::<Vec<_>>();
528		ColumnBuffer::Uint {
529			container: NumberContainer::from_vec(data),
530			max_bytes: MaxBytes::MAX,
531		}
532	}
533
534	pub fn uint_optional(data: impl IntoIterator<Item = Option<Uint>>) -> Self {
535		let mut values = Vec::new();
536		let mut bitvec = Vec::new();
537		let mut has_none = false;
538
539		for opt in data {
540			match opt {
541				Some(value) => {
542					values.push(value);
543					bitvec.push(true);
544				}
545				None => {
546					values.push(Uint::default());
547					bitvec.push(false);
548					has_none = true;
549				}
550			}
551		}
552
553		let inner = ColumnBuffer::Uint {
554			container: NumberContainer::from_vec(values),
555			max_bytes: MaxBytes::MAX,
556		};
557		if has_none {
558			ColumnBuffer::Option {
559				inner: Box::new(inner),
560				bitvec: BitVec::from(bitvec),
561			}
562		} else {
563			inner
564		}
565	}
566
567	pub fn int_with_capacity(capacity: usize) -> Self {
568		ColumnBuffer::Int {
569			container: NumberContainer::with_capacity(capacity),
570			max_bytes: MaxBytes::MAX,
571		}
572	}
573
574	pub fn uint_with_capacity(capacity: usize) -> Self {
575		ColumnBuffer::Uint {
576			container: NumberContainer::with_capacity(capacity),
577			max_bytes: MaxBytes::MAX,
578		}
579	}
580
581	pub fn int_with_bitvec(data: impl IntoIterator<Item = Int>, bitvec: impl Into<BitVec>) -> Self {
582		let data = data.into_iter().collect::<Vec<_>>();
583		let bitvec = bitvec.into();
584		assert_eq!(bitvec.len(), data.len());
585		let inner = ColumnBuffer::Int {
586			container: NumberContainer::from_vec(data),
587			max_bytes: MaxBytes::MAX,
588		};
589		if bitvec.all_ones() {
590			inner
591		} else {
592			ColumnBuffer::Option {
593				inner: Box::new(inner),
594				bitvec,
595			}
596		}
597	}
598
599	pub fn uint_with_bitvec(data: impl IntoIterator<Item = Uint>, bitvec: impl Into<BitVec>) -> Self {
600		let data = data.into_iter().collect::<Vec<_>>();
601		let bitvec = bitvec.into();
602		assert_eq!(bitvec.len(), data.len());
603		let inner = ColumnBuffer::Uint {
604			container: NumberContainer::from_vec(data),
605			max_bytes: MaxBytes::MAX,
606		};
607		if bitvec.all_ones() {
608			inner
609		} else {
610			ColumnBuffer::Option {
611				inner: Box::new(inner),
612				bitvec,
613			}
614		}
615	}
616
617	pub fn decimal(data: impl IntoIterator<Item = Decimal>) -> Self {
618		let data = data.into_iter().collect::<Vec<_>>();
619		ColumnBuffer::Decimal {
620			container: NumberContainer::from_vec(data),
621			precision: Precision::MAX,
622			scale: Scale::new(0),
623		}
624	}
625
626	pub fn decimal_optional(data: impl IntoIterator<Item = Option<Decimal>>) -> Self {
627		let mut values = Vec::new();
628		let mut bitvec = Vec::new();
629		let mut has_none = false;
630
631		for opt in data {
632			match opt {
633				Some(value) => {
634					values.push(value);
635					bitvec.push(true);
636				}
637				None => {
638					values.push(Decimal::default());
639					bitvec.push(false);
640					has_none = true;
641				}
642			}
643		}
644
645		let inner = ColumnBuffer::Decimal {
646			container: NumberContainer::from_vec(values),
647			precision: Precision::MAX,
648			scale: Scale::new(0),
649		};
650		if has_none {
651			ColumnBuffer::Option {
652				inner: Box::new(inner),
653				bitvec: BitVec::from(bitvec),
654			}
655		} else {
656			inner
657		}
658	}
659
660	pub fn decimal_with_capacity(capacity: usize) -> Self {
661		ColumnBuffer::Decimal {
662			container: NumberContainer::with_capacity(capacity),
663			precision: Precision::MAX,
664			scale: Scale::new(0),
665		}
666	}
667
668	pub fn decimal_with_bitvec(data: impl IntoIterator<Item = Decimal>, bitvec: impl Into<BitVec>) -> Self {
669		let data = data.into_iter().collect::<Vec<_>>();
670		let bitvec = bitvec.into();
671		assert_eq!(bitvec.len(), data.len());
672		let inner = ColumnBuffer::Decimal {
673			container: NumberContainer::from_vec(data),
674			precision: Precision::MAX,
675			scale: Scale::new(0),
676		};
677		if bitvec.all_ones() {
678			inner
679		} else {
680			ColumnBuffer::Option {
681				inner: Box::new(inner),
682				bitvec,
683			}
684		}
685	}
686
687	pub fn any(data: impl IntoIterator<Item = Box<Value>>) -> Self {
688		let data = data.into_iter().collect::<Vec<_>>();
689		ColumnBuffer::Any(AnyContainer::from_vec(data))
690	}
691
692	pub fn any_optional(data: impl IntoIterator<Item = Option<Box<Value>>>) -> Self {
693		let mut values = Vec::new();
694		let mut bitvec = Vec::new();
695		let mut has_none = false;
696
697		for opt in data {
698			match opt {
699				Some(value) => {
700					values.push(value);
701					bitvec.push(true);
702				}
703				None => {
704					values.push(Box::new(Value::none()));
705					bitvec.push(false);
706					has_none = true;
707				}
708			}
709		}
710
711		let inner = ColumnBuffer::Any(AnyContainer::from_vec(values));
712		if has_none {
713			ColumnBuffer::Option {
714				inner: Box::new(inner),
715				bitvec: BitVec::from(bitvec),
716			}
717		} else {
718			inner
719		}
720	}
721
722	pub fn any_with_capacity(capacity: usize) -> Self {
723		ColumnBuffer::Any(AnyContainer::with_capacity(capacity))
724	}
725
726	pub fn any_with_bitvec(data: impl IntoIterator<Item = Box<Value>>, bitvec: impl Into<BitVec>) -> Self {
727		let data = data.into_iter().collect::<Vec<_>>();
728		let bitvec = bitvec.into();
729		assert_eq!(bitvec.len(), data.len());
730		let inner = ColumnBuffer::Any(AnyContainer::from_vec(data));
731		if bitvec.all_ones() {
732			inner
733		} else {
734			ColumnBuffer::Option {
735				inner: Box::new(inner),
736				bitvec,
737			}
738		}
739	}
740
741	pub fn dictionary_id(data: impl IntoIterator<Item = DictionaryEntryId>) -> Self {
742		let data = data.into_iter().collect::<Vec<_>>();
743		ColumnBuffer::DictionaryId(DictionaryContainer::from_vec(data))
744	}
745
746	pub fn dictionary_id_optional(data: impl IntoIterator<Item = Option<DictionaryEntryId>>) -> Self {
747		let mut values = Vec::new();
748		let mut bitvec = Vec::new();
749		let mut has_none = false;
750
751		for opt in data {
752			match opt {
753				Some(value) => {
754					values.push(value);
755					bitvec.push(true);
756				}
757				None => {
758					values.push(DictionaryEntryId::default());
759					bitvec.push(false);
760					has_none = true;
761				}
762			}
763		}
764
765		let inner = ColumnBuffer::DictionaryId(DictionaryContainer::from_vec(values));
766		if has_none {
767			ColumnBuffer::Option {
768				inner: Box::new(inner),
769				bitvec: BitVec::from(bitvec),
770			}
771		} else {
772			inner
773		}
774	}
775
776	pub fn dictionary_id_with_capacity(capacity: usize) -> Self {
777		ColumnBuffer::DictionaryId(DictionaryContainer::with_capacity(capacity))
778	}
779
780	pub fn dictionary_id_with_bitvec(
781		data: impl IntoIterator<Item = DictionaryEntryId>,
782		bitvec: impl Into<BitVec>,
783	) -> Self {
784		let data = data.into_iter().collect::<Vec<_>>();
785		let bitvec = bitvec.into();
786		assert_eq!(bitvec.len(), data.len());
787		let inner = ColumnBuffer::DictionaryId(DictionaryContainer::from_vec(data));
788		if bitvec.all_ones() {
789			inner
790		} else {
791			ColumnBuffer::Option {
792				inner: Box::new(inner),
793				bitvec,
794			}
795		}
796	}
797
798	pub fn typed_none(ty: &ValueType) -> Self {
799		match ty {
800			ValueType::Option(inner) => Self::typed_none(inner),
801			_ => Self::none_typed(ty.clone(), 1),
802		}
803	}
804
805	pub fn none_typed(ty: ValueType, len: usize) -> Self {
806		let bitvec = BitVec::repeat(len, false);
807		let inner = match ty {
808			ValueType::Boolean => Self::bool(vec![false; len]),
809			ValueType::Float4 => Self::float4(vec![0.0f32; len]),
810			ValueType::Float8 => Self::float8(vec![0.0f64; len]),
811			ValueType::Int1 => Self::int1(vec![0i8; len]),
812			ValueType::Int2 => Self::int2(vec![0i16; len]),
813			ValueType::Int4 => Self::int4(vec![0i32; len]),
814			ValueType::Int8 => Self::int8(vec![0i64; len]),
815			ValueType::Int16 => Self::int16(vec![0i128; len]),
816			ValueType::Utf8 => Self::utf8(vec![String::new(); len]),
817			ValueType::Uint1 => Self::uint1(vec![0u8; len]),
818			ValueType::Uint2 => Self::uint2(vec![0u16; len]),
819			ValueType::Uint4 => Self::uint4(vec![0u32; len]),
820			ValueType::Uint8 => Self::uint8(vec![0u64; len]),
821			ValueType::Uint16 => Self::uint16(vec![0u128; len]),
822			ValueType::Date => Self::date(vec![Date::default(); len]),
823			ValueType::DateTime => Self::datetime(vec![DateTime::default(); len]),
824			ValueType::Time => Self::time(vec![Time::default(); len]),
825			ValueType::Duration => Self::duration(vec![Duration::default(); len]),
826			ValueType::Blob => Self::blob(vec![Blob::new(vec![]); len]),
827			ValueType::Uuid4 => Self::uuid4(vec![Uuid4::default(); len]),
828			ValueType::Uuid7 => Self::uuid7(vec![Uuid7::default(); len]),
829			ValueType::IdentityId => Self::identity_id(vec![IdentityId::default(); len]),
830			ValueType::Int => Self::int(vec![Int::default(); len]),
831			ValueType::Uint => Self::uint(vec![Uint::default(); len]),
832			ValueType::Decimal => Self::decimal(vec![Decimal::from(0); len]),
833			ValueType::Any => Self::any(vec![Box::new(Value::none()); len]),
834			ValueType::DictionaryId => Self::dictionary_id(vec![DictionaryEntryId::default(); len]),
835			ValueType::List(_) => Self::any(vec![Box::new(Value::List(vec![])); len]),
836			ValueType::Record(_) => Self::any(vec![Box::new(Value::Record(vec![])); len]),
837			ValueType::Tuple(_) => Self::any(vec![Box::new(Value::Tuple(vec![])); len]),
838			ValueType::Option(inner) => return Self::none_typed(*inner, len),
839		};
840		ColumnBuffer::Option {
841			inner: Box::new(inner),
842			bitvec,
843		}
844	}
845}