From 915c9d46db3ad4ac5da79430df8e19049fc7c19a Mon Sep 17 00:00:00 2001 From: Brian Phillips Date: Fri, 29 Jul 2022 10:47:01 -0400 Subject: [PATCH 1/3] add append_option support to decimal builders --- arrow/src/array/builder/decimal_builder.rs | 37 +++++++++++++++++++--- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/arrow/src/array/builder/decimal_builder.rs b/arrow/src/array/builder/decimal_builder.rs index d015d3dcecd..c1c764bdd80 100644 --- a/arrow/src/array/builder/decimal_builder.rs +++ b/arrow/src/array/builder/decimal_builder.rs @@ -117,6 +117,18 @@ impl Decimal128Builder { self.builder.append_null() } + /// Appends an `Option` into the builder. + #[inline] + pub fn append_option(&mut self, value: Option>) -> Result<()> { + match value { + None => { + self.append_null(); + Ok(()) + }, + Some(value) => self.append_value(value), + } + } + /// Builds the `Decimal128Array` and reset this builder. pub fn finish(&mut self) -> Decimal128Array { Decimal128Array::from_fixed_size_binary_array( @@ -219,6 +231,18 @@ impl Decimal256Builder { self.builder.append_null() } + /// Appends an `Option` into the builder. + #[inline] + pub fn append_option(&mut self, value: Option<&Decimal256>) -> Result<()> { + match value { + None => { + self.append_null(); + Ok(()) + }, + Some(value) => self.append_value(value), + } + } + /// Builds the [`Decimal256Array`] and reset this builder. pub fn finish(&mut self) -> Decimal256Array { Decimal256Array::from_fixed_size_binary_array( @@ -246,11 +270,13 @@ mod tests { builder.append_value(8_887_000_000_i128).unwrap(); builder.append_null(); builder.append_value(-8_887_000_000_i128).unwrap(); + builder.append_option(None::).unwrap(); + builder.append_option(Some(8_887_000_000_i128)).unwrap(); let decimal_array: Decimal128Array = builder.finish(); assert_eq!(&DataType::Decimal(38, 6), decimal_array.data_type()); - assert_eq!(3, decimal_array.len()); - assert_eq!(1, decimal_array.null_count()); + assert_eq!(5, decimal_array.len()); + assert_eq!(2, decimal_array.null_count()); assert_eq!(32, decimal_array.value_offset(2)); assert_eq!(16, decimal_array.value_length()); } @@ -296,11 +322,14 @@ mod tests { let value = Decimal256::try_new_from_bytes(40, 6, bytes.as_slice()).unwrap(); builder.append_value(&value).unwrap(); + builder.append_option(None::<&Decimal256>).unwrap(); + builder.append_option(Some(&value)).unwrap(); + let decimal_array: Decimal256Array = builder.finish(); assert_eq!(&DataType::Decimal256(40, 6), decimal_array.data_type()); - assert_eq!(4, decimal_array.len()); - assert_eq!(1, decimal_array.null_count()); + assert_eq!(6, decimal_array.len()); + assert_eq!(2, decimal_array.null_count()); assert_eq!(64, decimal_array.value_offset(2)); assert_eq!(32, decimal_array.value_length()); From 0067761c5e37c3bdf54194dde4691301ca5e6f98 Mon Sep 17 00:00:00 2001 From: Brian Phillips Date: Fri, 29 Jul 2022 12:20:52 -0400 Subject: [PATCH 2/3] fix linting --- arrow/src/array/builder/decimal_builder.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arrow/src/array/builder/decimal_builder.rs b/arrow/src/array/builder/decimal_builder.rs index c1c764bdd80..0b79850ac6e 100644 --- a/arrow/src/array/builder/decimal_builder.rs +++ b/arrow/src/array/builder/decimal_builder.rs @@ -124,7 +124,7 @@ impl Decimal128Builder { None => { self.append_null(); Ok(()) - }, + } Some(value) => self.append_value(value), } } @@ -238,7 +238,7 @@ impl Decimal256Builder { None => { self.append_null(); Ok(()) - }, + } Some(value) => self.append_value(value), } } From 67407e9eeea248b5265770d2ea4ce52b5b10b0db Mon Sep 17 00:00:00 2001 From: Brian Phillips Date: Fri, 29 Jul 2022 14:00:58 -0400 Subject: [PATCH 3/3] pr comments --- arrow/src/array/builder/decimal_builder.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arrow/src/array/builder/decimal_builder.rs b/arrow/src/array/builder/decimal_builder.rs index 0b79850ac6e..98f1088cf9a 100644 --- a/arrow/src/array/builder/decimal_builder.rs +++ b/arrow/src/array/builder/decimal_builder.rs @@ -117,7 +117,7 @@ impl Decimal128Builder { self.builder.append_null() } - /// Appends an `Option` into the builder. + /// Appends an `Option>` into the builder. #[inline] pub fn append_option(&mut self, value: Option>) -> Result<()> { match value { @@ -231,7 +231,7 @@ impl Decimal256Builder { self.builder.append_null() } - /// Appends an `Option` into the builder. + /// Appends an `Option<&Decimal256>` into the builder. #[inline] pub fn append_option(&mut self, value: Option<&Decimal256>) -> Result<()> { match value {