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


##########
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:
   > If the writer schema is a union with multiple branches resolving to the 
same reader branch
   
   A closer reading of the spec suggests that this should not in fact be 
possible (or should be ignored in non-strict mode?), though the immediate logic 
of union resolution does not prevent it. For example, record types are matched 
by name, and there can't be two record type variants with the same name within 
a schema. So, for each reader branch there should be at most one writer branch, 
and the resolution can be recorded on the resolved reader type.



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