Skip to main content

vortex_array/scalar_fn/fns/like/
kernel.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_error::VortexExpect;
5use vortex_error::VortexResult;
6
7use crate::ArrayRef;
8use crate::ExecutionCtx;
9use crate::array::ArrayView;
10use crate::array::VTable;
11use crate::arrays::ScalarFnVTable;
12use crate::arrays::scalar_fn::ExactScalarFn;
13use crate::arrays::scalar_fn::ScalarFnArrayExt;
14use crate::arrays::scalar_fn::ScalarFnArrayView;
15use crate::kernel::ExecuteParentKernel;
16use crate::optimizer::rules::ArrayParentReduceRule;
17use crate::scalar_fn::fns::like::Like as LikeExpr;
18use crate::scalar_fn::fns::like::LikeOptions;
19
20/// Like pattern matching on an array without reading buffers.
21///
22/// This trait is for like implementations that can operate purely on array metadata
23/// and structure without needing to read or execute on the underlying buffers.
24/// Implementations should return `None` if the operation requires buffer access.
25///
26/// Dispatch is on child 0 (the input). The `pattern` and `options` are extracted from
27/// the parent `ScalarFnArray`.
28pub trait LikeReduce: VTable {
29    fn like(
30        array: ArrayView<'_, Self>,
31        pattern: &ArrayRef,
32        options: LikeOptions,
33    ) -> VortexResult<Option<ArrayRef>>;
34}
35
36/// Like pattern matching on an array, potentially reading buffers.
37///
38/// Unlike [`LikeReduce`], this trait is for like implementations that may need
39/// to read and execute on the underlying buffers to produce the result.
40///
41/// Dispatch is on child 0 (the input). The `pattern` and `options` are extracted from
42/// the parent `ScalarFnArray`.
43pub trait LikeKernel: VTable {
44    fn like(
45        array: ArrayView<'_, Self>,
46        pattern: &ArrayRef,
47        options: LikeOptions,
48        ctx: &mut ExecutionCtx,
49    ) -> VortexResult<Option<ArrayRef>>;
50}
51
52/// Adaptor that wraps a [`LikeReduce`] impl as an [`ArrayParentReduceRule`].
53#[derive(Default, Debug)]
54pub struct LikeReduceAdaptor<V>(pub V);
55
56impl<V> ArrayParentReduceRule<V> for LikeReduceAdaptor<V>
57where
58    V: LikeReduce,
59{
60    type Parent = ExactScalarFn<LikeExpr>;
61
62    fn reduce_parent(
63        &self,
64        array: ArrayView<'_, V>,
65        parent: ScalarFnArrayView<'_, LikeExpr>,
66        child_idx: usize,
67    ) -> VortexResult<Option<ArrayRef>> {
68        if child_idx != 0 {
69            return Ok(None);
70        }
71        let scalar_fn_array = parent
72            .as_opt::<ScalarFnVTable>()
73            .vortex_expect("ExactScalarFn matcher confirmed ScalarFnArray");
74        let pattern = scalar_fn_array.get_child(1);
75        let options = *parent.options;
76        <V as LikeReduce>::like(array, pattern, options)
77    }
78}
79
80/// Adaptor that wraps a [`LikeKernel`] impl as an [`ExecuteParentKernel`].
81#[derive(Default, Debug)]
82pub struct LikeExecuteAdaptor<V>(pub V);
83
84impl<V> ExecuteParentKernel<V> for LikeExecuteAdaptor<V>
85where
86    V: LikeKernel,
87{
88    type Parent = ExactScalarFn<LikeExpr>;
89
90    fn execute_parent(
91        &self,
92        array: ArrayView<'_, V>,
93        parent: ScalarFnArrayView<'_, LikeExpr>,
94        child_idx: usize,
95        ctx: &mut ExecutionCtx,
96    ) -> VortexResult<Option<ArrayRef>> {
97        if child_idx != 0 {
98            return Ok(None);
99        }
100        let scalar_fn_array = parent
101            .as_opt::<ScalarFnVTable>()
102            .vortex_expect("ExactScalarFn matcher confirmed ScalarFnArray");
103        let pattern = scalar_fn_array.get_child(1);
104        let options = *parent.options;
105        <V as LikeKernel>::like(array, pattern, options, ctx)
106    }
107}