Skip to content

Fix interval text encoding of math.MinInt64 microseconds - #2689

Merged
jackc merged 1 commit into
jackc:masterfrom
spicysharkco:fix-interval-text-min-microseconds
Oct 17, 2026
Merged

jackc merged 1 commit into
jackc:masterfrom
spicysharkco:fix-interval-text-min-microseconds

Conversation

@spicysharkco

Copy link
Copy Markdown
Contributor

Encoding an Interval with Microseconds of math.MinInt64 to text gave --2562047788:00:-54.-775808, which does not scan back. absMicroseconds = -absMicroseconds overflows for that one value.

PostgreSQL can return it: '-2562047788:00:54.775807'::interval - '1 microsecond'::interval prints -2562047788:00:54.775808, and '-infinity'::interval on 17+ carries the same microseconds field. The text encoder is what Interval.Value and DecodeDatabaseSQLValue (binary results) go through.

The magnitude is now a uint64, so it cannot overflow. I added a row to TestIntervalTextEncode. I also ran the encoder against PostgreSQL 18: the new output matches PostgreSQL's own text and scans back to the same Interval.

This is separate from #2065 (interval infinity), which rewrites the same function.

Negating math.MinInt64 overflows, so the text encoder wrote
"--2562047788:00:-54.-775808" for it. PostgreSQL prints that value as
-2562047788:00:54.775808. Take the magnitude as a uint64.
@jackc
jackc merged commit 421afc6 into jackc:master Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants