Abacn commented on code in PR #22561:
URL: https://github.com/apache/beam/pull/22561#discussion_r955081637


##########
sdks/python/apache_beam/coders/row_coder.py:
##########
@@ -163,10 +165,15 @@ def _nonnull_coder_from_type(field_type):
         _coder_from_type(field_type.map_type.key_type),
         _coder_from_type(field_type.map_type.value_type))
   elif type_info == "logical_type":
-    # Special case for the Any logical type. Just use the default coder for an
-    # unknown Python object.
     if field_type.logical_type.urn == PYTHON_ANY_URN:
+      # Special case for the Any logical type. Just use the default coder for 
an
+      # unknown Python object.
       return typecoders.registry.get_coder(object)
+    elif field_type.logical_type.urn == DATETIME_URN:
+      # Special case for datetime logical type.
+      # DATETIME_URN explicitly uses TimestampCoder which deals with fix length
+      # 8-bytes big-endian-long instead of varint coder.
+      return TimestampCoder()

Review Comment:
   Managed to get a test case using the following code snippet
   
   ```python
   
   def generate_millis():
       # Logical type that deals with millis_instant urn (MillisInstant)
       MillisLogicalType = 
LogicalType._known_logical_types.get_logical_type_by_urn('beam:logical_type:millis_instant:v1')
       # Original Logical type used to represent Timestamp (MicrosInstant)
       TimestampLogicalType = 
LogicalType._known_logical_types.get_logical_type_by_language_type(Timestamp)
       
       LogicalType._known_logical_types.by_language_type[Timestamp] = 
MillisLogicalType
   
       schema = beam.typehints.schemas.named_tuple_to_schema(TestTuple)
       coder = beam.coders.row_coder.RowCoder(schema)
       print("payload = %s" % schema.SerializeToString())
       examples = (TestTuple(
           f_timestamp=Timestamp.from_rfc3339("2020-08-13T14:14:14.123Z"),
           f_string="2020-08-13T14:14:14.123Z",
           f_int=1597328054123),)
       for example in examples:
           print("example = %s" % coder.encode(example))
       
       # recover original registration
       LogicalType._known_logical_types.by_language_type[Timestamp] = 
TimestampLogicalType
   
   ```
   
   The workaround is temporarily change the mapping of Timestamp -> 
MillisInstant logical type. Without it Timestamp always maps to MicrosInstant 
logical type in Python.



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