Struct re_sdk::external::arrow::array::StructBuilder
pub struct StructBuilder {
fields: Fields,
field_builders: Vec<Box<dyn ArrayBuilder>>,
null_buffer_builder: NullBufferBuilder,
}
Expand description
Builder for StructArray
Note that callers should make sure that methods of all the child field builders are properly called to maintain the consistency of the data structure.
Handling arrays with complex layouts, such as List<Struct<List<Struct>>>
, in Rust can be challenging due to its strong typing system.
To construct a collection builder (ListBuilder
, LargeListBuilder
, or MapBuilder
) using make_builder
, multiple calls are required. This complexity arises from the recursive approach utilized by StructBuilder::from_fields
.
Initially, StructBuilder::from_fields
invokes make_builder
, which returns a Box<dyn ArrayBuilder>
. To obtain the specific collection builder, one must first use StructBuilder::field_builder
to get a Collection<[Box<dyn ArrayBuilder>]>
. Subsequently, the values()
result from this operation can be downcast to the desired builder type.
For example, when working with ListBuilder
, you would first call StructBuilder::field_builder::<ListBuilder<Box<dyn ArrayBuilder>>>
and then downcast the Box<dyn ArrayBuilder>
to the specific StructBuilder
you need.
For a practical example see the code below:
use arrow_array::builder::{ArrayBuilder, ListBuilder, StringBuilder, StructBuilder};
use arrow_schema::{DataType, Field, Fields};
use std::sync::Arc;
// This is an example column that has a List<Struct<List<Struct>>> layout
let mut example_col = ListBuilder::new(StructBuilder::from_fields(
vec![Field::new(
"value_list",
DataType::List(Arc::new(Field::new(
"item",
DataType::Struct(Fields::from(vec![
Field::new("key", DataType::Utf8, true),
Field::new("value", DataType::Utf8, true),
])), //In this example we are trying to get to this builder and insert key/value pairs
true,
))),
true,
)],
0,
));
// We can obtain the StructBuilder without issues, because example_col was created with StructBuilder
let col_struct_builder: &mut StructBuilder = example_col.values();
// We can't obtain the ListBuilder<StructBuilder> with the expected generic types, because under the hood
// the StructBuilder was returned as a Box<dyn ArrayBuilder> and passed as such to the ListBuilder constructor
// This panics in runtime, even though we know that the builder is a ListBuilder<StructBuilder>.
// let sb = col_struct_builder
// .field_builder::<ListBuilder<StructBuilder>>(0)
// .as_mut()
// .unwrap();
//To keep in line with Rust's strong typing, we fetch a ListBuilder<Box<dyn ArrayBuilder>> from the column StructBuilder first...
let mut list_builder_option =
col_struct_builder.field_builder::<ListBuilder<Box<dyn ArrayBuilder>>>(0);
let list_builder = list_builder_option.as_mut().unwrap();
// ... and then downcast the key/value pair values to a StructBuilder
let struct_builder = list_builder
.values()
.as_any_mut()
.downcast_mut::<StructBuilder>()
.unwrap();
// We can now append values to the StructBuilder
let key_builder = struct_builder.field_builder::<StringBuilder>(0).unwrap();
key_builder.append_value("my key");
let value_builder = struct_builder.field_builder::<StringBuilder>(1).unwrap();
value_builder.append_value("my value");
struct_builder.append(true);
list_builder.append(true);
col_struct_builder.append(true);
example_col.append(true);
let array = example_col.finish();
println!("My array: {:?}", array);
Fields§
§fields: Fields
§field_builders: Vec<Box<dyn ArrayBuilder>>
§null_buffer_builder: NullBufferBuilder
Implementations§
§impl StructBuilder
impl StructBuilder
pub fn new(
fields: impl Into<Fields>,
field_builders: Vec<Box<dyn ArrayBuilder>>,
) -> StructBuilder
pub fn new( fields: impl Into<Fields>, field_builders: Vec<Box<dyn ArrayBuilder>>, ) -> StructBuilder
Creates a new StructBuilder
pub fn from_fields(fields: impl Into<Fields>, capacity: usize) -> StructBuilder
pub fn from_fields(fields: impl Into<Fields>, capacity: usize) -> StructBuilder
Creates a new StructBuilder
from Fields
and capacity
pub fn field_builder<T>(&mut self, i: usize) -> Option<&mut T>where
T: ArrayBuilder,
pub fn field_builder<T>(&mut self, i: usize) -> Option<&mut T>where
T: ArrayBuilder,
Returns a mutable reference to the child field builder at index i
.
Result will be None
if the input type T
provided doesn’t match the actual
field builder’s type.
pub fn num_fields(&self) -> usize
pub fn num_fields(&self) -> usize
Returns the number of fields for the struct this builder is building.
pub fn append(&mut self, is_valid: bool)
pub fn append(&mut self, is_valid: bool)
Appends an element (either null or non-null) to the struct. The actual elements should be appended for each child sub-array in a consistent way.
pub fn append_null(&mut self)
pub fn append_null(&mut self)
Appends a null element to the struct.
pub fn finish(&mut self) -> StructArray
pub fn finish(&mut self) -> StructArray
Builds the StructArray
and reset this builder.
pub fn finish_cloned(&self) -> StructArray
pub fn finish_cloned(&self) -> StructArray
Builds the StructArray
without resetting the builder.
pub fn validity_slice(&self) -> Option<&[u8]>
pub fn validity_slice(&self) -> Option<&[u8]>
Returns the current null buffer as a slice
Trait Implementations§
§impl ArrayBuilder for StructBuilder
impl ArrayBuilder for StructBuilder
§fn len(&self) -> usize
fn len(&self) -> usize
Returns the number of array slots in the builder.
Note that this always return the first child field builder’s length, and it is the caller’s responsibility to maintain the consistency that all the child field builder should have the equal number of elements.
§fn finish_cloned(&self) -> Arc<dyn Array>
fn finish_cloned(&self) -> Arc<dyn Array>
Builds the array without resetting the builder.
§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Returns the builder as a non-mutable Any
reference.
This is most useful when one wants to call non-mutable APIs on a specific builder
type. In this case, one can first cast this into a Any
, and then use
downcast_ref
to get a reference on the specific builder.
§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Returns the builder as a mutable Any
reference.
This is most useful when one wants to call mutable APIs on a specific builder
type. In this case, one can first cast this into a Any
, and then use
downcast_mut
to get a reference on the specific builder.
§fn into_box_any(self: Box<StructBuilder>) -> Box<dyn Any>
fn into_box_any(self: Box<StructBuilder>) -> Box<dyn Any>
Returns the boxed builder as a box of Any
.
Auto Trait Implementations§
impl Freeze for StructBuilder
impl !RefUnwindSafe for StructBuilder
impl Send for StructBuilder
impl Sync for StructBuilder
impl Unpin for StructBuilder
impl !UnwindSafe for StructBuilder
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> CheckedAs for T
impl<T> CheckedAs for T
source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
§impl<T> Conv for T
impl<T> Conv for T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request
source§impl<Src, Dst> LosslessTryInto<Dst> for Srcwhere
Dst: LosslessTryFrom<Src>,
impl<Src, Dst> LosslessTryInto<Dst> for Srcwhere
Dst: LosslessTryFrom<Src>,
source§fn lossless_try_into(self) -> Option<Dst>
fn lossless_try_into(self) -> Option<Dst>
source§impl<Src, Dst> LossyInto<Dst> for Srcwhere
Dst: LossyFrom<Src>,
impl<Src, Dst> LossyInto<Dst> for Srcwhere
Dst: LossyFrom<Src>,
source§fn lossy_into(self) -> Dst
fn lossy_into(self) -> Dst
source§impl<T> OverflowingAs for T
impl<T> OverflowingAs for T
source§fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
source§impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
source§fn overflowing_cast_from(src: Src) -> (Dst, bool)
fn overflowing_cast_from(src: Src) -> (Dst, bool)
§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.§impl<T> Pointable for T
impl<T> Pointable for T
source§impl<T> SaturatingAs for T
impl<T> SaturatingAs for T
source§fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
source§impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
source§fn saturating_cast_from(src: Src) -> Dst
fn saturating_cast_from(src: Src) -> Dst
§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.