You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "jayzhan211 (via GitHub)" <gi...@apache.org> on 2025/02/01 00:48:28 UTC
Re: [PR] Do not rename struct fields when coercing types in `CASE` [datafusion]
jayzhan211 commented on code in PR #14384:
URL: https://github.com/apache/datafusion/pull/14384#discussion_r1938125583
##########
datafusion/expr-common/src/type_coercion/binary.rs:
##########
@@ -961,23 +961,31 @@ fn struct_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<DataType>
return None;
}
- let types = std::iter::zip(lhs_fields.iter(), rhs_fields.iter())
+ let coerced_types = std::iter::zip(lhs_fields.iter(), rhs_fields.iter())
.map(|(lhs, rhs)| comparison_coercion(lhs.data_type(), rhs.data_type()))
.collect::<Option<Vec<DataType>>>()?;
- let fields = types
+ // preserve the field name and nullability
+ let orig_fields = std::iter::zip(lhs_fields.iter(), rhs_fields.iter());
+
+ let fields: Vec<FieldRef> = coerced_types
.into_iter()
- .enumerate()
- .map(|(i, datatype)| {
- Arc::new(Field::new(format!("c{i}"), datatype, true))
- })
- .collect::<Vec<FieldRef>>();
+ .zip(orig_fields)
+ .map(|(datatype, (lhs, rhs))| coerce_fields(datatype, lhs, rhs))
+ .collect();
Some(Struct(fields.into()))
}
_ => None,
}
}
+/// returns the result of coercing two fields to a common type
+fn coerce_fields(common_type: DataType, lhs: &FieldRef, rhs: &FieldRef) -> FieldRef {
+ let is_nullable = lhs.is_nullable() || rhs.is_nullable();
+ let name = lhs.name(); // pick the name from the left field
Review Comment:
Yes, I think type union resolution is the correct on for CASE
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org