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                ctx,
40            )
41            .map(Some);
42        }
43
44        // Otherwise, fall back
45        Ok(None)
46    }
47}
48
49#[cfg(test)]
50mod test {
51    use vortex_array::ExecutionCtx;
52    use vortex_array::IntoArray;
53    use vortex_array::LEGACY_SESSION;
54    use vortex_array::VortexSessionExecute;
55    use vortex_array::arrays::BoolArray;
56    use vortex_array::arrays::ConstantArray;
57    use vortex_array::arrays::PrimitiveArray;
58    use vortex_array::assert_arrays_eq;
59    use vortex_array::builtins::ArrayBuiltins;
60    use vortex_array::scalar_fn::fns::operators::Operator;
61
62    use crate::RunEnd;
63    use crate::RunEndArray;
64
65    fn ree_array(ctx: &mut ExecutionCtx) -> RunEndArray {
66        RunEnd::encode(
67            PrimitiveArray::from_iter([1, 1, 1, 4, 4, 4, 2, 2, 5, 5, 5, 5]).into_array(),
68            ctx,
69        )
70        .unwrap()
71    }
72
73    #[test]
74    fn compare_run_end() {
75        let mut ctx = LEGACY_SESSION.create_execution_ctx();
76        let arr = ree_array(&mut ctx);
77        let res = arr
78            .into_array()
79            .binary(ConstantArray::new(5, 12).into_array(), Operator::Eq)
80            .unwrap();
81        let expected = BoolArray::from_iter([
82            false, false, false, false, false, false, false, false, true, true, true, true,
83        ]);
84        assert_arrays_eq!(res, expected);
85    }
86}