brancz commented on code in PR #8871:
URL: https://github.com/apache/arrow-rs/pull/8871#discussion_r2543214523
##########
arrow-cast/src/cast/mod.rs:
##########
@@ -10836,11 +10890,11 @@ mod tests {
let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
let struct_array = StructArray::from(vec![
(
- Arc::new(Field::new("b", DataType::Boolean, false)),
+ Arc::new(Field::new("a", DataType::Boolean, false)),
Review Comment:
It turns out these tests were actually wrong to begin with, have a look at
the names of the columns, how can a/b be cast to b/c? They only ever worked by
accident, and now that we test whether they match, they needed to be fixed.
##########
arrow-cast/src/cast/mod.rs:
##########
@@ -221,12 +221,34 @@ pub fn can_cast_types(from_type: &DataType, to_type:
&DataType) -> bool {
Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) |
Decimal256(_, _),
) => true,
(Struct(from_fields), Struct(to_fields)) => {
- from_fields.len() == to_fields.len()
- && from_fields.iter().zip(to_fields.iter()).all(|(f1, f2)| {
+ if from_fields.len() != to_fields.len() {
+ return false;
+ }
+
+ // fast path, all field names are in the same order and same
number of fields
+ if from_fields
+ .iter()
+ .zip(to_fields.iter())
+ .all(|(f1, f2)| f1.name() == f2.name())
+ {
+ return from_fields.iter().zip(to_fields.iter()).all(|(f1, f2)|
{
// Assume that nullability between two structs are
compatible, if not,
// cast kernel will return error.
can_cast_types(f1.data_type(), f2.data_type())
- })
+ });
+ }
+
+ // slow path, we match the fields by name
Review Comment:
I was also going back and forth, but I decided that an additional allocation
in the average case would be far worse in perf cost than comparisons. We're
heavily profiling all these code paths, if it's significant, we will come back
and improve the perf! 😄
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]