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
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#[cfg(feature = "object")]
use crate::chunked_array::object::builder::ObjectChunkedBuilder;
use crate::prelude::*;
#[cfg(feature = "object")]
use arrow::array::Array;
use arrow::compute::concat;
use itertools::Itertools;
#[cfg(feature = "dtype-categorical")]
use std::ops::Deref;

pub trait ChunkOps {
    /// Aggregate to contiguous memory.
    fn rechunk(&self) -> Self
    where
        Self: std::marker::Sized;
}

impl<T> ChunkOps for ChunkedArray<T>
where
    T: PolarsNumericType,
{
    fn rechunk(&self) -> Self {
        if self.chunks().len() == 1 {
            self.clone()
        } else {
            let chunks = vec![concat::concatenate(
                self.chunks.iter().map(|a| &**a).collect_vec().as_slice(),
            )
            .unwrap()
            .into()];
            ChunkedArray::new_from_chunks(self.name(), chunks)
        }
    }
}

impl ChunkOps for BooleanChunked {
    fn rechunk(&self) -> Self {
        if self.chunks().len() == 1 {
            self.clone()
        } else {
            let chunks = vec![concat::concatenate(
                self.chunks.iter().map(|a| &**a).collect_vec().as_slice(),
            )
            .unwrap()
            .into()];
            ChunkedArray::new_from_chunks(self.name(), chunks)
        }
    }
}

impl ChunkOps for Utf8Chunked {
    fn rechunk(&self) -> Self {
        if self.chunks().len() == 1 {
            self.clone()
        } else {
            let chunks = vec![concat::concatenate(
                self.chunks.iter().map(|a| &**a).collect_vec().as_slice(),
            )
            .unwrap()
            .into()];
            ChunkedArray::new_from_chunks(self.name(), chunks)
        }
    }
}

#[cfg(feature = "dtype-categorical")]
impl ChunkOps for CategoricalChunked {
    fn rechunk(&self) -> Self
    where
        Self: std::marker::Sized,
    {
        let mut out: CategoricalChunked = self.deref().rechunk().into();
        let cat_map = self.categorical_map.clone();
        out.categorical_map = cat_map;
        out
    }
}

impl ChunkOps for ListChunked {
    fn rechunk(&self) -> Self {
        if self.chunks.len() == 1 {
            self.clone()
        } else {
            let chunks = vec![concat::concatenate(
                self.chunks.iter().map(|a| &**a).collect_vec().as_slice(),
            )
            .unwrap()
            .into()];
            let mut ca = ListChunked::new_from_chunks(self.name(), chunks);
            if self.can_fast_explode() {
                ca.set_fast_explode()
            }
            ca
        }
    }
}

#[cfg(feature = "object")]
impl<T> ChunkOps for ObjectChunked<T>
where
    T: PolarsObject,
{
    fn rechunk(&self) -> Self
    where
        Self: std::marker::Sized,
    {
        if self.chunks.len() == 1 {
            self.clone()
        } else {
            let mut builder = ObjectChunkedBuilder::new(self.name(), self.len());
            let chunks = self.downcast_iter();

            // todo! use iterators once implemented
            // no_null path
            if self.null_count() == 0 {
                for arr in chunks {
                    for idx in 0..arr.len() {
                        builder.append_value(arr.value(idx).clone())
                    }
                }
            } else {
                for arr in chunks {
                    for idx in 0..arr.len() {
                        if arr.is_valid(idx) {
                            builder.append_value(arr.value(idx).clone())
                        } else {
                            builder.append_null()
                        }
                    }
                }
            }
            builder.finish()
        }
    }
}

#[cfg(test)]
mod test {
    use crate::prelude::*;

    #[test]
    #[cfg(feature = "dtype-categorical")]
    fn test_categorical_map_after_rechunk() {
        let s = Series::new("", &["foo", "bar", "spam"]);
        let mut a = s.cast(&DataType::Categorical).unwrap();

        a.append(&a.slice(0, 2)).unwrap();
        a.rechunk();
        assert!(a.categorical().unwrap().categorical_map.is_some());
    }
}