Skip to content

Commit dffc937

Browse files
committed
refactor(array): drop the Narrow zip kernel for the if_false branch
ZipExecuteAdaptor dispatches only on the if_true branch, so the second kernel needed its own execute-parent impl and a branch enum. A Narrow in the if_false branch now takes the default path, as every other encoding does. Signed-off-by: Connor Tsui <connor.tsui20@gmail.com>
1 parent 46fe3e3 commit dffc937

4 files changed

Lines changed: 25 additions & 85 deletions

File tree

‎vortex-array/src/arrays/narrow/compute/mod.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub(crate) mod numeric;
3030
pub(crate) mod rules;
3131
mod slice;
3232
mod take;
33-
pub(crate) mod zip;
33+
mod zip;
3434

3535
use vortex_error::VortexResult;
3636

Lines changed: 22 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,52 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: Copyright the Vortex contributors
33

4-
use vortex_error::VortexExpect;
54
use vortex_error::VortexResult;
65

76
use crate::ArrayRef;
87
use crate::ExecutionCtx;
98
use crate::array::ArrayView;
109
use crate::arrays::Narrow;
11-
use crate::arrays::ScalarFn;
1210
use crate::arrays::narrow::NarrowArraySlotsExt;
1311
use crate::arrays::narrow::compute::cast_keeping_nullability;
1412
use crate::arrays::narrow::compute::ptype_holding_both_values;
1513
use crate::arrays::narrow::compute::ptype_holding_values_and_scalar;
1614
use crate::arrays::narrow::compute::wrap_values;
17-
use crate::arrays::scalar_fn::ExactScalarFn;
18-
use crate::arrays::scalar_fn::ScalarFnArrayExt;
19-
use crate::arrays::scalar_fn::ScalarFnArrayView;
2015
use crate::builtins::ArrayBuiltins;
21-
use crate::kernel::ExecuteParentKernel;
22-
use crate::scalar_fn::fns::zip::Zip;
2316
use crate::scalar_fn::fns::zip::ZipKernel;
2417

18+
/// Zips the values of a [`Narrow`] in the `if_true` branch with the `if_false` branch, when that is
19+
/// another `Narrow` or a constant, at the type that holds both. Any other `if_false` declines.
20+
///
21+
/// [`ZipExecuteAdaptor`](crate::scalar_fn::fns::zip::ZipExecuteAdaptor) runs this kernel only for
22+
/// the `if_true` branch, so a `Narrow` in the `if_false` branch takes the default path.
2523
impl ZipKernel for Narrow {
2624
fn zip(
2725
array: ArrayView<'_, Self>,
2826
if_false: &ArrayRef,
2927
mask: &ArrayRef,
3028
_ctx: &mut ExecutionCtx,
3129
) -> VortexResult<Option<ArrayRef>> {
32-
zip_values(array, if_false, mask, Branch::IfTrue)
33-
}
34-
}
35-
36-
/// Zips a [`Narrow`] in the `if_false` branch.
37-
///
38-
/// [`ZipExecuteAdaptor`] runs [`ZipKernel`] only for the `if_true` branch.
39-
///
40-
/// [`ZipExecuteAdaptor`]: crate::scalar_fn::fns::zip::ZipExecuteAdaptor
41-
#[derive(Debug)]
42-
pub(crate) struct NarrowZipIfFalseKernel;
43-
44-
impl ExecuteParentKernel<Narrow> for NarrowZipIfFalseKernel {
45-
type Parent = ExactScalarFn<Zip>;
46-
47-
fn execute_parent(
48-
&self,
49-
array: ArrayView<'_, Narrow>,
50-
parent: ScalarFnArrayView<'_, Zip>,
51-
child_idx: usize,
52-
_ctx: &mut ExecutionCtx,
53-
) -> VortexResult<Option<ArrayRef>> {
54-
if child_idx != 1 {
55-
return Ok(None);
56-
}
57-
58-
let zip = parent
59-
.as_opt::<ScalarFn>()
60-
.vortex_expect("ExactScalarFn matcher confirmed ScalarFnArray");
61-
62-
zip_values(array, zip.get_child(0), zip.get_child(2), Branch::IfFalse)
63-
}
64-
}
65-
66-
/// The branch of a zip that holds the [`Narrow`].
67-
enum Branch {
68-
IfTrue,
69-
IfFalse,
70-
}
71-
72-
/// Zips the values of `array` with the values of `other`, a `Narrow` or a constant, at the type
73-
/// that holds both. Any other `other` declines.
74-
fn zip_values(
75-
array: ArrayView<'_, Narrow>,
76-
other: &ArrayRef,
77-
mask: &ArrayRef,
78-
branch: Branch,
79-
) -> VortexResult<Option<ArrayRef>> {
80-
let values = array.values();
81-
let (other_values, ptype) = if let Some(other) = other.as_opt::<Narrow>() {
82-
let Some(ptype) = ptype_holding_both_values(array, other) else {
30+
let (if_false_values, ptype) = if let Some(other) = if_false.as_opt::<Narrow>() {
31+
let Some(ptype) = ptype_holding_both_values(array, other) else {
32+
return Ok(None);
33+
};
34+
(other.values().clone(), ptype)
35+
} else if let Some(constant) = if_false.as_constant() {
36+
let Some(ptype) = ptype_holding_values_and_scalar(array, constant)? else {
37+
return Ok(None);
38+
};
39+
(if_false.clone(), ptype)
40+
} else {
8341
return Ok(None);
8442
};
85-
(other.values().clone(), ptype)
86-
} else if let Some(constant) = other.as_constant() {
87-
let Some(ptype) = ptype_holding_values_and_scalar(array, constant)? else {
88-
return Ok(None);
89-
};
90-
(other.clone(), ptype)
91-
} else {
92-
return Ok(None);
93-
};
9443

95-
let values = cast_keeping_nullability(values, ptype)?;
96-
let other_values = cast_keeping_nullability(&other_values, ptype)?;
44+
let if_true_values = cast_keeping_nullability(array.values(), ptype)?;
45+
let if_false_values = cast_keeping_nullability(&if_false_values, ptype)?;
9746

98-
let zipped = match branch {
99-
Branch::IfTrue => mask.zip(values, other_values)?,
100-
Branch::IfFalse => mask.zip(other_values, values)?,
101-
};
102-
let dtype = array.dtype().with_nullability(zipped.dtype().nullability());
47+
let zipped = mask.zip(if_true_values, if_false_values)?;
48+
let dtype = array.dtype().with_nullability(zipped.dtype().nullability());
10349

104-
wrap_values(zipped, dtype).map(Some)
50+
wrap_values(zipped, dtype).map(Some)
51+
}
10552
}

