Skip to content

Commit b748502

Browse files
authored
Simplify RowFn execution contracts (#9496)
## Rationale for this change - Progress towards #9130 - API tracking: #9129 - Epic: #9128 Separates the mechanical `RowFn` cleanup from batch execution in #9450. ## What changes are included in this PR? Renames the fallibility constants to the positive `INFALLIBLE` contract, removes `RowExecution` and `DenseWithRetry`, and returns arrays directly from the executors. The tuple guard checks every decoded view length before unchecked row access because `ViewLen::len` returns one member length after its debug assertion. ## What APIs are changed? Are there any user-facing changes? Renames the unstable `RowFn::FALLIBLE` and `SinkResult::FALLIBLE` associated constants to `INFALLIBLE`. There are no stable API changes. --------- Signed-off-by: Connor Tsui <[email protected]>
1 parent 604cd33 commit b748502

17 files changed

Lines changed: 89 additions & 179 deletions

File tree

‎vortex-array/src/scalar_fn/unstable/row/execute/mod.rs‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,12 @@
44
//! Row-loop execution for owned outputs and output sinks.
55
//!
66
//! [`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.
7+
//! drives output builders whose row handles may share batch state.
98
109
mod owned;
1110
pub(super) use owned::execute_owned;
1211
pub(super) use owned::execute_owned_infallible;
1312

14-
mod outcome;
15-
pub use outcome::RowExecution;
16-
1713
mod sink;
1814
pub(super) use sink::execute_sink;
1915
pub(super) use sink::execute_sink_valid_rows;

‎vortex-array/src/scalar_fn/unstable/row/execute/outcome.rs‎

Lines changed: 0 additions & 43 deletions
This file was deleted.

‎vortex-array/src/scalar_fn/unstable/row/execute/owned.rs‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ use vortex_compute::lane_kernels::IndexedSourceExt;
1313
use vortex_error::VortexResult;
1414
use vortex_error::vortex_ensure;
1515

16-
use super::RowExecution;
16+
use crate::ArrayRef;
1717
use crate::ExecutionCtx;
1818
use crate::scalar_fn::ExecutionArgs;
1919
use crate::scalar_fn::unstable::row::FailureEvidence;
2020
use crate::scalar_fn::unstable::row::IndexedElementTuple;
2121
use crate::scalar_fn::unstable::row::OutputElement;
22-
use crate::scalar_fn::unstable::row::ViewLen;
2322
use crate::scalar_fn::unstable::row::visitor::assert_owned_output_needs_no_drop;
2423

2524
/// Zero-sized failure accumulator for infallible owned visits.
@@ -36,7 +35,7 @@ pub(crate) fn execute_owned_infallible<Args, Out, Prepared>(
3635
ctx: &mut ExecutionCtx,
3736
prepare: impl FnOnce(Args::ConstElems<'_>) -> Prepared,
3837
apply: impl Fn(&Prepared, Args::Elems<'_>) -> Out,
39-
) -> VortexResult<RowExecution>
38+
) -> VortexResult<ArrayRef>
4039
where
4140
Args: IndexedElementTuple,
4241
Out: OutputElement,
@@ -57,7 +56,7 @@ pub(crate) fn execute_owned<Args, Out, Prepared, Fail>(
5756
prepare: impl FnOnce(Args::ConstElems<'_>) -> Prepared,
5857
apply: impl Fn(&Prepared, Args::Elems<'_>) -> (Out, Fail),
5958
finish_failure: impl FnOnce(Fail) -> VortexResult<()>,
60-
) -> VortexResult<RowExecution>
59+
) -> VortexResult<ArrayRef>
6160
where
6261
Args: IndexedElementTuple,
6362
Out: OutputElement,
@@ -78,12 +77,12 @@ where
7877
let failure = if let Some(views) = Args::views_if_no_consts(&columns) {
7978
// Keep this validation beside the views so LLVM sees their common length here.
8079
vortex_ensure!(
81-
Args::ARITY == 0 || views.len() == row_count,
80+
Args::view_lens_match(&views, row_count),
8281
"a decoded row input does not address exactly {row_count} rows",
8382
);
8483

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.
84+
// SAFETY: `view_lens_match` checked that these exact retained views address `row_count`
85+
// rows.
8786
let source = unsafe { Args::indexed_source(views, row_count) };
8887

8988
source.map_checked_into(output, |elements| apply(&prepared, elements))
@@ -115,9 +114,8 @@ where
115114
// once, and `values` was allocated with at least `row_count` capacity.
116115
unsafe { values.set_len(row_count) };
117116

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-
}
117+
// Defer rich error construction until after the row loop.
118+
finish_failure(failure)?;
119+
120+
Ok(Out::build(values))
123121
}

‎vortex-array/src/scalar_fn/unstable/row/execute/sink.rs‎

Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use vortex_error::vortex_ensure_eq;
1414
use vortex_mask::AllOr;
1515
use vortex_mask::Mask;
1616

17-
use super::RowExecution;
17+
use crate::ArrayRef;
1818
use crate::ExecutionCtx;
1919
use crate::scalar_fn::ExecutionArgs;
2020
use crate::scalar_fn::unstable::row::ElementTuple;
@@ -32,7 +32,7 @@ pub(crate) fn execute_sink<Args, Prepared, Sink, ApplyResult, Options>(
3232
ctx: &mut ExecutionCtx,
3333
prepare: impl FnOnce(Args::ConstElems<'_>) -> Prepared,
3434
apply: impl Fn(&Prepared, Args::Elems<'_>, <Sink as OutputSink<Options>>::Row<'_>) -> ApplyResult,
35-
) -> VortexResult<RowExecution>
35+
) -> VortexResult<ArrayRef>
3636
where
3737
Args: ElementTuple,
3838
Sink: OutputSink<Options>,
@@ -60,13 +60,13 @@ where
6060

6161
let views = Args::views_if_no_consts(&columns);
6262
if let Some(views) = views {
63-
if Args::ARITY != 0 && views.len() != row_count {
63+
if !Args::view_lens_match(&views, row_count) {
6464
decoded_length_error(row_count)?;
6565
}
6666

6767
for index in 0..row_count {
68-
// SAFETY: the tuple length check proved every non-nullary view has `row_count`
69-
// rows before the loop. Nullary tuples do not access an input view.
68+
// SAFETY: `view_lens_match` checked that these exact retained views address
69+
// `row_count` rows before the loop.
7070
let elements = unsafe { Args::get_from_views_unchecked(&views, index) };
7171
// SAFETY: the sink row-count check above proved every loop index is in bounds.
7272
let output =
@@ -92,21 +92,21 @@ where
9292
}
9393

9494
// SAFETY: every row callback completed successfully, so each returned the required write token.
95-
unsafe { <Sink as OutputSink<Options>>::finish(sink) }.map(RowExecution::Output)
95+
unsafe { <Sink as OutputSink<Options>>::finish(sink) }
9696
}
9797

9898
/// Write only the rows set in `valid`, or decline when the inputs or sink cannot support
9999
/// skip-invalid execution.
100100
///
101-
/// `Ok(None)` signals batch execution to filter every input to the valid rows, run the dense
102-
/// kernel, and scatter the results back into a null-padded array.
101+
/// `Ok(None)` signals that direct skip-invalid execution is unavailable. Batch execution decides
102+
/// how to handle the decline.
103103
pub(crate) fn execute_sink_valid_rows<Args, Prepared, Sink, ApplyResult, Options>(
104104
args: &dyn ExecutionArgs,
105105
valid: &Mask,
106106
ctx: &mut ExecutionCtx,
107107
prepare: impl FnOnce(Args::ConstElems<'_>) -> Prepared,
108108
apply: impl Fn(&Prepared, Args::Elems<'_>, <Sink as OutputSink<Options>>::Row<'_>) -> ApplyResult,
109-
) -> VortexResult<Option<RowExecution>>
109+
) -> VortexResult<Option<ArrayRef>>
110110
where
111111
Args: ElementTuple,
112112
Sink: OutputSink<Options>,
@@ -144,7 +144,7 @@ where
144144
);
145145

146146
if let Some(views) = views {
147-
if Args::ARITY != 0 && views.len() != row_count {
147+
if !Args::view_lens_match(&views, row_count) {
148148
decoded_length_error(row_count)?;
149149
}
150150

@@ -154,9 +154,8 @@ where
154154
let output =
155155
unsafe { <Sink as OutputSink<Options>>::row_unchecked(&mut rows, index) };
156156

157-
// SAFETY: the tuple length check proved every non-nullary view has `row_count`
158-
// rows, and mask indices are below `row_count`. Nullary tuples do not access an
159-
// input view.
157+
// SAFETY: `view_lens_match` checked that these exact retained views address
158+
// `row_count` rows, and mask indices are below `row_count`.
160159
let elements = unsafe { Args::get_from_views_unchecked(&views, index) };
161160

162161
apply(&prepared, elements, output).into_result()
@@ -179,9 +178,7 @@ where
179178

180179
// SAFETY: the initializer completed before traversal, and every visited callback completed
181180
// successfully and returned the required write token.
182-
unsafe { <Sink as OutputSink<Options>>::finish(sink) }
183-
.map(RowExecution::Output)
184-
.map(Some)
181+
unsafe { <Sink as OutputSink<Options>>::finish(sink) }.map(Some)
185182
}
186183

187184
/// Construct a decoded-length error outside the traversal branches.
@@ -267,21 +264,17 @@ mod tests {
267264
use vortex_error::vortex_err;
268265
use vortex_mask::Mask;
269266

270-
use super::RowExecution;
271267
use super::execute_sink_valid_rows;
272268
use crate::ArrayRef;
273269
use crate::IntoArray;
274270
use crate::VortexSessionExecute;
275271
use crate::array_session;
276272
use crate::arrays::PrimitiveArray;
277-
use crate::assert_arrays_eq;
278273
use crate::dtype::DType;
279274
use crate::dtype::NativePType;
280275
use crate::scalar_fn::EmptyOptions;
281276
use crate::scalar_fn::VecExecutionArgs;
282-
use crate::scalar_fn::unstable::row::InitializedElement;
283277
use crate::scalar_fn::unstable::row::OutputSink;
284-
use crate::scalar_fn::unstable::row::UninitElementSink;
285278
use crate::validity::Validity;
286279

287280
struct NonSkippingSink;
@@ -370,39 +363,6 @@ mod tests {
370363
Ok(())
371364
}
372365

373-
#[test]
374-
fn test_skip_invalid_sink_initializes_and_writes_addressed_rows() -> VortexResult<()> {
375-
let input = PrimitiveArray::from_iter([10_i64, 20, 30]).into_array();
376-
let args = VecExecutionArgs::new(vec![input], 3);
377-
let valid = Mask::from_iter([true, false, true]);
378-
let mut ctx = array_session().create_execution_ctx();
379-
380-
let execution = execute_sink_valid_rows::<
381-
(i64,),
382-
(),
383-
UninitElementSink<i64>,
384-
InitializedElement,
385-
EmptyOptions,
386-
>(
387-
&args,
388-
&valid,
389-
&mut ctx,
390-
|_| (),
391-
|_, (value,), output| {
392-
// SAFETY: `output` is the row supplied to this callback.
393-
unsafe { InitializedElement::write(output, value * 2) }
394-
},
395-
)?;
396-
let Some(RowExecution::Output(actual)) = execution else {
397-
vortex_bail!("the skip-invalid sink must produce an output");
398-
};
399-
let expected = PrimitiveArray::from_iter([20_i64, 0, 60]);
400-
401-
assert_arrays_eq!(&actual, expected.as_ref(), &mut ctx);
402-
403-
Ok(())
404-
}
405-
406366
#[test]
407367
fn test_skip_invalid_sink_rechecks_rows_after_initialization() -> VortexResult<()> {
408368
let input = PrimitiveArray::from_iter([10_i64, 20]).into_array();

‎vortex-array/src/scalar_fn/unstable/row/mod.rs‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@
1414
//! Unlike a general strict function, a [`RowFn`] cannot produce null from valid inputs.
1515
//!
1616
//! Prepared visits move work derived from constant operands outside the hot loop. Deferred visits
17-
//! reduce compact failure evidence in that loop and retry only valid rows when null payloads may
18-
//! have caused the failure.
17+
//! reduce compact failure evidence without constructing errors in that loop.
1918
2019
// TODO(connor)[RowFn]: Remove this expectation when #9450 connects the batch executor.
2120
#[expect(dead_code)]
2221
mod execute;
23-
pub use execute::RowExecution;
2422

2523
mod row_fn;
2624
pub use row_fn::RowFn;

‎vortex-array/src/scalar_fn/unstable/row/row_fn.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ pub trait RowFn: 'static + Sized + Clone + Send + Sync {
4040
/// The arguments in display order. Its length is the function's exact arity.
4141
const ARG_NAMES: &'static [&'static str];
4242

43-
/// Whether any dispatch can raise a semantic error.
43+
/// Whether every dispatch is infallible.
4444
///
4545
/// See [`ScalarFnVTable::is_fallible`](crate::scalar_fn::ScalarFnVTable::is_fallible) for a
4646
/// more detailed explanation of semantic errors.
4747
///
48-
/// The framework checks dispatched element and result types. A conservative `true` is allowed.
49-
const FALLIBLE: bool;
48+
/// The framework checks dispatched element and result types. A conservative `false` is allowed.
49+
const INFALLIBLE: bool;
5050

5151
/// Returns the ID of the scalar function.
5252
fn id(&self) -> ScalarFnId;

‎vortex-array/src/scalar_fn/unstable/row/types/element/input.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ pub unsafe trait InputElement: 'static {
5454

5555
/// Decode `array` into its column representation.
5656
///
57-
/// Called once per row-kernel invocation, including deferred-error retries. Hoist dtype checks,
58-
/// downcasts, and other invocation-invariant work into this method.
57+
/// Called once per row-kernel invocation. Hoist dtype checks, downcasts, and other
58+
/// invocation-invariant work into this method.
5959
fn decode(array: ArrayRef, ctx: &mut ExecutionCtx) -> VortexResult<Self::Column>;
6060

6161
/// Whether [`decode_null_tolerant`](Self::decode_null_tolerant) can decode this array.

‎vortex-array/src/scalar_fn/unstable/row/types/element/tuple/element_tuple.rs‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,6 @@ pub trait ElementTuple: 'static + private::Sealed {
175175
fn validate(dtypes: &[DType]) -> VortexResult<()>;
176176

177177
/// Decode every input column once for one row-kernel invocation.
178-
///
179-
/// A dense deferred-error retry starts another invocation over filtered valid rows.
180178
fn decode(args: &dyn ExecutionArgs, ctx: &mut ExecutionCtx) -> VortexResult<Self::Columns>;
181179

182180
/// Whether every input can be decoded without assuming that all rows are valid.
@@ -206,15 +204,19 @@ pub trait ElementTuple: 'static + private::Sealed {
206204
/// returned tuple removes batch-constant checks from the row loop.
207205
fn views_if_no_consts(columns: &Self::Columns) -> Option<Self::Views<'_>>;
208206

209-
/// Whether every argument decoded at full batch length contains exactly `row_count` rows.
207+
/// Whether every borrowed view contains exactly `row_count` rows.
210208
///
211-
/// NB: `Columns` cannot implement [`ViewLen`] because a batch-constant [`ArgColumn`] physically
212-
/// contains one decoded row while logically addressing every row in the batch. By contrast,
213-
/// [`views_if_no_consts`](Self::views_if_no_consts) constructs `Views` only when every argument
214-
/// is non-constant, so those views have one common physical length.
209+
/// Executors must validate each retained view before unchecked traversal. [`ViewLen::len`] on
210+
/// a tuple asserts that component lengths are equal and panics on a mismatch. Rebuilding views
211+
/// from [`Columns`](Self::Columns) does not prove the lengths of the views passed to
212+
/// [`get_from_views_unchecked`](Self::get_from_views_unchecked).
213+
fn view_lens_match(views: &Self::Views<'_>, row_count: usize) -> bool;
214+
215+
/// Whether every argument decoded at full batch length contains exactly `row_count` rows.
215216
///
216-
/// This check runs once before the hot loop. A batch constant is exempt because its
217-
/// [`ArgColumn`] constructor already validated the one-row representation produced by decoding.
217+
/// `Columns` cannot implement [`ViewLen`] because a batch-constant `ArgColumn` stores one
218+
/// decoded row while logically addressing the full batch. The batch-constant constructor
219+
/// validates that one-row representation, so this method checks only non-constant columns.
218220
fn decoded_lens_match(columns: &Self::Columns, row_count: usize) -> bool;
219221

220222
/// Read one row from borrowed views.
@@ -281,6 +283,10 @@ impl ElementTuple for () {
281283
Some(())
282284
}
283285

286+
fn view_lens_match(_views: &Self::Views<'_>, _row_count: usize) -> bool {
287+
true
288+
}
289+
284290
fn decoded_lens_match(_columns: &Self::Columns, _row_count: usize) -> bool {
285291
true
286292
}
@@ -361,6 +367,10 @@ macro_rules! element_tuple {
361367
Some(($($t::view(columns.$idx.as_column()?),)+))
362368
}
363369

370+
fn view_lens_match(views: &Self::Views<'_>, row_count: usize) -> bool {
371+
$(views.$idx.len() == row_count &&)+ true
372+
}
373+
364374
fn decoded_lens_match(columns: &Self::Columns, row_count: usize) -> bool {
365375
$(columns.$idx.addresses_rows(row_count) &&)+ true
366376
}

‎vortex-array/src/scalar_fn/unstable/row/types/element/tuple/tests.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ i64_test_element!(
9090
);
9191
i64_test_element!(DenseFallible, false);
9292

93+
#[test]
94+
fn test_view_lens_match_checks_each_view() {
95+
let first: &[i64] = &[1, 2];
96+
let second: &[i64] = &[3];
97+
98+
assert!(!<(i64, i64)>::view_lens_match(&(first, second), 2));
99+
}
100+
93101
#[test]
94102
fn test_null_tolerant_decline_precedes_decoding() -> VortexResult<()> {
95103
DECODE_CALLS.store(0, Ordering::Relaxed);

0 commit comments

Comments
 (0)