Module arrow_array::builder
source · Expand description
Defines push-based APIs for constructing arrays
Basic Usage
Builders can be used to build simple, non-nested arrays
let mut a = Int32Builder::new();
a.append_value(1);
a.append_null();
a.append_value(2);
let a = a.finish();
assert_eq!(a, PrimitiveArray::from(vec![Some(1), None, Some(2)]));
let mut a = StringBuilder::new();
a.append_value("foo");
a.append_value("bar");
a.append_null();
let a = a.finish();
assert_eq!(a, StringArray::from_iter([Some("foo"), Some("bar"), None]));
Nested Usage
Builders can also be used to build more complex nested arrays, such as lists
let mut a = ListBuilder::new(Int32Builder::new());
// [1, 2]
a.values().append_value(1);
a.values().append_value(2);
a.append(true);
// null
a.append(false);
// []
a.append(true);
// [3, null]
a.values().append_value(3);
a.values().append_null();
a.append(true);
// [[1, 2], null, [], [3, null]]
let a = a.finish();
assert_eq!(a, ListArray::from_iter_primitive::<Int32Type, _, _>([
Some(vec![Some(1), Some(2)]),
None,
Some(vec![]),
Some(vec![Some(3), None])]
))
Custom Builders
It is common to have a collection of statically defined Rust types that you want to convert to Arrow arrays.
An example of doing so is below
/// A custom row representation
struct MyRow {
i32: i32,
optional_i32: Option<i32>,
string: Option<String>,
i32_list: Option<Vec<Option<i32>>>,
}
/// Converts `Vec<Row>` into `StructArray`
#[derive(Debug, Default)]
struct MyRowBuilder {
i32: Int32Builder,
string: StringBuilder,
i32_list: ListBuilder<Int32Builder>,
}
impl MyRowBuilder {
fn append(&mut self, row: &MyRow) {
self.i32.append_value(row.i32);
self.string.append_option(row.string.as_ref());
self.i32_list.append_option(row.i32_list.as_ref().map(|x| x.iter().copied()));
}
/// Note: returns StructArray to allow nesting within another array if desired
fn finish(&mut self) -> StructArray {
let i32 = Arc::new(self.i32.finish()) as ArrayRef;
let i32_field = Arc::new(Field::new("i32", DataType::Int32, false));
let string = Arc::new(self.string.finish()) as ArrayRef;
let string_field = Arc::new(Field::new("i32", DataType::Utf8, false));
let i32_list = Arc::new(self.i32_list.finish()) as ArrayRef;
let value_field = Arc::new(Field::new("item", DataType::Int32, true));
let i32_list_field = Arc::new(Field::new("i32_list", DataType::List(value_field), true));
StructArray::from(vec![
(i32_field, i32),
(string_field, string),
(i32_list_field, i32_list),
])
}
}
impl<'a> Extend<&'a MyRow> for MyRowBuilder {
fn extend<T: IntoIterator<Item = &'a MyRow>>(&mut self, iter: T) {
iter.into_iter().for_each(|row| self.append(row));
}
}
/// Converts a slice of [`MyRow`] to a [`RecordBatch`]
fn rows_to_batch(rows: &[MyRow]) -> RecordBatch {
let mut builder = MyRowBuilder::default();
builder.extend(rows);
RecordBatch::from(&builder.finish())
}
Structs
- Builder for
BooleanBuffer
- Builder for
BooleanArray
- Builder for creating a Buffer object.
- Builder for
FixedSizeBinaryArray
- Builder for
FixedSizeListArray
- Builder for
GenericByteArray
- Builder for
DictionaryArray
ofGenericByteArray
- Builder for
RunArray
ofGenericByteArray
- Builder for
GenericListArray
- Builder for
MapArray
- Builder for
NullArray
- Builder for
PrimitiveArray
- Builder for
DictionaryArray
ofPrimitiveArray
- Builder for
RunArray
ofPrimitiveArray
- Builder for
StructArray
- Builder for
UnionArray
Traits
- Trait for dealing with different array builders at runtime
Functions
- Returns a builder with capacity
capacity
that corresponds to the datatypeDataType
This function is useful to construct arrays from an arbitrary vectors with known/expected schema.
Type Definitions
- Builder for
BinaryArray
- Builder for
DictionaryArray
ofBinaryArray
- Builder for
RunArray
ofBinaryArray
- Buffer builder for 32-bit date type.
- A 32-bit date array builder.
- Buffer builder for 64-bit date type.
- A 64-bit date array builder.
- Buffer builder for 128-bit decimal type.
- A decimal 128 array builder
- Buffer builder for 256-bit decimal type.
- A decimal 256 array builder
- Buffer builder for elaspsed time of microseconds unit.
- An elapsed time in microseconds array builder.
- Buffer builder for elaspsed time of milliseconds unit.
- An elapsed time in milliseconds array builder.
- Buffer builder for elaspsed time of nanoseconds unit.
- An elapsed time in nanoseconds array builder.
- Buffer builder for elaspsed time of second unit.
- An elapsed time in seconds array builder.
- Buffer builder for 16-bit floating point type.
- A 16-bit floating point array builder.
- Buffer builder for 32-bit floating point type.
- A 32-bit floating point array builder.
- Buffer builder for 64-bit floating point type.
- A 64-bit floating point array builder.
- Array builder for
GenericBinaryArray
- Array builder for
GenericStringArray
- Buffer builder for signed 8-bit integer type.
- A signed 8-bit integer array builder.
- Buffer builder for signed 16-bit integer type.
- A signed 16-bit integer array builder.
- Buffer builder for signed 32-bit integer type.
- A signed 32-bit integer array builder.
- Buffer builder for signed 64-bit integer type.
- A signed 64-bit integer array builder.
- Buffer builder for “calendar” interval in days and milliseconds.
- A “calendar” interval in days and milliseconds array builder.
- Buffer builder “calendar” interval in months, days, and nanoseconds.
- A “calendar” interval in months, days, and nanoseconds array builder.
- Buffer builder for “calendar” interval in months.
- A “calendar” interval in months array builder.
- Builder for
LargeBinaryArray
- Builder for
DictionaryArray
ofLargeBinaryArray
- Builder for
RunArray
ofLargeBinaryArray
- Builder for
LargeListArray
- Builder for
LargeStringArray
- Builder for
DictionaryArray
ofLargeStringArray
- Builder for
RunArray
ofLargeStringArray
- Builder for
ListArray
- Builder for
StringArray
- Builder for
DictionaryArray
ofStringArray
- Builder for
RunArray
ofStringArray
- Buffer builder for 32-bit elaspsed time since midnight of millisecond unit.
- A 32-bit elaspsed time in milliseconds array builder.
- Buffer builder for 32-bit elaspsed time since midnight of second unit.
- A 32-bit elaspsed time in seconds array builder.
- Buffer builder for 64-bit elaspsed time since midnight of microsecond unit.
- A 64-bit elaspsed time in microseconds array builder.
- Buffer builder for 64-bit elaspsed time since midnight of nanosecond unit.
- A 64-bit elaspsed time in nanoseconds array builder.
- Buffer builder for timestamp type of microsecond unit.
- A timestamp microsecond array builder.
- Buffer builder for timestamp type of millisecond unit.
- A timestamp millisecond array builder.
- Buffer builder for timestamp type of nanosecond unit.
- A timestamp nanosecond array builder.
- Buffer builder for timestamp type of second unit.
- A timestamp second array builder.
- Buffer builder for usigned 8-bit integer type.
- An usigned 8-bit integer array builder.
- Buffer builder for usigned 16-bit integer type.
- An usigned 16-bit integer array builder.
- Buffer builder for usigned 32-bit integer type.
- An usigned 32-bit integer array builder.
- Buffer builder for usigned 64-bit integer type.
- An usigned 64-bit integer array builder.