Yeah you are right about the integer overflow.
Consider this query: select date_bin('15 minutes'::interval, timestamp '294276-12-30 10:24:00', timestamp '4000-12-20 23:00:00 BC');
It will return 294276-12-30 10:31:49.551616 when it should be 294276-12-30 10:15:00, which happens because source timestamp is close to INT64_MAX and origin timestamp is negative, causing an integer overflow.
So, the subsequent calculations are wrong.
I fixed the integer overflow and original bug and added test cases in my 3 patches in this reply below:
v2-0001:
- Fixed both the original bug discussed and the integer overflow issues (and used your suggestion to store the modulo result).
- Any timestamp used will output the correct date_bin() result.
- I have used INT64 -> UINT64 mapping in order to ensure no integer overflows are possible.
- The only additional cost is 3 subtractions/addition to do the INT64 -> UINT64 mapping for timestamp, origin and final result.
- It is probably possible to tackle the integer overflow issue without casting but, from my attempts, it seemed much more convoluted and complex.
- Implemented the fix for both timestamp_bin() and timestamptz_bin().
v2-0002:
- Added multiple test cases in timestamp.sql for integer overflow by testing with timestamps around INT64_MIN and INT64_MAX.
- Added test case for the original bug where source timestamp is a valid binned date already and does not require the additional stride interval subtraction.
v2-0003:
- Exactly the same as v2-0002 but for timestamptz.sql
Also, I would like to create a new patch on the 2024-03 commitfest, but since I just created my account yesterday I get this error:
"The site you are trying to log in to (
commitfest.postgresql.org) requires a cool-off period between account creation and logging in. You have not passed the cool off period yet."
How long is the cool off period, so that I can create a new patch in the commitfest before submissions close after tomorrow.
Alternatively, is it possible for someone to open a new patch on my behalf linking this email thread, so it can be added to the 2024-03 commitfest?
Best regards,
Moaaz Assali