INTERVAL '1 day ago' loses "ago" and becomes a positive interval
Los mantenedores suelen responder en 1 día
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 35/100
Línea de trabajo
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.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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.
- Lenguaje dominante
- Python
- Estrellas
- 9.8k
- Forks
- 1.5k
- Merge medio
- 10 h 54 min
- PR fusionados (30 d)
- 310
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Sin plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de tobymao/sqlglot
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 1 día
-
ClickHouse: quantile is read as the exact Quantile, though ClickHouse's quantile is approximateAbierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
Los mantenedores suelen responder en 1 día
-
ClickHouse: DAY_OF_WEEK is read as DayOfWeek, so every call is written back as toDayOfWeek(t, 3)Abierto
Dificultad 3/5 1-2 días Aptitud para principiantes 68/100
Los mantenedores suelen responder en 1 día
-
Oracle: qualify quotes USER, UID, ORA_ROWSCN and CONNECT_BY_ISLEAF/ISCYCLE as if they were columnsPosiblemente ocupada @georgesittas la tomó hoy. Abierto
Dificultad 3/5 1-2 días Aptitud para principiantes 35/100
tobymao/sqlglot#9468 · 1 comentario ·
Los mantenedores suelen responder en 1 día
Todos los issues de tobymao/sqlglot
Issues similares
-
Make the "%" unit spacing the same in the web and in A10Posiblemente ocupada @kragent66-glitch la tomó hoy. Abiertobug good first issue
Dificultad 2/5 1-3 horas Aptitud para principiantes 74/100
maiychrus25/OpenTea-DX-OS#226 · 1 comentario ·
-
ci severity: low
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
TMHSDigital/Spackle#205 ·
Los mantenedores suelen responder en 1 día
-
pkg:lgm type:chore upstream-gaussx
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
jejjohnson/pyrox#610 ·
Los mantenedores suelen responder en 1 día
-
changelog investigate
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
ramnes/notion-sdk-py#523 ·
-
feature-request
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
open-telemetry/opentelemetry-python#5994 ·
Los mantenedores suelen responder en 2 días