mod once;
mod whole;
pub(crate) use once::*;
use smallvec::SmallVec;
use vortex_array::ArrayRef;
use vortex_array::Canonical;
use vortex_array::IntoArray;
use vortex_array::arrays::ChunkedArray;
use vortex_array::dtype::DType;
use vortex_array::optimizer::ArrayOptimizer;
use vortex_error::VortexResult;
use vortex_error::vortex_err;
use vortex_session::VortexSession;
pub(crate) use whole::*;
use crate::execution::operator::Cx;
use crate::execution::pipeline::Inlet;
pub(crate) use crate::plan::builtins::concat::execute::*;
pub(crate) use crate::plan::builtins::eval::execute::*;
pub(crate) use crate::plan::builtins::filter::execute::*;
pub(crate) use crate::plan::builtins::pack::execute::*;
pub(crate) use crate::plan::builtins::take::execute::*;
pub(crate) const UNBOUNDED: usize = usize::MAX;
pub(crate) fn take_rows(
inlet: &mut Inlet<'_>,
len: usize,
session: &VortexSession,
) -> VortexResult<ArrayRef> {
let mut pieces: SmallVec<[ArrayRef; 2]> = SmallVec::new();
let mut left = len;
while left > 0 {
let front = inlet
.peek_mut()
.ok_or_else(|| vortex_err!("Taking {len} rows from an inlet holding fewer"))?;
if front.len() <= left {
left -= front.len();
pieces.extend(inlet.take());
} else {
let rest = front.slice(left..front.len())?.optimize_ctx(session)?;
pieces.push(std::mem::replace(front, rest).slice(0..left)?);
left = 0;
}
}
if pieces.len() == 1 {
return Ok(pieces.swap_remove(0));
}
let dtype = pieces[0].dtype().clone();
Ok(unsafe { ChunkedArray::new_unchecked(pieces, dtype) }.into_array())
}
pub(crate) fn join(dtype: &DType, mut arrays: Vec<ArrayRef>) -> VortexResult<ArrayRef> {
match arrays.len() {
0 => Ok(Canonical::empty(dtype).into_array()),
1 => Ok(arrays.remove(0)),
_ => Ok(ChunkedArray::try_new(arrays, dtype.clone())?.into_array()),
}
}
fn drain(inlet: &mut Inlet<'_>) -> Vec<ArrayRef> {
let mut batches = Vec::with_capacity(inlet.len());
while let Some(batch) = inlet.take() {
batches.push(batch);
}
batches
}
pub(crate) fn join_inlet(cx: &mut Cx<'_>, index: usize, dtype: &DType) -> VortexResult<ArrayRef> {
join(dtype, drain(&mut cx.inlet(index)))
}