From b3b01f8ac47671ada7cb0d736dab4afbbf27edf9 Mon Sep 17 00:00:00 2001 From: Jason Ni Date: Tue, 24 Nov 2026 16:33:00 -0500 Subject: [PATCH] fix: keep metadata of struct children that are NULL in every input Struct coercion merges each child's metadata from its typed contributors and ignores untyped NULL children. When a child is NULL in every input, nothing contributes, so the merged child lost all of its metadata. That happens for a single struct argument as well as for several arguments that are all NULL at the same position, for example `make_array(struct(1, NULL))` or `coalesce(NULL, s, s)` where `s` has a NULL-typed child. Merge the NULL inputs' metadata in that case, with earlier inputs taking precedence and the extension name and metadata taken together from the first input that declares one. Children with at least one typed contributor keep ignoring NULL-child metadata, as before. --- datafusion/core/tests/sql/select.rs | 28 +++++++--- .../expr-common/src/type_coercion/binary.rs | 35 ++++++++++-- .../binary/tests/null_coercion.rs | 53 +++++++++++++++++++ 3 files changed, 107 insertions(+), 9 deletions(-) diff --git a/datafusion/core/tests/sql/select.rs b/datafusion/core/tests/sql/select.rs index 67883a71d63cb..771996d083824 100644 --- a/datafusion/core/tests/sql/select.rs +++ b/datafusion/core/tests/sql/select.rs @@ -564,14 +564,14 @@ async fn test_coalesce_struct_field_metadata() -> Result<()> { ]))], Some(NullBuffer::from(vec![false, true, true, false])), )); + let null_child_metadata = Metadata::new() + .with("ARROW:extension:name", "example.null") + .with("origin", "null_child") + .with("null_only", "1"); let null_child: ArrayRef = Arc::new(StructArray::new( vec![ - Field::new("v", DataType::Null, true).with_metadata( - Metadata::new() - .with("ARROW:extension:name", "example.null") - .with("origin", "ignored") - .with("null_only", "ignored"), - ), + Field::new("v", DataType::Null, true) + .with_metadata(null_child_metadata.clone()), ] .into(), vec![Arc::new(NullArray::new(4))], @@ -682,6 +682,22 @@ async fn test_coalesce_struct_field_metadata() -> Result<()> { Some(NullBuffer::from(vec![true, false, true, false])), ), ), + ( + [ + "SELECT coalesce(NULL, null_child, null_child) FROM t", + "SELECT coalesce(volatile_null_child(null_child), null_child) FROM t", + ], + true, + StructArray::new( + vec![ + Field::new("v", DataType::Null, true) + .with_metadata(null_child_metadata), + ] + .into(), + vec![Arc::new(NullArray::new(4))], + Some(NullBuffer::from(vec![false, false, true, false])), + ), + ), ]; for (queries, expected_nullable, expected) in cases { diff --git a/datafusion/expr-common/src/type_coercion/binary.rs b/datafusion/expr-common/src/type_coercion/binary.rs index ed57604246c12..11e108e9ad611 100644 --- a/datafusion/expr-common/src/type_coercion/binary.rs +++ b/datafusion/expr-common/src/type_coercion/binary.rs @@ -947,8 +947,14 @@ fn merge_struct_fields(structs: &[&Fields], common_fields: &Fields) -> Result>(); - let metadata = - coerced_fields_metadata(coerced.iter().map(|field| field.as_ref()))?; + // Untyped NULL children defer to typed contributors. A child that is + // NULL in every input has none, so it keeps the NULL inputs' metadata. + let metadata = if coerced.is_empty() { + null_fields_metadata(&inputs) + } else { + coerced_fields_metadata(coerced.iter().map(|field| field.as_ref()))? + .to_hashmap() + }; let nullable = inputs.iter().any(|field| field.is_nullable()); let field = inputs .iter() @@ -960,12 +966,35 @@ fn merge_struct_fields(structs: &[&Fields], common_fields: &Fields) -> Result HashMap { + let mut merged = HashMap::new(); + if let Some(field) = fields + .iter() + .find(|field| field.metadata().contains_key(EXTENSION_TYPE_NAME_KEY)) + { + for key in [EXTENSION_TYPE_NAME_KEY, EXTENSION_TYPE_METADATA_KEY] { + if let Some(value) = field.metadata().get(key) { + merged.insert(key.to_string(), value.clone()); + } + } + } + for (key, value) in fields.iter().flat_map(|field| field.metadata()) { + if key != EXTENSION_TYPE_NAME_KEY && key != EXTENSION_TYPE_METADATA_KEY { + merged.entry(key.clone()).or_insert_with(|| value.clone()); + } + } + merged +} + /// Coerce `lhs_type` and `rhs_type` to a common type for type unification /// contexts — where two values must be brought to a common type but are not /// being compared. Examples: UNION, CASE THEN/ELSE branches, NVL2. For other diff --git a/datafusion/expr-common/src/type_coercion/binary/tests/null_coercion.rs b/datafusion/expr-common/src/type_coercion/binary/tests/null_coercion.rs index 91c826b563c7c..c58430c904211 100644 --- a/datafusion/expr-common/src/type_coercion/binary/tests/null_coercion.rs +++ b/datafusion/expr-common/src/type_coercion/binary/tests/null_coercion.rs @@ -70,3 +70,56 @@ fn test_type_coercion_logical_op() -> Result<()> { ); Ok(()) } + +fn struct_with_child(child: DataType, metadata: &[(&str, &str)]) -> DataType { + let metadata = metadata + .iter() + .map(|(key, value)| (key.to_string(), value.to_string())) + .collect(); + DataType::Struct(Fields::from(vec![ + Field::new("a", DataType::Int64, false), + Field::new("b", child, true).with_metadata(metadata), + ])) +} + +fn child_metadata(data_type: &DataType) -> &HashMap { + let DataType::Struct(fields) = data_type else { + panic!("expected struct, got {data_type}"); + }; + fields[1].metadata() +} + +#[test] +fn test_struct_coercion_keeps_metadata_of_children_null_in_every_input() -> Result<()> { + let single = struct_with_child(DataType::Null, &[("k", "v")]); + let coerced = try_type_union_resolution_with_struct(std::slice::from_ref(&single))?; + assert_eq!(coerced, vec![single]); + + let first = struct_with_child(DataType::Null, &[("k", "first"), ("only_first", "1")]); + let second = + struct_with_child(DataType::Null, &[("k", "second"), ("only_second", "2")]); + let coerced = try_type_union_resolution_with_struct(&[first, second])?; + assert_eq!( + child_metadata(&coerced[0]), + &HashMap::from([ + ("k".to_string(), "first".to_string()), + ("only_first".to_string(), "1".to_string()), + ("only_second".to_string(), "2".to_string()), + ]) + ); + Ok(()) +} + +#[test] +fn test_struct_coercion_prefers_typed_child_metadata_over_null_children() -> Result<()> { + let null_child = struct_with_child(DataType::Null, &[("k", "null")]); + let typed_child = struct_with_child(DataType::Int64, &[("k", "typed")]); + for inputs in [ + [null_child.clone(), typed_child.clone()], + [typed_child.clone(), null_child.clone()], + ] { + let coerced = try_type_union_resolution_with_struct(&inputs)?; + assert_eq!(coerced[0], typed_child); + } + Ok(()) +}