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

Improve the error message for casting between struct and non-struct types #6919

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Changes from all commits
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
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
Loading