SQLite: day intervals on an expression of unknown type drop the time

Open
#9,303 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
65/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python, sql, sqlite
Domain
databases

Research direction

Start by running the reproducer: sqlglot.transpile("SELECT ts + INTERVAL 1 DAY FROM t", read="duckdb", write="sqlite") and trace it into the SQLite dialect's interval generator, where day/week/month/year units choose DATE() while hour/minute/second use DATETIME(). Compare that path with the annotated-schema handling for DATE and TIMESTAMP columns. Add a regression case to the SQLite transpilation tests for unknown-type timestamp intervals; done means the output either keeps the time or reports the case as unsupported without changing known DATE/TIMESTAMP behavior.

Written by the indexing model from the issue text.

Description

For an interval added to an expression of unknown type, the SQLite generator picks DATE() for day, week, month and year units, which drops the time of a value that holds a timestamp:

import sqlglot
sqlglot.transpile("SELECT ts + INTERVAL 1 DAY FROM t", read="duckdb", write="sqlite")
# ["SELECT DATE(ts, '1 DAY') FROM t"]

With t.ts holding '2024-01-15 23:30:00', SQLite returns '2024-01-16', while DuckDB (with ts TIMESTAMP) returns 2024-01-16 23:30:00. Hour, minute and second units already use DATETIME(), which keeps the time.

Without type information the generator can't tell date text from timestamp text, so either choice is wrong for one of them. It may be safer to flag the unknown-type case as unsupported, unless the caller annotates types with a schema, in which case DATE and TIMESTAMP columns are already handled correctly.

I found this while running BenchBox benchmarks on SQLite. The issue was drafted with Claude.

Dominant language
Python
Stars
9.8k
Forks
1.4k
Avg merge
10h 52m
Merged PRs (30d)
303

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from tobymao/sqlglot

All issues in tobymao/sqlglot

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.