vortex_array/array/chunked/compute/
compare.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use vortex_dtype::DType;
use vortex_error::VortexResult;

use crate::array::{ChunkedArray, ChunkedEncoding};
use crate::compute::{compare, slice, CompareFn, Operator};
use crate::{ArrayDType, ArrayData, IntoArrayData};

impl CompareFn<ChunkedArray> for ChunkedEncoding {
    fn compare(
        &self,
        lhs: &ChunkedArray,
        rhs: &ArrayData,
        operator: Operator,
    ) -> VortexResult<Option<ArrayData>> {
        let mut idx = 0;
        let mut compare_chunks = Vec::with_capacity(lhs.nchunks());

        for chunk in lhs.chunks().filter(|c| !c.is_empty()) {
            let sliced = slice(rhs, idx, idx + chunk.len())?;
            let cmp_result = compare(&chunk, &sliced, operator)?;

            compare_chunks.push(cmp_result);
            idx += chunk.len();
        }

        Ok(Some(
            ChunkedArray::try_new(
                compare_chunks,
                DType::Bool((lhs.dtype().is_nullable() || rhs.dtype().is_nullable()).into()),
            )?
            .into_array(),
        ))
    }
}

#[cfg(test)]
mod tests {

    use super::*;
    use crate::array::PrimitiveArray;

    #[test]
    fn empty_compare() {
        let base = PrimitiveArray::from_iter(Vec::<u32>::new()).into_array();
        let chunked =
            ChunkedArray::try_new(vec![base.clone(), base.clone()], base.dtype().clone()).unwrap();
        let chunked_empty = ChunkedArray::try_new(vec![], base.dtype().clone()).unwrap();

        let r = compare(&chunked, &chunked_empty, Operator::Eq).unwrap();

        assert!(r.is_empty());
    }
}