Skip to main content

vortex_array/arrays/runend/compute/
compare.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_error::VortexResult;
5
6use crate::ArrayRef;
7use crate::ArrayView;
8use crate::ExecutionCtx;
9use crate::IntoArray;
10use crate::arrays::BoolArray;
11use crate::arrays::ConstantArray;
12use crate::arrays::PrimitiveArray;
13use crate::arrays::runend::RunEnd;
14use crate::arrays::runend::array::RunEndArrayExt;
15use crate::arrays::runend::array::RunEndArraySlotsExt;
16use crate::arrays::runend::decompress_bool::runend_decode_bools;
17use crate::builtins::ArrayBuiltins;
18use crate::scalar_fn::fns::binary::CompareKernel;
19use crate::scalar_fn::fns::operators::CompareOperator;
20use crate::scalar_fn::fns::operators::Operator;
21
22impl CompareKernel for RunEnd {
23    fn compare(
24        lhs: ArrayView<'_, Self>,
25        rhs: &ArrayRef,
26        operator: CompareOperator,
27        ctx: &mut ExecutionCtx,
28    ) -> VortexResult<Option<ArrayRef>> {
29        // If the RHS is constant, then we just need to compare against our encoded values.
30        if let Some(const_scalar) = rhs.as_constant() {
31            let values = lhs.values().binary(
32                ConstantArray::new(const_scalar.clone(), lhs.values().len()).into_array(),
33                Operator::from(operator),
34            )?;
35            return runend_decode_bools(
36                lhs.ends().clone().execute::<PrimitiveArray>(ctx)?,
37                values.execute::<BoolArray>(ctx)?,
38                lhs.offset(),
39                lhs.len(),
40                ctx,
41            )
42            .map(Some);
43        }
44
45        // Otherwise, fall back
46        Ok(None)
47    }
48}
49
50#[cfg(test)]
51mod test {
52    use std::sync::LazyLock;
53
54    use vortex_session::VortexSession;
55
56    use crate::ExecutionCtx;
57    use crate::IntoArray;
58    use crate::VortexSessionExecute;
59    use crate::arrays::BoolArray;
60    use crate::arrays::ConstantArray;
61    use crate::arrays::PrimitiveArray;
62    use crate::arrays::runend::RunEnd;
63    use crate::arrays::runend::RunEndArray;
64    use crate::assert_arrays_eq;
65    use crate::builtins::ArrayBuiltins;
66    use crate::scalar_fn::fns::operators::Operator;
67
68    static SESSION: LazyLock<VortexSession> = LazyLock::new(VortexSession::empty);
69
70    fn ree_array(ctx: &mut ExecutionCtx) -> RunEndArray {
71        RunEnd::encode(
72            PrimitiveArray::from_iter([1, 1, 1, 4, 4, 4, 2, 2, 5, 5, 5, 5]).into_array(),
73            ctx,
74        )
75        .unwrap()
76    }
77
78    #[test]
79    fn compare_run_end() {
80        let mut ctx = SESSION.create_execution_ctx();
81        let arr = ree_array(&mut ctx);
82        let res = arr
83            .into_array()
84            .binary(ConstantArray::new(5, 12).into_array(), Operator::Eq)
85            .unwrap();
86        let expected = BoolArray::from_iter([
87            false, false, false, false, false, false, false, false, true, true, true, true,
88        ]);
89        assert_arrays_eq!(res, expected, &mut ctx);
90    }
91}