arrow::array

Struct GenericByteViewArray

pub struct GenericByteViewArray<T>
where T: ByteViewType + ?Sized,
{ data_type: DataType, views: ScalarBuffer<u128>, buffers: Vec<Buffer>, phantom: PhantomData<T>, nulls: Option<NullBuffer>, }
Expand description

Variable-size Binary View Layout: An array of variable length bytes view arrays.

This is different from GenericByteArray as it stores both an offset and length meaning that take / filter operations can be implemented without copying the underlying data. In addition, it stores an inlined prefix which can be used to speed up comparisons.

§See Also

§Notes

Comparing two GenericByteViewArray using PartialEq compares by structure, not by value. as there are many different buffer layouts to represent the same data (e.g. different offsets, different buffer sizes, etc).

§Layout: “views” and buffers

A GenericByteViewArray stores variable length byte strings. An array of N elements is stored as N fixed length “views” and a variable number of variable length “buffers”.

Each view is a u128 value whose layout is different depending on the length of the string stored at that location:

                        ┌──────┬────────────────────────┐
                        │length│      string value      │
   Strings (len <= 12)  │      │    (padded with 0)     │
                        └──────┴────────────────────────┘
                         0    31                      127

                        ┌───────┬───────┬───────┬───────┐
                        │length │prefix │  buf  │offset │
   Strings (len > 12)   │       │       │ index │       │
                        └───────┴───────┴───────┴───────┘
                         0    31       63      95    127
  • Strings with length <= 12 are stored directly in the view. See Self::inline_value to access the inlined prefix from a short view.

  • Strings with length > 12: The first four bytes are stored inline in the view and the entire string is stored in one of the buffers. See ByteView to access the fields of the these views.

Unlike GenericByteArray, there are no constraints on the offsets other than they must point into a valid buffer. However, they can be out of order, non continuous and overlapping.

For example, in the following diagram, the strings “FishWasInTownToday” and “CrumpleFacedFish” are both longer than 12 bytes and thus are stored in a separate buffer while the string “LavaMonster” is stored inlined in the view. In this case, the same bytes for “Fish” are used to store both strings.

                                                                           ┌───┐
                        ┌──────┬──────┬──────┬──────┐               offset │...│
"FishWasInTownTodayYay" │  21  │ Fish │  0   │ 115  │─ ─              103  │Mr.│
                        └──────┴──────┴──────┴──────┘   │      ┌ ─ ─ ─ ─ ▶ │Cru│
                        ┌──────┬──────┬──────┬──────┐                      │mpl│
"CrumpleFacedFish"      │  16  │ Crum │  0   │ 103  │─ ─│─ ─ ─ ┘           │eFa│
                        └──────┴──────┴──────┴──────┘                      │ced│
                        ┌──────┬────────────────────┐   └ ─ ─ ─ ─ ─ ─ ─ ─ ▶│Fis│
"LavaMonster"           │  11  │   LavaMonster\0    │                      │hWa│
                        └──────┴────────────────────┘               offset │sIn│
                                                                      115  │Tow│
                                                                           │nTo│
                                                                           │day│
                                 u128 "views"                              │Yay│
                                                                  buffer 0 │...│
                                                                           └───┘

Fields§

§data_type: DataType§views: ScalarBuffer<u128>§buffers: Vec<Buffer>§phantom: PhantomData<T>§nulls: Option<NullBuffer>

Implementations§

§

impl<T> GenericByteViewArray<T>
where T: ByteViewType + ?Sized,

pub fn new( views: ScalarBuffer<u128>, buffers: Vec<Buffer>, nulls: Option<NullBuffer>, ) -> GenericByteViewArray<T>

Create a new GenericByteViewArray from the provided parts, panicking on failure

§Panics

Panics if GenericByteViewArray::try_new returns an error

pub fn try_new( views: ScalarBuffer<u128>, buffers: Vec<Buffer>, nulls: Option<NullBuffer>, ) -> Result<GenericByteViewArray<T>, ArrowError>

Create a new GenericByteViewArray from the provided parts, returning an error on failure

§Errors

pub unsafe fn new_unchecked( views: ScalarBuffer<u128>, buffers: Vec<Buffer>, nulls: Option<NullBuffer>, ) -> GenericByteViewArray<T>

Create a new GenericByteViewArray from the provided parts, without validation

§Safety

Safe if Self::try_new would not error

pub fn new_null(len: usize) -> GenericByteViewArray<T>

Create a new GenericByteViewArray of length len where all values are null

pub fn new_scalar( value: impl AsRef<<T as ByteViewType>::Native>, ) -> Scalar<GenericByteViewArray<T>>

Create a new Scalar from value

