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