Skip to content

Commit e2b59e0

Browse files
committed
Dispatch zip kernels on either value branch
Normalize if_false dispatch by filling null mask entries with false before inversion. This preserves SQL ELSE semantics while allowing both execute and reduce kernels to handle either branch. Refs #10908. Signed-off-by: blaginin <[email protected]>
1 parent eed0212 commit e2b59e0

3 files changed

Lines changed: 108 additions & 24 deletions

File tree

‎vortex-array/src/arrays/widen/compute/zip.rs‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@ use crate::arrays::widen::compute::wrap_values;
1515
use crate::builtins::ArrayBuiltins;
1616
use crate::scalar_fn::fns::zip::ZipKernel;
1717

18-
/// Zips the values of a [`Widen`] in the `if_true` branch with the `if_false` branch, when that is
19-
/// another `Widen` 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 `Widen` in the `if_false` branch takes the default path.
18+
/// Zips the values of a [`Widen`] with another `Widen` or a constant at the type that holds both.
19+
/// The adaptor normalizes either branch to `array` and adjusts the mask accordingly.
2320
impl ZipKernel for Widen {
2421
fn zip(
2522
array: ArrayView<'_, Self>,

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,14 @@ fn test_fill_null(
252252
signed().into_array(),
253253
ConstantArray::new(1_000i64, 5).into_array(),
254254
)]
255+
#[case::constant_if_true(
256+
ConstantArray::new(1_000i64, 5).into_array(),
257+
signed().into_array(),
258+
)]
259+
#[case::nullable_if_false(
260+
ConstantArray::new(1_000i64, 5).into_array(),
261+
partially_valid().into_array(),
262+
)]
255263
#[case::widen_both(
256264
signed().into_array(),
257265
widen(buffer![1_000i16, 2, 300, -4, 5], PType::I64).into_array(),

‎vortex-array/src/scalar_fn/fns/zip/kernel.rs‎

