vortex_array/scalar_fn/fns/between/
kernel.rs1use vortex_error::VortexExpect;
5use vortex_error::VortexResult;
6
7use super::Between;
8use super::BetweenOptions;
9use super::short_circuit;
10use crate::ArrayRef;
11use crate::ExecutionCtx;
12use crate::array::ArrayView;
13use crate::array::VTable;
14use crate::arrays::ScalarFn;
15use crate::arrays::scalar_fn::ExactScalarFn;
16use crate::arrays::scalar_fn::ScalarFnArrayExt;
17use crate::arrays::scalar_fn::ScalarFnArrayView;
18use crate::kernel::ExecuteParentKernel;
19use crate::optimizer::rules::ArrayParentReduceRule;
20
21pub trait BetweenReduce: VTable {
25 fn between(
26 array: ArrayView<'_, Self>,
27 lower: &ArrayRef,
28 upper: &ArrayRef,
29 options: &BetweenOptions,
30 ) -> VortexResult<Option<ArrayRef>>;
31}
32
33pub trait BetweenKernel: VTable {
37 fn between(
38 array: ArrayView<'_, Self>,
39 lower: &ArrayRef,
40 upper: &ArrayRef,
41 options: &BetweenOptions,
42 ctx: &mut ExecutionCtx,
43 ) -> VortexResult<Option<ArrayRef>>;
44}
45
46#[derive(Default, Debug)]
48pub struct BetweenReduceAdaptor<V>(pub V);
49
50impl<V> ArrayParentReduceRule<V> for BetweenReduceAdaptor<V>
51where
52 V: BetweenReduce,
53{
54 type Parent = ExactScalarFn<Between>;
55
56 fn reduce_parent(
57 &self,
58 array: ArrayView<'_, V>,
59 parent: ScalarFnArrayView<'_, Between>,
60 child_idx: usize,
61 ) -> VortexResult<Option<ArrayRef>> {
62 if child_idx != 0 {
64 return Ok(None);
65 }
66 let scalar_fn_array = parent
67 .as_opt::<ScalarFn>()
68 .vortex_expect("ExactScalarFn matcher confirmed ScalarFnArray");
69 let lower = scalar_fn_array.get_child(1);
72 let upper = scalar_fn_array.get_child(2);
73 let arr = array.array().clone();
74 if let Some(result) = short_circuit(&arr, lower, upper, parent.options)? {
75 return Ok(Some(result));
76 }
77 <V as BetweenReduce>::between(array, lower, upper, parent.options)
78 }
79}
80
81#[derive(Default, Debug)]
83pub struct BetweenExecuteAdaptor<V>(pub V);
84
85impl<V> ExecuteParentKernel<V> for BetweenExecuteAdaptor<V>
86where
87 V: BetweenKernel,
88{
89 type Parent = ExactScalarFn<Between>;
90
91 fn execute_parent(
92 &self,
93 array: ArrayView<'_, V>,
94 parent: ScalarFnArrayView<'_, Between>,
95 child_idx: usize,
96 ctx: &mut ExecutionCtx,
97 ) -> VortexResult<Option<ArrayRef>> {
98 if child_idx != 0 {
100 return Ok(None);
101 }
102 let scalar_fn_array = parent
103 .as_opt::<ScalarFn>()
104 .vortex_expect("ExactScalarFn matcher confirmed ScalarFnArray");
105 let lower = scalar_fn_array.get_child(1);
108 let upper = scalar_fn_array.get_child(2);
109 let arr = array.array().clone();
110 if let Some(result) = short_circuit(&arr, lower, upper, parent.options)? {
111 return result.execute::<ArrayRef>(ctx).map(Some);
115 }
116 <V as BetweenKernel>::between(array, lower, upper, parent.options, ctx)
117 }
118}