Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cast from StringArray to Decimal128Array and Decimal256Array doesn't work on 0 #5876

Open
iajoiner opened this issue Jun 12, 2024 · 2 comments · May be fixed by #6547
Open

cast from StringArray to Decimal128Array and Decimal256Array doesn't work on 0 #5876

iajoiner opened this issue Jun 12, 2024 · 2 comments · May be fixed by #6547
Assignees
Labels

Comments

@iajoiner
Copy link
Contributor

iajoiner commented Jun 12, 2024

Describe the bug

Casting a StringArray to Decimal256Array with 0 doesn't work if the decimal type is (1, 0), (2, 0) or (76, 0). Same for Decimal128.

called `Result::unwrap()` on an `Err` value: CastError("Cannot cast string '0' to value of Decimal256(76, 10) type")

To Reproduce

use arrow::array::{Array, Decimal256Array, StringArray};
use arrow::compute::{cast_with_options, CastOptions};
use arrow::datatypes::{DataType, Field, Schema};
use arrow::record_batch::RecordBatch;
use arrow::util::display::FormatOptions;
use std::sync::Arc;

fn main() {
    // Create a StringArray with "0" as the only element
    let string_values = vec![Some("0")];
    let string_array = StringArray::from(string_values);

    // Define the schema for the record batch
    let schema = Arc::new(Schema::new(vec![Field::new("string", DataType::Utf8, false)]));

    // Create a record batch
    let record_batch = RecordBatch::try_new(schema, vec![Arc::new(string_array)]).unwrap();

    // Define the target type (Decimal256 with precision and scale)
    let decimal_type = DataType::Decimal256(1, 0);

    // Cast the StringArray to Decimal256Array
    let casted_array = cast_with_options(&record_batch.column(0), &decimal_type, &CastOptions {
        safe: false,
        format_options: FormatOptions::new(),
    },).unwrap();

    // Downcast the casted array to Decimal256Array
    let decimal_array = casted_array
        .as_any()
        .downcast_ref::<Decimal256Array>()
        .unwrap();

    // Print the result
    for i in 0..decimal_array.len() {
        println!("{}", decimal_array.value(i));
    }
}

Expected behavior

0
Additional context

@iajoiner iajoiner added the bug label Jun 12, 2024
@iajoiner iajoiner changed the title cast from StringArray to Decimal256Array doesn't work on 0 cast from StringArray to Decimal128Array and Decimal256Array doesn't work on 0 Jun 12, 2024
@findepi findepi linked a pull request Oct 11, 2024 that will close this issue
@findepi
Copy link
Member

findepi commented Oct 11, 2024

take

@findepi
Copy link
Member

findepi commented Oct 11, 2024

#6547 should fix this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants