Skip to main content

vortex_functions/list_contains/
chunked.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::Chunked;
8use vortex_array::arrays::ChunkedArray;
9use vortex_array::arrays::chunked::ChunkedArrayExt;
10use vortex_array::dtype::DType;
11use vortex_error::VortexResult;
12
13use crate::list_contains::ListContains;
14use crate::list_contains::ListContainsElementReduce;
15use crate::list_contains::ListContainsOptions;
16use crate::list_contains::PreparedSet;
17
18/// Pushes `list_contains` of a prepared set into each chunk of the needles.
19///
20/// Each chunk gets a slice of the prepared set, which shares the one set, so that the set is built
21/// once for every chunk.
22impl ListContainsElementReduce for Chunked {
23    fn list_contains(
24        list: &ArrayRef,
25        needles: ArrayView<'_, Chunked>,
26        options: &ListContainsOptions,
27    ) -> VortexResult<Option<ArrayRef>> {
28        if PreparedSet::set_of(list).is_none() {
29            return Ok(None);
30        }
31
32        let mut offset = 0;
33        let chunks = needles
34            .iter_chunks()
35            .map(|chunk| {
36                let set = list.slice(offset..offset + chunk.len())?;
37                offset += chunk.len();
38                Ok(ListContains::try_new_opts(set, chunk.clone(), *options)?.into_array())
39            })
40            .collect::<VortexResult<Vec<_>>>()?;
41
42        let dtype = DType::Bool(options.result_nullability(list.dtype(), needles.dtype()));
43
44        // SAFETY: each chunk is `list_contains` of the prepared set and a needle chunk of one dtype,
45        // so every chunk has the dtype of the whole result.
46        Ok(Some(
47            unsafe { ChunkedArray::new_unchecked(chunks, dtype) }.into_array(),
48        ))
49    }
50}