pub fn from_iter_values<Ptr, I>(iter: I) -> GenericByteViewArray<T>
where Ptr: AsRef<<T as ByteViewType>::Native>, I: IntoIterator<Item = Ptr>,

Creates a GenericByteViewArray based on an iterator of values without nulls

pub fn into_parts(self) -> (ScalarBuffer<u128>, Vec<Buffer>, Option<NullBuffer>)

Deconstruct this array into its constituent parts

pub fn views(&self) -> &ScalarBuffer<u128>

Returns the views buffer

pub fn data_buffers(&self) -> &[Buffer]

Returns the buffers storing string data

pub fn value(&self, i: usize) -> &<T as ByteViewType>::Native

Returns the element at index i

§Panics

Panics if index i is out of bounds.

pub unsafe fn value_unchecked(&self, idx: usize) -> &<T as ByteViewType>::Native

Returns the element at index i without bounds checking

§Safety

Caller is responsible for ensuring that the index is within the bounds of the array

pub unsafe fn inline_value(view: &u128, len: usize) -> &[u8]

Returns the first len bytes the inline value of the view.

§Safety
  • The view must be a valid element from Self::views() that adheres to the view layout.
  • The len must be the length of the inlined value. It should never be larger than 12.

pub fn iter(&self) -> ArrayIter<&GenericByteViewArray<T>>

Constructs a new iterator for iterating over the values of this array

pub fn bytes_iter(&self) -> impl Iterator<Item = &[u8]>

Returns an iterator over the bytes of this array, including null values

pub fn prefix_bytes_iter( &self, prefix_len: usize, ) -> impl Iterator<Item = &[u8]>

Returns an iterator over the first prefix_len bytes of each array element, including null values.

If prefix_len is larger than the element’s length, the iterator will return an empty slice (&[]).

pub fn suffix_bytes_iter( &self, suffix_len: usize, ) -> impl Iterator<Item = &[u8]>

Returns an iterator over the last suffix_len bytes of each array element, including null values.

Note that for StringViewArray the last bytes may start in the middle of a UTF-8 codepoint, and thus may not be a valid &str.

If suffix_len is larger than the element’s length, the iterator will return an empty slice (&[]).

pub fn slice(&self, offset: usize, length: usize) -> GenericByteViewArray<T>

Returns a zero-copy slice of this array with the indicated offset and length.

pub fn gc(&self) -> GenericByteViewArray<T>

Returns a “compacted” version of this array

The original array will not be modified

§Garbage Collection

Before GC:

                                       ┌──────┐                 
                                       │......│                 
                                       │......│                 
┌────────────────────┐       ┌ ─ ─ ─ ▶ │Data1 │   Large buffer  
│       View 1       │─ ─ ─ ─          │......│  with data that
├────────────────────┤                 │......│ is not referred
│       View 2       │─ ─ ─ ─ ─ ─ ─ ─▶ │Data2 │ to by View 1 or
└────────────────────┘                 │......│      View 2     
                                       │......│                 
   2 views, refer to                   │......│                 
  small portions of a                  └──────┘                 
     large buffer                                               

After GC:

┌────────────────────┐                 ┌─────┐    After gc, only
│       View 1       │─ ─ ─ ─ ─ ─ ─ ─▶ │Data1│     data that is  
├────────────────────┤       ┌ ─ ─ ─ ▶ │Data2│    pointed to by  
│       View 2       │─ ─ ─ ─          └─────┘     the views is  
└────────────────────┘                                 left      
                                                                  
                                                                  
        2 views                                                  

This method will compact the data buffers by recreating the view array and only include the data that is pointed to by the views.

Note that it will copy the array regardless of whether the original array is compact. Use with caution as this can be an expensive operation, only use it when you are sure that the view array is significantly smaller than when it is originally created, e.g., after filtering or slicing.

Note: this function does not attempt to canonicalize / deduplicate values. For this feature see GenericByteViewBuilder::with_deduplicate_strings.

pub unsafe fn compare_unchecked( left: &GenericByteViewArray<T>, left_idx: usize, right: &GenericByteViewArray<T>, right_idx: usize, ) -> Ordering

Compare two GenericByteViewArray at index left_idx and right_idx

Comparing two ByteView types are non-trivial. It takes a bit of patience to understand why we don’t just compare two &u8 directly.

ByteView types give us the following two advantages, and we need to be careful not to lose them: (1) For string/byte smaller than 12 bytes, the entire data is inlined in the view. Meaning that reading one array element requires only one memory access (two memory access required for StringArray, one for offset buffer, the other for value buffer).

(2) For string/byte larger than 12 bytes, we can still be faster than (for certain operations) StringArray/ByteArray, thanks to the inlined 4 bytes. Consider equality check: If the first four bytes of the two strings are different, we can return false immediately (with just one memory access).

