Skip to main content

vortex_array/scalar_fn/fns/binary/
compare.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::cmp::Ordering;
5
6use arrow_array::BooleanArray;
7use arrow_buffer::NullBuffer;
8use arrow_ord::cmp;
9use arrow_ord::ord::make_comparator;
10use arrow_schema::SortOptions;
11use vortex_error::VortexResult;
12
13use crate::ArrayRef;
14use crate::Canonical;
15use crate::ExecutionCtx;
16use crate::IntoArray;
17use crate::arrays::ConstantArray;
18use crate::arrays::ConstantVTable;
19use crate::arrays::ExactScalarFn;
20use crate::arrays::ScalarFnArrayView;
21use crate::arrays::ScalarFnVTable;
22use crate::arrow::Datum;
23use crate::arrow::IntoArrowArray;
24use crate::arrow::from_arrow_array_with_len;
25use crate::dtype::DType;
26use crate::dtype::Nullability;
27use crate::kernel::ExecuteParentKernel;
28use crate::scalar::Scalar;
29use crate::scalar_fn::fns::binary::Binary;
30use crate::scalar_fn::fns::operators::CompareOperator;
31use crate::vtable::VTable;
32
33/// Trait for encoding-specific comparison kernels that operate in encoded space.
34///
35/// Implementations can compare an encoded array against another array (typically a constant)
36/// without first decompressing. The adaptor normalizes operand order so `array` is always
37/// the left-hand side, swapping the operator when necessary.
38pub trait CompareKernel: VTable {
39    fn compare(
40        lhs: &Self::Array,
41        rhs: &ArrayRef,
42        operator: CompareOperator,
43        ctx: &mut ExecutionCtx,
44    ) -> VortexResult<Option<ArrayRef>>;
45}
46
47/// Adaptor that bridges [`CompareKernel`] implementations to [`ExecuteParentKernel`].
48///
49/// When a `ScalarFnArray(Binary, cmp_op)` wraps a child that implements `CompareKernel`,
50/// this adaptor extracts the comparison operator and other operand, normalizes operand order
51/// (swapping the operator if the encoded array is on the RHS), and delegates to the kernel.
52#[derive(Default, Debug)]
53pub struct CompareExecuteAdaptor<V>(pub V);
54
55impl<V> ExecuteParentKernel<V> for CompareExecuteAdaptor<V>
56where
57    V: CompareKernel,
58{
59    type Parent = ExactScalarFn<Binary>;
60
61    fn execute_parent(
62        &self,
63        array: &V::Array,
64        parent: ScalarFnArrayView<'_, Binary>,
65        child_idx: usize,
66        ctx: &mut ExecutionCtx,
67    ) -> VortexResult<Option<ArrayRef>> {
68        // Only handle comparison operators
69        let Ok(cmp_op) = CompareOperator::try_from(*parent.options) else {
70            return Ok(None);
71        };
72
73        // Get the ScalarFnArray to access children
74        let Some(scalar_fn_array) = parent.as_opt::<ScalarFnVTable>() else {
75            return Ok(None);
76        };
77        let children = scalar_fn_array.children();
78
79        // Normalize so `array` is always LHS, swapping the operator if needed
80        // TODO(joe): should be go this here or in the Rule/Kernel
81        let (cmp_op, other) = match child_idx {
82            0 => (cmp_op, &children[1]),
83            1 => (cmp_op.swap(), &children[0]),
84            _ => return Ok(None),
85        };
86
87        let len = array.len();
88        let nullable = array.dtype().is_nullable() || other.dtype().is_nullable();
89
90        // Empty array → empty bool result
91        if len == 0 {
92            return Ok(Some(
93                Canonical::empty(&DType::Bool(nullable.into())).into_array(),
94            ));
95        }
96
97        // Null constant on either side → all-null bool result
98        if other.as_constant().is_some_and(|s| s.is_null()) {
99            return Ok(Some(
100                ConstantArray::new(Scalar::null(DType::Bool(nullable.into())), len).into_array(),
101            ));
102        }
103
104        V::compare(array, other, cmp_op, ctx)
105    }
106}
107
108/// Execute a compare operation between two arrays.
109///
110/// This is the entry point for compare operations from the binary expression.
111/// Handles empty, constant-null, and constant-constant directly, otherwise falls back to Arrow.
112pub(crate) fn execute_compare(
113    lhs: &ArrayRef,
114    rhs: &ArrayRef,
115    op: CompareOperator,
116) -> VortexResult<ArrayRef> {
117    let nullable = lhs.dtype().is_nullable() || rhs.dtype().is_nullable();
118
119    if lhs.is_empty() {
120        return Ok(Canonical::empty(&DType::Bool(nullable.into())).into_array());
121    }
122
123    let left_constant_null = lhs.as_constant().map(|l| l.is_null()).unwrap_or(false);
124    let right_constant_null = rhs.as_constant().map(|r| r.is_null()).unwrap_or(false);
125    if left_constant_null || right_constant_null {
126        return Ok(
127            ConstantArray::new(Scalar::null(DType::Bool(nullable.into())), lhs.len()).into_array(),
128        );
129    }
130
131    // Constant-constant fast path
132    if let (Some(lhs_const), Some(rhs_const)) = (
133        lhs.as_opt::<ConstantVTable>(),
134        rhs.as_opt::<ConstantVTable>(),
135    ) {
136        let result = scalar_cmp(lhs_const.scalar(), rhs_const.scalar(), op);
137        return Ok(ConstantArray::new(result, lhs.len()).into_array());
138    }
139
140    arrow_compare_arrays(lhs, rhs, op)
141}
142
143/// Fall back to Arrow for comparison.
144fn arrow_compare_arrays(
145    left: &ArrayRef,
146    right: &ArrayRef,
147    operator: CompareOperator,
148) -> VortexResult<ArrayRef> {
149    assert_eq!(left.len(), right.len());
150
151    let nullable = left.dtype().is_nullable() || right.dtype().is_nullable();
152
153    // Arrow's vectorized comparison kernels don't support nested types.
154    // For nested types, fall back to `make_comparator` which does element-wise comparison.
155    let array: BooleanArray = if left.dtype().is_nested() || right.dtype().is_nested() {
156        let rhs = right.to_array().into_arrow_preferred()?;
157        let lhs = left.to_array().into_arrow(rhs.data_type())?;
158
159        assert!(
160            lhs.data_type().equals_datatype(rhs.data_type()),
161            "lhs data_type: {}, rhs data_type: {}",
162            lhs.data_type(),
163            rhs.data_type()
164        );
165
166        compare_nested_arrow_arrays(lhs.as_ref(), rhs.as_ref(), operator)?
167    } else {
168        // Fast path: use vectorized kernels for primitive types.
169        let lhs = Datum::try_new(left)?;
170        let rhs = Datum::try_new_with_target_datatype(right, lhs.data_type())?;
171
172        match operator {
173            CompareOperator::Eq => cmp::eq(&lhs, &rhs)?,
174            CompareOperator::NotEq => cmp::neq(&lhs, &rhs)?,
175            CompareOperator::Gt => cmp::gt(&lhs, &rhs)?,
176            CompareOperator::Gte => cmp::gt_eq(&lhs, &rhs)?,
177            CompareOperator::Lt => cmp::lt(&lhs, &rhs)?,
178            CompareOperator::Lte => cmp::lt_eq(&lhs, &rhs)?,
179        }
180    };
181    from_arrow_array_with_len(&array, left.len(), nullable)
182}
183
184pub fn scalar_cmp(lhs: &Scalar, rhs: &Scalar, operator: CompareOperator) -> Scalar {
185    if lhs.is_null() | rhs.is_null() {
186        Scalar::null(DType::Bool(Nullability::Nullable))
187    } else {
188        let b = match operator {
189            CompareOperator::Eq => lhs == rhs,
190            CompareOperator::NotEq => lhs != rhs,
191            CompareOperator::Gt => lhs > rhs,
192            CompareOperator::Gte => lhs >= rhs,
193            CompareOperator::Lt => lhs < rhs,
194            CompareOperator::Lte => lhs <= rhs,
195        };
196
197        Scalar::bool(b, lhs.dtype().nullability() | rhs.dtype().nullability())
198    }
199}
200
201/// Compare two Arrow arrays element-wise using [`make_comparator`].
202///
203/// This function is required for nested types (Struct, List, FixedSizeList) because Arrow's
204/// vectorized comparison kernels ([`cmp::eq`], [`cmp::neq`], etc.) do not support them.
205///
206/// The vectorized kernels are faster but only work on primitive types, so for non-nested types,
207/// prefer using the vectorized kernels directly for better performance.
208pub fn compare_nested_arrow_arrays(
209    lhs: &dyn arrow_array::Array,
210    rhs: &dyn arrow_array::Array,
211    operator: CompareOperator,
212) -> VortexResult<BooleanArray> {
213    let compare_arrays_at = make_comparator(lhs, rhs, SortOptions::default())?;
214
215    let cmp_fn = match operator {
216        CompareOperator::Eq => Ordering::is_eq,
217        CompareOperator::NotEq => Ordering::is_ne,
218        CompareOperator::Gt => Ordering::is_gt,
219        CompareOperator::Gte => Ordering::is_ge,
220        CompareOperator::Lt => Ordering::is_lt,
221        CompareOperator::Lte => Ordering::is_le,
222    };
223
224    let values = (0..lhs.len())
225        .map(|i| cmp_fn(compare_arrays_at(i, i)))
226        .collect();
227    let nulls = NullBuffer::union(lhs.nulls(), rhs.nulls());
228
229    Ok(BooleanArray::new(values, nulls))
230}
231
232#[cfg(test)]
233mod tests {
234    use std::sync::Arc;
235
236    use rstest::rstest;
237    use vortex_buffer::buffer;
238
239    use crate::ArrayRef;
240    use crate::IntoArray;
241    use crate::ToCanonical;
242    use crate::arrays::BoolArray;
243    use crate::arrays::ConstantArray;
244    use crate::arrays::ListArray;
245    use crate::arrays::ListViewArray;
246    use crate::arrays::PrimitiveArray;
247    use crate::arrays::StructArray;
248    use crate::arrays::VarBinArray;
249    use crate::arrays::VarBinViewArray;
250    use crate::assert_arrays_eq;
251    use crate::builtins::ArrayBuiltins;
252    use crate::dtype::DType;
253    use crate::dtype::FieldName;
254    use crate::dtype::FieldNames;
255    use crate::dtype::Nullability;
256    use crate::dtype::PType;
257    use crate::scalar::Scalar;
258    use crate::scalar_fn::fns::operators::Operator;
259    use crate::test_harness::to_int_indices;
260    use crate::validity::Validity;
261
262    #[test]
263    fn test_bool_basic_comparisons() {
264        use vortex_buffer::BitBuffer;
265
266        let arr = BoolArray::new(
267            BitBuffer::from_iter([true, true, false, true, false]),
268            Validity::from_iter([false, true, true, true, true]),
269        );
270
271        let matches = arr
272            .to_array()
273            .binary(arr.to_array(), Operator::Eq)
274            .unwrap()
275            .to_bool();
276        assert_eq!(to_int_indices(matches).unwrap(), [1u64, 2, 3, 4]);
277
278        let matches = arr
279            .to_array()
280            .binary(arr.to_array(), Operator::NotEq)
281            .unwrap()
282            .to_bool();
283        let empty: [u64; 0] = [];
284        assert_eq!(to_int_indices(matches).unwrap(), empty);
285
286        let other = BoolArray::new(
287            BitBuffer::from_iter([false, false, false, true, true]),
288            Validity::from_iter([false, true, true, true, true]),
289        );
290
291        let matches = arr
292            .to_array()
293            .binary(other.to_array(), Operator::Lte)
294            .unwrap()
295            .to_bool();
296        assert_eq!(to_int_indices(matches).unwrap(), [2u64, 3, 4]);
297
298        let matches = arr
299            .to_array()
300            .binary(other.to_array(), Operator::Lt)
301            .unwrap()
302            .to_bool();
303        assert_eq!(to_int_indices(matches).unwrap(), [4u64]);
304
305        let matches = other
306            .to_array()
307            .binary(arr.to_array(), Operator::Gte)
308            .unwrap()
309            .to_bool();
310        assert_eq!(to_int_indices(matches).unwrap(), [2u64, 3, 4]);
311
312        let matches = other
313            .to_array()
314            .binary(arr.to_array(), Operator::Gt)
315            .unwrap()
316            .to_bool();
317        assert_eq!(to_int_indices(matches).unwrap(), [4u64]);
318    }
319
320    #[test]
321    fn constant_compare() {
322        let left = ConstantArray::new(Scalar::from(2u32), 10);
323        let right = ConstantArray::new(Scalar::from(10u32), 10);
324
325        let result = left
326            .to_array()
327            .binary(right.to_array(), Operator::Gt)
328            .unwrap();
329        assert_eq!(result.len(), 10);
330        let scalar = result.scalar_at(0).unwrap();
331        assert_eq!(scalar.as_bool().value(), Some(false));
332    }
333
334    #[rstest]
335    #[case(VarBinArray::from(vec!["a", "b"]).into_array(), VarBinViewArray::from_iter_str(["a", "b"]).into_array())]
336    #[case(VarBinViewArray::from_iter_str(["a", "b"]).into_array(), VarBinArray::from(vec!["a", "b"]).into_array())]
337    #[case(VarBinArray::from(vec!["a".as_bytes(), "b".as_bytes()]).into_array(), VarBinViewArray::from_iter_bin(["a".as_bytes(), "b".as_bytes()]).into_array())]
338    #[case(VarBinViewArray::from_iter_bin(["a".as_bytes(), "b".as_bytes()]).into_array(), VarBinArray::from(vec!["a".as_bytes(), "b".as_bytes()]).into_array())]
339    fn arrow_compare_different_encodings(#[case] left: ArrayRef, #[case] right: ArrayRef) {
340        let res = left.binary(right, Operator::Eq).unwrap();
341        let expected = BoolArray::from_iter([true, true]);
342        assert_arrays_eq!(res, expected);
343    }
344
345    #[ignore = "Arrow's ListView cannot be compared"]
346    #[test]
347    fn test_list_array_comparison() {
348        let values1 = PrimitiveArray::from_iter([1i32, 2, 3, 4, 5, 6]);
349        let offsets1 = PrimitiveArray::from_iter([0i32, 2, 4, 6]);
350        let list1 = ListArray::try_new(
351            values1.into_array(),
352            offsets1.into_array(),
353            Validity::NonNullable,
354        )
355        .unwrap();
356
357        let values2 = PrimitiveArray::from_iter([1i32, 2, 3, 4, 7, 8]);
358        let offsets2 = PrimitiveArray::from_iter([0i32, 2, 4, 6]);
359        let list2 = ListArray::try_new(
360            values2.into_array(),
361            offsets2.into_array(),
362            Validity::NonNullable,
363        )
364        .unwrap();
365
366        let result = list1
367            .to_array()
368            .binary(list2.to_array(), Operator::Eq)
369            .unwrap();
370        let expected = BoolArray::from_iter([true, true, false]);
371        assert_arrays_eq!(result, expected);
372
373        let result = list1
374            .to_array()
375            .binary(list2.to_array(), Operator::NotEq)
376            .unwrap();
377        let expected = BoolArray::from_iter([false, false, true]);
378        assert_arrays_eq!(result, expected);
379
380        let result = list1
381            .to_array()
382            .binary(list2.to_array(), Operator::Lt)
383            .unwrap();
384        let expected = BoolArray::from_iter([false, false, true]);
385        assert_arrays_eq!(result, expected);
386    }
387
388    #[ignore = "Arrow's ListView cannot be compared"]
389    #[test]
390    fn test_list_array_constant_comparison() {
391        let values = PrimitiveArray::from_iter([1i32, 2, 3, 4, 5, 6]);
392        let offsets = PrimitiveArray::from_iter([0i32, 2, 4, 6]);
393        let list = ListArray::try_new(
394            values.into_array(),
395            offsets.into_array(),
396            Validity::NonNullable,
397        )
398        .unwrap();
399
400        let list_scalar = Scalar::list(
401            Arc::new(DType::Primitive(PType::I32, Nullability::NonNullable)),
402            vec![3i32.into(), 4i32.into()],
403            Nullability::NonNullable,
404        );
405        let constant = ConstantArray::new(list_scalar, 3);
406
407        let result = list
408            .to_array()
409            .binary(constant.to_array(), Operator::Eq)
410            .unwrap();
411        let expected = BoolArray::from_iter([false, true, false]);
412        assert_arrays_eq!(result, expected);
413    }
414
415    #[test]
416    fn test_struct_array_comparison() {
417        let bool_field1 = BoolArray::from_iter([Some(true), Some(false), Some(true)]);
418        let int_field1 = PrimitiveArray::from_iter([1i32, 2, 3]);
419
420        let bool_field2 = BoolArray::from_iter([Some(true), Some(false), Some(false)]);
421        let int_field2 = PrimitiveArray::from_iter([1i32, 2, 4]);
422
423        let struct1 = StructArray::from_fields(&[
424            ("bool_col", bool_field1.into_array()),
425            ("int_col", int_field1.into_array()),
426        ])
427        .unwrap();
428
429        let struct2 = StructArray::from_fields(&[
430            ("bool_col", bool_field2.into_array()),
431            ("int_col", int_field2.into_array()),
432        ])
433        .unwrap();
434
435        let result = struct1
436            .to_array()
437            .binary(struct2.to_array(), Operator::Eq)
438            .unwrap();
439        let expected = BoolArray::from_iter([true, true, false]);
440        assert_arrays_eq!(result, expected);
441
442        let result = struct1
443            .to_array()
444            .binary(struct2.to_array(), Operator::Gt)
445            .unwrap();
446        let expected = BoolArray::from_iter([false, false, true]);
447        assert_arrays_eq!(result, expected);
448    }
449
450    #[test]
451    fn test_empty_struct_compare() {
452        let empty1 = StructArray::try_new(
453            FieldNames::from(Vec::<FieldName>::new()),
454            Vec::new(),
455            5,
456            Validity::NonNullable,
457        )
458        .unwrap();
459
460        let empty2 = StructArray::try_new(
461            FieldNames::from(Vec::<FieldName>::new()),
462            Vec::new(),
463            5,
464            Validity::NonNullable,
465        )
466        .unwrap();
467
468        let result = empty1
469            .to_array()
470            .binary(empty2.to_array(), Operator::Eq)
471            .unwrap();
472        let expected = BoolArray::from_iter([true, true, true, true, true]);
473        assert_arrays_eq!(result, expected);
474    }
475
476    #[test]
477    fn test_empty_list() {
478        let list = ListViewArray::new(
479            BoolArray::from_iter(Vec::<bool>::new()).into_array(),
480            buffer![0i32, 0i32, 0i32].into_array(),
481            buffer![0i32, 0i32, 0i32].into_array(),
482            Validity::AllValid,
483        );
484
485        let result = list
486            .to_array()
487            .binary(list.to_array(), Operator::Eq)
488            .unwrap();
489        assert!(result.scalar_at(0).unwrap().is_valid());
490        assert!(result.scalar_at(1).unwrap().is_valid());
491        assert!(result.scalar_at(2).unwrap().is_valid());
492    }
493}