Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions datafusion/core/tests/sql/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))],
Expand Down Expand Up @@ -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 {
Expand Down
35 changes: 32 additions & 3 deletions datafusion/expr-common/src/type_coercion/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -947,8 +947,14 @@ fn merge_struct_fields(structs: &[&Fields], common_fields: &Fields) -> Result<Fi
}
})
.collect::<Vec<_>>();
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()
Expand All @@ -960,12 +966,35 @@ fn merge_struct_fields(structs: &[&Fields], common_fields: &Fields) -> Result<Fi
.clone()
.with_data_type(data_type)
.with_nullable(nullable)
.with_metadata(metadata.to_hashmap()),
.with_metadata(metadata),
))
})
.collect()
}

/// Merge metadata from untyped NULL fields, with earlier fields taking
/// precedence. The extension name and its metadata are taken together from the
/// first field that declares an extension type.
fn null_fields_metadata(fields: &[&Field]) -> HashMap<String, String> {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> {
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(())
}
Loading