Skip to main content

vortex_array/scalar_fn/fns/not/
kernel.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_error::VortexResult;
5
6use crate::ArrayRef;
7use crate::ExecutionCtx;
8use crate::array::ArrayView;
9use crate::array::VTable;
10use crate::arrays::scalar_fn::ExactScalarFn;
11use crate::arrays::scalar_fn::ScalarFnArrayView;
12use crate::kernel::ExecuteParentKernel;
13use crate::optimizer::rules::ArrayParentReduceRule;
14use crate::scalar_fn::fns::not::Not as NotExpr;
15
16/// Invert a boolean array without reading buffers.
17///
18/// This trait is for invert implementations that can operate purely on array metadata
19/// and structure without needing to read or execute on the underlying buffers.
20/// Implementations should return `None` if the operation requires buffer access.
21pub trait NotReduce: VTable {
22    fn invert(array: ArrayView<'_, Self>) -> VortexResult<Option<ArrayRef>>;
23}
24
25/// Invert a boolean array, potentially reading buffers.
26///
27/// Unlike [`NotReduce`], this trait is for invert implementations that may need
28/// to read and execute on the underlying buffers to produce the result.
29pub trait NotKernel: VTable {
30    fn invert(array: ArrayView<'_, Self>, ctx: &mut ExecutionCtx)
31    -> VortexResult<Option<ArrayRef>>;
32}
33
34/// Adaptor that wraps a [`NotReduce`] impl as an [`ArrayParentReduceRule`].
35#[derive(Default, Debug)]
36pub struct NotReduceAdaptor<V>(pub V);
37
38impl<V> ArrayParentReduceRule<V> for NotReduceAdaptor<V>
39where
40    V: NotReduce,
41{
42    type Parent = ExactScalarFn<NotExpr>;
43
44    fn reduce_parent(
45        &self,
46        array: ArrayView<'_, V>,
47        _parent: ScalarFnArrayView<'_, NotExpr>,
48        _child_idx: usize,
49    ) -> VortexResult<Option<ArrayRef>> {
50        <V as NotReduce>::invert(array)
51    }
52}
53
54/// Adaptor that wraps a [`NotKernel`] impl as an [`ExecuteParentKernel`].
55#[derive(Default, Debug)]
56pub struct NotExecuteAdaptor<V>(pub V);
57
58impl<V> ExecuteParentKernel<V> for NotExecuteAdaptor<V>
59where
60    V: NotKernel,
61{
62    type Parent = ExactScalarFn<NotExpr>;
63
64    fn execute_parent(
65        &self,
66        array: ArrayView<'_, V>,
67        _parent: ScalarFnArrayView<'_, NotExpr>,
68        _child_idx: usize,
69        ctx: &mut ExecutionCtx,
70    ) -> VortexResult<Option<ArrayRef>> {
71        <V as NotKernel>::invert(array, ctx)
72    }
73}