Skip to content

Commit 52c4e85

Browse files
authored
Map constant RowFn inputs directly
Signed-off-by: Connor Tsui <[email protected]>
1 parent beb7c15 commit 52c4e85

8 files changed

Lines changed: 264 additions & 47 deletions

File tree

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ struct InvalidKernelOutput;
6868
#[derive(Clone)]
6969
struct PackedPositive;
7070

71+
#[derive(Clone)]
72+
struct PackedGreaterThan;
73+
7174
#[derive(Clone)]
7275
struct ValidOnlyPositive;
7376

@@ -344,6 +347,27 @@ impl RowFn for PackedPositive {
344347
}
345348
}
346349

350+
impl RowFn for PackedGreaterThan {
351+
type Options = EmptyOptions;
352+
353+
const ARG_NAMES: &'static [&'static str] = &["lhs", "rhs"];
354+
const INFALLIBLE: bool = true;
355+
356+
fn id(&self) -> ScalarFnId {
357+
static ID: CachedId = CachedId::new("test.packed_greater_than");
358+
*ID
359+
}
360+
361+
fn dispatch<V: RowVisitor>(
362+
&self,
363+
_options: &Self::Options,
364+
_args: &[DType],
365+
visitor: V,
366+
) -> VortexResult<V::VisitResult> {
367+
visitor.visit::<(i64, i64), bool>(|(lhs, rhs)| lhs > rhs)
368+
}
369+
}
370+
347371
impl RowFn for ValidOnlyPositive {
348372
type Options = EmptyOptions;
349373

@@ -473,6 +497,39 @@ fn test_bool_output_word_boundaries(#[case] len: usize) -> VortexResult<()> {
473497
Ok(())
474498
}
475499

500+
#[test]
501+
fn test_bool_output_handles_partial_constants() -> VortexResult<()> {
502+
let values: Vec<_> = (0_i64..65).collect();
503+
let varying = PrimitiveArray::from_iter(values.iter().copied()).into_array();
504+
let constant = ConstantArray::new(32_i64, values.len()).into_array();
505+
let mut ctx = array_session().create_execution_ctx();
506+
507+
let lhs_varying = VecExecutionArgs::new(vec![varying.clone(), constant.clone()], values.len());
508+
let actual = execute_rows(&PackedGreaterThan, &EmptyOptions, &lhs_varying, &mut ctx)?;
509+
let expected = BoolArray::from_iter(values.iter().map(|value| *value > 32)).into_array();
510+
assert_arrays_eq!(&actual, &expected, &mut ctx);
511+
512+
let rhs_varying = VecExecutionArgs::new(vec![constant, varying], values.len());
513+
let actual = execute_rows(&PackedGreaterThan, &EmptyOptions, &rhs_varying, &mut ctx)?;
514+
let expected = BoolArray::from_iter(values.iter().map(|value| 32 > *value)).into_array();
515+
assert_arrays_eq!(&actual, &expected, &mut ctx);
516+
517+
Ok(())
518+
}
519+
520+
#[test]
521+
fn test_bool_output_handles_all_constant_input() -> VortexResult<()> {
522+
let input = ConstantArray::new(7_i64, 65).into_array();
523+
let args = VecExecutionArgs::new(vec![input], 65);
524+
let mut ctx = array_session().create_execution_ctx();
525+
526+
let actual = execute_rows(&PackedPositive, &EmptyOptions, &args, &mut ctx)?;
527+
let expected = BoolArray::from_iter(std::iter::repeat_n(true, 65)).into_array();
528+
529+
assert_arrays_eq!(&actual, &expected, &mut ctx);
530+
Ok(())
531+
}
532+
476533
#[test]
477534
fn test_bool_output_handles_nullary_rows() -> VortexResult<()> {
478535
let args = VecExecutionArgs::new(vec![], 65);

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

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
//!
66
//! [`execute_owned`] writes fallible row results into spare vector capacity and reduces compact
77
//! failure evidence outside the hot loop. [`execute_owned_infallible`] lets the output type map a
8-
//! validated non-constant row source directly into its physical representation.
8+
//! validated row source directly into its physical representation.
99
1010
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;
1516
use vortex_error::vortex_ensure_eq;
1617
use vortex_mask::MaskValuesRef;
@@ -21,6 +22,7 @@ use crate::scalar_fn::ExecutionArgs;
2122
use crate::scalar_fn::unstable::row::FailureEvidence;
2223
use crate::scalar_fn::unstable::row::IndexedElementTuple;
2324
use crate::scalar_fn::unstable::row::OutputElement;
25+
use crate::scalar_fn::unstable::row::types::decoded_source;
2426
use crate::scalar_fn::unstable::row::visitor::assert_owned_output_needs_no_drop;
2527

2628
/// Zero-sized failure accumulator for infallible owned visits.
@@ -48,38 +50,13 @@ where
4850
let prepared = prepare(Args::const_values(&columns));
4951
let row_count = args.row_count();
5052

51-
if let Some(views) = Args::views_if_no_consts(&columns) {
52-
vortex_ensure!(
53-
Args::view_lens_match(&views, row_count),
54-
"a decoded row input does not address exactly {row_count} rows",
55-
);
56-
57-
// SAFETY: `view_lens_match` checked that these exact retained views address `row_count`
58-
// rows.
59-
let source = unsafe { Args::indexed_source(views, row_count) };
60-
61-
return Ok(Out::build_from(source, |elements| {
62-
apply(&prepared, elements)
63-
}));
64-
}
65-
66-
vortex_ensure!(
67-
Args::decoded_lens_match(&columns, row_count),
68-
"a decoded row input does not address exactly {row_count} rows",
69-
);
70-
71-
let mut values = Vec::<Out>::with_capacity(row_count);
72-
let output = &mut values.spare_capacity_mut()[..row_count];
73-
74-
for (index, slot) in output.iter_mut().enumerate() {
75-
slot.write(apply(&prepared, Args::get(&columns, index)));
76-
}
77-
78-
// SAFETY: normal completion initializes every output slot exactly once, and `values` was
79-
// allocated with at least `row_count` capacity.
80-
unsafe { values.set_len(row_count) };
53+
let Some(source) = decoded_source::<Args>(&columns, row_count) else {
54+
vortex_bail!("a decoded row input does not address exactly {row_count} rows");
55+
};
8156

82-
Ok(Out::build(values))
57+
Ok(Out::build_from(source, |elements| {
58+
apply(&prepared, elements)
59+
}))
8360
}
8461

8562
/// Decode nullable inputs, then store one output for each valid row from an infallible kernel.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ pub unsafe trait InputElement: 'static {
9393
/// Borrow the representation used when this argument varies within the batch.
9494
///
9595
/// Called once before the hot loop. Constants do not use this view because the tuple adapter
96-
/// keeps their one-row decoded representation separate.
96+
/// keeps their one-row decoded representation separate. For every index below the returned
97+
/// view's length, [`get_from_view`](Self::get_from_view) must produce the same element as
98+
/// [`get`](Self::get) on `column`.
9799
fn view(column: &Self::Column) -> Self::View<'_>;
98100

99101
/// Read one row from a [`View`](Self::View).

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ mod tuple;
2121
pub use tuple::ElementTuple;
2222
pub use tuple::IndexedElementTuple;
2323
pub use tuple::batch_const;
24+
pub(in crate::scalar_fn::unstable::row) use tuple::decoded_source;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ use crate::scalar_fn::unstable::row::ViewLen;
2424
/// One decoded input, collapsed to a single row when it is constant for the batch.
2525
pub struct ArgColumn<T: InputElement>(
2626
/// The decoded argument, classified by how the row loop addresses it.
27-
ArgColumnKind<T>,
27+
pub(super) ArgColumnKind<T>,
2828
);
2929

30-
enum ArgColumnKind<T: InputElement> {
30+
pub(super) enum ArgColumnKind<T: InputElement> {
3131
/// A decoded column covering the full batch; executors validate its length before traversal.
3232
Column(T::Column),
3333

0 commit comments

Comments
 (0)