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::DynArray;
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::RunEndArray;
18use crate::RunEndVTable;
19use crate::compress::runend_decode_bools;
20
21impl CompareKernel for RunEndVTable {
22    fn compare(
23        lhs: &RunEndArray,
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            let decoded = runend_decode_bools(
35                lhs.ends().clone().execute::<PrimitiveArray>(ctx)?,
36                values.execute::<BoolArray>(ctx)?,
37                lhs.offset(),
38                lhs.len(),
39            )?;
40            return Ok(Some(decoded.into_array()));
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::RunEndArray;
59
60    fn ree_array() -> RunEndArray {
61        RunEndArray::encode(
62            PrimitiveArray::from_iter([1, 1, 1, 4, 4, 4, 2, 2, 5, 5, 5, 5]).into_array(),
63        )
64        .unwrap()
65    }
66
67    #[test]
68    fn compare_run_end() {
69        let arr = ree_array();
70        let res = arr
71            .into_array()
72            .binary(ConstantArray::new(5, 12).into_array(), Operator::Eq)
73            .unwrap();
74        let expected = BoolArray::from_iter([
75            false, false, false, false, false, false, false, false, true, true, true, true,
76        ]);
77        assert_arrays_eq!(res, expected);
78    }
79}