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
use crate::prelude::*;
use crate::series::IsSorted;

pub(crate) fn new_chunks(chunks: &mut Vec<ArrayRef>, other: &[ArrayRef], len: usize) {
    // replace an empty array
    if chunks.len() == 1 && len == 0 {
        *chunks = other.to_owned();
    } else {
        chunks.extend_from_slice(other);
    }
}

pub(super) fn update_sorted_flag_before_append<'a, T>(
    ca: &mut ChunkedArray<T>,
    other: &'a ChunkedArray<T>,
) where
    T: PolarsDataType,
    &'a ChunkedArray<T>: TakeRandom,
    <&'a ChunkedArray<T> as TakeRandom>::Item: PartialOrd,
{
    let get_start_end = || {
        let end = {
            unsafe {
                // reborrow and
                // inform bchk that we still have lifetime 'a
                // this is safe as we go from &mut borrow to &
                // because the trait is only implemented for &ChunkedArray<T>
                let borrow = std::mem::transmute::<&ChunkedArray<T>, &'a ChunkedArray<T>>(ca);
                // ensure we don't access with `len() - 1` this will have O(n^2) complexity
                // if we append many chunks that are sorted
                borrow.last()
            }
        };
        let start = unsafe { other.get_unchecked(0) };

        (start, end)
    };

    if !ca.is_empty() && !other.is_empty() {
        match (ca.is_sorted_flag(), other.is_sorted_flag()) {
            (IsSorted::Ascending, IsSorted::Ascending) => {
                let (start, end) = get_start_end();
                if end > start {
                    ca.set_sorted_flag(IsSorted::Not)
                }
            },
            (IsSorted::Descending, IsSorted::Descending) => {
                let (start, end) = get_start_end();
                if end < start {
                    ca.set_sorted_flag(IsSorted::Not)
                }
            },
            _ => ca.set_sorted_flag(IsSorted::Not),
        }
    } else if ca.is_empty() {
        ca.set_sorted_flag(other.is_sorted_flag())
    }
}

impl<T> ChunkedArray<T>
where
    T: PolarsNumericType,
{
    /// Append in place. This is done by adding the chunks of `other` to this [`ChunkedArray`].
    ///
    /// See also [`extend`](Self::extend) for appends to the underlying memory
    pub fn append(&mut self, other: &Self) {
        update_sorted_flag_before_append(self, other);

        let len = self.len();
        self.length += other.length;
        new_chunks(&mut self.chunks, &other.chunks, len);
    }
}

#[doc(hidden)]
impl BooleanChunked {
    pub fn append(&mut self, other: &Self) {
        update_sorted_flag_before_append(self, other);
        let len = self.len();
        self.length += other.length;
        new_chunks(&mut self.chunks, &other.chunks, len);
        self.set_sorted_flag(IsSorted::Not);
    }
}
#[doc(hidden)]
impl Utf8Chunked {
    pub fn append(&mut self, other: &Self) {
        update_sorted_flag_before_append(self, other);
        let len = self.len();
        self.length += other.length;
        new_chunks(&mut self.chunks, &other.chunks, len);
        self.set_sorted_flag(IsSorted::Not);
    }
}

#[doc(hidden)]
impl BinaryChunked {
    pub fn append(&mut self, other: &Self) {
        update_sorted_flag_before_append(self, other);
        let len = self.len();
        self.length += other.length;
        new_chunks(&mut self.chunks, &other.chunks, len);
        self.set_sorted_flag(IsSorted::Not);
    }
}

#[doc(hidden)]
impl ListChunked {
    pub fn append(&mut self, other: &Self) -> PolarsResult<()> {
        let dtype = merge_dtypes(self.dtype(), other.dtype())?;
        self.field = Arc::new(Field::new(self.name(), dtype));

        let len = self.len();
        self.length += other.length;
        new_chunks(&mut self.chunks, &other.chunks, len);
        self.set_sorted_flag(IsSorted::Not);
        if !other._can_fast_explode() {
            self.unset_fast_explode()
        }
        Ok(())
    }
}

#[cfg(feature = "dtype-array")]
#[doc(hidden)]
impl ArrayChunked {
    pub fn append(&mut self, other: &Self) -> PolarsResult<()> {
        let dtype = merge_dtypes(self.dtype(), other.dtype())?;
        self.field = Arc::new(Field::new(self.name(), dtype));

        let len = self.len();
        self.length += other.length;
        new_chunks(&mut self.chunks, &other.chunks, len);
        self.set_sorted_flag(IsSorted::Not);
        Ok(())
    }
}

#[cfg(feature = "object")]
#[doc(hidden)]
impl<T: PolarsObject> ObjectChunked<T> {
    pub fn append(&mut self, other: &Self) {
        let len = self.len();
        self.length += other.length;
        self.set_sorted_flag(IsSorted::Not);
        new_chunks(&mut self.chunks, &other.chunks, len);
    }
}