Struct arrow2::array::BinaryArray
source · pub struct BinaryArray<O: Offset> { /* private fields */ }
Expand description
A BinaryArray
is Arrow’s semantically equivalent of an immutable Vec<Option<Vec<u8>>>
.
It implements Array
.
The size of this struct is O(1)
, as all data is stored behind an std::sync::Arc
.
§Example
use arrow2::array::BinaryArray;
use arrow2::bitmap::Bitmap;
use arrow2::buffer::Buffer;
let array = BinaryArray::<i32>::from([Some([1, 2].as_ref()), None, Some([3].as_ref())]);
assert_eq!(array.value(0), &[1, 2]);
assert_eq!(array.iter().collect::<Vec<_>>(), vec![Some([1, 2].as_ref()), None, Some([3].as_ref())]);
assert_eq!(array.values_iter().collect::<Vec<_>>(), vec![[1, 2].as_ref(), &[], &[3]]);
// the underlying representation:
assert_eq!(array.values(), &Buffer::from(vec![1, 2, 3]));
assert_eq!(array.offsets().buffer(), &Buffer::from(vec![0, 2, 2, 3]));
assert_eq!(array.validity(), Some(&Bitmap::from([true, false, true])));
§Generic parameter
The generic parameter Offset
can only be i32
or i64
and tradeoffs maximum array length with
memory usage:
- the sum of lengths of all elements cannot exceed
Offset::MAX
- the total size of the underlying data is
array.len() * size_of::<Offset>() + sum of lengths of all elements
§Safety
The following invariants hold:
- Two consecutives
offsets
casted (as
) tousize
are valid slices ofvalues
. len
is equal tovalidity.len()
, when defined.
Implementations§
source§impl<O: Offset> BinaryArray<O>
impl<O: Offset> BinaryArray<O>
sourcepub fn try_new(
data_type: DataType,
offsets: OffsetsBuffer<O>,
values: Buffer<u8>,
validity: Option<Bitmap>
) -> Result<Self, Error>
pub fn try_new( data_type: DataType, offsets: OffsetsBuffer<O>, values: Buffer<u8>, validity: Option<Bitmap> ) -> Result<Self, Error>
Returns a BinaryArray
created from its internal representation.
§Errors
This function returns an error iff:
- The last offset is not equal to the values’ length.
- the validity’s length is not equal to
offsets.len()
. - The
data_type
’scrate::datatypes::PhysicalType
is not equal to eitherBinary
orLargeBinary
.
§Implementation
This function is O(1)
sourcepub fn from_slice<T: AsRef<[u8]>, P: AsRef<[T]>>(slice: P) -> Self
pub fn from_slice<T: AsRef<[u8]>, P: AsRef<[T]>>(slice: P) -> Self
Creates a new BinaryArray
from slices of &[u8]
.
sourcepub fn from<T: AsRef<[u8]>, P: AsRef<[Option<T>]>>(slice: P) -> Self
pub fn from<T: AsRef<[u8]>, P: AsRef<[Option<T>]>>(slice: P) -> Self
Creates a new BinaryArray
from a slice of optional &[u8]
.
sourcepub fn iter(&self) -> ZipValidity<&[u8], BinaryValueIter<'_, O>, BitmapIter<'_>> ⓘ
pub fn iter(&self) -> ZipValidity<&[u8], BinaryValueIter<'_, O>, BitmapIter<'_>> ⓘ
Returns an iterator of Option<&[u8]>
over every element of this array.
sourcepub fn values_iter(&self) -> BinaryValueIter<'_, O>
pub fn values_iter(&self) -> BinaryValueIter<'_, O>
Returns an iterator of &[u8]
over every element of this array, ignoring the validity
sourcepub unsafe fn value_unchecked(&self, i: usize) -> &[u8] ⓘ
pub unsafe fn value_unchecked(&self, i: usize) -> &[u8] ⓘ
sourcepub fn values(&self) -> &Buffer<u8>
pub fn values(&self) -> &Buffer<u8>
Returns the values of this BinaryArray
.
sourcepub fn offsets(&self) -> &OffsetsBuffer<O>
pub fn offsets(&self) -> &OffsetsBuffer<O>
Returns the offsets of this BinaryArray
.
sourcepub fn slice(&self, offset: usize, length: usize) -> Self
pub fn slice(&self, offset: usize, length: usize) -> Self
Creates a new BinaryArray
by slicing this BinaryArray
.
§Implementation
This function is O(1)
: all data will be shared between both arrays.
§Panics
iff offset + length > self.len()
.
sourcepub unsafe fn slice_unchecked(&self, offset: usize, length: usize) -> Self
pub unsafe fn slice_unchecked(&self, offset: usize, length: usize) -> Self
Creates a new BinaryArray
by slicing this BinaryArray
.
§Implementation
This function is O(1)
: all data will be shared between both arrays.
§Safety
The caller must ensure that offset + length <= self.len()
.
sourcepub fn boxed(self) -> Box<dyn Array>
pub fn boxed(self) -> Box<dyn Array>
Boxes self into a Box<dyn Array>
.
sourcepub fn arced(self) -> Arc<dyn Array>
pub fn arced(self) -> Arc<dyn Array>
Boxes self into a std::sync::Arc<dyn Array>
.
sourcepub fn with_validity(self, validity: Option<Bitmap>) -> Self
pub fn with_validity(self, validity: Option<Bitmap>) -> Self
sourcepub fn set_validity(&mut self, validity: Option<Bitmap>)
pub fn set_validity(&mut self, validity: Option<Bitmap>)
sourcepub fn into_mut(self) -> Either<Self, MutableBinaryArray<O>> ⓘ
pub fn into_mut(self) -> Either<Self, MutableBinaryArray<O>> ⓘ
Try to convert this BinaryArray
to a MutableBinaryArray
sourcepub fn new_empty(data_type: DataType) -> Self
pub fn new_empty(data_type: DataType) -> Self
Creates an empty BinaryArray
, i.e. whose .len
is zero.
sourcepub fn new_null(data_type: DataType, length: usize) -> Self
pub fn new_null(data_type: DataType, length: usize) -> Self
Creates an null BinaryArray
, i.e. whose .null_count() == .len()
.
sourcepub fn default_data_type() -> DataType
pub fn default_data_type() -> DataType
Returns the default DataType
, DataType::Binary
or DataType::LargeBinary
sourcepub fn new(
data_type: DataType,
offsets: OffsetsBuffer<O>,
values: Buffer<u8>,
validity: Option<Bitmap>
) -> Self
pub fn new( data_type: DataType, offsets: OffsetsBuffer<O>, values: Buffer<u8>, validity: Option<Bitmap> ) -> Self
Alias for unwrapping Self::try_new
sourcepub fn from_trusted_len_values_iter<T: AsRef<[u8]>, I: TrustedLen<Item = T>>(
iterator: I
) -> Self
pub fn from_trusted_len_values_iter<T: AsRef<[u8]>, I: TrustedLen<Item = T>>( iterator: I ) -> Self
Returns a BinaryArray
from an iterator of trusted length.
The BinaryArray
is guaranteed to not have a validity
sourcepub fn from_iter_values<T: AsRef<[u8]>, I: Iterator<Item = T>>(
iterator: I
) -> Self
pub fn from_iter_values<T: AsRef<[u8]>, I: Iterator<Item = T>>( iterator: I ) -> Self
Returns a new BinaryArray
from a Iterator
of &[u8]
.
The BinaryArray
is guaranteed to not have a validity
sourcepub unsafe fn from_trusted_len_iter_unchecked<I, P>(iterator: I) -> Self
pub unsafe fn from_trusted_len_iter_unchecked<I, P>(iterator: I) -> Self
Creates a BinaryArray
from an iterator of trusted length.
§Safety
The iterator must be TrustedLen
.
I.e. that size_hint().1
correctly reports its length.
sourcepub fn from_trusted_len_iter<I, P>(iterator: I) -> Self
pub fn from_trusted_len_iter<I, P>(iterator: I) -> Self
Creates a BinaryArray
from a TrustedLen
sourcepub unsafe fn try_from_trusted_len_iter_unchecked<E, I, P>(
iterator: I
) -> Result<Self, E>
pub unsafe fn try_from_trusted_len_iter_unchecked<E, I, P>( iterator: I ) -> Result<Self, E>
Creates a BinaryArray
from an falible iterator of trusted length.
§Safety
The iterator must be TrustedLen
.
I.e. that size_hint().1
correctly reports its length.
sourcepub fn try_from_trusted_len_iter<E, I, P>(iter: I) -> Result<Self, E>
pub fn try_from_trusted_len_iter<E, I, P>(iter: I) -> Result<Self, E>
Creates a BinaryArray
from an fallible iterator of trusted length.
Trait Implementations§
source§impl<O: Offset> Array for BinaryArray<O>
impl<O: Offset> Array for BinaryArray<O>
source§fn as_any(&self) -> &dyn Any
fn as_any(&self) -> &dyn Any
Any
, which enables downcasting to concrete types.source§fn as_any_mut(&mut self) -> &mut dyn Any
fn as_any_mut(&mut self) -> &mut dyn Any
Any
, which enables mutable downcasting to concrete types.source§fn len(&self) -> usize
fn len(&self) -> usize
Array
. Every array has a length corresponding to the number of
elements (slots).source§fn data_type(&self) -> &DataType
fn data_type(&self) -> &DataType
DataType
of the Array
. In combination with Array::as_any
, this can be
used to downcast trait objects (dyn Array
) to concrete arrays.source§impl<O: Clone + Offset> Clone for BinaryArray<O>
impl<O: Clone + Offset> Clone for BinaryArray<O>
source§fn clone(&self) -> BinaryArray<O>
fn clone(&self) -> BinaryArray<O>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<O: Offset> Debug for BinaryArray<O>
impl<O: Offset> Debug for BinaryArray<O>
source§impl<'a, O: Offset> From<GrowableBinary<'a, O>> for BinaryArray<O>
impl<'a, O: Offset> From<GrowableBinary<'a, O>> for BinaryArray<O>
source§fn from(val: GrowableBinary<'a, O>) -> Self
fn from(val: GrowableBinary<'a, O>) -> Self
source§impl<O: Offset> From<MutableBinaryArray<O>> for BinaryArray<O>
impl<O: Offset> From<MutableBinaryArray<O>> for BinaryArray<O>
source§fn from(other: MutableBinaryArray<O>) -> Self
fn from(other: MutableBinaryArray<O>) -> Self
source§impl<O: Offset> From<MutableBinaryValuesArray<O>> for BinaryArray<O>
impl<O: Offset> From<MutableBinaryValuesArray<O>> for BinaryArray<O>
source§fn from(other: MutableBinaryValuesArray<O>) -> Self
fn from(other: MutableBinaryValuesArray<O>) -> Self
source§impl<O: Offset, P: AsRef<[u8]>> FromIterator<Option<P>> for BinaryArray<O>
impl<O: Offset, P: AsRef<[u8]>> FromIterator<Option<P>> for BinaryArray<O>
source§impl<O: Offset> GenericBinaryArray<O> for BinaryArray<O>
impl<O: Offset> GenericBinaryArray<O> for BinaryArray<O>
source§impl<'a, O: Offset> IntoIterator for &'a BinaryArray<O>
impl<'a, O: Offset> IntoIterator for &'a BinaryArray<O>
§type IntoIter = ZipValidity<&'a [u8], ArrayValuesIter<'a, BinaryArray<O>>, BitmapIter<'a>>
type IntoIter = ZipValidity<&'a [u8], ArrayValuesIter<'a, BinaryArray<O>>, BitmapIter<'a>>
source§impl<O: Offset> PartialEq<&(dyn Array + 'static)> for BinaryArray<O>
impl<O: Offset> PartialEq<&(dyn Array + 'static)> for BinaryArray<O>
source§impl<O: Offset> PartialEq<BinaryArray<O>> for &dyn Array
impl<O: Offset> PartialEq<BinaryArray<O>> for &dyn Array
source§fn eq(&self, other: &BinaryArray<O>) -> bool
fn eq(&self, other: &BinaryArray<O>) -> bool
self
and other
values to be equal, and is used
by ==
.