anishmehta24 opened a new issue, #41557:
URL: https://github.com/apache/beam/issues/41557

   ### What happened?
   
   `apache_beam.utils.timestamp.Duration.to_proto()` builds a 
`google.protobuf.Duration` the way `Timestamp.to_proto()` builds a `Timestamp`, 
with seconds rounded down and a non-negative `nanos`. That is right for a 
`Timestamp`, but a `Duration` proto with a nonzero `nanos` must have `nanos` 
with the same sign as `seconds`, and `seconds` must be 0 for a duration under 
one second. So every negative duration with a fractional part becomes an 
invalid proto:
   
   ```python
   from apache_beam.utils.timestamp import Duration
   
   print(Duration(micros=-1).to_proto())        # seconds: -1 nanos: 999999000  
 (expected seconds: 0 nanos: -1000)
   print(Duration(micros=-1500000).to_proto())  # seconds: -2 nanos: 500000000  
 (expected seconds: -1 nanos: -500000000)
   Duration(micros=-1500000).to_proto().ToJsonString()
   # ValueError: Duration is not valid: Sign mismatch.
   ```
   
   `Duration.from_proto` reads it back to the same value, but anything else 
that checks the proto rejects it, such as protobuf's own `ToJsonString()` or 
Java's `Durations.checkValid`. `proto_utils.from_micros`, which the runner API 
code uses, already builds the proto with `Duration.FromMicroseconds`, so the 
two disagree for negative values.
   
   The fix would be `FromMicroseconds(self.micros)` in `to_proto` too. I have a 
PR with a test ready.
   
   ### Issue Priority
   
   Priority: 3 (minor)
   
   ### Issue Components
   
   - [x] Component: Python SDK
   


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