Skip to main content

arrow_array/array/
byte_view_array.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::array::print_long_array;
6use crate::builder::{ArrayBuilder, GenericByteViewBuilder};
7use crate::iterator::ArrayIter;
8use crate::types::bytes::ByteArrayNativeType;
9use crate::types::{BinaryViewType, ByteViewType, StringViewType};
10use crate::{Array, ArrayAccessor, ArrayRef, GenericByteArray, OffsetSizeTrait, Scalar};
11use arrow_buffer::{ArrowNativeType, Buffer, NullBuffer, ScalarBuffer};
12use arrow_data::{ArrayData, ArrayDataBuilder, ByteView, MAX_INLINE_VIEW_LEN};
13use arrow_schema::{ArrowError, DataType};
14use core::str;
15use num_traits::ToPrimitive;
16use std::any::Any;
17use std::cmp::Ordering;
18use std::fmt::Debug;
19use std::marker::PhantomData;
20use std::sync::Arc;
21
22use super::ByteArrayType;
23
24/// [Variable-size Binary View Layout]: An array of variable length bytes views.
25///
26/// This array type is used to store variable length byte data (e.g. Strings, Binary)
27/// and has efficient operations such as `take`, `filter`, and comparison.
28///
29/// [Variable-size Binary View Layout]: https://arrow.apache.org/docs/format/Columnar.html#variable-size-binary-view-layout
30///
31/// This is different from [`GenericByteArray`], which also stores variable
32/// length byte data, as it represents strings with an offset and length. `take`
33/// and `filter` like operations are implemented by manipulating the "views"
34/// (`u128`) without modifying the bytes. Each view also stores an inlined
35/// prefix which speed up comparisons.
36///
37/// # See Also
38///
39/// * [`StringViewArray`] for storing utf8 encoded string data
40/// * [`BinaryViewArray`] for storing bytes
41/// * [`ByteView`] to interpret `u128`s layout of the views.
42///
43/// [`ByteView`]: arrow_data::ByteView
44///
45/// # Layout: "views" and buffers
46///
47/// A `GenericByteViewArray` stores variable length byte strings. An array of
48/// `N` elements is stored as `N` fixed length "views" and a variable number
49/// of variable length "buffers".
50///
51/// Each view is a `u128` value whose layout is different depending on the
52/// length of the string stored at that location:
53///
54/// ```text
55///                         ┌──────┬────────────────────────┐
56///                         │length│      string value      │
57///    Strings (len <= 12)  │      │    (padded with 0)     │
58///                         └──────┴────────────────────────┘
59///                          0    31                      127
60///
61///                         ┌───────┬───────┬───────┬───────┐
62///                         │length │prefix │  buf  │offset │
63///    Strings (len > 12)   │       │       │ index │       │
64///                         └───────┴───────┴───────┴───────┘
65///                          0    31       63      95    127
66/// ```
67///
68/// * Strings with length <= 12 ([`MAX_INLINE_VIEW_LEN`]) are stored directly in
69///   the view. See [`Self::inline_value`] to access the inlined prefix from a
70///   short view.
71///
72/// * Strings with length > 12: The first four bytes are stored inline in the
73///   view and the entire string is stored in one of the buffers. See [`ByteView`]
74///   to access the fields of the these views.
75///
76/// As with other arrays, the optimized kernels in [`arrow_compute`] are likely
77/// the easiest and fastest way to work with this data. However, it is possible
78/// to access the views and buffers directly for more control.
79///
80/// For example
81///
82/// ```rust
83/// # use arrow_array::StringViewArray;
84/// # use arrow_array::Array;
85/// use arrow_data::ByteView;
86/// let array = StringViewArray::from(vec![
87///   "hello",
88///   "this string is longer than 12 bytes",
89///   "this string is also longer than 12 bytes"
90/// ]);
91///
92/// // ** Examine the first view (short string) **
93/// assert!(array.is_valid(0)); // Check for nulls
94/// let short_view: u128 = array.views()[0]; // "hello"
95/// // get length of the string
96/// let len = short_view as u32;
97/// assert_eq!(len, 5); // strings less than 12 bytes are stored in the view
98/// // SAFETY: `view` is a valid view
99/// let value = unsafe {
100///   StringViewArray::inline_value(&short_view, len as usize)
101/// };
102/// assert_eq!(value, b"hello");
103///
104/// // ** Examine the third view (long string) **
105/// assert!(array.is_valid(12)); // Check for nulls
106/// let long_view: u128 = array.views()[2]; // "this string is also longer than 12 bytes"
107/// let len = long_view as u32;
108/// assert_eq!(len, 40); // strings longer than 12 bytes are stored in the buffer
109/// let view = ByteView::from(long_view); // use ByteView to access the fields
110/// assert_eq!(view.length, 40);
111/// assert_eq!(view.buffer_index, 0);
112/// assert_eq!(view.offset, 35); // data starts after the first long string
113/// // Views for long strings store a 4 byte prefix
114/// let prefix = view.prefix.to_le_bytes();
115/// assert_eq!(&prefix, b"this");
116/// let value = array.value(2); // get the string value (see `value` implementation for how to access the bytes directly)
117/// assert_eq!(value, "this string is also longer than 12 bytes");
118/// ```
119///
120/// [`MAX_INLINE_VIEW_LEN`]: arrow_data::MAX_INLINE_VIEW_LEN
121/// [`arrow_compute`]: https://docs.rs/arrow/latest/arrow/compute/index.html
122///
123/// Unlike [`GenericByteArray`], there are no constraints on the offsets other
124/// than they must point into a valid buffer. However, they can be out of order,
125/// non continuous and overlapping.
126///
127/// For example, in the following diagram, the strings "FishWasInTownToday" and
128/// "CrumpleFacedFish" are both longer than 12 bytes and thus are stored in a
129/// separate buffer while the string "LavaMonster" is stored inlined in the
130/// view. In this case, the same bytes for "Fish" are used to store both strings.
131///
132/// [`ByteView`]: arrow_data::ByteView
133///
134/// ```text
135///                                                                            ┌───┐
136///                         ┌──────┬──────┬──────┬──────┐               offset │...│
137/// "FishWasInTownTodayYay" │  21  │ Fish │  0   │ 115  │─ ─              103  │Mr.│
138///                         └──────┴──────┴──────┴──────┘   │      ┌ ─ ─ ─ ─ ▶ │Cru│
139///                         ┌──────┬──────┬──────┬──────┐                      │mpl│
140/// "CrumpleFacedFish"      │  16  │ Crum │  0   │ 103  │─ ─│─ ─ ─ ┘           │eFa│
141///                         └──────┴──────┴──────┴──────┘                      │ced│
142///                         ┌──────┬────────────────────┐   └ ─ ─ ─ ─ ─ ─ ─ ─ ▶│Fis│
143/// "LavaMonster"           │  11  │   LavaMonster      │                      │hWa│
144///                         └──────┴────────────────────┘               offset │sIn│
145///                                                                       115  │Tow│
146///                                                                            │nTo│
147///                                                                            │day│
148///                                  u128 "views"                              │Yay│
149///                                                                   buffer 0 │...│
150///                                                                            └───┘
151/// ```
152pub struct GenericByteViewArray<T: ByteViewType + ?Sized> {
153    data_type: DataType,
154    views: ScalarBuffer<u128>,
155    buffers: Arc<[Buffer]>,
156    phantom: PhantomData<T>,
157    nulls: Option<NullBuffer>,
158}
159
160impl<T: ByteViewType + ?Sized> Clone for GenericByteViewArray<T> {
161    fn clone(&self) -> Self {
162        Self {
163            data_type: T::DATA_TYPE,
164            views: self.views.clone(),
165            buffers: self.buffers.clone(),
166            nulls: self.nulls.clone(),
167            phantom: Default::default(),
168        }
169    }
170}
171
172impl<T: ByteViewType + ?Sized> GenericByteViewArray<T> {
173    /// Create a new [`GenericByteViewArray`] from the provided parts, panicking on failure
174    ///
175    /// # Panics
176    ///
177    /// Panics if [`GenericByteViewArray::try_new`] returns an error
178    pub fn new<U>(views: ScalarBuffer<u128>, buffers: U, nulls: Option<NullBuffer>) -> Self
179    where
180        U: Into<Arc<[Buffer]>>,
181    {
182        Self::try_new(views, buffers, nulls).unwrap()
183    }
184
185    /// Create a new [`GenericByteViewArray`] from the provided parts, returning an error on failure
186    ///
187    /// # Errors
188    ///
189    /// * `views.len() != nulls.len()`
190    /// * [ByteViewType::validate] fails
191    pub fn try_new<U>(
192        views: ScalarBuffer<u128>,
193        buffers: U,
194        nulls: Option<NullBuffer>,
195    ) -> Result<Self, ArrowError>
196    where
197        U: Into<Arc<[Buffer]>>,
198    {
199        let buffers: Arc<[Buffer]> = buffers.into();
200
201        T::validate(&views, &buffers)?;
202
203        if let Some(n) = nulls.as_ref()
204            && n.len() != views.len()
205        {
206            return Err(ArrowError::InvalidArgumentError(format!(
207                "Incorrect length of null buffer for {}ViewArray, expected {} got {}",
208                T::PREFIX,
209                views.len(),
210                n.len(),
211            )));
212        }
213
214        Ok(Self {
215            data_type: T::DATA_TYPE,
216            views,
217            buffers,
218            nulls,
219            phantom: Default::default(),
220        })
221    }
222
223    /// Create a new [`GenericByteViewArray`] from the provided parts, without validation
224    ///
225    /// # Safety
226    ///
227    /// Safe if [`Self::try_new`] would not error
228    pub unsafe fn new_unchecked(
229        views: ScalarBuffer<u128>,
230        buffers: Arc<[Buffer]>,
231        nulls: Option<NullBuffer>,
232    ) -> Self {
233        if cfg!(feature = "force_validate") {
234            return Self::new(views, buffers, nulls);
235        }
236
237        Self {
238            data_type: T::DATA_TYPE,
239            phantom: Default::default(),
240            views,
241            buffers,
242            nulls,
243        }
244    }
245
246    /// Create a new [`GenericByteViewArray`] of length `len` where all values are null
247    pub fn new_null(len: usize) -> Self {
248        Self {
249            data_type: T::DATA_TYPE,
250            views: vec![0; len].into(),
251            buffers: vec![].into(),
252            nulls: Some(NullBuffer::new_null(len)),
253            phantom: Default::default(),
254        }
255    }
256
257    /// Create a new [`Scalar`] from `value`
258    pub fn new_scalar(value: impl AsRef<T::Native>) -> Scalar<Self> {
259        Scalar::new(Self::from_iter_values(std::iter::once(value)))
260    }
261
262    /// Creates a [`GenericByteViewArray`] based on an iterator of values without nulls
263    pub fn from_iter_values<Ptr, I>(iter: I) -> Self
264    where
265        Ptr: AsRef<T::Native>,
266        I: IntoIterator<Item = Ptr>,
267    {
268        let iter = iter.into_iter();
269        let mut builder = GenericByteViewBuilder::<T>::with_capacity(iter.size_hint().0);
270        for v in iter {
271            builder.append_value(v);
272        }
273        builder.finish()
274    }
275
276    /// Deconstruct this array into its constituent parts
277    pub fn into_parts(self) -> (ScalarBuffer<u128>, Arc<[Buffer]>, Option<NullBuffer>) {
278        (self.views, self.buffers, self.nulls)
279    }
280
281    /// Returns the views buffer
282    #[inline]
283    pub fn views(&self) -> &ScalarBuffer<u128> {
284        &self.views
285    }
286
287    /// Returns the shared collection of buffers storing non-inline string or binary data.
288    ///
289    /// The returned `Arc` can be cloned to share the buffers with another array without
290    /// allocating a new collection or cloning the individual buffers. To consume this
291    /// array and take ownership of its buffers, use [`Self::into_parts`].
292    #[inline]
293    pub fn data_buffers(&self) -> &Arc<[Buffer]> {
294        &self.buffers
295    }
296
297    /// Returns the element at index `i`
298    ///
299    /// Note: This method does not check for nulls and the value is arbitrary
300    /// (but still well-defined) if [`is_null`](Self::is_null) returns true for the index.
301    ///
302    /// # Panics
303    /// Panics if index `i` is out of bounds.
304    pub fn value(&self, i: usize) -> &T::Native {
305        assert!(
306            i < self.len(),
307            "Trying to access an element at index {} from a {}ViewArray of length {}",
308            i,
309            T::PREFIX,
310            self.len()
311        );
312
313        unsafe { self.value_unchecked(i) }
314    }
315
316    /// Returns the element at index `i` without bounds checking
317    ///
318    /// Note: This method does not check for nulls and the value is arbitrary
319    /// if [`is_null`](Self::is_null) returns true for the index.
320    ///
321    /// # Safety
322    ///
323    /// Caller is responsible for ensuring that the index is within the bounds
324    /// of the array
325    pub unsafe fn value_unchecked(&self, idx: usize) -> &T::Native {
326        let v = unsafe { self.views.get_unchecked(idx) };
327        let len = *v as u32;
328        let b = if len <= MAX_INLINE_VIEW_LEN {
329            unsafe { Self::inline_value(v, len as usize) }
330        } else {
331            let view = ByteView::from(*v);
332            let data = unsafe { self.buffers.get_unchecked(view.buffer_index as usize) };
333            let offset = view.offset as usize;
334            unsafe { data.get_unchecked(offset..offset + len as usize) }
335        };
336        unsafe { T::Native::from_bytes_unchecked(b) }
337    }
338
339    /// Returns the first `len` bytes the inline value of the view.
340    ///
341    /// # Safety
342    /// - The `view` must be a valid element from `Self::views()` that adheres to the view layout.
343    /// - The `len` must be the length of the inlined value. It should never be larger than [`MAX_INLINE_VIEW_LEN`].
344    #[inline(always)]
345    pub unsafe fn inline_value(view: &u128, len: usize) -> &[u8] {
346        debug_assert!(len <= MAX_INLINE_VIEW_LEN as usize);
347        unsafe {
348            std::slice::from_raw_parts(
349                std::ptr::from_ref::<u128>(view)
350                    .cast::<u8>()
351                    .wrapping_add(4),
352                len,
353            )
354        }
355    }
356
357    /// Constructs a new iterator for iterating over the values of this array
358    pub fn iter(&self) -> ArrayIter<&Self> {
359        ArrayIter::new(self)
360    }
361
362    /// Returns an iterator over the bytes of this array, including null values
363    pub fn bytes_iter(&self) -> impl Iterator<Item = &[u8]> {
364        self.views.iter().map(move |v| {
365            let len = *v as u32;
366            if len <= MAX_INLINE_VIEW_LEN {
367                unsafe { Self::inline_value(v, len as usize) }
368            } else {
369                let view = ByteView::from(*v);
370                let data = &self.buffers[view.buffer_index as usize];
371                let offset = view.offset as usize;
372                unsafe { data.get_unchecked(offset..offset + len as usize) }
373            }
374        })
375    }
376
377    /// Returns an iterator over the first `prefix_len` bytes of each array
378    /// element, including null values.
379    ///
380    /// If `prefix_len` is larger than the element's length, the iterator will
381    /// return an empty slice (`&[]`).
382    pub fn prefix_bytes_iter(&self, prefix_len: usize) -> impl Iterator<Item = &[u8]> {
383        self.views().into_iter().map(move |v| {
384            let len = (*v as u32) as usize;
385
386            if len < prefix_len {
387                return &[] as &[u8];
388            }
389
390            if prefix_len <= 4 || len as u32 <= MAX_INLINE_VIEW_LEN {
391                unsafe { StringViewArray::inline_value(v, prefix_len) }
392            } else {
393                let view = ByteView::from(*v);
394                let data = unsafe {
395                    self.data_buffers()
396                        .get_unchecked(view.buffer_index as usize)
397                };
398                let offset = view.offset as usize;
399                unsafe { data.get_unchecked(offset..offset + prefix_len) }
400            }
401        })
402    }
403
404    /// Returns an iterator over the last `suffix_len` bytes of each array
405    /// element, including null values.
406    ///
407    /// Note that for [`StringViewArray`] the last bytes may start in the middle
408    /// of a UTF-8 codepoint, and thus may not be a valid `&str`.
409    ///
410    /// If `suffix_len` is larger than the element's length, the iterator will
411    /// return an empty slice (`&[]`).
412    pub fn suffix_bytes_iter(&self, suffix_len: usize) -> impl Iterator<Item = &[u8]> {
413        self.views().into_iter().map(move |v| {
414            let len = (*v as u32) as usize;
415
416            if len < suffix_len {
417                return &[] as &[u8];
418            }
419
420            if len as u32 <= MAX_INLINE_VIEW_LEN {
421                unsafe { &StringViewArray::inline_value(v, len)[len - suffix_len..] }
422            } else {
423                let view = ByteView::from(*v);
424                let data = unsafe {
425                    self.data_buffers()
426                        .get_unchecked(view.buffer_index as usize)
427                };
428                let offset = view.offset as usize;
429                unsafe { data.get_unchecked(offset + len - suffix_len..offset + len) }
430            }
431        })
432    }
433
434    /// Return an iterator over the length of each array element, including null values.
435    ///
436    /// Null values length would equal to the underlying bytes length and NOT 0
437    ///
438    /// Example of getting 0 for null values
439    /// ```rust
440    /// # use arrow_array::StringViewArray;
441    /// # use arrow_array::Array;
442    /// use arrow_data::ByteView;
443    ///
444    /// fn lengths_with_zero_for_nulls(view: &StringViewArray) -> impl Iterator<Item = u32> {
445    ///     view.lengths()
446    ///         .enumerate()
447    ///         .map(|(index, length)| if view.is_null(index) { 0 } else { length })
448    /// }
449    /// ```
450    pub fn lengths(&self) -> impl ExactSizeIterator<Item = u32> + Clone {
451        self.views().iter().map(|v| *v as u32)
452    }
453
454    /// Returns a zero-copy slice of this array with the indicated offset and length.
455    ///
456    /// # Panics
457    /// Panics if `offset + length > self.len()`
458    pub fn slice(&self, offset: usize, length: usize) -> Self {
459        Self {
460            data_type: T::DATA_TYPE,
461            views: self.views.slice(offset, length),
462            buffers: self.buffers.clone(),
463            nulls: self.nulls.as_ref().map(|n| n.slice(offset, length)),
464            phantom: Default::default(),
465        }
466    }
467
468    /// Returns a "compacted" version of this array
469    ///
470    /// The original array will *not* be modified
471    ///
472    /// # Garbage Collection
473    ///
474    /// Before GC:
475    /// ```text
476    ///                                        ┌──────┐
477    ///                                        │......│
478    ///                                        │......│
479    /// ┌────────────────────┐       ┌ ─ ─ ─ ▶ │Data1 │   Large buffer
480    /// │       View 1       │─ ─ ─ ─          │......│  with data that
481    /// ├────────────────────┤                 │......│ is not referred
482    /// │       View 2       │─ ─ ─ ─ ─ ─ ─ ─▶ │Data2 │ to by View 1 or
483    /// └────────────────────┘                 │......│      View 2
484    ///                                        │......│
485    ///    2 views, refer to                   │......│
486    ///   small portions of a                  └──────┘
487    ///      large buffer
488    /// ```
489    ///
490    /// After GC:
491    ///
492    /// ```text
493    /// ┌────────────────────┐                 ┌─────┐    After gc, only
494    /// │       View 1       │─ ─ ─ ─ ─ ─ ─ ─▶ │Data1│     data that is
495    /// ├────────────────────┤       ┌ ─ ─ ─ ▶ │Data2│    pointed to by
496    /// │       View 2       │─ ─ ─ ─          └─────┘     the views is
497    /// └────────────────────┘                                 left
498    ///
499    ///
500    ///         2 views
501    /// ```
502    /// This method will compact the data buffers by recreating the view array and only include the data
503    /// that is pointed to by the views.
504    ///
505    /// Note that it will copy the array regardless of whether the original array is compact.
506    /// Use with caution as this can be an expensive operation, only use it when you are sure that the view
507    /// array is significantly smaller than when it is originally created, e.g., after filtering or slicing.
508    ///
509    /// Note: this function does not attempt to canonicalize / deduplicate values. For this
510    /// feature see  [`GenericByteViewBuilder::with_deduplicate_strings`].
511    pub fn gc(&self) -> Self {
512        // A single Arrow data buffer is addressed by a `u32` offset, so no
513        // output buffer may exceed `i32::MAX` bytes. Above that the data is
514        // split across multiple buffers (the "slow path").
515        self.gc_with_max_buffer_size(i32::MAX as usize)
516    }
517
518    /// Like [`Self::gc`], but with a configurable maximum output buffer size.
519    ///
520    /// `gc` always uses `i32::MAX` (the largest a single Arrow buffer can be).
521    /// This method exists so the multi-buffer split path — which `gc` only
522    /// reaches once a column references more than 2 GiB of non-inline data —
523    /// can be exercised in tests with a small threshold and tiny inputs.
524    fn gc_with_max_buffer_size(&self, max_buffer_size: usize) -> Self {
525        // 1) Read basic properties once
526        let len = self.len(); // number of elements
527        let nulls = self.nulls().cloned(); // reuse & clone existing null bitmap
528
529        // 1.5) Fast path: if there are no buffers, just reuse original views and no data blocks
530        if self.data_buffers().is_empty() {
531            return unsafe {
532                GenericByteViewArray::new_unchecked(
533                    self.views().clone(),
534                    Arc::from([]), // empty data blocks
535                    nulls,
536                )
537            };
538        }
539
540        // 2) Calculate total size of all non-inline data and detect if any exists
541        let total_large = self.total_buffer_bytes_used();
542
543        // 2.5) Fast path: if there is no non-inline data, avoid buffer allocation & processing
544        if total_large == 0 {
545            // Views are inline-only or all null; just reuse original views and no data blocks
546            return unsafe {
547                GenericByteViewArray::new_unchecked(
548                    self.views().clone(),
549                    Arc::from([]), // empty data blocks
550                    nulls,
551                )
552            };
553        }
554
555        let (views_buf, data_blocks) = if total_large <= max_buffer_size {
556            // fast path, the entire data fits in a single buffer
557            // 3) Allocate exactly capacity for all non-inline data
558            let mut data_buf = Vec::with_capacity(total_large);
559
560            // 4) Iterate over views and process each inline/non-inline view
561            let views_buf: Vec<u128> = (0..len)
562                .map(|i| unsafe { self.copy_view_to_buffer(i, 0, &mut data_buf) })
563                .collect();
564            let data_block = Buffer::from_vec(data_buf);
565            let data_blocks = vec![data_block];
566            (views_buf, data_blocks)
567        } else {
568            // slow path: the non-inline data does not fit in a single buffer
569            // (a buffer offset is a `u32`, so no buffer may exceed `i32::MAX`),
570            // so it must be split across several buffers.
571
572            // A contiguous run of views destined for one output buffer.
573            struct GcCopyGroup {
574                total_buffer_bytes: usize,
575                total_len: usize,
576            }
577
578            // First pass: partition the views into contiguous groups, each of
579            // which fits within one output buffer. `total_len` counts *all*
580            // views in the group, not just the non-inline ones — this is
581            // essential for correctness, as gc must preserve the row count.
582            // Inline views reference no buffer and are copied unchanged, but
583            // they still belong to a group and must be accounted for. Recording
584            // each group's exact byte size lets the second pass size every
585            // buffer precisely, with no over-reservation or trailing waste.
586            let mut groups: Vec<GcCopyGroup> = Vec::new();
587            // Bytes accumulated for the current group. A view length is a `u32`
588            // and a single value may be larger than `max_buffer_size`, so the
589            // running sum is kept in `u64` to stay within the addressable
590            // buffer-offset space without wrapping.
591            let mut current_length: u64 = 0;
592            let mut current_elements = 0;
593
594            for view in self.views() {
595                let view_len = *view as u32;
596                if view_len > MAX_INLINE_VIEW_LEN {
597                    // Seal the current group before adding this view would push
598                    // its buffer past `max_buffer_size`.
599                    if current_length + view_len as u64 > max_buffer_size as u64 {
600                        groups.push(GcCopyGroup {
601                            total_buffer_bytes: current_length as usize,
602                            total_len: current_elements,
603                        });
604                        current_length = 0;
605                        current_elements = 0;
606                    }
607                    current_length += view_len as u64;
608                }
609                current_elements += 1;
610            }
611            if current_elements != 0 {
612                groups.push(GcCopyGroup {
613                    total_buffer_bytes: current_length as usize,
614                    total_len: current_elements,
615                });
616            }
617            debug_assert!(i32::try_from(groups.len()).is_ok());
618
619            // Second pass: copy each group into an exactly-sized buffer.
620            let mut views_buf = Vec::with_capacity(len);
621            let mut data_blocks = Vec::with_capacity(groups.len());
622            let mut current_view_idx = 0;
623
624            for (group_idx, group) in groups.iter().enumerate() {
625                let mut data_buf = Vec::with_capacity(group.total_buffer_bytes);
626
627                // Directly push views to avoid an intermediate Vec allocation.
628                let new_views =
629                    (current_view_idx..current_view_idx + group.total_len).map(|view_idx| {
630                        // SAFETY: `view_idx` came from iterating a valid range
631                        // within `len`, and every view refers to valid data.
632                        unsafe {
633                            self.copy_view_to_buffer(view_idx, group_idx as i32, &mut data_buf)
634                        }
635                    });
636                views_buf.extend(new_views);
637
638                data_blocks.push(Buffer::from_vec(data_buf));
639                current_view_idx += group.total_len;
640            }
641            (views_buf, data_blocks)
642        };
643
644        // 5) Wrap up views buffer
645        let views_scalar = ScalarBuffer::from(views_buf);
646
647        // SAFETY: views_scalar, data_blocks, and nulls are correctly aligned and sized
648        unsafe { GenericByteViewArray::new_unchecked(views_scalar, data_blocks.into(), nulls) }
649    }
650
651    /// Copy the i‑th view into `data_buf` if it refers to an out‑of‑line buffer.
652    ///
653    /// # Safety
654    ///
655    /// - `i < self.len()`.
656    /// - Every element in `self.views()` must currently refer to a valid slice
657    ///   inside one of `self.buffers`.
658    /// - `data_buf` must be ready to have additional bytes appended.
659    /// - After this call, the returned view will have its
660    ///   `buffer_index` reset to `buffer_idx` and its `offset` updated so that it points
661    ///   into the bytes just appended at the end of `data_buf`.
662    #[inline(always)]
663    unsafe fn copy_view_to_buffer(
664        &self,
665        i: usize,
666        buffer_idx: i32,
667        data_buf: &mut Vec<u8>,
668    ) -> u128 {
669        // SAFETY: `i < self.len()` ensures this is in‑bounds.
670        let raw_view = unsafe { *self.views().get_unchecked(i) };
671        let mut bv = ByteView::from(raw_view);
672
673        // Inline‑small views stay as‑is.
674        if bv.length <= MAX_INLINE_VIEW_LEN {
675            raw_view
676        } else {
677            // SAFETY: `bv.buffer_index` and `bv.offset..bv.offset+bv.length`
678            // must both lie within valid ranges for `self.buffers`.
679            let buffer = unsafe { self.buffers.get_unchecked(bv.buffer_index as usize) };
680            let start = bv.offset as usize;
681            let end = start + bv.length as usize;
682            let slice = unsafe { buffer.get_unchecked(start..end) };
683
684            // Copy out‑of‑line data into our single “0” buffer.
685            let new_offset = data_buf.len() as u32;
686            data_buf.extend_from_slice(slice);
687
688            bv.buffer_index = buffer_idx as u32;
689            bv.offset = new_offset;
690            bv.into()
691        }
692    }
693
694    /// Returns the total number of bytes of all non-null values in this array.
695    ///
696    /// Unlike [`Self::total_buffer_bytes_used`], this method includes inlined strings
697    /// (those with length ≤ [`MAX_INLINE_VIEW_LEN`]), making it suitable as a
698    /// capacity hint when pre-allocating output buffers.
699    ///
700    /// Null values are excluded from the sum.
701    ///
702    /// # Example
703    ///
704    /// ```rust
705    /// # use arrow_array::StringViewArray;
706    /// let array = StringViewArray::from_iter(vec![
707    ///     Some("hello"),   // 5 bytes, inlined
708    ///     None,            // excluded
709    ///     Some("large payload over 12 bytes"),  // 27 bytes, non-inlined
710    /// ]);
711    /// assert_eq!(array.total_bytes_len(), 5 + 27);
712    /// ```
713    pub fn total_bytes_len(&self) -> usize {
714        match self.nulls() {
715            None => self.views().iter().map(|v| (*v as u32) as usize).sum(),
716            Some(nulls) => self
717                .views()
718                .iter()
719                .zip(nulls.iter())
720                .map(|(v, is_valid)| if is_valid { (*v as u32) as usize } else { 0 })
721                .sum(),
722        }
723    }
724
725    /// Returns the total number of bytes used by all non inlined views in all
726    /// buffers.
727    ///
728    /// Note this does not account for views that point at the same underlying
729    /// data in buffers
730    ///
731    /// For example, if the array has three strings views:
732    /// * View with length = 9 (inlined)
733    /// * View with length = 32 (non inlined)
734    /// * View with length = 16 (non inlined)
735    ///
736    /// Then this method would report 48
737    pub fn total_buffer_bytes_used(&self) -> usize {
738        self.views()
739            .iter()
740            .map(|v| {
741                let len = *v as u32;
742                if len > MAX_INLINE_VIEW_LEN {
743                    len as usize
744                } else {
745                    0
746                }
747            })
748            .sum()
749    }
750
751    /// Compare two [`GenericByteViewArray`] at index `left_idx` and `right_idx`
752    ///
753    /// Comparing two ByteView types are non-trivial.
754    /// It takes a bit of patience to understand why we don't just compare two &[u8] directly.
755    ///
756    /// ByteView types give us the following two advantages, and we need to be careful not to lose them:
757    /// (1) For string/byte smaller than [`MAX_INLINE_VIEW_LEN`] bytes, the entire data is inlined in the view.
758    ///     Meaning that reading one array element requires only one memory access
759    ///     (two memory access required for StringArray, one for offset buffer, the other for value buffer).
760    ///
761    /// (2) For string/byte larger than [`MAX_INLINE_VIEW_LEN`] bytes, we can still be faster than (for certain operations) StringArray/ByteArray,
762    ///     thanks to the inlined 4 bytes.
763    ///     Consider equality check:
764    ///     If the first four bytes of the two strings are different, we can return false immediately (with just one memory access).
765    ///
766    /// If we directly compare two &[u8], we materialize the entire string (i.e., make multiple memory accesses), which might be unnecessary.
767    /// - Most of the time (eq, ord), we only need to look at the first 4 bytes to know the answer,
768    ///   e.g., if the inlined 4 bytes are different, we can directly return unequal without looking at the full string.
769    ///
770    /// # Order check flow
771    /// (1) if both string are smaller than [`MAX_INLINE_VIEW_LEN`] bytes, we can directly compare the data inlined to the view.
772    /// (2) if any of the string is larger than [`MAX_INLINE_VIEW_LEN`] bytes, we need to compare the full string.
773    ///     (2.1) if the inlined 4 bytes are different, we can return the result immediately.
774    ///     (2.2) o.w., we need to compare the full string.
775    ///
776    /// # Safety
777    /// The left/right_idx must within range of each array
778    pub unsafe fn compare_unchecked(
779        left: &GenericByteViewArray<T>,
780        left_idx: usize,
781        right: &GenericByteViewArray<T>,
782        right_idx: usize,
783    ) -> Ordering {
784        let l_view = unsafe { left.views().get_unchecked(left_idx) };
785        let l_byte_view = ByteView::from(*l_view);
786
787        let r_view = unsafe { right.views().get_unchecked(right_idx) };
788        let r_byte_view = ByteView::from(*r_view);
789
790        let l_len = l_byte_view.length;
791        let r_len = r_byte_view.length;
792
793        if l_len <= 12 && r_len <= 12 {
794            return Self::inline_key_fast(*l_view).cmp(&Self::inline_key_fast(*r_view));
795        }
796
797        // one of the string is larger than 12 bytes,
798        // we then try to compare the inlined data first
799
800        // Note: In theory, ByteView is only used for string which is larger than 12 bytes,
801        // but we can still use it to get the inlined prefix for shorter strings.
802        // The prefix is always the first 4 bytes of the view, for both short and long strings.
803        let l_inlined_be = l_byte_view.prefix.swap_bytes();
804        let r_inlined_be = r_byte_view.prefix.swap_bytes();
805        if l_inlined_be != r_inlined_be {
806            return l_inlined_be.cmp(&r_inlined_be);
807        }
808
809        // unfortunately, we need to compare the full data
810        let l_full_data: &[u8] = unsafe { left.value_unchecked(left_idx).as_ref() };
811        let r_full_data: &[u8] = unsafe { right.value_unchecked(right_idx).as_ref() };
812
813        l_full_data.cmp(r_full_data)
814    }
815
816    /// Builds a 128-bit composite key for an inline value:
817    ///
818    /// - High 96 bits: the inline data in big-endian byte order (for correct lexicographical sorting).
819    /// - Low  32 bits: the length in big-endian byte order, acting as a tiebreaker so shorter strings
820    ///   (or those with fewer meaningful bytes) always numerically sort before longer ones.
821    ///
822    /// This function extracts the length and the 12-byte inline string data from the raw
823    /// little-endian `u128` representation, converts them to big-endian ordering, and packs them
824    /// into a single `u128` value suitable for fast, branchless comparisons.
825    ///
826    /// # Why include length?
827    ///
828    /// A pure 96-bit content comparison can’t distinguish between two values whose inline bytes
829    /// compare equal—either because one is a true prefix of the other or because zero-padding
830    /// hides extra bytes. By tucking the 32-bit length into the lower bits, a single `u128` compare
831    /// handles both content and length in one go.
832    ///
833    /// Example: comparing "bar" (3 bytes) vs "bar\0" (4 bytes)
834    ///
835    /// | String     | Bytes 0–4 (length LE) | Bytes 4–16 (data + padding)    |
836    /// |------------|-----------------------|---------------------------------|
837    /// | `"bar"`   | `03 00 00 00`         | `62 61 72` + 9 × `00`           |
838    /// | `"bar\0"`| `04 00 00 00`         | `62 61 72 00` + 8 × `00`        |
839    ///
840    /// Both inline parts become `62 61 72 00…00`, so they tie on content. The length field
841    /// then differentiates:
842    ///
843    /// ```text
844    /// key("bar")   = 0x0000000000000000000062617200000003
845    /// key("bar\0") = 0x0000000000000000000062617200000004
846    /// ⇒ key("bar") < key("bar\0")
847    /// ```
848    /// - `raw` is treated as a 128-bit integer with its bits laid out as follows:
849    ///   - bits 0–31: length (little-endian)
850    ///   - bits 32–127: data (little-endian)
851    ///
852    /// # Inlining and Endianness
853    ///
854    /// This function uses platform-independent bitwise operations to construct a 128-bit key:
855    /// - `raw.swap_bytes() << 32` effectively clears the length bits and shifts the 12-byte inline data
856    ///   into the high 96 bits in Big-Endian order. This ensures the first byte of the string
857    ///   is the most significant byte of the resulting `u128`.
858    /// - `raw as u32` extracts the length as a numeric integer, which is then placed in the low 32 bits.
859    #[inline(always)]
860    pub fn inline_key_fast(raw: u128) -> u128 {
861        (raw.swap_bytes() << 32) | (raw as u32 as u128)
862    }
863}
864
865impl<T: ByteViewType + ?Sized> Debug for GenericByteViewArray<T> {
866    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
867        write!(f, "{}ViewArray\n[\n", T::PREFIX)?;
868        print_long_array(self, f, &mut |index, f| {
869            std::fmt::Debug::fmt(&self.value(index), f)
870        })?;
871        write!(f, "]")
872    }
873}
874
875/// SAFETY: Correctly implements the contract of Arrow Arrays
876unsafe impl<T: ByteViewType + ?Sized> Array for GenericByteViewArray<T> {
877    fn as_any(&self) -> &dyn Any {
878        self
879    }
880
881    fn to_arc(&self) -> ArrayRef {
882        Arc::new(self.clone())
883    }
884
885    fn to_data(&self) -> ArrayData {
886        self.clone().into()
887    }
888
889    fn into_data(self) -> ArrayData {
890        self.into()
891    }
892
893    fn data_type(&self) -> &DataType {
894        &self.data_type
895    }
896
897    fn slice(&self, offset: usize, length: usize) -> ArrayRef {
898        Arc::new(self.slice(offset, length))
899    }
900
901    fn len(&self) -> usize {
902        self.views.len()
903    }
904
905    fn is_empty(&self) -> bool {
906        self.views.is_empty()
907    }
908
909    fn shrink_to_fit(&mut self) {
910        self.views.shrink_to_fit();
911
912        // The goal of `shrink_to_fit` is to minimize the space used by any of
913        // its allocations. The use of `Arc::get_mut` over `Arc::make_mut` is
914        // because if the reference count is greater than 1, `Arc::make_mut`
915        // will first clone its contents. So, any large allocations will first
916        // be cloned before being shrunk, leaving the pre-cloned allocations
917        // intact, before adding the extra (used) space of the new clones.
918        if let Some(buffers) = Arc::get_mut(&mut self.buffers) {
919            buffers.iter_mut().for_each(|b| b.shrink_to_fit());
920        }
921
922        // With the assumption that this is a best-effort function, no attempt
923        // is made to shrink `self.buffers`, which it can't because it's type
924        // does not expose a `shrink_to_fit` method.
925
926        if let Some(nulls) = &mut self.nulls {
927            nulls.shrink_to_fit();
928        }
929    }
930
931    fn offset(&self) -> usize {
932        0
933    }
934
935    fn nulls(&self) -> Option<&NullBuffer> {
936        self.nulls.as_ref()
937    }
938
939    fn logical_null_count(&self) -> usize {
940        // More efficient that the default implementation
941        self.null_count()
942    }
943
944    fn get_buffer_memory_size(&self) -> usize {
945        let mut sum = self.buffers.iter().map(|b| b.capacity()).sum::<usize>();
946        sum += self.views.inner().capacity();
947        if let Some(x) = &self.nulls {
948            sum += x.buffer().capacity()
949        }
950        sum
951    }
952
953    fn get_array_memory_size(&self) -> usize {
954        std::mem::size_of::<Self>() + self.get_buffer_memory_size()
955    }
956
957    #[cfg(feature = "pool")]
958    fn claim(&self, pool: &dyn arrow_buffer::MemoryPool) {
959        self.views.claim(pool);
960        for buffer in self.buffers.iter() {
961            buffer.claim(pool);
962        }
963        if let Some(nulls) = &self.nulls {
964            nulls.claim(pool);
965        }
966    }
967}
968
969impl<'a, T: ByteViewType + ?Sized> ArrayAccessor for &'a GenericByteViewArray<T> {
970    type Item = &'a T::Native;
971
972    fn value(&self, index: usize) -> Self::Item {
973        GenericByteViewArray::value(self, index)
974    }
975
976    unsafe fn value_unchecked(&self, index: usize) -> Self::Item {
977        unsafe { GenericByteViewArray::value_unchecked(self, index) }
978    }
979}
980
981impl<'a, T: ByteViewType + ?Sized> IntoIterator for &'a GenericByteViewArray<T> {
982    type Item = Option<&'a T::Native>;
983    type IntoIter = ArrayIter<Self>;
984
985    fn into_iter(self) -> Self::IntoIter {
986        ArrayIter::new(self)
987    }
988}
989
990impl<T: ByteViewType + ?Sized> From<ArrayData> for GenericByteViewArray<T> {
991    fn from(data: ArrayData) -> Self {
992        let (data_type, len, nulls, offset, buffers, _child_data) = data.into_parts();
993        assert_eq!(
994            data_type,
995            T::DATA_TYPE,
996            "Mismatched data type, expected {}, got {data_type}",
997            T::DATA_TYPE
998        );
999        let mut buffers = buffers.into_iter();
1000        // first buffer is views, remaining are data buffers
1001        let views = ScalarBuffer::new(buffers.next().unwrap(), offset, len);
1002        Self {
1003            data_type,
1004            views,
1005            buffers: Arc::from_iter(buffers),
1006            nulls,
1007            phantom: Default::default(),
1008        }
1009    }
1010}
1011
1012/// Efficiently convert a [`GenericByteArray`] to a [`GenericByteViewArray`]
1013///
1014/// For example this method can convert a [`StringArray`] to a
1015/// [`StringViewArray`].
1016///
1017/// If the offsets are all less than u32::MAX, the new [`GenericByteViewArray`]
1018/// is built without copying the underlying string data (views are created
1019/// directly into the existing buffer)
1020///
1021/// [`StringArray`]: crate::StringArray
1022impl<FROM, V> From<&GenericByteArray<FROM>> for GenericByteViewArray<V>
1023where
1024    FROM: ByteArrayType,
1025    FROM::Offset: OffsetSizeTrait + ToPrimitive,
1026    V: ByteViewType<Native = FROM::Native>,
1027{
1028    fn from(byte_array: &GenericByteArray<FROM>) -> Self {
1029        let offsets = byte_array.offsets();
1030
1031        let can_reuse_buffer = offsets.last().as_usize() < u32::MAX as usize;
1032
1033        if can_reuse_buffer {
1034            // build views directly pointing to the existing buffer
1035            let len = byte_array.len();
1036            let mut views_builder = GenericByteViewBuilder::<V>::with_capacity(len);
1037            let str_values_buf = byte_array.values().clone();
1038            let block = views_builder.append_block(str_values_buf);
1039            for (i, w) in offsets.windows(2).enumerate() {
1040                let offset = w[0].as_usize();
1041                let end = w[1].as_usize();
1042                let length = end - offset;
1043
1044                if byte_array.is_null(i) {
1045                    views_builder.append_null();
1046                } else {
1047                    // Safety: the input was a valid array so it valid UTF8 (if string). And
1048                    // all offsets were valid
1049                    unsafe {
1050                        views_builder.append_view_unchecked(block, offset as u32, length as u32)
1051                    }
1052                }
1053            }
1054            assert_eq!(views_builder.len(), len);
1055            views_builder.finish()
1056        } else {
1057            // Otherwise, create a new buffer for large strings
1058            // TODO: the original buffer could still be used
1059            // by making multiple slices of u32::MAX length
1060            GenericByteViewArray::<V>::from_iter(byte_array.iter())
1061        }
1062    }
1063}
1064
1065impl<T: ByteViewType + ?Sized> From<GenericByteViewArray<T>> for ArrayData {
1066    fn from(array: GenericByteViewArray<T>) -> Self {
1067        let len = array.len();
1068
1069        let mut buffers = array.buffers.to_vec();
1070        buffers.insert(0, array.views.into_inner());
1071
1072        let builder = ArrayDataBuilder::new(T::DATA_TYPE)
1073            .len(len)
1074            .buffers(buffers)
1075            .nulls(array.nulls);
1076
1077        unsafe { builder.build_unchecked() }
1078    }
1079}
1080
1081impl<'a, Ptr, T> FromIterator<&'a Option<Ptr>> for GenericByteViewArray<T>
1082where
1083    Ptr: AsRef<T::Native> + 'a,
1084    T: ByteViewType + ?Sized,
1085{
1086    fn from_iter<I: IntoIterator<Item = &'a Option<Ptr>>>(iter: I) -> Self {
1087        iter.into_iter()
1088            .map(|o| o.as_ref().map(|p| p.as_ref()))
1089            .collect()
1090    }
1091}
1092
1093impl<Ptr, T: ByteViewType + ?Sized> FromIterator<Option<Ptr>> for GenericByteViewArray<T>
1094where
1095    Ptr: AsRef<T::Native>,
1096{
1097    fn from_iter<I: IntoIterator<Item = Option<Ptr>>>(iter: I) -> Self {
1098        let iter = iter.into_iter();
1099        let mut builder = GenericByteViewBuilder::<T>::with_capacity(iter.size_hint().0);
1100        builder.extend(iter);
1101        builder.finish()
1102    }
1103}
1104
1105/// A [`GenericByteViewArray`] of `[u8]`
1106///
1107/// See [`GenericByteViewArray`] for format and layout details.
1108///
1109/// # Example
1110/// ```
1111/// use arrow_array::BinaryViewArray;
1112/// let array = BinaryViewArray::from_iter_values(vec![b"hello" as &[u8], b"world", b"lulu", b"large payload over 12 bytes"]);
1113/// assert_eq!(array.value(0), b"hello");
1114/// assert_eq!(array.value(3), b"large payload over 12 bytes");
1115/// ```
1116pub type BinaryViewArray = GenericByteViewArray<BinaryViewType>;
1117
1118impl BinaryViewArray {
1119    /// Convert the [`BinaryViewArray`] to [`StringViewArray`]
1120    /// If items not utf8 data, validate will fail and error returned.
1121    pub fn to_string_view(self) -> Result<StringViewArray, ArrowError> {
1122        StringViewType::validate(self.views(), self.data_buffers())?;
1123        unsafe { Ok(self.to_string_view_unchecked()) }
1124    }
1125
1126    /// Convert the [`BinaryViewArray`] to [`StringViewArray`]
1127    /// # Safety
1128    /// Caller is responsible for ensuring that items in array are utf8 data.
1129    pub unsafe fn to_string_view_unchecked(self) -> StringViewArray {
1130        unsafe { StringViewArray::new_unchecked(self.views, self.buffers, self.nulls) }
1131    }
1132}
1133
1134impl From<Vec<&[u8]>> for BinaryViewArray {
1135    fn from(v: Vec<&[u8]>) -> Self {
1136        Self::from_iter_values(v)
1137    }
1138}
1139
1140impl From<Vec<Option<&[u8]>>> for BinaryViewArray {
1141    fn from(v: Vec<Option<&[u8]>>) -> Self {
1142        v.into_iter().collect()
1143    }
1144}
1145
1146/// A [`GenericByteViewArray`] that stores utf8 data
1147///
1148/// See [`GenericByteViewArray`] for format and layout details.
1149///
1150/// # Example
1151/// ```
1152/// use arrow_array::StringViewArray;
1153/// let array = StringViewArray::from_iter_values(vec!["hello", "world", "lulu", "large payload over 12 bytes"]);
1154/// assert_eq!(array.value(0), "hello");
1155/// assert_eq!(array.value(3), "large payload over 12 bytes");
1156/// ```
1157pub type StringViewArray = GenericByteViewArray<StringViewType>;
1158
1159impl StringViewArray {
1160    /// Convert the [`StringViewArray`] to [`BinaryViewArray`]
1161    pub fn to_binary_view(self) -> BinaryViewArray {
1162        unsafe { BinaryViewArray::new_unchecked(self.views, self.buffers, self.nulls) }
1163    }
1164
1165    /// Returns true if all data within this array is ASCII
1166    pub fn is_ascii(&self) -> bool {
1167        // Alternative (but incorrect): directly check the underlying buffers
1168        // (1) Our string view might be sparse, i.e., a subset of the buffers,
1169        //      so even if the buffer is not ascii, we can still be ascii.
1170        // (2) It is quite difficult to know the range of each buffer (unlike StringArray)
1171        // This means that this operation is quite expensive, shall we cache the result?
1172        //  i.e. track `is_ascii` in the builder.
1173        self.iter().all(|v| match v {
1174            Some(v) => v.is_ascii(),
1175            None => true,
1176        })
1177    }
1178}
1179
1180impl From<Vec<&str>> for StringViewArray {
1181    fn from(v: Vec<&str>) -> Self {
1182        Self::from_iter_values(v)
1183    }
1184}
1185
1186impl From<Vec<Option<&str>>> for StringViewArray {
1187    fn from(v: Vec<Option<&str>>) -> Self {
1188        v.into_iter().collect()
1189    }
1190}
1191
1192impl From<Vec<String>> for StringViewArray {
1193    fn from(v: Vec<String>) -> Self {
1194        Self::from_iter_values(v)
1195    }
1196}
1197
1198impl From<Vec<Option<String>>> for StringViewArray {
1199    fn from(v: Vec<Option<String>>) -> Self {
1200        v.into_iter().collect()
1201    }
1202}
1203
1204#[cfg(test)]
1205mod tests {
1206    use crate::builder::{BinaryViewBuilder, StringViewBuilder};
1207    use crate::types::BinaryViewType;
1208    use crate::{
1209        Array, BinaryViewArray, GenericBinaryArray, GenericByteViewArray, StringViewArray,
1210    };
1211    use arrow_buffer::{Buffer, NullBuffer, ScalarBuffer};
1212    use arrow_data::{ArrayDataBuilder, ByteView, MAX_INLINE_VIEW_LEN};
1213    use arrow_schema::DataType;
1214    use rand::prelude::StdRng;
1215    use rand::{RngExt, SeedableRng};
1216    use std::str::from_utf8;
1217
1218    const BLOCK_SIZE: u32 = 8;
1219
1220    #[test]
1221    fn try_new_string() {
1222        let array = StringViewArray::from_iter_values(vec![
1223            "hello",
1224            "world",
1225            "lulu",
1226            "large payload over 12 bytes",
1227        ]);
1228        assert_eq!(array.value(0), "hello");
1229        assert_eq!(array.value(3), "large payload over 12 bytes");
1230    }
1231
1232    #[test]
1233    fn try_new_binary() {
1234        let array = BinaryViewArray::from_iter_values(vec![
1235            b"hello".as_slice(),
1236            b"world".as_slice(),
1237            b"lulu".as_slice(),
1238            b"large payload over 12 bytes".as_slice(),
1239        ]);
1240        assert_eq!(array.value(0), b"hello");
1241        assert_eq!(array.value(3), b"large payload over 12 bytes");
1242    }
1243
1244    #[test]
1245    fn try_new_empty_string() {
1246        // test empty array
1247        let array = {
1248            let mut builder = StringViewBuilder::new();
1249            builder.finish()
1250        };
1251        assert!(array.is_empty());
1252    }
1253
1254    #[test]
1255    fn try_new_empty_binary() {
1256        // test empty array
1257        let array = {
1258            let mut builder = BinaryViewBuilder::new();
1259            builder.finish()
1260        };
1261        assert!(array.is_empty());
1262    }
1263
1264    #[test]
1265    fn test_append_string() {
1266        // test builder append
1267        let array = {
1268            let mut builder = StringViewBuilder::new();
1269            builder.append_value("hello");
1270            builder.append_null();
1271            builder.append_option(Some("large payload over 12 bytes"));
1272            builder.finish()
1273        };
1274        assert_eq!(array.value(0), "hello");
1275        assert!(array.is_null(1));
1276        assert_eq!(array.value(2), "large payload over 12 bytes");
1277    }
1278
1279    #[test]
1280    fn test_append_binary() {
1281        // test builder append
1282        let array = {
1283            let mut builder = BinaryViewBuilder::new();
1284            builder.append_value(b"hello");
1285            builder.append_null();
1286            builder.append_option(Some(b"large payload over 12 bytes"));
1287            builder.finish()
1288        };
1289        assert_eq!(array.value(0), b"hello");
1290        assert!(array.is_null(1));
1291        assert_eq!(array.value(2), b"large payload over 12 bytes");
1292    }
1293
1294    #[test]
1295    fn test_in_progress_recreation() {
1296        let array = {
1297            // make a builder with small block size.
1298            let mut builder = StringViewBuilder::new().with_fixed_block_size(14);
1299            builder.append_value("large payload over 12 bytes");
1300            builder.append_option(Some("another large payload over 12 bytes that double than the first one, so that we can trigger the in_progress in builder re-created"));
1301            builder.finish()
1302        };
1303        assert_eq!(array.value(0), "large payload over 12 bytes");
1304        assert_eq!(
1305            array.value(1),
1306            "another large payload over 12 bytes that double than the first one, so that we can trigger the in_progress in builder re-created"
1307        );
1308        assert_eq!(2, array.buffers.len());
1309    }
1310
1311    #[test]
1312    #[should_panic(expected = "Invalid buffer index at 0: got index 3 but only has 1 buffers")]
1313    fn new_with_invalid_view_data() {
1314        let v = "large payload over 12 bytes";
1315        let view = ByteView::new(13, &v.as_bytes()[0..4])
1316            .with_buffer_index(3)
1317            .with_offset(1);
1318        let views = ScalarBuffer::from(vec![view.into()]);
1319        let buffers = vec![Buffer::from_slice_ref(v)];
1320        StringViewArray::new(views, buffers, None);
1321    }
1322
1323    #[test]
1324    #[should_panic(
1325        expected = "Encountered non-UTF-8 data at index 0: invalid utf-8 sequence of 1 bytes from index 0"
1326    )]
1327    fn new_with_invalid_utf8_data() {
1328        let v: Vec<u8> = vec![
1329            // invalid UTF8
1330            0xf0, 0x80, 0x80, 0x80, // more bytes to make it larger than 12
1331            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1332        ];
1333        let view = ByteView::new(v.len() as u32, &v[0..4]);
1334        let views = ScalarBuffer::from(vec![view.into()]);
1335        let buffers = vec![Buffer::from_slice_ref(v)];
1336        StringViewArray::new(views, buffers, None);
1337    }
1338
1339    #[test]
1340    #[should_panic(expected = "View at index 0 contained non-zero padding for string of length 1")]
1341    fn new_with_invalid_zero_padding() {
1342        let mut data = [0; 12];
1343        data[0] = b'H';
1344        data[11] = 1; // no zero padding
1345
1346        let mut view_buffer = [0; 16];
1347        view_buffer[0..4].copy_from_slice(&1u32.to_le_bytes());
1348        view_buffer[4..].copy_from_slice(&data);
1349
1350        let view = ByteView::from(u128::from_le_bytes(view_buffer));
1351        let views = ScalarBuffer::from(vec![view.into()]);
1352        let buffers = vec![];
1353        StringViewArray::new(views, buffers, None);
1354    }
1355
1356    #[test]
1357    #[should_panic(expected = "Mismatch between embedded prefix and data")]
1358    fn test_mismatch_between_embedded_prefix_and_data() {
1359        let input_str_1 = "Hello, Rustaceans!";
1360        let input_str_2 = "Hallo, Rustaceans!";
1361        let length = input_str_1.len() as u32;
1362        assert!(input_str_1.len() > 12);
1363
1364        let mut view_buffer = [0; 16];
1365        view_buffer[0..4].copy_from_slice(&length.to_le_bytes());
1366        view_buffer[4..8].copy_from_slice(&input_str_1.as_bytes()[0..4]);
1367        view_buffer[8..12].copy_from_slice(&0u32.to_le_bytes());
1368        view_buffer[12..].copy_from_slice(&0u32.to_le_bytes());
1369        let view = ByteView::from(u128::from_le_bytes(view_buffer));
1370        let views = ScalarBuffer::from(vec![view.into()]);
1371        let buffers = vec![Buffer::from_slice_ref(input_str_2.as_bytes())];
1372
1373        StringViewArray::new(views, buffers, None);
1374    }
1375
1376    #[test]
1377    fn test_gc() {
1378        let test_data = [
1379            Some("longer than 12 bytes"),
1380            Some("short"),
1381            Some("t"),
1382            Some("longer than 12 bytes"),
1383            None,
1384            Some("short"),
1385        ];
1386
1387        let array = {
1388            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // create multiple buffers
1389            test_data.into_iter().for_each(|v| builder.append_option(v));
1390            builder.finish()
1391        };
1392        assert!(array.buffers.len() > 1);
1393
1394        fn check_gc(to_test: &StringViewArray) {
1395            let gc = to_test.gc();
1396            assert_ne!(to_test.data_buffers().len(), gc.data_buffers().len());
1397
1398            to_test.iter().zip(gc.iter()).for_each(|(a, b)| {
1399                assert_eq!(a, b);
1400            });
1401            assert_eq!(to_test.len(), gc.len());
1402        }
1403
1404        check_gc(&array);
1405        check_gc(&array.slice(1, 3));
1406        check_gc(&array.slice(2, 1));
1407        check_gc(&array.slice(2, 2));
1408        check_gc(&array.slice(3, 1));
1409    }
1410
1411    /// 1) Empty array: no elements, expect gc to return empty with no data buffers
1412    #[test]
1413    fn test_gc_empty_array() {
1414        let array = StringViewBuilder::new()
1415            .with_fixed_block_size(BLOCK_SIZE)
1416            .finish();
1417        let gced = array.gc();
1418        // length and null count remain zero
1419        assert_eq!(gced.len(), 0);
1420        assert_eq!(gced.null_count(), 0);
1421        // no underlying data buffers should be allocated
1422        assert!(
1423            gced.data_buffers().is_empty(),
1424            "Expected no data buffers for empty array"
1425        );
1426    }
1427
1428    /// 2) All inline values (<= INLINE_LEN): capacity-only data buffer, same values
1429    #[test]
1430    fn test_gc_all_inline() {
1431        let mut builder = StringViewBuilder::new().with_fixed_block_size(BLOCK_SIZE);
1432        // append many short strings, each exactly INLINE_LEN long
1433        for _ in 0..100 {
1434            let s = "A".repeat(MAX_INLINE_VIEW_LEN as usize);
1435            builder.append_option(Some(&s));
1436        }
1437        let array = builder.finish();
1438        let gced = array.gc();
1439        // Since all views fit inline, data buffer is empty
1440        assert_eq!(
1441            gced.data_buffers().len(),
1442            0,
1443            "Should have no data buffers for inline values"
1444        );
1445        assert_eq!(gced.len(), 100);
1446        // verify element-wise equality
1447        array.iter().zip(gced.iter()).for_each(|(orig, got)| {
1448            assert_eq!(orig, got, "Inline value mismatch after gc");
1449        });
1450    }
1451
1452    /// 3) All large values (> INLINE_LEN): each must be copied into the new data buffer
1453    #[test]
1454    fn test_gc_all_large() {
1455        let mut builder = StringViewBuilder::new().with_fixed_block_size(BLOCK_SIZE);
1456        let large_str = "X".repeat(MAX_INLINE_VIEW_LEN as usize + 5);
1457        // append multiple large strings
1458        for _ in 0..50 {
1459            builder.append_option(Some(&large_str));
1460        }
1461        let array = builder.finish();
1462        let gced = array.gc();
1463        // New data buffers should be populated (one or more blocks)
1464        assert!(
1465            !gced.data_buffers().is_empty(),
1466            "Expected data buffers for large values"
1467        );
1468        assert_eq!(gced.len(), 50);
1469        // verify that every large string emerges unchanged
1470        array.iter().zip(gced.iter()).for_each(|(orig, got)| {
1471            assert_eq!(orig, got, "Large view mismatch after gc");
1472        });
1473    }
1474
1475    /// 4) All null elements: ensure null bitmap handling path is correct
1476    #[test]
1477    fn test_gc_all_nulls() {
1478        let mut builder = StringViewBuilder::new().with_fixed_block_size(BLOCK_SIZE);
1479        for _ in 0..20 {
1480            builder.append_null();
1481        }
1482        let array = builder.finish();
1483        let gced = array.gc();
1484        // length and null count match
1485        assert_eq!(gced.len(), 20);
1486        assert_eq!(gced.null_count(), 20);
1487        // data buffers remain empty for null-only array
1488        assert!(
1489            gced.data_buffers().is_empty(),
1490            "No data should be stored for nulls"
1491        );
1492    }
1493
1494    /// 5) Random mix of inline, large, and null values with slicing tests
1495    #[test]
1496    fn test_gc_random_mixed_and_slices() {
1497        let mut rng = StdRng::seed_from_u64(42);
1498        let mut builder = StringViewBuilder::new().with_fixed_block_size(BLOCK_SIZE);
1499        // Keep a Vec of original Option<String> for later comparison
1500        let mut original: Vec<Option<String>> = Vec::new();
1501
1502        for _ in 0..200 {
1503            if rng.random_bool(0.1) {
1504                // 10% nulls
1505                builder.append_null();
1506                original.push(None);
1507            } else {
1508                // random length between 0 and twice the inline limit
1509                let len = rng.random_range(0..(MAX_INLINE_VIEW_LEN * 2));
1510                let s = "A".repeat(len as usize);
1511                builder.append_option(Some(&s));
1512                original.push(Some(s));
1513            }
1514        }
1515
1516        let array = builder.finish();
1517        // Test multiple slice ranges to ensure offset logic is correct
1518        for (offset, slice_len) in &[(0, 50), (10, 100), (150, 30)] {
1519            let sliced = array.slice(*offset, *slice_len);
1520            let gced = sliced.gc();
1521            // Build expected slice of Option<&str>
1522            let expected: Vec<Option<&str>> = original[*offset..(*offset + *slice_len)]
1523                .iter()
1524                .map(|opt| opt.as_deref())
1525                .collect();
1526
1527            assert_eq!(gced.len(), *slice_len, "Slice length mismatch");
1528            // Compare element-wise
1529            gced.iter().zip(expected.iter()).for_each(|(got, expect)| {
1530                assert_eq!(got, *expect, "Value mismatch in mixed slice after gc");
1531            });
1532        }
1533    }
1534
1535    #[test]
1536    #[cfg_attr(miri, ignore)] // Takes too long
1537    fn test_gc_huge_array() {
1538        // Construct multiple 128 MiB BinaryView entries so total > 4 GiB
1539        let block_len: usize = 128 * 1024 * 1024; // 128 MiB per view
1540        let num_views: usize = 36;
1541
1542        // Create a single 128 MiB data block with a simple byte pattern
1543        let buffer = Buffer::from_vec(vec![0xAB; block_len]);
1544        let buffer2 = Buffer::from_vec(vec![0xFF; block_len]);
1545
1546        // Append this block and then add many views pointing to it
1547        let mut builder = BinaryViewBuilder::new();
1548        let block_id = builder.append_block(buffer);
1549        for _ in 0..num_views / 2 {
1550            builder
1551                .try_append_view(block_id, 0, block_len as u32)
1552                .expect("append view into 128MiB block");
1553        }
1554        let block_id2 = builder.append_block(buffer2);
1555        for _ in 0..num_views / 2 {
1556            builder
1557                .try_append_view(block_id2, 0, block_len as u32)
1558                .expect("append view into 128MiB block");
1559        }
1560
1561        let array = builder.finish();
1562        let total = array.total_buffer_bytes_used();
1563        assert!(
1564            total > u32::MAX as usize,
1565            "Expected total non-inline bytes to exceed 4 GiB, got {total}"
1566        );
1567
1568        // Run gc and verify correctness
1569        let gced = array.gc();
1570        assert_eq!(gced.len(), num_views, "Length mismatch after gc");
1571        assert_eq!(gced.null_count(), 0, "Null count mismatch after gc");
1572        assert_ne!(
1573            gced.data_buffers().len(),
1574            1,
1575            "gc with huge buffer should not consolidate data into a single buffer"
1576        );
1577
1578        // Element-wise equality check across the entire array
1579        array.iter().zip(gced.iter()).for_each(|(orig, got)| {
1580            assert_eq!(orig, got, "Value mismatch after gc on huge array");
1581        });
1582    }
1583
1584    #[test]
1585    fn test_gc_slow_path_preserves_inline_views() {
1586        // Regression test for a bug in the multi-buffer "slow path" of `gc()`,
1587        // which `gc` only reaches once a view column references more than
1588        // `i32::MAX` (~2.1 GiB) of non-inline data. That path grouped and copied
1589        // only the *non-inline* views and dropped every inline (short) view,
1590        // returning an array shorter than its input. GC is a pure size
1591        // optimisation and must never change the row count.
1592        //
1593        // Rather than allocate gigabytes to hit the real threshold, drive the
1594        // same code via `gc_with_max_buffer_size` with a tiny cap so a handful
1595        // of bytes forces the buffer split. Inline, large, and null views are
1596        // interspersed so the split lands between and around inline entries.
1597        let long = "this is definitely longer than twelve bytes"; // non-inline
1598        let test_data = [
1599            Some("short"),      // inline
1600            Some(long),         // large
1601            Some("s"),          // inline
1602            Some(long),         // large
1603            None,               // null
1604            Some(long),         // large
1605            Some("also short"), // inline
1606            Some(long),         // large
1607            Some("tail"),       // inline
1608        ];
1609        let array: StringViewArray = test_data.into_iter().collect();
1610
1611        // Cap each output buffer at ~1.5 large values so the copy is forced to
1612        // split across several buffers, exercising the slow path with no
1613        // multi-GiB allocation.
1614        let max_buffer_size = long.len() + long.len() / 2;
1615        let gced = array.gc_with_max_buffer_size(max_buffer_size);
1616
1617        // The core invariant: gc preserves the row count.
1618        assert_eq!(gced.len(), array.len(), "gc changed the row count");
1619        // The split must actually have happened, otherwise we'd be exercising
1620        // the fast path and could not catch the inline-dropping regression.
1621        assert!(
1622            gced.data_buffers().len() > 1,
1623            "expected output split across multiple buffers, got {}",
1624            gced.data_buffers().len()
1625        );
1626        // No output buffer may exceed the cap.
1627        for buf in gced.data_buffers().iter() {
1628            assert!(buf.len() <= max_buffer_size, "buffer exceeded max size");
1629        }
1630        // Every value (inline, large, and null) is unchanged and in order.
1631        array
1632            .iter()
1633            .zip(gced.iter())
1634            .enumerate()
1635            .for_each(|(i, (a, b))| {
1636                assert_eq!(a, b, "value mismatch at index {i} after gc");
1637            });
1638        // The result round-trips through full validation.
1639        gced.to_data().validate_full().unwrap();
1640    }
1641
1642    #[test]
1643    fn test_gc_slow_path_value_larger_than_buffer() {
1644        // A single non-inline value can be larger than one output buffer (a view
1645        // length is a `u32`, so a value may exceed `max_buffer_size`). Such a
1646        // value cannot be split, so it occupies a buffer of its own; gc must
1647        // still preserve every row and produce a valid array.
1648        //
1649        // The real threshold needs a multi-GiB value, so drive the same logic
1650        // with a `max_buffer_size` smaller than the value length.
1651        let big = "x".repeat(64); // non-inline, larger than the cap below
1652        let test_data = [
1653            Some("short"),      // inline
1654            Some(big.as_str()), // larger than max_buffer_size
1655            None,               // null
1656            Some("tail"),       // inline
1657            Some(big.as_str()), // larger than max_buffer_size
1658        ];
1659        let array: StringViewArray = test_data.into_iter().collect();
1660
1661        let max_buffer_size = 16; // smaller than a single `big` value
1662        let gced = array.gc_with_max_buffer_size(max_buffer_size);
1663
1664        // Row count is preserved.
1665        assert_eq!(gced.len(), array.len(), "gc changed the row count");
1666        // Each oversized value lands in its own buffer, so the copy splits.
1667        assert!(
1668            gced.data_buffers().len() > 1,
1669            "expected output split across multiple buffers, got {}",
1670            gced.data_buffers().len()
1671        );
1672        // Every value is unchanged and in order.
1673        array
1674            .iter()
1675            .zip(gced.iter())
1676            .enumerate()
1677            .for_each(|(i, (a, b))| {
1678                assert_eq!(a, b, "value mismatch at index {i} after gc");
1679            });
1680        // The result round-trips through full validation.
1681        gced.to_data().validate_full().unwrap();
1682    }
1683
1684    #[test]
1685    fn test_eq() {
1686        let test_data = [
1687            Some("longer than 12 bytes"),
1688            None,
1689            Some("short"),
1690            Some("again, this is longer than 12 bytes"),
1691        ];
1692
1693        let array1 = {
1694            let mut builder = StringViewBuilder::new().with_fixed_block_size(8);
1695            test_data.into_iter().for_each(|v| builder.append_option(v));
1696            builder.finish()
1697        };
1698        let array2 = {
1699            // create a new array with the same data but different layout
1700            let mut builder = StringViewBuilder::new().with_fixed_block_size(100);
1701            test_data.into_iter().for_each(|v| builder.append_option(v));
1702            builder.finish()
1703        };
1704        assert_eq!(array1, array1.clone());
1705        assert_eq!(array2, array2.clone());
1706        assert_eq!(array1, array2);
1707    }
1708
1709    /// Integration tests for `inline_key_fast` covering:
1710    ///
1711    /// 1. Monotonic ordering across increasing lengths and lexical variations.
1712    /// 2. Cross-check against `GenericBinaryArray` comparison to ensure semantic equivalence.
1713    ///
1714    /// This also includes a specific test for the “bar” vs. “bar\0” case, demonstrating why
1715    /// the length field is required even when all inline bytes fit in 12 bytes.
1716    ///
1717    /// The test includes strings that verify correct byte order (prevent reversal bugs),
1718    /// and length-based tie-breaking in the composite key.
1719    ///
1720    /// The test confirms that `inline_key_fast` produces keys which sort consistently
1721    /// with the expected lexicographical order of the raw byte arrays.
1722    #[test]
1723    fn test_inline_key_fast_various_lengths_and_lexical() {
1724        /// Helper to create a raw u128 value representing an inline ByteView:
1725        /// - `length`: number of meaningful bytes (must be ≤ 12)
1726        /// - `data`: the actual inline data bytes
1727        ///
1728        /// The first 4 bytes encode length in little-endian,
1729        /// the following 12 bytes contain the inline string data (unpadded).
1730        fn make_raw_inline(length: u32, data: &[u8]) -> u128 {
1731            assert!(length as usize <= 12, "Inline length must be ≤ 12");
1732            assert!(
1733                data.len() == length as usize,
1734                "Data length must match `length`"
1735            );
1736
1737            let mut raw_bytes = [0u8; 16];
1738            raw_bytes[0..4].copy_from_slice(&length.to_le_bytes()); // length stored little-endian
1739            raw_bytes[4..(4 + data.len())].copy_from_slice(data); // inline data
1740            u128::from_le_bytes(raw_bytes)
1741        }
1742
1743        // Test inputs: various lengths and lexical orders,
1744        // plus special cases for byte order and length tie-breaking
1745        let test_inputs: Vec<&[u8]> = vec![
1746            b"a",
1747            b"aa",
1748            b"aaa",
1749            b"aab",
1750            b"abcd",
1751            b"abcde",
1752            b"abcdef",
1753            b"abcdefg",
1754            b"abcdefgh",
1755            b"abcdefghi",
1756            b"abcdefghij",
1757            b"abcdefghijk",
1758            b"abcdefghijkl",
1759            // Tests for byte-order reversal bug:
1760            // Without the fix, "backend one" would compare as "eno dnekcab",
1761            // causing incorrect sort order relative to "backend two".
1762            b"backend one",
1763            b"backend two",
1764            // Tests length-tiebreaker logic:
1765            // "bar" (3 bytes) and "bar\0" (4 bytes) have identical inline data,
1766            // so only the length differentiates their ordering.
1767            b"bar",
1768            b"bar\0",
1769            // Additional lexical and length tie-breaking cases with same prefix, in correct lex order:
1770            b"than12Byt",
1771            b"than12Bytes",
1772            b"than12Bytes\0",
1773            b"than12Bytesx",
1774            b"than12Bytex",
1775            b"than12Bytez",
1776            // Additional lexical tests
1777            b"xyy",
1778            b"xyz",
1779            b"xza",
1780        ];
1781
1782        // Create a GenericBinaryArray for cross-comparison of lex order
1783        let array: GenericBinaryArray<i32> =
1784            GenericBinaryArray::from(test_inputs.iter().map(|s| Some(*s)).collect::<Vec<_>>());
1785
1786        for i in 0..array.len() - 1 {
1787            let v1 = array.value(i);
1788            let v2 = array.value(i + 1);
1789
1790            // Assert the array's natural lexical ordering is correct
1791            assert!(v1 < v2, "Array compare failed: {v1:?} !< {v2:?}");
1792
1793            // Assert the keys produced by inline_key_fast reflect the same ordering
1794            let key1 = GenericByteViewArray::<BinaryViewType>::inline_key_fast(make_raw_inline(
1795                v1.len() as u32,
1796                v1,
1797            ));
1798            let key2 = GenericByteViewArray::<BinaryViewType>::inline_key_fast(make_raw_inline(
1799                v2.len() as u32,
1800                v2,
1801            ));
1802
1803            assert!(
1804                key1 < key2,
1805                "Key compare failed: key({v1:?})=0x{key1:032x} !< key({v2:?})=0x{key2:032x}",
1806            );
1807        }
1808    }
1809
1810    #[test]
1811    fn empty_array_should_return_empty_lengths_iterator() {
1812        let empty = GenericByteViewArray::<BinaryViewType>::from(Vec::<&[u8]>::new());
1813
1814        let mut lengths_iter = empty.lengths();
1815        assert_eq!(lengths_iter.len(), 0);
1816        assert_eq!(lengths_iter.next(), None);
1817    }
1818
1819    #[test]
1820    fn array_lengths_should_return_correct_length_for_both_inlined_and_non_inlined() {
1821        let cases = GenericByteViewArray::<BinaryViewType>::from(vec![
1822            // Not inlined as longer than 12 bytes
1823            b"Supercalifragilisticexpialidocious" as &[u8],
1824            // Inlined as shorter than 12 bytes
1825            b"Hello",
1826            // Empty value
1827            b"",
1828            // Exactly 12 bytes
1829            b"abcdefghijkl",
1830        ]);
1831
1832        let mut lengths_iter = cases.lengths();
1833
1834        assert_eq!(lengths_iter.len(), cases.len());
1835
1836        let cases_iter = cases.iter();
1837
1838        for case in cases_iter {
1839            let case_value = case.unwrap();
1840            let length = lengths_iter.next().expect("Should have a length");
1841
1842            assert_eq!(case_value.len(), length as usize);
1843        }
1844
1845        assert_eq!(lengths_iter.next(), None, "Should not have more lengths");
1846    }
1847
1848    #[test]
1849    fn array_lengths_should_return_the_underlying_length_for_null_values() {
1850        let cases = GenericByteViewArray::<BinaryViewType>::from(vec![
1851            // Not inlined as longer than 12 bytes
1852            b"Supercalifragilisticexpialidocious" as &[u8],
1853            // Inlined as shorter than 12 bytes
1854            b"Hello",
1855            // Empty value
1856            b"",
1857            // Exactly 12 bytes
1858            b"abcdefghijkl",
1859        ]);
1860
1861        let (views, buffer, _) = cases.clone().into_parts();
1862
1863        // Keeping the values but just adding nulls on top
1864        let cases_with_all_nulls = GenericByteViewArray::<BinaryViewType>::new(
1865            views,
1866            buffer,
1867            Some(NullBuffer::new_null(cases.len())),
1868        );
1869
1870        let lengths_iter = cases.lengths();
1871        let mut all_nulls_lengths_iter = cases_with_all_nulls.lengths();
1872
1873        assert_eq!(lengths_iter.len(), all_nulls_lengths_iter.len());
1874
1875        for expected_length in lengths_iter {
1876            let actual_length = all_nulls_lengths_iter.next().expect("Should have a length");
1877
1878            assert_eq!(expected_length, actual_length);
1879        }
1880
1881        assert_eq!(
1882            all_nulls_lengths_iter.next(),
1883            None,
1884            "Should not have more lengths"
1885        );
1886    }
1887
1888    #[test]
1889    fn array_lengths_on_sliced_should_only_return_lengths_for_sliced_data() {
1890        let array = GenericByteViewArray::<BinaryViewType>::from(vec![
1891            b"aaaaaaaaaaaaaaaaaaaaaaaaaaa" as &[u8],
1892            b"Hello",
1893            b"something great",
1894            b"is",
1895            b"coming soon!",
1896            b"when you find what it is",
1897            b"let me know",
1898            b"cause",
1899            b"I",
1900            b"have no idea",
1901            b"what it",
1902            b"is",
1903        ]);
1904
1905        let sliced_array = array.slice(2, array.len() - 3);
1906
1907        let mut lengths_iter = sliced_array.lengths();
1908
1909        assert_eq!(lengths_iter.len(), sliced_array.len());
1910
1911        let values_iter = sliced_array.iter();
1912
1913        for value in values_iter {
1914            let value = value.unwrap();
1915            let length = lengths_iter.next().expect("Should have a length");
1916
1917            assert_eq!(value.len(), length as usize);
1918        }
1919
1920        assert_eq!(lengths_iter.next(), None, "Should not have more lengths");
1921    }
1922
1923    #[should_panic(expected = "Mismatched data type, expected Utf8View, got BinaryView")]
1924    #[test]
1925    fn invalid_casting_from_array_data() {
1926        // Should not be able to cast to StringViewArray due to invalid UTF-8
1927        let array_data = binary_view_array_with_invalid_utf8_data().into_data();
1928        let _ = StringViewArray::from(array_data);
1929    }
1930
1931    #[should_panic(expected = "invalid utf-8 sequence")]
1932    #[test]
1933    fn invalid_array_data() {
1934        let (views, buffers, nulls) = binary_view_array_with_invalid_utf8_data().into_parts();
1935
1936        // manually try and add invalid array data with Utf8View data type
1937        let mut builder = ArrayDataBuilder::new(DataType::Utf8View)
1938            .add_buffer(views.into_inner())
1939            .len(3);
1940        for buffer in buffers.iter() {
1941            builder = builder.add_buffer(buffer.clone())
1942        }
1943        builder = builder.nulls(nulls);
1944
1945        let data = builder.build().unwrap(); // should fail validation
1946        let _arr = StringViewArray::from(data);
1947    }
1948
1949    /// Returns a BinaryViewArray with one invalid UTF-8 value
1950    fn binary_view_array_with_invalid_utf8_data() -> BinaryViewArray {
1951        let array = GenericByteViewArray::<BinaryViewType>::from(vec![
1952            b"aaaaaaaaaaaaaaaaaaaaaaaaaaa" as &[u8],
1953            &[
1954                0xf0, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1955                0x00, 0x00,
1956            ],
1957            b"good",
1958        ]);
1959        assert!(from_utf8(array.value(0)).is_ok());
1960        assert!(from_utf8(array.value(1)).is_err()); // value 1 is invalid utf8
1961        assert!(from_utf8(array.value(2)).is_ok());
1962        array
1963    }
1964
1965    #[test]
1966    fn test_total_bytes_len() {
1967        // inlined: "hello"=5, "world"=5, "lulu"=4 → 14
1968        // non-inlined: "large payload over 12 bytes"=27
1969        // null: should not count
1970        let mut builder = StringViewBuilder::new();
1971        builder.append_value("hello");
1972        builder.append_value("world");
1973        builder.append_value("lulu");
1974        builder.append_null();
1975        builder.append_value("large payload over 12 bytes");
1976        let array = builder.finish();
1977        assert_eq!(array.total_bytes_len(), 5 + 5 + 4 + 27);
1978    }
1979
1980    #[test]
1981    fn test_total_bytes_len_empty() {
1982        let array = StringViewArray::from_iter::<Vec<Option<&str>>>(vec![]);
1983        assert_eq!(array.total_bytes_len(), 0);
1984    }
1985
1986    #[test]
1987    fn test_total_bytes_len_all_nulls() {
1988        let array = StringViewArray::new_null(5);
1989        assert_eq!(array.total_bytes_len(), 0);
1990    }
1991
1992    #[test]
1993    fn test_total_bytes_len_binary_view() {
1994        let array = BinaryViewArray::from_iter(vec![
1995            Some(b"hi".as_ref()),
1996            None,
1997            Some(b"large payload over 12 bytes".as_ref()),
1998        ]);
1999        assert_eq!(array.total_bytes_len(), 2 + 27);
2000    }
2001}