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