Skip to main content

vortex_runend/compute/
compare.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_array::Array;
5use vortex_array::ArrayRef;
6use vortex_array::ExecutionCtx;
7use vortex_array::IntoArray;
8use vortex_array::ToCanonical;
9use vortex_array::arrays::ConstantArray;
10use vortex_array::builtins::ArrayBuiltins;
11use vortex_array::scalar_fn::fns::binary::CompareKernel;
12use vortex_array::scalar_fn::fns::operators::CompareOperator;
13use vortex_array::scalar_fn::fns::operators::Operator;
14use vortex_error::VortexResult;
15
16use crate::RunEndArray;
17use crate::RunEndVTable;
18use crate::compress::runend_decode_bools;
19
20impl CompareKernel for RunEndVTable {
21    fn compare(
22        lhs: &RunEndArray,
23        rhs: &ArrayRef,
24        operator: CompareOperator,
25        _ctx: &mut ExecutionCtx,
26    ) -> VortexResult<Option<ArrayRef>> {
27        // If the RHS is constant, then we just need to compare against our encoded values.
28        if let Some(const_scalar) = rhs.as_constant() {
29            let values = lhs.values().binary(
30                ConstantArray::new(const_scalar, lhs.values().len()).into_array(),
31                Operator::from(operator),
32            )?;
33            let decoded = runend_decode_bools(
34                lhs.ends().to_primitive(),
35                values.to_bool(),
36                lhs.offset(),
37                lhs.len(),
38            )?;
39            return Ok(Some(decoded.into_array()));
40        }
41
42        // Otherwise, fall back
43        Ok(None)
44    }
45}
46
47#[cfg(test)]
48mod test {
49    use vortex_array::IntoArray;
50    use vortex_array::arrays::BoolArray;
51    use vortex_array::arrays::ConstantArray;
52    use vortex_array::arrays::PrimitiveArray;
53    use vortex_array::assert_arrays_eq;
54    use vortex_array::builtins::ArrayBuiltins;
55    use vortex_array::scalar_fn::fns::operators::Operator;
56
57    use crate::RunEndArray;
58
59    fn ree_array() -> RunEndArray {
60        RunEndArray::encode(
61            PrimitiveArray::from_iter([1, 1, 1, 4, 4, 4, 2, 2, 5, 5, 5, 5]).into_array(),
62        )
63        .unwrap()
64    }
65
66    #[test]
67    fn compare_run_end() {
68        let arr = ree_array();
69        let res = arr
70            .to_array()
71            .binary(ConstantArray::new(5, 12).into_array(), Operator::Eq)
72            .unwrap();
73        let expected = BoolArray::from_iter([
74            false, false, false, false, false, false, false, false, true, true, true, true,
75        ]);
76        assert_arrays_eq!(res, expected);
77    }
78}