Repository navigation
Commit 0996313
Support date arithmetic with
## Summary
This PR **adds date arithmetic with `INTERVAL`**, building on WordPress#510 by
@samuelmbabhazi:
```sql
SELECT d + INTERVAL 1 DAY;
SELECT d - INTERVAL 5 HOUR;
SELECT d + INTERVAL 1 DAY - INTERVAL 2 HOUR;
```
These queries previously failed with a syntax error. They now use
SQLite’s `DATE()` and `DATETIME()` functions, sharing the translation
with `DATE_ADD()` and `DATE_SUB()`.
## Why the extra fixes
Our current parser groups some expressions differently from MySQL.
Translating them directly can produce wrong results—or update or delete
the wrong rows.
This PR adds safeguards to the implementation in WordPress#510:
- **Reject expressions the parser cannot group correctly**, including
`INTERVAL 1 DAY + d`, `1 + d + INTERVAL 1 DAY`, and `d + INTERVAL 1 DAY
+ x`. Errors suggest adding parentheses.
- **Support `QUARTER` and `SQL_TSI_*` units**, and handle negative and
computed interval values.
- **Reject unsupported units**, including `MICROSECOND` and composite
units such as `HOUR_MINUTE`, instead of silently returning `NULL`.
- **Return dates for date-only inputs**, as MySQL does. Previously,
`'2024-01-01' + INTERVAL 1 DAY` returned `2024-01-02 00:00:00`, so
comparisons with `DATE` values could match the wrong rows. This also
fixes `DATE_ADD()` and `DATE_SUB()`, the first case of WordPress#531.
The supported forms cover the linked issues. The expression restrictions
can be revisited after the planned switch to a parser based on MySQL’s
own grammar.
## Remaining limitations
The existing date translation still differs from MySQL in these cases:
- **Numeric dates:** Values such as `20240101` are not interpreted as
MySQL dates.
- **Interval values:** MySQL converts the value to a whole number. Here,
`INTERVAL 1.5 DAY` adds 36 hours instead of 2 days, and a string such as
`' 1'` returns `NULL` with `+` but works with `-`.
- **Time inputs:** `TIME` values return datetime strings, and times past
midnight wrap around instead of going over 24 hours.
- **Month ends:** Month-end arithmetic can overflow into the following
month.
- **`ADDDATE()` and `SUBDATE()`:** These functions remain unsupported.
Month ends and `ADDDATE()`/`SUBDATE()` are tracked in WordPress#531.
Expected results in the new tests were checked against MySQL 8.4.
Fixes WordPress#454
Fixes WordPress#90
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
- **New Features**
- Improved support for MySQL date and time interval calculations.
- Added support for interval units from seconds through years, including
weeks and quarters.
- Supports chained additions and subtractions, computed intervals, and
parenthesized expressions.
- Date and datetime results are now handled appropriately across
supported calculations.
- **Bug Fixes**
- Improved validation and handling of unsupported or ambiguous interval
expressions.
- Preserved correct NULL behavior and date calculations in updates and
default values.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: Samuel Mbabhazi <samuelmbabhazi@gmail.com>INTERVAL operators (WordPress#539)1 parent e88a817 commit 0996313
3 files changed
Lines changed: 455 additions & 12 deletions
File tree
- packages/mysql-on-sqlite
- src/sqlite
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
330 | 330 | | |
331 | 331 | | |
332 | 332 | | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
333 | 350 | | |
334 | 351 | | |
335 | 352 | | |
| |||
4349 | 4366 | | |
4350 | 4367 | | |
4351 | 4368 | | |
| 4369 | + | |
| 4370 | + | |
4352 | 4371 | | |
4353 | 4372 | | |
4354 | 4373 | | |
| |||
4915 | 4934 | | |
4916 | 4935 | | |
4917 | 4936 | | |
| 4937 | + | |
| 4938 | + | |
| 4939 | + | |
| 4940 | + | |
| 4941 | + | |
| 4942 | + | |
| 4943 | + | |
| 4944 | + | |
| 4945 | + | |
| 4946 | + | |
| 4947 | + | |
| 4948 | + | |
| 4949 | + | |
4918 | 4950 | | |
4919 | 4951 | | |
4920 | 4952 | | |
| 4953 | + | |
| 4954 | + | |
| 4955 | + | |
| 4956 | + | |
| 4957 | + | |
| 4958 | + | |
| 4959 | + | |
| 4960 | + | |
| 4961 | + | |
| 4962 | + | |
| 4963 | + | |
| 4964 | + | |
| 4965 | + | |
| 4966 | + | |
| 4967 | + | |
| 4968 | + | |
| 4969 | + | |
| 4970 | + | |
| 4971 | + | |
| 4972 | + | |
| 4973 | + | |
| 4974 | + | |
| 4975 | + | |
| 4976 | + | |
| 4977 | + | |
| 4978 | + | |
| 4979 | + | |
| 4980 | + | |
| 4981 | + | |
| 4982 | + | |
| 4983 | + | |
| 4984 | + | |
| 4985 | + | |
| 4986 | + | |
| 4987 | + | |
| 4988 | + | |
| 4989 | + | |
| 4990 | + | |
| 4991 | + | |
| 4992 | + | |
| 4993 | + | |
| 4994 | + | |
| 4995 | + | |
| 4996 | + | |
| 4997 | + | |
| 4998 | + | |
| 4999 | + | |
| 5000 | + | |
| 5001 | + | |
| 5002 | + | |
| 5003 | + | |
| 5004 | + | |
| 5005 | + | |
| 5006 | + | |
| 5007 | + | |
| 5008 | + | |
| 5009 | + | |
| 5010 | + | |
| 5011 | + | |
| 5012 | + | |
| 5013 | + | |
| 5014 | + | |
| 5015 | + | |
| 5016 | + | |
4921 | 5017 | | |
4922 | 5018 | | |
4923 | 5019 | | |
| |||
5051 | 5147 | | |
5052 | 5148 | | |
5053 | 5149 | | |
5054 | | - | |
5055 | | - | |
5056 | | - | |
5057 | | - | |
5058 | | - | |
5059 | | - | |
5060 | | - | |
5061 | | - | |
| 5150 | + | |
5062 | 5151 | | |
5063 | | - | |
5064 | | - | |
5065 | | - | |
| 5152 | + | |
5066 | 5153 | | |
5067 | 5154 | | |
5068 | 5155 | | |
| |||
5076 | 5163 | | |
5077 | 5164 | | |
5078 | 5165 | | |
| 5166 | + | |
| 5167 | + | |
| 5168 | + | |
| 5169 | + | |
| 5170 | + | |
| 5171 | + | |
| 5172 | + | |
| 5173 | + | |
| 5174 | + | |
| 5175 | + | |
| 5176 | + | |
| 5177 | + | |
| 5178 | + | |
| 5179 | + | |
| 5180 | + | |
| 5181 | + | |
| 5182 | + | |
| 5183 | + | |
| 5184 | + | |
| 5185 | + | |
| 5186 | + | |
| 5187 | + | |
| 5188 | + | |
| 5189 | + | |
| 5190 | + | |
| 5191 | + | |
| 5192 | + | |
| 5193 | + | |
| 5194 | + | |
| 5195 | + | |
| 5196 | + | |
| 5197 | + | |
| 5198 | + | |
| 5199 | + | |
| 5200 | + | |
| 5201 | + | |
| 5202 | + | |
| 5203 | + | |
| 5204 | + | |
| 5205 | + | |
| 5206 | + | |
| 5207 | + | |
| 5208 | + | |
| 5209 | + | |
| 5210 | + | |
| 5211 | + | |
| 5212 | + | |
| 5213 | + | |
| 5214 | + | |
| 5215 | + | |
| 5216 | + | |
| 5217 | + | |
| 5218 | + | |
| 5219 | + | |
| 5220 | + | |
| 5221 | + | |
5079 | 5222 | | |
5080 | 5223 | | |
5081 | 5224 | | |
| |||
0 commit comments