Skip to content

Commit 7ff3769

Browse files
committed
fix coalece bug
1 parent 86b4afc commit 7ff3769

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ fn type_union_resolution_coercion(
595595

596596
/// Handle type union resolution including struct type and others.
597597
pub fn try_type_union_resolution(data_types: &[DataType]) -> Result<Vec<DataType>> {
598-
let err = match try_type_union_resolution_with_struct(data_types) {
598+
let err = match try_type_union_resolution_with_struct(data_types, false) {
599599
Ok(struct_types) => return Ok(struct_types),
600600
Err(e) => Some(e),
601601
};
@@ -611,6 +611,7 @@ pub fn try_type_union_resolution(data_types: &[DataType]) -> Result<Vec<DataType
611611
// Since field name is the key of the struct, so it shouldn't be updated to the common column name like "c0" or "c1"
612612
pub fn try_type_union_resolution_with_struct(
613613
data_types: &[DataType],
614+
is_unique: bool,
614615
) -> Result<Vec<DataType>> {
615616
let mut keys_string: Option<String> = None;
616617
for data_type in data_types {
@@ -632,6 +633,12 @@ pub fn try_type_union_resolution_with_struct(
632633
}
633634
}
634635

636+
let first_fields = if let DataType::Struct(fields) = &data_types[0] {
637+
fields.clone()
638+
} else {
639+
return internal_err!("Struct type is checked is the previous function, so this should be unreachable");
640+
};
641+
635642
let mut struct_types_map: HashMap<String, DataType> = if let DataType::Struct(
636643
fields,
637644
) = &data_types[0]
@@ -650,7 +657,7 @@ pub fn try_type_union_resolution_with_struct(
650657
let field_name = field.name();
651658
if let Some(existing_type) = struct_types_map.get_mut(field_name) {
652659
if let Some(coerced_type) =
653-
type_union_resolution_coercion(&field.data_type(), existing_type)
660+
type_union_resolution_coercion(field.data_type(), existing_type)
654661
{
655662
*existing_type = coerced_type;
656663
} else {
@@ -669,6 +676,20 @@ pub fn try_type_union_resolution_with_struct(
669676
}
670677
}
671678

679+
if is_unique {
680+
let new_fields =
681+
first_fields
682+
.iter()
683+
.map(|f| {
684+
Arc::new(Arc::unwrap_or_clone(Arc::clone(f)).with_data_type(
685+
struct_types_map.get(f.name()).unwrap().to_owned(),
686+
))
687+
})
688+
.collect();
689+
let unified_struct = DataType::Struct(new_fields);
690+
return Ok(vec![unified_struct; data_types.len()]);
691+
}
692+
672693
let mut final_struct_types = vec![];
673694
for s in data_types {
674695
let mut new_fields = vec![];

datafusion/functions-nested/src/make_array.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl ScalarUDFImpl for MakeArray {
128128

129129
fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> {
130130
let mut errors = vec![];
131-
match try_type_union_resolution_with_struct(arg_types) {
131+
match try_type_union_resolution_with_struct(arg_types, true) {
132132
Ok(r) => return Ok(r),
133133
Err(e) => {
134134
errors.push(e);

0 commit comments

Comments
 (0)