Skip to content

Commit 28f7cf0

Browse files
authored
Pack Boolean RowFn output directly (#9620)
## Summary - Tracking Issue: #9128 Lets infallible RowFn output types collect directly from a validated row source. Boolean output uses `BitBuffer::collect_bool` instead of materializing an intermediate `Vec<bool>`. This is the first layer of the stack. Constant inputs follow in #9628. ## Changes Adds a default `OutputElement::build_from` implementation and a Boolean override that writes the packed bitmap. A dedicated `execute_owned_infallible` route uses it when all inputs have validated non-constant views. Constant and valid-only execution keep their existing `Vec` paths. Tests cover packed values and nullability, bitmap word boundaries, nullary input, and skipped invalid rows. A focused Divan benchmark covers `i32` and `i64` Boolean output with varying inputs and both constant orientations. With rustc 1.98.0 and LLVM 22.1.8 targeting x86-64 AVX2 with 16 CGUs and no LTO, the varying-input loop now emits vector compares, `vpmovmskb`, and a direct packed-word store. The `develop` control writes unpacked Boolean bytes to a temporary vector before packing them. ## API Changes Adds `OutputElement::build_from`. Implementations must evaluate every source row exactly once in order, return the same values as `build`, and cannot introduce value-dependent errors or panics. Signed-off-by: Connor Tsui <[email protected]>
1 parent 0a128b6 commit 28f7cf0

6 files changed

Lines changed: 406 additions & 12 deletions

File tree

‎vortex-array/Cargo.toml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ harness = false
135135
name = "binary_ops"
136136
harness = false
137137

138+
[[bench]]
139+
name = "row_fn_output"
140+
harness = false
141+
138142
[[bench]]
139143
name = "kleene_bool"
140144
harness = false
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3+
4+
#![expect(
5+
clippy::cast_possible_truncation,
6+
reason = "benchmark fixtures reduce values below 1024 before narrowing"
7+
)]
8+
9+
use std::marker::PhantomData;
10+
use std::sync::LazyLock;
11+
12+
use divan::Bencher;
13+
use divan::counter::ItemsCount;
14+
use mimalloc::MiMalloc;
15+
use vortex_array::ArrayRef;
16+
use vortex_array::IntoArray;
17+
use vortex_array::VortexSessionExecute;
18+
use vortex_array::array_session;
19+
use vortex_array::arrays::ConstantArray;
20+
use vortex_array::arrays::PrimitiveArray;
21+
use vortex_array::dtype::DType;
22+
use vortex_array::dtype::NativePType;
23+
use vortex_array::scalar_fn::EmptyOptions;
24+
use vortex_array::scalar_fn::ScalarFnId;
25+
use vortex_array::scalar_fn::VecExecutionArgs;
26+
use vortex_array::scalar_fn::unstable::row::RowFn;
27+
use vortex_array::scalar_fn::unstable::row::RowVisitor;
28+
use vortex_array::scalar_fn::unstable::row::execute_rows;
29+
use vortex_error::VortexExpect;
30+
use vortex_error::VortexResult;
31+
use vortex_session::VortexSession;
32+
use vortex_session::registry::CachedId;
33+
34+
#[global_allocator]
35+
static GLOBAL: MiMalloc = MiMalloc;
36+
37+
fn main() {
38+
LazyLock::force(&SESSION);
39+
divan::main();
40+
}
41+
42+
static SESSION: LazyLock<VortexSession> = LazyLock::new(array_session);
43+
44+
const ROWS: usize = 1 << 14;
45+
46+
const INPUT_SHAPES: &[InputShape] = &[
47+
InputShape::PerRowPerRow,
48+
InputShape::PerRowConstant,
49+
InputShape::ConstantPerRow,
50+
];
51+
52+
#[derive(Clone, Copy, Debug)]
53+
enum InputShape {
54+
PerRowPerRow,
55+
PerRowConstant,
56+
ConstantPerRow,
57+
}
58+
59+
trait BenchPrimitive: NativePType {
60+
fn per_row(offset: usize) -> ArrayRef;
61+
62+
fn constant() -> ArrayRef;
63+
}
64+
65+
impl BenchPrimitive for i32 {
66+
fn per_row(offset: usize) -> ArrayRef {
67+
PrimitiveArray::from_iter((0..ROWS).map(|index| ((index + offset) % 1024) as i32))
68+
.into_array()
69+
}
70+
71+
fn constant() -> ArrayRef {
72+
ConstantArray::new(512_i32, ROWS).into_array()
73+
}
74+
}
75+
76+
impl BenchPrimitive for i64 {
77+
fn per_row(offset: usize) -> ArrayRef {
78+
PrimitiveArray::from_iter((0..ROWS).map(|index| ((index + offset) % 1024) as i64))
79+
.into_array()
80+
}
81+
82+
fn constant() -> ArrayRef {
83+
ConstantArray::new(512_i64, ROWS).into_array()
84+
}
85+
}
86+
87+
#[derive(Clone)]
88+
struct InfallibleBool<T>(PhantomData<T>);
89+
90+
impl<T: BenchPrimitive> RowFn for InfallibleBool<T> {
91+
type Options = EmptyOptions;
92+
93+
const ARG_NAMES: &'static [&'static str] = &["lhs", "rhs"];
94+
const INFALLIBLE: bool = true;
95+
96+
fn id(&self) -> ScalarFnId {
97+
static ID: CachedId = CachedId::new("bench.row_fn_output.infallible_bool");
98+
*ID
99+
}
100+
101+
fn dispatch<V: RowVisitor>(
102+
&self,
103+
_options: &Self::Options,
104+
_args: &[DType],
105+
visitor: V,
106+
) -> VortexResult<V::VisitResult> {
107+
visitor.visit::<(T, T), bool>(|(lhs, rhs)| lhs.is_lt(rhs))
108+
}
109+
}
110+
111+
#[vortex_bench_support::cpu_features]
112+
#[divan::bench(types = [i32, i64], args = INPUT_SHAPES)]
113+
fn infallible_bool<T: BenchPrimitive>(bencher: Bencher, &shape: &InputShape) {
114+
let function = InfallibleBool::<T>(PhantomData);
115+
let args = match shape {
116+
InputShape::PerRowPerRow => vec![T::per_row(0), T::per_row(1)],
117+
InputShape::PerRowConstant => vec![T::per_row(0), T::constant()],
118+
InputShape::ConstantPerRow => vec![T::constant(), T::per_row(1)],
119+
};
120+
let args = VecExecutionArgs::new(args, ROWS);
121+
122+
bencher
123+
.counter(ItemsCount::new(ROWS))
124+
.with_inputs(|| (&args, SESSION.create_execution_ctx()))
125+
.bench_refs(|(args, ctx)| {
126+
execute_rows(&function, &EmptyOptions, *args, ctx)
127+
.vortex_expect("row execution should succeed in benchmark")
128+
});
129+
}

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

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::sync::atomic::AtomicUsize;
66
use std::sync::atomic::Ordering;
77