If we directly compare two &u8, we materialize the entire string (i.e., make multiple memory accesses), which might be unnecessary.

  • Most of the time (eq, ord), we only need to look at the first 4 bytes to know the answer, e.g., if the inlined 4 bytes are different, we can directly return unequal without looking at the full string.
§Order check flow

(1) if both string are smaller than 12 bytes, we can directly compare the data inlined to the view. (2) if any of the string is larger than 12 bytes, we need to compare the full string. (2.1) if the inlined 4 bytes are different, we can return the result immediately. (2.2) o.w., we need to compare the full string.

§Safety

The left/right_idx must within range of each array

§

impl GenericByteViewArray<BinaryViewType>

pub fn to_string_view( self, ) -> Result<GenericByteViewArray<StringViewType>, ArrowError>

Convert the BinaryViewArray to StringViewArray If items not utf8 data, validate will fail and error returned.

pub unsafe fn to_string_view_unchecked( self, ) -> GenericByteViewArray<StringViewType>

Convert the BinaryViewArray to StringViewArray

§Safety

Caller is responsible for ensuring that items in array are utf8 data.

§

impl GenericByteViewArray<StringViewType>

pub fn to_binary_view(self) -> GenericByteViewArray<BinaryViewType>

pub fn is_ascii(&self) -> bool

Returns true if all data within this array is ASCII

Trait Implementations§

§

impl<T> Array for GenericByteViewArray<T>
where T: ByteViewType + ?Sized,

§

