Skip to main content

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