You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avro.apache.org by GitBox <gi...@apache.org> on 2021/03/21 15:43:21 UTC
[GitHub] [avro] subhashb opened a new pull request #1148: AVRO-3065 Introduce UUID logical type to Python implementation
subhashb opened a new pull request #1148:
URL: https://github.com/apache/avro/pull/1148
This PR introduces the UUID logical type implementation that was missing in the
primary python implementation. A new `UUIDSchema` has been introduced that
validates for valid UUID4 string values.
Closes: https://issues.apache.org/jira/browse/AVRO-3065
Make sure you have checked _all_ steps below.
### Jira
- [x] My PR addresses the following [Avro Jira](https://issues.apache.org/jira/browse/AVRO/) issues and references them in the PR title. For example, "AVRO-3065: Add support for uuid logical type to Python implementation"
- https://issues.apache.org/jira/browse/AVRO-3065
- In case you are adding a dependency, check if the license complies with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
### Tests
- [x] My PR adds the following unit tests:
- Test for valid UUID Logical Type in Schema
- Test for valid UUID in values
### Commits
- [x] My commits all reference Jira issues in their subject lines. In addition, my commits follow the guidelines from "[How to write a good git commit message](https://chris.beams.io/posts/git-commit/)":
1. Subject is separated from body by a blank line
1. Subject is limited to 50 characters (not including Jira issue reference)
1. Subject does not end with a period
1. Subject uses the imperative mood ("add", not "adding")
1. Body wraps at 72 characters
1. Body explains "what" and "why", not "how"
### Documentation
- [x] In case of new functionality, my PR adds documentation that describes how to use it.
- All the public functions and the classes in the PR contain Javadoc that explain what it does
--
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [avro] RyanSkraba commented on pull request #1148: AVRO-3065: Introduce UUID logical type to Python implementation
Posted by GitBox <gi...@apache.org>.
RyanSkraba commented on pull request #1148:
URL: https://github.com/apache/avro/pull/1148#issuecomment-821122824
Hey, if nobody has any other comments, let's just merge this! Thanks for the contribution!
--
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [avro] RyanSkraba commented on a change in pull request #1148: AVRO-3065: Introduce UUID logical type to Python implementation
Posted by GitBox <gi...@apache.org>.
RyanSkraba commented on a change in pull request #1148:
URL: https://github.com/apache/avro/pull/1148#discussion_r602052542
##########
File path: lang/py/avro/schema.py
##########
@@ -1069,6 +1070,33 @@ def validate(self, datum):
def __eq__(self, that):
return self.props == that.props
+
+#
+# uuid Type
+#
+
+
+class UUIDSchema(LogicalSchema, PrimitiveSchema):
+ def __init__(self, other_props=None):
+ LogicalSchema.__init__(self, avro.constants.UUID)
+ PrimitiveSchema.__init__(self, 'string', other_props)
+
+ def to_json(self, names=None):
+ return self.props
+
+ def validate(self, datum):
+ try:
+ val = uuid.UUID(datum, version=4)
+ except ValueError:
+ # If it's a value error, then the string
+ # is not a valid hex code for a UUID.
+ return False
Review comment:
```suggestion
return None
```
for consistency with the others
--
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [avro] subhashb commented on a change in pull request #1148: AVRO-3065: Introduce UUID logical type to Python implementation
Posted by GitBox <gi...@apache.org>.
subhashb commented on a change in pull request #1148:
URL: https://github.com/apache/avro/pull/1148#discussion_r602480611
##########
File path: lang/py/avro/schema.py
##########
@@ -1069,6 +1070,33 @@ def validate(self, datum):
def __eq__(self, that):
return self.props == that.props
+
+#
+# uuid Type
+#
+
+
+class UUIDSchema(LogicalSchema, PrimitiveSchema):
+ def __init__(self, other_props=None):
+ LogicalSchema.__init__(self, avro.constants.UUID)
+ PrimitiveSchema.__init__(self, 'string', other_props)
+
+ def to_json(self, names=None):
+ return self.props
+
+ def validate(self, datum):
+ try:
+ val = uuid.UUID(datum, version=4)
+ except ValueError:
+ # If it's a value error, then the string
+ # is not a valid hex code for a UUID.
+ return False
Review comment:
Handled as part of https://github.com/apache/avro/pull/1148/commits/f7ec8498193cbd2d853489aedefdcbedc86315a4
--
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [avro] RyanSkraba commented on a change in pull request #1148: AVRO-3065: Introduce UUID logical type to Python implementation
Posted by GitBox <gi...@apache.org>.
RyanSkraba commented on a change in pull request #1148:
URL: https://github.com/apache/avro/pull/1148#discussion_r602051594
##########
File path: lang/py/avro/test/test_io.py
##########
@@ -80,7 +80,7 @@
'{"type": "long", "logicalType": "timestamp-micros"}',
datetime.datetime(2000, 1, 18, 2, 2, 1, 123499, tzinfo=avro.timezones.tst)
),
- ('{"type": "string", "logicalType": "uuid"}', u'12345abcd'),
+ ('{"type": "string", "logicalType": "uuid"}', u'570feebe-2bbc-4937-98df-285944e1dbbd'),
Review comment:
For the test case, should this be:
```suggestion
('{"type": "string", "logicalType": "uuid"}', uuid.UUID('570feebe-2bbc-4937-98df-285944e1dbbd')),
```
I'm not _sure_ if this suggestion is correct, but it's what I would expect once UUID logical type is implemented!
Since this is a new feature, should we go with the expectation that the in-memory representation of a UUID datum is a UUID object instead of a string? Should we accept either/or when serializing, but always deserialize to one or the other? What do you think?
--
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [avro] subhashb commented on pull request #1148: AVRO-3065: Introduce UUID logical type to Python implementation
Posted by GitBox <gi...@apache.org>.
subhashb commented on pull request #1148:
URL: https://github.com/apache/avro/pull/1148#issuecomment-808399113
Hey @RyanSkraba!
Thank you for the review.
Yes, I was thinking primarily about backward compatibility when I represented UUID as a string.
Some aspects (and past experiences) that made string representation a better option for me:
- UUID objects are not serializable by default.
- A project persisting objects (with a UUID logical field) from Avro today will need to be enhanced to handle UUID objects (you mentioned this too) manually
- Marshmallow's UUID field accepts a string but always returns a UUID object
- Django has a UUID Field, but Postgres is the only database with native UUID data type support. Rest interpret the value as a string. Using the Django UUID field still requires you to generate the UUID yourself.
- SQLAlchemy treats UUIDs as strings and needs `as_uuid=True` to explicitly treat the value as a UUID object.
Having said that, UUID object representation might be a better long-term option. Please let me know what you think.
--
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [avro] RyanSkraba merged pull request #1148: AVRO-3065: Introduce UUID logical type to Python implementation
Posted by GitBox <gi...@apache.org>.
RyanSkraba merged pull request #1148:
URL: https://github.com/apache/avro/pull/1148
--
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [avro] RyanSkraba commented on pull request #1148: AVRO-3065: Introduce UUID logical type to Python implementation
Posted by GitBox <gi...@apache.org>.
RyanSkraba commented on pull request #1148:
URL: https://github.com/apache/avro/pull/1148#issuecomment-809994391
Thanks for the explanation -- your reasoning is sound. Especially since the [`uuid`](https://docs.python.org/3/library/uuid.html) package doesn't really add a lot of functionality over strings (in comparison to using `datetime` instances over numbers).
--
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [avro] subhashb commented on pull request #1148: AVRO-3065: Introduce UUID logical type to Python implementation
Posted by GitBox <gi...@apache.org>.
subhashb commented on pull request #1148:
URL: https://github.com/apache/avro/pull/1148#issuecomment-808404523
https://github.com/apache/avro/pull/1148/commits/f7ec8498193cbd2d853489aedefdcbedc86315a4 updates the validation to accept UUIDs of different versions, instead of being locked to version 4.
--
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org