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::builtin::compare::CompareKernel;
19use crate::scalar_fn::builtin::operators::CompareOperator;
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().compare(
31                ConstantArray::new(const_scalar.clone(), lhs.values().len()).into_array(),
32                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 std::sync::LazyLock;
52
53    use vortex_session::VortexSession;
54
55    use crate::ExecutionCtx;
56    use crate::IntoArray;
57    use crate::VortexSessionExecute;
58    use crate::arrays::BoolArray;
59    use crate::arrays::ConstantArray;
60    use crate::arrays::PrimitiveArray;
61    use crate::arrays::runend::RunEnd;
62    use crate::arrays::runend::RunEndArray;
63    use crate::assert_arrays_eq;
64    use crate::builtins::ArrayBuiltins;
65    use crate::scalar_fn::builtin::operators::CompareOperator;
66
67    static SESSION: LazyLock<VortexSession> = LazyLock::new(VortexSession::empty);
68
69    fn ree_array(ctx: &mut ExecutionCtx) -> RunEndArray {
70        RunEnd::encode(
71            PrimitiveArray::from_iter([1, 1, 1, 4, 4, 4, 2, 2, 5, 5, 5, 5]).into_array(),
72            ctx,
73        )
74        .unwrap()
75    }
76
77    #[test]
78    fn compare_run_end() {
79        let mut ctx = SESSION.create_execution_ctx();
80        let arr = ree_array(&mut ctx);
81        let res = arr
82            .into_array()
83            .compare(ConstantArray::new(5, 12).into_array(), CompareOperator::Eq)
84            .unwrap();
85        let expected = BoolArray::from_iter([
86            false, false, false, false, false, false, false, false, true, true, true, true,
87        ]);
88        assert_arrays_eq!(res, expected, &mut ctx);
89    }
90}