use reifydb_type::{
Result,
storage::DataBitVec,
util::bitvec::BitVec,
value::{
Value,
blob::Blob,
constraint::Constraint,
date::Date,
datetime::DateTime,
decimal::Decimal,
duration::Duration,
int::Int,
row_number::RowNumber,
time::Time,
r#type::Type,
uint::Uint,
uuid::{Uuid4, Uuid7},
},
};
use uuid::Uuid;
use crate::{
encoded::{row::EncodedRow, shape::RowShape},
error::CoreError,
value::column::{ColumnBuffer, columns::Columns},
};
impl Columns {
pub fn append_columns(&mut self, other: Columns) -> Result<()> {
if self.len() != other.len() {
return Err(CoreError::FrameError {
message: "mismatched column count".to_string(),
}
.into());
}
if !other.row_numbers.is_empty() {
self.row_numbers.make_mut().extend(other.row_numbers.iter().copied());
}
if !other.created_at.is_empty() {
self.created_at.make_mut().extend(other.created_at.iter().copied());
}
if !other.updated_at.is_empty() {
self.updated_at.make_mut().extend(other.updated_at.iter().copied());
}
for i in 0..self.columns.len() {
let self_name = self.names[i].text().to_string();
let other_name = other.names[i].text().to_string();
if self_name != other_name {
return Err(CoreError::FrameError {
message: format!(
"column name mismatch at index {}: '{}' vs '{}'",
i, self_name, other_name,
),
}
.into());
}
let other_data = other.columns[i].clone();
self.columns.make_mut()[i].extend(other_data)?;
}
Ok(())
}
}
impl Columns {
pub fn append_rows(
&mut self,
shape: &RowShape,
rows: impl IntoIterator<Item = EncodedRow>,
row_numbers: Vec<RowNumber>,
) -> Result<()> {
if self.len() != shape.field_count() {
return Err(CoreError::FrameError {
message: format!(
"mismatched column count: expected {}, got {}",
self.len(),
shape.field_count()
),
}
.into());
}
let rows: Vec<EncodedRow> = rows.into_iter().collect();
if !row_numbers.is_empty() && row_numbers.len() != rows.len() {
return Err(CoreError::FrameError {
message: format!(
"row_numbers length {} does not match rows length {}",
row_numbers.len(),
rows.len()
),
}
.into());
}
if !row_numbers.is_empty() {
self.row_numbers.make_mut().extend(row_numbers);
}
for row in &rows {
self.created_at.make_mut().push(DateTime::from_nanos(row.created_at_nanos()));
self.updated_at.make_mut().push(DateTime::from_nanos(row.updated_at_nanos()));
}
let columns = self.columns.make_mut();
for (index, column) in columns.iter_mut().enumerate() {
let field = shape.get_field(index).unwrap();
let is_all_none = if let ColumnBuffer::Option {
bitvec,
..
} = &*column
{
DataBitVec::count_ones(bitvec) == 0
} else {
false
};
if is_all_none {
let size = column.len();
let new_data = match field.constraint.get_type() {
Type::Boolean => ColumnBuffer::bool_with_bitvec(
vec![false; size],
BitVec::repeat(size, false),
),
Type::Float4 => ColumnBuffer::float4_with_bitvec(
vec![0.0f32; size],
BitVec::repeat(size, false),
),
Type::Float8 => ColumnBuffer::float8_with_bitvec(
vec![0.0f64; size],
BitVec::repeat(size, false),
),
Type::Int1 => ColumnBuffer::int1_with_bitvec(
vec![0i8; size],
BitVec::repeat(size, false),
),
Type::Int2 => ColumnBuffer::int2_with_bitvec(
vec![0i16; size],
BitVec::repeat(size, false),
),
Type::Int4 => ColumnBuffer::int4_with_bitvec(
vec![0i32; size],
BitVec::repeat(size, false),
),
Type::Int8 => ColumnBuffer::int8_with_bitvec(
vec![0i64; size],
BitVec::repeat(size, false),
),
Type::Int16 => ColumnBuffer::int16_with_bitvec(
vec![0i128; size],
BitVec::repeat(size, false),
),
Type::Utf8 => ColumnBuffer::utf8_with_bitvec(
vec![String::new(); size],
BitVec::repeat(size, false),
),
Type::Uint1 => ColumnBuffer::uint1_with_bitvec(
vec![0u8; size],
BitVec::repeat(size, false),
),
Type::Uint2 => ColumnBuffer::uint2_with_bitvec(
vec![0u16; size],
BitVec::repeat(size, false),
),
Type::Uint4 => ColumnBuffer::uint4_with_bitvec(
vec![0u32; size],
BitVec::repeat(size, false),
),
Type::Uint8 => ColumnBuffer::uint8_with_bitvec(
vec![0u64; size],
BitVec::repeat(size, false),
),
Type::Uint16 => ColumnBuffer::uint16_with_bitvec(
vec![0u128; size],
BitVec::repeat(size, false),
),
Type::Date => ColumnBuffer::date_with_bitvec(
vec![Date::default(); size],
BitVec::repeat(size, false),
),
Type::DateTime => ColumnBuffer::datetime_with_bitvec(
vec![DateTime::default(); size],
BitVec::repeat(size, false),
),
Type::Time => ColumnBuffer::time_with_bitvec(
vec![Time::default(); size],
BitVec::repeat(size, false),
),
Type::Duration => ColumnBuffer::duration_with_bitvec(
vec![Duration::default(); size],
BitVec::repeat(size, false),
),
Type::Option(_) => column.clone(),
Type::IdentityId => ColumnBuffer::identity_id_with_bitvec(
vec![Default::default(); size],
BitVec::repeat(size, false),
),
Type::Uuid4 => ColumnBuffer::uuid4_with_bitvec(
vec![Uuid4::from(Uuid::nil()); size],
BitVec::repeat(size, false),
),
Type::Uuid7 => ColumnBuffer::uuid7_with_bitvec(
vec![Uuid7::from(Uuid::nil()); size],
BitVec::repeat(size, false),
),
Type::Blob => ColumnBuffer::blob_with_bitvec(
vec![Blob::new(vec![]); size],
BitVec::repeat(size, false),
),
Type::Int => ColumnBuffer::int_with_bitvec(
vec![Int::default(); size],
BitVec::repeat(size, false),
),
Type::Uint => ColumnBuffer::uint_with_bitvec(
vec![Uint::default(); size],
BitVec::repeat(size, false),
),
Type::Decimal => ColumnBuffer::decimal_with_bitvec(
vec![Decimal::from(0); size],
BitVec::repeat(size, false),
),
Type::DictionaryId => {
let mut col_data = ColumnBuffer::dictionary_id_with_bitvec(
vec![Default::default(); size],
BitVec::repeat(size, false),
);
if let ColumnBuffer::DictionaryId(container) = &mut col_data
&& let Some(Constraint::Dictionary(dict_id, _)) =
field.constraint.constraint()
{
container.set_dictionary_id(*dict_id);
}
col_data
}
Type::Any | Type::List(_) | Type::Record(_) | Type::Tuple(_) => {
ColumnBuffer::any_with_bitvec(
vec![Box::new(Value::none()); size],
BitVec::repeat(size, false),
)
}
};
*column = new_data;
}
if let ColumnBuffer::DictionaryId(container) = &mut *column
&& container.dictionary_id().is_none()
&& let Some(Constraint::Dictionary(dict_id, _)) = field.constraint.constraint()
{
container.set_dictionary_id(*dict_id);
}
}
for row in &rows {
let all_defined = (0..shape.field_count()).all(|i| row.is_defined(i));
if all_defined {
self.append_all_defined_from_shape(shape, row)?;
} else {
self.append_fallback_from_shape(shape, row)?;
}
}
Ok(())
}
fn append_all_defined_from_shape(&mut self, shape: &RowShape, row: &EncodedRow) -> Result<()> {
let names_snapshot: Vec<String> = self.names.iter().map(|n| n.text().to_string()).collect();
let columns = self.columns.make_mut();
for (index, column) in columns.iter_mut().enumerate() {
let field = shape.get_field(index).unwrap();
match (&mut *column, field.constraint.get_type()) {
(
ColumnBuffer::Option {
inner,
bitvec,
},
_ty,
) => {
let value = shape.get_value(row, index);
if matches!(value, Value::None { .. }) {
inner.push_none();
DataBitVec::push(bitvec, false);
} else {
inner.push_value(value);
DataBitVec::push(bitvec, true);
}
}
(ColumnBuffer::Bool(container), Type::Boolean) => {
container.push(shape.get_bool(row, index));
}
(ColumnBuffer::Float4(container), Type::Float4) => {
container.push(shape.get_f32(row, index));
}
(ColumnBuffer::Float8(container), Type::Float8) => {
container.push(shape.get_f64(row, index));
}
(ColumnBuffer::Int1(container), Type::Int1) => {
container.push(shape.get_i8(row, index));
}
(ColumnBuffer::Int2(container), Type::Int2) => {
container.push(shape.get_i16(row, index));
}
(ColumnBuffer::Int4(container), Type::Int4) => {
container.push(shape.get_i32(row, index));
}
(ColumnBuffer::Int8(container), Type::Int8) => {
container.push(shape.get_i64(row, index));
}
(ColumnBuffer::Int16(container), Type::Int16) => {
container.push(shape.get_i128(row, index));
}
(
ColumnBuffer::Utf8 {
container,
..
},
Type::Utf8,
) => {
container.push(shape.get_utf8(row, index).to_string());
}
(ColumnBuffer::Uint1(container), Type::Uint1) => {
container.push(shape.get_u8(row, index));
}
(ColumnBuffer::Uint2(container), Type::Uint2) => {
container.push(shape.get_u16(row, index));
}
(ColumnBuffer::Uint4(container), Type::Uint4) => {
container.push(shape.get_u32(row, index));
}
(ColumnBuffer::Uint8(container), Type::Uint8) => {
container.push(shape.get_u64(row, index));
}
(ColumnBuffer::Uint16(container), Type::Uint16) => {
container.push(shape.get_u128(row, index));
}
(ColumnBuffer::Date(container), Type::Date) => {
container.push(shape.get_date(row, index));
}
(ColumnBuffer::DateTime(container), Type::DateTime) => {
container.push(shape.get_datetime(row, index));
}
(ColumnBuffer::Time(container), Type::Time) => {
container.push(shape.get_time(row, index));
}
(ColumnBuffer::Duration(container), Type::Duration) => {
container.push(shape.get_duration(row, index));
}
(ColumnBuffer::Uuid4(container), Type::Uuid4) => {
container.push(shape.get_uuid4(row, index));
}
(ColumnBuffer::Uuid7(container), Type::Uuid7) => {
container.push(shape.get_uuid7(row, index));
}
(ColumnBuffer::IdentityId(container), Type::IdentityId) => {
container.push(shape.get_identity_id(row, index));
}
(
ColumnBuffer::Blob {
container,
..
},
Type::Blob,
) => {
container.push(shape.get_blob(row, index));
}
(
ColumnBuffer::Int {
container,
..
},
Type::Int,
) => {
container.push(shape.get_int(row, index));
}
(
ColumnBuffer::Uint {
container,
..
},
Type::Uint,
) => {
container.push(shape.get_uint(row, index));
}
(
ColumnBuffer::Decimal {
container,
..
},
Type::Decimal,
) => {
container.push(shape.get_decimal(row, index));
}
(ColumnBuffer::DictionaryId(container), Type::DictionaryId) => {
match shape.get_value(row, index) {
Value::DictionaryId(id) => container.push(id),
_ => container.push_default(),
}
}
(_, v) => {
return Err(CoreError::FrameError {
message: format!(
"type mismatch for column '{}'({}): incompatible with value {}",
names_snapshot[index],
column.get_type(),
v
),
}
.into());
}
}
}
Ok(())
}
fn append_fallback_from_shape(&mut self, shape: &RowShape, row: &EncodedRow) -> Result<()> {
let columns = self.columns.make_mut();
for (index, column) in columns.iter_mut().enumerate() {
let field = shape.get_field(index).unwrap();
if !row.is_defined(index) {
column.push_none();
continue;
}
match (&mut *column, field.constraint.get_type()) {
(
ColumnBuffer::Option {
inner,
bitvec,
},
_ty,
) => {
let value = shape.get_value(row, index);
inner.push_value(value);
DataBitVec::push(bitvec, true);
}
(ColumnBuffer::Bool(container), Type::Boolean) => {
container.push(shape.get_bool(row, index));
}
(ColumnBuffer::Float4(container), Type::Float4) => {
container.push(shape.get_f32(row, index));
}
(ColumnBuffer::Float8(container), Type::Float8) => {
container.push(shape.get_f64(row, index));
}
(ColumnBuffer::Int1(container), Type::Int1) => {
container.push(shape.get_i8(row, index));
}
(ColumnBuffer::Int2(container), Type::Int2) => {
container.push(shape.get_i16(row, index));
}
(ColumnBuffer::Int4(container), Type::Int4) => {
container.push(shape.get_i32(row, index));
}
(ColumnBuffer::Int8(container), Type::Int8) => {
container.push(shape.get_i64(row, index));
}
(ColumnBuffer::Int16(container), Type::Int16) => {
container.push(shape.get_i128(row, index));
}
(
ColumnBuffer::Utf8 {
container,
..
},
Type::Utf8,
) => {
container.push(shape.get_utf8(row, index).to_string());
}
(ColumnBuffer::Uint1(container), Type::Uint1) => {
container.push(shape.get_u8(row, index));
}
(ColumnBuffer::Uint2(container), Type::Uint2) => {
container.push(shape.get_u16(row, index));
}
(ColumnBuffer::Uint4(container), Type::Uint4) => {
container.push(shape.get_u32(row, index));
}
(ColumnBuffer::Uint8(container), Type::Uint8) => {
container.push(shape.get_u64(row, index));
}
(ColumnBuffer::Uint16(container), Type::Uint16) => {
container.push(shape.get_u128(row, index));
}
(ColumnBuffer::Date(container), Type::Date) => {
container.push(shape.get_date(row, index));
}
(ColumnBuffer::DateTime(container), Type::DateTime) => {
container.push(shape.get_datetime(row, index));
}
(ColumnBuffer::Time(container), Type::Time) => {
container.push(shape.get_time(row, index));
}
(ColumnBuffer::Duration(container), Type::Duration) => {
container.push(shape.get_duration(row, index));
}
(ColumnBuffer::Uuid4(container), Type::Uuid4) => {
container.push(shape.get_uuid4(row, index));
}
(ColumnBuffer::Uuid7(container), Type::Uuid7) => {
container.push(shape.get_uuid7(row, index));
}
(ColumnBuffer::IdentityId(container), Type::IdentityId) => {
container.push(shape.get_identity_id(row, index));
}
(
ColumnBuffer::Blob {
container,
..
},
Type::Blob,
) => {
container.push(shape.get_blob(row, index));
}
(
ColumnBuffer::Int {
container,
..
},
Type::Int,
) => {
container.push(shape.get_int(row, index));
}
(
ColumnBuffer::Uint {
container,
..
},
Type::Uint,
) => {
container.push(shape.get_uint(row, index));
}
(
ColumnBuffer::Decimal {
container,
..
},
Type::Decimal,
) => {
container.push(shape.get_decimal(row, index));
}
(ColumnBuffer::DictionaryId(container), Type::DictionaryId) => {
match shape.get_value(row, index) {
Value::DictionaryId(id) => container.push(id),
_ => container.push_default(),
}
}
(l, r) => unreachable!("{:#?} {:#?}", l, r),
}
}
Ok(())
}
}
#[cfg(test)]
pub mod tests {
mod columns {
use reifydb_type::value::{
r#type::Type,
uuid::{Uuid4, Uuid7},
};
use uuid::{Timestamp, Uuid};
use crate::value::column::{ColumnBuffer, ColumnWithName, columns::Columns};
#[test]
fn test_boolean() {
let mut test_instance1 =
Columns::new(vec![ColumnWithName::bool_with_bitvec("id", [true], [false])]);
let test_instance2 =
Columns::new(vec![ColumnWithName::bool_with_bitvec("id", [false], [true])]);
test_instance1.append_columns(test_instance2).unwrap();
assert_eq!(test_instance1[0], ColumnBuffer::bool_with_bitvec([true, false], [false, true]));
}
#[test]
fn test_float4() {
let mut test_instance1 = Columns::new(vec![ColumnWithName::float4("id", [1.0f32, 2.0])]);
let test_instance2 = Columns::new(vec![ColumnWithName::float4_with_bitvec(
"id",
[3.0f32, 4.0],
[true, false],
)]);
test_instance1.append_columns(test_instance2).unwrap();
assert_eq!(
test_instance1[0],
ColumnBuffer::float4_with_bitvec([1.0f32, 2.0, 3.0, 4.0], [true, true, true, false])
);
}
#[test]
fn test_float8() {
let mut test_instance1 = Columns::new(vec![ColumnWithName::float8("id", [1.0f64, 2.0])]);
let test_instance2 = Columns::new(vec![ColumnWithName::float8_with_bitvec(
"id",
[3.0f64, 4.0],
[true, false],
)]);
test_instance1.append_columns(test_instance2).unwrap();
assert_eq!(
test_instance1[0],
ColumnBuffer::float8_with_bitvec([1.0f64, 2.0, 3.0, 4.0], [true, true, true, false])
);
}
#[test]
fn test_int1() {
let mut test_instance1 = Columns::new(vec![ColumnWithName::int1("id", [1, 2])]);
let test_instance2 =
Columns::new(vec![ColumnWithName::int1_with_bitvec("id", [3, 4], [true, false])]);
test_instance1.append_columns(test_instance2).unwrap();
assert_eq!(
test_instance1[0],
ColumnBuffer::int1_with_bitvec([1, 2, 3, 4], [true, true, true, false])
);
}
#[test]
fn test_int2() {
let mut test_instance1 = Columns::new(vec![ColumnWithName::int2("id", [1, 2])]);
let test_instance2 =
Columns::new(vec![ColumnWithName::int2_with_bitvec("id", [3, 4], [true, false])]);
test_instance1.append_columns(test_instance2).unwrap();
assert_eq!(
test_instance1[0],
ColumnBuffer::int2_with_bitvec([1, 2, 3, 4], [true, true, true, false])
);
}
#[test]
fn test_int4() {
let mut test_instance1 = Columns::new(vec![ColumnWithName::int4("id", [1, 2])]);
let test_instance2 =
Columns::new(vec![ColumnWithName::int4_with_bitvec("id", [3, 4], [true, false])]);
test_instance1.append_columns(test_instance2).unwrap();
assert_eq!(
test_instance1[0],
ColumnBuffer::int4_with_bitvec([1, 2, 3, 4], [true, true, true, false])
);
}
#[test]
fn test_int8() {
let mut test_instance1 = Columns::new(vec![ColumnWithName::int8("id", [1, 2])]);
let test_instance2 =
Columns::new(vec![ColumnWithName::int8_with_bitvec("id", [3, 4], [true, false])]);
test_instance1.append_columns(test_instance2).unwrap();
assert_eq!(
test_instance1[0],
ColumnBuffer::int8_with_bitvec([1, 2, 3, 4], [true, true, true, false])
);
}
#[test]
fn test_int16() {
let mut test_instance1 = Columns::new(vec![ColumnWithName::int16("id", [1, 2])]);
let test_instance2 =
Columns::new(vec![ColumnWithName::int16_with_bitvec("id", [3, 4], [true, false])]);
test_instance1.append_columns(test_instance2).unwrap();
assert_eq!(
test_instance1[0],
ColumnBuffer::int16_with_bitvec([1, 2, 3, 4], [true, true, true, false])
);
}
#[test]
fn test_string() {
let mut test_instance1 = Columns::new(vec![ColumnWithName::utf8_with_bitvec(
"id",
vec!["a".to_string(), "b".to_string()],
[true, true],
)]);
let test_instance2 = Columns::new(vec![ColumnWithName::utf8_with_bitvec(
"id",
vec!["c".to_string(), "d".to_string()],
[true, false],
)]);
test_instance1.append_columns(test_instance2).unwrap();
assert_eq!(
test_instance1[0],
ColumnBuffer::utf8_with_bitvec(
vec!["a".to_string(), "b".to_string(), "c".to_string(), "d".to_string()],
vec![true, true, true, false]
)
);
}
#[test]
fn test_uint1() {
let mut test_instance1 = Columns::new(vec![ColumnWithName::uint1("id", [1, 2])]);
let test_instance2 =
Columns::new(vec![ColumnWithName::uint1_with_bitvec("id", [3, 4], [true, false])]);
test_instance1.append_columns(test_instance2).unwrap();
assert_eq!(
test_instance1[0],
ColumnBuffer::uint1_with_bitvec([1, 2, 3, 4], [true, true, true, false])
);
}
#[test]
fn test_uint2() {
let mut test_instance1 = Columns::new(vec![ColumnWithName::uint2("id", [1, 2])]);
let test_instance2 =
Columns::new(vec![ColumnWithName::uint2_with_bitvec("id", [3, 4], [true, false])]);
test_instance1.append_columns(test_instance2).unwrap();
assert_eq!(
test_instance1[0],
ColumnBuffer::uint2_with_bitvec([1, 2, 3, 4], [true, true, true, false])
);
}
#[test]
fn test_uint4() {
let mut test_instance1 = Columns::new(vec![ColumnWithName::uint4("id", [1, 2])]);
let test_instance2 =
Columns::new(vec![ColumnWithName::uint4_with_bitvec("id", [3, 4], [true, false])]);
test_instance1.append_columns(test_instance2).unwrap();
assert_eq!(
test_instance1[0],
ColumnBuffer::uint4_with_bitvec([1, 2, 3, 4], [true, true, true, false])
);
}
#[test]
fn test_uint8() {
let mut test_instance1 = Columns::new(vec![ColumnWithName::uint8("id", [1, 2])]);
let test_instance2 =
Columns::new(vec![ColumnWithName::uint8_with_bitvec("id", [3, 4], [true, false])]);
test_instance1.append_columns(test_instance2).unwrap();
assert_eq!(
test_instance1[0],
ColumnBuffer::uint8_with_bitvec([1, 2, 3, 4], [true, true, true, false])
);
}
#[test]
fn test_uint16() {
let mut test_instance1 = Columns::new(vec![ColumnWithName::uint16("id", [1, 2])]);
let test_instance2 =
Columns::new(vec![ColumnWithName::uint16_with_bitvec("id", [3, 4], [true, false])]);
test_instance1.append_columns(test_instance2).unwrap();
assert_eq!(
test_instance1[0],
ColumnBuffer::uint16_with_bitvec([1, 2, 3, 4], [true, true, true, false])
);
}
#[test]
fn test_uuid4() {
let uuid1 = Uuid4::from(Uuid::new_v4());
let uuid2 = Uuid4::from(Uuid::new_v4());
let uuid3 = Uuid4::from(Uuid::new_v4());
let uuid4 = Uuid4::from(Uuid::new_v4());
let mut test_instance1 = Columns::new(vec![ColumnWithName::uuid4("id", [uuid1, uuid2])]);
let test_instance2 = Columns::new(vec![ColumnWithName::uuid4_with_bitvec(
"id",
[uuid3, uuid4],
[true, false],
)]);
test_instance1.append_columns(test_instance2).unwrap();
assert_eq!(
test_instance1[0],
ColumnBuffer::uuid4_with_bitvec(
[uuid1, uuid2, uuid3, uuid4],
[true, true, true, false]
)
);
}
#[test]
fn test_uuid7() {
let uuid1 = Uuid7::from(Uuid::new_v7(Timestamp::from_gregorian_time(1, 1)));
let uuid2 = Uuid7::from(Uuid::new_v7(Timestamp::from_gregorian_time(1, 2)));
let uuid3 = Uuid7::from(Uuid::new_v7(Timestamp::from_gregorian_time(2, 1)));
let uuid4 = Uuid7::from(Uuid::new_v7(Timestamp::from_gregorian_time(2, 2)));
let mut test_instance1 = Columns::new(vec![ColumnWithName::uuid7("id", [uuid1, uuid2])]);
let test_instance2 = Columns::new(vec![ColumnWithName::uuid7_with_bitvec(
"id",
[uuid3, uuid4],
[true, false],
)]);
test_instance1.append_columns(test_instance2).unwrap();
assert_eq!(
test_instance1[0],
ColumnBuffer::uuid7_with_bitvec(
[uuid1, uuid2, uuid3, uuid4],
[true, true, true, false]
)
);
}
#[test]
fn test_with_undefined_lr_promotes_correctly() {
let mut test_instance1 =
Columns::new(vec![ColumnWithName::int2_with_bitvec("id", [1, 2], [true, false])]);
let test_instance2 =
Columns::new(vec![ColumnWithName::undefined_typed("id", Type::Boolean, 2)]);
test_instance1.append_columns(test_instance2).unwrap();
assert_eq!(
test_instance1[0],
ColumnBuffer::int2_with_bitvec([1, 2, 0, 0], [true, false, false, false])
);
}
#[test]
fn test_with_undefined_l_promotes_correctly() {
let mut test_instance1 =
Columns::new(vec![ColumnWithName::undefined_typed("score", Type::Boolean, 2)]);
let test_instance2 =
Columns::new(vec![ColumnWithName::int2_with_bitvec("score", [10, 20], [true, false])]);
test_instance1.append_columns(test_instance2).unwrap();
assert_eq!(
test_instance1[0],
ColumnBuffer::int2_with_bitvec([0, 0, 10, 20], [false, false, true, false])
);
}
#[test]
fn test_fails_on_column_count_mismatch() {
let mut test_instance1 = Columns::new(vec![ColumnWithName::int2("id", [1])]);
let test_instance2 = Columns::new(vec![
ColumnWithName::int2("id", [2]),
ColumnWithName::utf8("name", vec!["Bob".to_string()]),
]);
let result = test_instance1.append_columns(test_instance2);
assert!(result.is_err());
}
#[test]
fn test_fails_on_column_name_mismatch() {
let mut test_instance1 = Columns::new(vec![ColumnWithName::int2("id", [1])]);
let test_instance2 = Columns::new(vec![ColumnWithName::int2("wrong", [2])]);
let result = test_instance1.append_columns(test_instance2);
assert!(result.is_err());
}
#[test]
fn test_fails_on_type_mismatch() {
let mut test_instance1 = Columns::new(vec![ColumnWithName::int2("id", [1])]);
let test_instance2 = Columns::new(vec![ColumnWithName::utf8("id", vec!["A".to_string()])]);
let result = test_instance1.append_columns(test_instance2);
assert!(result.is_err());
}
}
mod row {
use reifydb_type::{
fragment::Fragment,
util::bitvec::BitVec,
value::{
Value,
blob::Blob,
constraint::TypeConstraint,
dictionary::{DictionaryEntryId, DictionaryId},
identity::IdentityId,
ordered_f32::OrderedF32,
ordered_f64::OrderedF64,
r#type::Type,
},
};
use crate::{
encoded::shape::{RowShape, RowShapeField},
value::column::{ColumnBuffer, ColumnWithName, columns::Columns},
};
#[test]
fn test_before_undefined_bool() {
let mut test_instance =
Columns::new(vec![ColumnWithName::undefined_typed("test_col", Type::Boolean, 2)]);
let shape = RowShape::testing(&[Type::Boolean]);
let mut row = shape.allocate();
shape.set_values(&mut row, &[Value::Boolean(true)]);
test_instance.append_rows(&shape, [row], vec![]).unwrap();
assert_eq!(
test_instance[0],
ColumnBuffer::bool_with_bitvec(
[false, false, true],
BitVec::from_slice(&[false, false, true])
)
);
}
#[test]
fn test_before_undefined_float4() {
let mut test_instance =
Columns::new(vec![ColumnWithName::undefined_typed("test_col", Type::Boolean, 2)]);
let shape = RowShape::testing(&[Type::Float4]);
let mut row = shape.allocate();
shape.set_values(&mut row, &[Value::Float4(OrderedF32::try_from(1.5).unwrap())]);
test_instance.append_rows(&shape, [row], vec![]).unwrap();
assert_eq!(
test_instance[0],
ColumnBuffer::float4_with_bitvec(
[0.0, 0.0, 1.5],
BitVec::from_slice(&[false, false, true])
)
);
}
#[test]
fn test_before_undefined_float8() {
let mut test_instance =
Columns::new(vec![ColumnWithName::undefined_typed("test_col", Type::Boolean, 2)]);
let shape = RowShape::testing(&[Type::Float8]);
let mut row = shape.allocate();
shape.set_values(&mut row, &[Value::Float8(OrderedF64::try_from(2.25).unwrap())]);
test_instance.append_rows(&shape, [row], vec![]).unwrap();
assert_eq!(
test_instance[0],
ColumnBuffer::float8_with_bitvec(
[0.0, 0.0, 2.25],
BitVec::from_slice(&[false, false, true])
)
);
}
#[test]
fn test_before_undefined_int1() {
let mut test_instance =
Columns::new(vec![ColumnWithName::undefined_typed("test_col", Type::Boolean, 2)]);
let shape = RowShape::testing(&[Type::Int1]);
let mut row = shape.allocate();
shape.set_values(&mut row, &[Value::Int1(42)]);
test_instance.append_rows(&shape, [row], vec![]).unwrap();
assert_eq!(
test_instance[0],
ColumnBuffer::int1_with_bitvec([0, 0, 42], BitVec::from_slice(&[false, false, true]))
);
}
#[test]
fn test_before_undefined_int2() {
let mut test_instance =
Columns::new(vec![ColumnWithName::undefined_typed("test_col", Type::Boolean, 2)]);
let shape = RowShape::testing(&[Type::Int2]);
let mut row = shape.allocate();
shape.set_values(&mut row, &[Value::Int2(-1234)]);
test_instance.append_rows(&shape, [row], vec![]).unwrap();
assert_eq!(
test_instance[0],
ColumnBuffer::int2_with_bitvec(
[0, 0, -1234],
BitVec::from_slice(&[false, false, true])
)
);
}
#[test]
fn test_before_undefined_int4() {
let mut test_instance =
Columns::new(vec![ColumnWithName::undefined_typed("test_col", Type::Boolean, 2)]);
let shape = RowShape::testing(&[Type::Int4]);
let mut row = shape.allocate();
shape.set_values(&mut row, &[Value::Int4(56789)]);
test_instance.append_rows(&shape, [row], vec![]).unwrap();
assert_eq!(
test_instance[0],
ColumnBuffer::int4_with_bitvec(
[0, 0, 56789],
BitVec::from_slice(&[false, false, true])
)
);
}
#[test]
fn test_before_undefined_int8() {
let mut test_instance =
Columns::new(vec![ColumnWithName::undefined_typed("test_col", Type::Boolean, 2)]);
let shape = RowShape::testing(&[Type::Int8]);
let mut row = shape.allocate();
shape.set_values(&mut row, &[Value::Int8(-987654321)]);
test_instance.append_rows(&shape, [row], vec![]).unwrap();
assert_eq!(
test_instance[0],
ColumnBuffer::int8_with_bitvec(
[0, 0, -987654321],
BitVec::from_slice(&[false, false, true])
)
);
}
#[test]
fn test_before_undefined_int16() {
let mut test_instance =
Columns::new(vec![ColumnWithName::undefined_typed("test_col", Type::Boolean, 2)]);
let shape = RowShape::testing(&[Type::Int16]);
let mut row = shape.allocate();
shape.set_values(&mut row, &[Value::Int16(123456789012345678901234567890i128)]);
test_instance.append_rows(&shape, [row], vec![]).unwrap();
assert_eq!(
test_instance[0],
ColumnBuffer::int16_with_bitvec(
[0, 0, 123456789012345678901234567890i128],
BitVec::from_slice(&[false, false, true])
)
);
}
#[test]
fn test_before_undefined_string() {
let mut test_instance =
Columns::new(vec![ColumnWithName::undefined_typed("test_col", Type::Boolean, 2)]);
let shape = RowShape::testing(&[Type::Utf8]);
let mut row = shape.allocate();
shape.set_values(&mut row, &[Value::Utf8("reifydb".into())]);
test_instance.append_rows(&shape, [row], vec![]).unwrap();
assert_eq!(
test_instance[0],
ColumnBuffer::utf8_with_bitvec(
["".to_string(), "".to_string(), "reifydb".to_string()],
BitVec::from_slice(&[false, false, true])
)
);
}
#[test]
fn test_before_undefined_uint1() {
let mut test_instance =
Columns::new(vec![ColumnWithName::undefined_typed("test_col", Type::Boolean, 2)]);
let shape = RowShape::testing(&[Type::Uint1]);
let mut row = shape.allocate();
shape.set_values(&mut row, &[Value::Uint1(255)]);
test_instance.append_rows(&shape, [row], vec![]).unwrap();
assert_eq!(
test_instance[0],
ColumnBuffer::uint1_with_bitvec([0, 0, 255], BitVec::from_slice(&[false, false, true]))
);
}
#[test]
fn test_before_undefined_uint2() {
let mut test_instance =
Columns::new(vec![ColumnWithName::undefined_typed("test_col", Type::Boolean, 2)]);
let shape = RowShape::testing(&[Type::Uint2]);
let mut row = shape.allocate();
shape.set_values(&mut row, &[Value::Uint2(65535)]);
test_instance.append_rows(&shape, [row], vec![]).unwrap();
assert_eq!(
test_instance[0],
ColumnBuffer::uint2_with_bitvec(
[0, 0, 65535],
BitVec::from_slice(&[false, false, true])
)
);
}
#[test]
fn test_before_undefined_uint4() {
let mut test_instance =
Columns::new(vec![ColumnWithName::undefined_typed("test_col", Type::Boolean, 2)]);
let shape = RowShape::testing(&[Type::Uint4]);
let mut row = shape.allocate();
shape.set_values(&mut row, &[Value::Uint4(4294967295)]);
test_instance.append_rows(&shape, [row], vec![]).unwrap();
assert_eq!(
test_instance[0],
ColumnBuffer::uint4_with_bitvec(
[0, 0, 4294967295],
BitVec::from_slice(&[false, false, true])
)
);
}
#[test]
fn test_before_undefined_uint8() {
let mut test_instance =
Columns::new(vec![ColumnWithName::undefined_typed("test_col", Type::Boolean, 2)]);
let shape = RowShape::testing(&[Type::Uint8]);
let mut row = shape.allocate();
shape.set_values(&mut row, &[Value::Uint8(18446744073709551615)]);
test_instance.append_rows(&shape, [row], vec![]).unwrap();
assert_eq!(
test_instance[0],
ColumnBuffer::uint8_with_bitvec(
[0, 0, 18446744073709551615],
BitVec::from_slice(&[false, false, true])
)
);
}
#[test]
fn test_before_undefined_uint16() {
let mut test_instance =
Columns::new(vec![ColumnWithName::undefined_typed("test_col", Type::Boolean, 2)]);
let shape = RowShape::testing(&[Type::Uint16]);
let mut row = shape.allocate();
shape.set_values(&mut row, &[Value::Uint16(340282366920938463463374607431768211455u128)]);
test_instance.append_rows(&shape, [row], vec![]).unwrap();
assert_eq!(
test_instance[0],
ColumnBuffer::uint16_with_bitvec(
[0, 0, 340282366920938463463374607431768211455u128],
BitVec::from_slice(&[false, false, true])
)
);
}
#[test]
fn test_mismatched_columns() {
let mut test_instance = Columns::new(vec![]);
let shape = RowShape::testing(&[Type::Int2]);
let mut row = shape.allocate();
shape.set_values(&mut row, &[Value::Int2(2)]);
let err = test_instance.append_rows(&shape, [row], vec![]).err().unwrap();
assert!(err.to_string().contains("mismatched column count: expected 0, got 1"));
}
#[test]
fn test_ok() {
let mut test_instance = test_instance_with_columns();
let shape = RowShape::testing(&[Type::Int2, Type::Boolean]);
let mut row_one = shape.allocate();
shape.set_values(&mut row_one, &[Value::Int2(2), Value::Boolean(true)]);
let mut row_two = shape.allocate();
shape.set_values(&mut row_two, &[Value::Int2(3), Value::Boolean(false)]);
test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
assert_eq!(test_instance[0], ColumnBuffer::int2([1, 2, 3]));
assert_eq!(test_instance[1], ColumnBuffer::bool([true, true, false]));
}
#[test]
fn test_all_defined_bool() {
let mut test_instance =
Columns::new(vec![ColumnWithName::bool("test_col", Vec::<bool>::new())]);
let shape = RowShape::testing(&[Type::Boolean]);
let mut row_one = shape.allocate();
shape.set_bool(&mut row_one, 0, true);
let mut row_two = shape.allocate();
shape.set_bool(&mut row_two, 0, false);
test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
assert_eq!(test_instance[0], ColumnBuffer::bool([true, false]));
}
#[test]
fn test_all_defined_float4() {
let mut test_instance =
Columns::new(vec![ColumnWithName::float4("test_col", Vec::<f32>::new())]);
let shape = RowShape::testing(&[Type::Float4]);
let mut row_one = shape.allocate();
shape.set_values(&mut row_one, &[Value::Float4(OrderedF32::try_from(1.0).unwrap())]);
let mut row_two = shape.allocate();
shape.set_values(&mut row_two, &[Value::Float4(OrderedF32::try_from(2.0).unwrap())]);
test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
assert_eq!(test_instance[0], ColumnBuffer::float4([1.0, 2.0]));
}
#[test]
fn test_all_defined_float8() {
let mut test_instance =
Columns::new(vec![ColumnWithName::float8("test_col", Vec::<f64>::new())]);
let shape = RowShape::testing(&[Type::Float8]);
let mut row_one = shape.allocate();
shape.set_values(&mut row_one, &[Value::Float8(OrderedF64::try_from(1.0).unwrap())]);
let mut row_two = shape.allocate();
shape.set_values(&mut row_two, &[Value::Float8(OrderedF64::try_from(2.0).unwrap())]);
test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
assert_eq!(test_instance[0], ColumnBuffer::float8([1.0, 2.0]));
}
#[test]
fn test_all_defined_int1() {
let mut test_instance = Columns::new(vec![ColumnWithName::int1("test_col", Vec::<i8>::new())]);
let shape = RowShape::testing(&[Type::Int1]);
let mut row_one = shape.allocate();
shape.set_values(&mut row_one, &[Value::Int1(1)]);
let mut row_two = shape.allocate();
shape.set_values(&mut row_two, &[Value::Int1(2)]);
test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
assert_eq!(test_instance[0], ColumnBuffer::int1([1, 2]));
}
#[test]
fn test_all_defined_int2() {
let mut test_instance = Columns::new(vec![ColumnWithName::int2("test_col", Vec::<i16>::new())]);
let shape = RowShape::testing(&[Type::Int2]);
let mut row_one = shape.allocate();
shape.set_values(&mut row_one, &[Value::Int2(100)]);
let mut row_two = shape.allocate();
shape.set_values(&mut row_two, &[Value::Int2(200)]);
test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
assert_eq!(test_instance[0], ColumnBuffer::int2([100, 200]));
}
#[test]
fn test_all_defined_int4() {
let mut test_instance = Columns::new(vec![ColumnWithName::int4("test_col", Vec::<i32>::new())]);
let shape = RowShape::testing(&[Type::Int4]);
let mut row_one = shape.allocate();
shape.set_values(&mut row_one, &[Value::Int4(1000)]);
let mut row_two = shape.allocate();
shape.set_values(&mut row_two, &[Value::Int4(2000)]);
test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
assert_eq!(test_instance[0], ColumnBuffer::int4([1000, 2000]));
}
#[test]
fn test_all_defined_int8() {
let mut test_instance = Columns::new(vec![ColumnWithName::int8("test_col", Vec::<i64>::new())]);
let shape = RowShape::testing(&[Type::Int8]);
let mut row_one = shape.allocate();
shape.set_values(&mut row_one, &[Value::Int8(10000)]);
let mut row_two = shape.allocate();
shape.set_values(&mut row_two, &[Value::Int8(20000)]);
test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
assert_eq!(test_instance[0], ColumnBuffer::int8([10000, 20000]));
}
#[test]
fn test_all_defined_int16() {
let mut test_instance =
Columns::new(vec![ColumnWithName::int16("test_col", Vec::<i128>::new())]);
let shape = RowShape::testing(&[Type::Int16]);
let mut row_one = shape.allocate();
shape.set_values(&mut row_one, &[Value::Int16(1000)]);
let mut row_two = shape.allocate();
shape.set_values(&mut row_two, &[Value::Int16(2000)]);
test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
assert_eq!(test_instance[0], ColumnBuffer::int16([1000, 2000]));
}
#[test]
fn test_all_defined_string() {
let mut test_instance =
Columns::new(vec![ColumnWithName::utf8("test_col", Vec::<String>::new())]);
let shape = RowShape::testing(&[Type::Utf8]);
let mut row_one = shape.allocate();
shape.set_values(&mut row_one, &[Value::Utf8("a".into())]);
let mut row_two = shape.allocate();
shape.set_values(&mut row_two, &[Value::Utf8("b".into())]);
test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
assert_eq!(test_instance[0], ColumnBuffer::utf8(["a".to_string(), "b".to_string()]));
}
#[test]
fn test_all_defined_uint1() {
let mut test_instance = Columns::new(vec![ColumnWithName::uint1("test_col", Vec::<u8>::new())]);
let shape = RowShape::testing(&[Type::Uint1]);
let mut row_one = shape.allocate();
shape.set_values(&mut row_one, &[Value::Uint1(1)]);
let mut row_two = shape.allocate();
shape.set_values(&mut row_two, &[Value::Uint1(2)]);
test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
assert_eq!(test_instance[0], ColumnBuffer::uint1([1, 2]));
}
#[test]
fn test_all_defined_uint2() {
let mut test_instance =
Columns::new(vec![ColumnWithName::uint2("test_col", Vec::<u16>::new())]);
let shape = RowShape::testing(&[Type::Uint2]);
let mut row_one = shape.allocate();
shape.set_values(&mut row_one, &[Value::Uint2(100)]);
let mut row_two = shape.allocate();
shape.set_values(&mut row_two, &[Value::Uint2(200)]);
test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
assert_eq!(test_instance[0], ColumnBuffer::uint2([100, 200]));
}
#[test]
fn test_all_defined_uint4() {
let mut test_instance =
Columns::new(vec![ColumnWithName::uint4("test_col", Vec::<u32>::new())]);
let shape = RowShape::testing(&[Type::Uint4]);
let mut row_one = shape.allocate();
shape.set_values(&mut row_one, &[Value::Uint4(1000)]);
let mut row_two = shape.allocate();
shape.set_values(&mut row_two, &[Value::Uint4(2000)]);
test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
assert_eq!(test_instance[0], ColumnBuffer::uint4([1000, 2000]));
}
#[test]
fn test_all_defined_uint8() {
let mut test_instance =
Columns::new(vec![ColumnWithName::uint8("test_col", Vec::<u64>::new())]);
let shape = RowShape::testing(&[Type::Uint8]);
let mut row_one = shape.allocate();
shape.set_values(&mut row_one, &[Value::Uint8(10000)]);
let mut row_two = shape.allocate();
shape.set_values(&mut row_two, &[Value::Uint8(20000)]);
test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
assert_eq!(test_instance[0], ColumnBuffer::uint8([10000, 20000]));
}
#[test]
fn test_all_defined_uint16() {
let mut test_instance =
Columns::new(vec![ColumnWithName::uint16("test_col", Vec::<u128>::new())]);
let shape = RowShape::testing(&[Type::Uint16]);
let mut row_one = shape.allocate();
shape.set_values(&mut row_one, &[Value::Uint16(1000)]);
let mut row_two = shape.allocate();
shape.set_values(&mut row_two, &[Value::Uint16(2000)]);
test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
assert_eq!(test_instance[0], ColumnBuffer::uint16([1000, 2000]));
}
#[test]
fn test_row_with_undefined() {
let mut test_instance = test_instance_with_columns();
let shape = RowShape::testing(&[Type::Int2, Type::Boolean]);
let mut row = shape.allocate();
shape.set_values(&mut row, &[Value::none(), Value::Boolean(false)]);
test_instance.append_rows(&shape, [row], vec![]).unwrap();
assert_eq!(test_instance[0], ColumnBuffer::int2_with_bitvec(vec![1, 0], vec![true, false]));
assert_eq!(test_instance[1], ColumnBuffer::bool_with_bitvec([true, false], [true, true]));
}
#[test]
fn test_row_with_type_mismatch_fails() {
let mut test_instance = test_instance_with_columns();
let shape = RowShape::testing(&[Type::Boolean, Type::Boolean]);
let mut row = shape.allocate();
shape.set_values(&mut row, &[Value::Boolean(true), Value::Boolean(true)]);
let result = test_instance.append_rows(&shape, [row], vec![]);
assert!(result.is_err());
assert!(result.unwrap_err().to_string().contains("type mismatch"));
}
#[test]
fn test_row_wrong_length_fails() {
let mut test_instance = test_instance_with_columns();
let shape = RowShape::testing(&[Type::Int2]);
let mut row = shape.allocate();
shape.set_values(&mut row, &[Value::Int2(2)]);
let result = test_instance.append_rows(&shape, [row], vec![]);
assert!(result.is_err());
assert!(result.unwrap_err().to_string().contains("mismatched column count"));
}
#[test]
fn test_fallback_bool() {
let mut test_instance = Columns::new(vec![
ColumnWithName::bool("test_col", Vec::<bool>::new()),
ColumnWithName::bool("none", Vec::<bool>::new()),
]);
let shape = RowShape::testing(&[Type::Boolean, Type::Boolean]);
let mut row_one = shape.allocate();
shape.set_bool(&mut row_one, 0, true);
shape.set_none(&mut row_one, 1);
test_instance.append_rows(&shape, [row_one], vec![]).unwrap();
assert_eq!(test_instance[0], ColumnBuffer::bool_with_bitvec([true], [true]));
assert_eq!(test_instance[1], ColumnBuffer::bool_with_bitvec([false], [false]));
}
#[test]
fn test_fallback_float4() {
let mut test_instance = Columns::new(vec![
ColumnWithName::float4("test_col", Vec::<f32>::new()),
ColumnWithName::float4("none", Vec::<f32>::new()),
]);
let shape = RowShape::testing(&[Type::Float4, Type::Float4]);
let mut row = shape.allocate();
shape.set_f32(&mut row, 0, 1.5);
shape.set_none(&mut row, 1);
test_instance.append_rows(&shape, [row], vec![]).unwrap();
assert_eq!(test_instance[0], ColumnBuffer::float4_with_bitvec([1.5], [true]));
assert_eq!(test_instance[1], ColumnBuffer::float4_with_bitvec([0.0], [false]));
}
#[test]
fn test_fallback_float8() {
let mut test_instance = Columns::new(vec![
ColumnWithName::float8("test_col", Vec::<f64>::new()),
ColumnWithName::float8("none", Vec::<f64>::new()),
]);
let shape = RowShape::testing(&[Type::Float8, Type::Float8]);
let mut row = shape.allocate();
shape.set_f64(&mut row, 0, 2.5);
shape.set_none(&mut row, 1);
test_instance.append_rows(&shape, [row], vec![]).unwrap();
assert_eq!(test_instance[0], ColumnBuffer::float8_with_bitvec([2.5], [true]));
assert_eq!(test_instance[1], ColumnBuffer::float8_with_bitvec([0.0], [false]));
}
#[test]
fn test_fallback_int1() {
let mut test_instance = Columns::new(vec![
ColumnWithName::int1("test_col", Vec::<i8>::new()),
ColumnWithName::int1("none", Vec::<i8>::new()),
]);
let shape = RowShape::testing(&[Type::Int1, Type::Int1]);
let mut row = shape.allocate();
shape.set_i8(&mut row, 0, 42);
shape.set_none(&mut row, 1);
test_instance.append_rows(&shape, [row], vec![]).unwrap();
assert_eq!(test_instance[0], ColumnBuffer::int1_with_bitvec([42], [true]));
assert_eq!(test_instance[1], ColumnBuffer::int1_with_bitvec([0], [false]));
}
#[test]
fn test_fallback_int2() {
let mut test_instance = Columns::new(vec![
ColumnWithName::int2("test_col", Vec::<i16>::new()),
ColumnWithName::int2("none", Vec::<i16>::new()),
]);
let shape = RowShape::testing(&[Type::Int2, Type::Int2]);
let mut row = shape.allocate();
shape.set_i16(&mut row, 0, -1234i16);
shape.set_none(&mut row, 1);
test_instance.append_rows(&shape, [row], vec![]).unwrap();
assert_eq!(test_instance[0], ColumnBuffer::int2_with_bitvec([-1234], [true]));
assert_eq!(test_instance[1], ColumnBuffer::int2_with_bitvec([0], [false]));
}
#[test]
fn test_fallback_int4() {
let mut test_instance = Columns::new(vec![
ColumnWithName::int4("test_col", Vec::<i32>::new()),
ColumnWithName::int4("none", Vec::<i32>::new()),
]);
let shape = RowShape::testing(&[Type::Int4, Type::Int4]);
let mut row = shape.allocate();
shape.set_i32(&mut row, 0, 56789);
shape.set_none(&mut row, 1);
test_instance.append_rows(&shape, [row], vec![]).unwrap();
assert_eq!(test_instance[0], ColumnBuffer::int4_with_bitvec([56789], [true]));
assert_eq!(test_instance[1], ColumnBuffer::int4_with_bitvec([0], [false]));
}
#[test]
fn test_fallback_int8() {
let mut test_instance = Columns::new(vec![
ColumnWithName::int8("test_col", Vec::<i64>::new()),
ColumnWithName::int8("none", Vec::<i64>::new()),
]);
let shape = RowShape::testing(&[Type::Int8, Type::Int8]);
let mut row = shape.allocate();
shape.set_i64(&mut row, 0, -987654321);
shape.set_none(&mut row, 1);
test_instance.append_rows(&shape, [row], vec![]).unwrap();
assert_eq!(test_instance[0], ColumnBuffer::int8_with_bitvec([-987654321], [true]));
assert_eq!(test_instance[1], ColumnBuffer::int8_with_bitvec([0], [false]));
}
#[test]
fn test_fallback_int16() {
let mut test_instance = Columns::new(vec![
ColumnWithName::int16("test_col", Vec::<i128>::new()),
ColumnWithName::int16("none", Vec::<i128>::new()),
]);
let shape = RowShape::testing(&[Type::Int16, Type::Int16]);
let mut row = shape.allocate();
shape.set_i128(&mut row, 0, 123456789012345678901234567890i128);
shape.set_none(&mut row, 1);
test_instance.append_rows(&shape, [row], vec![]).unwrap();
assert_eq!(
test_instance[0],
ColumnBuffer::int16_with_bitvec([123456789012345678901234567890i128], [true])
);
assert_eq!(test_instance[1], ColumnBuffer::int16_with_bitvec([0], [false]));
}
#[test]
fn test_fallback_string() {
let mut test_instance = Columns::new(vec![
ColumnWithName::utf8("test_col", Vec::<String>::new()),
ColumnWithName::utf8("none", Vec::<String>::new()),
]);
let shape = RowShape::testing(&[Type::Utf8, Type::Utf8]);
let mut row = shape.allocate();
shape.set_utf8(&mut row, 0, "reifydb");
shape.set_none(&mut row, 1);
test_instance.append_rows(&shape, [row], vec![]).unwrap();
assert_eq!(test_instance[0], ColumnBuffer::utf8_with_bitvec(["reifydb".to_string()], [true]));
assert_eq!(test_instance[1], ColumnBuffer::utf8_with_bitvec(["".to_string()], [false]));
}
#[test]
fn test_fallback_uint1() {
let mut test_instance = Columns::new(vec![
ColumnWithName::uint1("test_col", Vec::<u8>::new()),
ColumnWithName::uint1("none", Vec::<u8>::new()),
]);
let shape = RowShape::testing(&[Type::Uint1, Type::Uint1]);
let mut row = shape.allocate();
shape.set_u8(&mut row, 0, 255);
shape.set_none(&mut row, 1);
test_instance.append_rows(&shape, [row], vec![]).unwrap();
assert_eq!(test_instance[0], ColumnBuffer::uint1_with_bitvec([255], [true]));
assert_eq!(test_instance[1], ColumnBuffer::uint1_with_bitvec([0], [false]));
}
#[test]
fn test_fallback_uint2() {
let mut test_instance = Columns::new(vec![
ColumnWithName::uint2("test_col", Vec::<u16>::new()),
ColumnWithName::uint2("none", Vec::<u16>::new()),
]);
let shape = RowShape::testing(&[Type::Uint2, Type::Uint2]);
let mut row = shape.allocate();
shape.set_u16(&mut row, 0, 65535u16);
shape.set_none(&mut row, 1);
test_instance.append_rows(&shape, [row], vec![]).unwrap();
assert_eq!(test_instance[0], ColumnBuffer::uint2_with_bitvec([65535], [true]));
assert_eq!(test_instance[1], ColumnBuffer::uint2_with_bitvec([0], [false]));
}
#[test]
fn test_fallback_uint4() {
let mut test_instance = Columns::new(vec![
ColumnWithName::uint4("test_col", Vec::<u32>::new()),
ColumnWithName::uint4("none", Vec::<u32>::new()),
]);
let shape = RowShape::testing(&[Type::Uint4, Type::Uint4]);
let mut row = shape.allocate();
shape.set_u32(&mut row, 0, 4294967295u32);
shape.set_none(&mut row, 1);
test_instance.append_rows(&shape, [row], vec![]).unwrap();
assert_eq!(test_instance[0], ColumnBuffer::uint4_with_bitvec([4294967295], [true]));
assert_eq!(test_instance[1], ColumnBuffer::uint4_with_bitvec([0], [false]));
}
#[test]
fn test_fallback_uint8() {
let mut test_instance = Columns::new(vec![
ColumnWithName::uint8("test_col", Vec::<u64>::new()),
ColumnWithName::uint8("none", Vec::<u64>::new()),
]);
let shape = RowShape::testing(&[Type::Uint8, Type::Uint8]);
let mut row = shape.allocate();
shape.set_u64(&mut row, 0, 18446744073709551615u64);
shape.set_none(&mut row, 1);
test_instance.append_rows(&shape, [row], vec![]).unwrap();
assert_eq!(test_instance[0], ColumnBuffer::uint8_with_bitvec([18446744073709551615], [true]));
assert_eq!(test_instance[1], ColumnBuffer::uint8_with_bitvec([0], [false]));
}
#[test]
fn test_fallback_uint16() {
let mut test_instance = Columns::new(vec![
ColumnWithName::uint16("test_col", Vec::<u128>::new()),
ColumnWithName::uint16("none", Vec::<u128>::new()),
]);
let shape = RowShape::testing(&[Type::Uint16, Type::Uint16]);
let mut row = shape.allocate();
shape.set_u128(&mut row, 0, 340282366920938463463374607431768211455u128);
shape.set_none(&mut row, 1);
test_instance.append_rows(&shape, [row], vec![]).unwrap();
assert_eq!(
test_instance[0],
ColumnBuffer::uint16_with_bitvec([340282366920938463463374607431768211455u128], [true])
);
assert_eq!(test_instance[1], ColumnBuffer::uint16_with_bitvec([0], [false]));
}
#[test]
fn test_all_defined_dictionary_id() {
let constraint = TypeConstraint::dictionary(DictionaryId::from(1u64), Type::Uint4);
let shape = RowShape::new(vec![RowShapeField::new("status", constraint)]);
let mut test_instance = Columns::new(vec![ColumnWithName::dictionary_id(
"status",
Vec::<DictionaryEntryId>::new(),
)]);
let mut row_one = shape.allocate();
shape.set_values(&mut row_one, &[Value::DictionaryId(DictionaryEntryId::U4(10))]);
let mut row_two = shape.allocate();
shape.set_values(&mut row_two, &[Value::DictionaryId(DictionaryEntryId::U4(20))]);
test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
assert_eq!(test_instance[0].get_value(0), Value::DictionaryId(DictionaryEntryId::U4(10)));
assert_eq!(test_instance[0].get_value(1), Value::DictionaryId(DictionaryEntryId::U4(20)));
}
#[test]
fn test_fallback_dictionary_id() {
let dict_constraint = TypeConstraint::dictionary(DictionaryId::from(1u64), Type::Uint4);
let shape = RowShape::new(vec![
RowShapeField::new("dict_col", dict_constraint),
RowShapeField::unconstrained("bool_col", Type::Boolean),
]);
let mut test_instance = Columns::new(vec![
ColumnWithName::dictionary_id("dict_col", Vec::<DictionaryEntryId>::new()),
ColumnWithName::bool("bool_col", Vec::<bool>::new()),
]);
let mut row = shape.allocate();
shape.set_values(&mut row, &[Value::none(), Value::Boolean(true)]);
test_instance.append_rows(&shape, [row], vec![]).unwrap();
assert!(!test_instance[0].is_defined(0));
assert_eq!(test_instance[1].get_value(0), Value::Boolean(true));
}
#[test]
fn test_before_undefined_dictionary_id() {
let constraint = TypeConstraint::dictionary(DictionaryId::from(2u64), Type::Uint4);
let shape = RowShape::new(vec![RowShapeField::new("tag", constraint)]);
let mut test_instance =
Columns::new(vec![ColumnWithName::undefined_typed("tag", Type::Boolean, 2)]);
let mut row = shape.allocate();
shape.set_values(&mut row, &[Value::DictionaryId(DictionaryEntryId::U4(5))]);
test_instance.append_rows(&shape, [row], vec![]).unwrap();
assert!(!test_instance[0].is_defined(0));
assert!(!test_instance[0].is_defined(1));
assert!(test_instance[0].is_defined(2));
assert_eq!(test_instance[0].get_value(2), Value::DictionaryId(DictionaryEntryId::U4(5)));
}
#[test]
fn test_all_defined_identity_id() {
let id1 = IdentityId::anonymous();
let id2 = IdentityId::root();
let shape = RowShape::testing(&[Type::IdentityId]);
let mut test_instance = Columns::new(vec![ColumnWithName::new(
Fragment::internal("id_col"),
ColumnBuffer::identity_id(Vec::<IdentityId>::new()),
)]);
let mut row_one = shape.allocate();
shape.set_identity_id(&mut row_one, 0, id1);
let mut row_two = shape.allocate();
shape.set_identity_id(&mut row_two, 0, id2);
test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
assert_eq!(test_instance[0].get_value(0), Value::IdentityId(id1));
assert_eq!(test_instance[0].get_value(1), Value::IdentityId(id2));
}
#[test]
fn test_fallback_identity_id() {
let id = IdentityId::anonymous();
let shape = RowShape::testing(&[Type::IdentityId, Type::Boolean]);
let mut test_instance = Columns::new(vec![
ColumnWithName::new(
Fragment::internal("id_col"),
ColumnBuffer::identity_id(Vec::<IdentityId>::new()),
),
ColumnWithName::bool("bool_col", Vec::<bool>::new()),
]);
let mut row = shape.allocate();
shape.set_identity_id(&mut row, 0, id);
shape.set_none(&mut row, 1);
test_instance.append_rows(&shape, [row], vec![]).unwrap();
assert_eq!(test_instance[0].get_value(0), Value::IdentityId(id));
assert!(test_instance[0].is_defined(0));
assert!(!test_instance[1].is_defined(0));
}
#[test]
fn test_all_defined_blob() {
let blob1 = Blob::new(vec![1, 2, 3]);
let blob2 = Blob::new(vec![4, 5]);
let shape = RowShape::testing(&[Type::Blob]);
let mut test_instance = Columns::new(vec![ColumnWithName::new(
Fragment::internal("blob_col"),
ColumnBuffer::blob(Vec::<Blob>::new()),
)]);
let mut row_one = shape.allocate();
shape.set_blob(&mut row_one, 0, &blob1);
let mut row_two = shape.allocate();
shape.set_blob(&mut row_two, 0, &blob2);
test_instance.append_rows(&shape, [row_one, row_two], vec![]).unwrap();
assert_eq!(test_instance[0].get_value(0), Value::Blob(blob1));
assert_eq!(test_instance[0].get_value(1), Value::Blob(blob2));
}
#[test]
fn test_fallback_blob() {
let blob = Blob::new(vec![10, 20, 30]);
let shape = RowShape::testing(&[Type::Blob, Type::Boolean]);
let mut test_instance = Columns::new(vec![
ColumnWithName::new(
Fragment::internal("blob_col"),
ColumnBuffer::blob(Vec::<Blob>::new()),
),
ColumnWithName::bool("bool_col", Vec::<bool>::new()),
]);
let mut row = shape.allocate();
shape.set_blob(&mut row, 0, &blob);
shape.set_none(&mut row, 1);
test_instance.append_rows(&shape, [row], vec![]).unwrap();
assert_eq!(test_instance[0].get_value(0), Value::Blob(blob));
assert!(test_instance[0].is_defined(0));
assert!(!test_instance[1].is_defined(0));
}
fn test_instance_with_columns() -> Columns {
Columns::new(vec![
ColumnWithName::new(Fragment::internal("int2"), ColumnBuffer::int2(vec![1])),
ColumnWithName::new(Fragment::internal("bool"), ColumnBuffer::bool(vec![true])),
])
}
}
}