Skip to content

Commit

Permalink
Allow merge of Null to any datatype. (#4902)
Browse files Browse the repository at this point in the history
  • Loading branch information
kskalski authored Oct 9, 2023
1 parent ed58e76 commit 2af5163
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions arrow-schema/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,10 @@ impl Field {
));
}
},
DataType::Null
DataType::Null => {
self.nullable = true;
self.data_type = from.data_type.clone();
}
| DataType::Boolean
| DataType::Int8
| DataType::Int16
Expand Down Expand Up @@ -494,7 +497,9 @@ impl Field {
| DataType::LargeUtf8
| DataType::Decimal128(_, _)
| DataType::Decimal256(_, _) => {
if self.data_type != from.data_type {
if from.data_type == DataType::Null {
self.nullable = true;
} else 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 {}",
self.name, from.data_type, self.data_type)
Expand Down Expand Up @@ -580,6 +585,21 @@ mod test {
assert_eq!("Schema error: Fail to merge schema field 'c1' because the from data_type = Float32 does not equal Int64", result);
}

#[test]
fn test_merge_with_null() {
let mut field1 = Field::new("c1", DataType::Null, true);
field1
.try_merge(&Field::new("c1", DataType::Float32, false))
.expect("should widen type to nullable float");
assert_eq!(Field::new("c1", DataType::Float32, true), field1);

let mut field2 = Field::new("c2", DataType::Utf8, false);
field2
.try_merge(&Field::new("c2", DataType::Null, true))
.expect("should widen type to nullable utf8");
assert_eq!(Field::new("c2", DataType::Utf8, true), field2);
}

#[test]
fn test_fields_with_dict_id() {
let dict1 = Field::new_dict(
Expand Down

0 comments on commit 2af5163

Please sign in to comment.