Skip to content

Commit

Permalink
Improve error message for unsupported cast between struct and other t…
Browse files Browse the repository at this point in the history
…ypes (#6919)
  • Loading branch information
takaebato authored Dec 30, 2024
1 parent 1d2696d commit 7d9bb37
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions arrow-cast/src/cast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1189,12 +1189,12 @@ pub fn cast_with_options(
let array = StructArray::try_new(to_fields.clone(), fields, array.nulls().cloned())?;
Ok(Arc::new(array) as ArrayRef)
}
(Struct(_), _) => Err(ArrowError::CastError(
"Cannot cast from struct to other types except struct".to_string(),
)),
(_, Struct(_)) => Err(ArrowError::CastError(
"Cannot cast to struct from other types except struct".to_string(),
)),
(Struct(_), _) => Err(ArrowError::CastError(format!(
"Casting from {from_type:?} to {to_type:?} not supported"
))),
(_, Struct(_)) => Err(ArrowError::CastError(format!(
"Casting from {from_type:?} to {to_type:?} not supported"
))),
(_, Boolean) => match from_type {
UInt8 => cast_numeric_to_bool::<UInt8Type>(array),
UInt16 => cast_numeric_to_bool::<UInt16Type>(array),
Expand Down Expand Up @@ -9941,6 +9941,32 @@ mod tests {
);
}

#[test]
fn test_cast_struct_to_non_struct() {
let boolean = Arc::new(BooleanArray::from(vec![true, false]));
let struct_array = StructArray::from(vec![(
Arc::new(Field::new("a", DataType::Boolean, false)),
boolean.clone() as ArrayRef,
)]);
let to_type = DataType::Utf8;
let result = cast(&struct_array, &to_type);
assert_eq!(
r#"Cast error: Casting from Struct([Field { name: "a", data_type: Boolean, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }]) to Utf8 not supported"#,
result.unwrap_err().to_string()
);
}

#[test]
fn test_cast_non_struct_to_struct() {
let array = StringArray::from(vec!["a", "b"]);
let to_type = DataType::Struct(vec![Field::new("a", DataType::Boolean, false)].into());
let result = cast(&array, &to_type);
assert_eq!(
r#"Cast error: Casting from Utf8 to Struct([Field { name: "a", data_type: Boolean, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }]) not supported"#,
result.unwrap_err().to_string()
);
}

#[test]
fn test_decimal_to_decimal_throw_error_on_precision_overflow_same_scale() {
let array = vec![Some(123456789)];
Expand Down

0 comments on commit 7d9bb37

Please sign in to comment.