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

Add more info to data mismatch error #4

Merged
merged 2 commits into from
Apr 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
10 changes: 7 additions & 3 deletions arrow_convert/src/deserialize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,9 +424,13 @@ where
<ArrowType as ArrowDeserialize>::ArrayType: ArrowArrayIterable,
{
if &<ArrowType as ArrowField>::data_type() != arr.data_type() {
Err(arrow::error::ArrowError::InvalidArgumentError(
"Data type mismatch".to_string(),
))
Err(arrow::error::ArrowError::InvalidArgumentError(format!(
"Data type mismatch. Expected type={:#?} is_nullable={}, but was type={:#?} is_nullable={}",
&<ArrowType as ArrowField>::data_type(),
&<ArrowType as ArrowField>::is_nullable(),
arr.data_type(),
arr.is_nullable()
)))
} else {
Ok(arrow_array_deserialize_iterator_internal::<
Element,
Expand Down
26 changes: 23 additions & 3 deletions arrow_convert/tests/test_deserialize.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use arrow::buffer::ScalarBuffer;
use arrow::error::Result;
use arrow::{array::*, buffer::Buffer};
use arrow_convert::field::ArrowField;
use arrow_convert::{deserialize::*, serialize::*, ArrowDeserialize, ArrowField, ArrowSerialize};

#[test]
Expand Down Expand Up @@ -31,6 +32,16 @@ fn test_deserialize_iterator() {
}
}

fn data_mismatch_error<Expected: ArrowField, Actual: ArrowField>() -> arrow::error::ArrowError {
arrow::error::ArrowError::InvalidArgumentError(format!(
"Data type mismatch. Expected type={:#?} is_nullable={}, but was type={:#?} is_nullable={}",
Expected::data_type(),
Expected::is_nullable(),
Actual::data_type(),
Actual::is_nullable()
))
}

#[test]
fn test_deserialize_schema_mismatch_error() {
#[derive(Debug, Clone, PartialEq, ArrowField, ArrowSerialize, ArrowDeserialize)]
Expand All @@ -45,12 +56,18 @@ fn test_deserialize_schema_mismatch_error() {
let arr1 = vec![S1 { a: 1 }, S1 { a: 2 }];
let arr1: ArrayRef = arr1.try_into_arrow().unwrap();
let result: Result<Vec<S2>> = arr1.try_into_collection();
assert!(result.is_err());
assert_eq!(
result.unwrap_err().to_string(),
data_mismatch_error::<S2, S1>().to_string()
);

let arr1 = vec![S1 { a: 1 }, S1 { a: 2 }];
let arr1: ArrayRef = arr1.try_into_arrow().unwrap();
let result: Result<Vec<_>> = arr1.try_into_collection_as_type::<S2>();
assert!(result.is_err());
assert_eq!(
result.unwrap_err().to_string(),
data_mismatch_error::<S2, S1>().to_string()
);
}

#[test]
Expand All @@ -76,7 +93,10 @@ fn test_deserialize_large_types_schema_mismatch_error() {
let arr1: ArrayRef = arr1.try_into_arrow().unwrap();

let result: Result<Vec<S2>> = arr1.try_into_collection();
assert!(result.is_err());
assert_eq!(
result.unwrap_err().to_string(),
data_mismatch_error::<S2, S1>().to_string()
);
}

#[test]
Expand Down
Loading