Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add append_option support to decimal builders #2225

Merged
merged 3 commits into from Jul 29, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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<T>` into the builder.
This conversation was marked as resolved.
Show resolved Hide resolved
#[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<T>` into the builder.
This conversation was marked as resolved.
Show resolved Hide resolved
#[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