INTERVAL '1 day ago' loses "ago" and becomes a positive interval
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 35/100
Research direction
Start with _parse_interval_span in sqlglot/parser.py and the INTERVAL_STRING_RE matching behavior described in the issue. Add or inspect coverage for single-unit intervals ending in ago, including DuckDB and Postgres parsing; run the relevant interval parser tests. Done means these forms preserve the negative interval while ordinary INTERVAL '1 day' behavior remains unchanged.
Written by the indexing model from the issue text.
Description
Thanks for SQLGlot! I found this while replaying DuckDB's sqllogictest suite (test/sql/types/interval/test_interval.test) through SQLGlot and comparing the results against a formalization of parts of DuckDB's SQL semantics.
An interval string with a single unit followed by ago loses the ago: INTERVAL '1 day ago' becomes INTERVAL '1' DAY. DuckDB (and Postgres) read ago as a negation, so the transpiled query adds a day where the original subtracts one. An interval string with several units keeps its text, ago included, so only the one-unit form is affected.
Fully reproducible code snippet
Read and write dialect: duckdb. Reproduces on 30.35.0 and on main (cce8bd3bf7).
import duckdb
import sqlglot
print("sqlglot", sqlglot.__version__, "duckdb", duckdb.__version__)
con = duckdb.connect()
for sql in [
"SELECT INTERVAL '1 day ago'",
"SELECT INTERVAL '2 days ago'",
"SELECT DATE '2024-01-10' + INTERVAL '1 day ago'",
"SELECT INTERVAL '2 days 3 hours ago'",
"SELECT INTERVAL '1 day'",
]:
out = sqlglot.transpile(sql, read="duckdb", write="duckdb")[0]
print(sql)
print(" DuckDB, original: ", con.execute(sql).fetchall()[0][0])
print(" transpiled: ", out)
print(" DuckDB, transpiled: ", con.execute(out).fetchall()[0][0])
Output with sqlglot 30.35.0 (the same on main at cce8bd3bf7), DuckDB 2.0.1, Python 3.13:
sqlglot 30.35.0 duckdb 2.0.1
SELECT INTERVAL '1 day ago'
DuckDB, original: -1 day, 0:00:00
transpiled: SELECT INTERVAL '1' DAY
DuckDB, transpiled: 1 day, 0:00:00
SELECT INTERVAL '2 days ago'
DuckDB, original: -2 days, 0:00:00
transpiled: SELECT INTERVAL '2' DAYS
DuckDB, transpiled: 2 days, 0:00:00
SELECT DATE '2024-01-10' + INTERVAL '1 day ago'
DuckDB, original: 2024-01-09 00:00:00
transpiled: SELECT CAST('2024-01-10' AS DATE) + INTERVAL '1' DAY
DuckDB, transpiled: 2024-01-11 00:00:00
SELECT INTERVAL '2 days 3 hours ago'
DuckDB, original: -3 days, 21:00:00
transpiled: SELECT INTERVAL '2 days 3 hours ago'
DuckDB, transpiled: -3 days, 21:00:00
SELECT INTERVAL '1 day'
DuckDB, original: 1 day, 0:00:00
transpiled: SELECT INTERVAL '1' DAY
DuckDB, transpiled: 1 day, 0:00:00
Expected: the first three keep ago (INTERVAL '1 day ago') or write the negative value (INTERVAL '-1' DAY), so the transpiled query gives the same result as the original. Reading with read="postgres" drops it the same way: SELECT INTERVAL '1 day ago' becomes SELECT INTERVAL '1 DAY'.
Official Documentation
- DuckDB's interval type, which accepts
ago: https://duckdb.org/docs/stable/sql/data_types/interval - Postgres interval input (
@ 1 day ago): https://www.postgresql.org/docs/current/datatype-datetime.html#DATATYPE-INTERVAL-INPUT - The DuckDB test the case comes from: https://github.com/duckdb/duckdb/blob/main/test/sql/types/interval/test_interval.test
Where it comes from
_parse_interval_span in sqlglot/parser.py canonicalises a string literal with exp.INTERVAL_STRING_RE.findall(this.name). findall skips text the pattern doesn't match, so '1 day ago' gives a single part ('1', 'day'), and the len(parts) == 1 branch rewrites the literal to '1' with unit DAY, dropping ago. With several parts the literal is left as written, which is why '2 days 3 hours ago' survives. Canonicalising only when the pattern matches the whole string keeps ago:
- if len(parts) == 1:
+ if len(parts) == 1 and exp.INTERVAL_STRING_RE.fullmatch(this.name):
this = exp.Literal.string(parts[0][0])
With this change on main, the first three queries keep INTERVAL '1 day ago' (and '2 days ago') and give DuckDB's original results; INTERVAL '1 day' still becomes INTERVAL '1' DAY. I checked it only against the script above, not against SQLGlot's test suite.
#3523 is related but different: there ago was kept and Postgres-to-MySQL needed type inference; here ago is dropped even from DuckDB to DuckDB.
I used Claude to help reproduce this and draft the report; I reviewed and edited it.
- Dominant language
- Python
- Stars
- 9.8k
- Forks
- 1.5k
- Avg merge
- 10h 54m
- Merged PRs (30d)
- 310
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from tobymao/sqlglot
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
ClickHouse: quantile is read as the exact Quantile, though ClickHouse's quantile is approximateOpen
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
ClickHouse: DAY_OF_WEEK is read as DayOfWeek, so every call is written back as toDayOfWeek(t, 3)Open
Difficulty 3/5 1-2 days Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
Oracle: qualify quotes USER, UID, ORA_ROWSCN and CONNECT_BY_ISLEAF/ISCYCLE as if they were columnsPossibly taken @georgesittas claimed this today. Open
Difficulty 3/5 1-2 days Newbie friendliness 35/100
tobymao/sqlglot#9468 · 1 comment ·
Maintainers usually reply within 1 day
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
daniel-ospina/tortoise#15014 ·
Maintainers usually reply within 1 day
-
area:api p2 product:business triage:active type:feat
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
cuadrao-ai/argus#2656 ·
Maintainers usually reply within 1 day
-
documentation good first issue help wanted
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
-
good first issue tests
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
yeab7505/tenasig#20 · 1 comment ·
Maintainers usually reply within 1 day