-
Notifications
You must be signed in to change notification settings - Fork 852
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
snowflake: fix timestamps at extreme ranges #3220
Conversation
@@ -145,7 +145,7 @@ func fls128(n Num) int { | |||
if n.hi != 0 { | |||
return 127 - bits.LeadingZeros64(uint64(n.hi)) | |||
} | |||
return 64 - bits.LeadingZeros64(n.lo) | |||
return 63 - bits.LeadingZeros64(n.lo) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was the bug fix, the rest is mostly tests or refactoring/clean up :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Heh, that reminded me of https://pkg.go.dev/math/rand#Int63 :)
1634668
to
fedf8ff
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, just a small nit. Feel free to 🐑 🚀
// Compare returns -1 if a < b, 0 if a == b, and 1 if a > b. | ||
func Compare(a, b Num) int { | ||
if a.hi < b.hi || (a.hi == b.hi && a.lo < b.lo) { | ||
return -1 | ||
} | ||
if a.hi == b.hi && a.lo == b.lo { | ||
return 0 | ||
} | ||
return 1 | ||
} | ||
|
||
// CompareUnsigned returns -1 if |a| < |b|, 0 if a == b, and 1 if |a| > |b|. | ||
func CompareUnsigned(a, b Num) int { | ||
if uint64(a.hi) < uint64(b.hi) || (a.hi == b.hi && a.lo < b.lo) { | ||
return -1 | ||
} | ||
if a.hi == b.hi && a.lo == b.lo { | ||
return 0 | ||
} | ||
return 1 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we can leverage https://pkg.go.dev/cmp#Compare for these. Not sure if that's possible
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes we can make this much more readable :) Thanks for this!
@@ -145,7 +145,7 @@ func fls128(n Num) int { | |||
if n.hi != 0 { | |||
return 127 - bits.LeadingZeros64(uint64(n.hi)) | |||
} | |||
return 64 - bits.LeadingZeros64(n.lo) | |||
return 63 - bits.LeadingZeros64(n.lo) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Heh, that reminded me of https://pkg.go.dev/math/rand#Int63 :)
6262bc7
to
ac75908
Compare
This all boiled down to an amazing off by one error in calculating the MSB when there are less than 64 bits in the int128. Added a bunch of tests around these values at every layer of the stack, each was really an artifact of my debugging :)
When preserving logical types, preserve time values as timestamps instead of duration strings, timestamps are much more natural to work with in bloblang.
ac75908
to
ca5139b
Compare
Fix a bug where timestamps at the extreme ends of the spectrum could be encoded incorrectly due to a bug in
int128.Div
. Also fix Time types in avro to be something more usable.