mzabaluev-flarion commented on code in PR #9328:
URL: https://github.com/apache/arrow-rs/pull/9328#discussion_r2781933353


##########
arrow-avro/src/codec.rs:
##########
@@ -1533,62 +1550,35 @@ impl<'a> Maker<'a> {
                     nullable_union_variants(reader_variants)
                 {
                     let mut dt = self.resolve_type(writer_non_union, 
non_null_branch, namespace)?;
-                    let non_null_idx = match nullability {
-                        Nullability::NullFirst => 1,
-                        Nullability::NullSecond => 0,
-                    };
                     #[cfg(feature = "avro_custom_types")]
                     Self::propagate_nullability_into_ree(&mut dt, nullability);
                     dt.nullability = Some(nullability);
-                    let promotion = Self::coercion_from(&dt);
-                    dt.resolution = Some(ResolutionInfo::Union(ResolvedUnion {
-                        writer_to_reader: Arc::from(vec![Some((non_null_idx, 
promotion))]),
-                        writer_is_union: false,
-                        reader_is_union: true,
-                    }));
+                    // Ensure resolution is set to a non-Union variant to 
suppress
+                    // reading the union tag which is the default behavior.
+                    if dt.resolution.is_none() {
+                        dt.resolution = 
Some(ResolutionInfo::Promotion(Promotion::Direct));
+                    }
                     Ok(dt)
                 } else {
-                    let mut best_match: Option<(usize, AvroDataType, 
Promotion)> = None;
-                    for (i, variant) in reader_variants.iter().enumerate() {
-                        if let Ok(resolved_dt) =
-                            self.resolve_type(writer_non_union, variant, 
namespace)
-                        {
-                            let promotion = Self::coercion_from(&resolved_dt);
-                            if promotion == Promotion::Direct {
-                                best_match = Some((i, resolved_dt, promotion));
-                                break;
-                            } else if best_match.is_none() {
-                                best_match = Some((i, resolved_dt, promotion));
-                            }
-                        }
-                    }
-                    let Some((match_idx, match_dt, promotion)) = best_match 
else {
+                    let Some((match_idx, resolution)) =
+                        self.find_best_union_match(writer_non_union, 
reader_variants, namespace)
+                    else {
                         return Err(ArrowError::SchemaError(
                             "Writer schema does not match any reader union 
branch".to_string(),
                         ));
                     };
-                    let mut children = 
Vec::with_capacity(reader_variants.len());
-                    let mut match_dt = Some(match_dt);
-                    for (i, variant) in reader_variants.iter().enumerate() {
-                        if i == match_idx {
-                            if let Some(mut dt) = match_dt.take() {
-                                if matches!(dt.resolution, 
Some(ResolutionInfo::Promotion(_))) {
-                                    dt.resolution = None;
-                                }
-                                children.push(dt);
-                            }
-                        } else {
-                            children.push(self.parse_type(variant, 
namespace)?);
-                        }
-                    }
+                    let children = reader_variants
+                        .iter()
+                        .map(|variant| self.parse_type(variant, namespace))
+                        .collect::<Result<Vec<_>, _>>()?;

Review Comment:
   Since the writer type is not a union in this case, the only resolution we 
need is with the best matching union variant that is found above.
   
   A case could be made for not processing the variant schemas the second time 
to build the field information for the codec, but previous code does that as 
well since the loop in find_best_union_match exits early upon finding the 
direct match. Since schema resolution is not on a critical stage for 
performance, I left it to work as before.



-- 
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]

Reply via email to