Skip to content

Commit

Permalink
fix: Return an error on type mismatch rather than panic (#4995)
Browse files Browse the repository at this point in the history
  • Loading branch information
carols10cents committed Jan 29, 2024
1 parent 5117b38 commit 0504703
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion parquet/src/arrow/arrow_writer/levels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ struct LevelContext {
}

/// A helper to construct [`ArrayLevels`] from a potentially nested [`Field`]
#[derive(Debug)]
enum LevelInfoBuilder {
/// A primitive, leaf array
Primitive(ArrayLevels),
Expand Down Expand Up @@ -132,7 +133,15 @@ enum LevelInfoBuilder {
impl LevelInfoBuilder {
/// Create a new [`LevelInfoBuilder`] for the given [`Field`] and parent [`LevelContext`]
fn try_new(field: &Field, parent_ctx: LevelContext, array: &ArrayRef) -> Result<Self> {
assert_eq!(field.data_type(), array.data_type());
if field.data_type() != array.data_type() {
return Err(arrow_err!(format!(
"Incompatible type. Field '{}' has type {}, array has type {}",
field.name(),
field.data_type(),
array.data_type(),
)));
}

let is_nullable = field.is_nullable();

match array.data_type() {
Expand Down Expand Up @@ -1835,6 +1844,21 @@ mod tests {
assert_eq!(levels[0], expected_level);
}

#[test]
fn mismatched_types() {
let array = Arc::new(Int32Array::from_iter(0..10)) as ArrayRef;
let field = Field::new("item", DataType::Float64, false);

let err = LevelInfoBuilder::try_new(&field, Default::default(), &array)
.unwrap_err()
.to_string();

assert_eq!(
err,
"Arrow: Incompatible type. Field 'item' has type Float64, array has type Int32",
);
}

fn levels<T: Array + 'static>(field: &Field, array: T) -> LevelInfoBuilder {
let v = Arc::new(array) as ArrayRef;
LevelInfoBuilder::try_new(field, Default::default(), &v).unwrap()
Expand Down

0 comments on commit 0504703

Please sign in to comment.