Skip to content

Casting decimals that exceed declared precision is buggy #10947

Open

Description

@neilconway

Describe the bug

A decimal can hold values that exceed its declared precision. Casting such a value to another decimal type either panics — even with safe = true, which is documented to replace unrepresentable values with null — or silently returns a wrapped value in both safe and strict modes, depending on the target type.

To Reproduce

use arrow_array::Decimal128Array;
use arrow_cast::{cast_with_options, CastOptions};
use arrow_schema::DataType;

let array = Decimal128Array::from(vec![10_i128.pow(37)])
    .with_precision_and_scale(5, 0)
    .unwrap();

let result = cast_with_options(&array, &DataType::Decimal128(9, 2), &options).unwrap();
// safe=true and safe=false both return incorrect raw value
//   -20847100762815390390123822295304634368
// and do not indicate an error

Related / similar:

use arrow_array::Decimal128Array;
use arrow_cast::{cast_with_options, CastOptions};
use arrow_schema::DataType;

// Declared precision 2; the stored value has 31 digits
let array = Decimal128Array::from(vec![10_i128.pow(30)])
    .with_precision_and_scale(2, 1)
    .unwrap();

let result = cast_with_options(
    &array,
    &DataType::Decimal32(9, 2),
    &CastOptions { safe: true, ..Default::default() },
);

with safe=true, this results in

thread 'main' panicked at arrow-cast/src/cast/decimal.rs:193:56:
called `Option::unwrap()` on a `None` value

Expected behavior

Values that cannot be represented in the target type become null in safe mode and an error with safe = false.

Additional context

No response

Activity

  1. added theissue type on Sep 1, 2026
  2. added
    bug
    on Sep 1, 2026
  3. neilconway commented on Sep 1, 2026

    @neilconway
    ContributorAuthor

    take

  4. mbutrovich commented on Oct 15, 2026

    @mbutrovich
    Contributor

    The same panic also happens when every valid value fits the declared precision and only the data under a null slot does not:

    use std::sync::Arc;
    use arrow_array::{ArrayRef, Decimal128Array};
    use arrow_buffer::{NullBuffer, ScalarBuffer};
    use arrow_cast::cast::{cast_with_options, CastOptions};
    use arrow_schema::DataType;
    
    fn main() {
        // [1, null], where the null slot holds a value that does not fit i64
        let values = ScalarBuffer::from(vec![1i128, i128::MAX]);
        let nulls = NullBuffer::from(vec![true, false]);
        let a: ArrayRef = Arc::new(Decimal128Array::new(values, Some(nulls)).with_precision_and_scale(5, -2).unwrap());
        let options = CastOptions { safe: true, ..Default::default() };
        let _ = cast_with_options(&a, &DataType::Decimal64(18, 0), &options);
        // panics: called `Option::unwrap()` on a `None` value
    }

    make_upscaler chooses the infallible closure when input_precision + delta_scale <= output_precision, and that closure unwraps from_decimal for every slot (decimal.rs). unary applies it to null slots too, whose values the spec leaves unspecified (Columnar.rst). Should the infallible path be taken only when the array has no nulls, or apply the fallible closure to null slots?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions