Skip to main content

reifydb_core/value/column/transform/
append.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use reifydb_type::{
5	Result,
6	storage::DataBitVec,
7	util::bitvec::BitVec,
8	value::{
9		Value,
10		blob::Blob,
11		constraint::Constraint,
12		date::Date,
13		datetime::DateTime,
14		decimal::Decimal,
15		duration::Duration,
16		int::Int,
17		row_number::RowNumber,
18		time::Time,
19		r#type::Type,
20		uint::Uint,
21		uuid::{Uuid4, Uuid7},
22	},
23};
24use uuid::Uuid;
25
26use crate::{
27	encoded::{row::EncodedRow, shape::RowShape},
28	error::CoreError,
29	value::column::{ColumnBuffer, columns::Columns},
30};
31
32impl Columns {
33	pub fn append_columns(&mut self, other: Columns) -> Result<()> {
34		if self.len() != other.len() {
35			return Err(CoreError::FrameError {
36				message: "mismatched column count".to_string(),
37			}
38			.into());
39		}
40
41		if !other.row_numbers.is_empty() {
42			self.row_numbers.make_mut().extend(other.row_numbers.iter().copied());
43		}
44		if !other.created_at.is_empty() {
45			self.created_at.make_mut().extend(other.created_at.iter().copied());
46		}
47		if !other.updated_at.is_empty() {
48			self.updated_at.make_mut().extend(other.updated_at.iter().copied());
49		}
50
51		for i in 0..self.columns.len() {
52			let self_name = self.names[i].text().to_string();
53			let other_name = other.names[i].text().to_string();
54			if self_name != other_name {
55				return Err(CoreError::FrameError {
56					message: format!(
57						"column name mismatch at index {}: '{}' vs '{}'",
58						i, self_name, other_name,
59					),
60				}
61				.into());
62			}
63			let other_data = other.columns[i].clone();
64			self.columns.make_mut()[i].extend(other_data)?;
65		}
66		Ok(())
67	}
68}
69
70impl Columns {
71	pub fn append_rows(
72		&mut self,
73		shape: &RowShape,
74		rows: impl IntoIterator<Item = EncodedRow>,
75		row_numbers: Vec<RowNumber>,
76	) -> Result<()> {
77		if self.len() != shape.field_count() {
78			return Err(CoreError::FrameError {
79				message: format!(
80					"mismatched column count: expected {}, got {}",
81					self.len(),
82					shape.field_count()
83				),
84			}
85			.into());
86		}
87
88		let rows: Vec<EncodedRow> = rows.into_iter().collect();
89
90		if !row_numbers.is_empty() && row_numbers.len() != rows.len() {
91			return Err(CoreError::FrameError {
92				message: format!(
93					"row_numbers length {} does not match rows length {}",
94					row_numbers.len(),
95					rows.len()
96				),
97			}
98			.into());
99		}
100
101		if !row_numbers.is_empty() {
102			self.row_numbers.make_mut().extend(row_numbers);
103		}
104
105		for row in &rows {
106			self.created_at.make_mut().push(DateTime::from_nanos(row.created_at_nanos()));
107			self.updated_at.make_mut().push(DateTime::from_nanos(row.updated_at_nanos()));
108		}
109
110		let columns = self.columns.make_mut();
111		for (index, column) in columns.iter_mut().enumerate() {
112			let field = shape.get_field(index).unwrap();
113			let is_all_none = if let ColumnBuffer::Option {
114				bitvec,
115				..
116			} = &*column
117			{
118				DataBitVec::count_ones(bitvec) == 0
119			} else {
120				false
121			};
122			if is_all_none {
123				let size = column.len();
124				let new_data = match field.constraint.get_type() {
125					Type::Boolean => ColumnBuffer::bool_with_bitvec(
126						vec![false; size],
127						BitVec::repeat(size, false),
128					),
129					Type::Float4 => ColumnBuffer::float4_with_bitvec(
130						vec![0.0f32; size],
131						BitVec::repeat(size, false),
132					),
133					Type::Float8 => ColumnBuffer::float8_with_bitvec(
134						vec![0.0f64; size],
135						BitVec::repeat(size, false),
136					),
137					Type::Int1 => ColumnBuffer::int1_with_bitvec(
138						vec![0i8; size],
139						BitVec::repeat(size, false),
140					),
141					Type::Int2 => ColumnBuffer::int2_with_bitvec(
142						vec![0i16; size],
143						BitVec::repeat(size, false),
144					),
145					Type::Int4 => ColumnBuffer::int4_with_bitvec(
146						vec![0i32; size],
147						BitVec::repeat(size, false),
148					),
149					Type::Int8 => ColumnBuffer::int8_with_bitvec(
150						vec![0i64; size],
151						BitVec::repeat(size, false),
152					),
153					Type::Int16 => ColumnBuffer::int16_with_bitvec(
154						vec![0i128; size],
155						BitVec::repeat(size, false),
156					),
157					Type::Utf8 => ColumnBuffer::utf8_with_bitvec(
158						vec![String::new(); size],
159						BitVec::repeat(size, false),
160					),
161					Type::Uint1 => ColumnBuffer::uint1_with_bitvec(
162						vec![0u8; size],
163						BitVec::repeat(size, false),
164					),
165					Type::Uint2 => ColumnBuffer::uint2_with_bitvec(
166						vec![0u16; size],
167						BitVec::repeat(size, false),
168					),
169					Type::Uint4 => ColumnBuffer::uint4_with_bitvec(
170						vec![0u32; size],
171						BitVec::repeat(size, false),
172					),
173					Type::Uint8 => ColumnBuffer::uint8_with_bitvec(
174						vec![0u64; size],
175						BitVec::repeat(size, false),
176					),
177					Type::Uint16 => ColumnBuffer::uint16_with_bitvec(
178						vec![0u128; size],
179						BitVec::repeat(size, false),
180					),
181					Type::Date => ColumnBuffer::date_with_bitvec(
182						vec![Date::default(); size],
183						BitVec::repeat(size, false),
184					),
185					Type::DateTime => ColumnBuffer::datetime_with_bitvec(
186						vec![DateTime::default(); size],
187						BitVec::repeat(size, false),
188					),
189					Type::Time => ColumnBuffer::time_with_bitvec(
190						vec![Time::default(); size],
191						BitVec::repeat(size, false),
192					),
193					Type::Duration => ColumnBuffer::duration_with_bitvec(
194						vec![Duration::default(); size],
195						BitVec::repeat(size, false),
196					),
197					Type::Option(_) => column.clone(),
198					Type::IdentityId => ColumnBuffer::identity_id_with_bitvec(
199						vec![Default::default(); size],
200						BitVec::repeat(size, false),
201					),
202					Type::Uuid4 => ColumnBuffer::uuid4_with_bitvec(
203						vec![Uuid4::from(Uuid::nil()); size],
204						BitVec::repeat(size, false),
205					),
206					Type::Uuid7 => ColumnBuffer::uuid7_with_bitvec(
207						vec![Uuid7::from(Uuid::nil()); size],
208						BitVec::repeat(size, false),
209					),
210					Type::Blob => ColumnBuffer::blob_with_bitvec(
211						vec![Blob::new(vec![]); size],
212						BitVec::repeat(size, false),
213					),
214					Type::Int => ColumnBuffer::int_with_bitvec(
215						vec![Int::default(); size],
216						BitVec::repeat(size, false),
217					),
218					Type::Uint => ColumnBuffer::uint_with_bitvec(
219						vec![Uint::default(); size],
220						BitVec::repeat(size, false),
221					),
222					Type::Decimal => ColumnBuffer::decimal_with_bitvec(
223						vec![Decimal::from(0); size],
224						BitVec::repeat(size, false),
225					),
226					Type::DictionaryId => {
227						let mut col_data = ColumnBuffer::dictionary_id_with_bitvec(
228							vec![Default::default(); size],
229							BitVec::repeat(size, false),
230						);
231						if let ColumnBuffer::DictionaryId(container) = &mut col_data
232							&& let Some(Constraint::Dictionary(dict_id, _)) =
233								field.constraint.constraint()
234						{
235							container.set_dictionary_id(*dict_id);
236						}
237						col_data
238					}
239					Type::Any | Type::List(_) | Type::Record(_) | Type::Tuple(_) => {
240						ColumnBuffer::any_with_bitvec(
241							vec![Box::new(Value::none()); size],
242							BitVec::repeat(size, false),
243						)
244					}
245				};
246
247				*column = new_data;
248			}
249
250			if let ColumnBuffer::DictionaryId(container) = &mut *column
251				&& container.dictionary_id().is_none()
252				&& let Some(Constraint::Dictionary(dict_id, _)) = field.constraint.constraint()
253			{
254				container.set_dictionary_id(*dict_id);
255			}
256		}
257
258		for row in &rows {
259			let all_defined = (0..shape.field_count()).all(|i| row.is_defined(i));
260
261			if all_defined {
262				self.append_all_defined_from_shape(shape, row)?;
263			} else {
264				self.append_fallback_from_shape(shape, row)?;
265			}
266		}
267
268		Ok(())
269	}
270
271	fn append_all_defined_from_shape(&mut self, shape: &RowShape, row: &EncodedRow) -> Result<()> {
272		let names_snapshot: Vec<String> = self.names.iter().map(|n| n.text().to_string()).collect();
273		let columns = self.columns.make_mut();
274		for (index, column) in columns.iter_mut().enumerate() {
275			let field = shape.get_field(index).unwrap();
276			match (&mut *column, field.constraint.get_type()) {
277				(
278					ColumnBuffer::Option {
279						inner,
280						bitvec,
281					},
282					_ty,
283				) => {
284					let value = shape.get_value(row, index);
285					if matches!(value, Value::None { .. }) {
286						inner.push_none();
287						DataBitVec::push(bitvec, false);
288					} else {
289						inner.push_value(value);
290						DataBitVec::push(bitvec, true);
291					}
292				}
293				(ColumnBuffer::Bool(container), Type::Boolean) => {
294					container.push(shape.get_bool(row, index));
295				}
296				(ColumnBuffer::Float4(container), Type::Float4) => {
297					container.push(shape.get_f32(row, index));
298				}
299				(ColumnBuffer::Float8(container), Type::Float8) => {
300					container.push(shape.get_f64(row, index));
301				}
302				(ColumnBuffer::Int1(container), Type::Int1) => {
303					container.push(shape.get_i8(row, index));
304				}
305				(ColumnBuffer::Int2(container), Type::Int2) => {
306					container.push(shape.get_i16(row, index));
307				}
308				(ColumnBuffer::Int4(container), Type::Int4) => {
309					container.push(shape.get_i32(row, index));
310				}
311				(ColumnBuffer::Int8(container), Type::Int8) => {
312					container.push(shape.get_i64(row, index));
313				}
314				(ColumnBuffer::Int16(container), Type::Int16) => {
315					container.push(shape.get_i128(row, index));
316				}
317				(
318					ColumnBuffer::Utf8 {
319						container,
320						..
321					},
322					Type::Utf8,
323				) => {
324					container.push(shape.get_utf8(row, index).to_string());
325				}
326				(ColumnBuffer::Uint1(container), Type::Uint1) => {
327					container.push(shape.get_u8(row, index));
328				}
329				(ColumnBuffer::Uint2(container), Type::Uint2) => {
330					container.push(shape.get_u16(row, index));
331				}
332				(ColumnBuffer::Uint4(container), Type::Uint4) => {
333					container.push(shape.get_u32(row, index));
334				}
335				(ColumnBuffer::Uint8(container), Type::Uint8) => {
336					container.push(shape.get_u64(row, index));
337				}
338				(ColumnBuffer::Uint16(container), Type::Uint16) => {
339					container.push(shape.get_u128(row, index));
340				}
341				(ColumnBuffer::Date(container), Type::Date) => {
342					container.push(shape.get_date(row, index));
343				}
344				(ColumnBuffer::DateTime(container), Type::DateTime) => {
345					container.push(shape.get_datetime(row, index));
346				}
347				(ColumnBuffer::Time(container), Type::Time) => {
348					container.push(shape.get_time(row, index));
349				}
350				(ColumnBuffer::Duration(container), Type::Duration) => {
351					container.push(shape.get_duration(row, index));
352				}
353				(ColumnBuffer::Uuid4(container), Type::Uuid4) => {
354					container.push(shape.get_uuid4(row, index));
355				}
356				(ColumnBuffer::Uuid7(container), Type::Uuid7) => {
357					container.push(shape.get_uuid7(row, index));
358				}
359				(ColumnBuffer::IdentityId(container), Type::IdentityId) => {
360					container.push(shape.get_identity_id(row, index));
361				}
362				(
363					ColumnBuffer::Blob {
364						container,
365						..
366					},
367					Type::Blob,
368				) => {
369					container.push(shape.get_blob(row, index));
370				}
371				(
372					ColumnBuffer::Int {
373						container,
374						..
375					},
376					Type::Int,
377				) => {
378					container.push(shape.get_int(row, index));
379				}
380				(
381					ColumnBuffer::Uint {
382						container,
383						..
384					},
385					Type::Uint,
386				) => {
387					container.push(shape.get_uint(row, index));
388				}
389				(
390					ColumnBuffer::Decimal {
391						container,
392						..
393					},
394					Type::Decimal,
395				) => {
396					container.push(shape.get_decimal(row, index));
397				}
398				(ColumnBuffer::DictionaryId(container), Type::DictionaryId) => {
399					match shape.get_value(row, index) {
400						Value::DictionaryId(id) => container.push(id),
401						_ => container.push_default(),
402					}
403				}
404				(_, v) => {
405					return Err(CoreError::FrameError {
406						message: format!(
407							"type mismatch for column '{}'({}): incompatible with value {}",
408							names_snapshot[index],
409							column.get_type(),
410							v
411						),
412					}
413					.into());
414				}
415			}
416		}
417		Ok(())
418	}
419
420	fn append_fallback_from_shape(&mut self, shape: &RowShape, row: &EncodedRow) -> Result<()> {
421		let columns = self.columns.make_mut();
422		for (index, column) in columns.iter_mut().enumerate() {
423			let field = shape.get_field(index).unwrap();
424
425			if !row.is_defined(index) {
426				column.push_none();
427				continue;
428			}
429
430			match (&mut *column, field.constraint.get_type()) {
431				(
432					ColumnBuffer::Option {
433						inner,
434						bitvec,
435					},
436					_ty,
437				) => {
438					let value = shape.get_value(row, index);
439					inner.push_value(value);
440					DataBitVec::push(bitvec, true);
441				}
442				(ColumnBuffer::Bool(container), Type::Boolean) => {
443					container.push(shape.get_bool(row, index));
444				}
445				(ColumnBuffer::Float4(container), Type::Float4) => {
446					container.push(shape.get_f32(row, index));
447				}
448				(ColumnBuffer::Float8(container), Type::Float8) => {
449					container.push(shape.get_f64(row, index));
450				}
451				(ColumnBuffer::Int1(container), Type::Int1) => {
452					container.push(shape.get_i8(row, index));
453				}
454				(ColumnBuffer::Int2(container), Type::Int2) => {
455					container.push(shape.get_i16(row, index));
456				}
457				(ColumnBuffer::Int4(container), Type::Int4) => {
458					container.push(shape.get_i32(row, index));
459				}
460				(ColumnBuffer::Int8(container), Type::Int8) => {
461					container.push(shape.get_i64(row, index));
462				}
463				(ColumnBuffer::Int16(container), Type::Int16) => {
464					container.push(shape.get_i128(row, index));
465				}
466				(
467					ColumnBuffer::Utf8 {
468						container,
469						..
470					},
471					Type::Utf8,
472				) => {
473					container.push(shape.get_utf8(row, index).to_string());
474				}
475				(ColumnBuffer::Uint1(container), Type::Uint1) => {
476					container.push(shape.get_u8(row, index));
477				}
478				(ColumnBuffer::Uint2(container), Type::Uint2) => {
479					container.push(shape.get_u16(row, index));
480				}
481				(ColumnBuffer::Uint4(container), Type::Uint4) => {
482					container.push(shape.get_u32(row, index));
483				}
484				(ColumnBuffer::Uint8(container), Type::Uint8) => {
485					container.push(shape.get_u64(row, index));
486				}
487				(ColumnBuffer::Uint16(container), Type::Uint16) => {
488					container.push(shape.get_u128(row, index));
489				}
490				(ColumnBuffer::Date(container), Type::Date) => {
491					container.push(shape.get_date(row, index));
492				}
493				(ColumnBuffer::DateTime(container), Type::DateTime) => {
494					container.push(shape.get_datetime(row, index));
495				}
496				(ColumnBuffer::Time(container), Type::Time) => {
497					container.push(shape.get_time(row, index));
498				}
499				(ColumnBuffer::Duration(container), Type::Duration) => {
500					container.push(shape.get_duration(row, index));
501				}
502				(ColumnBuffer::Uuid4(container), Type::Uuid4) => {
503					container.push(shape.get_uuid4(row, index));
504				}
505				(ColumnBuffer::Uuid7(container), Type::Uuid7) => {
506					container.push(shape.get_uuid7(row, index));
507				}
508				(ColumnBuffer::IdentityId(container), Type::IdentityId) => {
509					container.push(shape.get_identity_id(row, index));
510				}
511				(
512					ColumnBuffer::Blob {
513						container,
514						..
515					},
516					Type::Blob,
517				) => {
518					container.push(shape.get_blob(row, index));
519				}
520				(
521					ColumnBuffer::Int {
522						container,
523						..
524					},
525					Type::Int,
526				) => {
527					container.push(shape.get_int(row, index));
528				}
529				(
530					ColumnBuffer::Uint {
531						container,
532						..
533					},
534					Type::Uint,
535				) => {
536					container.push(shape.get_uint(row, index));
537				}
538				(
539					ColumnBuffer::Decimal {
540						container,
541						..
542					},
543					Type::Decimal,
544				) => {
545					container.push(shape.get_decimal(row, index));
546				}
547				(ColumnBuffer::DictionaryId(container), Type::DictionaryId) => {
548					match shape.get_value(row, index) {
549						Value::DictionaryId(id) => container.push(id),
550						_ => container.push_default(),
551					}
552				}
553				(l, r) => unreachable!("{:#?} {:#?}", l, r),
554			}
555		}
556		Ok(())
557	}
558}
559
560#[cfg(test)]
561pub mod tests {
562	mod columns {
563		use reifydb_type::value::{
564			r#type::Type,
565			uuid::{Uuid4, Uuid7},
566		};
567		use uuid::{Timestamp, Uuid};
568
569		use crate::value::column::{ColumnBuffer, ColumnWithName, columns::Columns};
570
571		#[test]
572		fn test_boolean() {
573			let mut test_instance1 =
574				Columns::new(vec![ColumnWithName::bool_with_bitvec("id", [true], [false])]);
575
576			let test_instance2 =
577				Columns::new(vec![ColumnWithName::bool_with_bitvec("id", [false], [true])]);
578
579			test_instance1.append_columns(test_instance2).unwrap();
580
581			assert_eq!(test_instance1[0], ColumnBuffer::bool_with_bitvec([true, false], [false, true]));
582		}
583
584		#[test]
585		fn test_float4() {
586			let mut test_instance1 = Columns::new(vec![ColumnWithName::float4("id", [1.0f32, 2.0])]);
587
588			let test_instance2 = Columns::new(vec![ColumnWithName::float4_with_bitvec(
589				"id",
590				[3.0f32, 4.0],
591				[true, false],
592			)]);
593
594			test_instance1.append_columns(test_instance2).unwrap();
595
596			assert_eq!(
597				test_instance1[0],
598				ColumnBuffer::float4_with_bitvec([1.0f32, 2.0, 3.0, 4.0], [true, true, true, false])
599			);
600		}
601
602		#[test]
603		fn test_float8() {
604			let mut test_instance1 = Columns::new(vec![ColumnWithName::float8("id", [1.0f64, 2.0])]);
605
606			let test_instance2 = Columns::new(vec![ColumnWithName::float8_with_bitvec(
607				"id",
608				[3.0f64, 4.0],
609				[true, false],
610			)]);
611
612			test_instance1.append_columns(test_instance2).unwrap();
613
614			assert_eq!(
615				test_instance1[0],
616				ColumnBuffer::float8_with_bitvec([1.0f64, 2.0, 3.0, 4.0], [true, true, true, false])
617			);
618		}
619
620		#[test]
621		fn test_int1() {
622			let mut test_instance1 = Columns::new(vec![ColumnWithName::int1("id", [1, 2])]);
623
624			let test_instance2 =
625				Columns::new(vec![ColumnWithName::int1_with_bitvec("id", [3, 4], [true, false])]);
626
627			test_instance1.append_columns(test_instance2).unwrap();
628
629			assert_eq!(
630				test_instance1[0],
631				ColumnBuffer::int1_with_bitvec([1, 2, 3, 4], [true, true, true, false])
632			);
633		}
634
635		#[test]
636		fn test_int2() {
637			let mut test_instance1 = Columns::new(vec![ColumnWithName::int2("id", [1, 2])]);
638
639			let test_instance2 =
640				Columns::new(vec![ColumnWithName::int2_with_bitvec("id", [3, 4], [true, false])]);
641
642			test_instance1.append_columns(test_instance2).unwrap();
643
644			assert_eq!(
645				test_instance1[0],
646				ColumnBuffer::int2_with_bitvec([1, 2, 3, 4], [true, true, true, false])
647			);
648		}
649
650		#[test]
651		fn test_int4() {
652			let mut test_instance1 = Columns::new(vec![ColumnWithName::int4("id", [1, 2])]);
653
654			let test_instance2 =
655				Columns::new(vec![ColumnWithName::int4_with_bitvec("id", [3, 4], [true, false])]);
656
657			test_instance1.append_columns(test_instance2).unwrap();
658
659			assert_eq!(
660				test_instance1[0],
661				ColumnBuffer::int4_with_bitvec([1, 2, 3, 4], [true, true, true, false])
662			);
663		}
664
665		#[test]
666		fn test_int8() {
667			let mut test_instance1 = Columns::new(vec![ColumnWithName::int8("id", [1, 2])]);
668
669			let test_instance2 =
670				Columns::new(vec![ColumnWithName::int8_with_bitvec("id", [3, 4], [true, false])]);
671
672			test_instance1.append_columns(test_instance2).unwrap();
673
674			assert_eq!(
675				test_instance1[0],
676				ColumnBuffer::int8_with_bitvec([1, 2, 3, 4], [true, true, true, false])
677			);
678		}
679
680		#[test]
681		fn test_int16() {
682			let mut test_instance1 = Columns::new(vec![ColumnWithName::int16("id", [1, 2])]);
683
684			let test_instance2 =
685				Columns::new(vec![ColumnWithName::int16_with_bitvec("id", [3, 4], [true, false])]);
686
687			test_instance1.append_columns(test_instance2).unwrap();
688
689			assert_eq!(
690				test_instance1[0],
691				ColumnBuffer::int16_with_bitvec([1, 2, 3, 4], [true, true, true, false])
692			);
693		}
694
695		#[test]
696		fn test_string() {
697			let mut test_instance1 = Columns::new(vec![ColumnWithName::utf8_with_bitvec(
698				"id",
699				vec!["a".to_string(), "b".to_string()],
700				[true, true],
701			)]);
702
703			let test_instance2 = Columns::new(vec![ColumnWithName::utf8_with_bitvec(
704				"id",
705				vec!["c".to_string(), "d".to_string()],
706				[true, false],
707			)]);
708
709			test_instance1.append_columns(test_instance2).unwrap();
710
711			assert_eq!(
712				test_instance1[0],
713				ColumnBuffer::utf8_with_bitvec(
714					vec!["a".to_string(), "b".to_string(), "c".to_string(), "d".to_string()],
715					vec![true, true, true, false]
716				)
717			);
718		}
719
720		#[test]
721		fn test_uint1() {
722			let mut test_instance1 = Columns::new(vec![ColumnWithName::uint1("id", [1, 2])]);
723
724			let test_instance2 =
725				Columns::new(vec![ColumnWithName::uint1_with_bitvec("id", [3, 4], [true, false])]);
726
727			test_instance1.append_columns(test_instance2).unwrap();
728
729			assert_eq!(
730				test_instance1[0],
731				ColumnBuffer::uint1_with_bitvec([1, 2, 3, 4], [true, true, true, false])
732			);
733		}
734
735		#[test]
736		fn test_uint2() {
737			let mut test_instance1 = Columns::new(vec![ColumnWithName::uint2("id", [1, 2])]);
738
739			let test_instance2 =
740				Columns::new(vec![ColumnWithName::uint2_with_bitvec("id", [3, 4], [true, false])]);
741
742			test_instance1.append_columns(test_instance2).unwrap();
743
744			assert_eq!(
745				test_instance1[0],
746				ColumnBuffer::uint2_with_bitvec([1, 2, 3, 4], [true, true, true, false])
747			);
748		}
749
750		#[test]
751		fn test_uint4() {
752			let mut test_instance1 = Columns::new(vec![ColumnWithName::uint4("id", [1, 2])]);
753
754			let test_instance2 =
755				Columns::new(vec![ColumnWithName::uint4_with_bitvec("id", [3, 4], [true, false])]);
756
757			test_instance1.append_columns(test_instance2).unwrap();
758
759			assert_eq!(
760				test_instance1[0],
761				ColumnBuffer::uint4_with_bitvec([1, 2, 3, 4], [true, true, true, false])
762			);
763		}
764
765		#[test]
766		fn test_uint8() {
767			let mut test_instance1 = Columns::new(vec![ColumnWithName::uint8("id", [1, 2])]);
768
769			let test_instance2 =
770				Columns::new(vec![ColumnWithName::uint8_with_bitvec("id", [3, 4], [true, false])]);
771
772			test_instance1.append_columns(test_instance2).unwrap();
773
774			assert_eq!(
775				test_instance1[0],
776				ColumnBuffer::uint8_with_bitvec([1, 2, 3, 4], [true, true, true, false])
777			);
778		}
779
780		#[test]
781		fn test_uint16() {
782			let mut test_instance1 = Columns::new(vec![ColumnWithName::uint16("id", [1, 2])]);
783
784			let test_instance2 =
785				Columns::new(vec![ColumnWithName::uint16_with_bitvec("id", [3, 4], [true, false])]);
786
787			test_instance1.append_columns(test_instance2).unwrap();
788
789			assert_eq!(
790				test_instance1[0],
791				ColumnBuffer::uint16_with_bitvec([1, 2, 3, 4], [true, true, true, false])
792			);
793		}
794
795		#[test]
796		fn test_uuid4() {
797			let uuid1 = Uuid4::from(Uuid::new_v4());
798			let uuid2 = Uuid4::from(Uuid::new_v4());
799			let uuid3 = Uuid4::from(Uuid::new_v4());
800			let uuid4 = Uuid4::from(Uuid::new_v4());
801
802			let mut test_instance1 = Columns::new(vec![ColumnWithName::uuid4("id", [uuid1, uuid2])]);
803
804			let test_instance2 = Columns::new(vec![ColumnWithName::uuid4_with_bitvec(
805				"id",
806				[uuid3, uuid4],
807				[true, false],
808			)]);
809
810			test_instance1.append_columns(test_instance2).unwrap();
811
812			assert_eq!(
813				test_instance1[0],
814				ColumnBuffer::uuid4_with_bitvec(
815					[uuid1, uuid2, uuid3, uuid4],
816					[true, true, true, false]
817				)
818			);
819		}
820
821		#[test]
822		fn test_uuid7() {
823			let uuid1 = Uuid7::from(Uuid::new_v7(Timestamp::from_gregorian_time(1, 1)));
824			let uuid2 = Uuid7::from(Uuid::new_v7(Timestamp::from_gregorian_time(1, 2)));
825			let uuid3 = Uuid7::from(Uuid::new_v7(Timestamp::from_gregorian_time(2, 1)));
826			let uuid4 = Uuid7::from(Uuid::new_v7(Timestamp::from_gregorian_time(2, 2)));
827
828			let mut test_instance1 = Columns::new(vec![ColumnWithName::uuid7("id", [uuid1, uuid2])]);
829
830			let test_instance2 = Columns::new(vec![ColumnWithName::uuid7_with_bitvec(
831				"id",
832				[uuid3, uuid4],
833				[true, false],
834			)]);
835
836			test_instance1.append_columns(test_instance2).unwrap();
837
838			assert_eq!(
839				test_instance1[0],
840				ColumnBuffer::uuid7_with_bitvec(
841					[uuid1, uuid2, uuid3, uuid4],
842					[true, true, true, false]
843				)
844			);
845		}
846
847		#[test]
848		fn test_with_undefined_lr_promotes_correctly() {
849			let mut test_instance1 =
850				Columns::new(vec![ColumnWithName::int2_with_bitvec("id", [1, 2], [true, false])]);
851
852			let test_instance2 =
853				Columns::new(vec![ColumnWithName::undefined_typed("id", Type::Boolean, 2)]);
854
855			test_instance1.append_columns(test_instance2).unwrap();
856
857			assert_eq!(
858				test_instance1[0],
859				ColumnBuffer::int2_with_bitvec([1, 2, 0, 0], [true, false, false, false])
860			);
861		}
862
863		#[test]
864		fn test_with_undefined_l_promotes_correctly() {
865			let mut test_instance1 =
866				Columns::new(vec![ColumnWithName::undefined_typed("score", Type::Boolean, 2)]);
867
868			let test_instance2 =
869				Columns::new(vec![ColumnWithName::int2_with_bitvec("score", [10, 20], [true, false])]);
870
871			test_instance1.append_columns(test_instance2).unwrap();
872
873			assert_eq!(
874				test_instance1[0],
875				ColumnBuffer::int2_with_bitvec([0, 0, 10, 20], [false, false, true, false])
876			);
877		}
878
879		#[test]
880		fn test_fails_on_column_count_mismatch() {
881			let mut test_instance1 = Columns::new(vec![ColumnWithName::int2("id", [1])]);
882
883			let test_instance2 = Columns::new(vec![
884				ColumnWithName::int2("id", [2]),
885				ColumnWithName::utf8("name", vec!["Bob".to_string()]),
886			]);
887
888			let result = test_instance1.append_columns(test_instance2);
889			assert!(result.is_err());
890		}
891
892		#[test]
893		fn test_fails_on_column_name_mismatch() {
894			let mut test_instance1 = Columns::new(vec![ColumnWithName::int2("id", [1])]);
895
896			let test_instance2 = Columns::new(vec![ColumnWithName::int2("wrong", [2])]);
897
898			let result = test_instance1.append_columns(test_instance2);
899			assert!(result.is_err());
900		}
901
902		#[test]
903		fn test_fails_on_type_mismatch() {
904			let mut test_instance1 = Columns::new(vec![ColumnWithName::int2("id", [1])]);
905
906			let test_instance2 = Columns::new(vec![ColumnWithName::utf8("id", vec!["A".to_string()])]);
907
908			let result = test_instance1.append_columns(test_instance2);
909			assert!(result.is_err());
910		}
911	}
912
913	mod row {
914		use reifydb_type::{
915			fragment::Fragment,
916			util::bitvec::BitVec,
917			value::{
918				Value,
919				blob::Blob,
920				constraint::TypeConstraint,
921				dictionary::{DictionaryEntryId, DictionaryId},
922				identity::IdentityId,
923				ordered_f32::OrderedF32,
924				ordered_f64::OrderedF64,
925				r#type::Type,
926			},
927		};
928
929		use crate::{
930			encoded::shape::{RowShape, RowShapeField},
931			value::column::{ColumnBuffer, ColumnWithName, columns::Columns},
932		};
933
934		#[test]
935		fn test_before_undefined_bool() {
936			let mut test_instance =
937				Columns::new(vec![ColumnWithName::undefined_typed("test_col", Type::Boolean, 2)]);
938
939			let shape = RowShape::testing(&[Type::Boolean]);
940			let mut row = shape.allocate();
941			shape.set_values(&mut row, &[Value::Boolean(true)]);
942
943			test_instance.append_rows(&shape, [row], vec![]).unwrap();
944
945			assert_eq!(
946				test_instance[0],
947				ColumnBuffer::bool_with_bitvec(
948					[false, false, true],
949					BitVec::from_slice(&[false, false, true])
950				)
951			);
952		}
953
954		#[test]
955		fn test_before_undefined_float4() {
956			let mut test_instance =
957				Columns::new(vec![ColumnWithName::undefined_typed("test_col", Type::Boolean, 2)]);
958			let shape = RowShape::testing(&[Type::Float4]);
959			let mut row = shape.allocate();
960			shape.set_values(&mut row, &[Value::Float4(OrderedF32::try_from(1.5).unwrap())]);
961			test_instance.append_rows(&shape, [row], vec![]).unwrap();
962
963			assert_eq!(
964				test_instance[0],
965				ColumnBuffer::float4_with_bitvec(
966					[0.0, 0.0, 1.5],
967					BitVec::from_slice(&[false, false, true])
968				)
969			);
970		}
971
972		#[test]
973		fn test_before_undefined_float8() {
974			let mut test_instance =
975				Columns::new(vec![ColumnWithName::undefined_typed("test_col", Type::Boolean, 2)]);
976			let shape = RowShape::testing(&[Type::Float8]);
977			let mut row = shape.allocate();
978			shape.set_values(&mut row, &[Value::Float8(OrderedF64::try_from(2.25).unwrap())]);
979			test_instance.append_rows(&shape, [row], vec![]).unwrap();
980
981			assert_eq!(
982				test_instance[0],
983				ColumnBuffer::float8_with_bitvec(
984					[0.0, 0.0, 2.25],
985					BitVec::from_slice(&[false, false, true])
986				)
987			);
988		}
989
990		#[test]
991		fn test_before_undefined_int1() {
992			let mut test_instance =
993				Columns::new(vec![ColumnWithName::undefined_typed("test_col", Type::Boolean, 2)]);
994			let shape = RowShape::testing(&[Type::Int1]);
995			let mut row = shape.allocate();
996			shape.set_values(&mut row, &[Value::Int1(42)]);
997			test_instance.append_rows(&shape, [row], vec![]).unwrap();
998
999			assert_eq!(
1000				test_instance[0],
1001				ColumnBuffer::int1_with_bitvec([0, 0, 42], BitVec::from_slice(&[false, false, true]))
1002			);
1003		}
1004
1005		#[test]
1006		fn test_before_undefined_int2() {
1007			let mut test_instance =
1008				Columns::new(vec![ColumnWithName::undefined_typed("test_col", Type::Boolean, 2)]);
1009			let shape = RowShape::testing(&[Type::Int2]);
1010			let mut row = shape.allocate();
1011			shape.set_values(&mut row, &[Value::Int2(-1234)]);
1012			test_instance.append_rows(&shape, [row], vec![]).unwrap();
1013
1014			assert_eq!(
1015				test_instance[0],
1016				ColumnBuffer::int2_with_bitvec(
1017					[0, 0, -1234],
1018					BitVec::from_slice(&[false, false, true])
1019				)
1020			);
1021		}
1022
1023		#[test]
1024		fn test_before_undefined_int4() {
1025			let mut test_instance =
1026				Columns::new(vec![ColumnWithName::undefined_typed("test_col", Type::Boolean, 2)]);
1027			let shape = RowShape::testing(&[Type::Int4]);
1028			let mut row = shape.allocate();
1029			shape.set_values(&mut row, &[Value::Int4(56789)]);
1030			test_instance.append_rows(&shape, [row], vec![]).unwrap();
1031
1032			assert_eq!(
1033				test_instance[0],
1034				ColumnBuffer::int4_with_bitvec(
1035					[0, 0, 56789],
1036					BitVec::from_slice(&[false, false, true])
1037				)
1038			);
1039		}
1040
1041		#[test]
1042		fn test_before_undefined_int8() {
1043			let mut test_instance =
1044				Columns::new(vec![ColumnWithName::undefined_typed("test_col", Type::Boolean, 2)]);
1045			let shape = RowShape::testing(&[Type::Int8]);
1046			let mut row = shape.allocate();
1047			shape.set_values(&mut row, &[Value::Int8(-987654321)]);
1048			test_instance.append_rows(&shape, [row], vec![]).unwrap();
1049
1050			assert_eq!(
1051				test_instance[0],
1052				ColumnBuffer::int8_with_bitvec(
1053					[0, 0, -987654321],
1054					BitVec::from_slice(&[false, false, true])
1055				)
1056			);
1057		}
1058
1059		#[test]
1060		fn test_before_undefined_int16() {
1061			let mut test_instance =
1062				Columns::new(vec![ColumnWithName::undefined_typed("test_col", Type::Boolean, 2)]);
1063			let shape = RowShape::testing(&[Type::Int16]);
1064			let mut row = shape.allocate();
1065			shape.set_values(&mut row, &[Value::Int16(123456789012345678901234567890i128)]);
1066			test_instance.append_rows(&shape, [row], vec![]).unwrap();
1067
1068			assert_eq!(
1069				test_instance[0],
1070				ColumnBuffer::int16_with_bitvec(
1071					[0, 0, 123456789012345678901234567890i128],
1072					BitVec::from_slice(&[false, false, true])
1073				)
1074			);
1075		}
1076
1077		#[test]
1078		fn test_before_undefined_string() {
1079			let mut test_instance =
1080				Columns::new(vec![ColumnWithName::undefined_typed("test_col", Type::Boolean, 2)]);
1081			let shape = RowShape::testing(&[Type::Utf8]);
1082			let mut row = shape.allocate();
1083			shape.set_values(&mut row, &[Value::Utf8("reifydb".into())]);
1084			test_instance.append_rows(&shape, [row], vec![]).unwrap();
1085
1086			assert_eq!(
1087				test_instance[0],
1088				ColumnBuffer::utf8_with_bitvec(
1089					["".to_string(), "".to_string(), "reifydb".to_string()],
1090					BitVec::from_slice(&[false, false, true])
1091				)
1092			);
1093		}
1094
1095		#[test]
1096		fn test_before_undefined_uint1() {
1097			let mut test_instance =
1098				Columns::new(vec![ColumnWithName::undefined_typed("test_col", Type::Boolean, 2)]);
1099			let shape = RowShape::testing(&[Type::Uint1]);
1100			let mut row = shape.allocate();
1101			shape.set_values(&mut row, &[Value::Uint1(255)]);
1102			test_instance.append_rows(&shape, [row], vec![]).unwrap();
1103
1104			assert_eq!(
1105				test_instance[0],
1106				ColumnBuffer::uint1_with_bitvec([0, 0, 255], BitVec::from_slice(&[false, false, true]))
1107			);
1108		}
1109
1110		#[test]
1111		fn test_before_undefined_uint2() {
1112			let mut test_instance =
1113				Columns::new(vec![ColumnWithName::undefined_typed("test_col", Type::Boolean, 2)]);
1114			let shape = RowShape::testing(&[Type::Uint2]);
1115			let mut row = shape.allocate();
1116			shape.set_values(&mut row, &[Value::Uint2(65535)]);
1117			test_instance.append_rows(&shape, [row], vec![]).unwrap();
1118
1119			assert_eq!(
1120				test_instance[0],
1121				ColumnBuffer::uint2_with_bitvec(
1122					[0, 0, 65535],
1123					BitVec::from_slice(&[false, false, true])
1124				)
1125			);
1126		}
1127
1128		#[test]
1129		fn test_before_undefined_uint4() {
1130			let mut test_instance =
1131				Columns::new(vec![ColumnWithName::undefined_typed("test_col", Type::Boolean, 2)]);
1132			let shape = RowShape::testing(&[Type::Uint4]);
1133			let mut row = shape.allocate();
1134			shape.set_values(&mut row, &[Value::Uint4(4294967295)]);
1135			test_instance.append_rows(&shape, [row], vec![]).unwrap();
1136
1137			assert_eq!(
1138				test_instance[0],
1139				ColumnBuffer::uint4_with_bitvec(
1140					[0, 0, 4294967295],
1141					BitVec::from_slice(&[false, false, true])
1142				)
1143			);
1144		}
1145
1146		#[test]
1147		fn test_before_undefined_uint8() {
1148			let mut test_instance =
1149				Columns::new(vec![ColumnWithName::undefined_typed("test_col", Type::Boolean, 2)]);
1150			let shape = RowShape::testing(&[Type::Uint8]);
1151			let mut row = shape.allocate();
1152			shape.set_values(&mut row, &[Value::Uint8(18446744073709551615)]);
1153			test_instance.append_rows(&shape, [row], vec![]).unwrap();
1154
1155			assert_eq!(
1156				test_instance[0],
1157				ColumnBuffer::uint8_with_bitvec(
1158					[0, 0, 18446744073709551615],
1159					BitVec::from_slice(&[false, false, true])
1160				)
1161			);
1162		}
1163
1164		#[test]
1165		fn test_before_undefined_uint16() {
1166			let mut test_instance =
1167				Columns::new(vec![ColumnWithName::undefined_typed("test_col", Type::Boolean, 2)]);
1168			let shape = RowShape::testing(&[Type::Uint16]);
1169			let mut row = shape.allocate();
1170			shape.set_values(&mut row, &[Value::Uint16(340282366920938463463374607431768211455u128)]);
1171			test_instance.append_rows(&shape, [row], vec![]).unwrap();
1172
1173			assert_eq!(
1174				test_instance[0],
1175				ColumnBuffer::uint16_with_bitvec(
1176					[0, 0, 340282366920938463463374607431768211455u128],
1177					BitVec::from_slice(&[false, false, true])
1178				)
1179			);
1180		}
1181
1182		#[test]
1183		fn test_mismatched_columns() {
1184			let mut test_instance = Columns::new(vec![]);
1185
1186			let shape = RowShape::testing(&[Type::Int2]);
1187			let mut row = shape.allocate();
1188			shape.set_values(&mut row, &[Value::Int2(2)]);
1189
1190			let err = test_instance.append_rows(&shape, [row], vec![]).err().unwrap();
1191			assert!(err.to_string().contains("mismatched column count: expected 0, got 1"));
1192		}
1193
1194		#[test]
1195		fn test_ok() {
1196			let mut test_instance = test_instance_with_columns();
1197
1198			let shape = RowShape::testing(&[Type::Int2, Type::Boolean]);
1199			let mut row_one = shape.allocate();
1200			shape.set_values(&mut row_one, &[Value::Int2(2), Value::Boolean(true)]);
1201			let mut row_two = shape.allocate();
1202			shape.set_values(&mut row_two, &[Value::Int2(3), Value::Boolean(false)]);
1203
1204			test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
1205
1206			assert_eq!(test_instance[0], ColumnBuffer::int2([1, 2, 3]));
1207			assert_eq!(test_instance[1], ColumnBuffer::bool([true, true, false]));
1208		}
1209
1210		#[test]
1211		fn test_all_defined_bool() {
1212			let mut test_instance =
1213				Columns::new(vec![ColumnWithName::bool("test_col", Vec::<bool>::new())]);
1214
1215			let shape = RowShape::testing(&[Type::Boolean]);
1216			let mut row_one = shape.allocate();
1217			shape.set_bool(&mut row_one, 0, true);
1218			let mut row_two = shape.allocate();
1219			shape.set_bool(&mut row_two, 0, false);
1220
1221			test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
1222
1223			assert_eq!(test_instance[0], ColumnBuffer::bool([true, false]));
1224		}
1225
1226		#[test]
1227		fn test_all_defined_float4() {
1228			let mut test_instance =
1229				Columns::new(vec![ColumnWithName::float4("test_col", Vec::<f32>::new())]);
1230
1231			let shape = RowShape::testing(&[Type::Float4]);
1232			let mut row_one = shape.allocate();
1233			shape.set_values(&mut row_one, &[Value::Float4(OrderedF32::try_from(1.0).unwrap())]);
1234			let mut row_two = shape.allocate();
1235			shape.set_values(&mut row_two, &[Value::Float4(OrderedF32::try_from(2.0).unwrap())]);
1236
1237			test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
1238
1239			assert_eq!(test_instance[0], ColumnBuffer::float4([1.0, 2.0]));
1240		}
1241
1242		#[test]
1243		fn test_all_defined_float8() {
1244			let mut test_instance =
1245				Columns::new(vec![ColumnWithName::float8("test_col", Vec::<f64>::new())]);
1246
1247			let shape = RowShape::testing(&[Type::Float8]);
1248			let mut row_one = shape.allocate();
1249			shape.set_values(&mut row_one, &[Value::Float8(OrderedF64::try_from(1.0).unwrap())]);
1250			let mut row_two = shape.allocate();
1251			shape.set_values(&mut row_two, &[Value::Float8(OrderedF64::try_from(2.0).unwrap())]);
1252
1253			test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
1254
1255			assert_eq!(test_instance[0], ColumnBuffer::float8([1.0, 2.0]));
1256		}
1257
1258		#[test]
1259		fn test_all_defined_int1() {
1260			let mut test_instance = Columns::new(vec![ColumnWithName::int1("test_col", Vec::<i8>::new())]);
1261
1262			let shape = RowShape::testing(&[Type::Int1]);
1263			let mut row_one = shape.allocate();
1264			shape.set_values(&mut row_one, &[Value::Int1(1)]);
1265			let mut row_two = shape.allocate();
1266			shape.set_values(&mut row_two, &[Value::Int1(2)]);
1267
1268			test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
1269
1270			assert_eq!(test_instance[0], ColumnBuffer::int1([1, 2]));
1271		}
1272
1273		#[test]
1274		fn test_all_defined_int2() {
1275			let mut test_instance = Columns::new(vec![ColumnWithName::int2("test_col", Vec::<i16>::new())]);
1276
1277			let shape = RowShape::testing(&[Type::Int2]);
1278			let mut row_one = shape.allocate();
1279			shape.set_values(&mut row_one, &[Value::Int2(100)]);
1280			let mut row_two = shape.allocate();
1281			shape.set_values(&mut row_two, &[Value::Int2(200)]);
1282
1283			test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
1284
1285			assert_eq!(test_instance[0], ColumnBuffer::int2([100, 200]));
1286		}
1287
1288		#[test]
1289		fn test_all_defined_int4() {
1290			let mut test_instance = Columns::new(vec![ColumnWithName::int4("test_col", Vec::<i32>::new())]);
1291
1292			let shape = RowShape::testing(&[Type::Int4]);
1293			let mut row_one = shape.allocate();
1294			shape.set_values(&mut row_one, &[Value::Int4(1000)]);
1295			let mut row_two = shape.allocate();
1296			shape.set_values(&mut row_two, &[Value::Int4(2000)]);
1297
1298			test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
1299
1300			assert_eq!(test_instance[0], ColumnBuffer::int4([1000, 2000]));
1301		}
1302
1303		#[test]
1304		fn test_all_defined_int8() {
1305			let mut test_instance = Columns::new(vec![ColumnWithName::int8("test_col", Vec::<i64>::new())]);
1306
1307			let shape = RowShape::testing(&[Type::Int8]);
1308			let mut row_one = shape.allocate();
1309			shape.set_values(&mut row_one, &[Value::Int8(10000)]);
1310			let mut row_two = shape.allocate();
1311			shape.set_values(&mut row_two, &[Value::Int8(20000)]);
1312
1313			test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
1314
1315			assert_eq!(test_instance[0], ColumnBuffer::int8([10000, 20000]));
1316		}
1317
1318		#[test]
1319		fn test_all_defined_int16() {
1320			let mut test_instance =
1321				Columns::new(vec![ColumnWithName::int16("test_col", Vec::<i128>::new())]);
1322
1323			let shape = RowShape::testing(&[Type::Int16]);
1324			let mut row_one = shape.allocate();
1325			shape.set_values(&mut row_one, &[Value::Int16(1000)]);
1326			let mut row_two = shape.allocate();
1327			shape.set_values(&mut row_two, &[Value::Int16(2000)]);
1328
1329			test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
1330
1331			assert_eq!(test_instance[0], ColumnBuffer::int16([1000, 2000]));
1332		}
1333
1334		#[test]
1335		fn test_all_defined_string() {
1336			let mut test_instance =
1337				Columns::new(vec![ColumnWithName::utf8("test_col", Vec::<String>::new())]);
1338
1339			let shape = RowShape::testing(&[Type::Utf8]);
1340			let mut row_one = shape.allocate();
1341			shape.set_values(&mut row_one, &[Value::Utf8("a".into())]);
1342			let mut row_two = shape.allocate();
1343			shape.set_values(&mut row_two, &[Value::Utf8("b".into())]);
1344
1345			test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
1346
1347			assert_eq!(test_instance[0], ColumnBuffer::utf8(["a".to_string(), "b".to_string()]));
1348		}
1349
1350		#[test]
1351		fn test_all_defined_uint1() {
1352			let mut test_instance = Columns::new(vec![ColumnWithName::uint1("test_col", Vec::<u8>::new())]);
1353
1354			let shape = RowShape::testing(&[Type::Uint1]);
1355			let mut row_one = shape.allocate();
1356			shape.set_values(&mut row_one, &[Value::Uint1(1)]);
1357			let mut row_two = shape.allocate();
1358			shape.set_values(&mut row_two, &[Value::Uint1(2)]);
1359
1360			test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
1361
1362			assert_eq!(test_instance[0], ColumnBuffer::uint1([1, 2]));
1363		}
1364
1365		#[test]
1366		fn test_all_defined_uint2() {
1367			let mut test_instance =
1368				Columns::new(vec![ColumnWithName::uint2("test_col", Vec::<u16>::new())]);
1369
1370			let shape = RowShape::testing(&[Type::Uint2]);
1371			let mut row_one = shape.allocate();
1372			shape.set_values(&mut row_one, &[Value::Uint2(100)]);
1373			let mut row_two = shape.allocate();
1374			shape.set_values(&mut row_two, &[Value::Uint2(200)]);
1375
1376			test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
1377
1378			assert_eq!(test_instance[0], ColumnBuffer::uint2([100, 200]));
1379		}
1380
1381		#[test]
1382		fn test_all_defined_uint4() {
1383			let mut test_instance =
1384				Columns::new(vec![ColumnWithName::uint4("test_col", Vec::<u32>::new())]);
1385
1386			let shape = RowShape::testing(&[Type::Uint4]);
1387			let mut row_one = shape.allocate();
1388			shape.set_values(&mut row_one, &[Value::Uint4(1000)]);
1389			let mut row_two = shape.allocate();
1390			shape.set_values(&mut row_two, &[Value::Uint4(2000)]);
1391
1392			test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
1393
1394			assert_eq!(test_instance[0], ColumnBuffer::uint4([1000, 2000]));
1395		}
1396
1397		#[test]
1398		fn test_all_defined_uint8() {
1399			let mut test_instance =
1400				Columns::new(vec![ColumnWithName::uint8("test_col", Vec::<u64>::new())]);
1401
1402			let shape = RowShape::testing(&[Type::Uint8]);
1403			let mut row_one = shape.allocate();
1404			shape.set_values(&mut row_one, &[Value::Uint8(10000)]);
1405			let mut row_two = shape.allocate();
1406			shape.set_values(&mut row_two, &[Value::Uint8(20000)]);
1407
1408			test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
1409
1410			assert_eq!(test_instance[0], ColumnBuffer::uint8([10000, 20000]));
1411		}
1412
1413		#[test]
1414		fn test_all_defined_uint16() {
1415			let mut test_instance =
1416				Columns::new(vec![ColumnWithName::uint16("test_col", Vec::<u128>::new())]);
1417
1418			let shape = RowShape::testing(&[Type::Uint16]);
1419			let mut row_one = shape.allocate();
1420			shape.set_values(&mut row_one, &[Value::Uint16(1000)]);
1421			let mut row_two = shape.allocate();
1422			shape.set_values(&mut row_two, &[Value::Uint16(2000)]);
1423
1424			test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
1425
1426			assert_eq!(test_instance[0], ColumnBuffer::uint16([1000, 2000]));
1427		}
1428
1429		#[test]
1430		fn test_row_with_undefined() {
1431			let mut test_instance = test_instance_with_columns();
1432
1433			let shape = RowShape::testing(&[Type::Int2, Type::Boolean]);
1434			let mut row = shape.allocate();
1435			shape.set_values(&mut row, &[Value::none(), Value::Boolean(false)]);
1436
1437			test_instance.append_rows(&shape, [row], vec![]).unwrap();
1438
1439			assert_eq!(test_instance[0], ColumnBuffer::int2_with_bitvec(vec![1, 0], vec![true, false]));
1440			assert_eq!(test_instance[1], ColumnBuffer::bool_with_bitvec([true, false], [true, true]));
1441		}
1442
1443		#[test]
1444		fn test_row_with_type_mismatch_fails() {
1445			let mut test_instance = test_instance_with_columns();
1446
1447			let shape = RowShape::testing(&[Type::Boolean, Type::Boolean]);
1448			let mut row = shape.allocate();
1449			shape.set_values(&mut row, &[Value::Boolean(true), Value::Boolean(true)]);
1450
1451			let result = test_instance.append_rows(&shape, [row], vec![]);
1452			assert!(result.is_err());
1453			assert!(result.unwrap_err().to_string().contains("type mismatch"));
1454		}
1455
1456		#[test]
1457		fn test_row_wrong_length_fails() {
1458			let mut test_instance = test_instance_with_columns();
1459
1460			let shape = RowShape::testing(&[Type::Int2]);
1461			let mut row = shape.allocate();
1462			shape.set_values(&mut row, &[Value::Int2(2)]);
1463
1464			let result = test_instance.append_rows(&shape, [row], vec![]);
1465			assert!(result.is_err());
1466			assert!(result.unwrap_err().to_string().contains("mismatched column count"));
1467		}
1468
1469		#[test]
1470		fn test_fallback_bool() {
1471			let mut test_instance = Columns::new(vec![
1472				ColumnWithName::bool("test_col", Vec::<bool>::new()),
1473				ColumnWithName::bool("none", Vec::<bool>::new()),
1474			]);
1475
1476			let shape = RowShape::testing(&[Type::Boolean, Type::Boolean]);
1477			let mut row_one = shape.allocate();
1478			shape.set_bool(&mut row_one, 0, true);
1479			shape.set_none(&mut row_one, 1);
1480
1481			test_instance.append_rows(&shape, [row_one], vec![]).unwrap();
1482
1483			assert_eq!(test_instance[0], ColumnBuffer::bool_with_bitvec([true], [true]));
1484
1485			assert_eq!(test_instance[1], ColumnBuffer::bool_with_bitvec([false], [false]));
1486		}
1487
1488		#[test]
1489		fn test_fallback_float4() {
1490			let mut test_instance = Columns::new(vec![
1491				ColumnWithName::float4("test_col", Vec::<f32>::new()),
1492				ColumnWithName::float4("none", Vec::<f32>::new()),
1493			]);
1494
1495			let shape = RowShape::testing(&[Type::Float4, Type::Float4]);
1496			let mut row = shape.allocate();
1497			shape.set_f32(&mut row, 0, 1.5);
1498			shape.set_none(&mut row, 1);
1499
1500			test_instance.append_rows(&shape, [row], vec![]).unwrap();
1501
1502			assert_eq!(test_instance[0], ColumnBuffer::float4_with_bitvec([1.5], [true]));
1503			assert_eq!(test_instance[1], ColumnBuffer::float4_with_bitvec([0.0], [false]));
1504		}
1505
1506		#[test]
1507		fn test_fallback_float8() {
1508			let mut test_instance = Columns::new(vec![
1509				ColumnWithName::float8("test_col", Vec::<f64>::new()),
1510				ColumnWithName::float8("none", Vec::<f64>::new()),
1511			]);
1512
1513			let shape = RowShape::testing(&[Type::Float8, Type::Float8]);
1514			let mut row = shape.allocate();
1515			shape.set_f64(&mut row, 0, 2.5);
1516			shape.set_none(&mut row, 1);
1517
1518			test_instance.append_rows(&shape, [row], vec![]).unwrap();
1519
1520			assert_eq!(test_instance[0], ColumnBuffer::float8_with_bitvec([2.5], [true]));
1521			assert_eq!(test_instance[1], ColumnBuffer::float8_with_bitvec([0.0], [false]));
1522		}
1523
1524		#[test]
1525		fn test_fallback_int1() {
1526			let mut test_instance = Columns::new(vec![
1527				ColumnWithName::int1("test_col", Vec::<i8>::new()),
1528				ColumnWithName::int1("none", Vec::<i8>::new()),
1529			]);
1530
1531			let shape = RowShape::testing(&[Type::Int1, Type::Int1]);
1532			let mut row = shape.allocate();
1533			shape.set_i8(&mut row, 0, 42);
1534			shape.set_none(&mut row, 1);
1535
1536			test_instance.append_rows(&shape, [row], vec![]).unwrap();
1537
1538			assert_eq!(test_instance[0], ColumnBuffer::int1_with_bitvec([42], [true]));
1539			assert_eq!(test_instance[1], ColumnBuffer::int1_with_bitvec([0], [false]));
1540		}
1541
1542		#[test]
1543		fn test_fallback_int2() {
1544			let mut test_instance = Columns::new(vec![
1545				ColumnWithName::int2("test_col", Vec::<i16>::new()),
1546				ColumnWithName::int2("none", Vec::<i16>::new()),
1547			]);
1548
1549			let shape = RowShape::testing(&[Type::Int2, Type::Int2]);
1550			let mut row = shape.allocate();
1551			shape.set_i16(&mut row, 0, -1234i16);
1552			shape.set_none(&mut row, 1);
1553
1554			test_instance.append_rows(&shape, [row], vec![]).unwrap();
1555
1556			assert_eq!(test_instance[0], ColumnBuffer::int2_with_bitvec([-1234], [true]));
1557			assert_eq!(test_instance[1], ColumnBuffer::int2_with_bitvec([0], [false]));
1558		}
1559
1560		#[test]
1561		fn test_fallback_int4() {
1562			let mut test_instance = Columns::new(vec![
1563				ColumnWithName::int4("test_col", Vec::<i32>::new()),
1564				ColumnWithName::int4("none", Vec::<i32>::new()),
1565			]);
1566
1567			let shape = RowShape::testing(&[Type::Int4, Type::Int4]);
1568			let mut row = shape.allocate();
1569			shape.set_i32(&mut row, 0, 56789);
1570			shape.set_none(&mut row, 1);
1571
1572			test_instance.append_rows(&shape, [row], vec![]).unwrap();
1573
1574			assert_eq!(test_instance[0], ColumnBuffer::int4_with_bitvec([56789], [true]));
1575			assert_eq!(test_instance[1], ColumnBuffer::int4_with_bitvec([0], [false]));
1576		}
1577
1578		#[test]
1579		fn test_fallback_int8() {
1580			let mut test_instance = Columns::new(vec![
1581				ColumnWithName::int8("test_col", Vec::<i64>::new()),
1582				ColumnWithName::int8("none", Vec::<i64>::new()),
1583			]);
1584
1585			let shape = RowShape::testing(&[Type::Int8, Type::Int8]);
1586			let mut row = shape.allocate();
1587			shape.set_i64(&mut row, 0, -987654321);
1588			shape.set_none(&mut row, 1);
1589
1590			test_instance.append_rows(&shape, [row], vec![]).unwrap();
1591
1592			assert_eq!(test_instance[0], ColumnBuffer::int8_with_bitvec([-987654321], [true]));
1593			assert_eq!(test_instance[1], ColumnBuffer::int8_with_bitvec([0], [false]));
1594		}
1595
1596		#[test]
1597		fn test_fallback_int16() {
1598			let mut test_instance = Columns::new(vec![
1599				ColumnWithName::int16("test_col", Vec::<i128>::new()),
1600				ColumnWithName::int16("none", Vec::<i128>::new()),
1601			]);
1602
1603			let shape = RowShape::testing(&[Type::Int16, Type::Int16]);
1604			let mut row = shape.allocate();
1605			shape.set_i128(&mut row, 0, 123456789012345678901234567890i128);
1606			shape.set_none(&mut row, 1);
1607
1608			test_instance.append_rows(&shape, [row], vec![]).unwrap();
1609
1610			assert_eq!(
1611				test_instance[0],
1612				ColumnBuffer::int16_with_bitvec([123456789012345678901234567890i128], [true])
1613			);
1614			assert_eq!(test_instance[1], ColumnBuffer::int16_with_bitvec([0], [false]));
1615		}
1616
1617		#[test]
1618		fn test_fallback_string() {
1619			let mut test_instance = Columns::new(vec![
1620				ColumnWithName::utf8("test_col", Vec::<String>::new()),
1621				ColumnWithName::utf8("none", Vec::<String>::new()),
1622			]);
1623
1624			let shape = RowShape::testing(&[Type::Utf8, Type::Utf8]);
1625			let mut row = shape.allocate();
1626			shape.set_utf8(&mut row, 0, "reifydb");
1627			shape.set_none(&mut row, 1);
1628
1629			test_instance.append_rows(&shape, [row], vec![]).unwrap();
1630
1631			assert_eq!(test_instance[0], ColumnBuffer::utf8_with_bitvec(["reifydb".to_string()], [true]));
1632			assert_eq!(test_instance[1], ColumnBuffer::utf8_with_bitvec(["".to_string()], [false]));
1633		}
1634
1635		#[test]
1636		fn test_fallback_uint1() {
1637			let mut test_instance = Columns::new(vec![
1638				ColumnWithName::uint1("test_col", Vec::<u8>::new()),
1639				ColumnWithName::uint1("none", Vec::<u8>::new()),
1640			]);
1641
1642			let shape = RowShape::testing(&[Type::Uint1, Type::Uint1]);
1643			let mut row = shape.allocate();
1644			shape.set_u8(&mut row, 0, 255);
1645			shape.set_none(&mut row, 1);
1646
1647			test_instance.append_rows(&shape, [row], vec![]).unwrap();
1648
1649			assert_eq!(test_instance[0], ColumnBuffer::uint1_with_bitvec([255], [true]));
1650			assert_eq!(test_instance[1], ColumnBuffer::uint1_with_bitvec([0], [false]));
1651		}
1652
1653		#[test]
1654		fn test_fallback_uint2() {
1655			let mut test_instance = Columns::new(vec![
1656				ColumnWithName::uint2("test_col", Vec::<u16>::new()),
1657				ColumnWithName::uint2("none", Vec::<u16>::new()),
1658			]);
1659
1660			let shape = RowShape::testing(&[Type::Uint2, Type::Uint2]);
1661			let mut row = shape.allocate();
1662			shape.set_u16(&mut row, 0, 65535u16);
1663			shape.set_none(&mut row, 1);
1664
1665			test_instance.append_rows(&shape, [row], vec![]).unwrap();
1666
1667			assert_eq!(test_instance[0], ColumnBuffer::uint2_with_bitvec([65535], [true]));
1668			assert_eq!(test_instance[1], ColumnBuffer::uint2_with_bitvec([0], [false]));
1669		}
1670
1671		#[test]
1672		fn test_fallback_uint4() {
1673			let mut test_instance = Columns::new(vec![
1674				ColumnWithName::uint4("test_col", Vec::<u32>::new()),
1675				ColumnWithName::uint4("none", Vec::<u32>::new()),
1676			]);
1677
1678			let shape = RowShape::testing(&[Type::Uint4, Type::Uint4]);
1679			let mut row = shape.allocate();
1680			shape.set_u32(&mut row, 0, 4294967295u32);
1681			shape.set_none(&mut row, 1);
1682
1683			test_instance.append_rows(&shape, [row], vec![]).unwrap();
1684
1685			assert_eq!(test_instance[0], ColumnBuffer::uint4_with_bitvec([4294967295], [true]));
1686			assert_eq!(test_instance[1], ColumnBuffer::uint4_with_bitvec([0], [false]));
1687		}
1688
1689		#[test]
1690		fn test_fallback_uint8() {
1691			let mut test_instance = Columns::new(vec![
1692				ColumnWithName::uint8("test_col", Vec::<u64>::new()),
1693				ColumnWithName::uint8("none", Vec::<u64>::new()),
1694			]);
1695
1696			let shape = RowShape::testing(&[Type::Uint8, Type::Uint8]);
1697			let mut row = shape.allocate();
1698			shape.set_u64(&mut row, 0, 18446744073709551615u64);
1699			shape.set_none(&mut row, 1);
1700
1701			test_instance.append_rows(&shape, [row], vec![]).unwrap();
1702
1703			assert_eq!(test_instance[0], ColumnBuffer::uint8_with_bitvec([18446744073709551615], [true]));
1704			assert_eq!(test_instance[1], ColumnBuffer::uint8_with_bitvec([0], [false]));
1705		}
1706
1707		#[test]
1708		fn test_fallback_uint16() {
1709			let mut test_instance = Columns::new(vec![
1710				ColumnWithName::uint16("test_col", Vec::<u128>::new()),
1711				ColumnWithName::uint16("none", Vec::<u128>::new()),
1712			]);
1713
1714			let shape = RowShape::testing(&[Type::Uint16, Type::Uint16]);
1715			let mut row = shape.allocate();
1716			shape.set_u128(&mut row, 0, 340282366920938463463374607431768211455u128);
1717			shape.set_none(&mut row, 1);
1718
1719			test_instance.append_rows(&shape, [row], vec![]).unwrap();
1720
1721			assert_eq!(
1722				test_instance[0],
1723				ColumnBuffer::uint16_with_bitvec([340282366920938463463374607431768211455u128], [true])
1724			);
1725			assert_eq!(test_instance[1], ColumnBuffer::uint16_with_bitvec([0], [false]));
1726		}
1727
1728		#[test]
1729		fn test_all_defined_dictionary_id() {
1730			let constraint = TypeConstraint::dictionary(DictionaryId::from(1u64), Type::Uint4);
1731			let shape = RowShape::new(vec![RowShapeField::new("status", constraint)]);
1732
1733			let mut test_instance = Columns::new(vec![ColumnWithName::dictionary_id(
1734				"status",
1735				Vec::<DictionaryEntryId>::new(),
1736			)]);
1737
1738			let mut row_one = shape.allocate();
1739			shape.set_values(&mut row_one, &[Value::DictionaryId(DictionaryEntryId::U4(10))]);
1740			let mut row_two = shape.allocate();
1741			shape.set_values(&mut row_two, &[Value::DictionaryId(DictionaryEntryId::U4(20))]);
1742
1743			test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
1744
1745			assert_eq!(test_instance[0].get_value(0), Value::DictionaryId(DictionaryEntryId::U4(10)));
1746			assert_eq!(test_instance[0].get_value(1), Value::DictionaryId(DictionaryEntryId::U4(20)));
1747		}
1748
1749		#[test]
1750		fn test_fallback_dictionary_id() {
1751			let dict_constraint = TypeConstraint::dictionary(DictionaryId::from(1u64), Type::Uint4);
1752			let shape = RowShape::new(vec![
1753				RowShapeField::new("dict_col", dict_constraint),
1754				RowShapeField::unconstrained("bool_col", Type::Boolean),
1755			]);
1756
1757			let mut test_instance = Columns::new(vec![
1758				ColumnWithName::dictionary_id("dict_col", Vec::<DictionaryEntryId>::new()),
1759				ColumnWithName::bool("bool_col", Vec::<bool>::new()),
1760			]);
1761
1762			let mut row = shape.allocate();
1763			shape.set_values(&mut row, &[Value::none(), Value::Boolean(true)]);
1764
1765			test_instance.append_rows(&shape, [row], vec![]).unwrap();
1766
1767			// Dictionary column should be undefined
1768			assert!(!test_instance[0].is_defined(0));
1769			// Bool column should be defined
1770			assert_eq!(test_instance[1].get_value(0), Value::Boolean(true));
1771		}
1772
1773		#[test]
1774		fn test_before_undefined_dictionary_id() {
1775			let constraint = TypeConstraint::dictionary(DictionaryId::from(2u64), Type::Uint4);
1776			let shape = RowShape::new(vec![RowShapeField::new("tag", constraint)]);
1777
1778			let mut test_instance =
1779				Columns::new(vec![ColumnWithName::undefined_typed("tag", Type::Boolean, 2)]);
1780
1781			let mut row = shape.allocate();
1782			shape.set_values(&mut row, &[Value::DictionaryId(DictionaryEntryId::U4(5))]);
1783
1784			test_instance.append_rows(&shape, [row], vec![]).unwrap();
1785
1786			// First two are undefined (promoted from Undefined column), third is defined
1787			assert!(!test_instance[0].is_defined(0));
1788			assert!(!test_instance[0].is_defined(1));
1789			assert!(test_instance[0].is_defined(2));
1790			assert_eq!(test_instance[0].get_value(2), Value::DictionaryId(DictionaryEntryId::U4(5)));
1791		}
1792
1793		#[test]
1794		fn test_all_defined_identity_id() {
1795			let id1 = IdentityId::anonymous();
1796			let id2 = IdentityId::root();
1797
1798			let shape = RowShape::testing(&[Type::IdentityId]);
1799			let mut test_instance = Columns::new(vec![ColumnWithName::new(
1800				Fragment::internal("id_col"),
1801				ColumnBuffer::identity_id(Vec::<IdentityId>::new()),
1802			)]);
1803
1804			let mut row_one = shape.allocate();
1805			shape.set_identity_id(&mut row_one, 0, id1);
1806			let mut row_two = shape.allocate();
1807			shape.set_identity_id(&mut row_two, 0, id2);
1808
1809			test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
1810
1811			assert_eq!(test_instance[0].get_value(0), Value::IdentityId(id1));
1812			assert_eq!(test_instance[0].get_value(1), Value::IdentityId(id2));
1813		}
1814
1815		#[test]
1816		fn test_fallback_identity_id() {
1817			let id = IdentityId::anonymous();
1818
1819			let shape = RowShape::testing(&[Type::IdentityId, Type::Boolean]);
1820			let mut test_instance = Columns::new(vec![
1821				ColumnWithName::new(
1822					Fragment::internal("id_col"),
1823					ColumnBuffer::identity_id(Vec::<IdentityId>::new()),
1824				),
1825				ColumnWithName::bool("bool_col", Vec::<bool>::new()),
1826			]);
1827
1828			let mut row = shape.allocate();
1829			shape.set_identity_id(&mut row, 0, id);
1830			shape.set_none(&mut row, 1);
1831
1832			test_instance.append_rows(&shape, [row], vec![]).unwrap();
1833
1834			assert_eq!(test_instance[0].get_value(0), Value::IdentityId(id));
1835			assert!(test_instance[0].is_defined(0));
1836			assert!(!test_instance[1].is_defined(0));
1837		}
1838
1839		#[test]
1840		fn test_all_defined_blob() {
1841			let blob1 = Blob::new(vec![1, 2, 3]);
1842			let blob2 = Blob::new(vec![4, 5]);
1843
1844			let shape = RowShape::testing(&[Type::Blob]);
1845			let mut test_instance = Columns::new(vec![ColumnWithName::new(
1846				Fragment::internal("blob_col"),
1847				ColumnBuffer::blob(Vec::<Blob>::new()),
1848			)]);
1849
1850			let mut row_one = shape.allocate();
1851			shape.set_blob(&mut row_one, 0, &blob1);
1852			let mut row_two = shape.allocate();
1853			shape.set_blob(&mut row_two, 0, &blob2);
1854
1855			test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
1856
1857			assert_eq!(test_instance[0].get_value(0), Value::Blob(blob1));
1858			assert_eq!(test_instance[0].get_value(1), Value::Blob(blob2));
1859		}
1860
1861		#[test]
1862		fn test_fallback_blob() {
1863			let blob = Blob::new(vec![10, 20, 30]);
1864
1865			let shape = RowShape::testing(&[Type::Blob, Type::Boolean]);
1866			let mut test_instance = Columns::new(vec![
1867				ColumnWithName::new(
1868					Fragment::internal("blob_col"),
1869					ColumnBuffer::blob(Vec::<Blob>::new()),
1870				),
1871				ColumnWithName::bool("bool_col", Vec::<bool>::new()),
1872			]);
1873
1874			let mut row = shape.allocate();
1875			shape.set_blob(&mut row, 0, &blob);
1876			shape.set_none(&mut row, 1);
1877
1878			test_instance.append_rows(&shape, [row], vec![]).unwrap();
1879
1880			assert_eq!(test_instance[0].get_value(0), Value::Blob(blob));
1881			assert!(test_instance[0].is_defined(0));
1882			assert!(!test_instance[1].is_defined(0));
1883		}
1884
1885		fn test_instance_with_columns() -> Columns {
1886			Columns::new(vec![
1887				ColumnWithName::new(Fragment::internal("int2"), ColumnBuffer::int2(vec![1])),
1888				ColumnWithName::new(Fragment::internal("bool"), ColumnBuffer::bool(vec![true])),
1889			])
1890		}
1891	}
1892}