Skip to content

[Regression in 54.0.0]. Decimal cast to smaller precision gives invalid (off-by-one) result in some cases #7069

@Blizzara

Description

@Blizzara

Describe the bug

See practical examples in apache/datafusion#14450.
Somehow a regression caused by #6833.

To Reproduce

Adding this into arrow-cast/src/cast/mod.rs:

    #[test]
    fn test_decimal_to_decimal() {
        let array = vec![Some(520)];
        let array = create_decimal_array(array, 4, 2).unwrap();
        let input_type = DataType::Decimal128(4, 2);
        let output_type = DataType::Decimal128(3, 2);
        assert!(can_cast_types(&input_type, &output_type));

        let options = CastOptions {
            safe: false,
            ..Default::default()
        };
        let result = cast_with_options(&array, &output_type, &options);
        assert_eq!(result.unwrap().as_primitive::<Decimal128Type>().value(0), 520);
    }

passes on 84dba34, but fails on eb7ab83 with

assertion `left == right` failed
  left: 521
 right: 520

Somehow something in the code adds an extra 1 to the value?

Expected behavior

Test passes

Additional context

Fyi @himadripal @andygrove @findepi @viirya @tustvold as you were involved in that earlier change. I tried to look at it but don't understand how it'd go wrong myself, maybe you know?

Metadata

Metadata

Assignees

No one assigned

    Labels

    arrowChanges to the arrow cratebug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions