INTERVAL '1 day ago' loses "ago" and becomes a positive interval
メンテナーはふだん 1 日以内に返信
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 35/100
調査の方向性
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
- 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.
- 主要言語
- Python
- スター
- 9.8k
- フォーク
- 1.5k
- 平均マージ
- 10時間 54分
- マージ済み PR(30日)
- 310
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
tobymao/sqlglot のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
ClickHouse: quantile is read as the exact Quantile, though ClickHouse's quantile is approximateオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 1 日以内に返信
-
ClickHouse: DAY_OF_WEEK is read as DayOfWeek, so every call is written back as toDayOfWeek(t, 3)オープン
難易度 3/5 1〜2日 初心者へのやさしさ 68/100
メンテナーはふだん 1 日以内に返信
-
Oracle: qualify quotes USER, UID, ORA_ROWSCN and CONNECT_BY_ISLEAF/ISCYCLE as if they were columns対応中かも @georgesittas が今日担当しました。 オープン
難易度 3/5 1〜2日 初心者へのやさしさ 35/100
tobymao/sqlglot#9468 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
tobymao/sqlglot の issue をすべて見る
似ている issue
-
Make the "%" unit spacing the same in the web and in A10対応中かも @kragent66-glitch が今日担当しました。 オープンbug good first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
maiychrus25/OpenTea-DX-OS#226 · コメント 1 件 ·
-
ci severity: low
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
TMHSDigital/Spackle#205 ·
メンテナーはふだん 1 日以内に返信
-
pkg:lgm type:chore upstream-gaussx
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
jejjohnson/pyrox#610 ·
メンテナーはふだん 1 日以内に返信
-
changelog investigate
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
ramnes/notion-sdk-py#523 ·
-
feature-request
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
open-telemetry/opentelemetry-python#5994 ·
メンテナーはふだん 2 日以内に返信