88
use rstest::rstest;
9+
use vortex_buffer::Buffer;
910
use vortex_buffer::BufferMut;
1011
use vortex_error::VortexResult;
1112
use vortex_error::vortex_bail;
@@ -15,9 +16,11 @@ use vortex_session::registry::CachedId;
1516

1617
use super::finalize_kernel_output;
1718
use crate::ArrayRef;
19+
use crate::ExecutionCtx;
1820
use crate::IntoArray;
1921
use crate::VortexSessionExecute;
2022
use crate::array_session;
23+
use crate::arrays::BoolArray;
2124
use crate::arrays::ConstantArray;
2225
use crate::arrays::ExtensionArray;
2326
use crate::arrays::FixedSizeListArray;
@@ -35,6 +38,7 @@ use crate::scalar_fn::ScalarFnId;
3538
use crate::scalar_fn::VecExecutionArgs;
3639
use crate::scalar_fn::unstable::row::FixedSizeListSink;
3740
use crate::scalar_fn::unstable::row::InitializedRow;
41+
use crate::scalar_fn::unstable::row::InputElement;
3842
use crate::scalar_fn::unstable::row::OutputElement;
3943
use crate::scalar_fn::unstable::row::OutputSink;
4044
use crate::scalar_fn::unstable::row::RowFn;
@@ -61,6 +65,51 @@ struct ValidOnlyIdentity;
6165
#[derive(Clone)]
6266
struct InvalidKernelOutput;
6367

