Skip to content

Commit d90f471

Browse files
committed
Execute owned RowFn outputs over valid rows
Signed-off-by: Connor Tsui <[email protected]>
1 parent e0e3fe7 commit d90f471

5 files changed

Lines changed: 176 additions & 12 deletions

File tree

‎vortex-array/src/scalar_fn/unstable/row/batch/tests.rs‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,17 @@ struct OriginalInputReducer;
4141
#[derive(Clone)]
4242
struct InvalidEncodedReduction;
4343

44+
#[derive(Clone)]
45+
struct DeferredAdd;
46+
4447
#[derive(Clone)]
4548
struct ValidOnlyIdentity;
4649

4750
#[derive(Clone)]
4851
struct InvalidKernelOutput;
4952

5053
/// Produces a null row to exercise output validation at the row-function boundary.
54+
#[derive(Default)]
5155
struct NullProducingI64(i64);
5256

5357
impl OutputElement for NullProducingI64 {
@@ -164,6 +168,36 @@ impl RowFn for InvalidEncodedReduction {
164168
}
165169
}
166170

171+
impl RowFn for DeferredAdd {
172+
type Options = EmptyOptions;
173+
174+
const ARG_NAMES: &'static [&'static str] = &["lhs", "rhs"];
175+
const INFALLIBLE: bool = false;
176+
177+
fn id(&self) -> ScalarFnId {
178+
static ID: CachedId = CachedId::new("test.deferred_add");
179+
*ID
180+
}
181+
182+
fn dispatch<V: RowVisitor<Self::Options>>(
183+
&self,
184+
_options: &Self::Options,
185+
_args: &[DType],
186+
visitor: V,
187+
) -> VortexResult<V::VisitResult> {
188+
visitor.visit_deferred::<(i64, i64), i64, bool>(
189+
|(lhs, rhs)| lhs.overflowing_add(rhs),
190+
|overflowed| {
191+
if overflowed {
192+
vortex_bail!(InvalidArgument: "deferred addition overflowed");
193+
}
194+
195+
Ok(())
196+
},
197+
)
198+
}
199+
}
200+
167201
impl RowFn for ValidOnlyIdentity {
168202
type Options = EmptyOptions;
169203

@@ -335,6 +369,21 @@ fn test_kernel_output_rejects_nulls_at_function_boundary() -> VortexResult<()> {
335369
Ok(())
336370
}
337371

372+
#[test]
373+
fn test_deferred_owned_execution_skips_invalid_rows() -> VortexResult<()> {
374+
let validity = Validity::from_iter([true, false]);
375+
let lhs = PrimitiveArray::new(vec![1_i64, i64::MAX], validity.clone()).into_array();
376+
let rhs = ConstantArray::new(1_i64, 2).into_array();
377+
let args = VecExecutionArgs::new(vec![lhs, rhs], 2);
378+
let mut ctx = array_session().create_execution_ctx();
379+
380+
let actual = execute_rows(&DeferredAdd, &EmptyOptions, &args, &mut ctx)?;
381+
let expected = PrimitiveArray::new(vec![2_i64, 0], validity).into_array();
382+
383+
assert_arrays_eq!(&actual, &expected, &mut ctx);
384+
Ok(())
385+
}
386+
338387
#[test]
339388
fn test_valid_only_empty_batch_preserves_nonnullable_dtype() -> VortexResult<()> {
340389
let input = PrimitiveArray::from_iter(std::iter::empty::<i64>()).into_array();

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
mod owned;
1010
pub(super) use owned::execute_owned;
1111
pub(super) use owned::execute_owned_infallible;
12+
pub(super) use owned::execute_owned_infallible_valid_rows;
13+
pub(super) use owned::execute_owned_valid_rows;
1214

1315
mod sink;
1416
pub(super) use sink::execute_sink;

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

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ use std::ops::BitOrAssign;
1111

1212
use vortex_compute::lane_kernels::IndexedSourceExt;
1313
use vortex_error::VortexResult;
14+
use vortex_error::vortex_bail;
1415
use vortex_error::vortex_ensure;
16+
use vortex_error::vortex_ensure_eq;
17+
use vortex_mask::AllOr;
18+
use vortex_mask::Mask;
1519

1620
use crate::ArrayRef;
1721
use crate::ExecutionCtx;
@@ -49,6 +53,103 @@ where
4953
)
5054
}
5155

