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: improve errors on field cast failures #2932

Merged
merged 1 commit into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
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
19 changes: 18 additions & 1 deletion crates/core/src/operations/cast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,19 @@ fn cast_field(
add_missing,
)?) as ArrayRef),
_ if is_cast_required(col_type, field_type) => {
cast_with_options(col, field_type, cast_options)
cast_with_options(col, field_type, cast_options).map_err(|err| {
if let ArrowError::CastError(err) = err {
ArrowError::CastError(format!(
"Failed to cast {} from {} to {}: {}",
field.name(),
field_type,
col_type,
err
))
} else {
err
}
})
}
_ => Ok(col.clone()),
}
Expand Down Expand Up @@ -337,6 +349,11 @@ mod tests {
assert!(!is_cast_required(&field1, &field2));
}

#[test]
fn test_is_cast_required_with_smol_int() {
assert!(is_cast_required(&DataType::Int8, &DataType::Int32));
}

#[test]
fn test_is_cast_required_with_list_non_default_item() {
let field1 = DataType::List(FieldRef::from(Field::new("item", DataType::Int32, false)));
Expand Down
6 changes: 4 additions & 2 deletions python/tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ def test_write_type_castable_types(existing_table: DeltaTable):
engine="rust",
)
with pytest.raises(
Exception, match="Cast error: Cannot cast string 'hello' to value of Int8 type"
Exception,
match="Cast error: Failed to cast int8 from Int8 to Utf8: Cannot cast string 'hello' to value of Int8 type",
):
write_deltalake(
existing_table,
Expand All @@ -284,7 +285,8 @@ def test_write_type_castable_types(existing_table: DeltaTable):
)

with pytest.raises(
Exception, match="Cast error: Can't cast value 1000 to type Int8"
Exception,
match="Cast error: Failed to cast int8 from Int8 to Int64: Can't cast value 1000 to type Int8",
):
write_deltalake(
existing_table,
Expand Down
Loading