Skip to content

Commit e11bc39

Browse files
committed
Execute primitive numeric operators with RowFn
Signed-off-by: Connor Tsui <[email protected]>
1 parent 69fae0c commit e11bc39

6 files changed

Lines changed: 248 additions & 368 deletions

File tree

‎vortex-array/src/scalar/typed_view/primitive/numeric_operator.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use vortex_error::vortex_err;
1010

1111
use crate::scalar_fn::fns::operators::Operator;
1212

13-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1414
/// Binary element-wise operations.
1515
pub enum NumericOperator {
1616
/// Binary element-wise addition of two arrays or of two scalars.
Lines changed: 11 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: Copyright the Vortex contributors
33

4-
//! Checked-lane execution for numeric kernels, driven by the shared
4+
//! Checked-lane execution for the decimal kernels, driven by the shared
55
//! `vortex-compute` lane kernels.
6-
7-
use std::ops::BitOrAssign;
6+
//!
7+
//! The primitive widths do not come through here: they are computed one row at a time by
8+
//! [`row`](super::row), which writes a value for every row and reduces failure evidence without
9+
//! scanning the finished output.
810
911
use vortex_buffer::Buffer;
1012
use vortex_buffer::BufferMut;
@@ -13,33 +15,18 @@ use vortex_compute::lane_kernels::IndexedSourceExt;
1315
use vortex_mask::AllOr;
1416
use vortex_mask::Mask;
1517

16-
/// Evidence that a lane failed, anything other than [`Default`] meaning failure.
17-
///
18-
/// `bool` is the ordinary choice; [`map_checked_into`] explains why the wider members exist and
19-
/// asserts the width bound that membership here does **not** imply.
20-
///
21-
/// [`map_checked_into`]: IndexedSourceExt::map_checked_into
22-
pub(super) trait Failure: Copy + Default + PartialEq + BitOrAssign {}
23-
24-
impl Failure for bool {}
25-
impl Failure for u8 {}
26-
impl Failure for u16 {}
27-
impl Failure for u32 {}
28-
impl Failure for u64 {}
29-
3018
/// Apply the fallible `apply` over every lane of `source`, returning
31-
/// `Err(first_failing_valid_lane)` only when it returns `None` on a _valid_ lane.
19+
/// `Err(first_failing_valid_lane)` only when it returns `None` on a valid lane.
3220
///
3321
/// `apply` also runs on invalid lanes, whose failures are masked out and whose values are
3422
/// unspecified, so it must be total: no panics or side effects on any stored lane value.
3523
///
36-
/// This drives the one-pass early-exit kernels, which abort at the end of the enclosing 64-lane
37-
/// chunk. Use it when per-lane failure handling is cheap relative to the operation, as in integer
38-
/// division. Prefer [`checked_apply_lanes`] when the failure check itself vectorizes.
24+
/// This drives the one-pass early-exit kernels: failures abort at the end of the enclosing
25+
/// 64-lane chunk. It suits an operation whose per-lane failure handling is cheap relative to the
26+
/// operation itself, which is what the decimal kernels and their per-lane casts are.
3927
///
40-
/// `#[inline]`: this must inline into the caller that builds the closure, so that a captured
41-
/// constant operand flattens into a register rather than living behind a pointer the loop reloads
42-
/// on every lane, which blocks vectorization.
28+
/// Keep this wrapper inlineable so captured constants can become loop invariants in the caller.
29+
/// The lane kernels retain their own inlining decisions.
4330
#[inline]
4431
pub(super) fn checked_lanes<S, T, Apply>(
4532
source: S,
@@ -61,7 +48,6 @@ where
6148
};
6249

6350
let mut values = BufferMut::<T>::with_capacity(len);
64-
6551
let out = &mut values.spare_capacity_mut()[..len];
6652
match valid_bits {
6753
None => source.try_map_into(out, apply)?,
@@ -73,55 +59,3 @@ where
7359

7460
Ok(values.freeze())
7561
}
76-
77-
/// Apply the split value/failure `apply` over every lane of `source`, returning
78-
/// `Err(first_failing_valid_lane)` only when it flags a _valid_ lane.
79-
///
80-
/// The hot pass writes every value unconditionally and OR-reduces one piece of evidence, leaving
81-
/// the loop free of per-lane selects and per-chunk exit branches. Only if a lane flagged does a
82-
/// cold second pass re-run `apply` through the early-exit kernels, which drop null-lane failures
83-
/// and attribute the first valid one.
84-
///
85-
/// Like [`checked_lanes`], `apply` runs on invalid lanes and must be total.
86-
///
87-
/// `#[inline]`: see [`checked_lanes`].
88-
#[inline]
89-
pub(super) fn checked_apply_lanes<S, T, Fail, Apply>(
90-
source: S,
91-
valid_rows: &Mask,
92-
apply: Apply,
93-
) -> Result<Buffer<T>, usize>
94-
where
95-
S: IndexedSource + Copy,
96-
T: Copy + Default,
97-
Fail: Failure,
98-
Apply: Fn(S::Item) -> (T, Fail),
99-
{
100-
let len = source.len();
101-
debug_assert_eq!(len, valid_rows.len());
102-
103-
let valid_bits = match valid_rows.bit_buffer() {
104-
AllOr::All => None,
105-
AllOr::None => return Ok(Buffer::zeroed(len)),
106-
AllOr::Some(valid_bits) => Some(valid_bits),
107-
};
108-
109-
let mut values = BufferMut::<T>::with_capacity(len);
110-
111-
let out = &mut values.spare_capacity_mut()[..len];
112-
if source.map_checked_into(out, &apply) != Fail::default() {
113-
let checked = |item: S::Item| {
114-
let (value, failure) = apply(item);
115-
(failure == Fail::default()).then_some(value)
116-
};
117-
match valid_bits {
118-
None => source.try_map_into(out, checked)?,
119-
Some(valid_bits) => source.try_map_masked_into(valid_bits, out, checked)?,
120-
}
121-
}
122-
123-
// SAFETY: the kernels initialize every lane in `out`.
124-
unsafe { values.set_len(len) };
125-
126-
Ok(values.freeze())
127-
}

‎vortex-array/src/scalar_fn/fns/binary/numeric/mod.rs‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@
44
//! Native execution of the arithmetic operators (Add/Sub/Mul/Div) of the [`Binary`] scalar
55
//! function. There is no Arrow fallback.
66
//!
7+
//! The primitive widths are computed by a
8+
//! [`RowFn`](crate::scalar_fn::unstable::row::RowFn), which owns null handling, constants, and
9+
//! validity for them; see [`row`]. Decimal keeps its own columnar implementation in [`decimal`].
10+
//!
711
//! [`Binary`]: super::Binary
812
913
mod checked;
1014
mod decimal;
1115
mod primitive;
12-
#[cfg(test)]
13-
mod tests;
16+
mod row;
1417

1518
use decimal::execute_numeric_decimal;
16-
use primitive::execute_numeric_primitive;
19+
use row::execute_numeric_primitive;
1720
use vortex_error::VortexResult;
1821
use vortex_error::vortex_ensure;
1922

@@ -81,3 +84,6 @@ fn build_empty_result(
8184

8285
Ok(Canonical::empty(&result_dtype).into_array())
8386
}
87+
88+
#[cfg(test)]
89+
mod tests;

0 commit comments

Comments
 (0)