mirror of
https://github.com/allankoechke/turso.git
synced 2026-11-30 11:12:58 +00:00
The built-in `date` type defined only `<`, so `+` and `-` fell back to
standard numeric arithmetic on the date text, and `'2025-01-05'` was
read as 2025:
```sql
CREATE TABLE t(a date, b date);
INSERT INTO t VALUES ('2025-01-05', '2025-01-01');
SELECT a + 1, a - 1, a - b FROM t;
-- before: 2026 | 2024 | 0
-- after: 2025-01-06 | 2025-01-04 | 4
```
`date` now declares `'+' date_plus` and `'-' date_minus`. `date_minus`
checks at run time whether the right side is a number of days, which
returns a date, or another date, which returns the number of days
between them, as PostgreSQL does. `integer + date` works too. `date +
1.5` and `integer - date` raise an error.
Adding the operators was not enough on its own. A custom type's
arithmetic operator only ran for another column of the same type or for
a literal compatible with the type's `value` input. So `date + 1`, `date
+ -1`, `date + int_col` and `date + ?` never reached it. For types whose
DECODE returns the stored value unchanged, such as `date`, arithmetic
operators now also run for any other operand that is not a column of a
different custom type, and the operand is passed unencoded. Types whose
DECODE changes the value, such as `numeric` or a user-defined `cents`,
keep the old behavior, because an unencoded operand would not match the
format of the other arguments. Comparisons are unchanged.
One difference from PostgreSQL remains: `date + '1'` raises an error. A
quoted literal matches `date`'s text input, so it goes through the
`date` encode step and is read as a date, not as the integer 1.
Fixes #9608
Autofixer wrote this change with Claude Code in an isolated sandbox. A
separate verifier agent tested and reviewed it in its own container
before this PR was opened. A maintainer must review it before merge.
Reviewed-by: Jussi Saurio <jussi.saurio@gmail.com>
Closes #9715