From 6219d0da118b8bdbeb5de27a34519da694e177bd Mon Sep 17 00:00:00 2001 From: Andy Grove Date: Mon, 14 Nov 2022 17:01:22 -0700 Subject: [PATCH 1/2] Include field name in merge error message --- arrow-schema/src/field.rs | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/arrow-schema/src/field.rs b/arrow-schema/src/field.rs index 4d13f523fb96..6de6ea8b8105 100644 --- a/arrow-schema/src/field.rs +++ b/arrow-schema/src/field.rs @@ -267,14 +267,14 @@ impl Field { pub fn try_merge(&mut self, from: &Field) -> Result<(), ArrowError> { if from.dict_id != self.dict_id { return Err(ArrowError::SchemaError(format!( - "Fail to merge schema field because from dict_id = {} does not match {}", - from.dict_id, self.dict_id + "Fail to merge schema field '{}' because from dict_id = {} does not match {}", + self.name, from.dict_id, self.dict_id ))); } if from.dict_is_ordered != self.dict_is_ordered { return Err(ArrowError::SchemaError(format!( - "Fail to merge schema field because from dict_is_ordered = {} does not match {}", - from.dict_is_ordered, self.dict_is_ordered + "Fail to merge schema field '{}' because from dict_is_ordered = {} does not match {}", + self.name, from.dict_is_ordered, self.dict_is_ordered ))); } // merge metadata @@ -285,8 +285,8 @@ impl Field { if let Some(self_value) = self_metadata.get(key) { if self_value != from_value { return Err(ArrowError::SchemaError(format!( - "Fail to merge field due to conflicting metadata data value for key {}. - From value = {} does not match {}", key, from_value, self_value), + "Fail to merge field '{}' due to conflicting metadata data value for key {}. + From value = {} does not match {}", self.name, key, from_value, self_value), )); } } else { @@ -315,8 +315,8 @@ impl Field { } _ => { return Err(ArrowError::SchemaError( - format!("Fail to merge schema field because the from data_type = {} is not DataType::Struct", - from.data_type) + format!("Fail to merge schema field '{}' because the from data_type = {} is not DataType::Struct", + self.name, from.data_type) ))} }, DataType::Union(nested_fields, type_ids, _) => match &from.data_type { @@ -334,8 +334,8 @@ impl Field { // type id. if self_type_id != field_type_id { return Err(ArrowError::SchemaError( - format!("Fail to merge schema field because the self_type_id = {} does not equal field_type_id = {}", - self_type_id, field_type_id) + format!("Fail to merge schema field '{}' because the self_type_id = {} does not equal field_type_id = {}", + self.name, self_type_id, field_type_id) )); } @@ -352,8 +352,8 @@ impl Field { } _ => { return Err(ArrowError::SchemaError( - format!("Fail to merge schema field because the from data_type = {} is not DataType::Union", - from.data_type) + format!("Fail to merge schema field '{}' because the from data_type = {} is not DataType::Union", + self.name, from.data_type) )); } }, @@ -391,8 +391,8 @@ impl Field { | DataType::Decimal256(_, _) => { if self.data_type != from.data_type { return Err(ArrowError::SchemaError( - format!("Fail to merge schema field because the from data_type = {} does not equal {}", - from.data_type, self.data_type) + format!("Fail to merge schema field '{}' because the from data_type = {} does not equal {}", + self.name, from.data_type, self.data_type) )); } } @@ -443,6 +443,15 @@ mod test { use std::collections::hash_map::DefaultHasher; use std::hash::{Hash, Hasher}; + #[test] + fn test_merge_incompatible_types() { + let mut field = Field::new("c1", DataType::Int64, false); + let result = field + .try_merge(&Field::new("c1", DataType::Float32, true)) + .expect_err("should fail"); + assert_eq!("Schema error: Fail to merge schema field 'c1' because the from data_type = Float32 does not equal Int64", format!("{}", result)); + } + #[test] fn test_fields_with_dict_id() { let dict1 = Field::new_dict( From d368daa9a42b559613fed6297331761baf21ee15 Mon Sep 17 00:00:00 2001 From: Raphael Taylor-Davies Date: Wed, 16 Nov 2022 10:24:34 +1300 Subject: [PATCH 2/2] Lint --- arrow-schema/src/field.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/arrow-schema/src/field.rs b/arrow-schema/src/field.rs index 6de6ea8b8105..92431d903b6b 100644 --- a/arrow-schema/src/field.rs +++ b/arrow-schema/src/field.rs @@ -448,8 +448,9 @@ mod test { let mut field = Field::new("c1", DataType::Int64, false); let result = field .try_merge(&Field::new("c1", DataType::Float32, true)) - .expect_err("should fail"); - assert_eq!("Schema error: Fail to merge schema field 'c1' because the from data_type = Float32 does not equal Int64", format!("{}", result)); + .expect_err("should fail") + .to_string(); + assert_eq!("Schema error: Fail to merge schema field 'c1' because the from data_type = Float32 does not equal Int64", result); } #[test]