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

オープン
#9,429 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@treysp がすでに取り組んでいます。

2026年11月30日 から。

  • #9436 @treysp による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
停滞
技術スタック
postgresql, python
領域
databases

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Python
スター
9.8k
フォーク
1.5k
平均マージ
10時間 54分
マージ済み PR(30日)
310

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

tobymao/sqlglot のほかの issue

tobymao/sqlglot の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。