Skip to content

Commit 85e79fb

Browse files
committed
Flat Layout Execution
Signed-off-by: Nicholas Gates <[email protected]>
1 parent c6aa122 commit 85e79fb

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3+
4+
use itertools::Itertools;
5+
use vortex_dtype::DType;
6+
use vortex_error::VortexResult;
7+
use vortex_vector::Datum;
8+
use vortex_vector::Scalar;
9+
use vortex_vector::Vector;
10+
11+
use crate::expr::ExecutionArgs;
12+
use crate::expr::ScalarFn;
13+
use crate::kernel::Kernel;
14+
use crate::kernel::KernelRef;
15+
16+
/// A CPU kernel for executing scalar functions.
17+
#[derive(Debug)]
18+
pub struct ScalarFnKernel {
19+
/// The scalar function to apply.
20+
scalar_fn: ScalarFn,
21+
22+
/// Inputs to the kernel, either constants or other kernels.
23+
inputs: Vec<KernelInput>,
24+
/// The input data types
25+
input_dtypes: Vec<DType>,
26+
/// The row count for vector inputs
27+
row_count: usize,
28+
/// The return data type
29+
return_dtype: DType,
30+
}
31+
32+
#[derive(Debug)]
33+
enum KernelInput {
34+
Scalar(Scalar),
35+
Vector(KernelRef),
36+
}
37+
38+
impl Kernel for ScalarFnKernel {
39+
fn execute(self: Box<Self>) -> VortexResult<Vector> {
40+
let datums: Vec<_> = self
41+
.inputs
42+
.into_iter()
43+
.map(|input| {
44+
Ok(match input {
45+
KernelInput::Scalar(s) => Datum::Scalar(s),
46+
KernelInput::Vector(k) => Datum::Vector(k.execute()?),
47+
})
48+
})
49+
.try_collect()?;
50+
51+
let args = ExecutionArgs {
52+
datums,
53+
dtypes: self.input_dtypes,
54+
row_count: self.row_count,
55+
return_dtype: self.return_dtype,
56+
};
57+
58+
Ok(self.scalar_fn.execute(args)?.ensure_vector(self.row_count))
59+
}
60+
61+
fn children(&self) -> Vec<&KernelRef> {
62+
self.inputs
63+
.iter()
64+
.filter_map(|input| match input {
65+
KernelInput::Vector(k) => Some(k),
66+
KernelInput::Scalar(_) => None,
67+
})
68+
.collect()
69+
}
70+
}

vortex-array/src/arrays/scalar_fn/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-FileCopyrightText: Copyright the Vortex contributors
33

44
mod array;
5+
mod kernel;
56
mod metadata;
67
mod vtable;
78

vortex-array/src/kernel/mod.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3+
4+
//! Kernels represent the CPU physical plan for array execution.
5+
6+
use std::fmt::Debug;
7+
use std::fmt::Display;
8+
9+
use vortex_error::VortexResult;
10+
use vortex_mask::Mask;
11+
use vortex_vector::Vector;
12+
13+
/// A boxed reference to a kernel.
14+
pub type KernelRef = Box<dyn Kernel>;
15+
16+
/// A trait representing the physical CPU execution of an array tree.
17+
pub trait Kernel: 'static + Send + Debug {
18+
/// Execute the kernel and produce a vector result.
19+
fn execute(self: Box<Self>) -> VortexResult<Vector>;
20+
21+
/// Return the child kernels of this kernel.
22+
fn children(&self) -> Vec<&KernelRef>;
23+
24+
/// Report an estimated cost for computing this kernel over the given filter mask.
25+
///
26+
/// This is obviously a very rough estimate, but is used to decide when a filter should be
27+
/// pushed through a kernel using [`Kernel::push_down_filter`].
28+
///
29+
/// Return [`f64::INFINITY`] if the kernel has unknown cost, meaning filters will _always_
30+
/// be pushed through the kernel if possible.
31+
fn cost_estimate(&self, selection: &Mask) -> f64 {
32+
_ = selection;
33+
f64::INFINITY
34+
}
35+
36+
/// Push a selection mask through this kernel.
37+
///
38+
/// Return `Ok(None)` if the filter cannot be pushed down.
39+
fn push_down_filter(&self, selection: &Mask) -> VortexResult<Option<KernelRef>> {
40+
_ = selection;
41+
Ok(None)
42+
}
43+
}

vortex-array/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub mod expr;
3535
mod expression;
3636
mod hash;
3737
pub mod iter;
38+
mod kernel;
3839
mod mask;
3940
mod mask_future;
4041
mod metadata;

0 commit comments

Comments
 (0)