sqlglot
sqlglot copied to clipboard
SQLite: date arithmetic with + / - INTERVAL produces invalid SQL
SQLite has no INTERVAL syntax, but date/time arithmetic written with +/- and an interval is emitted unchanged, so the SQLite output is a syntax error:
import sqlglot
sqlglot.transpile("SELECT CAST(d AS DATE) + INTERVAL '1 day'", read="postgres", write="sqlite")
# ["SELECT DATE(d) + INTERVAL '1' DAY"]
# SQLite: near "DAY": syntax error
sqlglot.transpile("SELECT d + INTERVAL 1 MONTH", read="mysql", write="sqlite")
# ["SELECT d + INTERVAL '1' MONTH"]
The function form already works, e.g. DATE_ADD(d, INTERVAL 1 MONTH) from MySQL becomes DATE(d, '1 MONTH'). Since the right operand is an INTERVAL, the operator form is unambiguous, so I'd expect x + INTERVAL n unit / x - INTERVAL n unit to go through the same DATE(...) / DATETIME(...) modifier path in the SQLite generator.
Tested on main (374f0e67).
Official Documentation https://www.sqlite.org/lang_datefunc.html#modifiers