Skip to content

Commit cfbd513

Browse files
committed
Attach RowFn validity during Boolean output construction
Let the packed Boolean collectors consume the batch validity when constructing their result. Keep custom output validation before masking and attach validity directly to other canonical outputs. Extracted from #9979. Signed-off-by: Connor Tsui <[email protected]>
1 parent d270200 commit cfbd513

9 files changed

Lines changed: 181 additions & 25 deletions

File tree

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,21 @@ use crate::ArrayRef;
99
use crate::ExecutionCtx;
1010
use crate::IntoArray;
1111
use crate::arrays::ConstantArray;
12+
use crate::validity::Validity;
1213

1314
impl RowFnExecutionArgs {
1415
/// Execute all-constant inputs by evaluating one row and broadcasting the validated result.
1516
pub(super) fn execute_all_constant(
1617
&self,
17-
kernel: impl Fn(BorrowedRowFnArgs<'_>, &mut ExecutionCtx) -> VortexResult<ArrayRef>,
18+
kernel: impl FnOnce(
19+
BorrowedRowFnArgs<'_>,
20+
&mut Option<Validity>,
21+
&mut ExecutionCtx,
22+
) -> VortexResult<ArrayRef>,
1823
ctx: &mut ExecutionCtx,
1924
) -> VortexResult<ArrayRef> {
2025
let result = self.validate_kernel_output(
21-
kernel(self.execution_args(&self.inputs, 1), ctx)?,
26+
kernel(self.execution_args(&self.inputs, 1), &mut None, ctx)?,
2227
1,
2328
ctx,
2429
)?;

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

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,39 @@ use vortex_mask::MaskValuesRef;
99
use super::super::RowFnExecutionArgs;
1010
use super::super::args::BorrowedRowFnArgs;
1111
use crate::ArrayRef;
12+
use crate::Canonical;
1213
use crate::ExecutionCtx;
14+
use crate::IntoArray;
15+
use crate::arrays::masked::mask_validity_canonical;
1316
use crate::builtins::ArrayBuiltins;
17+
use crate::canonical::AnyCanonical;
1418
use crate::scalar_fn::unstable::row::execute::DenseAttempt;
1519
use crate::validity::Validity;
1620

1721
impl RowFnExecutionArgs {
1822
/// Run every stored payload, then attach the input validity without materializing its mask.
1923
pub(super) fn execute_dense(
2024
&self,
21-
kernel: impl Fn(BorrowedRowFnArgs<'_>, &mut ExecutionCtx) -> VortexResult<ArrayRef>,
25+
kernel: impl FnOnce(
26+
BorrowedRowFnArgs<'_>,
27+
&mut Option<Validity>,
28+
&mut ExecutionCtx,
29+
) -> VortexResult<ArrayRef>,
2230
ctx: &mut ExecutionCtx,
2331
) -> VortexResult<ArrayRef> {
24-
let values = kernel(self.execution_args(&self.inputs, self.row_count), ctx)?;
32+
// Offer a partial validity to the kernel. A collector that takes it builds the output with
33+
// that validity attached, so the output needs neither a second construction nor an
34+
// all-valid check.
35+
let mut validity =
36+
matches!(self.validity, Validity::Array(_)).then(|| self.validity.clone());
37+
let values = kernel(
38+
self.execution_args(&self.inputs, self.row_count),
39+
&mut validity,
40+
ctx,
41+
)?;
42+
if validity.is_none() && matches!(self.validity, Validity::Array(_)) {
43+
return self.finalize_output(values, self.row_count);
44+
}
2545

2646
self.finalize_dense_output(values, ctx)
2747
}
@@ -107,7 +127,20 @@ impl RowFnExecutionArgs {
107127
Validity::NonNullable | Validity::AllValid => {
108128
self.finalize_output(values, self.row_count)
109129
}
110-
Validity::Array(valid) => self.finalize_output(values.mask(valid)?, self.row_count),
130+
// A canonical kernel output takes the validity directly. Masking it lazily would add
131+
// a scalar-function node that the caller must execute again just to attach it.
132+
Validity::Array(valid) => {
133+
let values = match values.as_opt::<AnyCanonical>() {
134+
Some(canonical) => mask_validity_canonical(
135+
Canonical::from(canonical),
136+
Validity::Array(valid),
137+
ctx,
138+
)?
139+
.into_array(),
140+
None => values.mask(valid)?,
141+
};
142+
self.finalize_output(values, self.row_count)
143+
}
111144
Validity::AllInvalid => {
112145
unreachable!("all-invalid validity is handled before dense row execution")
113146
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ impl RowFnExecutionArgs {
3636
/// and dtype.
3737
pub(crate) fn execute(
3838
&self,
39-
kernel: impl Fn(BorrowedRowFnArgs<'_>, &mut ExecutionCtx) -> VortexResult<ArrayRef>,
39+
kernel: impl FnOnce(
40+
BorrowedRowFnArgs<'_>,
41+
&mut Option<Validity>,
42+
&mut ExecutionCtx,
43+
) -> VortexResult<ArrayRef>,
4044
execute_dense_attempt: impl FnOnce(
4145
BorrowedRowFnArgs<'_>,
4246
&mut ExecutionCtx,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub(crate) fn finalize_kernel_output(
6868
) -> VortexResult<ArrayRef> {
6969
validate_output(id, result_dtype, expected_len, &values)?;
7070
vortex_ensure!(
71-
values.all_valid(ctx)?,
71+
!values.dtype().is_nullable() || values.all_valid(ctx)?,
7272
"the {id} row kernel must produce only valid rows, got at least one null row",
7373
);
7474

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@ use crate::ArrayRef;
1111
use crate::ExecutionCtx;
1212
use crate::IntoArray;
1313
use crate::builtins::ArrayBuiltins;
14+
use crate::validity::Validity;
1415

1516
impl RowFnExecutionArgs {
1617
/// Resolve validity and try direct valid-row execution before filtered execution.
1718
pub(super) fn execute_valid_only(
1819
&self,
19-
kernel: impl Fn(BorrowedRowFnArgs<'_>, &mut ExecutionCtx) -> VortexResult<ArrayRef>,
20+
kernel: impl FnOnce(
21+
BorrowedRowFnArgs<'_>,
22+
&mut Option<Validity>,
23+
&mut ExecutionCtx,
24+
) -> VortexResult<ArrayRef>,
2025
try_valid_rows: impl FnOnce(
2126
BorrowedRowFnArgs<'_>,
2227
MaskValuesRef,
@@ -34,7 +39,11 @@ impl RowFnExecutionArgs {
3439
let valid_rows = match validity {
3540
// An empty mask is both all-valid and all-null. Preserve the all-valid behavior.
3641
Mask::AllTrue(_) | Mask::AllFalse(0) => {
37-
let values = kernel(self.execution_args(&self.inputs, self.row_count), ctx)?;
42+
let values = kernel(
43+
self.execution_args(&self.inputs, self.row_count),
44+
&mut None,
45+
ctx,
46+
)?;
3847
let values = self.validate_kernel_output(values, self.row_count, ctx)?;
3948

4049
return self.finalize_output(values, self.row_count);

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ use crate::scalar_fn::unstable::row::IndexedElementTuple;
2828
use crate::scalar_fn::unstable::row::types::decoded_source;
2929
use crate::validity::Validity;
3030

31-
/// Decode every input column, then pack infallible Boolean outputs.
31+
/// Decode every input column, then pack infallible Boolean outputs with `validity` attached.
3232
pub(crate) fn execute_owned_infallible_bool<Args, const MULTIVERSIONED: bool>(
3333
args: &dyn ExecutionArgs,
3434
ctx: &mut ExecutionCtx,
35+
validity: Validity,
3536
apply: impl Fn(Args::Elems<'_>) -> bool,
3637
) -> VortexResult<ArrayRef>
3738
where
@@ -55,13 +56,15 @@ where
5556
BitBuffer::collect_bool_in(row_count, collect, ctx.allocator().clone())
5657
};
5758

58-
Ok(BoolArray::new(values, Validity::NonNullable).into_array())
59+
Ok(BoolArray::try_new(values, validity)?.into_array())
5960
}
6061

61-
/// Decode every input column, then pack Boolean outputs while combining failure evidence.
62+
/// Decode every input column, then pack Boolean outputs with `validity` attached while combining
63+
/// failure evidence.
6264
pub(crate) fn execute_owned_bool<Args, Prepared, Fail, const MULTIVERSIONED: bool>(
6365
args: &dyn ExecutionArgs,
6466
ctx: &mut ExecutionCtx,
67+
validity: Validity,
6568
prepare: impl FnOnce(Args::ConstElems<'_>) -> Prepared,
6669
apply: impl Fn(&Prepared, Args::Elems<'_>) -> (bool, Fail),
6770
finish_failure: impl FnOnce(Fail) -> VortexResult<()>,
@@ -104,7 +107,7 @@ where
104107

105108
finish_failure(failure)?;
106109

107-
Ok(BoolArray::new(values, Validity::NonNullable).into_array())
110+
Ok(BoolArray::try_new(values, validity)?.into_array())
108111
}
109112

110113
/// Pack a dense Boolean attempt and report rejected failure evidence to the batch executor.

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

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use crate::IntoArray;
3232
use crate::VortexSessionExecute;
3333
use crate::array_session;
3434
use crate::arrays::Bool;
35+
use crate::arrays::BoolArray;
3536
use crate::arrays::ConstantArray;
3637
use crate::arrays::FixedSizeList;
3738
use crate::arrays::Primitive;
@@ -190,22 +191,30 @@ fn packed_boolean_payload_uses_allocator(
190191
.with_allocator(allocator);
191192

192193
let output = match (multiversioned, traversal) {
193-
(false, Traversal::Infallible) => {
194-
execute_owned_infallible_bool::<(i64,), false>(&args, &mut ctx, |(value,)| value > 2)?
195-
}
196-
(true, Traversal::Infallible) => {
197-
execute_owned_infallible_bool::<(i64,), true>(&args, &mut ctx, |(value,)| value > 2)?
198-
}
194+
(false, Traversal::Infallible) => execute_owned_infallible_bool::<(i64,), false>(
195+
&args,
196+
&mut ctx,
197+
Validity::NonNullable,
198+
|(value,)| value > 2,
199+
)?,
200+
(true, Traversal::Infallible) => execute_owned_infallible_bool::<(i64,), true>(
201+
&args,
202+
&mut ctx,
203+
Validity::NonNullable,
204+
|(value,)| value > 2,
205+
)?,
199206
(false, Traversal::Fallible) => execute_owned_bool::<(i64,), (), bool, false>(
200207
&args,
201208
&mut ctx,
209+
Validity::NonNullable,
202210
|_| (),
203211
|_, (value,)| (value > 2, false),
204212
|_| Ok(()),
205213
)?,
206214
(true, Traversal::Fallible) => execute_owned_bool::<(i64,), (), bool, true>(
207215
&args,
208216
&mut ctx,
217+
Validity::NonNullable,
209218
|_| (),
210219
|_, (value,)| (value > 2, false),
211220
|_| Ok(()),
@@ -347,8 +356,12 @@ fn empty_outputs_and_zero_width_rows_do_not_allocate_payloads() -> VortexResult<
347356

348357
let primitive =
349358
execute_owned_infallible::<(i64,), i64, ()>(&args, &mut ctx, |_| (), |_, (value,)| value)?;
350-
let boolean =
351-
execute_owned_infallible_bool::<(i64,), false>(&args, &mut ctx, |(value,)| value > 0)?;
359+
let boolean = execute_owned_infallible_bool::<(i64,), false>(
360+
&args,
361+
&mut ctx,
362+
Validity::NonNullable,
363+
|(value,)| value > 0,
364+
)?;
352365
let scalar = execute_sink::<(i64,), (), UninitElementSink<i64>, _>(
353366
&args,
354367
&(),
@@ -495,3 +508,32 @@ fn zero_sized_output_uses_its_own_storage(#[case] traversal: Traversal) -> Vorte
495508

496509
Ok(())
497510
}
511+
512+
#[rstest]
513+
fn packed_boolean_attaches_validity(
514+
#[values(false, true)] multiversioned: bool,
515+
) -> VortexResult<()> {
516+
let values = PrimitiveArray::from_iter([1_i64, 2, 3, 4]).into_array();
517+
let args = VecExecutionArgs::new(vec![values], 4);
518+
let validity = Validity::from_iter([true, false, true, true]);
519+
let mut ctx = array_session().create_execution_ctx();
520+
521+
let output = if multiversioned {
522+
execute_owned_infallible_bool::<(i64,), true>(&args, &mut ctx, validity, |(value,)| {
523+
value > 2
524+
})?
525+
} else {
526+
execute_owned_infallible_bool::<(i64,), false>(&args, &mut ctx, validity, |(value,)| {
527+
value > 2
528+
})?
529+
};
530+
531+
assert!(output.is::<Bool>());
532+
let expected = BoolArray::new(
533+
[false, false, true, true].into_iter().collect(),
534+
Validity::from_iter([true, false, true, true]),
535+
)
536+
.into_array();
537+
assert_arrays_eq!(&output, &expected, &mut ctx);
538+
Ok(())
539+
}

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ use crate::scalar_fn::unstable::row::execute::execute_owned_valid_rows;
4444
use crate::scalar_fn::unstable::row::execute::execute_sink;
4545
use crate::scalar_fn::unstable::row::execute::execute_sink_filtered;
4646
use crate::scalar_fn::unstable::row::execute::execute_sink_valid_rows;
47+
use crate::validity::Validity;
4748

4849
/// The runtime visit that decodes every column once and runs the selected row loop.
4950
pub(crate) struct ExecuteRows<'args, 'ctx, F: RowFn> {
@@ -59,6 +60,9 @@ pub(crate) struct ExecuteRows<'args, 'ctx, F: RowFn> {
5960
/// The output dtype declared by [`RowVisitor::with_output_dtype`], if any.
6061
output_dtype: Option<DType>,
6162

63+
/// A batch validity that a collector able to attach it takes for its output.
64+
validity: &'ctx mut Option<Validity>,
65+
6266
/// The execution context used to decode the input columns.
6367
ctx: &'ctx mut ExecutionCtx,
6468

@@ -71,13 +75,15 @@ impl<'args, 'ctx, F: RowFn> ExecuteRows<'args, 'ctx, F> {
7175
args: &'args dyn ExecutionArgs,
7276
dtypes: &'args [DType],
7377
plan: &'args BatchPlan,
78+
validity: &'ctx mut Option<Validity>,
7479
ctx: &'ctx mut ExecutionCtx,
7580
) -> Self {
7681
Self {
7782
args,
7883
dtypes,
7984
plan,
8085
output_dtype: None,
86+
validity,
8187
ctx,
8288
function: PhantomData,
8389
}
@@ -129,7 +135,12 @@ impl<F: RowFn> RowVisitor for ExecuteRows<'_, '_, F> {
129135
)?;
130136
self.plan.ensure_reproduced_by(&visited)?;
131137

132-
execute_owned_infallible_bool::<Args, MULTIVERSIONED>(self.args, self.ctx, apply)
138+
execute_owned_infallible_bool::<Args, MULTIVERSIONED>(
139+
self.args,
140+
self.ctx,
141+
self.validity.take().unwrap_or(Validity::NonNullable),
142+
apply,
143+
)
133144
}
134145

135146
fn visit_prepared_into<Args, Sink, Prepared, ApplyResult>(
@@ -205,6 +216,7 @@ impl<F: RowFn> RowVisitor for ExecuteRows<'_, '_, F> {
205216
execute_owned_bool::<Args, Prepared, Fail, MULTIVERSIONED>(
206217
self.args,
207218
self.ctx,
219+
self.validity.take().unwrap_or(Validity::NonNullable),
208220
prepare,
209221
apply,
210222
finish_failure,

0 commit comments

Comments
 (0)