Lines changed: 98 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::arrays::ScalarFn;
1212
use crate::arrays::scalar_fn::ExactScalarFn;
1313
use crate::arrays::scalar_fn::ScalarFnArrayExt;
1414
use crate::arrays::scalar_fn::ScalarFnArrayView;
15+
use crate::builtins::ArrayBuiltins;
1516
use crate::kernel::ExecuteParentKernel;
1617
use crate::optimizer::rules::ArrayParentReduceRule;
1718
use crate::scalar_fn::fns::zip::Zip as ZipExpr;
@@ -22,8 +23,8 @@ use crate::scalar_fn::fns::zip::Zip as ZipExpr;
2223
/// and structure without needing to read or execute on the underlying buffers.
2324
/// Implementations should return `None` if the operation requires buffer access.
2425
///
25-
/// Dispatch is on child 0 (if_true). The `if_false` and `mask` are extracted from
26-
/// the parent `ScalarFnArray`.
26+
/// Dispatch supports either value branch. The adaptor normalizes the inputs so
27+
/// `array` is selected where `mask` is true, and `if_false` everywhere else.
2728
pub trait ZipReduce: VTable {
2829
fn zip(
2930
array: ArrayView<'_, Self>,
@@ -37,8 +38,8 @@ pub trait ZipReduce: VTable {
3738
/// Unlike [`ZipReduce`], this trait is for zip implementations that may need
3839
/// to read and execute on the underlying buffers to produce the result.
3940
///
40-
/// Dispatch is on child 0 (if_true). The `if_false` and `mask` are extracted from
41-
/// the parent `ScalarFnArray`.
41+
/// Dispatch supports either value branch. The adaptor normalizes the inputs so
42+
/// `array` is selected where `mask` is true, and `if_false` everywhere else.
4243
pub trait ZipKernel: VTable {
4344
fn zip(
4445
array: ArrayView<'_, Self>,
@@ -64,15 +65,10 @@ where
6465
parent: ScalarFnArrayView<'_, ZipExpr>,
6566
child_idx: usize,
6667
) -> VortexResult<Option<ArrayRef>> {
67-
if child_idx != 0 {
68+
let Some((if_false, mask)) = normalized_zip_inputs(parent, child_idx)? else {
6869
return Ok(None);
69-
}
70-
let scalar_fn_array = parent
71-
.as_opt::<ScalarFn>()
72-
.vortex_expect("ExactScalarFn matcher confirmed ScalarFnArray");
73-
let if_false = scalar_fn_array.get_child(1);
74-
let mask_array = scalar_fn_array.get_child(2);
75-
<V as ZipReduce>::zip(array, if_false, mask_array)
70+
};
71+
<V as ZipReduce>::zip(array, &if_false, &mask)
7672
}
7773
}
7874

@@ -93,14 +89,97 @@ where
9389
child_idx: usize,
9490
ctx: &mut ExecutionCtx,
9591
) -> VortexResult<Option<ArrayRef>> {
96-
if child_idx != 0 {
92+
let Some((if_false, mask)) = normalized_zip_inputs(parent, child_idx)? else {
9793
return Ok(None);
94+
};
95+
<V as ZipKernel>::zip(array, &if_false, &mask, ctx)
96+
}
97+
}
98+
99+
fn normalized_zip_inputs(
100+
parent: ScalarFnArrayView<'_, ZipExpr>,
101+
child_idx: usize,
102+
) -> VortexResult<Option<(ArrayRef, ArrayRef)>> {
103+
let scalar_fn_array = parent
104+
.as_opt::<ScalarFn>()
105+
.vortex_expect("ExactScalarFn matcher confirmed ScalarFnArray");
106+
let mask = scalar_fn_array.get_child(2);
107+
match child_idx {
108+
0 => Ok(Some((scalar_fn_array.get_child(1).clone(), mask.clone()))),
109+
1 => {
110+
// Null mask rows select the original if_false branch, so they must
111+
// become true when that branch is passed to the kernel as if_true.
112+
let mask = mask.fill_null(false)?.not()?;
113+
Ok(Some((scalar_fn_array.get_child(0).clone(), mask)))
114+
}
115+
_ => Ok(None),
116+
}
117+
}
118+
119+
#[cfg(test)]
120+
mod tests {
121+
use rstest::rstest;
122+
use vortex_session::VortexSession;
123+
124+
use super::*;
125+
use crate::IntoArray;
126+
use crate::VortexSessionExecute;
127+
use crate::arrays::Bool;
128+
use crate::arrays::BoolArray;
129+
use crate::assert_arrays_eq;
130+
131+
// A metadata-only reduction exposes the inputs passed by the adaptor.
132+
impl ZipReduce for Bool {
133+
fn zip(
134+
array: ArrayView<'_, Self>,
135+
if_false: &ArrayRef,
136+
mask: &ArrayRef,
137+
) -> VortexResult<Option<ArrayRef>> {
138+
mask.zip(array.array().clone(), if_false.clone()).map(Some)
139+
}
140+
}
141+
142+
#[rstest]
143+
#[case::if_true(0)]
144+
#[case::if_false(1)]
145+
#[case::mask(2)]
146+
fn adaptors_preserve_branch_and_null_mask_semantics(
147+
#[case] child_idx: usize,
148+
) -> VortexResult<()> {
149+
let mut ctx = VortexSession::empty().create_execution_ctx();
150+
let children = [
151+
BoolArray::from_iter([Some(true), Some(false), None, Some(true)]).into_array(),
152+
BoolArray::from_iter([Some(false), Some(true), Some(true), None]).into_array(),
153+
BoolArray::from_iter([Some(true), Some(false), None, None]).into_array(),
154+
];
155+
let parent = ZipExpr::try_new(
156+
children[0].clone(),
157+
children[1].clone(),
158+
children[2].clone(),
159+
)?
160+
.into_array();
161+
let child = children[child_idx].as_opt::<Bool>().unwrap();
162+
let reduced = ZipReduceAdaptor(Bool).reduce_parent(
163+
child,
164+
parent.as_opt::<ExactScalarFn<ZipExpr>>().unwrap(),
165+
child_idx,
166+
)?;
167+
let executed = ZipExecuteAdaptor(Bool).execute_parent(
168+
child,
169+
parent.as_opt::<ExactScalarFn<ZipExpr>>().unwrap(),
170+
child_idx,
171+
&mut ctx,
172+
)?;
173+
174+
for result in [reduced, executed] {
175+
if child_idx == 2 {
176+
assert!(result.is_none(), "the mask is not a value branch");
177+
} else {
178+
let result = result.expect("both value branches dispatch to their kernel");
179+
let expected = BoolArray::from_iter([Some(true), Some(true), Some(true), None]);
180+
assert_arrays_eq!(result, expected, &mut ctx);
181+
}
98182
}
99-
let scalar_fn_array = parent
100-
.as_opt::<ScalarFn>()
101-
.vortex_expect("ExactScalarFn matcher confirmed ScalarFnArray");
102-
let if_false = scalar_fn_array.get_child(1);
103-
let mask_array = scalar_fn_array.get_child(2);
104-
<V as ZipKernel>::zip(array, if_false, mask_array, ctx)
183+
Ok(())
105184
}
106185
}

0 commit comments

Comments
 (0)