[SPARK-51961][SQL] Fix from_avro to handle union schemas#55412
Open
yadavay-amzn wants to merge 1 commit intoapache:masterfrom
Open
[SPARK-51961][SQL] Fix from_avro to handle union schemas#55412yadavay-amzn wants to merge 1 commit intoapache:masterfrom
yadavay-amzn wants to merge 1 commit intoapache:masterfrom
Conversation
When a union schema like [{record}, "null"] is passed to both to_avro and
from_avro, to_avro succeeds because AvroSerializer.resolveNullableType
unwraps the union to extract the single non-null record type. However,
from_avro (AvroDataToCatalyst) passes the union schema directly to
AvroDeserializer, which expects a RECORD type, causing an
IncompatibleSchemaException.
The fix adds the same union-unwrapping logic to AvroDataToCatalyst: when
the expected schema is a UNION with a single non-null branch, unwrap it
before passing to AvroDeserializer.
Closes #XXXXX
### What changes were proposed in this pull request?
Added union schema resolution in AvroDataToCatalyst to unwrap single
non-null union branches before passing to AvroDeserializer, mirroring
the existing logic in AvroSerializer.resolveNullableType.
### Why are the changes needed?
from_avro throws IncompatibleSchemaException when given a union schema
like [{record}, "null"], even though to_avro works with the same schema.
### Does this PR introduce _any_ user-facing change?
Yes. from_avro now correctly handles union schemas containing a single
record type and null, matching the behavior of to_avro.
### How was this patch tested?
Added a new test in AvroFunctionsSuite that verifies roundtrip
serialization with a union schema of [record, null].
### Was this patch authored or co-authored using generative AI tooling?
Yes.
1978d29 to
55a6bfb
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
Added union schema resolution in
AvroDataToCatalystto unwrap single non-null union branches before passing toAvroDeserializer, mirroring the existing logic inAvroSerializer.resolveNullableType.Why are the changes needed?
from_avrothrowsIncompatibleSchemaExceptionwhen given a union schema like[{record}, "null"], even thoughto_avroworks with the same schema. The root cause is thatAvroSerializerunwraps the union to find the single non-null record type, butAvroDataToCatalystpasses the union directly toAvroDeserializer, which expects a RECORD.Does this PR introduce any user-facing change?
Yes.
from_avronow correctly handles union schemas containing a single record type and null, matching the behavior ofto_avro.How was this patch tested?
Added a new test in
AvroFunctionsSuitethat verifies roundtrip serialization with a union schema of[record, null].Was this patch authored or co-authored using generative AI tooling?
Yes.