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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use arrow_array::types::GenericBinaryType;
use arrow_array::{BinaryArray, LargeBinaryArray};
use rayon::iter::ParallelIterator;

use crate::parallel_byte_array::{ParallelGenericByteArray, ParallelGenericByteArrayRef};

pub type ParallelGenericBinaryArray<OffsetSize> =
    ParallelGenericByteArray<GenericBinaryType<OffsetSize>>;
pub type ParallelGenericBinaryArrayRef<'data, OffsetSize> =
    ParallelGenericByteArrayRef<'data, GenericBinaryType<OffsetSize>>;

pub type ParallelBinaryArray = ParallelGenericBinaryArray<i32>;
pub type ParallelBinaryArrayRef<'data> = ParallelGenericBinaryArrayRef<'data, i32>;
pub type ParallelLargeBinaryArray = ParallelGenericBinaryArray<i64>;
pub type ParallelLargeBinaryArrayRef<'data> = ParallelGenericBinaryArrayRef<'data, i64>;

pub trait BinaryArrayRefParallelIterator<'data> {
    type Iter: ParallelIterator<Item = Option<&'data [u8]>>;

    fn par_iter(&'data self) -> Self::Iter;
}

impl<'data> BinaryArrayRefParallelIterator<'data> for BinaryArray {
    type Iter = ParallelBinaryArrayRef<'data>;

    fn par_iter(&'data self) -> Self::Iter {
        ParallelBinaryArrayRef::new(self)
    }
}

pub trait LargeBinaryArrayRefParallelIterator<'data> {
    type Iter: ParallelIterator<Item = Option<&'data [u8]>>;

    fn par_iter(&'data self) -> Self::Iter;
}

impl<'data> LargeBinaryArrayRefParallelIterator<'data> for LargeBinaryArray {
    type Iter = ParallelLargeBinaryArrayRef<'data>;

    fn par_iter(&'data self) -> Self::Iter {
        ParallelLargeBinaryArrayRef::new(self)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_par_iter() {
        let array =
            BinaryArray::from_opt_vec(vec![Some(b"one"), None, Some(b"two"), Some(b"three")]);
        let items: Vec<String> = array
            .par_iter()
            .map(|item| {
                item.map_or_else(String::new, |item| {
                    String::from_utf8_lossy(item).to_string()
                })
            })
            .collect();
        assert_eq!(
            items,
            vec![
                "one".to_owned(),
                "".to_owned(),
                "two".to_owned(),
                "three".to_owned()
            ]
        );
    }

    #[test]
    fn test_collect_array() {
        let array =
            BinaryArray::from_opt_vec(vec![Some(b"one"), None, Some(b"two"), Some(b"three")]);
        let collected_array: ParallelBinaryArray = array
            .par_iter()
            .map(|item| item.map_or_else(Vec::new, |item| item.to_vec()))
            .collect();
        let binary_array = collected_array.into_inner();
        assert_eq!(binary_array.value(0), b"one");
        assert_eq!(binary_array.value(1), b"");
        assert_eq!(binary_array.value(2), b"two");
        assert_eq!(binary_array.value(3), b"three");
    }
}