• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vortex-data / vortex / 16593958537

29 Jul 2025 10:48AM UTC coverage: 82.285% (+0.5%) from 81.796%
16593958537

Pull #4036

github

web-flow
Merge 04147cb0f into 348079fc3
Pull Request #4036: varbinview builder buffer deduplication

146 of 154 new or added lines in 2 files covered. (94.81%)

348 existing lines in 26 files now uncovered.

44470 of 54044 relevant lines covered (82.28%)

169522.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.47
/vortex-array/src/compute/like.rs
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3

4
use std::any::Any;
5
use std::sync::LazyLock;
6

7
use arcref::ArcRef;
8
use vortex_dtype::DType;
9
use vortex_error::{VortexError, VortexExpect, VortexResult, vortex_bail, vortex_err};
10

11
use crate::arrow::{Datum, from_arrow_array_with_len};
12
use crate::compute::{ComputeFn, ComputeFnVTable, InvocationArgs, Kernel, Options, Output};
13
use crate::vtable::VTable;
14
use crate::{Array, ArrayRef};
15

16
static LIKE_FN: LazyLock<ComputeFn> = LazyLock::new(|| {
40✔
17
    let compute = ComputeFn::new("like".into(), ArcRef::new_ref(&Like));
40✔
18
    for kernel in inventory::iter::<LikeKernelRef> {
80✔
19
        compute.register_kernel(kernel.0.clone());
40✔
20
    }
40✔
21
    compute
40✔
22
});
40✔
23

24
/// Perform SQL left LIKE right
25
///
26
/// There are two wildcards supported with the LIKE operator:
27
/// - %: matches zero or more characters
28
/// - _: matches exactly one character
29
pub fn like(
98✔
30
    array: &dyn Array,
98✔
31
    pattern: &dyn Array,
98✔
32
    options: LikeOptions,
98✔
33
) -> VortexResult<ArrayRef> {
98✔
34
    LIKE_FN
98✔
35
        .invoke(&InvocationArgs {
98✔
36
            inputs: &[array.into(), pattern.into()],
98✔
37
            options: &options,
98✔
38
        })?
98✔
39
        .unwrap_array()
98✔
40
}
98✔
41

42
pub struct LikeKernelRef(ArcRef<dyn Kernel>);
43
inventory::collect!(LikeKernelRef);
44

45
pub trait LikeKernel: VTable {
46
    fn like(
47
        &self,
48
        array: &Self::Array,
49
        pattern: &dyn Array,
50
        options: LikeOptions,
51
    ) -> VortexResult<Option<ArrayRef>>;
52
}
53

54
#[derive(Debug)]
55
pub struct LikeKernelAdapter<V: VTable>(pub V);
56

57
impl<V: VTable + LikeKernel> LikeKernelAdapter<V> {
UNCOV
58
    pub const fn lift(&'static self) -> LikeKernelRef {
×
UNCOV
59
        LikeKernelRef(ArcRef::new_ref(self))
×
UNCOV
60
    }
×
61
}
62

63
impl<V: VTable + LikeKernel> Kernel for LikeKernelAdapter<V> {
64
    fn invoke(&self, args: &InvocationArgs) -> VortexResult<Option<Output>> {
83✔
65
        let inputs = LikeArgs::try_from(args)?;
83✔
66
        let Some(array) = inputs.array.as_opt::<V>() else {
83✔
67
            return Ok(None);
73✔
68
        };
69
        Ok(V::like(&self.0, array, inputs.pattern, inputs.options)?.map(|array| array.into()))
10✔
70
    }
83✔
71
}
72

73
struct Like;
74

