mod operations;
mod plugin;
mod validity;
use std::hash::Hasher;
use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use vortex_error::vortex_ensure;
use vortex_error::vortex_ensure_eq;
use vortex_error::vortex_panic;
use vortex_session::registry::CachedId;
use crate::AnyCanonical;
use crate::ArrayEq;
use crate::ArrayHash;
use crate::ArrayParts;
use crate::ArrayRef;
use crate::Canonical;
use crate::EqMode;
use crate::IntoArray;
use crate::array::Array;
use crate::array::ArrayId;
use crate::array::ArrayView;
use crate::array::VTable;
use crate::array::with_empty_buffers;
use crate::arrays::ConstantArray;
use crate::arrays::masked::MaskedArrayExt;
use crate::arrays::masked::MaskedArraySlotsExt;
use crate::arrays::masked::MaskedData;
use crate::arrays::masked::array::MaskedSlots;
use crate::arrays::masked::mask_validity_canonical;
use crate::buffer::BufferHandle;
use crate::dtype::DType;
use crate::executor::ExecutionCtx;
use crate::executor::ExecutionResult;
use crate::require_child;
use crate::scalar::Scalar;
pub type MaskedArray = Array<Masked>;
#[derive(Clone, Debug)]
pub struct Masked;
impl ArrayHash for MaskedData {
fn array_hash<H: Hasher>(&self, _state: &mut H, _accuracy: EqMode) {}
}
impl ArrayEq for MaskedData {
fn array_eq(&self, _other: &Self, _accuracy: EqMode) -> bool {
true
}
}
impl VTable for Masked {
type TypedArrayData = MaskedData;
type OperationsVTable = Self;
type ValidityVTable = Self;
fn id(&self) -> ArrayId {
static ID: CachedId = CachedId::new("vortex.masked");
*ID
}
fn validate(
&self,
_data: &MaskedData,
dtype: &DType,
len: usize,
slots: &[Option<ArrayRef>],
) -> VortexResult<()> {
vortex_ensure!(
slots[MaskedSlots::CHILD].is_some(),
"MaskedArray child slot must be present"
);
let child = slots[MaskedSlots::CHILD]
.as_ref()
.vortex_expect("validated child slot");
vortex_ensure_eq!(child.len(), len, "MaskedArray child length mismatch");
vortex_ensure_eq!(
child.dtype().as_nullable(),
*dtype,
"MaskedArray dtype does not match child and validity",
);
Ok(())
}
fn nbuffers(_array: ArrayView<'_, Self>) -> usize {
0
}
fn buffer(_array: ArrayView<'_, Self>, _idx: usize) -> BufferHandle {
vortex_panic!("MaskedArray has no buffers")
}
fn buffer_name(_array: ArrayView<'_, Self>, _idx: usize) -> Option<String> {
None
}
fn with_buffers(
&self,
array: ArrayView<'_, Self>,
buffers: &[BufferHandle],
) -> VortexResult<ArrayParts<Self>> {
with_empty_buffers(self, array, buffers)
}
fn execute(array: Array<Self>, ctx: &mut ExecutionCtx) -> VortexResult<ExecutionResult> {
let array = require_child!(array, array.child(), MaskedSlots::CHILD => AnyCanonical);
let validity = array.masked_validity();
if validity.definitely_all_null() {
return Ok(ExecutionResult::done(
ConstantArray::new(Scalar::null(array.dtype().as_nullable()), array.len())
.into_array(),
));
}
let child = Canonical::from(array.child().as_::<AnyCanonical>());
Ok(ExecutionResult::done(
mask_validity_canonical(child, validity, ctx)?.into_array(),
))
}
fn slot_name(_array: ArrayView<'_, Self>, idx: usize) -> String {
MaskedSlots::NAMES[idx].to_string()
}
}
#[cfg(test)]
mod tests {
use std::sync::LazyLock;
use rstest::rstest;
use vortex_buffer::ByteBufferMut;
use vortex_error::VortexError;
use vortex_session::VortexSession;
use vortex_session::registry::ReadContext;
use crate::ArrayContext;
use crate::Canonical;
use crate::IntoArray;
use crate::VortexSessionExecute;
use crate::arrays::Masked;
use crate::arrays::MaskedArray;
use crate::arrays::PrimitiveArray;
use crate::assert_arrays_eq;
use crate::dtype::Nullability;
use crate::serde::SerializeOptions;
use crate::serde::SerializedArray;
use crate::validity::Validity;
static SESSION: LazyLock<VortexSession> = LazyLock::new(VortexSession::empty);
#[rstest]
#[case(
MaskedArray::try_new(
PrimitiveArray::from_iter([1i32, 2, 3]).into_array(),
Validity::AllValid,
&mut SESSION.create_execution_ctx(),
).unwrap()
)]
#[case(
MaskedArray::try_new(
PrimitiveArray::from_iter([1i32, 2, 3, 4, 5]).into_array(),
Validity::from_iter([true, true, false, true, false]),
&mut SESSION.create_execution_ctx(),
).unwrap()
)]
#[case(
MaskedArray::try_new(
PrimitiveArray::from_iter(0..100).into_array(),
Validity::from_iter((0..100).map(|i| i % 3 != 0)),
&mut SESSION.create_execution_ctx(),
).unwrap()
)]
fn test_serde_roundtrip(#[case] array: MaskedArray) {
let dtype = array.dtype().clone();
let len = array.len();
let ctx = ArrayContext::empty();
let serialized = array
.clone()
.into_array()
.serialize(&ctx, &VortexSession::empty(), &SerializeOptions::default())
.unwrap();
let mut concat = ByteBufferMut::empty();
for buf in serialized {
concat.extend_from_slice(buf.as_ref());
}
let concat = concat.freeze();
let parts = SerializedArray::try_from(concat).unwrap();
let decoded = parts
.decode(
&dtype,
len,
&ReadContext::new(ctx.to_ids()),
&VortexSession::empty(),
)
.unwrap();
assert!(decoded.is::<Masked>());
assert_arrays_eq!(decoded, array, &mut SESSION.create_execution_ctx());
}
#[test]
fn test_execute_with_all_valid_preserves_nullable_dtype() -> Result<(), VortexError> {
let mut ctx = SESSION.create_execution_ctx();
let child = PrimitiveArray::from_iter([1i32, 2, 3]).into_array();
assert_eq!(child.dtype().nullability(), Nullability::NonNullable);
let array = MaskedArray::try_new(child, Validity::AllValid, &mut ctx)?;
assert_eq!(array.dtype().nullability(), Nullability::Nullable);
let mut ctx = SESSION.create_execution_ctx();
let result: Canonical = array.into_array().execute(&mut ctx)?;
assert_eq!(
result.dtype().nullability(),
Nullability::Nullable,
"MaskedArray execute should produce Nullable dtype"
);
Ok(())
}
}