polars_core/chunked_array/logical/categorical/ops/
append.rs

1use polars_error::constants::LENGTH_LIMIT_MSG;
2
3use super::*;
4use crate::chunked_array::ops::append::new_chunks;
5
6struct CategoricalAppend;
7
8impl CategoricalMergeOperation for CategoricalAppend {
9    fn finish(self, lhs: &UInt32Chunked, rhs: &UInt32Chunked) -> PolarsResult<UInt32Chunked> {
10        let mut lhs_mut = lhs.clone();
11        lhs_mut.append(rhs)?;
12        Ok(lhs_mut)
13    }
14}
15
16impl CategoricalChunked {
17    fn set_lengths(&mut self, other: &Self) {
18        let length_self = &mut self.physical_mut().length;
19        *length_self = length_self
20            .checked_add(other.len())
21            .expect(LENGTH_LIMIT_MSG);
22
23        assert!(
24            IdxSize::try_from(*length_self).is_ok(),
25            "{}",
26            LENGTH_LIMIT_MSG
27        );
28        self.physical_mut().null_count += other.null_count();
29    }
30
31    pub fn append(&mut self, other: &Self) -> PolarsResult<()> {
32        // fast path all nulls
33        if self.physical.null_count() == self.len() && other.physical.null_count() == other.len() {
34            let len = self.len();
35            self.set_lengths(other);
36            new_chunks(&mut self.physical.chunks, &other.physical().chunks, len);
37            return Ok(());
38        }
39
40        let mut new_self = call_categorical_merge_operation(self, other, CategoricalAppend)?;
41        std::mem::swap(self, &mut new_self);
42        Ok(())
43    }
44
45    pub fn append_owned(&mut self, other: Self) -> PolarsResult<()> {
46        // @TODO: Move the implementation to append_owned and make append dispatch here.
47        self.append(&other)
48    }
49}