Skip to content

Commit b3b01f8

Browse files
committed
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.
1 parent 4dc8b1d commit b3b01f8

3 files changed

Lines changed: 107 additions & 9 deletions

File tree

‎datafusion/core/tests/sql/select.rs‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -564,14 +564,14 @@ async fn test_coalesce_struct_field_metadata() -> Result<()> {
564564
]))],
565565
Some(NullBuffer::from(vec![false, true, true, false])),
566566
));
567+
let null_child_metadata = Metadata::new()
568+
.with("ARROW:extension:name", "example.null")
569+
.with("origin", "null_child")
570+
.with("null_only", "1");
567571
let null_child: ArrayRef = Arc::new(StructArray::new(
568572
vec![
569-
Field::new("v", DataType::Null, true).with_metadata(
570-
Metadata::new()
571-
.with("ARROW:extension:name", "example.null")
572-
.with("origin", "ignored")
573-
.with("null_only", "ignored"),
574-
),
573+
Field::new("v", DataType::Null, true)
574+
.with_metadata(null_child_metadata.clone()),
575575
]
576576
.into(),
577577
vec![Arc::new(NullArray::new(4))],
@@ -682,6 +682,22 @@ async fn test_coalesce_struct_field_metadata() -> Result<()> {
682682
Some(NullBuffer::from(vec![true, false, true, false])),
683683
),
684684
),
685+
(
686+
[
687+
"SELECT coalesce(NULL, null_child, null_child) FROM t",
688+
"SELECT coalesce(volatile_null_child(null_child), null_child) FROM t",
689+
],
690+
true,
691+
StructArray::new(
692+
vec![
693+
Field::new("v", DataType::Null, true)
694+
.with_metadata(null_child_metadata),
695+
]
696+
.into(),
697+
vec![Arc::new(NullArray::new(4))],
698+
Some(NullBuffer::from(vec![false, false, true, false])),
699+
),
700+
),
685701
];
686702

687703
for (queries, expected_nullable, expected) in cases {

‎datafusion/expr-common/src/type_coercion/binary.rs‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -947,8 +947,14 @@ fn merge_struct_fields(structs: &[&Fields], common_fields: &Fields) -> Result<Fi
947947
}
948948
})
949949
.collect::<Vec<_>>();
950-
let metadata =
951-
coerced_fields_metadata(coerced.iter().map(|field| field.as_ref()))?;
950+
// Untyped NULL children defer to typed contributors. A child that is
951+
// NULL in every input has none, so it keeps the NULL inputs' metadata.
952+
let metadata = if coerced.is_empty() {
953+
null_fields_metadata(&inputs)
954+
} else {
955+
coerced_fields_metadata(coerced.iter().map(|field| field.as_ref()))?
956+
.to_hashmap()
957+
};
952958
let nullable = inputs.iter().any(|field| field.is_nullable());
953959
let field = inputs
954960
.iter()
@@ -960,12 +966,35 @@ fn merge_struct_fields(structs: &[&Fields], common_fields: &Fields) -> Result<Fi
960966
.clone()
961967
.with_data_type(data_type)
962968
.with_nullable(nullable)
963-
.with_metadata(metadata.to_hashmap()),
969+
.with_metadata(metadata),
964970
))
965971
})
966972
.collect()
967973
}
968974

975+
/// Merge metadata from untyped NULL fields, with earlier fields taking
976+
/// precedence. The extension name and its metadata are taken together from the
977+
/// first field that declares an extension type.
978+
fn null_fields_metadata(fields: &[&Field]) -> HashMap<String, String> {
979+
let mut merged = HashMap::new();
980+
if let Some(field) = fields
981+
.iter()
982+
.find(|field| field.metadata().contains_key(EXTENSION_TYPE_NAME_KEY))
983+
{
984+
for key in [EXTENSION_TYPE_NAME_KEY, EXTENSION_TYPE_METADATA_KEY] {
985+
if let Some(value) = field.metadata().get(key) {
986+
merged.insert(key.to_string(), value.clone());
987+
}
988+
}
989+
}
990+
for (key, value) in fields.iter().flat_map(|field| field.metadata()) {
991+
if key != EXTENSION_TYPE_NAME_KEY && key != EXTENSION_TYPE_METADATA_KEY {
992+
merged.entry(key.clone()).or_insert_with(|| value.clone());
993+
}
994+
}
995+
merged
996+
}
997+
969998
/// Coerce `lhs_type` and `rhs_type` to a common type for type unification
970999
/// contexts — where two values must be brought to a common type but are not
9711000
/// being compared. Examples: UNION, CASE THEN/ELSE branches, NVL2. For other

‎datafusion/expr-common/src/type_coercion/binary/tests/null_coercion.rs‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,56 @@ fn test_type_coercion_logical_op() -> Result<()> {
7070
);
7171
Ok(())
7272
}
73+
74+
fn struct_with_child(child: DataType, metadata: &[(&str, &str)]) -> DataType {
75+
let metadata = metadata
76+
.iter()
77+
.map(|(key, value)| (key.to_string(), value.to_string()))
78+
.collect();
79+
DataType::Struct(Fields::from(vec![
80+
Field::new("a", DataType::Int64, false),
81+
Field::new("b", child, true).with_metadata(metadata),
82+
]))
83+
}
84+
85+
fn child_metadata(data_type: &DataType) -> &HashMap<String, String> {
86+
let DataType::Struct(fields) = data_type else {
87+
panic!("expected struct, got {data_type}");
88+
};
89+
fields[1].metadata()
90+
}
91+
92+
#[test]
93+
fn test_struct_coercion_keeps_metadata_of_children_null_in_every_input() -> Result<()> {
94+
let single = struct_with_child(DataType::Null, &[("k", "v")]);
95+
let coerced = try_type_union_resolution_with_struct(std::slice::from_ref(&single))?;
96+
assert_eq!(coerced, vec![single]);
97+
98+
let first = struct_with_child(DataType::Null, &[("k", "first"), ("only_first", "1")]);
99+
let second =
100+
struct_with_child(DataType::Null, &[("k", "second"), ("only_second", "2")]);
101+
let coerced = try_type_union_resolution_with_struct(&[first, second])?;
102+
assert_eq!(
103+
child_metadata(&coerced[0]),
104+
&HashMap::from([
105+
("k".to_string(), "first".to_string()),
106+
("only_first".to_string(), "1".to_string()),
107+
("only_second".to_string(), "2".to_string()),
108+
])
109+
);
110+
Ok(())
111+
}
112+
113+
#[test]
114+
fn test_struct_coercion_prefers_typed_child_metadata_over_null_children() -> Result<()> {
115+
let null_child = struct_with_child(DataType::Null, &[("k", "null")]);
116+
let typed_child = struct_with_child(DataType::Int64, &[("k", "typed")]);
117+
for inputs in [
118+
[null_child.clone(), typed_child.clone()],
119+
[typed_child.clone(), null_child.clone()],
120+
] {
121+
let coerced = try_type_union_resolution_with_struct(&inputs)?;
122+
assert_eq!(coerced[0], typed_child);
123+
}
124+
Ok(())
125+
}

0 commit comments

Comments
 (0)