sqlglot icon indicating copy to clipboard operation
sqlglot copied to clipboard

SQLite: LIMIT -1 (no limit) is transpiled verbatim, other dialects reject it

Open EnesSoydan opened this issue 2 weeks ago • 0 comments

In SQLite a negative LIMIT means "no limit", and it's the usual way to write an OFFSET without a limit. sqlglot parses it as a plain -1, so every other target gets an invalid query:

import sqlglot

sql = "SELECT x FROM t LIMIT -1 OFFSET 5"
sqlglot.transpile(sql, read="sqlite", write="duckdb")
# ['SELECT x FROM t LIMIT -1 OFFSET 5']  -> DuckDB: "LIMIT/OFFSET cannot be negative"
sqlglot.transpile(sql, read="sqlite", write="postgres")
# ['SELECT x FROM t LIMIT -1 OFFSET 5']  -> ERROR: LIMIT must not be negative
sqlglot.transpile(sql, read="sqlite", write="tsql")
# ['SELECT x FROM t ORDER BY (SELECT NULL) OFFSET 5 ROWS FETCH FIRST -1 ROWS ONLY']

Expected: the same output as reading LIMIT ALL OFFSET 5 from Postgres, e.g. LIMIT ALL OFFSET 5 for DuckDB and OFFSET 5 ROWS for T-SQL.

The SQLite generator already goes the other way: SELECT x FROM t OFFSET 5 from Postgres is written as LIMIT -1 OFFSET 5 for SQLite, so a postgres -> sqlite -> duckdb round trip currently breaks.

Possible fix: in the SQLite parser, treat a negative numeric LIMIT literal as LIMIT ALL.

Tested on main (b4babf3a).

Official Documentation https://www.sqlite.org/lang_select.html#limitoffset ("If the LIMIT expression evaluates to a negative value, then there is no upper bound on the number of rows returned.")

EnesSoydan avatar Nov 18 '26 21:11 EnesSoydan