Skip to main content

arrow_buffer/buffer/
offset.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5use crate::buffer::ScalarBuffer;
6use crate::{ArrowNativeType, MutableBuffer, NullBuffer, OffsetBufferBuilder, OverflowError};
7use std::ops::Deref;
8
9/// A non-empty buffer of monotonically increasing, positive integers.
10///
11/// [`OffsetBuffer`] are used to represent ranges of offsets. An
12/// `OffsetBuffer` of `N+1` items contains `N` such ranges. The start
13/// offset for element `i` is `offsets[i]` and the end offset is
14/// `offsets[i+1]`. Equal offsets represent an empty range.
15///
16/// # Example
17///
18/// This example shows how 5 distinct ranges, are represented using a
19/// 6 entry `OffsetBuffer`. The first entry `(0, 3)` represents the
20/// three offsets `0, 1, 2`. The entry `(3,3)` represent no offsets
21/// (e.g. an empty list).
22///
23/// ```text
24///   ┌───────┐                ┌───┐
25///   │ (0,3) │                │ 0 │
26///   ├───────┤                ├───┤
27///   │ (3,3) │                │ 3 │
28///   ├───────┤                ├───┤
29///   │ (3,4) │                │ 3 │
30///   ├───────┤                ├───┤
31///   │ (4,5) │                │ 4 │
32///   ├───────┤                ├───┤
33///   │ (5,7) │                │ 5 │
34///   └───────┘                ├───┤
35///                            │ 7 │
36///                            └───┘
37///
38///                        Offsets Buffer
39///    Logical
40///    Offsets
41///
42///  (offsets[i],
43///   offsets[i+1])
44/// ```
45#[derive(Debug, Clone, PartialEq, Eq)]
46pub struct OffsetBuffer<O: ArrowNativeType>(ScalarBuffer<O>);
47
48impl<O: ArrowNativeType> OffsetBuffer<O> {
49    /// Create a new [`OffsetBuffer`] from the provided [`ScalarBuffer`]
50    ///
51    /// # Panics
52    ///
53    /// Panics if `buffer` is not a non-empty buffer containing
54    /// monotonically increasing values greater than or equal to zero
55    pub fn new(buffer: ScalarBuffer<O>) -> Self {
56        assert!(!buffer.is_empty(), "offsets cannot be empty");
57        assert!(
58            buffer[0] >= O::usize_as(0),
59            "offsets must be greater than 0"
60        );
61        assert!(
62            buffer.windows(2).all(|w| w[0] <= w[1]),
63            "offsets must be monotonically increasing"
64        );
65        Self(buffer)
66    }
67
68    /// Create a new [`OffsetBuffer`] from the provided [`ScalarBuffer`]
69    ///
70    /// # Safety
71    ///
72    /// `buffer` must be a non-empty buffer containing monotonically increasing
73    /// values greater than or equal to zero
74    pub unsafe fn new_unchecked(buffer: ScalarBuffer<O>) -> Self {
75        Self(buffer)
76    }
77
78    /// Create a new [`OffsetBuffer`] containing a single 0 value
79    pub fn new_empty() -> Self {
80        let buffer = MutableBuffer::from_len_zeroed(std::mem::size_of::<O>());
81        Self(buffer.into_buffer().into())
82    }
83
84    /// Create a new [`OffsetBuffer`] containing `len + 1` `0` values
85    ///
86    /// # Panics
87    ///
88    /// Panics if `(len + 1) * size_of::<O>()` overflows `usize` or if the
89    /// required buffer cannot be allocated.
90    /// Use [`Self::try_new_zeroed`] for a fallible version.
91    pub fn new_zeroed(len: usize) -> Self {
92        Self::try_new_zeroed(len).unwrap_or_else(|err| panic!("{err}"))
93    }
94
95    /// Create a new [`OffsetBuffer`] containing `len + 1` `0` values
96    ///
97    /// # Errors
98    ///
99    /// Errors if `(len + 1) * size_of::<O>()` overflows `usize` or if the
100    /// required buffer cannot be allocated.
101    pub fn try_new_zeroed(len: usize) -> Result<Self, OverflowError> {
102        let len_bytes = len
103            .checked_add(1)
104            .and_then(|o| o.checked_mul(std::mem::size_of::<O>()))
105            .ok_or_else(|| OverflowError::new::<usize>("buffer length"))?;
106        let buffer = MutableBuffer::try_from_len_zeroed(len_bytes)
107            .map_err(|_| OverflowError::new::<usize>("buffer length"))?;
108        Ok(Self(buffer.into_buffer().into()))
109    }
110
111    /// Create a new [`OffsetBuffer`] from the iterator of slice lengths
112    ///
113    /// ```
114    /// # use arrow_buffer::OffsetBuffer;
115    /// let offsets = OffsetBuffer::<i32>::from_lengths([1, 3, 5]);
116    /// assert_eq!(offsets.as_ref(), &[0, 1, 4, 9]);
117    /// ```
118    ///
119    /// If you want to create an [`OffsetBuffer`] where all lengths are the same,
120    /// consider using the faster [`OffsetBuffer::from_repeated_length`] instead.
121    ///
122    /// # Panics
123    ///
124    /// Panics on overflow. Use [`Self::try_from_lengths`] for a fallible version.
125    pub fn from_lengths<I>(lengths: I) -> Self
126    where
127        I: IntoIterator<Item = usize>,
128    {
129        Self::try_from_lengths(lengths).unwrap_or_else(|err| panic!("{err}"))
130    }
131
132    /// Create a new [`OffsetBuffer`] from the iterator of slice lengths
133    ///
134    /// ```
135    /// # use arrow_buffer::OffsetBuffer;
136    /// let offsets = OffsetBuffer::<i32>::try_from_lengths([1, 3, 5]).unwrap();
137    /// assert_eq!(offsets.as_ref(), &[0, 1, 4, 9]);
138    /// ```
139    ///
140    /// # Errors
141    ///
142    /// Errors if the total length overflows `usize` or `O`, e.g. if the lengths
143    /// add up to more than `i32::MAX` for a `OffsetBuffer<i32>`.
144    pub fn try_from_lengths<I>(lengths: I) -> Result<Self, OverflowError>
145    where
146        I: IntoIterator<Item = usize>,
147    {
148        let iter = lengths.into_iter();
149        let mut out = Vec::with_capacity(iter.size_hint().0 + 1);
150        out.push(O::usize_as(0));
151
152        let mut acc = 0_usize;
153        for length in iter {
154            acc = acc
155                .checked_add(length)
156                .ok_or_else(|| OverflowError::new::<usize>("total length"))?;
157            out.push(O::usize_as(acc))
158        }
159        // Check for overflow
160        O::from_usize(acc).ok_or_else(|| OverflowError::new::<O>("offset").with_value(acc))?;
161        Ok(Self(out.into()))
162    }
163
164    /// Create a new [`OffsetBuffer`] where each slice has the same length
165    /// `length`, repeated `n` times.
166    ///
167    ///
168    /// Example
169    /// ```
170    /// # use arrow_buffer::OffsetBuffer;
171    /// let offsets = OffsetBuffer::<i32>::from_repeated_length(4, 3);
172    /// assert_eq!(offsets.as_ref(), &[0, 4, 8, 12]);
173    /// ```
174    ///
175    /// # Panics
176    ///
177    /// Panics on overflow. Use [`Self::try_from_repeated_length`] for a fallible version.
178    pub fn from_repeated_length(length: usize, n: usize) -> Self {
179        Self::try_from_repeated_length(length, n).unwrap_or_else(|err| panic!("{err}"))
180    }
181
182    /// Create a new [`OffsetBuffer`] where each slice has the same length
183    /// `length`, repeated `n` times.
184    ///
185    /// ```
186    /// # use arrow_buffer::OffsetBuffer;
187    /// let offsets = OffsetBuffer::<i32>::try_from_repeated_length(4, 3).unwrap();
188    /// assert_eq!(offsets.as_ref(), &[0, 4, 8, 12]);
189    /// ```
190    ///
191    /// # Errors
192    ///
193    /// Errors if `length * n` overflows `usize` or `O`, or if the buffer cannot be allocated.
194    pub fn try_from_repeated_length(length: usize, n: usize) -> Result<Self, OverflowError> {
195        if n == 0 {
196            return Ok(Self::new_empty());
197        }
198
199        if length == 0 {
200            return Self::try_new_zeroed(n);
201        }
202
203        // Making sure we don't overflow usize or O when calculating the total length
204        let total_length = length
205            .checked_mul(n)
206            .ok_or_else(|| OverflowError::new::<usize>("total length"))?;
207
208        O::from_usize(total_length)
209            .ok_or_else(|| OverflowError::new::<O>("offset").with_value(total_length))?;
210
211        let len_bytes = n
212            .checked_add(1)
213            .and_then(|o| o.checked_mul(std::mem::size_of::<O>()))
214            .ok_or_else(|| OverflowError::new::<usize>("buffer length"))?;
215
216        let mut buffer = MutableBuffer::try_with_capacity(len_bytes)
217            .map_err(|_| OverflowError::new::<usize>("buffer length"))?;
218
219        for index in 0..=n {
220            // SAFETY: buffer capacity was allocated for `(n + 1) * size_of::<O>()` bytes.
221            unsafe {
222                buffer.push_unchecked(O::usize_as(index * length));
223            }
224        }
225
226        Ok(Self(buffer.into_buffer().into()))
227    }
228
229    /// The first offset, i.e. the start of the first range.
230    ///
231    /// An [`OffsetBuffer`] is never empty, so this always returns an offset.
232    ///
233    /// ```
234    /// # use arrow_buffer::OffsetBuffer;
235    /// let offsets = OffsetBuffer::<i32>::from_lengths([1, 3, 5]);
236    /// assert_eq!(offsets.first(), 0);
237    /// assert_eq!(OffsetBuffer::<i32>::new_empty().first(), 0);
238    /// ```
239    #[inline]
240    pub fn first(&self) -> O {
241        self.0
242            .first()
243            .copied()
244            .expect("An `OffsetBuffer` is never empty")
245    }
246
247    /// The last offset, i.e. the end of the last range.
248    ///
249    /// An [`OffsetBuffer`] is never empty, so this always returns an offset.
250    ///
251    /// ```
252    /// # use arrow_buffer::OffsetBuffer;
253    /// let offsets = OffsetBuffer::<i32>::from_lengths([1, 3, 5]);
254    /// assert_eq!(offsets.last(), 9);
255    /// assert_eq!(OffsetBuffer::<i32>::new_empty().last(), 0);
256    /// ```
257    #[inline]
258    pub fn last(&self) -> O {
259        self.0
260            .last()
261            .copied()
262            .expect("An `OffsetBuffer` is never empty")
263    }
264
265    /// Get an Iterator over the lengths of this [`OffsetBuffer`]
266    ///
267    /// ```
268    /// # use arrow_buffer::{OffsetBuffer, ScalarBuffer};
269    /// let offsets = OffsetBuffer::<_>::new(ScalarBuffer::<i32>::from(vec![0, 1, 4, 9]));
270    /// assert_eq!(offsets.lengths().collect::<Vec<usize>>(), vec![1, 3, 5]);
271    /// ```
272    ///
273    /// Empty [`OffsetBuffer`] will return an empty iterator
274    /// ```
275    /// # use arrow_buffer::OffsetBuffer;
276    /// let offsets = OffsetBuffer::<i32>::new_empty();
277    /// assert_eq!(offsets.lengths().count(), 0);
278    /// ```
279    ///
280    /// This can be used to merge multiple [`OffsetBuffer`]s to one
281    /// ```
282    /// # use arrow_buffer::{OffsetBuffer, ScalarBuffer};
283    ///
284    /// let buffer1 = OffsetBuffer::<i32>::from_lengths([2, 6, 3, 7, 2]);
285    /// let buffer2 = OffsetBuffer::<i32>::from_lengths([1, 3, 5, 7, 9]);
286    ///
287    /// let merged = OffsetBuffer::<i32>::from_lengths(
288    ///     vec![buffer1, buffer2].iter().flat_map(|x| x.lengths())
289    /// );
290    ///
291    /// assert_eq!(merged.lengths().collect::<Vec<_>>(), &[2, 6, 3, 7, 2, 1, 3, 5, 7, 9]);
292    /// ```
293    pub fn lengths(&self) -> impl ExactSizeIterator<Item = usize> + '_ {
294        self.0.windows(2).map(|x| x[1].as_usize() - x[0].as_usize())
295    }
296
297    /// Free up unused memory.
298    pub fn shrink_to_fit(&mut self) {
299        self.0.shrink_to_fit();
300    }
301
302    /// Returns the inner [`ScalarBuffer`]
303    pub fn inner(&self) -> &ScalarBuffer<O> {
304        &self.0
305    }
306
307    /// Returns the inner [`ScalarBuffer`], consuming self
308    pub fn into_inner(self) -> ScalarBuffer<O> {
309        self.0
310    }
311
312    /// Claim memory used by this buffer in the provided memory pool.
313    #[cfg(feature = "pool")]
314    pub fn claim(&self, pool: &dyn crate::MemoryPool) {
315        self.0.claim(pool);
316    }
317
318    /// Returns a zero-copy slice of this buffer with length `len` and starting at `offset`
319    ///
320    /// # Panics
321    ///
322    /// Panics if `offset + len > self.len()`
323    pub fn slice(&self, offset: usize, len: usize) -> Self {
324        Self(self.0.slice(offset, len.saturating_add(1)))
325    }
326
327    /// Returns true if this [`OffsetBuffer`] is equal to `other`, using pointer comparisons
328    /// to determine buffer equality. This is cheaper than `PartialEq::eq` but may
329    /// return false when the arrays are logically equal
330    #[inline]
331    pub fn ptr_eq(&self, other: &Self) -> bool {
332        self.0.ptr_eq(&other.0)
333    }
334
335    /// Check if any null positions in the `null_buffer` correspond to
336    /// non-empty ranges in this [`OffsetBuffer`].
337    ///
338    /// In variable-length array types (e.g., `StringArray`, `ListArray`),
339    /// null entries may or may not have empty offset ranges. This method
340    /// detects cases where a null entry has a non-empty range
341    /// (i.e., `offsets[i] != offsets[i+1]`), which means the underlying
342    /// data buffer contains data behind nulls.
343    ///
344    /// This matters because unwrapping (flattening) a list array exposes
345    /// the child values, including those behind null entries. If null
346    /// entries point to non-empty ranges, the unwrapped values will
347    /// contain data that may not be meaningful to operate on and could
348    /// cause errors (e.g., division by zero in the child values).
349    ///
350    /// Returns `false` if `null_buffer` is `None` or contains no nulls.
351    ///
352    /// # Example
353    ///
354    /// ```
355    /// # use arrow_buffer::{OffsetBuffer, ScalarBuffer, NullBuffer};
356    /// // Offsets where null at index 1 has an empty range (3..3)
357    /// let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![0, 3, 3, 6]));
358    /// let nulls = NullBuffer::from(vec![true, false, true]);
359    /// assert!(!offsets.has_non_empty_nulls(Some(&nulls)));
360    ///
361    /// // Offsets where null at index 1 has a non-empty range (3..7)
362    /// let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![0, 3, 7, 10]));
363    /// let nulls = NullBuffer::from(vec![true, false, true]);
364    /// assert!(offsets.has_non_empty_nulls(Some(&nulls)));
365    /// ```
366    ///
367    /// # Panics
368    ///
369    /// Panics if the length of the `null_buffer` does not equal `self.len() - 1`.
370    pub fn has_non_empty_nulls(&self, null_buffer: Option<&NullBuffer>) -> bool {
371        let Some(null_buffer) = null_buffer else {
372            return false;
373        };
374
375        assert_eq!(
376            self.len() - 1,
377            null_buffer.len(),
378            "The length of the offsets should be 1 more than the length of the null buffer"
379        );
380
381        if null_buffer.null_count() == 0 {
382            return false;
383        }
384
385        // Offsets always have at least 1 value
386        let initial_offset = self[0];
387        let last_offset = self[self.len() - 1];
388
389        // If all the values are null (offsets have 1 more value than the length of the array)
390        if null_buffer.null_count() == self.len() - 1 {
391            return last_offset != initial_offset;
392        }
393
394        let mut valid_slices_iter = null_buffer.valid_slices();
395
396        // This is safe as we validated that are at least 1 valid value in the array
397        let (start, end) = valid_slices_iter.next().unwrap();
398
399        // If the nulls before have length greater than 0
400        if self[start] != initial_offset {
401            return true;
402        }
403
404        // End is exclusive, so it already point to the last offset value
405        // This is valid as the length of the array is always 1 less than the length of the offsets
406        let mut end_offset_of_last_valid_value = self[end];
407
408        for (start, end) in valid_slices_iter {
409            // If there is a null value that point to a non-empty value than the start offset of the valid value
410            // will be different that the end offset of the last valid value
411            if self[start] != end_offset_of_last_valid_value {
412                return true;
413            }
414
415            // End is exclusive, so it already point to the last offset value
416            // This is valid as the length of the array is always 1 less than the length of the offsets
417            end_offset_of_last_valid_value = self[end];
418        }
419
420        end_offset_of_last_valid_value != last_offset
421    }
422
423    /// Subtract `rhs` from all offsets
424    /// This will try to reuse the existing allocation as much as possible
425    ///
426    /// # Panics
427    ///
428    /// Panics if `rhs` > the first offset or if `rhs` will lead to overflow (when `rhs` is negative)
429    ///
430    /// # Example
431    ///
432    /// ```
433    /// # use arrow_buffer::OffsetBuffer;
434    /// let offsets = OffsetBuffer::<i32>::from_lengths(vec![4, 1, 5, 6]);
435    /// assert_eq!(offsets.as_ref(), &[0, 4, 5, 10, 16]);
436    ///
437    /// let sliced_offsets = offsets.slice(1, 2);
438    /// assert_eq!(sliced_offsets.as_ref(), &[4, 5, 10]);
439    ///
440    /// let shifted_offsets = sliced_offsets.subtract(4);
441    /// assert_eq!(shifted_offsets.as_ref(), &[0, 1, 6]);
442    /// ```
443    ///
444    pub fn subtract(self, rhs: O) -> Self
445    where
446        O: std::ops::Sub<Output = O> + std::cmp::PartialOrd + num_traits::CheckedSub,
447    {
448        if rhs == O::usize_as(0) {
449            return self;
450        }
451
452        let len = self.len();
453
454        // Offset buffer is guaranteed to be non-empty
455        assert!(
456            self[0] >= rhs,
457            "shifted offsets will become negative which is not allowed"
458        );
459
460        // If negative, make sure that this will not create an overflow
461        if rhs < O::usize_as(0) {
462            self[len - 1].checked_sub(&rhs).expect("must not overflow");
463        }
464
465        // try and reuse buffer
466        let shifted_offsets: Vec<O> = match self.into_inner().into_inner().into_vec() {
467            // If we can reuse the buffer, update in place
468            Ok(mut v) => {
469                for offset in &mut v {
470                    *offset = *offset - rhs;
471                }
472                v
473            }
474            // otherwise, buffer is shared so we need a copy
475            Err(buffer) => {
476                let offsets = ScalarBuffer::<O>::from(buffer);
477                offsets.iter().map(|offset| *offset - rhs).collect()
478            }
479        };
480        let shifted_buffer = ScalarBuffer::from(shifted_offsets);
481        // Safety: offsets are valid as they are coming from a valid
482        // offset buffer and we checked overflow above, and we
483        // subtracted the same value from all offsets, thus keeping the
484        // same properties as the input buffer
485        unsafe { Self::new_unchecked(shifted_buffer) }
486    }
487}
488
489impl<T: ArrowNativeType> Deref for OffsetBuffer<T> {
490    type Target = [T];
491
492    #[inline]
493    fn deref(&self) -> &Self::Target {
494        &self.0
495    }
496}
497
498impl<T: ArrowNativeType> AsRef<[T]> for OffsetBuffer<T> {
499    #[inline]
500    fn as_ref(&self) -> &[T] {
501        self
502    }
503}
504
505impl<O: ArrowNativeType> From<OffsetBufferBuilder<O>> for OffsetBuffer<O> {
506    fn from(value: OffsetBufferBuilder<O>) -> Self {
507        value.finish()
508    }
509}
510
511impl<O: ArrowNativeType> Default for OffsetBuffer<O> {
512    fn default() -> Self {
513        Self::new_empty()
514    }
515}
516
517#[cfg(test)]
518mod tests {
519    use super::*;
520
521    #[test]
522    #[should_panic(expected = "offsets cannot be empty")]
523    fn empty_offsets() {
524        OffsetBuffer::new(Vec::<i32>::new().into());
525    }
526
527    #[test]
528    #[should_panic(expected = "offsets must be greater than 0")]
529    fn negative_offsets() {
530        OffsetBuffer::new(vec![-1, 0, 1].into());
531    }
532
533    #[test]
534    fn try_from_lengths_overflow() {
535        // Fits in an i64, but not in an i32:
536        let lengths = [u32::MAX as usize, 1];
537
538        let err = OffsetBuffer::<i32>::try_from_lengths(lengths).unwrap_err();
539        assert_eq!(
540            err.to_string(),
541            "offset overflow: 4294967296 does not fit in i32"
542        );
543        assert!(OffsetBuffer::<i64>::try_from_lengths(lengths).is_ok());
544
545        let err = OffsetBuffer::<i32>::try_from_lengths([usize::MAX, 1]).unwrap_err();
546        assert_eq!(
547            err.to_string(),
548            "total length overflow: does not fit in usize"
549        );
550
551        // The panicking version agrees:
552        assert!(std::panic::catch_unwind(|| OffsetBuffer::<i32>::from_lengths(lengths)).is_err());
553    }
554
555    #[test]
556    fn try_from_repeated_length_overflow() {
557        assert_eq!(
558            OffsetBuffer::<i32>::try_from_repeated_length(2, usize::MAX)
559                .unwrap_err()
560                .to_string(),
561            "total length overflow: does not fit in usize"
562        );
563        assert_eq!(
564            OffsetBuffer::<i32>::try_from_repeated_length(u32::MAX as usize, 2)
565                .unwrap_err()
566                .to_string(),
567            "offset overflow: 8589934590 does not fit in i32"
568        );
569        assert_eq!(
570            OffsetBuffer::<i32>::try_from_repeated_length(4, 3)
571                .unwrap()
572                .as_ref(),
573            &[0, 4, 8, 12]
574        );
575    }
576
577    #[test]
578    fn try_new_zeroed_overflow() {
579        assert_eq!(
580            OffsetBuffer::<i64>::try_new_zeroed(usize::MAX)
581                .unwrap_err()
582                .to_string(),
583            "buffer length overflow: does not fit in usize"
584        );
585        let max_layout_len = isize::MAX as usize / std::mem::size_of::<i64>();
586        assert!(OffsetBuffer::<i64>::try_new_zeroed(max_layout_len).is_err());
587        assert_eq!(
588            OffsetBuffer::<i32>::try_new_zeroed(3).unwrap().as_ref(),
589            &[0; 4]
590        );
591    }
592
593    #[test]
594    fn offsets() {
595        OffsetBuffer::new(vec![0, 1, 2, 3].into());
596
597        let offsets = OffsetBuffer::<i32>::new_zeroed(3);
598        assert_eq!(offsets.as_ref(), &[0; 4]);
599
600        let offsets = OffsetBuffer::<i32>::new_zeroed(0);
601        assert_eq!(offsets.as_ref(), &[0; 1]);
602    }
603
604    #[test]
605    #[should_panic(expected = "overflow")]
606    fn offsets_new_zeroed_overflow() {
607        OffsetBuffer::<i32>::new_zeroed(usize::MAX);
608    }
609
610    #[test]
611    #[should_panic(expected = "offsets must be monotonically increasing")]
612    fn non_monotonic_offsets() {
613        OffsetBuffer::new(vec![1, 2, 0].into());
614    }
615
616    #[test]
617    fn from_lengths() {
618        let buffer = OffsetBuffer::<i32>::from_lengths([2, 6, 3, 7, 2]);
619        assert_eq!(buffer.as_ref(), &[0, 2, 8, 11, 18, 20]);
620
621        let half_max = i32::MAX / 2;
622        let buffer = OffsetBuffer::<i32>::from_lengths([half_max as usize, half_max as usize]);
623        assert_eq!(buffer.as_ref(), &[0, half_max, half_max * 2]);
624    }
625
626    #[test]
627    #[should_panic(expected = "offset overflow")]
628    fn from_lengths_offset_overflow() {
629        OffsetBuffer::<i32>::from_lengths([i32::MAX as usize, 1]);
630    }
631
632    #[test]
633    #[should_panic(expected = "total length overflow: does not fit in usize")]
634    fn from_lengths_usize_overflow() {
635        OffsetBuffer::<i32>::from_lengths([usize::MAX, 1]);
636    }
637
638    #[test]
639    #[should_panic(expected = "offset overflow")]
640    fn from_repeated_lengths_offset_length_overflow() {
641        OffsetBuffer::<i32>::from_repeated_length(i32::MAX as usize / 4, 5);
642    }
643
644    #[test]
645    #[should_panic(expected = "offset overflow")]
646    fn from_repeated_lengths_offset_repeat_overflow() {
647        OffsetBuffer::<i32>::from_repeated_length(1, i32::MAX as usize + 1);
648    }
649
650    #[test]
651    #[should_panic(expected = "offset overflow")]
652    fn from_repeated_lengths_usize_length_overflow() {
653        OffsetBuffer::<i32>::from_repeated_length(usize::MAX, 1);
654    }
655
656    #[test]
657    #[should_panic(expected = "total length overflow: does not fit in usize")]
658    fn from_repeated_lengths_usize_length_usize_overflow() {
659        OffsetBuffer::<i32>::from_repeated_length(usize::MAX, 2);
660    }
661
662    #[test]
663    #[should_panic(expected = "offset overflow")]
664    fn from_repeated_lengths_usize_repeat_overflow() {
665        OffsetBuffer::<i32>::from_repeated_length(1, usize::MAX);
666    }
667
668    #[test]
669    fn get_lengths() {
670        let offsets = OffsetBuffer::<i32>::new(ScalarBuffer::<i32>::from(vec![0, 1, 4, 9]));
671        assert_eq!(offsets.lengths().collect::<Vec<usize>>(), vec![1, 3, 5]);
672    }
673
674    #[test]
675    fn get_lengths_should_be_with_fixed_size() {
676        let offsets = OffsetBuffer::<i32>::new(ScalarBuffer::<i32>::from(vec![0, 1, 4, 9]));
677        let iter = offsets.lengths();
678        assert_eq!(iter.size_hint(), (3, Some(3)));
679        assert_eq!(iter.len(), 3);
680    }
681
682    #[test]
683    fn get_lengths_from_empty_offset_buffer_should_be_empty_iterator() {
684        let offsets = OffsetBuffer::<i32>::new_empty();
685        assert_eq!(offsets.lengths().collect::<Vec<usize>>(), vec![]);
686    }
687
688    #[test]
689    fn impl_eq() {
690        fn are_equal<T: Eq>(a: &T, b: &T) -> bool {
691            a.eq(b)
692        }
693
694        assert!(
695            are_equal(
696                &OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![0, 1, 4, 9])),
697                &OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![0, 1, 4, 9]))
698            ),
699            "OffsetBuffer should implement Eq."
700        );
701    }
702
703    #[test]
704    fn impl_default() {
705        let default = OffsetBuffer::<i32>::default();
706        assert_eq!(default.as_ref(), &[0]);
707    }
708
709    #[test]
710    fn from_repeated_length_basic() {
711        // Basic case with length 4, repeated 3 times
712        let buffer = OffsetBuffer::<i32>::from_repeated_length(4, 3);
713        assert_eq!(buffer.as_ref(), &[0, 4, 8, 12]);
714
715        // Verify the lengths are correct
716        let lengths: Vec<usize> = buffer.lengths().collect();
717        assert_eq!(lengths, vec![4, 4, 4]);
718    }
719
720    #[test]
721    fn from_repeated_length_single_repeat() {
722        // Length 5, repeated once
723        let buffer = OffsetBuffer::<i32>::from_repeated_length(5, 1);
724        assert_eq!(buffer.as_ref(), &[0, 5]);
725
726        let lengths: Vec<usize> = buffer.lengths().collect();
727        assert_eq!(lengths, vec![5]);
728    }
729
730    #[test]
731    fn from_repeated_length_zero_repeats() {
732        let buffer = OffsetBuffer::<i32>::from_repeated_length(10, 0);
733        assert_eq!(buffer, OffsetBuffer::<i32>::new_empty());
734    }
735
736    #[test]
737    fn from_repeated_length_zero_length() {
738        // Zero length, repeated 5 times (all zeros)
739        let buffer = OffsetBuffer::<i32>::from_repeated_length(0, 5);
740        assert_eq!(buffer.as_ref(), &[0, 0, 0, 0, 0, 0]);
741
742        // All lengths should be 0
743        let lengths: Vec<usize> = buffer.lengths().collect();
744        assert_eq!(lengths, vec![0, 0, 0, 0, 0]);
745    }
746
747    #[test]
748    fn from_repeated_length_large_values() {
749        // Test with larger values that don't overflow
750        let buffer = OffsetBuffer::<i32>::from_repeated_length(1000, 100);
751        assert_eq!(buffer[0], 0);
752
753        // Verify all lengths are 1000
754        let lengths: Vec<usize> = buffer.lengths().collect();
755        assert_eq!(lengths.len(), 100);
756        assert!(lengths.iter().all(|&len| len == 1000));
757    }
758
759    #[test]
760    fn from_repeated_length_unit_length() {
761        // Length 1, repeated multiple times
762        let buffer = OffsetBuffer::<i32>::from_repeated_length(1, 10);
763        assert_eq!(buffer.as_ref(), &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
764
765        let lengths: Vec<usize> = buffer.lengths().collect();
766        assert_eq!(lengths, vec![1; 10]);
767    }
768
769    #[test]
770    fn from_repeated_length_max_safe_values() {
771        // Test with maximum safe values for i32
772        // i32::MAX / 3 ensures we don't overflow when repeated twice
773        let third_max = (i32::MAX / 3) as usize;
774        let buffer = OffsetBuffer::<i32>::from_repeated_length(third_max, 2);
775        assert_eq!(
776            buffer.as_ref(),
777            &[0, third_max as i32, (third_max * 2) as i32]
778        );
779    }
780
781    // ---------------------------------------------------------------
782    // Tests for has_non_empty_nulls
783    // ---------------------------------------------------------------
784
785    #[test]
786    fn has_non_empty_nulls_none_null_buffer() {
787        // No null buffer at all -> false
788        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![0, 3, 5, 8]));
789        assert!(!offsets.has_non_empty_nulls(None));
790    }
791
792    #[test]
793    fn has_non_empty_nulls_all_valid() {
794        // Null buffer with zero nulls -> false (early return via filter)
795        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![0, 3, 5, 8]));
796        let nulls = NullBuffer::new_valid(3);
797        assert!(!offsets.has_non_empty_nulls(Some(&nulls)));
798    }
799
800    #[test]
801    fn has_non_empty_nulls_all_null_empty_offsets() {
802        // All values are null and all offsets are equal (no data behind nulls) -> false
803        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![0, 0, 0, 0]));
804        let nulls = NullBuffer::new_null(3);
805        assert!(!offsets.has_non_empty_nulls(Some(&nulls)));
806    }
807
808    #[test]
809    fn has_non_empty_nulls_all_null_non_empty_offsets() {
810        // All values are null but offsets span data -> true
811        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![0, 2, 5, 7]));
812        let nulls = NullBuffer::new_null(3);
813        assert!(offsets.has_non_empty_nulls(Some(&nulls)));
814    }
815
816    #[test]
817    fn has_non_empty_nulls_all_null_nonzero_but_equal_offsets() {
818        // All null, offsets start at non-zero but are all equal -> false
819        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![5, 5, 5]));
820        let nulls = NullBuffer::new_null(2);
821        assert!(!offsets.has_non_empty_nulls(Some(&nulls)));
822    }
823
824    #[test]
825    fn has_non_empty_nulls_leading_nulls_with_data() {
826        // Nulls at the beginning that point to non-empty ranges -> true
827        // offsets: [0, 3, 5, 8]  nulls: [false, true, true]
828        // Index 0 is null with range 0..3 (non-empty)
829        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![0, 3, 5, 8]));
830        let nulls = NullBuffer::from(vec![false, true, true]);
831        assert!(offsets.has_non_empty_nulls(Some(&nulls)));
832    }
833
834    #[test]
835    fn has_non_empty_nulls_leading_nulls_without_data() {
836        // Nulls at the beginning with empty ranges -> continue checking
837        // offsets: [0, 0, 3, 6]  nulls: [false, true, true]
838        // Index 0 is null with range 0..0 (empty)
839        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![0, 0, 3, 6]));
840        let nulls = NullBuffer::from(vec![false, true, true]);
841        assert!(!offsets.has_non_empty_nulls(Some(&nulls)));
842    }
843
844    #[test]
845    fn has_non_empty_nulls_only_trailing_null_has_data() {
846        // Only the trailing null region has data, everything else is clean
847        // offsets: [0, 0, 3, 6, 8]  nulls: [false, true, true, false]
848        // Null at 0 (0..0 empty), valid at 1,2 (0..3, 3..6), null at 3 (6..8 non-empty)
849        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![0, 0, 3, 6, 8]));
850        let nulls = NullBuffer::from(vec![false, true, true, false]);
851        assert!(offsets.has_non_empty_nulls(Some(&nulls)));
852    }
853
854    #[test]
855    fn has_non_empty_nulls_trailing_nulls_without_data() {
856        // Nulls at the end with empty ranges -> false
857        // offsets: [0, 3, 6, 6]  nulls: [true, true, false]
858        // Index 2 is null with range 6..6 (empty)
859        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![0, 3, 6, 6]));
860        let nulls = NullBuffer::from(vec![true, true, false]);
861        assert!(!offsets.has_non_empty_nulls(Some(&nulls)));
862    }
863
864    #[test]
865    fn has_non_empty_nulls_middle_nulls_with_data() {
866        // Null in the middle with non-empty range -> true
867        // offsets: [0, 3, 7, 10]  nulls: [true, false, true]
868        // Index 1 is null with range 3..7 (non-empty)
869        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![0, 3, 7, 10]));
870        let nulls = NullBuffer::from(vec![true, false, true]);
871        assert!(offsets.has_non_empty_nulls(Some(&nulls)));
872    }
873
874    #[test]
875    fn has_non_empty_nulls_middle_nulls_without_data() {
876        // Null in the middle with empty range -> false
877        // offsets: [0, 3, 3, 6]  nulls: [true, false, true]
878        // Index 1 is null with range 3..3 (empty)
879        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![0, 3, 3, 6]));
880        let nulls = NullBuffer::from(vec![true, false, true]);
881        assert!(!offsets.has_non_empty_nulls(Some(&nulls)));
882    }
883
884    #[test]
885    fn has_non_empty_nulls_alternating_null_valid_all_empty() {
886        // Alternating null/valid where every null has an empty range -> false.
887
888        // Ends with null
889        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![0, 0, 3, 3, 6, 6]));
890        let nulls = NullBuffer::from(vec![false, true, false, true, false]);
891        assert!(!offsets.has_non_empty_nulls(Some(&nulls)));
892
893        // Ends with valid
894        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![0, 0, 3, 3, 6, 6, 9]));
895        let nulls = NullBuffer::from(vec![false, true, false, true, false, true]);
896        assert!(!offsets.has_non_empty_nulls(Some(&nulls)));
897    }
898
899    #[test]
900    fn has_non_empty_nulls_multiple_null_regions_second_has_data() {
901        // Two null regions: first empty, second non-empty -> true
902        // offsets: [0, 0, 3, 5, 6]  nulls: [false, true, false, true]
903        // Null at index 0 (0..0 empty), null at index 2 (3..5 non-empty)
904        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![0, 0, 3, 5, 6]));
905        let nulls = NullBuffer::from(vec![false, true, false, true]);
906        assert!(offsets.has_non_empty_nulls(Some(&nulls)));
907    }
908
909    #[test]
910    fn has_non_empty_nulls_multiple_null_regions_later_gap_has_data() {
911        // Three null regions: first two empty, third non-empty -> true
912        // offsets: [0, 0, 3, 3, 6, 8, 10]  nulls: [false, true, false, true, false, true]
913        // valid_slices: (1,2), (3,4), (5,6)
914        // first slice: start=1, self[1]=0 == initial_offset=0 OK, end_offset=self[2]=3
915        // loop iter 1: start=3, self[3]=3 == 3 OK (first gap empty), end_offset=self[4]=6
916        // loop iter 2: start=5, self[5]=8 != 6 -> true (second gap has data)
917        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![0, 0, 3, 3, 6, 8, 10]));
918        let nulls = NullBuffer::from(vec![false, true, false, true, false, true]);
919        assert!(offsets.has_non_empty_nulls(Some(&nulls)));
920    }
921
922    #[test]
923    fn has_non_empty_nulls_single_element_null_empty() {
924        // Single element, null with empty range -> false
925        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![0, 0]));
926        let nulls = NullBuffer::new_null(1);
927        assert!(!offsets.has_non_empty_nulls(Some(&nulls)));
928    }
929
930    #[test]
931    fn has_non_empty_nulls_single_element_null_non_empty() {
932        // Single element, null with non-empty range -> true
933        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![0, 5]));
934        let nulls = NullBuffer::new_null(1);
935        assert!(offsets.has_non_empty_nulls(Some(&nulls)));
936    }
937
938    #[test]
939    fn has_non_empty_nulls_single_element_valid() {
940        // Single element, valid -> false (no nulls at all)
941        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![0, 5]));
942        let nulls = NullBuffer::new_valid(1);
943        assert!(!offsets.has_non_empty_nulls(Some(&nulls)));
944    }
945
946    #[test]
947    fn has_non_empty_nulls_consecutive_nulls_between_valid_slices() {
948        // Multiple consecutive nulls between valid regions
949        // offsets: [0, 2, 2, 2, 5, 8]  nulls: [true, false, false, true, true]
950        // Valid: [0], nulls: [1,2], valid: [3,4]
951        // Null region [1,2] has offsets 2..2..2 (empty) -> false
952        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![0, 2, 2, 2, 5, 8]));
953        let nulls = NullBuffer::from(vec![true, false, false, true, true]);
954        assert!(!offsets.has_non_empty_nulls(Some(&nulls)));
955    }
956
957    #[test]
958    fn has_non_empty_nulls_consecutive_nulls_between_valid_slices_with_data() {
959        // Multiple consecutive nulls between valid regions, nulls have data
960        // offsets: [0, 2, 3, 4, 5, 8]  nulls: [true, false, false, true, true]
961        // valid_slices: (0,1), (3,5)
962        // first slice: start=0, end=1 -> self[0]=0 == initial_offset=0 OK
963        //   end_offset_of_last_valid_value = self[1] = 2
964        // second slice: start=3, end=5 -> self[3]=4 != 2 -> true
965        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![0, 2, 3, 4, 5, 8]));
966        let nulls = NullBuffer::from(vec![true, false, false, true, true]);
967        assert!(offsets.has_non_empty_nulls(Some(&nulls)));
968    }
969
970    #[test]
971    fn has_non_empty_nulls_nonzero_initial_offset_all_null_equal() {
972        // Non-zero starting offset, all null, all offsets equal -> false
973        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![10, 10, 10]));
974        let nulls = NullBuffer::new_null(2);
975        assert!(!offsets.has_non_empty_nulls(Some(&nulls)));
976    }
977
978    #[test]
979    fn has_non_empty_nulls_nonzero_initial_offset_with_data() {
980        // Non-zero starting offset, null has data
981        // offsets: [10, 15, 20]  nulls: [false, true]
982        // Null at index 0 with range 10..15 (non-empty) -> true
983        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![10, 15, 20]));
984        let nulls = NullBuffer::from(vec![false, true]);
985        assert!(offsets.has_non_empty_nulls(Some(&nulls)));
986    }
987
988    #[test]
989    fn has_non_empty_nulls_sliced_no_nulls_in_null_region() {
990        // Original: [0, 3, 3, 6, 6, 9]  -> slice(1, 3) -> [3, 3, 6, 6]
991        // initial_offset=3, last_offset=6
992        // nulls: [false, true, false]  (null at index 0 has range 3..3 = empty)
993        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![0, 3, 3, 6, 6, 9]));
994        let sliced = offsets.slice(1, 3);
995        let nulls = NullBuffer::from(vec![false, true, false]);
996        assert!(!sliced.has_non_empty_nulls(Some(&nulls)));
997    }
998
999    #[test]
1000    fn has_non_empty_nulls_sliced_null_has_data() {
1001        // Original: [0, 3, 7, 10, 15]  -> slice(1, 2) -> [3, 7, 10]
1002        // initial_offset=3, last_offset=10
1003        // nulls: [false, true]  (null at index 0 has range 3..7 = non-empty)
1004        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![0, 3, 7, 10, 15]));
1005        let sliced = offsets.slice(1, 2);
1006        let nulls = NullBuffer::from(vec![false, true]);
1007        assert!(sliced.has_non_empty_nulls(Some(&nulls)));
1008    }
1009
1010    #[test]
1011    #[should_panic(
1012        expected = "The length of the offsets should be 1 more than the length of the null buffer"
1013    )]
1014    fn has_non_empty_nulls_all_valid_mismatched_lengths_too_short() {
1015        // All-valid null buffer with wrong length should still panic
1016        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![0, 3, 5, 8]));
1017        let nulls = NullBuffer::new_valid(2); // expects 3
1018        offsets.has_non_empty_nulls(Some(&nulls));
1019    }
1020
1021    #[test]
1022    #[should_panic(
1023        expected = "The length of the offsets should be 1 more than the length of the null buffer"
1024    )]
1025    fn has_non_empty_nulls_all_valid_mismatched_lengths_too_long() {
1026        // All-valid null buffer with wrong length should still panic
1027        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![0, 3, 5, 8]));
1028        let nulls = NullBuffer::new_valid(5); // expects 3
1029        offsets.has_non_empty_nulls(Some(&nulls));
1030    }
1031
1032    #[test]
1033    #[should_panic(expected = "shifted offsets will become negative which is not allowed")]
1034    fn should_panic_for_subtract_by_value_that_will_cause_offsets_to_be_less_than_zero() {
1035        // self[0] = 0, rhs = 1 -> 0 >= 1 is false -> assert fires
1036        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![0, 3, 6]));
1037        offsets.subtract(1);
1038    }
1039
1040    #[test]
1041    fn subtract_by_value_that_will_cause_offsets_to_be_less_than_zero_for_outside_the_slice() {
1042        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![0, 1, 4, 7]));
1043        let sliced = offsets.slice(1, 2); // [1, 4, 7]
1044        drop(offsets);
1045        assert_eq!(sliced.as_ref(), &[1, 4, 7]);
1046
1047        let result = sliced.subtract(1);
1048        assert_eq!(result.as_ref(), &[0, 3, 6]);
1049        assert_eq!(result.len(), 3);
1050    }
1051
1052    #[test]
1053    #[should_panic(expected = "must not overflow")]
1054    fn should_panic_subtract_by_value_that_will_cause_offsets_to_overflow() {
1055        // rhs = -1 (negative). self[0] = 0 >= -1 passes.
1056        // last offset i32::MAX - (-1) = i32::MAX + 1 -> checked_sub returns None -> expect fires
1057        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![0, 5, i32::MAX]));
1058        offsets.subtract(-1);
1059    }
1060
1061    #[test]
1062    fn subtract_by_value_that_will_cause_offsets_to_overflow_outside_the_slice() {
1063        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![0, 3, 6, i32::MAX]));
1064        let sliced = offsets.slice(0, 2); // [0, 3, 6]
1065        assert_eq!(sliced.as_ref(), &[0, 3, 6]);
1066
1067        let result = sliced.subtract(-1);
1068        assert_eq!(result.as_ref(), &[1, 4, 7]);
1069        assert_eq!(result.len(), 3);
1070    }
1071
1072    #[test]
1073    fn when_shift_is_0_subtract_should_reuse_the_buffer_even_when_it_is_shared() {
1074        // subtract(0) hits the early `return self` before any into_mutable,
1075        // so the returned buffer is the exact same allocation even while shared.
1076        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![0, 3, 6]));
1077        let shared = offsets.clone(); // refcount now 2 -> shared
1078        let result = offsets.subtract(0);
1079        assert!(
1080            result.ptr_eq(&shared),
1081            "subtract(0) must return the same underlying buffer, even when shared"
1082        );
1083    }
1084
1085    #[test]
1086    fn should_reuse_the_underline_data_when_the_buffer_is_not_shared() {
1087        // Unique ownership, offset 0 -> into_mutable succeeds -> mutate in place,
1088        // and MutableBuffer -> Buffer keeps the same allocation.
1089        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![2, 5, 8]));
1090        let ptr_before = offsets.as_ptr();
1091        let result = offsets.subtract(2);
1092        assert_eq!(
1093            ptr_before,
1094            result.as_ptr(),
1095            "a non-shared buffer should be mutated in place, reusing the allocation"
1096        );
1097        assert_eq!(result.as_ref(), &[0, 3, 6]);
1098    }
1099
1100    #[test]
1101    fn should_create_a_new_buffer_when_the_buffer_is_shared() {
1102        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![2, 5, 8]));
1103        let shared = offsets.clone();
1104        let ptr_before = offsets.as_ptr();
1105        let result = offsets.subtract(2);
1106        assert_ne!(
1107            ptr_before,
1108            result.as_ptr(),
1109            "a shared buffer must not be mutated in place; a new allocation is created"
1110        );
1111        assert_eq!(result.as_ref(), &[0, 3, 6]);
1112        // The shared view is untouched.
1113        assert_eq!(shared.as_ref(), &[2, 5, 8]);
1114    }
1115
1116    #[test]
1117    fn when_shift_is_negative_it_should_shift_offsets_in_the_right_direction() {
1118        // rhs = -2 -> offset - (-2) = offset + 2, so all offsets move up.
1119        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![0, 3, 6]));
1120        let result = offsets.subtract(-2);
1121        assert_eq!(result.as_ref(), &[2, 5, 8]);
1122    }
1123
1124    // Replace this test with test that assert a reuse after PR #10118 is merged
1125    #[test]
1126    fn for_sliced_unshared_buffer_shift_should_not_reuse_buffer() {
1127        // Underlying [0, 3, 6, 9, 12]; slice -> view [3, 6, 9].
1128        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![1, 3, 6, 9, 12]));
1129        let sliced = offsets.slice(1, 2); // [3, 6, 9]
1130        drop(offsets); // uniquely owned
1131        assert_eq!(sliced.as_ref(), &[3, 6, 9]);
1132
1133        let ptr_before = sliced.as_ptr();
1134        let result = sliced.subtract(1);
1135
1136        assert_ne!(
1137            ptr_before,
1138            result.as_ptr(),
1139            "should not be reused until #10118 is merged"
1140        );
1141
1142        assert_eq!(result.as_ref(), &[2, 5, 8]);
1143    }
1144
1145    #[test]
1146    fn for_sliced_but_start_at_0_unshared_buffer_shift_should_reuse_buffer() {
1147        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![1, 3, 6, 9, 12]));
1148        let sliced = offsets.slice(0, 2);
1149        drop(offsets); // uniquely owned
1150        assert_eq!(sliced.as_ref(), &[1, 3, 6]);
1151
1152        let ptr_before = sliced.as_ptr();
1153        let result = sliced.subtract(1);
1154
1155        assert_eq!(ptr_before, result.as_ptr(), "should be reused");
1156
1157        assert_eq!(result.as_ref(), &[0, 2, 5]);
1158    }
1159
1160    #[test]
1161    fn for_sliced_shared_buffer_shifted_buffer_should_only_include_the_sliced_data() {
1162        // Underlying: [0, 3, 6, 9, 12]; slice(1, 2) -> view [3, 6, 9].
1163        // `offsets` stays alive, so the sliced buffer is shared -> Err branch.
1164        // The Err branch copies `len` (= 3) elements from the *sliced* typed_data,
1165        // so the result contains only the sliced data, shifted.
1166        let offsets = OffsetBuffer::new(ScalarBuffer::<i32>::from(vec![0, 3, 6, 9, 12]));
1167        let sliced = offsets.slice(1, 2);
1168        assert_eq!(sliced.as_ref(), &[3, 6, 9]);
1169
1170        let result = sliced.subtract(3);
1171
1172        assert_eq!(
1173            result.as_ref(),
1174            &[0, 3, 6],
1175            "shifted result should contain only the sliced data"
1176        );
1177        assert_eq!(result.len(), 3);
1178
1179        // Assert that the underlying buffer of the result is not sliced to make sure it does not include the data outside the slice range from the original buffer
1180        let underlying_buffer = result.inner().inner();
1181        assert_eq!(underlying_buffer.ptr_offset(), 0);
1182        assert_eq!(underlying_buffer.len(), 3 * std::mem::size_of::<i32>());
1183    }
1184}