fn as_any(&self) -> &(dyn Any + 'static)

Returns the array as Any so that it can be downcasted to a specific implementation. Read more
§

fn to_data(&self) -> ArrayData

Returns the underlying data of this array
§

fn into_data(self) -> ArrayData

Returns the underlying data of this array Read more
§

fn data_type(&self) -> &DataType

Returns a reference to the DataType of this array. Read more
§

fn slice(&self, offset: usize, length: usize) -> Arc<dyn Array>

Returns a zero-copy slice of this array with the indicated offset and length. Read more
§

fn len(&self) -> usize

Returns the length (i.e., number of elements) of this array. Read more
§

fn is_empty(&self) -> bool

Returns whether this array is empty. Read more
§

fn offset(&self) -> usize

Returns the offset into the underlying data used by this array(-slice). Note that the underlying data can be shared by many arrays. This defaults to 0. Read more
§

fn nulls(&self) -> Option<&NullBuffer>

Returns the null buffer of this array if any. Read more
§

fn get_buffer_memory_size(&self) -> usize

Returns the total number of bytes of memory pointed to by this array. The buffers store bytes in the Arrow memory format, and include the data as well as the validity map. Note that this does not always correspond to the exact memory usage of an array, since multiple arrays can share the same buffers or slices thereof.
§

fn get_array_memory_size(&self) -> usize

Returns the total number of bytes of memory occupied physically by this array. This value will always be greater than returned by get_buffer_memory_size() and includes the overhead of the data structures that contain the pointers to the various buffers.
§

fn logical_nulls(&self) -> Option<NullBuffer>

Returns a potentially computed NullBuffer that represents the logical null values of this array, if any. Read more
§

fn is_null(&self, index: usize) -> bool

Returns whether the element at index is null according to Array::nulls Read more
§

fn is_valid(&self, index: usize) -> bool

Returns whether the element at index is not null, the opposite of Self::is_null. Read more
§

fn null_count(&self) -> usize

Returns the total number of physical null values in this array. Read more
§

fn is_nullable(&self) -> bool

Returns false if the array is guaranteed to not contain any logical nulls Read more
§

impl<'a, T> ArrayAccessor for &'a GenericByteViewArray<T>
where T: ByteViewType + ?Sized,

§

type Item = &'a <T as ByteViewType>::Native

The Arrow type of the element being accessed.
§

fn value( &self, index: usize, ) -> <&'a GenericByteViewArray<T> as ArrayAccessor>::Item

Returns the element at index i Read more
§

unsafe fn value_unchecked( &self, index: usize, ) -> <&'a GenericByteViewArray<T> as ArrayAccessor>::Item

Returns the element at index i Read more
§

impl<T> Clone for GenericByteViewArray<T>
where T: ByteViewType + ?Sized,

§

fn clone(&self) -> GenericByteViewArray<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl<T> Debug for GenericByteViewArray<T>
where T: ByteViewType + ?Sized,

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl<FROM, V> From<&GenericByteArray<FROM>> for GenericByteViewArray<V>
where FROM: ByteArrayType, <FROM as ByteArrayType>::Offset: OffsetSizeTrait + ToPrimitive, V: ByteViewType<Native = <FROM as ByteArrayType>::Native>,

Convert a GenericByteArray to a GenericByteViewArray but in a smart way: If the offsets are all less than u32::MAX, then we directly build the view array on top of existing buffer.

§

fn from(byte_array: &GenericByteArray<FROM>) -> GenericByteViewArray<V>

Converts to this type from the input type.
§

impl<T> From<ArrayData> for GenericByteViewArray<T>
where T: ByteViewType + ?Sized,

§

fn from(value: ArrayData) -> GenericByteViewArray<T>

Converts to this type from the input type.
§

impl<T> From<GenericByteViewArray<T>> for ArrayData
where T: ByteViewType + ?Sized,

§

fn from(array: GenericByteViewArray<T>) -> ArrayData

Converts to this type from the input type.
§

impl From<Vec<&[u8]>> for GenericByteViewArray<BinaryViewType>

§

fn from(v: Vec<&[u8]>) -> GenericByteViewArray<BinaryViewType>

Converts to this type from the input type.
§

impl From<Vec<&str>> for GenericByteViewArray<StringViewType>

§

fn from(v: Vec<&str>) -> GenericByteViewArray<StringViewType>

Converts to this type from the input type.
§

impl From<Vec<Option<&[u8]>>> for GenericByteViewArray<BinaryViewType>

§

fn from(v: Vec<Option<&[u8]>>) -> GenericByteViewArray<BinaryViewType>

Converts to this type from the input type.
§

impl From<Vec<Option<&str>>> for GenericByteViewArray<StringViewType>

§

fn from(v: Vec<Option<&str>>) -> GenericByteViewArray<StringViewType>

Converts to this type from the input type.
§

impl From<Vec<Option<String>>> for GenericByteViewArray<StringViewType>

§

fn from(v: Vec<Option<String>>) -> GenericByteViewArray<StringViewType>

Converts to this type from the input type.
§

impl From<Vec<String>> for GenericByteViewArray<StringViewType>

§

fn from(v: Vec<String>) -> GenericByteViewArray<StringViewType>

Converts to this type from the input type.
§

impl<'a, Ptr, T> FromIterator<&'a Option<Ptr>> for GenericByteViewArray<T>
where Ptr: AsRef<<T as ByteViewType>::Native> + 'a, T: ByteViewType + ?Sized,

§

fn from_iter<I>(iter: I) -> GenericByteViewArray<T>
where I: IntoIterator<Item = &'a Option<Ptr>>,

Creates a value from an iterator. Read more
§

impl<Ptr, T> FromIterator<Option<Ptr>> for GenericByteViewArray<T>
where T: ByteViewType + ?Sized, Ptr: AsRef<<T as ByteViewType>::Native>,

§

fn from_iter<I>(iter: I) -> GenericByteViewArray<T>
where I: IntoIterator<Item = Option<Ptr>>,

Creates a value from an iterator. Read more
§

impl<'a, T> IntoIterator for &'a GenericByteViewArray<T>
where T: ByteViewType + ?Sized,

§

type Item = Option<&'a <T as ByteViewType>::Native>

The type of the elements being iterated over.
§

type IntoIter = ArrayIter<&'a GenericByteViewArray<T>>

Which kind of iterator are we turning this into?
§

fn into_iter(self) -> <&'a GenericByteViewArray<T> as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
§

impl<T> PartialEq for GenericByteViewArray<T>
where T: ByteViewType + ?Sized,

§

fn eq(&self, other: &GenericByteViewArray<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
§

impl<'a> StringArrayType<'a> for &'a GenericByteViewArray<StringViewType>

§

fn is_ascii(&self) -> bool

Returns true if all data within this string array is ASCII
§

fn iter(&self) -> ArrayIter<&'a GenericByteViewArray<StringViewType>>

Constructs a new iterator

Auto Trait Implementations§

§

impl<T> Freeze for GenericByteViewArray<T>
where T: ?Sized,

§

impl<T> RefUnwindSafe for GenericByteViewArray<T>
where T: RefUnwindSafe + ?Sized,

§

impl<T> Send for GenericByteViewArray<T>
where T: ?Sized,

§

impl<T> Sync for GenericByteViewArray<T>
where T: ?Sized,

§

impl<T> Unpin for GenericByteViewArray<T>
where T: Unpin + ?Sized,

§

impl<T> UnwindSafe for GenericByteViewArray<T>
where T: UnwindSafe + ?Sized,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
§

impl<T> Datum for T
where T: Array,

§

fn get(&self) -> (&dyn Array, bool)

Returns the value for this Datum and a boolean indicating if the value is scalar
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,

§

impl<T> Ungil for T
where T: Send,