Struct arrow_array::array::GenericByteViewArray
source · pub struct GenericByteViewArray<T: ByteViewType + ?Sized> { /* private fields */ }
Expand description
Variable-size Binary View Layout: An array of variable length bytes view arrays.
Different than crate::GenericByteArray
as it stores both an offset and length
meaning that take / filter operations can be implemented without copying the underlying data.
See StringViewArray
for storing utf8 encoded string data and
BinaryViewArray
for storing bytes.
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.
-
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.
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 │...│
└───┘
Implementations§
source§impl<T: ByteViewType + ?Sized> GenericByteViewArray<T>
impl<T: ByteViewType + ?Sized> GenericByteViewArray<T>
sourcepub fn new(
views: ScalarBuffer<u128>,
buffers: Vec<Buffer>,
nulls: Option<NullBuffer>,
) -> Self
pub fn new( views: ScalarBuffer<u128>, buffers: Vec<Buffer>, nulls: Option<NullBuffer>, ) -> Self
Create a new GenericByteViewArray
from the provided parts, panicking on failure
§Panics
Panics if GenericByteViewArray::try_new
returns an error
sourcepub fn try_new(
views: ScalarBuffer<u128>,
buffers: Vec<Buffer>,
nulls: Option<NullBuffer>,
) -> Result<Self, ArrowError>
pub fn try_new( views: ScalarBuffer<u128>, buffers: Vec<Buffer>, nulls: Option<NullBuffer>, ) -> Result<Self, ArrowError>
Create a new GenericByteViewArray
from the provided parts, returning an error on failure
§Errors
views.len() != nulls.len()
- ByteViewType::validate fails
sourcepub unsafe fn new_unchecked(
views: ScalarBuffer<u128>,
buffers: Vec<Buffer>,
nulls: Option<NullBuffer>,
) -> Self
pub unsafe fn new_unchecked( views: ScalarBuffer<u128>, buffers: Vec<Buffer>, nulls: Option<NullBuffer>, ) -> Self
Create a new GenericByteViewArray
from the provided parts, without validation
§Safety
Safe if Self::try_new
would not error
sourcepub fn new_null(len: usize) -> Self
pub fn new_null(len: usize) -> Self
Create a new GenericByteViewArray
of length len
where all values are null
sourcepub fn from_iter_values<Ptr, I>(iter: I) -> Self
pub fn from_iter_values<Ptr, I>(iter: I) -> Self
Creates a GenericByteViewArray
based on an iterator of values without nulls
sourcepub fn into_parts(self) -> (ScalarBuffer<u128>, Vec<Buffer>, Option<NullBuffer>)
pub fn into_parts(self) -> (ScalarBuffer<u128>, Vec<Buffer>, Option<NullBuffer>)
Deconstruct this array into its constituent parts
sourcepub fn views(&self) -> &ScalarBuffer<u128>
pub fn views(&self) -> &ScalarBuffer<u128>
Returns the views buffer
sourcepub fn data_buffers(&self) -> &[Buffer]
pub fn data_buffers(&self) -> &[Buffer]
Returns the buffers storing string data
sourcepub unsafe fn value_unchecked(&self, idx: usize) -> &T::Native
pub unsafe fn value_unchecked(&self, idx: usize) -> &T::Native
Returns the element at index i
§Safety
Caller is responsible for ensuring that the index is within the bounds of the array
sourcepub unsafe fn inline_value(view: &u128, len: usize) -> &[u8] ⓘ
pub unsafe fn inline_value(view: &u128, len: usize) -> &[u8] ⓘ
Returns the inline value of the view.
§Safety
- The
view
must be a valid element fromSelf::views()
that adheres to the view layout. - The
len
must be the length of the inlined value. It should never be larger than 12.
sourcepub fn slice(&self, offset: usize, length: usize) -> Self
pub fn slice(&self, offset: usize, length: usize) -> Self
Returns a zero-copy slice of this array with the indicated offset and length.
sourcepub fn gc(&self) -> Self
pub fn gc(&self) -> Self
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
.
sourcepub unsafe fn compare_unchecked(
left: &GenericByteViewArray<T>,
left_idx: usize,
right: &GenericByteViewArray<T>,
right_idx: usize,
) -> Ordering
pub unsafe fn compare_unchecked( left: &GenericByteViewArray<T>, left_idx: usize, right: &GenericByteViewArray<T>, right_idx: usize, ) -> Ordering
Comparing 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
source§impl GenericByteViewArray<BinaryViewType>
impl GenericByteViewArray<BinaryViewType>
sourcepub fn to_string_view(self) -> Result<StringViewArray, ArrowError>
pub fn to_string_view(self) -> Result<StringViewArray, ArrowError>
Convert the BinaryViewArray
to StringViewArray
If items not utf8 data, validate will fail and error returned.
sourcepub unsafe fn to_string_view_unchecked(self) -> StringViewArray
pub unsafe fn to_string_view_unchecked(self) -> StringViewArray
Convert the BinaryViewArray
to StringViewArray
§Safety
Caller is responsible for ensuring that items in array are utf8 data.
source§impl GenericByteViewArray<StringViewType>
impl GenericByteViewArray<StringViewType>
sourcepub fn to_binary_view(self) -> BinaryViewArray
pub fn to_binary_view(self) -> BinaryViewArray
Convert the StringViewArray
to BinaryViewArray
Trait Implementations§
source§impl<T: ByteViewType + ?Sized> Array for GenericByteViewArray<T>
impl<T: ByteViewType + ?Sized> Array for GenericByteViewArray<T>
source§fn slice(&self, offset: usize, length: usize) -> ArrayRef
fn slice(&self, offset: usize, length: usize) -> ArrayRef
source§fn offset(&self) -> usize
fn offset(&self) -> usize
0
. Read moresource§fn nulls(&self) -> Option<&NullBuffer>
fn nulls(&self) -> Option<&NullBuffer>
source§fn get_buffer_memory_size(&self) -> usize
fn get_buffer_memory_size(&self) -> usize
source§fn get_array_memory_size(&self) -> usize
fn get_array_memory_size(&self) -> usize
get_buffer_memory_size()
and
includes the overhead of the data structures that contain the pointers to the various buffers.source§fn logical_nulls(&self) -> Option<NullBuffer>
fn logical_nulls(&self) -> Option<NullBuffer>
NullBuffer
that represents the logical
null values of this array, if any. Read moresource§fn null_count(&self) -> usize
fn null_count(&self) -> usize
source§fn is_nullable(&self) -> bool
fn is_nullable(&self) -> bool
false
if the array is guaranteed to not contain any logical nulls Read moresource§impl<'a, T: ByteViewType + ?Sized> ArrayAccessor for &'a GenericByteViewArray<T>
impl<'a, T: ByteViewType + ?Sized> ArrayAccessor for &'a GenericByteViewArray<T>
source§impl<T: ByteViewType + ?Sized> Clone for GenericByteViewArray<T>
impl<T: ByteViewType + ?Sized> Clone for GenericByteViewArray<T>
source§impl<T: ByteViewType + ?Sized> Debug for GenericByteViewArray<T>
impl<T: ByteViewType + ?Sized> Debug for GenericByteViewArray<T>
source§impl<FROM, V> From<&GenericByteArray<FROM>> for GenericByteViewArray<V>where
FROM: ByteArrayType,
FROM::Offset: OffsetSizeTrait + ToPrimitive,
V: ByteViewType<Native = FROM::Native>,
impl<FROM, V> From<&GenericByteArray<FROM>> for GenericByteViewArray<V>where
FROM: ByteArrayType,
FROM::Offset: OffsetSizeTrait + ToPrimitive,
V: ByteViewType<Native = FROM::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.
source§fn from(byte_array: &GenericByteArray<FROM>) -> Self
fn from(byte_array: &GenericByteArray<FROM>) -> Self
source§impl<T: ByteViewType + ?Sized> From<ArrayData> for GenericByteViewArray<T>
impl<T: ByteViewType + ?Sized> From<ArrayData> for GenericByteViewArray<T>
source§impl<T: ByteViewType + ?Sized> From<GenericByteViewArray<T>> for ArrayData
impl<T: ByteViewType + ?Sized> From<GenericByteViewArray<T>> for ArrayData
source§fn from(array: GenericByteViewArray<T>) -> Self
fn from(array: GenericByteViewArray<T>) -> Self
source§impl<'a, Ptr, T> FromIterator<&'a Option<Ptr>> for GenericByteViewArray<T>
impl<'a, Ptr, T> FromIterator<&'a Option<Ptr>> for GenericByteViewArray<T>
source§impl<Ptr, T: ByteViewType + ?Sized> FromIterator<Option<Ptr>> for GenericByteViewArray<T>
impl<Ptr, T: ByteViewType + ?Sized> FromIterator<Option<Ptr>> for GenericByteViewArray<T>
source§impl<'a, T: ByteViewType + ?Sized> IntoIterator for &'a GenericByteViewArray<T>
impl<'a, T: ByteViewType + ?Sized> IntoIterator for &'a GenericByteViewArray<T>
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>
impl<T> UnwindSafe for GenericByteViewArray<T>where
T: UnwindSafe + ?Sized,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)