This is an automated email from the ASF dual-hosted git repository.
alamb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
The following commit(s) were added to refs/heads/master by this push:
new 1d6c374 Implement `StringBuilder::append_option` (#601)
1d6c374 is described below
commit 1d6c37452f74a6443d269d0bdfc2c3738130fd5e
Author: Matthew Zeitlin <[email protected]>
AuthorDate: Sun Jul 25 08:25:14 2021 -0400
Implement `StringBuilder::append_option` (#601)
* Implement StringBuilder::append_option
* Add another null append tesT
---
arrow/src/array/builder.rs | 27 +++++++++++++++++++++++++++
1 file changed, 27 insertions(+)
diff --git a/arrow/src/array/builder.rs b/arrow/src/array/builder.rs
index f226df4..aa880ef 100644
--- a/arrow/src/array/builder.rs
+++ b/arrow/src/array/builder.rs
@@ -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())
@@ -2992,6 +3002,23 @@ mod tests {
}
#[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);
let int_builder = Int32Builder::new(4);