vortex_functions/zip/
kernel.rs1use vortex_array::ArrayRef;
5use vortex_array::ArrayView;
6use vortex_array::ExecutionCtx;
7use vortex_array::VTable;
8use vortex_array::arrays::ScalarFn;
9use vortex_array::arrays::scalar_fn::ExactScalarFn;
10use vortex_array::arrays::scalar_fn::ScalarFnArrayExt;
11use vortex_array::arrays::scalar_fn::ScalarFnArrayView;
12use vortex_array::builtins::ArrayBuiltins;
13use vortex_array::kernel::ExecuteParentKernel;
14use vortex_array::optimizer::rules::ArrayParentReduceRule;
15use vortex_error::VortexExpect;
16use vortex_error::VortexResult;
17
18use crate::zip::Zip as ZipExpr;
19
20pub trait ZipReduce: VTable {
29 fn zip(
30 array: ArrayView<'_, Self>,
31 if_false: &ArrayRef,
32 mask: &ArrayRef,
33 ) -> VortexResult<Option<ArrayRef>>;
34}
35
36pub trait ZipKernel: VTable {
44 fn zip(
45 array: ArrayView<'_, Self>,
46 if_false: &ArrayRef,
47 mask: &ArrayRef,
48 ctx: &mut ExecutionCtx,
49 ) -> VortexResult<Option<ArrayRef>>;
50}
51
52#[derive(Default, Debug)]
54pub struct ZipReduceAdaptor<V>(pub V);
55
56impl<V> ArrayParentReduceRule<V> for ZipReduceAdaptor<V>
57where
58 V: ZipReduce,
59{
60 type Parent = ExactScalarFn<ZipExpr>;
61
62 fn reduce_parent(
63 &self,
64 array: ArrayView<'_, V>,
65 parent: ScalarFnArrayView<'_, ZipExpr>,
66 child_idx: usize,
67 ) -> VortexResult<Option<ArrayRef>> {
68 let Some((if_false, mask)) = normalized_zip_inputs(parent, child_idx)? else {
69 return Ok(None);
70 };
71 <V as ZipReduce>::zip(array, &if_false, &mask)
72 }
73}
74
75#[derive(Default, Debug)]
77pub struct ZipExecuteAdaptor<V>(pub V);
78
79impl<V> ExecuteParentKernel<V> for ZipExecuteAdaptor<V>
80where
81 V: ZipKernel,
82{
83 type Parent = ExactScalarFn<ZipExpr>;
84
85 fn execute_parent(
86 &self,
87 array: ArrayView<'_, V>,
88 parent: ScalarFnArrayView<'_, ZipExpr>,
89 child_idx: usize,
90 ctx: &mut ExecutionCtx,
91 ) -> VortexResult<Option<ArrayRef>> {
92 let Some((if_false, mask)) = normalized_zip_inputs(parent, child_idx)? else {
93 return Ok(None);
94 };
95 <V as ZipKernel>::zip(array, &if_false, &mask, ctx)
96 }
97}
98
99fn 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 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)]
120mod tests {
121 use rstest::rstest;
122 use vortex_array::IntoArray;
123 use vortex_array::VortexSessionExecute;
124 use vortex_array::arrays::Bool;
125 use vortex_array::arrays::BoolArray;
126 use vortex_array::assert_arrays_eq;
127 use vortex_session::VortexSession;
128
129 use super::*;
130 use crate::builtins::ArrayFunctions;
131
132 impl ZipReduce for Bool {
134 fn zip(
135 array: ArrayView<'_, Self>,
136 if_false: &ArrayRef,
137 mask: &ArrayRef,
138 ) -> VortexResult<Option<ArrayRef>> {
139 mask.zip(array.array().clone(), if_false.clone()).map(Some)
140 }
141 }
142
143 #[rstest]
144 #[case::if_true(0)]
145 #[case::if_false(1)]
146 #[case::mask(2)]
147 fn adaptors_preserve_branch_and_null_mask_semantics(
148 #[case] child_idx: usize,
149 ) -> VortexResult<()> {
150 let mut ctx = VortexSession::empty().create_execution_ctx();
151 let children = [
152 BoolArray::from_iter([Some(true), Some(false), None, Some(true)]).into_array(),
153 BoolArray::from_iter([Some(false), Some(true), Some(true), None]).into_array(),
154 BoolArray::from_iter([Some(true), Some(false), None, None]).into_array(),
155 ];
156 let parent = ZipExpr::try_new(
157 children[0].clone(),
158 children[1].clone(),
159 children[2].clone(),
160 )?
161 .into_array();
162 let child = children[child_idx].as_opt::<Bool>().unwrap();
163 let reduced = ZipReduceAdaptor(Bool).reduce_parent(
164 child,
165 parent.as_opt::<ExactScalarFn<ZipExpr>>().unwrap(),
166 child_idx,
167 )?;
168 let executed = ZipExecuteAdaptor(Bool).execute_parent(
169 child,
170 parent.as_opt::<ExactScalarFn<ZipExpr>>().unwrap(),
171 child_idx,
172 &mut ctx,
173 )?;
174
175 for result in [reduced, executed] {
176 if child_idx == 2 {
177 assert!(result.is_none(), "the mask is not a value branch");
178 } else {
179 let result = result.expect("both value branches dispatch to their kernel");
180 let expected = BoolArray::from_iter([Some(true), Some(true), Some(true), None]);
181 assert_arrays_eq!(result, expected, &mut ctx);
182 }
183 }
184 Ok(())
185 }
186}