use vortex_buffer::Buffer;
use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use vortex_error::vortex_err;
use crate::ExecutionCtx;
use crate::ProbeState;
use crate::arrays::runend::RunEnd;
use crate::arrays::runend::RunEndArrayExt;
use crate::arrays::runend::RunEndArraySlotsExt;
use crate::arrays::runend::RunEndSlots;
use crate::match_each_unsigned_integer_ptype;
use crate::scalar::Scalar;
use crate::search_sorted::SearchSorted;
use crate::search_sorted::SearchSortedPrimitiveArray;
use crate::search_sorted::SearchSortedSide;
use crate::search_sorted::decode_sorted_positions;
#[derive(Debug, Default)]
pub struct RunEndProbeState {
ends: Option<Buffer<u64>>,
}
pub(crate) fn scalar_at(
state: &mut ProbeState<'_, RunEnd>,
index: usize,
ctx: &mut ExecutionCtx,
) -> VortexResult<Scalar> {
let array = state.array();
let logical_index = array
.offset()
.checked_add(index)
.ok_or_else(|| vortex_err!("RunEnd logical index overflow"))?;
let (retained, mut children) = state.split();
let run = match retained {
Some(retained) => {
if retained.ends.is_none() {
retained.ends = Some(decode_sorted_positions(array.ends(), ctx)?);
}
let ends = retained.ends.as_ref().vortex_expect("decoded above");
let logical_index = u64::try_from(logical_index)?;
ends.partition_point(|&end| end <= logical_index)
}
None => {
let ends = children
.slot(RunEndSlots::ENDS)?
.ok_or_else(|| vortex_err!("RunEnd ends slot is missing"))?;
let len = ends.array().len();
match_each_unsigned_integer_ptype!(ends.array().dtype().as_ptype(), |T| {
SearchSortedPrimitiveArray::<T>::with_probe(ends, ctx)
.search_sorted(&logical_index, SearchSortedSide::Right)?
.to_ends_index(len)
})
}
};
children
.slot(RunEndSlots::VALUES)?
.ok_or_else(|| vortex_err!("RunEnd values slot is missing"))?
.execute_scalar(run, ctx)
}
#[cfg(test)]
mod tests;