pub struct PrimitiveArray<T: NativeType> { /* private fields */ }
Expand description

A PrimitiveArray is Arrow’s semantically equivalent of an immutable Vec<Option<T>> where T is NativeType (e.g. i32). It implements Array.

One way to think about a PrimitiveArray is (DataType, Arc<Vec<T>>, Option<Arc<Vec<u8>>>) where:

  • the first item is the array’s logical type
  • the second is the immutable values
  • the third is the immutable validity (whether a value is null or not as a bitmap).

The size of this struct is O(1), as all data is stored behind an std::sync::Arc.

Example

use arrow2::array::PrimitiveArray;
use arrow2::bitmap::Bitmap;
use arrow2::buffer::Buffer;

let array = PrimitiveArray::from([Some(1i32), None, Some(10)]);
assert_eq!(array.value(0), 1);
assert_eq!(array.iter().collect::<Vec<_>>(), vec![Some(&1i32), None, Some(&10)]);
assert_eq!(array.values_iter().copied().collect::<Vec<_>>(), vec![1, 0, 10]);
// the underlying representation
assert_eq!(array.values(), &Buffer::from(vec![1i32, 0, 10]));
assert_eq!(array.validity(), Some(&Bitmap::from([true, false, true])));

Implementations

The canonical method to create a PrimitiveArray out of its internal components.

Implementation

This function is O(1).

Errors

This function errors iff:

  • The validity is not None and its length is different from values’s length
  • The data_type’s PhysicalType is not equal to [PhysicalType::Primitive(T::PRIMITIVE)]

Returns a new PrimitiveArray with a different logical type.

This function is useful to assign a different DataType to the array. Used to change the arrays’ logical type (see example).

Example
use arrow2::array::Int32Array;
use arrow2::datatypes::DataType;

let array = Int32Array::from(&[Some(1), None, Some(2)]).to(DataType::Date32);
assert_eq!(
   format!("{:?}", array),
   "Date32[1970-01-02, None, 1970-01-03]"
);
Panics

Panics iff the data_type’s PhysicalType is not equal to [PhysicalType::Primitive(T::PRIMITIVE)]

Creates a (non-null) PrimitiveArray from a vector of values. This function is O(1).

Examples
use arrow2::array::PrimitiveArray;

let array = PrimitiveArray::from_vec(vec![1, 2, 3]);
assert_eq!(format!("{:?}", array), "Int32[1, 2, 3]");

Returns an iterator over the values and validity, Option<&T>.

Returns an iterator of the values, &T, ignoring the arrays’ validity.

Returns the length of this array

The values Buffer. Values on null slots are undetermined (they can be anything).

Returns the optional validity.

Returns the arrays’ DataType.

Returns the value at slot i.

Equivalent to self.values()[i]. The value of a null slot is undetermined (it can be anything).

Panic

This function panics iff i >= self.len.

Returns the value at index i. The value on null slots is undetermined (it can be anything).

Safety

Caller must be sure that i < self.len()

Returns a clone of this PrimitiveArray sliced by an offset and length.

Implementation

This operation is O(1) as it amounts to increase two ref counts.

Examples
use arrow2::array::PrimitiveArray;

let array = PrimitiveArray::from_vec(vec![1, 2, 3]);
assert_eq!(format!("{:?}", array), "Int32[1, 2, 3]");
let sliced = array.slice(1, 1);
assert_eq!(format!("{:?}", sliced), "Int32[2]");
// note: `sliced` and `array` share the same memory region.
Panic

This function panics iff offset + length > self.len().

Returns a clone of this PrimitiveArray sliced by an offset and length.

Implementation

This operation is O(1) as it amounts to increase two ref counts.

Safety

The caller must ensure that offset + length <= self.len().

Returns this PrimitiveArray with a new validity.

Panics

This function panics iff validity.len() != self.len().

Sets the validity of this PrimitiveArray.

Panics

This function panics iff validity.len() != self.len().

Returns this PrimitiveArray with new values.

Panics

This function panics iff values.len() != self.len().

Update the values of this PrimitiveArray.

Panics

This function panics iff values.len() != self.len().

Applies a function f to the validity of this array.

This is an API to leverage clone-on-write

Panics

This function panics if the function f modifies the length of the Bitmap.

Returns an option of a mutable reference to the values of this PrimitiveArray.

Try to convert this PrimitiveArray to a MutablePrimitiveArray via copy-on-write semantics.

A PrimitiveArray is backed by a Buffer and Bitmap which are essentially Arc<Vec<_>>. This function returns a MutablePrimitiveArray (via std::sync::Arc::get_mut) iff both values and validity have not been cloned / are unique references to their underlying vectors.

This function is primarily used to re-use memory regions.

Returns a new empty (zero-length) PrimitiveArray.

Returns a new PrimitiveArray where all slots are null / None.

Creates a (non-null) PrimitiveArray from an iterator of values.

Implementation

This does not assume that the iterator has a known length.

Creates a (non-null) PrimitiveArray from a slice of values.

Implementation

This is essentially a memcopy and is thus O(N)

Creates a (non-null) PrimitiveArray from a TrustedLen of values.

Implementation

This does not assume that the iterator has a known length.

Creates a new PrimitiveArray from an iterator over values

Safety

The iterator must be TrustedLen. I.e. that size_hint().1 correctly reports its length.

Creates a PrimitiveArray from a TrustedLen of optional values.

Creates a PrimitiveArray from an iterator of optional values.

Safety

The iterator must be TrustedLen. I.e. that size_hint().1 correctly reports its length.

Boxes self into a Box<dyn Array>.

Boxes self into a std::sync::Arc<dyn Array>.

Alias for Self::try_new(..).unwrap().

Panics

This function errors iff:

Alias for Self::try_new(..).unwrap().

Trait Implementations

Converts itself to a reference of Any, which enables downcasting to concrete types.

Converts itself to a mutable reference of Any, which enables mutable downcasting to concrete types.

The length of the Array. Every array has a length corresponding to the number of elements (slots). Read more

The DataType of the Array. In combination with Array::as_any, this can be used to downcast trait objects (dyn Array) to concrete arrays. Read more

The validity of the Array: every array has an optional Bitmap that, when available specifies whether the array slot is valid or not (null). When the validity is None, all slots are valid. Read more

Slices the Array, returning a new Box<dyn Array>. Read more

Slices the Array, returning a new Box<dyn Array>. Read more

Clones this Array with a new new assigned bitmap. Read more

Clone a &dyn Array to an owned Box<dyn Array>.

whether the array is empty

The number of null slots on this Array. Read more

Returns whether slot i is null. Read more

Returns whether slot i is valid. Read more

Adds itself to rhs

Adds itself to rhs

Adds itself to rhs

Checked add

Checked add

Checked add

checked division

checked division

checked division

checked multiplication

checked multiplication

checked multiplication

checked remainder

checked remainder

checked subtraction

checked subtraction

checked subtraction

division

division

division

multiplication

multiplication

multiplication

Overflowing add

Overflowing add

overflowing multiplication

overflowing multiplication

overflowing subtraction

overflowing subtraction

remainder

remainder

Saturating add

Saturating add

Saturating add

saturating multiplication

saturating multiplication

saturating multiplication

saturarting subtraction

saturarting subtraction

saturarting subtraction

subtraction

subtraction

subtraction

Adds itself to rhs using wrapping addition

wrapping multiplication

wrapping subtraction

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Creates a value from an iterator. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.

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

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.