Repository navigation
Conversation
…gative values from_microseconds_since_epoch used truncating `/` and `%`, so for a negative micros with a fractional second the `ns` remainder was negative. `ns as u32` then wrapped to a value >= 1e9 and DateTime::from_timestamp rejected a valid pre-epoch instant as InvalidTimestamp. This broke the round-trip invariant from_microseconds_since_epoch(t.as_microseconds_since_epoch()) == t for any negative Timestamp - whole-second negatives happened to work, only sub-second ones failed (e.g. -1_500_000, i.e. 1.5s before the epoch). Use div_euclid/rem_euclid so the nanosecond remainder is always in [0, 1e6). Positive values are unchanged (div_euclid/rem_euclid equal / and % there). Adds a round-trip test over positive, negative and sub-second-negative micros.
This branch has not been deployed
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.
What
Timestamp::from_microseconds_since_epochrejects valid sub-second negative (pre-epoch) timestamps.The bug
graph/src/data/store/scalar/timestamp.rs:Rust's
%truncates toward zero, so for a negativemicroswith a fractional second the remainder is negative →nsis negative →ns as u32wraps to a huge value (≥ 1e9) →DateTime::from_timestampreturnsNone→ the instant is wrongly rejected asInvalidTimestamp.This breaks the round-trip invariant
from_microseconds_since_epoch(t.as_microseconds_since_epoch()) == tfor any negativeTimestamp. Whole-second negatives (e.g.-1_000_000) happen to work; only sub-second negatives (e.g.-1_500_000, 1.5s before epoch) fail. It's reachable from subgraph mappings (Timestamp.fromMicroseconds) and GraphQL arg coercion — the symptom is a spurious parse/coercion error (no panic, no corruption).The fix
Use
div_euclid/rem_euclidso the nanosecond remainder is always in[0, 1e6). Positive values are unchanged.Tests
Adds a round-trip test over positive, negative and sub-second-negative micros.
Verification note
The function is self-contained (chrono + integer arithmetic). I verified the exact arithmetic against the pinned
chrono = 0.4.45in a standalone crate: the old//%form returnsNonefor-1_500_000, and thediv_euclid/rem_euclidform round-trips{0, ±1, ±1_500_000, ±999_999, -1_000_000, 1.7e15}. I couldn't run the in-cratecargo test -p graphlocally (thegraphcrate needs system OpenSSL, unavailable in my environment), so CI will confirm the added test in situ.