Skip to content

Commit d4dc3f2

Browse files
authored
Implement RowFn row execution (#9353)
## Rationale for this change - Tracking issue: #9130 - API tracking: #9129 - Epic: #9128 Implements typed row execution for the contracts in #9386. Batch planning and orchestration are split into #9450. ## What changes are included in this PR? Adds owned-output and sink-writing row loops, prepared and deferred execution, the `ViewLen` contract, and decoding validation. This layer does not yet connect the row loops to `execute_rows`. ## What APIs are changed? Are there any user-facing changes? Extends the existing `unstable_row_fns` API. There are no stable API changes. --------- Signed-off-by: Connor Tsui <[email protected]>
1 parent 42a72d9 commit d4dc3f2

23 files changed

Lines changed: 1302 additions & 181 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3+
4+
//! Row-loop execution for owned outputs and output sinks.
5+
//!
6+
//! [`owned`] stores one independent value per row and reduces compact failure evidence. [`sink`]
7+
//! drives output builders whose row handles may share batch state. Both return [`RowExecution`],
8+
//! which distinguishes a completed array from a deferred error that batch validity may suppress.
9+
10+
mod owned;
11+
pub(super) use owned::execute_owned;
12+
pub(super) use owned::execute_owned_infallible;
13+
14+
mod outcome;
15+
pub use outcome::RowExecution;
16+
17+
mod sink;
18+
pub(super) use sink::execute_sink;
19+
pub(super) use sink::execute_sink_valid_rows;
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+
//! The result of a completed row loop before batch-level null handling.
5+
//!
6+
//! [`RowExecution`] preserves deferred failure evidence until batch execution can determine whether
7+
//! the failing payload belonged to a valid row.
8+
9+
use vortex_error::VortexError;
10+
use vortex_error::VortexResult;
11+
12+
use crate::ArrayRef;
13+
14+
/// The outcome of a row loop before batch execution decides whether an error is observable.
15+
///
16+
/// Together with the surrounding [`VortexResult`], this represents three outcomes:
17+
///
18+
/// - `Err(error)` is a non-retryable execution or immediate row error.
19+
/// - [`Output`](Self::Output) is a successful row loop.
20+
/// - [`DeferredError`](Self::DeferredError) is failure evidence from a completed row loop.
21+
///
22+
/// A dense loop can evaluate null payloads, so its deferred error is not always observable. Batch
23+
/// execution can retry only valid rows to discard errors caused by null payloads. A plain
24+
/// `VortexResult<ArrayRef>` cannot distinguish these errors from failures that a retry cannot fix.
25+
///
26+
/// Once execution is known to contain only valid rows, converting this outcome into a
27+
/// `VortexResult<ArrayRef>` turns [`DeferredError`](Self::DeferredError) into an ordinary error.
28+
pub enum RowExecution {
29+
/// The successfully built, full-length output column.
30+
Output(ArrayRef),
31+
32+
/// An error constructed from failure evidence reduced across a completed row loop.
33+
DeferredError(VortexError),
34+
}
35+
36+
impl From<RowExecution> for VortexResult<ArrayRef> {
37+
fn from(execution: RowExecution) -> Self {
38+
match execution {
39+
RowExecution::Output(output) => Ok(output),
40+
RowExecution::DeferredError(error) => Err(error),
41+
}
42+
}
43+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3+
4+
//! Executes row kernels that return one independent owned value per row.
5+
//!
6+
//! [`execute_owned`] decodes inputs once, prepares constant state, writes into spare vector
7+
//! capacity, and reduces compact failure evidence without putting error construction in the hot
8+
//! loop. [`execute_owned_infallible`] removes that failure path for infallible kernels.
9+
10+
use std::ops::BitOrAssign;
11+
12+
use vortex_compute::lane_kernels::IndexedSourceExt;
13+
use vortex_error::VortexResult;
14+
use vortex_error::vortex_ensure;
15+
16+
use super::RowExecution;
17+
use crate::ExecutionCtx;
18+
use crate::scalar_fn::ExecutionArgs;
19+
use crate::scalar_fn::unstable::row::FailureEvidence;
20+
use crate::scalar_fn::unstable::row::IndexedElementTuple;
21+
use crate::scalar_fn::unstable::row::OutputElement;
22+
use crate::scalar_fn::unstable::row::ViewLen;
23+
use crate::scalar_fn::unstable::row::visitor::assert_owned_output_needs_no_drop;
24+
25+
/// Zero-sized failure accumulator for infallible owned visits.
26+
#[derive(Clone, Copy, Default)]
27+
struct NoFailure;
28+
29+
impl BitOrAssign for NoFailure {
30+
fn bitor_assign(&mut self, _rhs: Self) {}
31+
}
32+
33+
/// Decode every input column, then store one output per row from an infallible kernel.
34+
pub(crate) fn execute_owned_infallible<Args, Out, Prepared>(
35+
args: &dyn ExecutionArgs,
36+
ctx: &mut ExecutionCtx,
37+
prepare: impl FnOnce(Args::ConstElems<'_>) -> Prepared,
38+
apply: impl Fn(&Prepared, Args::Elems<'_>) -> Out,
39+
) -> VortexResult<RowExecution>
40+
where
41+
Args: IndexedElementTuple,
42+
Out: OutputElement,
43+
{
44+
execute_owned::<Args, Out, Prepared, NoFailure>(
45+
args,
46+
ctx,
47+
prepare,
48+
move |prepared, args| (apply(prepared, args), NoFailure),
49+
|_| Ok(()),
50+
)
51+
}
52+
53+
/// Decode every input column, then store outputs and combine per-row failure evidence.
54+
pub(crate) fn execute_owned<Args, Out, Prepared, Fail>(
55+
args: &dyn ExecutionArgs,
56+
ctx: &mut ExecutionCtx,
57+
prepare: impl FnOnce(Args::ConstElems<'_>) -> Prepared,
58+
apply: impl Fn(&Prepared, Args::Elems<'_>) -> (Out, Fail),
59+
finish_failure: impl FnOnce(Fail) -> VortexResult<()>,
60+
) -> VortexResult<RowExecution>
61+
where
62+
Args: IndexedElementTuple,
63+
Out: OutputElement,
64+
Fail: FailureEvidence,
65+
{
66+
// The output vector stays at length zero until every slot is initialized so that an unwind
67+
// abandons partially initialized spare capacity. This no-drop assertion proves that no
68+
// initialized value requires a destructor to run.
69+
const { assert_owned_output_needs_no_drop::<Out>() };
70+
71+
let columns = Args::decode(args, ctx)?;
72+
let prepared = prepare(Args::const_values(&columns));
73+
74+
let row_count = args.row_count();
75+
let mut values = Vec::<Out>::with_capacity(row_count);
76+
let output = &mut values.spare_capacity_mut()[..row_count];
77+
78+
let failure = if let Some(views) = Args::views_if_no_consts(&columns) {
79+
// Keep this validation beside the views so LLVM sees their common length here.
80+
vortex_ensure!(
81+
Args::ARITY == 0 || views.len() == row_count,
82+
"a decoded row input does not address exactly {row_count} rows",
83+
);
84+
85+
// SAFETY: the tuple length check proved every non-nullary view addresses exactly
86+
// `row_count` rows immediately above. Nullary tuples do not access an input view.
87+
let source = unsafe { Args::indexed_source(views, row_count) };
88+
89+
source.map_checked_into(output, |elements| apply(&prepared, elements))
90+
} else {
91+
// Keep this proof branch-local. Shared validation prevents LLVM from specializing this
92+
// loop for each batch-constant arrangement, leaving it scalar under multiple CGUs without
93+
// LTO.
94+
vortex_ensure!(
95+
Args::decoded_lens_match(&columns, row_count),
96+
"a decoded row input does not address exactly {row_count} rows",
97+
);
98+
99+
let mut accumulated = Fail::default();
100+
101+
// Iterate over `output` directly. A `0..row_count` range reuses the address-taken value
102+
// from the validation error formatter and retains an output bounds check.
103+
for (index, slot) in output.iter_mut().enumerate() {
104+
// LLVM unswitches the batch-constant checks in `Args::get` before vectorizing the loop.
105+
let (value, row_failure) = apply(&prepared, Args::get(&columns, index));
106+
107+
slot.write(value);
108+
accumulated |= row_failure;
109+
}
110+
111+
accumulated
112+
};
113+
114+
// SAFETY: normal completion of either execution path initializes `0..row_count` exactly
115+
// once, and `values` was allocated with at least `row_count` capacity.
116+
unsafe { values.set_len(row_count) };
117+
118+
// Defer failures so batch execution can retry with only valid rows.
119+
match finish_failure(failure) {
120+
Ok(()) => Ok(RowExecution::Output(Out::build(values))),
121+
Err(error) => Ok(RowExecution::DeferredError(error)),
122+
}
123+
}

0 commit comments

Comments
 (0)