Skip to content

Commit

Permalink
Add new API to validate the precision for decimal array (#3242)
Browse files Browse the repository at this point in the history
* support new api to validate decimal array: if value is overflow with the specified precision, will be changed to None

* Update arrow-array/src/array/primitive_array.rs

Co-authored-by: Raphael Taylor-Davies <1781103+tustvold@users.noreply.github.com>

Co-authored-by: Raphael Taylor-Davies <1781103+tustvold@users.noreply.github.com>
  • Loading branch information
liukun4515 and tustvold authored Nov 30, 2022
1 parent 54587e0 commit fdc3457
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions arrow-array/src/array/primitive_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,14 @@ impl<T: DecimalType + ArrowPrimitiveType> PrimitiveArray<T> {
})
}

/// Validates the Decimal Array, if the value of slot is overflow for the specified precision, and
/// will be casted to Null
pub fn null_if_overflow_precision(&self, precision: u8) -> Self {
self.unary_opt::<_, T>(|v| {
(T::validate_decimal_precision(v, precision).is_ok()).then_some(v)
})
}

/// Returns [`Self::value`] formatted as a string
pub fn value_as_string(&self, row: usize) -> String {
T::format_decimal(self.value(row), self.precision(), self.scale())
Expand Down Expand Up @@ -2055,6 +2063,15 @@ mod tests {
.unwrap();
}

#[test]
fn test_decimal_array_set_null_if_overflow_with_precision() {
let array =
Decimal128Array::from(vec![Some(123456), Some(123), None, Some(123456)]);
let result = array.null_if_overflow_precision(5);
let expected = Decimal128Array::from(vec![None, Some(123), None, None]);
assert_eq!(result, expected);
}

#[test]
fn test_decimal256_iter() {
let mut builder = Decimal256Builder::with_capacity(30);
Expand Down

0 comments on commit fdc3457

Please sign in to comment.