Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
add try_new for MutableFixedSizeListArray
Browse files Browse the repository at this point in the history
  • Loading branch information
Aryansh Omray committed Nov 6, 2023
1 parent d22c788 commit a9429cf
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/array/fixed_size_list/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,57 @@ impl<M: MutableArray> From<MutableFixedSizeListArray<M>> for FixedSizeListArray
}

impl<M: MutableArray> MutableFixedSizeListArray<M> {
/// Creates a new [`MutableFixedSizeListArray`].
///
/// # Errors
/// This function returns an error iff:
/// * The `data_type`'s physical type is not [`crate::datatypes::PhysicalType::FixedSizeList`]
/// * The `data_type`'s inner field's data type is not equal to `values.data_type`.
/// * The length of `values` is not a multiple of `size` in `data_type`
/// * the validity's length is not equal to `values.len() / size`.
pub fn try_new(
data_type: DataType,
values: M,
validity: Option<MutableBitmap>,
) -> Result<Self> {
let (child, size) = FixedSizeListArray::try_child_and_size(&data_type)?;

let child_data_type = &child.data_type;
let values_data_type = values.data_type();
if child_data_type != values_data_type {
return Err(Error::oos(
format!(
"MutableFixedSizeListArray's child's DataType must match. However, the expected DataType is {child_data_type:?} while it got {values_data_type:?}.",
)
));
}

if values.len() % size != 0 {
return Err(Error::oos(format!(
"values (of len {}) must be a multiple of size ({}) in MutableFixedSizeListArray.",
values.len(),
size
)));
}
let len = values.len() / size;

if validity
.as_ref()
.map_or(false, |validity| validity.len() != len)
{
return Err(Error::oos(
"validity mask length must be equal to the number of values divided by size",
));
}

Ok(Self {
data_type,
size,
values,
validity,
})
}

/// Creates a new [`MutableFixedSizeListArray`] from a [`MutableArray`] and size.
pub fn new(values: M, size: usize) -> Self {
let data_type = FixedSizeListArray::default_datatype(values.data_type().clone(), size);
Expand Down

0 comments on commit a9429cf

Please sign in to comment.