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::ArrayRef;
5use vortex_array::ArrayView;
6use vortex_array::ExecutionCtx;
7use vortex_array::IntoArray;
8use vortex_array::arrays::BoolArray;
9use vortex_array::arrays::ConstantArray;
10use vortex_array::arrays::PrimitiveArray;
11use vortex_array::builtins::ArrayBuiltins;
12use vortex_array::scalar_fn::fns::binary::CompareKernel;
13use vortex_array::scalar_fn::fns::operators::CompareOperator;
14use vortex_array::scalar_fn::fns::operators::Operator;
15use vortex_error::VortexResult;
16
17use crate::RunEnd;
18use crate::array::RunEndArrayExt;
19use crate::decompress_bool::runend_decode_bools;
20
21impl CompareKernel for RunEnd {
22    fn compare(
23        lhs: ArrayView<'_, Self>,
24        rhs: &ArrayRef,
25        operator: CompareOperator,
26        ctx: &mut ExecutionCtx,
27    ) -> VortexResult<Option<ArrayRef>> {
28        // If the RHS is constant, then we just need to compare against our encoded values.
29        if let Some(const_scalar) = rhs.as_constant() {
30            let values = lhs.values().binary(
31                ConstantArray::new(const_scalar, lhs.values().len()).into_array(),
32                Operator::from(operator),
33            )?;
34            return runend_decode_bools(
35                lhs.ends().clone().execute::<PrimitiveArray>(ctx)?,
36                values.execute::<BoolArray>(ctx)?,
37                lhs.offset(),
38                lhs.len(),
39            )
40            .map(Some);
41        }
42
43        // Otherwise, fall back
44        Ok(None)
45    }
46}
47
48#[cfg(test)]
49mod test {
50    use vortex_array::IntoArray;
51    use vortex_array::arrays::BoolArray;
52    use vortex_array::arrays::ConstantArray;
53    use vortex_array::arrays::PrimitiveArray;
54    use vortex_array::assert_arrays_eq;
55    use vortex_array::builtins::ArrayBuiltins;
56    use vortex_array::scalar_fn::fns::operators::Operator;
57
58    use crate::RunEnd;
59    use crate::RunEndArray;
60
61    fn ree_array() -> RunEndArray {
62        RunEnd::encode(PrimitiveArray::from_iter([1, 1, 1, 4, 4, 4, 2, 2, 5, 5, 5, 5]).into_array())
63            .unwrap()
64    }
65
66    #[test]
67    fn compare_run_end() {
68        let arr = ree_array();
69        let res = arr
70            .into_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}