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
use super::RepeatBy;
use crate::prelude::*;
use arrow::array::ListArray;
use polars_arrow::array::ListFromIter;
use std::ops::Deref;

type LargeListArray = ListArray<i64>;

impl<T> RepeatBy for ChunkedArray<T>
where
    T: PolarsNumericType,
{
    fn repeat_by(&self, by: &UInt32Chunked) -> ListChunked {
        let iter = self
            .into_iter()
            .zip(by.into_iter())
            .map(|(opt_v, opt_by)| opt_by.map(|by| std::iter::repeat(opt_v).take(by as usize)));

        // Safety:
        // Length of iter is trusted
        ListChunked::new_from_chunks(
            self.name(),
            vec![Arc::new(unsafe {
                LargeListArray::from_iter_primitive_trusted_len::<T::Native, _, _>(
                    iter,
                    T::get_dtype().to_arrow(),
                )
            })],
        )
    }
}
impl RepeatBy for BooleanChunked {
    fn repeat_by(&self, by: &UInt32Chunked) -> ListChunked {
        let iter = self
            .into_iter()
            .zip(by.into_iter())
            .map(|(opt_v, opt_by)| opt_by.map(|by| std::iter::repeat(opt_v).take(by as usize)));

        // Safety:
        // Length of iter is trusted
        ListChunked::new_from_chunks(
            self.name(),
            vec![Arc::new(unsafe {
                LargeListArray::from_iter_bool_trusted_len(iter)
            })],
        )
    }
}
impl RepeatBy for Utf8Chunked {
    fn repeat_by(&self, by: &UInt32Chunked) -> ListChunked {
        let iter = self
            .into_iter()
            .zip(by.into_iter())
            .map(|(opt_v, opt_by)| opt_by.map(|by| std::iter::repeat(opt_v).take(by as usize)));

        // Safety:
        // Length of iter is trusted
        ListChunked::new_from_chunks(
            self.name(),
            vec![Arc::new(unsafe {
                LargeListArray::from_iter_utf8_trusted_len(iter, self.len())
            })],
        )
    }
}

#[cfg(feature = "dtype-categorical")]
impl RepeatBy for CategoricalChunked {
    fn repeat_by(&self, by: &UInt32Chunked) -> ListChunked {
        let mut ca = self.deref().repeat_by(by);
        ca.categorical_map = self.categorical_map.clone();
        ca
    }
}