use kernel::PARENT_KERNELS;
use vortex_dtype::DType;
use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use vortex_error::vortex_bail;
use vortex_error::vortex_ensure;
use vortex_session::VortexSession;
use crate::ArrayRef;
use crate::DeserializeMetadata;
use crate::ExecutionCtx;
use crate::ProstMetadata;
use crate::SerializeMetadata;
use crate::arrays::BoolArray;
use crate::buffer::BufferHandle;
use crate::serde::ArrayChildren;
use crate::validity::Validity;
use crate::vtable;
use crate::vtable::VTable;
use crate::vtable::ValidityVTableFromValidityHelper;
mod array;
mod canonical;
mod kernel;
mod operations;
mod validity;
mod visitor;
use crate::arrays::bool::compute::rules::RULES;
use crate::vtable::ArrayId;
vtable!(Bool);
#[derive(prost::Message)]
pub struct BoolMetadata {
#[prost(uint32, tag = "1")]
pub offset: u32,
}
impl VTable for BoolVTable {
type Array = BoolArray;
type Metadata = ProstMetadata<BoolMetadata>;
type ArrayVTable = Self;
type OperationsVTable = Self;
type ValidityVTable = ValidityVTableFromValidityHelper;
type VisitorVTable = Self;
fn id(_array: &Self::Array) -> ArrayId {
Self::ID
}
fn metadata(array: &BoolArray) -> VortexResult<Self::Metadata> {
assert!(array.offset < 8, "Offset must be <8, got {}", array.offset);
Ok(ProstMetadata(BoolMetadata {
offset: u32::try_from(array.offset).vortex_expect("checked"),
}))
}
fn serialize(metadata: Self::Metadata) -> VortexResult<Option<Vec<u8>>> {
Ok(Some(metadata.serialize()))
}
fn deserialize(
bytes: &[u8],
_dtype: &DType,
_len: usize,
_buffers: &[BufferHandle],
_session: &VortexSession,
) -> VortexResult<Self::Metadata> {
let metadata = <Self::Metadata as DeserializeMetadata>::deserialize(bytes)?;
Ok(ProstMetadata(metadata))
}
fn build(
dtype: &DType,
len: usize,
metadata: &Self::Metadata,
buffers: &[BufferHandle],
children: &dyn ArrayChildren,
) -> VortexResult<BoolArray> {
if buffers.len() != 1 {
vortex_bail!("Expected 1 buffer, got {}", buffers.len());
}
let validity = if children.is_empty() {
Validity::from(dtype.nullability())
} else if children.len() == 1 {
let validity = children.get(0, &Validity::DTYPE, len)?;
Validity::Array(validity)
} else {
vortex_bail!("Expected 0 or 1 child, got {}", children.len());
};
let buffer = buffers[0].clone();
BoolArray::try_new_from_handle(buffer, metadata.offset as usize, len, validity)
}
fn with_children(array: &mut Self::Array, children: Vec<ArrayRef>) -> VortexResult<()> {
vortex_ensure!(
children.len() <= 1,
"BoolArray can have at most 1 child (validity), got {}",
children.len()
);
array.validity = if children.is_empty() {
Validity::from(array.dtype().nullability())
} else {
Validity::Array(children.into_iter().next().vortex_expect("checked"))
};
Ok(())
}
fn execute(array: &Self::Array, _ctx: &mut ExecutionCtx) -> VortexResult<ArrayRef> {
Ok(array.to_array())
}
fn reduce_parent(
array: &Self::Array,
parent: &ArrayRef,
child_idx: usize,
) -> VortexResult<Option<ArrayRef>> {
RULES.evaluate(array, parent, child_idx)
}
fn execute_parent(
array: &Self::Array,
parent: &ArrayRef,
child_idx: usize,
ctx: &mut ExecutionCtx,
) -> VortexResult<Option<ArrayRef>> {
PARENT_KERNELS.execute(array, parent, child_idx, ctx)
}
}
#[derive(Debug)]
pub struct BoolVTable;
impl BoolVTable {
pub const ID: ArrayId = ArrayId::new_ref("vortex.bool");
}