Skip to main content

vortex_array/arrays/varbin/compute/
compare.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::cmp::Ordering;
5
6use vortex_buffer::BitBuffer;
7use vortex_error::VortexExpect as _;
8use vortex_error::VortexResult;
9use vortex_error::vortex_bail;
10
11use crate::ArrayRef;
12use crate::ExecutionCtx;
13use crate::IntoArray;
14use crate::array::ArrayView;
15use crate::arrays::BoolArray;
16use crate::arrays::PrimitiveArray;
17use crate::arrays::VarBin;
18use crate::arrays::varbin::VarBinArraySlotsExt;
19use crate::dtype::DType;
20use crate::dtype::IntegerPType;
21use crate::match_each_integer_ptype;
22use crate::scalar_fn::fns::binary::CompareKernel;
23use crate::scalar_fn::fns::operators::CompareOperator;
24
25// This implementation exists so we can compare against a constant in encoded space, without
26// canonicalizing the VarBin array to VarBinView.
27impl CompareKernel for VarBin {
28    fn compare(
29        lhs: ArrayView<'_, VarBin>,
30        rhs: &ArrayRef,
31        operator: CompareOperator,
32        ctx: &mut ExecutionCtx,
33    ) -> VortexResult<Option<ArrayRef>> {
34        let Some(rhs_const) = rhs.as_constant() else {
35            return Ok(None);
36        };
37
38        let len = lhs.len();
39
40        // The compare adaptor resolves null constants before dispatching to this kernel, so
41        // the scalar always carries a value.
42        let rhs_bytes: &[u8] = match rhs_const.dtype() {
43            DType::Binary(_) => rhs_const
44                .as_binary()
45                .value()
46                .vortex_expect("RHS should not be null")
47                .as_slice(),
48            DType::Utf8(_) => rhs_const
49                .as_utf8()
50                .value()
51                .vortex_expect("RHS should not be null")
52                .as_str()
53                .as_bytes(),
54            _ => vortex_bail!("VarBinArray can only have type of Binary or Utf8"),
55        };
56
57        let buffer = if rhs_bytes.is_empty() {
58            // Comparisons against "" only need the value lengths, i.e. the offset deltas.
59            match operator {
60                CompareOperator::Gte => BitBuffer::new_set(len), /* Every possible value is >= "" */
61                CompareOperator::Lt => BitBuffer::new_unset(len), // No value is < ""
62                CompareOperator::Eq | CompareOperator::Lte => {
63                    let lhs_offsets = lhs.offsets().clone().execute::<PrimitiveArray>(ctx)?;
64                    match_each_integer_ptype!(lhs_offsets.ptype(), |P| {
65                        compare_offsets_to_empty::<P>(lhs_offsets, true)
66                    })
67                }
68                CompareOperator::NotEq | CompareOperator::Gt => {
69                    let lhs_offsets = lhs.offsets().clone().execute::<PrimitiveArray>(ctx)?;
70                    match_each_integer_ptype!(lhs_offsets.ptype(), |P| {
71                        compare_offsets_to_empty::<P>(lhs_offsets, false)
72                    })
73                }
74            }
75        } else {
76            let lhs_offsets = lhs.offsets().clone().execute::<PrimitiveArray>(ctx)?;
77            match_each_integer_ptype!(lhs_offsets.ptype(), |P| {
78                compare_bytes_to_constant(
79                    lhs_offsets.as_slice::<P>(),
80                    lhs.bytes().as_slice(),
81                    rhs_bytes,
82                    operator,
83                )
84            })
85        };
86
87        Ok(Some(
88            BoolArray::new(
89                buffer,
90                lhs.validity()?.union_nullability(rhs.dtype().nullability()),
91            )
92            .into_array(),
93        ))
94    }
95}
96
97fn compare_offsets_to_empty<P: IntegerPType>(offsets: PrimitiveArray, eq: bool) -> BitBuffer {
98    let fn_ = if eq { P::eq } else { P::ne };
99    let offsets = offsets.as_slice::<P>();
100    BitBuffer::collect_bool(offsets.len() - 1, |idx| {
101        let left = unsafe { offsets.get_unchecked(idx) };
102        let right = unsafe { offsets.get_unchecked(idx + 1) };
103        fn_(left, right)
104    })
105}
106
107/// Compare every value in a VarBin array against a constant, resolving values through the
108/// offsets. Dispatches the operator outside the lane loop so each predicate inlines into its
109/// own loop.
110fn compare_bytes_to_constant<P: IntegerPType>(
111    offsets: &[P],
112    bytes: &[u8],
113    constant: &[u8],
114    operator: CompareOperator,
115) -> BitBuffer {
116    match operator {
117        CompareOperator::Eq => {
118            collect_lane_bits(offsets, |start, end| value_eq(bytes, start, end, constant))
119        }
120        CompareOperator::NotEq => {
121            collect_lane_bits(offsets, |start, end| !value_eq(bytes, start, end, constant))
122        }
123        CompareOperator::Gt => collect_lane_bits(offsets, |start, end| {
124            value_cmp(bytes, start, end, constant).is_gt()
125        }),
126        CompareOperator::Gte => collect_lane_bits(offsets, |start, end| {
127            value_cmp(bytes, start, end, constant).is_ge()
128        }),
129        CompareOperator::Lt => collect_lane_bits(offsets, |start, end| {
130            value_cmp(bytes, start, end, constant).is_lt()
131        }),
132        CompareOperator::Lte => collect_lane_bits(offsets, |start, end| {
133            value_cmp(bytes, start, end, constant).is_le()
134        }),
135    }
136}
137
138/// Bit-pack `predicate(offsets[i], offsets[i + 1])` over each lane of a VarBin array.
139fn collect_lane_bits<P: IntegerPType>(
140    offsets: &[P],
141    predicate: impl Fn(usize, usize) -> bool,
142) -> BitBuffer {
143    BitBuffer::collect_bool(offsets.len() - 1, |idx| {
144        // SAFETY: `collect_bool` yields idx < offsets.len() - 1.
145        let start = unsafe { offsets.get_unchecked(idx) }.as_();
146        let end = unsafe { offsets.get_unchecked(idx + 1) }.as_();
147        predicate(start, end)
148    })
149}
150
151/// Whether `bytes[start..end]` equals `constant`, comparing lengths first so lanes of a
152/// different length never touch the value bytes.
153///
154/// Offsets at null positions are not validated, so an out-of-bounds or inverted range is
155/// possible there; such lanes answer `false`, and validity masks them out of the result anyway.
156#[allow(clippy::inline_always)]
157#[inline(always)]
158fn value_eq(bytes: &[u8], start: usize, end: usize, constant: &[u8]) -> bool {
159    // A lane can only match when its length equals the constant's, so lanes of a different
160    // length answer without touching the value bytes. An inverted garbage range (start > end)
161    // wraps to a huge value that never equals `constant.len()`.
162    end.wrapping_sub(start) == constant.len()
163        && bytes.get(start..end).is_some_and(|value| value == constant)
164}
165
166/// Order `bytes[start..end]` against `constant`, treating the unvalidated garbage ranges that
167/// can appear at null positions as empty; validity masks those lanes out of the result anyway.
168#[allow(clippy::inline_always)]
169#[inline(always)]
170fn value_cmp(bytes: &[u8], start: usize, end: usize, constant: &[u8]) -> Ordering {
171    bytes.get(start..end).unwrap_or_default().cmp(constant)
172}
173
174#[cfg(test)]
175mod test {
176    use vortex_buffer::ByteBuffer;
177
178    use crate::IntoArray;
179    use crate::VortexSessionExecute;
180    use crate::array_session;
181    use crate::arrays::BoolArray;
182    use crate::arrays::ConstantArray;
183    use crate::arrays::VarBinArray;
184    use crate::arrays::VarBinViewArray;
185    use crate::assert_arrays_eq;
186    use crate::builtins::ArrayBuiltins;
187    use crate::dtype::DType;
188    use crate::dtype::Nullability;
189    use crate::scalar::Scalar;
190    use crate::scalar_fn::fns::operators::Operator;
191
192    #[test]
193    fn test_binary_compare() {
194        let mut ctx = array_session().create_execution_ctx();
195        let array = VarBinArray::from_iter(
196            [Some(b"abc".to_vec()), None, Some(b"def".to_vec())],
197            DType::Binary(Nullability::Nullable),
198        );
199        let result = array
200            .into_array()
201            .binary(
202                ConstantArray::new(
203                    Scalar::binary(ByteBuffer::copy_from(b"abc"), Nullability::Nullable),
204                    3,
205                )
206                .into_array(),
207                Operator::Eq,
208            )
209            .unwrap();
210
211        assert_arrays_eq!(
212            result,
213            BoolArray::from_iter([Some(true), None, Some(false)]),
214            &mut ctx
215        );
216    }
217
218    #[test]
219    fn varbinview_compare() {
220        let mut ctx = array_session().create_execution_ctx();
221        let array = VarBinArray::from_iter(
222            [Some(b"abc".to_vec()), None, Some(b"def".to_vec())],
223            DType::Binary(Nullability::Nullable),
224        );
225        let vbv = VarBinViewArray::from_iter(
226            [None, None, Some(b"def".to_vec())],
227            DType::Binary(Nullability::Nullable),
228        );
229        let result = array
230            .into_array()
231            .binary(vbv.into_array(), Operator::Eq)
232            .unwrap();
233
234        assert_arrays_eq!(
235            result,
236            BoolArray::from_iter([None, None, Some(true)]),
237            &mut ctx
238        );
239    }
240}
241
242#[cfg(test)]
243mod tests {
244    use vortex_buffer::ByteBuffer;
245
246    use crate::IntoArray;
247    use crate::VortexSessionExecute;
248    use crate::array_session;
249    use crate::arrays::BoolArray;
250    use crate::arrays::ConstantArray;
251    use crate::arrays::VarBinArray;
252    use crate::arrays::varbin::builder::VarBinBuilder;
253    use crate::assert_arrays_eq;
254    use crate::builtins::ArrayBuiltins;
255    use crate::dtype::DType;
256    use crate::dtype::Nullability;
257    use crate::scalar::Scalar;
258    use crate::scalar_fn::fns::operators::Operator;
259
260    #[test]
261    fn test_null_compare() {
262        let arr = VarBinArray::from_iter([Some("h")], DType::Utf8(Nullability::NonNullable));
263
264        let const_ = ConstantArray::new(Scalar::utf8("", Nullability::Nullable), 1);
265
266        let result = arr
267            .into_array()
268            .binary(const_.into_array(), Operator::Eq)
269            .unwrap();
270        assert_arrays_eq!(
271            result,
272            BoolArray::from_iter([Some(false)]),
273            &mut array_session().create_execution_ctx()
274        );
275    }
276
277    /// Regression: [`CompareKernel`] must handle every offset width; a `VarBinArray` built with
278    /// `i64` offsets once failed the constant comparison. Triggering this only requires `i64`
279    /// offsets, not large data.
280    ///
281    /// [`CompareKernel`]: super::CompareKernel
282    #[test]
283    fn varbin_i64_offsets_compare_constant() {
284        let mut ctx = array_session().create_execution_ctx();
285        let mut builder = VarBinBuilder::<i64>::with_capacity_in(
286            DType::Utf8(Nullability::NonNullable),
287            3,
288            vortex_buffer::BufferAllocatorRef::static_ref(),
289        );
290        builder.append_value(b"abc");
291        builder.append_value(b"xyz");
292        builder.append_value(b"abc");
293        let array = builder.finish_into_varbin();
294
295        let result = array
296            .into_array()
297            .binary(
298                ConstantArray::new(Scalar::utf8("abc", Nullability::NonNullable), 3).into_array(),
299                Operator::Eq,
300            )
301            .unwrap();
302
303        let expected = BoolArray::from_iter([true, false, true]);
304        assert_arrays_eq!(result, expected, &mut ctx);
305    }
306
307    #[test]
308    fn varbin_i64_offsets_compare_constant_binary() {
309        let mut ctx = array_session().create_execution_ctx();
310        let mut builder = VarBinBuilder::<i64>::with_capacity_in(
311            DType::Binary(Nullability::NonNullable),
312            3,
313            vortex_buffer::BufferAllocatorRef::static_ref(),
314        );
315        builder.append_value(b"abc");
316        builder.append_value(b"xyz");
317        builder.append_value(b"abc");
318        let array = builder.finish_into_varbin();
319
320        let result = array
321            .into_array()
322            .binary(
323                ConstantArray::new(
324                    Scalar::binary(ByteBuffer::copy_from(b"abc"), Nullability::NonNullable),
325                    3,
326                )
327                .into_array(),
328                Operator::Eq,
329            )
330            .unwrap();
331
332        let expected = BoolArray::from_iter([true, false, true]);
333        assert_arrays_eq!(result, expected, &mut ctx);
334    }
335}