11 major breaking releases

35.0.0 Mar 13, 2023
34.0.0 Feb 27, 2023
33.0.0 Feb 13, 2023
32.0.0 Jan 30, 2023
24.0.0 Oct 3, 2022

#56 in Algorithms

Download history 12872/week @ 2022-12-04 12322/week @ 2022-12-11 10357/week @ 2022-12-18 6752/week @ 2022-12-25 11891/week @ 2023-01-01 14242/week @ 2023-01-08 16869/week @ 2023-01-15 16831/week @ 2023-01-22 19784/week @ 2023-01-29 18583/week @ 2023-02-05 21460/week @ 2023-02-12 20428/week @ 2023-02-19 21201/week @ 2023-02-26 21013/week @ 2023-03-05 24711/week @ 2023-03-12 30282/week @ 2023-03-19

99,394 downloads per month
Used in 80 crates (14 directly)


29K SLoC

The central type in Apache Arrow are arrays, which are a known-length sequence of values all having the same type. This crate provides concrete implementations of each type, as well as an [Array] trait that can be used for type-erasure.

Downcasting an Array

Arrays are often passed around as a dynamically typed &dyn Array or [ArrayRef]. For example, RecordBatch stores columns as [ArrayRef].

Whilst these arrays can be passed directly to the compute, csv, json, etc... APIs, it is often the case that you wish to interact with the data directly.

This requires downcasting to the concrete type of the array:

# use arrow_array::{Array, Float32Array, Int32Array};

fn sum_int32(array: &dyn Array) -> i32 {
    let integers: &Int32Array = array.as_any().downcast_ref().unwrap();
    integers.iter().map(|val| val.unwrap_or_default()).sum()

// Note: the values for positions corresponding to nulls will be arbitrary
fn as_f32_slice(array: &dyn Array) -> &[f32] {

Additionally, there are convenient functions to do this casting such as [cast::as_primitive_array<T>] and [cast::as_string_array]:

# use arrow_array::Array;
# use arrow_array::cast::as_primitive_array;
# use arrow_array::types::Float32Type;

fn as_f32_slice(array: &dyn Array) -> &[f32] {
    // use as_primtive_array

Building an Array

Most [Array] implementations can be constructed directly from iterators or [Vec]

# use arrow_array::{Int32Array, ListArray, StringArray};
# use arrow_array::types::Int32Type;

Int32Array::from(vec![1, 2]);
Int32Array::from(vec![Some(1), None]);
Int32Array::from_iter([1, 2, 3, 4]);
Int32Array::from_iter([Some(1), Some(2), None, Some(4)]);

StringArray::from(vec!["foo", "bar"]);
StringArray::from(vec![Some("foo"), None]);
StringArray::from_iter([Some("foo"), None]);
StringArray::from_iter_values(["foo", "bar"]);

ListArray::from_iter_primitive::<Int32Type, _, _>([
    Some(vec![Some(1), None, Some(3)]),

Additionally ArrayBuilder implementations can be used to construct arrays with a push-based interface

# use arrow_array::Int16Array;
// Create a new builder with a capacity of 100
let mut builder = Int16Array::builder(100);

// Append a single primitive value

// Append a null value

// Append a slice of primitive values
builder.append_slice(&[2, 3, 4]);

// Build the array
let array = builder.finish();

    "The array has 5 values, counting the null value"

assert_eq!(2, array.value(2), "Get the value with index 2");

    &[3, 4],
    "Get slice of len 2 starting at idx 3"

Zero-Copy Slicing

Given an [Array] of arbitrary length, it is possible to create an owned slice of this data. Internally this just increments some ref-counts, and so is incredibly cheap

# use std::sync::Arc;
# use arrow_array::{ArrayRef, Int32Array};
let array = Arc::new(Int32Array::from_iter([1, 2, 3])) as ArrayRef;

// Slice with offset 1 and length 2
let sliced = array.slice(1, 2);
let ints = sliced.as_any().downcast_ref::<Int32Array>().unwrap();
assert_eq!(ints.values(), &[2, 3]);

Internal Representation

Internally, arrays are represented by one or several Buffer, the number and meaning of which depend on the array’s data type, as documented in the Arrow specification.

For example, the type [Int16Array] represents an array of 16-bit integers and consists of:

  • An optional NullBuffer identifying any null values
  • A contiguous Buffer of 16-bit integers

Similarly, the type [StringArray] represents an array of UTF-8 strings and consists of:

  • An optional NullBuffer identifying any null values
  • An offsets Buffer of 32-bit integers identifying valid UTF-8 sequences within the values buffer
  • A values Buffer of UTF-8 encoded string data


~469K SLoC