pub fn saturating_sub(
    lhs: &PrimitiveArray<i128>,
    rhs: &PrimitiveArray<i128>,
) -> Result<PrimitiveArray<i128>> {
    // Matching on both data types from both arrays. This match will be true
    // only when precision and scale from both arrays are the same, otherwise
    // it will return and ArrowError
    match (lhs.data_type(), rhs.data_type()) {
        (DataType::Decimal(lhs_p, lhs_s), DataType::Decimal(rhs_p, rhs_s)) => {
            if lhs_p == rhs_p && lhs_s == rhs_s {
                // Closure for the binary operation.
                let op = move |a, b| {
                    let res: i128 = a - b;
                    let max: i128 = max_value(*lhs_p);

                    match res {
                        res if res.abs() > max => {
                            if res > 0 {
                                max
                            } else {
                                -max
                            }
                        }
                        _ => res,
                    }
                };

                binary(lhs, rhs, lhs.data_type().clone(), op)
            } else {
                Err(ArrowError::InvalidArgumentError(
                    "Arrays must have the same precision and scale".to_string(),
                ))
            }
        }
        _ => Err(ArrowError::InvalidArgumentError(
            "Incorrect data type for the array".to_string(),
        )),
    }
}
/// Saturated subtraction of two decimal primitive arrays with the same
/// precision and scale. If the precision and scale is different, then an
/// InvalidArgumentError is returned. If the result from the sum is smaller
/// than the possible number with the selected precision then the resulted
/// number in the arrow array is the minimum number for the selected precision.
///
/// # Examples
/// ```
/// use arrow2::compute::arithmetics::decimal::sub::saturating_sub;
/// use arrow2::array::PrimitiveArray;
/// use arrow2::datatypes::DataType;
///
/// let a = PrimitiveArray::from(&vec![Some(-99000i128), Some(11100i128), None, Some(22200i128)]).to(DataType::Decimal(5, 2));
/// let b = PrimitiveArray::from(&vec![Some(01000i128), Some(22200i128), None, Some(11100i128)]).to(DataType::Decimal(5, 2));
///
/// let result = saturating_sub(&a, &b).unwrap();
/// let expected = PrimitiveArray::from(&vec![Some(-99999i128), Some(-11100i128), None, Some(11100i128)]).to(DataType::Decimal(5, 2));
///
/// assert_eq!(result, expected);
/// ```
Saturated subtraction of two decimal primitive arrays with the same
precision and scale. If the precision and scale is different, then an
InvalidArgumentError is returned. If the result from the sum is smaller
than the possible number with the selected precision then the resulted
number in the arrow array is the minimum number for the selected precision.
Saturated subtraction of two decimal primitive arrays with the same precision and scale . If the precision and scale is different then an InvalidArgumentError is returned . If the result from the sum is smaller than the possible number with the selected precision then the resulted number in the arrow array is the minimum number for the selected precision .
pub fn saturating_sub(
    lhs: &PrimitiveArray<i128>,
    rhs: &PrimitiveArray<i128>,
) -> Result<PrimitiveArray<i128>> {
    // Matching on both data types from both arrays. This match will be true
    // only when precision and scale from both arrays are the same, otherwise
    // it will return and ArrowError
    match (lhs.data_type(), rhs.data_type()) {
        (DataType::Decimal(lhs_p, lhs_s), DataType::Decimal(rhs_p, rhs_s)) => {
            if lhs_p == rhs_p && lhs_s == rhs_s {
                // Closure for the binary operation.
                let op = move |a, b| {
                    let res: i128 = a - b;
                    let max: i128 = max_value(*lhs_p);
                    match res {
                        res if res.abs() > max => {
                            if res > 0 {
                                max
                            } else {
                                -max
                            }
                        }
                        _ => res,
                    }
                };
                binary(lhs, rhs, lhs.data_type().clone(), op)
            } else {
                Err(ArrowError::InvalidArgumentError(
                    "Arrays must have the same precision and scale".to_string(),
                ))
            }
        }
        _ => Err(ArrowError::InvalidArgumentError(
            "Incorrect data type for the array".to_string(),
        )),
    }
}
pub fn saturating_sub ( lhs : & PrimitiveArray < i128 > , rhs : & PrimitiveArray < i128 > , ) -> Result < PrimitiveArray < i128 > > { // Matching on both data types from both arrays. This match will be true // only when precision and scale from both arrays are the same, otherwise // it will return and ArrowError match ( lhs . data_type ( ) , rhs . data_type ( ) ) { ( DataType :: Decimal ( lhs_p , lhs_s ) , DataType :: Decimal ( rhs_p , rhs_s ) ) => { if lhs_p == rhs_p && lhs_s == rhs_s { // Closure for the binary operation. let op = move | a , b | { let res : i128 = a - b ; let max : i128 = max_value ( * lhs_p ) ; match res { res if res . abs ( ) > max => { if res > 0 { max } else { - max } } _ => res , } } ; binary ( lhs , rhs , lhs . data_type ( ) . clone ( ) , op ) } else { Err ( ArrowError :: InvalidArgumentError ( " " . to_string ( ) , ) ) } } _ => Err ( ArrowError :: InvalidArgumentError ( " " . to_string ( ) , ) ) , } }
Saturated subtraction of two decimal primitive arrays with the same
precision and scale.
Saturated subtraction of two decimal primitive arrays with the same precision and scale .
lhs
&PrimitiveArray<i128>
rhs
&PrimitiveArray<i128>
examples
```
use arrow2::compute::arithmetics::decimal::sub::saturating_sub;
use arrow2::array::PrimitiveArray;
use arrow2::datatypes::DataType.





assert_eq!(result, expected);
```
` ` ` use arrow2 :: compute :: arithmetics :: decimal :: sub :: saturating_sub ; use arrow2 :: array :: PrimitiveArray ; use arrow2 :: datatypes :: DataType . assert_eq! ( result expected ) ; ` ` `