You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by "anishmehta24 (via GitHub)" <gi...@apache.org> on 2026/09/23 10:13:13 UTC

[I] [Bug]: Timestamp(float seconds) truncates instead of rounding, losing a microsecond [beam]

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

   ### What happened?
   
   `Timestamp(seconds)` truncates instead of rounding when `seconds` is a float, so it can land one microsecond below the value the caller passed.
   
   The constructor does:
   
   ```python
   total = int(seconds * _POW_10[precision]) + int(subseconds)
   ```
   
   `seconds * 1e6` is a float multiplication, so for many values it lands just under the exact integer (`2.000002 * 1e6 == 2000001.9999999998`), and `int()` truncates toward zero rather than rounding to nearest:
   
   ```python
   >>> from apache_beam.utils.timestamp import Timestamp
   >>> Timestamp(2.000002).micros
   2000001          # expected 2000002
   >>> Timestamp(1.000001).micros
   1000000          # expected 1000001
   ```
   
   This hits the most common way of building a timestamp, `Timestamp.of(time.time())`:
   
   ```python
   >>> t = time.time()      # 1790158166.5462177
   >>> Timestamp.of(t).micros
   1790158166546217         # the float is ...546218 to the microsecond
   ```
   
   Taking microsecond values, converting them to float seconds the way a caller would, and feeding them back in, **1.2% come back one microsecond low** (2436 of 200000 random epoch-range values).
   
   The class docstring says "If `seconds` is a float, the fractional part will be captured up to `precision` digits", and explains that integer storage is used precisely because "floating point values lose precision" — but the conversion into that integer storage is itself lossy. Truncation is also asymmetric about zero, which sits oddly with "an int of floored seconds".
   
   ### Issue Priority
   
   Priority: 3 (minor)
   
   ### Issue Components
   
   - [X] Component: Python SDK
   - [ ] Component: Java SDK
   - [ ] Component: Go SDK
   - [ ] Component: Typescript SDK
   - [ ] Component: IO connector
   - [ ] Component: Beam YAML
   - [ ] Component: Beam examples
   - [ ] Component: Beam playground
   - [ ] Component: Beam katas
   - [ ] Component: Website
   - [ ] Component: Infrastructure
   - [ ] Component: Spark Runner
   - [ ] Component: Flink Runner
   - [ ] Component: Samza Runner
   - [ ] Component: Twister2 Runner
   - [ ] Component: Hazelcast Jet Runner
   - [ ] Component: Google Cloud Dataflow Runner
   


-- 
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: github-unsubscribe@beam.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] [Bug]: Timestamp(float seconds) truncates instead of rounding, losing a microsecond [beam]

Posted by "jrmccluskey (via GitHub)" <gi...@apache.org>.
jrmccluskey closed issue #40235: [Bug]: Timestamp(float seconds) truncates instead of rounding, losing a microsecond
URL: https://github.com/apache/beam/issues/40235


-- 
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: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] [Bug]: Timestamp(float seconds) truncates instead of rounding, losing a microsecond [beam]

Posted by "Deepika-lab (via GitHub)" <gi...@apache.org>.
Deepika-lab commented on issue #40235:
URL: https://github.com/apache/beam/issues/40235#issuecomment-5807404654

   I'd like to work on this — the fix is to round (rather than truncate) the float-to-micros conversion in the Timestamp constructor, plus a regression test. .take-issue


-- 
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: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org