68+
#[derive(Clone)]
69+
struct PackedPositive;
70+
71+
#[derive(Clone)]
72+
struct ValidOnlyPositive;
73+
74+
#[derive(Clone)]
75+
struct NullaryTrue;
76+
77+
struct ValidOnlyI64;
78+
79+
// SAFETY: the view is a slice, and its reported length is the buffer length.
80+
unsafe impl InputElement for ValidOnlyI64 {
81+
type Column = Buffer<i64>;
82+
type View<'a> = &'a [i64];
83+
type Elem<'a> = i64;
84+
85+
const DENSE_SAFE: bool = false;
86+
const DECODE_INFALLIBLE: bool = true;
87+
88+
fn validate(dtype: &DType) -> VortexResult<()> {
89+
<i64 as InputElement>::validate(dtype)
90+
}
91+
92+
fn decode(array: ArrayRef, ctx: &mut ExecutionCtx) -> VortexResult<Self::Column> {
93+
<i64 as InputElement>::decode(array, ctx)
94+
}
95+
96+
fn can_decode_null_tolerant(_array: &ArrayRef) -> VortexResult<bool> {
97+
Ok(true)
98+
}
99+
100+
fn get(column: &Self::Column, index: usize) -> Self::Elem<'_> {
101+
column[index]
102+
}
103+
104+
fn view(column: &Self::Column) -> Self::View<'_> {
105+
column.as_slice()
106+
}
107+
108+
fn get_from_view<'a>(view: &Self::View<'a>, index: usize) -> Self::Elem<'a> {
109+
view[index]
110+
}
111+
}
112+
64113
/// Produces a null row to exercise output validation at the row-function boundary.
65114
#[derive(Default)]
66115
struct NullProducingI64(i64);
@@ -274,6 +323,72 @@ fn test_fixed_size_list_sink_uses_runtime_width(
274323
Ok(())
275324
}
276325

