Skip to content

Commit 2cb8ddb

Browse files
committed
Add filter-and-scatter RowFn execution
Signed-off-by: Connor Tsui <[email protected]>
1 parent 28f7cf0 commit 2cb8ddb

8 files changed

Lines changed: 335 additions & 33 deletions

File tree

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
use vortex_error::VortexError;
55
use vortex_error::VortexResult;
6-
use vortex_error::vortex_panic;
76
use vortex_mask::Mask;
87
use vortex_mask::MaskValuesRef;
98

@@ -34,6 +33,7 @@ impl RowFnExecutionArgs {
3433
/// recompute the error.
3534
pub(super) fn execute_dense_with_retry(
3635
&self,
36+
kernel: impl Fn(BorrowedRowFnArgs<'_>, &mut ExecutionCtx) -> VortexResult<ArrayRef>,
3737
execute_dense_attempt: impl FnOnce(
3838
BorrowedRowFnArgs<'_>,
3939
&mut ExecutionCtx,
@@ -51,14 +51,15 @@ impl RowFnExecutionArgs {
5151
match attempt {
5252
DenseAttempt::Values(values) => self.finalize_dense_output(values, ctx),
5353
DenseAttempt::DeferredError(error) => {
54-
self.resolve_deferred_error(error, try_valid_rows, ctx)
54+
self.resolve_deferred_error(error, kernel, try_valid_rows, ctx)
5555
}
5656
}
5757
}
5858

5959
fn resolve_deferred_error(
6060
&self,
6161
deferred_error: VortexError,
62+
kernel: impl Fn(BorrowedRowFnArgs<'_>, &mut ExecutionCtx) -> VortexResult<ArrayRef>,
6263
try_valid_rows: impl FnOnce(
6364
BorrowedRowFnArgs<'_>,
6465
MaskValuesRef,
@@ -80,14 +81,11 @@ impl RowFnExecutionArgs {
8081
// retrying only observable rows.
8182
drop(deferred_error);
8283

83-
if let Some(result) = self.try_execute_valid_rows(try_valid_rows, valid_rows, ctx)? {
84+
if let Some(result) = self.try_execute_valid_rows(try_valid_rows, &valid_rows, ctx)? {
8485
return Ok(result);
8586
}
8687

87-
vortex_panic!(
88-
"dense retry requires direct valid-row support after a deferred error, but {} declined it",
89-
self.id,
90-
)
88+
self.filter_and_scatter(kernel, &valid_rows, ctx)
9189
}
9290

9391
fn finalize_dense_output(
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3+
4+
//! Fallback execution for row kernels that cannot operate on the original partially valid inputs.
5+
//!
6+
//! Direct skip-invalid execution retains the original row domain: it decodes every input column,
7+
//! initializes a full-length output, and visits only valid rows. Some input representations cannot
8+
//! decode unspecified payloads behind nulls, and some output sinks cannot initialize rows that the
9+
//! kernel skips. In either case, there is no safe row loop to enter over the original columns.
10+
//!
11+
//! This fallback filters every input to the valid row domain before decoding it. The ordinary
12+
//! kernel then produces a compact, all-valid output. A take with nullable indices scatters those
13+
//! values back to the original row domain and restores its nulls.
14+
//!
15+
//! Filtering and scattering add columnar work around the row loop. Filtering nested or compressed
16+
//! inputs can also materialize their selected representation. Batch execution therefore tries
17+
//! direct skip-invalid execution first and uses this path only when a required capability is
18+
//! unavailable.
19+
//!
20+
//! A prepared, type-specific view can provide selected-row decoding without filtering. That is an
21+
//! optional direct-execution capability rather than a replacement for this fallback: it requires a
22+
//! representation with safe selected access, and it does not initialize an output sink that cannot
23+
//! represent skipped rows. Calling the general scalar-extraction API once per valid row is also not
24+
//! equivalent because it repeats array execution and scalar construction inside the row loop.
25+
26+
use smallvec::SmallVec;
27+
use vortex_error::VortexResult;
28+
use vortex_mask::Mask;
29+
use vortex_mask::MaskValuesRef;
30+
31+
use super::super::RowFnExecutionArgs;
32+
use super::super::args::BorrowedRowFnArgs;
33+
use crate::ArrayRef;
34+
use crate::ExecutionCtx;
35+
use crate::IntoArray;
36+
use crate::arrays::PrimitiveArray;
37+
use crate::dtype::Nullability;
38+
use crate::validity::Validity;
39+
40+
impl RowFnExecutionArgs {
41+
/// Filter the original batch to valid rows, run the kernel, then restore its row count.
42+
pub(super) fn filter_and_scatter(
43+
&self,
44+
kernel: impl Fn(BorrowedRowFnArgs<'_>, &mut ExecutionCtx) -> VortexResult<ArrayRef>,
45+
original_validity: &MaskValuesRef,
46+
ctx: &mut ExecutionCtx,
47+
) -> VortexResult<ArrayRef> {
48+
let original_len = original_validity.len();
49+
let filtered_len = original_validity.true_count();
50+
let filter_mask = Mask::Values(MaskValuesRef::clone(original_validity));
51+
52+
let filtered_inputs: SmallVec<[ArrayRef; 4]> = self
53+
.inputs
54+
.iter()
55+
.map(|input| input.filter(filter_mask.clone()))
56+
.collect::<VortexResult<_>>()?;
57+
58+
let filtered_args = self.execution_args(&filtered_inputs, filtered_len);
59+
let filtered = kernel(filtered_args, ctx)?;
60+
let filtered = self.validate_kernel_output(filtered, filtered_len, ctx)?;
61+
62+
let output = Self::scatter_to_original_rows(filtered, original_validity)?;
63+
64+
self.finalize_output(output, original_len)
65+
}
66+
67+
/// Scatter `filtered` back to the rows selected by `original_validity`.
68+
fn scatter_to_original_rows(
69+
filtered: ArrayRef,
70+
original_validity: &MaskValuesRef,
71+
) -> VortexResult<ArrayRef> {
72+
let original_len = original_validity.len();
73+
let mut take_indices = vec![0u64; original_len];
74+
75+
let valid_rows = original_validity
76+
.slices()
77+
.iter()
78+
.flat_map(|&(start, end)| start..end);
79+
for (filtered_index, original_index) in valid_rows.enumerate() {
80+
take_indices[original_index] = u64::try_from(filtered_index)?;
81+
}
82+
83+
// Null indices restore invalid rows without selecting a value from the compact output.
84+
let take_indices = PrimitiveArray::new(
85+
take_indices,
86+
Validity::from_mask(
87+
Mask::Values(MaskValuesRef::clone(original_validity)),
88+
Nullability::Nullable,
89+
),
90+
)
91+
.into_array();
92+
93+
filtered.take(take_indices)
94+
}
95+
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use crate::validity::Validity;
2121

2222
mod constant;
2323
mod dense;
24+
mod filter_scatter;
2425
mod valid_only;
2526

2627
mod output;
@@ -29,8 +30,9 @@ pub(crate) use output::finalize_kernel_output;
2930
impl RowFnExecutionArgs {
3031
/// Apply constant folding and null handling around `kernel`.
3132
///
32-
/// For a partially valid batch, `try_valid_rows` executes only valid rows over the original
33-
/// inputs. Every result is checked against the planned shape and dtype.
33+
/// For a partially valid batch, `try_valid_rows` can avoid filtering. `Ok(None)` filters the
34+
/// valid rows and scatters the output back. Every result is checked against the planned shape
35+
/// and dtype.
3436
pub(crate) fn execute(
3537
&self,
3638
kernel: impl Fn(BorrowedRowFnArgs<'_>, &mut ExecutionCtx) -> VortexResult<ArrayRef>,
@@ -76,7 +78,7 @@ impl RowFnExecutionArgs {
7678
match self.plan.policy() {
7779
RowPolicy::Dense => self.execute_dense(kernel, ctx),
7880
RowPolicy::DenseWithRetry => {
79-
self.execute_dense_with_retry(execute_dense_attempt, try_valid_rows, ctx)
81+
self.execute_dense_with_retry(kernel, execute_dense_attempt, try_valid_rows, ctx)
8082
}
8183
RowPolicy::ValidOnly => self.execute_valid_only(kernel, try_valid_rows, ctx),
8284
}

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

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// SPDX-FileCopyrightText: Copyright the Vortex contributors
33

44
use vortex_error::VortexResult;
5-
use vortex_error::vortex_panic;
65
use vortex_mask::Mask;
76
use vortex_mask::MaskValuesRef;
87

@@ -14,12 +13,7 @@ use crate::IntoArray;
1413
use crate::builtins::ArrayBuiltins;
1514

1615
impl RowFnExecutionArgs {
17-
/// Resolve validity, then execute valid rows over the original inputs.
18-
///
19-
/// # Panics
20-
///
21-
/// Panics if the concrete row signature cannot use direct valid-row execution. Inputs must
22-
/// support null-tolerant decoding, and output sinks must initialize skipped rows.
16+
/// Resolve validity and try direct valid-row execution before filter-and-scatter.
2317
pub(super) fn execute_valid_only(
2418
&self,
2519
kernel: impl Fn(BorrowedRowFnArgs<'_>, &mut ExecutionCtx) -> VortexResult<ArrayRef>,
@@ -44,14 +38,11 @@ impl RowFnExecutionArgs {
4438
Mask::Values(valid_rows) => valid_rows,
4539
};
4640

47-
if let Some(result) = self.try_execute_valid_rows(try_valid_rows, valid_rows, ctx)? {
41+
if let Some(result) = self.try_execute_valid_rows(try_valid_rows, &valid_rows, ctx)? {
4842
return Ok(result);
4943
}
5044

51-
vortex_panic!(
52-
"valid-only execution requires direct valid-row support; {} selected an unsupported signature",
53-
self.id,
54-
)
45+
self.filter_and_scatter(kernel, &valid_rows, ctx)
5546
}
5647

5748
/// Try execution against the original inputs, then mask a returned full-length result.
@@ -62,12 +53,12 @@ impl RowFnExecutionArgs {
6253
MaskValuesRef,
6354
&mut ExecutionCtx,
6455
) -> VortexResult<Option<ArrayRef>>,
65-
valid: MaskValuesRef,
56+
valid: &MaskValuesRef,
6657
ctx: &mut ExecutionCtx,
6758
) -> VortexResult<Option<ArrayRef>> {
6859
let Some(values) = try_valid_rows(
6960
self.execution_args(&self.inputs, self.row_count),
70-
MaskValuesRef::clone(&valid),
61+
MaskValuesRef::clone(valid),
7162
ctx,
7263
)?
7364
else {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub(crate) struct RowFnExecutionArgs {
3939
/// The number of rows in the original execution scope.
4040
row_count: usize,
4141

42-
/// The input columns, collected once for validity, constant handling, and execution.
42+
/// The input columns, collected once for validity, constant folding, filtering, and execution.
4343
inputs: SmallVec<[ArrayRef; 4]>,
4444

4545
/// The input dtypes, collected with the columns and reused by both planning and execution.

0 commit comments

Comments
 (0)