Skip to content

Commit

Permalink
propagate error correctly when a date cannot be parsed
Browse files Browse the repository at this point in the history
  • Loading branch information
vidyasankarv committed May 15, 2024
1 parent df499fd commit 362363f
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions core/src/execution/datafusion/expressions/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,12 +657,12 @@ impl Cast {
let len = string_array.len();
let mut cast_array = PrimitiveArray::<Date32Type>::builder(len);
for i in 0..len {
if string_array.is_null(i) {
cast_array.append_null()
} else if let Ok(Some(cast_value)) =
date_parser(string_array.value(i), eval_mode)
{
cast_array.append_value(cast_value);
if !string_array.is_null(i) {
match date_parser(string_array.value(i), eval_mode) {
Ok(Some(cast_value)) => cast_array.append_value(cast_value),
Ok(None) => cast_array.append_null(),
Err(e) => return Err(e),
}
} else {
cast_array.append_null()
}
Expand Down Expand Up @@ -1742,19 +1742,16 @@ mod tests {

#[test]
fn test_cast_string_to_date() {
// Create a StringArray with various date strings
let array: ArrayRef = Arc::new(StringArray::from(vec![
Some("2020"),
Some("2020-01"),
Some("2020-01-01"),
Some("2020-01-01T"),
]));

// Invoke cast_string_to_date
let result =
Cast::cast_string_to_date(&array, &DataType::Date32, EvalMode::Legacy).unwrap();

// Verify that each element of the result is 18262
let date32_array = result
.as_any()
.downcast_ref::<arrow::array::Date32Array>()
Expand All @@ -1765,6 +1762,27 @@ mod tests {
.for_each(|v| assert_eq!(v.unwrap(), 18262));
}

fn test_cast_string_to_invalid_dates() {
// basic

let array_with_invalid_date: ArrayRef = Arc::new(StringArray::from(vec![
Some("2020"),
Some("2020-01"),
Some("2020-01-01"),
Some("2020-010-01T"),
Some("2020-01-01T"),
]));
let result =
Cast::cast_string_to_date(&array_with_invalid_date, &DataType::Date32, EvalMode::Ansi);
match result {
Err(e) => assert!(
e.to_string().contains(
"[CAST_INVALID_INPUT] The value '2020-010-01T' of the type \"STRING\" cannot be cast to \"DATE\" because it is malformed")
),
_ => panic!("Expected error"),
}
}

#[test]
fn test_cast_string_as_i8() {
// basic
Expand Down

0 comments on commit 362363f

Please sign in to comment.