326+
impl RowFn for PackedPositive {
327+
type Options = EmptyOptions;
328+
329+
const ARG_NAMES: &'static [&'static str] = &["value"];
330+
const INFALLIBLE: bool = true;
331+
332+
fn id(&self) -> ScalarFnId {
333+
static ID: CachedId = CachedId::new("test.packed_positive");
334+
*ID
335+
}
336+
337+
fn dispatch<V: RowVisitor>(
338+
&self,
339+
_options: &Self::Options,
340+
_args: &[DType],
341+
visitor: V,
342+
) -> VortexResult<V::VisitResult> {
343+
visitor.visit::<(i64,), bool>(|(value,)| value > 0)
344+
}
345+
}
346+
347+
impl RowFn for ValidOnlyPositive {
348+
type Options = EmptyOptions;
349+
350+
const ARG_NAMES: &'static [&'static str] = &["value"];
351+
const INFALLIBLE: bool = true;
352+
353+
fn id(&self) -> ScalarFnId {
354+
static ID: CachedId = CachedId::new("test.valid_only_positive");
355+
*ID
356+
}
357+
358+
fn dispatch<V: RowVisitor>(
359+
&self,
360+
_options: &Self::Options,
361+
_args: &[DType],
362+
visitor: V,
363+
) -> VortexResult<V::VisitResult> {
364+
visitor.visit::<(ValidOnlyI64,), bool>(|(value,)| {
365+
assert_ne!(value, i64::MIN, "an invalid row was evaluated");
366+
value > 0
367+
})
368+
}
369+
}
370+
371+
impl RowFn for NullaryTrue {
372+
type Options = EmptyOptions;
373+
374+
const ARG_NAMES: &'static [&'static str] = &[];
375+
const INFALLIBLE: bool = true;
376+
377+
fn id(&self) -> ScalarFnId {
378+
static ID: CachedId = CachedId::new("test.nullary_true");
379+
*ID
380+
}
381+
382+
fn dispatch<V: RowVisitor>(
383+
&self,
384+
_options: &Self::Options,
385+
_args: &[DType],
386+
visitor: V,
387+
) -> VortexResult<V::VisitResult> {
388+
visitor.visit::<(), bool>(|()| true)
389+
}
390+
}
391+
277392
#[test]
278393
fn test_finalize_kernel_output_rejects_nested_dtype_mismatch() -> VortexResult<()> {
279394
static ID: CachedId = CachedId::new("test.finalize_kernel_output");
@@ -320,6 +435,73 @@ fn test_kernel_output_rejects_nulls_at_function_boundary() -> VortexResult<()> {
320435
Ok(())
321436
}
322437

438+
#[test]
439+
fn test_bool_output_builds_packed_values() -> VortexResult<()> {
440+
let input = PrimitiveArray::new(
441+
vec![1_i64, -1, 2, 0, 3],
442+
Validity::from_iter([true, true, false, true, true]),
443+
)
444+
.into_array();
445+
let args = VecExecutionArgs::new(vec![input], 5);
446+
let mut ctx = array_session().create_execution_ctx();
447+
448+
let actual = execute_rows(&PackedPositive, &EmptyOptions, &args, &mut ctx)?;
449+
let expected =
450+
BoolArray::from_iter([Some(true), Some(false), None, Some(false), Some(true)]).into_array();
451+
452+
assert_arrays_eq!(&actual, &expected, &mut ctx);
453+
Ok(())
454+
}
455+
456+
#[rstest]
457+
#[case::empty(0)]
458+
#[case::one(1)]
459+
#[case::below_word(63)]
460+
#[case::exact_word(64)]
461+
#[case::above_word(65)]
462+
#[case::multiword_remainder(130)]
463+
fn test_bool_output_word_boundaries(#[case] len: usize) -> VortexResult<()> {
464+
let values: Vec<_> = (0..len).map(|index| index as i64 - 32).collect();
465+
let input = PrimitiveArray::from_iter(values.iter().copied()).into_array();
466+
let args = VecExecutionArgs::new(vec![input], len);
467+
let mut ctx = array_session().create_execution_ctx();
468+
469+
let actual = execute_rows(&PackedPositive, &EmptyOptions, &args, &mut ctx)?;
470+
let expected = BoolArray::from_iter(values.iter().map(|value| *value > 0)).into_array();
471+
472+
assert_arrays_eq!(&actual, &expected, &mut ctx);
473+
Ok(())
474+
}
475+
476+
#[test]
477+
fn test_bool_output_handles_nullary_rows() -> VortexResult<()> {
478+
let args = VecExecutionArgs::new(vec![], 65);
479+
let mut ctx = array_session().create_execution_ctx();
480+
481+
let actual = execute_rows(&NullaryTrue, &EmptyOptions, &args, &mut ctx)?;
482+
let expected = BoolArray::from_iter(std::iter::repeat_n(true, 65)).into_array();
483+
484+
assert_arrays_eq!(&actual, &expected, &mut ctx);
485+
Ok(())
486+
}
487+
488+
#[test]
489+
fn test_valid_only_bool_output_skips_invalid_rows() -> VortexResult<()> {
490+
let input = PrimitiveArray::new(
491+
vec![1_i64, i64::MIN, -1],
492+
Validity::from_iter([true, false, true]),
493+
)
494+
.into_array();
495+
let args = VecExecutionArgs::new(vec![input], 3);
496+
let mut ctx = array_session().create_execution_ctx();
497+
498+
let actual = execute_rows(&ValidOnlyPositive, &EmptyOptions, &args, &mut ctx)?;
499+
let expected = BoolArray::from_iter([Some(true), None, Some(false)]).into_array();
500+
501+
assert_arrays_eq!(&actual, &expected, &mut ctx);
502+
Ok(())
503+
}
504+
323505
#[test]
324506
fn test_deferred_owned_execution_retries_null_row_failure() -> VortexResult<()> {
325507
let function = DeferredAdd::default();

0 commit comments

Comments
 (0)