You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Casting decimals that exceed declared precision is buggy #10947
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 digitslet 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.
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;fnmain(){// [1, null], where the null slot holds a value that does not fit i64let 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?
Activity
neilconway commentedon Sep 1, 2026
take
mbutrovich commentedon Oct 15, 2026
The same panic also happens when every valid value fits the declared precision and only the data under a null slot does not:
make_upscalerchooses the infallible closure wheninput_precision + delta_scale <= output_precision, and that closure unwrapsfrom_decimalfor every slot (decimal.rs).unaryapplies 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?