Python: Handle optional fields in Avro conversion#5796
Merged
Fokko merged 1 commit intoapache:masterfrom Sep 20, 2022
Merged
Conversation
Fokko
approved these changes
Sep 19, 2022
Contributor
Fokko
left a comment
There was a problem hiding this comment.
This is great, thanks @joshuarobinson
singhpk234
approved these changes
Sep 19, 2022
rdblue
reviewed
Sep 20, 2022
| def _(list_type: ListType, values: List[Any]) -> Any: | ||
| """In the case of a list, we'll go over the elements in the list to handle complex types""" | ||
| return [_convert_pos_to_dict(list_type.element_type, value) for value in values] | ||
| return [_convert_pos_to_dict(list_type.element_type, value) for value in values] if values is not None else None |
Contributor
There was a problem hiding this comment.
Isn't there the same problem when calling values.get(pos) in the method for StructType?
Contributor
There was a problem hiding this comment.
Great catch! Looks like we need another guard there as well:
def test_null_struct_convert_pos_to_dict():
data = _convert_pos_to_dict(
Schema(
NestedField(
name="field",
field_id=1,
field_type=StructType(
NestedField(2, "required_field", StringType(), True),
NestedField(3, "optional_field", IntegerType())
),
required=False
)
),
AvroStruct([None]),
)
assert data["field"] is NoneRaises AttributeError:
> return {field.name: _convert_pos_to_dict(field.field_type, values.get(pos)) for pos, field in enumerate(struct_type.fields)}
E AttributeError: 'NoneType' object has no attribute 'get'
pyiceberg/manifest.py:176: AttributeError
Contributor
Author
There was a problem hiding this comment.
updated PR with the StructType fix as well, thanks for the catch
rdblue
approved these changes
Sep 20, 2022
Contributor
rdblue
left a comment
There was a problem hiding this comment.
The changes look good to me, but I think there's still a possibility that this would happen in the method that handles structs.
Found by processing fields in manifestentry with empty split_offsets field. For pos_to_dict, check if values is None before processing as list, struct, or dict. Added unit tests to verify. Thanks to @Fokko for the fix.
4c1d127 to
a168fc0
Compare
Contributor
|
Thanks for fixing this @joshuarobinson |
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.
Found by processing fields in manifestentry with empty split_offsets field.
For pos_to_dict, check if values is None before processing as list or dict. Add two unit tests to verify.
Thanks to @Fokko for the fix.