Skip to main content

polars_core/frame/group_by/
position.rs

1use std::mem::ManuallyDrop;
2use std::ops::{Deref, DerefMut};
3
4use arrow::offset::OffsetsBuffer;
5use polars_utils::idx_vec::IdxVec;
6use rayon::iter::plumbing::UnindexedConsumer;
7use rayon::prelude::*;
8
9use crate::prelude::*;
10use crate::runtime::RAYON;
11use crate::utils::{NoNull, flatten, slice_slice};
12
13/// Indexes of the groups, the first index is stored separately.
14/// this make sorting fast.
15#[derive(Debug, Clone, PartialEq, Eq, Default)]
16pub struct GroupsIdx {
17    pub(crate) sorted_by_first_idx: bool,
18    /// Positions of the start of each group.
19    first: Vec<IdxSize>,
20    /// Global positions of all elements of all groups.
21    all: Vec<IdxVec>,
22}
23
24pub type IdxItem = (IdxSize, IdxVec);
25pub type BorrowIdxItem<'a> = (IdxSize, &'a IdxVec);
26
27impl Drop for GroupsIdx {
28    fn drop(&mut self) {
29        let v = std::mem::take(&mut self.all);
30        // ~65k took approximately 1ms on local machine, so from that point we drop on other thread
31        // to stop query from being blocked
32        #[cfg(not(target_family = "wasm"))]
33        if v.len() > 1 << 16 {
34            std::thread::spawn(move || drop(v));
35        } else {
36            drop(v);
37        }
38
39        #[cfg(target_family = "wasm")]
40        drop(v);
41    }
42}
43
44impl From<Vec<IdxItem>> for GroupsIdx {
45    fn from(v: Vec<IdxItem>) -> Self {
46        v.into_iter().collect()
47    }
48}
49
50impl From<Vec<Vec<IdxItem>>> for GroupsIdx {
51    fn from(v: Vec<Vec<IdxItem>>) -> Self {
52        // single threaded flatten: 10% faster than `iter().flatten().collect()
53        // this is the multi-threaded impl of that
54        let (cap, offsets) = flatten::cap_and_offsets(&v);
55        let mut first = Vec::with_capacity(cap);
56        let first_ptr = first.as_ptr() as usize;
57        let mut all = Vec::with_capacity(cap);
58        let all_ptr = all.as_ptr() as usize;
59
60        RAYON.install(|| {
61            v.into_par_iter()
62                .zip(offsets)
63                .for_each(|(mut inner, offset)| {
64                    unsafe {
65                        let first = (first_ptr as *const IdxSize as *mut IdxSize).add(offset);
66                        let all = (all_ptr as *const IdxVec as *mut IdxVec).add(offset);
67
68                        let inner_ptr = inner.as_mut_ptr();
69                        for i in 0..inner.len() {
70                            let (first_val, vals) = std::ptr::read(inner_ptr.add(i));
71                            std::ptr::write(first.add(i), first_val);
72                            std::ptr::write(all.add(i), vals);
73                        }
74                        // set len to 0 so that the contents will not get dropped
75                        // they are moved to `first` and `all`
76                        inner.set_len(0);
77                    }
78                });
79        });
80        unsafe {
81            all.set_len(cap);
82            first.set_len(cap);
83        }
84        GroupsIdx {
85            sorted_by_first_idx: false,
86            first,
87            all,
88        }
89    }
90}
91
92impl GroupsIdx {
93    pub fn new(first: Vec<IdxSize>, all: Vec<IdxVec>, sorted_by_first_idx: bool) -> Self {
94        Self {
95            sorted_by_first_idx,
96            first,
97            all,
98        }
99    }
100
101    pub fn sort_by_first_idx(&mut self) {
102        if self.sorted_by_first_idx {
103            return;
104        }
105        let mut idx = 0;
106        let first = std::mem::take(&mut self.first);
107        // store index and values so that we can sort those
108        let mut idx_vals = first
109            .into_iter()
110            .map(|v| {
111                let out = [idx, v];
112                idx += 1;
113                out
114            })
115            .collect_trusted::<Vec<_>>();
116        idx_vals.sort_unstable_by_key(|v| v[1]);
117
118        let take_first = || idx_vals.iter().map(|v| v[1]).collect_trusted::<Vec<_>>();
119        let take_all = || {
120            idx_vals
121                .iter()
122                .map(|v| unsafe {
123                    let idx = v[0] as usize;
124                    std::mem::take(self.all.get_unchecked_mut(idx))
125                })
126                .collect_trusted::<Vec<_>>()
127        };
128        let (first, all) = RAYON.install(|| rayon::join(take_first, take_all));
129        self.first = first;
130        self.all = all;
131        self.sorted_by_first_idx = true
132    }
133    pub fn is_sorted_by_first_idx(&self) -> bool {
134        self.sorted_by_first_idx
135    }
136
137    pub fn iter(
138        &self,
139    ) -> std::iter::Zip<
140        std::iter::Copied<std::slice::Iter<'_, IdxSize>>,
141        std::slice::Iter<'_, IdxVec>,
142    > {
143        self.into_iter()
144    }
145
146    pub fn all(&self) -> &[IdxVec] {
147        &self.all
148    }
149
150    pub fn first(&self) -> &[IdxSize] {
151        &self.first
152    }
153
154    pub fn first_mut(&mut self) -> &mut Vec<IdxSize> {
155        &mut self.first
156    }
157
158    pub(crate) fn len(&self) -> usize {
159        self.first.len()
160    }
161
162    pub(crate) unsafe fn get_unchecked(&self, index: usize) -> BorrowIdxItem<'_> {
163        let first = *self.first.get_unchecked(index);
164        let all = self.all.get_unchecked(index);
165        (first, all)
166    }
167
168    // Create an 'empty group', containing 1 group of length 0
169    pub fn new_empty() -> Self {
170        Self {
171            sorted_by_first_idx: false,
172            first: vec![0],
173            all: vec![vec![].into()],
174        }
175    }
176}
177
178impl FromIterator<IdxItem> for GroupsIdx {
179    fn from_iter<T: IntoIterator<Item = IdxItem>>(iter: T) -> Self {
180        let (first, all) = iter.into_iter().unzip();
181        GroupsIdx {
182            sorted_by_first_idx: false,
183            first,
184            all,
185        }
186    }
187}
188
189impl<'a> IntoIterator for &'a GroupsIdx {
190    type Item = BorrowIdxItem<'a>;
191    type IntoIter = std::iter::Zip<
192        std::iter::Copied<std::slice::Iter<'a, IdxSize>>,
193        std::slice::Iter<'a, IdxVec>,
194    >;
195
196    fn into_iter(self) -> Self::IntoIter {
197        self.first.iter().copied().zip(self.all.iter())
198    }
199}
200
201impl IntoIterator for GroupsIdx {
202    type Item = IdxItem;
203    type IntoIter = std::iter::Zip<std::vec::IntoIter<IdxSize>, std::vec::IntoIter<IdxVec>>;
204
205    fn into_iter(mut self) -> Self::IntoIter {
206        let first = std::mem::take(&mut self.first);
207        let all = std::mem::take(&mut self.all);
208        first.into_iter().zip(all)
209    }
210}
211
212impl FromParallelIterator<IdxItem> for GroupsIdx {
213    fn from_par_iter<I>(par_iter: I) -> Self
214    where
215        I: IntoParallelIterator<Item = IdxItem>,
216    {
217        let (first, all) = par_iter.into_par_iter().unzip();
218        GroupsIdx {
219            sorted_by_first_idx: false,
220            first,
221            all,
222        }
223    }
224}
225
226impl<'a> IntoParallelIterator for &'a GroupsIdx {
227    type Iter = rayon::iter::Zip<
228        rayon::iter::Copied<rayon::slice::Iter<'a, IdxSize>>,
229        rayon::slice::Iter<'a, IdxVec>,
230    >;
231    type Item = BorrowIdxItem<'a>;
232
233    fn into_par_iter(self) -> Self::Iter {
234        self.first.par_iter().copied().zip(self.all.par_iter())
235    }
236}
237
238impl IntoParallelIterator for GroupsIdx {
239    type Iter = rayon::iter::Zip<rayon::vec::IntoIter<IdxSize>, rayon::vec::IntoIter<IdxVec>>;
240    type Item = IdxItem;
241
242    fn into_par_iter(mut self) -> Self::Iter {
243        let first = std::mem::take(&mut self.first);
244        let all = std::mem::take(&mut self.all);
245        first.into_par_iter().zip(all.into_par_iter())
246    }
247}
248
249/// Every group is indicated by an array where the
250///  - first value is an index to the start of the group
251///  - second value is the length of the group
252///
253/// Only used when group values are stored together
254///
255/// This type should have the invariant that it is always sorted in ascending
256/// order by the start indices.
257pub type GroupsSlice = Vec<[IdxSize; 2]>;
258
259#[derive(Debug, Clone, PartialEq, Eq)]
260pub enum GroupsType {
261    Idx(GroupsIdx),
262    Slice {
263        // the groups slices
264        groups: GroupsSlice,
265        /// Indicates if the groups may overlap, i.e., at least one index MAY be
266        /// included in more than one group slice.
267        overlapping: bool,
268        /// Indicates if the groups are rolling, i.e. for every consecutive group
269        /// slice (offset, len), and given start = offset and end = offset + len,
270        /// then both new_start >= start AND new_end >= end MUST be true.
271        monotonic: bool,
272    },
273}
274
275impl Default for GroupsType {
276    fn default() -> Self {
277        GroupsType::Idx(GroupsIdx::default())
278    }
279}
280
281impl GroupsType {
282    pub fn new_slice(groups: GroupsSlice, overlapping: bool, monotonic: bool) -> Self {
283        #[cfg(debug_assertions)]
284        {
285            fn groups_overlap(groups: &GroupsSlice) -> bool {
286                if groups.len() < 2 {
287                    return false;
288                }
289                let mut groups = groups.clone();
290                groups.sort();
291                let mut prev_end = groups[0][1];
292
293                for g in &groups[1..] {
294                    let start = g[0];
295                    let end = g[1];
296                    if start < prev_end {
297                        return true;
298                    }
299                    if end > prev_end {
300                        prev_end = end;
301                    }
302                }
303                false
304            }
305
306            assert!(overlapping || !groups_overlap(&groups));
307
308            fn groups_are_monotonic(groups: &GroupsSlice) -> bool {
309                if groups.len() < 2 {
310                    return true;
311                }
312
313                let (offset, len) = (groups[0][0], groups[0][1]);
314                let mut prev_start = offset;
315                let mut prev_end = offset + len;
316
317                for g in &groups[1..] {
318                    let start = g[0];
319                    let end = g[0] + g[1];
320
321                    if start < prev_start || end < prev_end {
322                        return false;
323                    }
324
325                    prev_start = start;
326                    prev_end = end;
327                }
328                true
329            }
330
331            assert!(!monotonic || groups_are_monotonic(&groups));
332        }
333
334        Self::Slice {
335            groups,
336            overlapping,
337            monotonic,
338        }
339    }
340
341    pub fn into_idx(self) -> GroupsIdx {
342        match self {
343            GroupsType::Idx(groups) => groups,
344            GroupsType::Slice { groups, .. } => {
345                polars_warn!(
346                    "Had to reallocate groups, missed an optimization opportunity. Please open an issue."
347                );
348                groups
349                    .iter()
350                    .map(|&[first, len]| (first, (first..first + len).collect::<IdxVec>()))
351                    .collect()
352            },
353        }
354    }
355
356    pub(crate) fn prepare_list_agg(
357        &self,
358        total_len: usize,
359    ) -> (Option<IdxCa>, OffsetsBuffer<i64>, bool) {
360        let mut can_fast_explode = true;
361        match self {
362            GroupsType::Idx(groups) => {
363                let mut list_offset = Vec::with_capacity(self.len() + 1);
364                let mut gather_offsets = Vec::with_capacity(total_len);
365
366                let mut len_so_far = 0i64;
367                list_offset.push(len_so_far);
368
369                for idx in groups {
370                    let idx = idx.1;
371                    gather_offsets.extend_from_slice(idx);
372                    len_so_far += idx.len() as i64;
373                    list_offset.push(len_so_far);
374                    can_fast_explode &= !idx.is_empty();
375                }
376                unsafe {
377                    (
378                        Some(IdxCa::from_vec(PlSmallStr::EMPTY, gather_offsets)),
379                        OffsetsBuffer::new_unchecked(list_offset.into()),
380                        can_fast_explode,
381                    )
382                }
383            },
384            GroupsType::Slice { groups, .. } => {
385                let mut list_offset = Vec::with_capacity(self.len() + 1);
386                let mut gather_offsets = Vec::with_capacity(total_len);
387                let mut len_so_far = 0i64;
388                list_offset.push(len_so_far);
389
390                for g in groups {
391                    let len = g[1];
392                    let offset = g[0];
393                    gather_offsets.extend(offset..offset + len);
394
395                    len_so_far += len as i64;
396                    list_offset.push(len_so_far);
397                    can_fast_explode &= len > 0;
398                }
399
400                unsafe {
401                    (
402                        Some(IdxCa::from_vec(PlSmallStr::EMPTY, gather_offsets)),
403                        OffsetsBuffer::new_unchecked(list_offset.into()),
404                        can_fast_explode,
405                    )
406                }
407            },
408        }
409    }
410
411    pub fn iter(&self) -> GroupsTypeIter<'_> {
412        GroupsTypeIter::new(self)
413    }
414
415    pub fn sort_by_first_idx(&mut self) {
416        match self {
417            GroupsType::Idx(groups) => {
418                if !groups.is_sorted_by_first_idx() {
419                    groups.sort_by_first_idx()
420                }
421            },
422            GroupsType::Slice { .. } => {
423                // invariant of the type
424            },
425        }
426    }
427
428    pub(crate) fn is_sorted_by_first_idx(&self) -> bool {
429        match self {
430            GroupsType::Idx(groups) => groups.is_sorted_by_first_idx(),
431            GroupsType::Slice { .. } => true,
432        }
433    }
434
435    pub fn is_overlapping(&self) -> bool {
436        matches!(
437            self,
438            GroupsType::Slice {
439                overlapping: true,
440                ..
441            }
442        )
443    }
444
445    pub fn is_monotonic(&self) -> bool {
446        matches!(
447            self,
448            GroupsType::Slice {
449                monotonic: true,
450                ..
451            }
452        )
453    }
454
455    pub fn take_group_firsts(self) -> Vec<IdxSize> {
456        match self {
457            GroupsType::Idx(mut groups) => std::mem::take(&mut groups.first),
458            GroupsType::Slice { groups, .. } => {
459                groups.into_iter().map(|[first, _len]| first).collect()
460            },
461        }
462    }
463
464    /// Checks if groups are of equal length. The caller is responsible for
465    /// updating the groups by calling `groups()` prior to calling this method.
466    pub fn check_lengths(self: &GroupsType, other: &GroupsType) -> PolarsResult<()> {
467        if std::ptr::eq(self, other) {
468            return Ok(());
469        }
470        polars_ensure!(self.iter().zip(other.iter()).all(|(a, b)| {
471            a.len() == b.len()
472        }), ShapeMismatch: "expressions must have matching group lengths");
473        Ok(())
474    }
475
476    /// # Safety
477    /// This will not do any bounds checks. The caller must ensure
478    /// all groups have members.
479    pub unsafe fn take_group_lasts(self) -> Vec<IdxSize> {
480        match self {
481            GroupsType::Idx(groups) => groups
482                .all
483                .iter()
484                .map(|idx| *idx.get_unchecked(idx.len() - 1))
485                .collect(),
486            GroupsType::Slice { groups, .. } => groups
487                .into_iter()
488                .map(|[first, len]| first + len - 1)
489                .collect(),
490        }
491    }
492
493    pub fn par_iter(&self) -> GroupsTypeParIter<'_> {
494        GroupsTypeParIter::new(self)
495    }
496
497    /// Get a reference to the `GroupsIdx`.
498    ///
499    /// # Panic
500    ///
501    /// panics if the groups are a slice.
502    pub fn unwrap_idx(&self) -> &GroupsIdx {
503        match self {
504            GroupsType::Idx(groups) => groups,
505            GroupsType::Slice { .. } => panic!("groups are slices not index"),
506        }
507    }
508
509    /// Get a reference to the `GroupsSlice`.
510    ///
511    /// # Panic
512    ///
513    /// panics if the groups are an idx.
514    pub fn unwrap_slice(&self) -> &GroupsSlice {
515        match self {
516            GroupsType::Slice { groups, .. } => groups,
517            GroupsType::Idx(_) => panic!("groups are index not slices"),
518        }
519    }
520
521    pub fn get(&self, index: usize) -> GroupsIndicator<'_> {
522        match self {
523            GroupsType::Idx(groups) => {
524                let first = groups.first[index];
525                let all = &groups.all[index];
526                GroupsIndicator::Idx((first, all))
527            },
528            GroupsType::Slice { groups, .. } => GroupsIndicator::Slice(groups[index]),
529        }
530    }
531
532    /// Get a mutable reference to the `GroupsIdx`.
533    ///
534    /// # Panic
535    ///
536    /// panics if the groups are a slice.
537    pub fn idx_mut(&mut self) -> &mut GroupsIdx {
538        match self {
539            GroupsType::Idx(groups) => groups,
540            GroupsType::Slice { .. } => panic!("groups are slices not index"),
541        }
542    }
543
544    pub fn len(&self) -> usize {
545        match self {
546            GroupsType::Idx(groups) => groups.len(),
547            GroupsType::Slice { groups, .. } => groups.len(),
548        }
549    }
550
551    pub fn is_empty(&self) -> bool {
552        self.len() == 0
553    }
554
555    pub fn group_count(&self) -> IdxCa {
556        match self {
557            GroupsType::Idx(groups) => {
558                let ca: NoNull<IdxCa> = groups
559                    .iter()
560                    .map(|(_first, idx)| idx.len() as IdxSize)
561                    .collect_trusted();
562                ca.into_inner()
563            },
564            GroupsType::Slice { groups, .. } => {
565                let ca: NoNull<IdxCa> = groups.iter().map(|[_first, len]| *len).collect_trusted();
566                ca.into_inner()
567            },
568        }
569    }
570
571    pub fn as_list_chunked(&self) -> ListChunked {
572        match self {
573            GroupsType::Idx(groups) => groups
574                .iter()
575                .map(|(_first, idx)| {
576                    let ca: NoNull<IdxCa> = idx.iter().map(|&v| v as IdxSize).collect();
577                    ca.into_inner().into_series()
578                })
579                .collect_trusted(),
580            GroupsType::Slice { groups, .. } => groups
581                .iter()
582                .map(|&[first, len]| {
583                    let ca: NoNull<IdxCa> = (first..first + len).collect_trusted();
584                    ca.into_inner().into_series()
585                })
586                .collect_trusted(),
587        }
588    }
589
590    pub fn into_sliceable(self) -> GroupPositions {
591        let len = self.len();
592        slice_groups(Arc::new(self), 0, len)
593    }
594
595    pub fn num_elements(&self) -> usize {
596        match self {
597            GroupsType::Idx(i) => i.all().iter().map(|v| v.len()).sum(),
598            GroupsType::Slice {
599                groups,
600                overlapping: _,
601                monotonic: _,
602            } => groups.iter().map(|[_, l]| *l as usize).sum(),
603        }
604    }
605}
606
607impl From<GroupsIdx> for GroupsType {
608    fn from(groups: GroupsIdx) -> Self {
609        GroupsType::Idx(groups)
610    }
611}
612
613pub enum GroupsIndicator<'a> {
614    Idx(BorrowIdxItem<'a>),
615    Slice([IdxSize; 2]),
616}
617
618impl GroupsIndicator<'_> {
619    pub fn len(&self) -> usize {
620        match self {
621            GroupsIndicator::Idx(g) => g.1.len(),
622            GroupsIndicator::Slice([_, len]) => *len as usize,
623        }
624    }
625    pub fn first(&self) -> IdxSize {
626        match self {
627            GroupsIndicator::Idx(g) => g.0,
628            GroupsIndicator::Slice([first, _]) => *first,
629        }
630    }
631    pub fn is_empty(&self) -> bool {
632        self.len() == 0
633    }
634}
635
636pub struct GroupsTypeIter<'a> {
637    vals: &'a GroupsType,
638    len: usize,
639    idx: usize,
640}
641
642impl<'a> GroupsTypeIter<'a> {
643    fn new(vals: &'a GroupsType) -> Self {
644        let len = vals.len();
645        let idx = 0;
646        GroupsTypeIter { vals, len, idx }
647    }
648}
649
650impl<'a> Iterator for GroupsTypeIter<'a> {
651    type Item = GroupsIndicator<'a>;
652
653    fn nth(&mut self, n: usize) -> Option<Self::Item> {
654        self.idx = self.idx.saturating_add(n);
655        self.next()
656    }
657
658    fn next(&mut self) -> Option<Self::Item> {
659        if self.idx >= self.len {
660            return None;
661        }
662
663        let out = unsafe {
664            match self.vals {
665                GroupsType::Idx(groups) => {
666                    let item = groups.get_unchecked(self.idx);
667                    Some(GroupsIndicator::Idx(item))
668                },
669                GroupsType::Slice { groups, .. } => {
670                    Some(GroupsIndicator::Slice(*groups.get_unchecked(self.idx)))
671                },
672            }
673        };
674        self.idx += 1;
675        out
676    }
677}
678
679pub struct GroupsTypeParIter<'a> {
680    vals: &'a GroupsType,
681    len: usize,
682}
683
684impl<'a> GroupsTypeParIter<'a> {
685    fn new(vals: &'a GroupsType) -> Self {
686        let len = vals.len();
687        GroupsTypeParIter { vals, len }
688    }
689}
690
691impl<'a> ParallelIterator for GroupsTypeParIter<'a> {
692    type Item = GroupsIndicator<'a>;
693
694    fn drive_unindexed<C>(self, consumer: C) -> C::Result
695    where
696        C: UnindexedConsumer<Self::Item>,
697    {
698        (0..self.len)
699            .into_par_iter()
700            .map(|i| unsafe {
701                match self.vals {
702                    GroupsType::Idx(groups) => GroupsIndicator::Idx(groups.get_unchecked(i)),
703                    GroupsType::Slice { groups, .. } => {
704                        GroupsIndicator::Slice(*groups.get_unchecked(i))
705                    },
706                }
707            })
708            .drive_unindexed(consumer)
709    }
710}
711
712#[derive(Debug)]
713pub struct GroupPositions {
714    // SAFETY: sliced is a shallow clone of original
715    // It emulates a shared reference, not an exclusive reference
716    // Its data must not be mutated through direct access
717    sliced: ManuallyDrop<GroupsType>,
718    // Unsliced buffer
719    original: Arc<GroupsType>,
720    offset: i64,
721    len: usize,
722}
723
724impl Clone for GroupPositions {
725    fn clone(&self) -> Self {
726        let sliced = slice_groups_inner(&self.original, self.offset, self.len);
727
728        Self {
729            sliced,
730            original: self.original.clone(),
731            offset: self.offset,
732            len: self.len,
733        }
734    }
735}
736
737impl AsRef<GroupsType> for GroupPositions {
738    fn as_ref(&self) -> &GroupsType {
739        self.sliced.deref()
740    }
741}
742
743impl Deref for GroupPositions {
744    type Target = GroupsType;
745
746    fn deref(&self) -> &Self::Target {
747        self.sliced.deref()
748    }
749}
750
751impl Default for GroupPositions {
752    fn default() -> Self {
753        GroupsType::default().into_sliceable()
754    }
755}
756
757impl GroupPositions {
758    pub fn slice(&self, offset: i64, len: usize) -> Self {
759        let offset = self.offset + offset;
760        slice_groups(self.original.clone(), offset, len)
761    }
762
763    pub fn sort_by_first_idx(&mut self) {
764        if !self.as_ref().is_sorted_by_first_idx() {
765            let original = Arc::make_mut(&mut self.original);
766            original.sort_by_first_idx();
767
768            self.sliced = slice_groups_inner(original, self.offset, self.len);
769        }
770    }
771
772    pub fn unroll(mut self) -> GroupPositions {
773        match self.sliced.deref_mut() {
774            GroupsType::Idx(_) => self,
775            GroupsType::Slice {
776                overlapping: false, ..
777            } => self,
778            GroupsType::Slice { groups, .. } => {
779                // SAFETY: sliced is a shallow partial clone of original.
780                // A new owning Vec is required per GH issue #21859
781                let mut cum_offset = 0 as IdxSize;
782                let groups: Vec<_> = groups
783                    .iter()
784                    .map(|[_, len]| {
785                        let new = [cum_offset, *len];
786                        cum_offset += *len;
787                        new
788                    })
789                    .collect();
790
791                GroupsType::new_slice(groups, false, true).into_sliceable()
792            },
793        }
794    }
795
796    pub fn as_unrolled_slice(&self) -> Option<&GroupsSlice> {
797        match &*self.sliced {
798            GroupsType::Idx(_) => None,
799            GroupsType::Slice {
800                groups: _,
801                overlapping: true,
802                monotonic: _,
803            } => None,
804            GroupsType::Slice {
805                groups,
806                overlapping: false,
807                monotonic: _,
808            } => Some(groups),
809        }
810    }
811
812    /// Compare groups based on inner pointer.
813    pub fn is_same(&self, other: &Self) -> bool {
814        Arc::ptr_eq(&self.original, &other.original)
815            && self.offset == other.offset
816            && self.len == other.len
817    }
818}
819
820fn slice_groups_inner(g: &GroupsType, offset: i64, len: usize) -> ManuallyDrop<GroupsType> {
821    // SAFETY:
822    // we create new `Vec`s from the sliced groups. But we wrap them in ManuallyDrop
823    // so that we never call drop on them.
824    // These groups lifetimes are bounded to the `g`. This must remain valid
825    // for the scope of the aggregation.
826    match g {
827        GroupsType::Idx(groups) => {
828            let first = unsafe {
829                let first = slice_slice(groups.first(), offset, len);
830                let ptr = first.as_ptr() as *mut _;
831                Vec::from_raw_parts(ptr, first.len(), first.len())
832            };
833
834            let all = unsafe {
835                let all = slice_slice(groups.all(), offset, len);
836                let ptr = all.as_ptr() as *mut _;
837                Vec::from_raw_parts(ptr, all.len(), all.len())
838            };
839            ManuallyDrop::new(GroupsType::Idx(GroupsIdx::new(
840                first,
841                all,
842                groups.is_sorted_by_first_idx(),
843            )))
844        },
845        GroupsType::Slice {
846            groups,
847            overlapping,
848            monotonic,
849        } => {
850            let groups = unsafe {
851                let groups = slice_slice(groups, offset, len);
852                let ptr = groups.as_ptr() as *mut _;
853                Vec::from_raw_parts(ptr, groups.len(), groups.len())
854            };
855
856            ManuallyDrop::new(GroupsType::new_slice(groups, *overlapping, *monotonic))
857        },
858    }
859}
860
861fn slice_groups(g: Arc<GroupsType>, offset: i64, len: usize) -> GroupPositions {
862    let sliced = slice_groups_inner(g.as_ref(), offset, len);
863
864    GroupPositions {
865        sliced,
866        original: g,
867        offset,
868        len,
869    }
870}