Conversation
_parse_struct() wraps nullable array and map fields in a [type, null] Avro union via _is_nullable(), but nullable struct fields (e.g. the 'candidate' field of a ZTF alert) were passed through as plain records. Spark's own to_avro() serializer always writes a union discriminator byte for nullable fields regardless of the published schema, so any strict Avro reader following the schema this function produces fails to decode nullable struct fields with an out-of-range/index error. Downstream, this has been worked around ad hoc by patching the generated schema JSON after the fact (see astrolabsoftware/ztf.fink-portal.org's spark_ztf_inference_feed.py); fixing it here removes the need for that per-caller patch.
There was a problem hiding this comment.
🟢 Approval recommended
The change is small, consistent with existing nullable handling for arrays/maps, and directly addresses the described Avro decoding failure mode.
Pull request overview
This PR fixes Avro schema generation for Spark to_avro() compatibility by ensuring nullable struct fields are encoded as Avro unions, matching Spark’s serialization behavior and preventing strict Avro readers from failing on nullable records (e.g., ZTF candidate).
Changes:
- Wrap nullable
structfields produced by_parse_struct()using the existing_is_nullable()union logic (consistent with array/map handling).
File summaries
| File | Description |
|---|---|
fink_utils/spark/schema_converter.py |
Ensures nullable nested structs are emitted as [recordType, "null"] unions so readers expecting unions can decode Spark-serialized data. |
Review details
- Files reviewed: 1/1 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
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.
_parse_struct() wraps nullable array and map fields in a [type, null] Avro union via _is_nullable(), but nullable struct fields (e.g. the 'candidate' field of a ZTF alert) were passed through as plain records. Spark's own to_avro() serializer always writes a union discriminator byte for nullable fields regardless of the published schema, so any strict Avro reader following the schema this function produces fails to decode nullable struct fields with an out-of-range/index error.
I hit this while building a Kubernetes AI inference feature for the
Fink science portal: a preprocessing container consumes ZTF alerts straight
from a topic whose schema was published with
to_avro(), and fails todeserialize every message because of the
candidatefield specifically:"error": "list index out of range", "topic": "ftransfer_ztf_...",
"partition": 6, "offset": 0