Skip to main content

vortex_array/
kernel.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Parent kernels: child-driven fused execution of parent arrays.
5//!
6//! A parent kernel allows a child encoding to provide a specialized execution path for its
7//! parent array. This is Layer 3 of the [execution model](https://docs.vortex.dev/developer-guide/internals/execution).
8//!
9//! For example, a `RunEndArray` child of a `SliceArray` can perform a binary search on its
10//! run ends rather than decoding the entire array and slicing the result.
11//!
12//! Encodings declare their parent kernels by implementing [`ExecuteParentKernel`] and
13//! registering them with the session's optimizer-kernel registry. Each kernel specifies which
14//! parent types it handles via a [`Matcher`].
15
16use std::fmt::Debug;
17
18use vortex_error::VortexResult;
19
20use crate::ArrayRef;
21use crate::ExecutionCtx;
22use crate::array::ArrayView;
23use crate::array::VTable;
24use crate::matcher::Matcher;
25
26/// A kernel that allows a child encoding `V` to execute its parent array in a fused manner.
27///
28/// This is the typed trait that encoding authors implement. The associated `Parent` type
29/// specifies which parent array types this kernel can handle. When the parent matches,
30/// [`execute_parent`](Self::execute_parent) is called with the strongly-typed child and parent views.
31///
32/// Unlike reduce rules, parent kernels may read buffers and perform real computation.
33///
34/// Return `Ok(None)` to decline handling (the scheduler will try the next kernel or fall
35/// through to the encoding's own `execute`).
36pub trait ExecuteParentKernel<V: VTable>: Debug + Send + Sync + 'static {
37    /// The parent array type this kernel handles.
38    type Parent: Matcher;
39
40    /// Attempt to execute the parent array fused with the child array.
41    fn execute_parent(
42        &self,
43        array: ArrayView<'_, V>,
44        parent: <Self::Parent as Matcher>::Match<'_>,
45        child_idx: usize,
46        ctx: &mut ExecutionCtx,
47    ) -> VortexResult<Option<ArrayRef>>;
48}