[Bug] date_add overflows while scaling week and quarter offsets
- Dominant language
- C++
- Stars
- 177
- Forks
- 107
- Avg merge
- 3d 15h
- Merged PRs (30d)
- 52
Description
### Summary
`date_add` accepts an `int32_t` offset, but `addToDate` scales week and quarter offsets using signed 32-bit multiplication. This can overflow before the offset is passed to the date library.
### Affected revision and environment
- Revision: `94ca12a44d01764cff389964b202a0e0a6ab0c6d` (`main`, fetched 2026-08-22)
- Platform inspected: Windows 11
- The source-level defect is independent of platform; the project documents Linux and experimental macOS as supported build platforms.
### Reproduction
For example, this accepted offset overflows when converted from weeks to days:
```sql
SELECT date_add('week', 306783379, DATE '1970-01-01');
```
At `bolt/functions/prestosql/DateTimeImpl.h:111`, the expression `7 * value` evaluates in signed 32-bit arithmetic. `306783379 * 7` is `2147483653`, which is outside the `int32_t` range. The quarter path has the same issue at line 119 with `3 * value`.
### Expected behavior
Offsets that cannot be represented after unit scaling should fail consistently with the existing `date_add` integer-overflow handling, without evaluating undefined signed-overflow arithmetic or producing a wrapped date.
### Actual behavior
The multiplication overflows before the result reaches `::date::days` or `::date::months`. Signed integer overflow is undefined behavior in C++.
### Impact
Large but accepted `date_add` offsets for `week` and `quarter` can yield compiler-dependent incorrect results instead of a deterministic user error.
### Suggested direction
Check the scaled value (or perform the multiplication in a wider type and validate it) before constructing the date duration. A focused regression test can cover the first overflowing week and quarter values.
Contributor guide
Research direction
Start in bolt/functions/prestosql/DateTimeImpl.h at lines 111 and 119, then inspect the existing date_add integer-overflow handling. Reproduce the overflowing week and quarter cases and add focused regression coverage for the first overflowing values. Done means both paths reject unrepresentable scaled offsets consistently without undefined overflow or wrapped dates.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 84/100