linliu-code opened a new pull request, #11959:
URL: https://github.com/apache/arrow-rs/pull/11959

   # Which issue does this PR close?
   
   - Closes #11958
   
   # Rationale for this change
   
   With a reader schema supplied, a writer union with more than two branches 
that references named types by name (e.g. `["null", "Bw", "Sw"]`) maps every 
by-name branch to reader branch 0. A non-null value is then decoded as `null`, 
its bytes are not consumed, and the remaining data is decoded from the wrong 
offset (`bad varint` and similar errors), even when the reader schema equals 
the writer schema.
   
   The cause is that `resolve_type` accepted a `TypeName::Ref` on either side 
against any schema on the other side, so `find_best_union_match` took the first 
reader branch it tried as an exact match.
   
   # What changes are included in this PR?
   
   - `resolve_type` now resolves a named-type reference only against a 
reference or named definition (record, enum, fixed) with the same full name, or 
a matching alias, using the existing `names_match` / `named_type_namespace` 
helpers. Otherwise it returns a `SchemaError`, so union matching moves on to 
the next reader branch.
   - When the names match, behavior is unchanged (`parse_type` of the reader 
schema).
   
   The case of a reference whose named type is defined differently by writer 
and reader (described in the issue) is not addressed here.
   
   # Are these changes tested?
   
   Yes. `test_union_ref_branches_resolve_by_name` in 
`arrow-avro/src/reader/mod.rs` writes records with `apache_avro` using a schema 
whose second union references the records defined inline in the first, and 
checks that decoding with no reader schema and with reader schema == writer 
schema both return the written branch and value for each row (`Bw`, `Sw`, and 
`null`) and the field after the union. It fails without the change in 
`codec.rs`.
   
   # Are there any significant user-facing changes?
   
   No API changes. Schemas that previously resolved a by-name union branch to 
the wrong reader branch now resolve it to the branch with the same name. A 
reference resolved against a reader schema that names a different type (outside 
of a union) now returns a schema error instead of silently decoding with the 
reader's type.
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


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