‎vortex-array/src/arrays/narrow/mod.rs‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ use crate::array::VTable;
4040
use crate::arrays::Dict;
4141
use crate::arrays::dict::TakeExecuteAdaptor;
4242
use crate::arrays::narrow::compute::numeric::NarrowNumericKernel;
43-
use crate::arrays::narrow::compute::zip::NarrowZipIfFalseKernel;
4443
use crate::optimizer::kernels::ArrayKernelsExt;
4544
use crate::scalar_fn::ScalarFnVTable;
4645
use crate::scalar_fn::fns::binary::Binary;
@@ -54,7 +53,6 @@ pub(crate) fn initialize(session: &VortexSession) {
5453
kernels.register_execute_parent_kernel(Binary.id(), Narrow, NarrowNumericKernel);
5554
kernels.register_execute_parent_kernel(Dict.id(), Narrow, TakeExecuteAdaptor(Narrow));
5655
kernels.register_execute_parent_kernel(Zip.id(), Narrow, ZipExecuteAdaptor(Narrow));
57-
kernels.register_execute_parent_kernel(Zip.id(), Narrow, NarrowZipIfFalseKernel);
5856
}
5957

6058
#[cfg(test)]

‎vortex-array/src/arrays/narrow/tests/compute.rs‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -245,18 +245,13 @@ fn test_fill_null(
245245
Ok(())
246246
}
247247

248-
/// The zip runs on the values at the type that holds both branches. A constant in the `if_true`
249-
/// branch is the only input that reaches the `if_false` kernel, because an execution step stops at
250-
/// the first child kernel that answers. The null mask row takes the `if_false` branch.
248+
/// The zip runs on the values at the type that holds both branches. The null mask row takes the
249+
/// `if_false` branch.
251250
#[rstest]
252251
#[case::constant_if_false(
253252
signed().into_array(),
254253
ConstantArray::new(1_000i64, 5).into_array(),
255254
)]
256-
#[case::constant_if_true(
257-
ConstantArray::new(1_000i64, 5).into_array(),
258-
signed().into_array(),
259-
)]
260255
#[case::narrow_both(
261256
signed().into_array(),
262257
narrow(buffer![1_000i16, 2, 300, -4, 5], PType::I64).into_array(),

0 commit comments

Comments
 (0)