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

Fix string '0' cast to decimal with scale 0 #6547

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion arrow-cast/src/cast/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ where
}
};

let integers = first_part.trim_start_matches('0');
let integers = first_part;
let decimals = if parts.len() == 2 { parts[1] } else { "" };

if !integers.is_empty() && !integers.as_bytes()[0].is_ascii_digit() {
Expand Down Expand Up @@ -567,3 +567,48 @@ where
let array = array.unary::<_, T>(op);
Ok(Arc::new(array))
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_parse_string_to_decimal_native() -> Result<(), ArrowError> {
assert_eq!(
parse_string_to_decimal_native::<Decimal128Type>("0", 0)?,
0_i128
);
assert_eq!(
parse_string_to_decimal_native::<Decimal128Type>("0", 5)?,
0_i128
);

assert_eq!(
parse_string_to_decimal_native::<Decimal128Type>("123", 0)?,
123_i128
);
assert_eq!(
parse_string_to_decimal_native::<Decimal128Type>("123", 5)?,
12300000_i128
);

assert_eq!(
parse_string_to_decimal_native::<Decimal128Type>("123.45", 0)?,
123_i128
);
assert_eq!(
parse_string_to_decimal_native::<Decimal128Type>("123.45", 5)?,
12345000_i128
);

assert_eq!(
parse_string_to_decimal_native::<Decimal128Type>("123.4567891", 0)?,
123_i128
);
assert_eq!(
parse_string_to_decimal_native::<Decimal128Type>("123.4567891", 5)?,
12345679_i128
);
Ok(())
}
}
79 changes: 79 additions & 0 deletions arrow-cast/src/cast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8481,6 +8481,9 @@ mod tests {
assert!(decimal_arr.is_null(25));
assert!(decimal_arr.is_null(26));
assert!(decimal_arr.is_null(27));
assert_eq!("0.00", decimal_arr.value_as_string(28));
assert_eq!("0.00", decimal_arr.value_as_string(29));
assert_eq!(decimal_arr.len(), 30);

// Decimal256
let output_type = DataType::Decimal256(76, 3);
Expand Down Expand Up @@ -8517,6 +8520,9 @@ mod tests {
assert!(decimal_arr.is_null(25));
assert!(decimal_arr.is_null(26));
assert!(decimal_arr.is_null(27));
assert_eq!("0.000", decimal_arr.value_as_string(28));
findepi marked this conversation as resolved.
Show resolved Hide resolved
assert_eq!("0.000", decimal_arr.value_as_string(29));
assert_eq!(decimal_arr.len(), 30);
}

#[test]
Expand Down Expand Up @@ -8550,10 +8556,28 @@ mod tests {
Some("1.-23499999"),
Some("-1.-23499999"),
Some("--1.23499999"),
Some("0"),
Some("000.000"),
]);
let array = Arc::new(str_array) as ArrayRef;

test_cast_string_to_decimal(array);

let test_cases = [
(None, None),
// (Some(""), None),
// (Some(" "), None),
(Some("0"), Some("0")),
(Some("000.000"), Some("0")),
(Some("12345"), Some("12345")),
(Some("-123"), Some("-123")),
(Some("+123"), Some("123")),
];
let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();

let array = Arc::new(StringArray::from(inputs)) as ArrayRef;
test_cast_string_to_decimal_scale_zero(array, &expected);
}

#[test]
Expand Down Expand Up @@ -8587,10 +8611,65 @@ mod tests {
Some("1.-23499999"),
Some("-1.-23499999"),
Some("--1.23499999"),
Some("0"),
Some("000.000"),
]);
let array = Arc::new(str_array) as ArrayRef;

test_cast_string_to_decimal(array);

let test_cases = [
(None, None),
(Some(""), None),
(Some(" "), None),
(Some("0"), Some("0")),
(Some("000.000"), Some("0")),
(Some("12345"), Some("12345")),
(Some("-123"), Some("-123")),
(Some("+123"), Some("123")),
];
let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();

let array = Arc::new(LargeStringArray::from(inputs)) as ArrayRef;
test_cast_string_to_decimal_scale_zero(array, &expected);
}

fn test_cast_string_to_decimal_scale_zero(
array: ArrayRef,
expected_as_string: &[Option<&str>],
) {
// Decimal128
let output_type = DataType::Decimal128(38, 0);
assert!(can_cast_types(array.data_type(), &output_type));
let casted_array = cast(&array, &output_type).unwrap();
let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
assert_decimal_array_contents(decimal_arr, expected_as_string);

// Decimal256
let output_type = DataType::Decimal256(76, 0);
assert!(can_cast_types(array.data_type(), &output_type));
let casted_array = cast(&array, &output_type).unwrap();
let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
assert_decimal_array_contents(decimal_arr, expected_as_string);
}

fn assert_decimal_array_contents<T>(
array: &PrimitiveArray<T>,
expected_as_string: &[Option<&str>],
) where
T: DecimalType + ArrowPrimitiveType,
{
assert_eq!(array.len(), expected_as_string.len());
for (i, expected) in expected_as_string.iter().enumerate() {
let actual = if array.is_null(i) {
None
} else {
Some(array.value_as_string(i))
};
let actual = actual.as_ref().map(|s| s.as_ref());
assert_eq!(*expected, actual, "Expected at position {}", i);
}
}

#[test]
Expand Down
Loading