Skip to content

Commit

Permalink
Add append_option support to decimal builders (#2225)
Browse files Browse the repository at this point in the history
* add append_option support to decimal builders

* fix linting

* pr comments
  • Loading branch information
Brian Phillips committed Jul 29, 2022
1 parent 41d96b2 commit 561b14c
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions arrow/src/array/builder/decimal_builder.rs
Expand Up @@ -117,6 +117,18 @@ impl Decimal128Builder {
self.builder.append_null()
}

/// Appends an `Option<impl Into<i128>>` into the builder.
#[inline]
pub fn append_option(&mut self, value: Option<impl Into<i128>>) -> 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(
Expand Down Expand Up @@ -219,6 +231,18 @@ impl Decimal256Builder {
self.builder.append_null()
}

/// Appends an `Option<&Decimal256>` 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(
Expand Down Expand Up @@ -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::<i128>).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());
}
Expand Down Expand Up @@ -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());

Expand Down

0 comments on commit 561b14c

Please sign in to comment.