pola-rs/polars · error

adding decimals with different precisions/scales is not…

Error message

adding decimals with different precisions/scales is not supported here

What it means

`AnyValue::add` for Decimal values requires both operands to have identical precision and scale; otherwise the result's scale is ambiguous. Differing (precision, scale) triggers an `unimplemented!` panic because this low-level path performs no rescaling.

Solutions

  1. Cast both decimal operands to the same DataType::Decimal(precision, scale) before adding
  2. Standardize decimal scale at data-ingestion/parse time
  3. Use the Series-level arithmetic which handles decimal rescaling, instead of AnyValue-level addition

Example fix

// before
let sum = dec_10_2.add(&dec_10_0); // unimplemented! panic
// after
let dec_10_0_rescaled = dec_10_0.cast(&DataType::Decimal(Some(10), Some(2)));
let sum = dec_10_2.add(&dec_10_0_rescaled);
Defensive patterns

Strategy: validation

Validate before calling

fn can_add_decimals(l: &AnyValue, r: &AnyValue) -> bool {
    match (l, r) {
        (AnyValue::Decimal(_, lp, ls), AnyValue::Decimal(_, rp, rs)) => (lp, ls) == (rp, rs),
        _ => true,
    }
}

Type guard

fn same_decimal_shape(l: &AnyValue, r: &AnyValue) -> bool {
    matches!((l, r), (AnyValue::Decimal(_, lp, ls), AnyValue::Decimal(_, rp, rs)) if (lp, ls) == (rp, rs))
}

Prevention

When it happens

Trigger: Using the `+` operator on two AnyValue::Decimal values whose (precision, scale) tuples differ — e.g. `Decimal(100, 10, 2) + Decimal(5, 10, 0)`.

Common situations: Adding decimal columns that were constructed with different scales (e.g. parsed from CSV strings with different decimal places, or defined with different dtypes), then summed row-wise or combined in expressions.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/89b1021d45dcb1c1. Report an issue: GitHub.

Appendix: source

Thrown at crates/polars-core/src/datatypes/any_value.rs:1054

            (Int32(l), Int32(r)) => Int32(l + r),
            (Int64(l), Int64(r)) => Int64(l + r),
            (UInt32(l), UInt32(r)) => UInt32(l + r),
            (UInt64(l), UInt64(r)) => UInt64(l + r),
            (Float16(l), Float16(r)) => Float16(*l + *r),
            (Float32(l), Float32(r)) => Float32(l + r),
            (Float64(l), Float64(r)) => Float64(l + r),
            #[cfg(feature = "dtype-duration")]
            (Duration(l, lu), Duration(r, ru)) => {
                if lu != ru {
                    unimplemented!("adding durations with different units is not supported here");
                }

                Duration(l + r, *lu)
            },
            #[cfg(feature = "dtype-decimal")]
            (Decimal(l, lp, ls), Decimal(r, rp, rs)) => {
                if (lp, ls) != (rp, rs) {
                    unimplemented!(
                        "adding decimals with different precisions/scales is not supported here"
                    );
                }

                Decimal(l + r, *lp, *ls)
            },
            _ => unimplemented!(),
        }
    }

    #[inline]
    pub fn as_borrowed(&self) -> AnyValue<'_> {
        match self {
            AnyValue::BinaryOwned(data) => AnyValue::Binary(data),
            AnyValue::StringOwned(data) => AnyValue::String(data.as_str()),
            #[cfg(feature = "dtype-datetime")]
            AnyValue::DatetimeOwned(v, tu, tz) => {
                AnyValue::Datetime(*v, *tu, tz.as_ref().map(AsRef::as_ref))

View on GitHub (pinned to fe841f959e)