Skip to main content

vortex_functions/list_contains/
runend.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::IntoArray;
7use vortex_array::arrays::RunEnd;
8use vortex_array::arrays::ScalarFnArray;
9use vortex_array::arrays::runend::RunEndArrayExt;
10use vortex_array::arrays::runend::RunEndArraySlotsExt;
11use vortex_array::scalar_fn::ScalarFnVTableExt;
12use vortex_error::VortexResult;
13
14use crate::list_contains::ListContains;
15use crate::list_contains::ListContainsElementReduce;
16use crate::list_contains::ListContainsOptions;
17use crate::list_contains::PreparedSet;
18
19/// Pushes `list_contains` of a prepared set into the values of run-end encoded needles.
20impl ListContainsElementReduce for RunEnd {
21    fn list_contains(
22        list: &ArrayRef,
23        needles: ArrayView<'_, RunEnd>,
24        options: &ListContainsOptions,
25    ) -> VortexResult<Option<ArrayRef>> {
26        let Some(set) = PreparedSet::set_of(list) else {
27            return Ok(None);
28        };
29
30        // The values get the shared set at their own length, which can exceed the rows of a
31        // zero-length slice that kept its runs.
32        let values = needles.values();
33        let set = ScalarFnArray::try_new_with_len(PreparedSet.bind(set.clone()), [], values.len())?
34            .into_array();
35        let values = ListContains::try_new_opts(set, values.clone(), *options)?.into_array();
36
37        // SAFETY: the ends are unchanged, and the values have one result for each run.
38        Ok(Some(
39            unsafe {
40                RunEnd::new_unchecked(
41                    needles.ends().clone(),
42                    values,
43                    needles.offset(),
44                    needles.len(),
45                )
46            }
47            .into_array(),
48        ))
49    }
50}
51
52#[cfg(test)]
53mod tests {
54    use std::sync::LazyLock;
55
56    use vortex_array::IntoArray;
57    use vortex_array::VortexSessionExecute;
58    use vortex_array::arrays::BoolArray;
59    use vortex_array::arrays::ConstantArray;
60    use vortex_array::arrays::RunEnd;
61    use vortex_array::assert_arrays_eq;
62    use vortex_array::dtype::DType;
63    use vortex_array::dtype::Nullability;
64    use vortex_array::dtype::PType;
65    use vortex_array::optimizer::ArrayOptimizer;
66    use vortex_array::scalar::Scalar;
67    use vortex_buffer::buffer;
68    use vortex_error::VortexResult;
69    use vortex_session::VortexSession;
70
71    use crate::list_contains::ListContains;
72
73    static SESSION: LazyLock<VortexSession> = LazyLock::new(|| {
74        let session = VortexSession::empty();
75
76        vortex_array::initialize(&session);
77
78        crate::initialize(&session);
79
80        session
81    });
82
83    #[test]
84    fn constant_list_is_looked_up_once_per_run() -> VortexResult<()> {
85        let mut ctx = SESSION.create_execution_ctx();
86        let needles = RunEnd::try_new(
87            buffer![2u32, 5, 6].into_array(),
88            buffer![1i32, 2, 3].into_array(),
89            &mut ctx,
90        )?
91        .into_array();
92        let element = DType::Primitive(PType::I32, Nullability::NonNullable);
93        let list = Scalar::list(
94            element,
95            vec![
96                Scalar::primitive(2i32, Nullability::NonNullable),
97                Scalar::primitive(3i32, Nullability::NonNullable),
98            ],
99            Nullability::NonNullable,
100        );
101        let list = ConstantArray::new(list, needles.len()).into_array();
102
103        let array = ListContains::try_new(list, needles)?
104            .into_array()
105            .optimize_ctx(&SESSION)?;
106        assert!(array.is::<RunEnd>());
107
108        assert_arrays_eq!(
109            array,
110            BoolArray::from_iter([false, false, true, true, true, true]),
111            &mut ctx
112        );
113        Ok(())
114    }
115}