pub fn decimal_to_decimal(
    from: &PrimitiveArray<i128>,
    to_precision: usize,
    to_scale: usize,
) -> PrimitiveArray<i128> {
    let (from_precision, from_scale) =
        if let DataType::Decimal(p, s) = from.data_type().to_logical_type() {
            (*p, *s)
        } else {
            panic!("internal error: i128 is always a decimal")
        };

    if to_scale == from_scale && to_precision >= from_precision {
        // fast path
        return from.clone().to(DataType::Decimal(to_precision, to_scale));
    }
    // todo: other fast paths include increasing scale and precision by so that
    // a number will never overflow (validity is preserved)

    if from_scale > to_scale {
        let factor = 10_i128.pow((from_scale - to_scale) as u32);
        decimal_to_decimal_impl(
            from,
            |x: i128| x.checked_div(factor),
            to_precision,
            to_scale,
        )
    } else {
        let factor = 10_i128.pow((to_scale - from_scale) as u32);
        decimal_to_decimal_impl(
            from,
            |x: i128| x.checked_mul(factor),
            to_precision,
            to_scale,
        )
    }
}
/// Returns a [`PrimitiveArray<i128>`] with the casted values. Values are `None` on overflow
Returns a [`PrimitiveArray`] with the casted values. Values are `None` on overflow
Returns a [ ` PrimitiveArray ` ] with the casted values . Values are ` None ` on overflow
pub fn decimal_to_decimal(
    from: &PrimitiveArray<i128>,
    to_precision: usize,
    to_scale: usize,
) -> PrimitiveArray<i128> {
    let (from_precision, from_scale) =
        if let DataType::Decimal(p, s) = from.data_type().to_logical_type() {
            (*p, *s)
        } else {
            panic!("internal error: i128 is always a decimal")
        };
    if to_scale == from_scale && to_precision >= from_precision {
        // fast path
        return from.clone().to(DataType::Decimal(to_precision, to_scale));
    }
    // todo: other fast paths include increasing scale and precision by so that
    // a number will never overflow (validity is preserved)
    if from_scale > to_scale {
        let factor = 10_i128.pow((from_scale - to_scale) as u32);
        decimal_to_decimal_impl(
            from,
            |x: i128| x.checked_div(factor),
            to_precision,
            to_scale,
        )
    } else {
        let factor = 10_i128.pow((to_scale - from_scale) as u32);
        decimal_to_decimal_impl(
            from,
            |x: i128| x.checked_mul(factor),
            to_precision,
            to_scale,
        )
    }
}
pub fn decimal_to_decimal ( from : & PrimitiveArray < i128 > , to_precision : usize , to_scale : usize , ) -> PrimitiveArray < i128 > { let ( from_precision , from_scale ) = if let DataType :: Decimal ( p , s ) = from . data_type ( ) . to_logical_type ( ) { ( * p , * s ) } else { panic ! ( " " ) } ; if to_scale == from_scale && to_precision >= from_precision { // fast path return from . clone ( ) . to ( DataType :: Decimal ( to_precision , to_scale ) ) ; } // todo: other fast paths include increasing scale and precision by so that // a number will never overflow (validity is preserved) if from_scale > to_scale { let factor = 10_i128 . pow ( ( from_scale - to_scale ) as u32 ) ; decimal_to_decimal_impl ( from , | x : i128 | x . checked_div ( factor ) , to_precision , to_scale , ) } else { let factor = 10_i128 . pow ( ( to_scale - from_scale ) as u32 ) ; decimal_to_decimal_impl ( from , | x : i128 | x . checked_mul ( factor ) , to_precision , to_scale , ) } }
Returns a [`PrimitiveArray<i128>`] with the casted values.
Returns a [ ` PrimitiveArray<i128 > ` ] with the casted values .
from
&PrimitiveArray<i128>
to_precision
usize
to_scale
usize
from
&PrimitiveArray<i128>
to_precision
usize
to_scale
usize