This is an automated email from the ASF dual-hosted git repository.

tustvold pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git


The following commit(s) were added to refs/heads/master by this push:
     new 2af51631e4 Allow merge of Null to any datatype. (#4902)
2af51631e4 is described below

commit 2af51631e492e0ea0ac71da67b3dba6a846dafd5
Author: Kamil Skalski <[email protected]>
AuthorDate: Mon Oct 9 18:19:32 2023 +0200

    Allow merge of Null to any datatype. (#4902)
---
 arrow-schema/src/field.rs | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)

diff --git a/arrow-schema/src/field.rs b/arrow-schema/src/field.rs
index 00deecf062..b50778c785 100644
--- a/arrow-schema/src/field.rs
+++ b/arrow-schema/src/field.rs
@@ -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
@@ -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)
@@ -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(

Reply via email to