Struct arrow_array::builder::StringDictionaryBuilder
source · pub struct StringDictionaryBuilder<K>where
K: ArrowDictionaryKeyType,{ /* private fields */ }
Expand description
Array builder for DictionaryArray
that stores Strings. For example to map a set of byte indices
to String values. Note that the use of a HashMap
here will not scale to very large
arrays or result in an ordered dictionary.
// Create a dictionary array indexed by bytes whose values are Strings.
// It can thus hold up to 256 distinct string values.
let mut builder = StringDictionaryBuilder::<Int8Type>::new();
// The builder builds the dictionary value by value
builder.append("abc").unwrap();
builder.append_null();
builder.append("def").unwrap();
builder.append("def").unwrap();
builder.append("abc").unwrap();
let array = builder.finish();
assert_eq!(
array.keys(),
&Int8Array::from(vec![Some(0), None, Some(1), Some(1), Some(0)])
);
// Values are polymorphic and so require a downcast.
let av = array.values();
let ava: &StringArray = av.as_any().downcast_ref::<StringArray>().unwrap();
assert_eq!(ava.value(0), "abc");
assert_eq!(ava.value(1), "def");
Implementations
sourceimpl<K> StringDictionaryBuilder<K>where
K: ArrowDictionaryKeyType,
impl<K> StringDictionaryBuilder<K>where
K: ArrowDictionaryKeyType,
sourcepub fn with_capacity(
keys_capacity: usize,
value_capacity: usize,
string_capacity: usize
) -> Self
pub fn with_capacity(
keys_capacity: usize,
value_capacity: usize,
string_capacity: usize
) -> Self
Creates a new StringDictionaryBuilder
with the provided capacities
keys_capacity
: the number of keys, i.e. length of array to build
value_capacity
: the number of distinct dictionary values, i.e. size of dictionary
string_capacity
: the total number of bytes of all distinct strings in the dictionary
sourcepub fn new_with_dictionary(
keys_capacity: usize,
dictionary_values: &StringArray
) -> Result<Self, ArrowError>
pub fn new_with_dictionary(
keys_capacity: usize,
dictionary_values: &StringArray
) -> Result<Self, ArrowError>
Creates a new StringDictionaryBuilder
from a keys capacity and a dictionary
which is initialized with the given values.
The indices of those dictionary values are used as keys.
Example
let dictionary_values = StringArray::from(vec![None, Some("abc"), Some("def")]);
let mut builder = StringDictionaryBuilder::new_with_dictionary(3, &dictionary_values).unwrap();
builder.append("def").unwrap();
builder.append_null();
builder.append("abc").unwrap();
let dictionary_array = builder.finish();
let keys = dictionary_array.keys();
assert_eq!(keys, &Int16Array::from(vec![Some(2), None, Some(1)]));
sourceimpl<K> StringDictionaryBuilder<K>where
K: ArrowDictionaryKeyType,
impl<K> StringDictionaryBuilder<K>where
K: ArrowDictionaryKeyType,
sourcepub fn append(&mut self, value: impl AsRef<str>) -> Result<K::Native, ArrowError>
pub fn append(&mut self, value: impl AsRef<str>) -> Result<K::Native, ArrowError>
Append a primitive value to the array. Return an existing index if already present in the values array or a new index if the value is appended to the values array.
Returns an error if the new index would overflow the key type.
pub fn append_null(&mut self)
sourcepub fn finish(&mut self) -> DictionaryArray<K>
pub fn finish(&mut self) -> DictionaryArray<K>
Builds the DictionaryArray
and reset this builder.
Trait Implementations
sourceimpl<K> ArrayBuilder for StringDictionaryBuilder<K>where
K: ArrowDictionaryKeyType,
impl<K> ArrayBuilder for StringDictionaryBuilder<K>where
K: ArrowDictionaryKeyType,
sourcefn as_any_mut(&mut self) -> &mut dyn Any
fn as_any_mut(&mut self) -> &mut dyn Any
Returns the builder as an mutable Any
reference.
sourcefn into_box_any(self: Box<Self>) -> Box<dyn Any>
fn into_box_any(self: Box<Self>) -> Box<dyn Any>
Returns the boxed builder as a box of Any
.