56+
/// Decode nullable inputs, then store one output for each valid row from an infallible kernel.
57+
pub(crate) fn execute_owned_infallible_valid_rows<Args, Out, Prepared>(
58+
args: &dyn ExecutionArgs,
59+
valid: &Mask,
60+
ctx: &mut ExecutionCtx,
61+
prepare: impl FnOnce(Args::ConstElems<'_>) -> Prepared,
62+
apply: impl Fn(&Prepared, Args::Elems<'_>) -> Out,
63+
) -> VortexResult<Option<ArrayRef>>
64+
where
65+
Args: IndexedElementTuple,
66+
Out: OutputElement,
67+
{
68+
execute_owned_valid_rows::<Args, Out, Prepared, NoFailure>(
69+
args,
70+
valid,
71+
ctx,
72+
prepare,
73+
move |prepared, args| (apply(prepared, args), NoFailure),
74+
|_| Ok(()),
75+
)
76+
}
77+
78+
/// Decode nullable inputs, then store outputs and combine failure evidence for valid rows.
79+
pub(crate) fn execute_owned_valid_rows<Args, Out, Prepared, Fail>(
80+
args: &dyn ExecutionArgs,
81+
valid: &Mask,
82+
ctx: &mut ExecutionCtx,
83+
prepare: impl FnOnce(Args::ConstElems<'_>) -> Prepared,
84+
apply: impl Fn(&Prepared, Args::Elems<'_>) -> (Out, Fail),
85+
finish_failure: impl FnOnce(Fail) -> VortexResult<()>,
86+
) -> VortexResult<Option<ArrayRef>>
87+
where
88+
Args: IndexedElementTuple,
89+
Out: OutputElement,
90+
Fail: FailureEvidence,
91+
{
92+
const { assert_owned_output_needs_no_drop::<Out>() };
93+
94+
let Some(columns) = Args::decode_null_tolerant(args, ctx)? else {
95+
return Ok(None);
96+
};
97+
98+
let row_count = args.row_count();
99+
let AllOr::Some(valid_rows) = valid.bit_buffer() else {
100+
vortex_bail!(
101+
"execute_owned_valid_rows requires valid and invalid rows, got an all-valid or all-invalid mask"
102+
);
103+
};
104+
vortex_ensure_eq!(
105+
valid_rows.len(),
106+
row_count,
107+
"the validity mask must address exactly {row_count} rows, got {}",
108+
valid_rows.len(),
109+
);
110+
111+
let prepared = prepare(Args::const_values(&columns));
112+
let mut values: Vec<Out> = std::iter::repeat_with(Out::default)
113+
.take(row_count)
114+
.collect();
115+
let mut failure = Fail::default();
116+
117+
if let Some(views) = Args::views_if_no_consts(&columns) {
118+
vortex_ensure!(
119+
Args::view_lens_match(&views, row_count),
120+
"a decoded row input does not address exactly {row_count} rows",
121+
);
122+
123+
valid_rows.for_each_set_index(|index| {
124+
// SAFETY: the tuple-wide length check proved every view has `row_count` rows, and mask
125+
// indices are below `row_count`. Nullary tuples do not access an input view.
126+
let elements = unsafe { Args::get_from_views_unchecked(&views, index) };
127+
let (value, row_failure) = apply(&prepared, elements);
128+
129+
// SAFETY: the mask length check proved that every set index is below `row_count`.
130+
unsafe { *values.get_unchecked_mut(index) = value };
131+
failure |= row_failure;
132+
});
133+
} else {
134+
vortex_ensure!(
135+
Args::decoded_lens_match(&columns, row_count),
136+
"a decoded row input does not address exactly {row_count} rows",
137+
);
138+
139+
valid_rows.for_each_set_index(|index| {
140+
let (value, row_failure) = apply(&prepared, Args::get(&columns, index));
141+
142+
// SAFETY: the mask length check proved that every set index is below `row_count`.
143+
unsafe { *values.get_unchecked_mut(index) = value };
144+
failure |= row_failure;
145+
});
146+
}
147+
148+
finish_failure(failure)?;
149+
150+
Ok(Some(Out::build(values)))
151+
}
152+
52153
/// Decode every input column, then store outputs and combine per-row failure evidence.
53154
pub(crate) fn execute_owned<Args, Out, Prepared, Fail>(
54155
args: &dyn ExecutionArgs,

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ use crate::ArrayRef;
99
use crate::dtype::DType;
1010

1111
/// An owned row value that can be built into an all-valid column.
12-
pub trait OutputElement: 'static + Sized {
12+
///
13+
/// Skip-invalid execution uses [`Default`] only as a placeholder for invalid rows. Batch execution
14+
/// masks those rows before returning the output.
15+
pub trait OutputElement: 'static + Sized + Default {
1316
/// The dtype of columns built from this element type. **Must** be non-nullable: nullability is
1417
/// derived from the inputs by batch execution.
1518
///

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

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ use crate::scalar_fn::unstable::row::RowFn;
3232
use crate::scalar_fn::unstable::row::SinkResult;
3333
use crate::scalar_fn::unstable::row::execute::execute_owned;
3434
use crate::scalar_fn::unstable::row::execute::execute_owned_infallible;
35+
use crate::scalar_fn::unstable::row::execute::execute_owned_infallible_valid_rows;
36+
use crate::scalar_fn::unstable::row::execute::execute_owned_valid_rows;
3537
use crate::scalar_fn::unstable::row::execute::execute_sink;
3638
use crate::scalar_fn::unstable::row::execute::execute_sink_valid_rows;
3739

@@ -159,8 +161,8 @@ impl<F: RowFn> RowVisitor<F::Options> for ExecuteRows<'_, '_, F> {
159161

160162
/// The runtime visit that executes valid rows over the original input columns.
161163
///
162-
/// Only output sinks have a contract for skipped output positions. Owned visits therefore decline
163-
/// so batch execution can filter the valid inputs and scatter the output back.
164+
/// Owned outputs initialize skipped positions with [`Default::default`]. Output sinks use
165+
/// their own skipped-row initializer.
164166
pub(crate) struct ExecuteValidRows<'args, 'ctx, F: RowFn> {
165167
/// The original inputs for this kernel invocation.
166168
args: &'args dyn ExecutionArgs,
@@ -213,8 +215,8 @@ impl<F: RowFn> RowVisitor<F::Options> for ExecuteValidRows<'_, '_, F> {
213215

214216
fn visit_prepared<Args, Out, Prepared>(
215217
self,
216-
_prepare: impl FnOnce(Args::ConstElems<'_>) -> Prepared,
217-
_apply: impl Fn(&Prepared, Args::Elems<'_>) -> Out,
218+
prepare: impl FnOnce(Args::ConstElems<'_>) -> Prepared,
219+
apply: impl Fn(&Prepared, Args::Elems<'_>) -> Out,
218220
) -> VortexResult<Self::VisitResult>
219221
where
220222
Args: IndexedElementTuple,
@@ -228,9 +230,9 @@ impl<F: RowFn> RowVisitor<F::Options> for ExecuteValidRows<'_, '_, F> {
228230
RowPolicy::for_owned_output::<Args>(),
229231
)?;
230232

231-
// Owned execution has no sink that can initialize skipped output positions. Decline so
232-
// batch execution filters the inputs and retries with the dense visitor.
233-
Ok(None)
233+
execute_owned_infallible_valid_rows::<Args, Out, Prepared>(
234+
self.args, self.valid, self.ctx, prepare, apply,
235+
)
234236
}
235237

236238
fn visit_prepared_into<Args, Sink, Prepared, ApplyResult>(
@@ -262,9 +264,9 @@ impl<F: RowFn> RowVisitor<F::Options> for ExecuteValidRows<'_, '_, F> {
262264

263265
fn visit_prepared_deferred<Args, Out, Prepared, Fail>(
264266
self,
265-
_prepare: impl FnOnce(Args::ConstElems<'_>) -> Prepared,
266-
_apply: impl Fn(&Prepared, Args::Elems<'_>) -> (Out, Fail),
267-
_finish_failure: impl FnOnce(Fail) -> VortexResult<()>,
267+
prepare: impl FnOnce(Args::ConstElems<'_>) -> Prepared,
268+
apply: impl Fn(&Prepared, Args::Elems<'_>) -> (Out, Fail),
269+
finish_failure: impl FnOnce(Fail) -> VortexResult<()>,
268270
) -> VortexResult<Self::VisitResult>
269271
where
270272
Args: IndexedElementTuple,
@@ -279,7 +281,14 @@ impl<F: RowFn> RowVisitor<F::Options> for ExecuteValidRows<'_, '_, F> {
279281
RowPolicy::for_deferred_output::<Args>(),
280282
)?;
281283

282-
Ok(None)
284+
execute_owned_valid_rows::<Args, Out, Prepared, Fail>(
285+
self.args,
286+
self.valid,
287+
self.ctx,
288+
prepare,
289+
apply,
290+
finish_failure,
291+
)
283292
}
284293
}
285294

0 commit comments

Comments
 (0)