Skip to content

Commit

Permalink
Implement StringBuilder::append_option (#601)
Browse files Browse the repository at this point in the history
* Implement StringBuilder::append_option

* Add another null append tesT
  • Loading branch information
mzeitlin11 authored and alamb committed Jul 25, 2021
1 parent 8835adc commit 096129d
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions arrow/src/array/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,16 @@ impl<OffsetSize: StringOffsetSizeTrait> GenericStringBuilder<OffsetSize> {
self.append(false)
}

/// Append an `Option` value to the array.
#[inline]
pub fn append_option(&mut self, value: Option<impl AsRef<str>>) -> Result<()> {
match value {
None => self.append_null()?,
Some(v) => self.append_value(v)?,
};
Ok(())
}

/// Builds the `StringArray` and reset this builder.
pub fn finish(&mut self) -> GenericStringArray<OffsetSize> {
GenericStringArray::<OffsetSize>::from(self.builder.finish())
Expand Down Expand Up @@ -2991,6 +3001,23 @@ mod tests {
assert_eq!(5, string_array.value_length(2));
}

#[test]
fn test_string_array_builder_append_option() {
let mut builder = StringBuilder::new(20);
builder.append_option(Some("hello")).unwrap();
builder.append_option(None::<&str>).unwrap();
builder.append_option(None::<String>).unwrap();
builder.append_option(Some("world")).unwrap();

let string_array = builder.finish();

assert_eq!(4, string_array.len());
assert_eq!("hello", string_array.value(0));
assert!(string_array.is_null(1));
assert!(string_array.is_null(2));
assert_eq!("world", string_array.value(3));
}

#[test]
fn test_struct_array_builder() {
let string_builder = StringBuilder::new(4);
Expand Down

0 comments on commit 096129d

Please sign in to comment.