INTERVAL '1 day ago' loses "ago" and becomes a positive interval

Abierto
#9,429 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

@treysp ya está trabajando en esto.

Desde el 30/11/2026.

  • #9436 de @treysp — abierto

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
35/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Estancado
Stack tecnológico
postgresql, python
Área
databases

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

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

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de tobymao/sqlglot

Todos los issues de tobymao/sqlglot

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.