use vortex_error::VortexResult;
use super::BatchPlan;
use super::RowPolicy;
use super::RowVisitor;
use super::check::assert_deferred_visit_contract;
use super::check::assert_owned_visit_contract;
use super::check::assert_sink_visit_contract;
use super::check::validate_owned_visit;
use super::check::validate_sink_visit;
use super::row_visitor::private;
use crate::ArrayRef;
use crate::ExecutionCtx;
use crate::dtype::DType;
use crate::scalar_fn::unstable::row::ElementTuple;
use crate::scalar_fn::unstable::row::FailureEvidence;
use crate::scalar_fn::unstable::row::IndexedElementTuple;
use crate::scalar_fn::unstable::row::OutputElement;
use crate::scalar_fn::unstable::row::OutputSink;
use crate::scalar_fn::unstable::row::RowFn;
use crate::scalar_fn::unstable::row::SinkResult;
use crate::scalar_fn::unstable::row::batch::BatchInputs;
use crate::scalar_fn::unstable::row::batch::BatchProbe;
use crate::scalar_fn::unstable::row::batch::BorrowedRowFnArgs;
use crate::scalar_fn::unstable::row::batch::PartiallyValidBatch;
use crate::scalar_fn::unstable::row::batch::RowInputs;
use crate::scalar_fn::unstable::row::execute::execute_owned;
use crate::scalar_fn::unstable::row::execute::execute_owned_bool;
use crate::scalar_fn::unstable::row::execute::execute_owned_infallible;
use crate::scalar_fn::unstable::row::execute::execute_owned_infallible_bool;
use crate::scalar_fn::unstable::row::execute::execute_sink;
pub(crate) enum Fused<'a> {
Done(ArrayRef),
Partial(PartiallyValidBatch<'a>),
}
pub(crate) struct ExecuteFused<'a, 'ctx, F: RowFn, I: ?Sized> {
inputs: &'a I,
probe: BatchProbe,
dtypes: &'a [DType],
output_dtype: Option<DType>,
ctx: &'ctx mut ExecutionCtx,
function: &'a F,
}
impl<'a, 'ctx, F: RowFn, I: RowInputs + ?Sized> ExecuteFused<'a, 'ctx, F, I> {
pub(crate) fn new(
function: &'a F,
inputs: &'a I,
probe: BatchProbe,
dtypes: &'a [DType],
ctx: &'ctx mut ExecutionCtx,
) -> Self {
Self {
inputs,
probe,
dtypes,
output_dtype: None,
ctx,
function,
}
}
fn execute(
self,
storage_dtype: DType,
policy: RowPolicy,
kernel: impl FnOnce(BorrowedRowFnArgs<'_>, &mut ExecutionCtx) -> VortexResult<ArrayRef>,
) -> VortexResult<Fused<'a>> {
let plan = BatchPlan::new(storage_dtype, self.output_dtype, policy)?;
let batch = BatchInputs::new(RowFn::id(self.function), self.probe, self.inputs);
let fused = match batch.try_execute_without_retry(self.dtypes, plan, kernel, self.ctx)? {
Ok(values) => Fused::Done(values),
Err(partial) => Fused::Partial(partial),
};
Ok(fused)
}
}
impl<F: RowFn, I: ?Sized> private::Sealed for ExecuteFused<'_, '_, F, I> {}
impl<'a, F: RowFn, I: RowInputs + ?Sized> RowVisitor for ExecuteFused<'a, '_, F, I> {
type VisitResult = Fused<'a>;
fn with_output_dtype(mut self, dtype: DType) -> Self {
self.output_dtype = Some(dtype);
self
}
fn visit_prepared<Args, Out, Prepared>(
self,
prepare: impl FnOnce(Args::ConstElems<'_>) -> Prepared,
apply: impl Fn(&Prepared, Args::Elems<'_>) -> Out,
) -> VortexResult<Self::VisitResult>
where
Args: IndexedElementTuple,
Out: OutputElement,
{
const { assert_owned_visit_contract::<F, Args, Out>() };
let storage_dtype = validate_owned_visit::<Args, Out>(self.dtypes)?;
self.execute(
storage_dtype,
RowPolicy::for_owned_output::<Args>(),
|args, ctx| execute_owned_infallible::<Args, Out, Prepared>(&args, ctx, prepare, apply),
)
}
fn visit_bool<Args, const MULTIVERSIONED: bool>(
self,
apply: impl Fn(Args::Elems<'_>) -> bool,
) -> VortexResult<Self::VisitResult>
where
Args: IndexedElementTuple,
{
const { assert_owned_visit_contract::<F, Args, bool>() };
let storage_dtype = validate_owned_visit::<Args, bool>(self.dtypes)?;
self.execute(
storage_dtype,
RowPolicy::for_owned_output::<Args>(),
|args, ctx| execute_owned_infallible_bool::<Args, MULTIVERSIONED>(&args, ctx, apply),
)
}
fn visit_prepared_into<Args, Sink, Prepared, ApplyResult>(
self,
params: Sink::Params,
prepare: impl FnOnce(Args::ConstElems<'_>) -> Prepared,
apply: impl Fn(&Prepared, Args::Elems<'_>, Sink::Row<'_>) -> ApplyResult,
) -> VortexResult<Self::VisitResult>
where
Args: ElementTuple,
Sink: OutputSink,
ApplyResult: SinkResult<WriteToken = Sink::WriteToken>,
{
const { assert_sink_visit_contract::<F, Args, ApplyResult>() };
let storage_dtype = validate_sink_visit::<Args, Sink>(self.dtypes, ¶ms)?;
self.execute(
storage_dtype,
RowPolicy::for_sink::<Args, ApplyResult>(),
|args, ctx| {
execute_sink::<Args, Prepared, Sink, ApplyResult>(
&args, ¶ms, ctx, prepare, apply,
)
},
)
}
fn visit_prepared_deferred<Args, Out, Prepared, Fail>(
self,
prepare: impl FnOnce(Args::ConstElems<'_>) -> Prepared,
apply: impl Fn(&Prepared, Args::Elems<'_>) -> (Out, Fail),
finish_failure: impl FnOnce(Fail) -> VortexResult<()>,
) -> VortexResult<Self::VisitResult>
where
Args: IndexedElementTuple,
Out: OutputElement,
Fail: FailureEvidence,
{
const { assert_deferred_visit_contract::<F, Args, Out, Fail>() };
let storage_dtype = validate_owned_visit::<Args, Out>(self.dtypes)?;
self.execute(
storage_dtype,
RowPolicy::for_deferred_output::<Args>(),
|args, ctx| {
execute_owned::<Args, Out, Prepared, Fail>(
&args,
ctx,
prepare,
apply,
finish_failure,
)
},
)
}
fn visit_prepared_deferred_bool<Args, Prepared, Fail, const MULTIVERSIONED: bool>(
self,
prepare: impl FnOnce(Args::ConstElems<'_>) -> Prepared,
apply: impl Fn(&Prepared, Args::Elems<'_>) -> (bool, Fail),
finish_failure: impl FnOnce(Fail) -> VortexResult<()>,
) -> VortexResult<Self::VisitResult>
where
Args: IndexedElementTuple,
Fail: FailureEvidence,
{
const { assert_deferred_visit_contract::<F, Args, bool, Fail>() };
let storage_dtype = validate_owned_visit::<Args, bool>(self.dtypes)?;
self.execute(
storage_dtype,
RowPolicy::for_deferred_output::<Args>(),
|args, ctx| {
execute_owned_bool::<Args, Prepared, Fail, MULTIVERSIONED>(
&args,
ctx,
prepare,
apply,
finish_failure,
)
},
)
}
}