75
impl ComputeFnVTable for Like {
76
    fn invoke(
98✔
77
        &self,
98✔
78
        args: &InvocationArgs,
98✔
79
        kernels: &[ArcRef<dyn Kernel>],
98✔
80
    ) -> VortexResult<Output> {
98✔
81
        let LikeArgs {
82
            array,
98✔
83
            pattern,
98✔
84
            options,
98✔
85
        } = LikeArgs::try_from(args)?;
98✔
86

87
        for kernel in kernels {
186✔
88
            if let Some(output) = kernel.invoke(args)? {
98✔
89
                return Ok(output);
10✔
90
            }
88✔
91
        }
92
        if let Some(output) = array.invoke(&LIKE_FN, args)? {
88✔
93
            return Ok(output);
×
94
        }
88✔
95

96
        // Otherwise, we fall back to the Arrow implementation
97
        Ok(arrow_like(array, pattern, options)?.into())
88✔
98
    }
98✔
99

100
    fn return_dtype(&self, args: &InvocationArgs) -> VortexResult<DType> {
98✔
101
        let LikeArgs { array, pattern, .. } = LikeArgs::try_from(args)?;
98✔
102
        if !matches!(array.dtype(), DType::Utf8(..)) {
98✔
103
            vortex_bail!("Expected utf8 array, got {}", array.dtype());
×
104
        }
98✔
105
        if !matches!(pattern.dtype(), DType::Utf8(..)) {
98✔
106
            vortex_bail!("Expected utf8 pattern, got {}", array.dtype());
×
107
        }
98✔
108
        let nullability = array.dtype().is_nullable() || pattern.dtype().is_nullable();
98✔
109
        Ok(DType::Bool(nullability.into()))
98✔
110
    }
98✔
111

112
    fn return_len(&self, args: &InvocationArgs) -> VortexResult<usize> {
98✔
113
        let LikeArgs { array, pattern, .. } = LikeArgs::try_from(args)?;
98✔
114
        if array.len() != pattern.len() {
98✔
115
            vortex_bail!(
×
116
                "Length mismatch lhs len {} ({}) != rhs len {} ({})",
×
117
                array.len(),
×
118
                array.encoding_id(),
×
119
                pattern.len(),
×
120
                pattern.encoding_id()
×
121
            );
122
        }
98✔
123
        Ok(array.len())
98✔
124
    }
98✔
125

126
    fn is_elementwise(&self) -> bool {
98✔
127
        true
98✔
128
    }
98✔
129
}
130

131
/// Options for SQL LIKE function
132
#[derive(Default, Debug, Clone, Copy)]
133
pub struct LikeOptions {
134
    pub negated: bool,
135
    pub case_insensitive: bool,
136
}
137

138
impl Options for LikeOptions {
139
    fn as_any(&self) -> &dyn Any {
392✔
140
        self
392✔
141
    }
392✔
142
}
143

144
struct LikeArgs<'a> {
145
    array: &'a dyn Array,
146
    pattern: &'a dyn Array,
147
    options: LikeOptions,
148
}
149

150
impl<'a> TryFrom<&InvocationArgs<'a>> for LikeArgs<'a> {
151
    type Error = VortexError;
152

153
    fn try_from(value: &InvocationArgs<'a>) -> Result<Self, Self::Error> {
392✔
154
        if value.inputs.len() != 2 {
392✔
155
            vortex_bail!("Expected 2 inputs, found {}", value.inputs.len());
×
156
        }
392✔
157
        let array = value.inputs[0]
392✔
158
            .array()
392✔
159
            .ok_or_else(|| vortex_err!("Expected first input to be an array"))?;
392✔
160
        let pattern = value.inputs[1]
392✔
161
            .array()
392✔
162
            .ok_or_else(|| vortex_err!("Expected second input to be an array"))?;
392✔
163
        let options = *value
392✔
164
            .options
392✔
165
            .as_any()
392✔
166
            .downcast_ref::<LikeOptions>()
392✔
167
            .vortex_expect("Expected options to be LikeOptions");
392✔
168

169
        Ok(LikeArgs {
392✔
170
            array,
392✔
171
            pattern,
392✔
172
            options,
392✔
173
        })
392✔
174
    }
392✔
175
}
176

177
/// Implementation of `LikeFn` using the Arrow crate.
178
pub(crate) fn arrow_like(
88✔
179
    array: &dyn Array,
88✔
180
    pattern: &dyn Array,
88✔
181
    options: LikeOptions,
88✔
182
) -> VortexResult<ArrayRef> {
88✔
183
    let nullable = array.dtype().is_nullable() | pattern.dtype().is_nullable();
88✔
184
    let len = array.len();
88✔
185
    assert_eq!(
88✔
186
        array.len(),
88✔
187
        pattern.len(),
88✔
188
        "Arrow Like: length mismatch for {}",
×
189
        array.encoding_id()
×
190
    );
191
    let lhs = Datum::try_new(array)?;
88✔
192
    let rhs = Datum::try_new(pattern)?;
88✔
193

194
    let result = match (options.negated, options.case_insensitive) {
88✔
195
        (false, false) => arrow_string::like::like(&lhs, &rhs)?,
48✔
196
        (true, false) => arrow_string::like::nlike(&lhs, &rhs)?,
40✔
197
        (false, true) => arrow_string::like::ilike(&lhs, &rhs)?,
×
198
        (true, true) => arrow_string::like::nilike(&lhs, &rhs)?,
×
199
    };
200

201
    from_arrow_array_with_len(&result, len, nullable)
88✔
202
}
88✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc