Skip to content

Fix SQLite issues: Infinity values, datetime('now'), FK column quoting, mssql:// URL parser - #1710

Merged
dimitri merged 6 commits into
masterfrom
fix/sqlite-issues
Jun 22, 2026
Merged

dimitri merged 6 commits into
masterfrom
fix/sqlite-issues

Conversation

@dimitri

@dimitri dimitri commented Jun 22, 2026 •

Copy link
Copy Markdown
Owner

Summary

Fixes six independent SQLite (and one parser ordering) issues found across v3 and v4.


#1341 — Infinity float values crash or corrupt on load (v3)

src/utils/transforms.lisp: float-to-string now guards for double-float infinities and emits the strings 'Infinity'/'-Infinity' that PostgreSQL accepts for float/real/double precision columns. SQLite can store IEEE 754 infinity (1e309); previously SBCL's princ-to-string on those values produced implementation-defined output.

Closes: #1341


#1348 — datetime('now') default dropped instead of mapped to CURRENT_TIMESTAMP (v3 + v4)

v3 (src/sources/sqlite/sqlite-cast-rules.lisp): the previous check only matched the literal string datetime('now','localtime'). Replaced with a regex that matches any datetime('now'…) variant and maps it to :current-timestamp.

v4 (clojure/src/pgloader/source/sqlite.clj): sqlite-function-default? correctly detected the patterns but its result was used in when-not, silently dropping the default. Now maps to the string "CURRENT_TIMESTAMP" that format-default already knows to emit unquoted.

Closes: #1348


#1256 — FK column names not quoted, breaks reserved-word column names (v3)

src/pgsql/pgsql-ddl.lisp: the format-create-sql FKEY method used ~{~a~^``,~} for both the local and referenced column lists, printing raw strings without quoting. Now calls (mapcar #'apply-identifier-case …) on both lists so reserved words (e.g. a column named references) are properly double-quoted.

Closes: #1256


#1580 — mssql:// URI parsed as a SQLite file path in two-argument mode (v3)

src/parsers/command-parser.lisp: pg-db-uri-from-source-target is the rule used when pgloader is invoked as pgloader SOURCE TARGET. It previously tried load-sqlite-command first; since sqlite-uri falls back to maybe-quoted-filename (which accepts any string including :, /, @ characters), it swallowed a bare mssql://… URI before MSSQL even got a chance. Reordered to try load-mssql-command and load-mysql-command before load-sqlite-command.

Closes: #1580


#1231 — ORM-generated byte[] type and sized integer aliases (int4, int8) fail to parse (v3 + v4)

v3 (src/parsers/parse-sqlite-type-name.lisp):

  • sqlite-type-name rule now allows digits so that int4, int8, float4, float8 parse as a single token instead of failing.
  • New sqlite-array-suffix rule consumes a trailing [] and appends it to the returned type name, so cast rules can match the full string "byte[]" explicitly.
  • A handler-case fallback covers any remaining unparseable type names by returning "text".

v3 (src/sources/sqlite/sqlite-cast-rules.lisp): added explicit cast rules for byte[] → bytea, byte → bytea, int2 → smallint, int4 → integer, int8 → bigint.

v4 (clojure/src/pgloader/source/sqlite.clj): rewrote sqlite-type-map with explicit entries for all sized aliases (int2, int4, int8, float4, float8) and ORM blob types (byte, byte[]). Lookup does an exact match first, then a longest-prefix match (sorted by key length descending) so "int" never shadows "int4" or "int8".

Closes: #1231


#1365 — {{VAR}} env-var expansion fails when the value contains spaces (v4)

v4 (clojure/src/pgloader/load_file/grammar.clj): the source-uri and pg-uri grammar rules now accept a single-quoted alternative ('…') in addition to the existing unquoted forms. This lets users write:

FROM 'sqlite:///{{DB_PATH}}'

where DB_PATH may contain spaces. The grammar strips the surrounding quotes and passes the raw URI to the AST layer.

v4 (clojure/src/pgloader/load_file/ast.clj): parse-uri now percent-encodes literal spaces before constructing a java.net.URI (which rejects unencoded spaces); getSchemeSpecificPart() decodes them back so the file path is preserved with the original spaces.

v4 (clojure/src/pgloader/load_file/parser.clj): removed the incorrect auto-quoting from expand-env — quoting just the substituted value (e.g. 'my path') inside a sqlite:/// prefix produced sqlite:///'my path', which the grammar could not match. The correct pattern is to single-quote the entire URI token in the load file.

New integration test: clojure/tests/sqlite/spaced-path/ loads a database file named my db.sqlite via SQLITE_DB env-var expansion, exercising the full grammar → expand-env → parse-uri pipeline.

Closes: #1365


Testing

  • clojure -M:test — 141 tests, 509 assertions, 0 failures (includes 37 new assertions for SQLite type mapping and 4 for FROM/TO sqlite://{{VAR}} with whitespace in VAR fails #1365 grammar rules)
  • make -C clojure/tests sqlite — all SQLite integration tests pass (including new spaced-path test)
  • v3 changes are logic-only; no v3 integration test infra changes needed

dimitri added 6 commits June 22, 2026 17:17
…sql:// parser order

- v3 float-to-string: handle Infinity/-Infinity for float columns (#1341)
  SQLite can store IEEE 754 infinity; PostgreSQL accepts 'Infinity'/'-Infinity'
  for float/real/double precision columns. Previously SBCL's princ-to-string
  of double-float infinity produced implementation-defined output.

- v3 sqlite-cast-rules: map datetime('now') to :current-timestamp (#1348)
  Previously only datetime('now','localtime') was recognised. Switch to a
  regex so any datetime('now',...) variant maps to CURRENT_TIMESTAMP.

- v3 pgsql-ddl: quote FK column names via apply-identifier-case (#1256)
  FOREIGN KEY(col) and REFERENCES t(col) used raw ~{~a~^,~} format which
  did not quote reserved words (e.g. a column named 'references'). Now
  mapcar apply-identifier-case over both column lists.

- v3 command-parser: move mssql/mysql before sqlite in pg-db-uri-from-source-target (#1580)
  sqlite-uri falls back to maybe-quoted-filename which accepts any string
  including mssql:// URIs (filename-character-p includes ':', '/', '@').
  Putting mssql and mysql rules first ensures their URIs are not swallowed
  by the sqlite filename fallback in two-argument pgloader mode.

- v4 sqlite.clj: map SQLite time-function defaults to CURRENT_TIMESTAMP (#1348)
  sqlite-function-default? detected datetime/strftime/etc but the result was
  used in when-not, silently dropping the default. Now maps to the string
  'CURRENT_TIMESTAMP' which format-default passes through unquoted.
…e names (#1231)

v3 parser (parse-sqlite-type-name.lisp):
- sqlite-type-name rule now allows digits so that int4, int8, float4,
  float8 etc. parse as a single token instead of failing.
- New sqlite-array-suffix rule consumes trailing [] and appends it to
  the returned type name so cast rules can match on the full string
  'byte[]' specifically.
- handler-case fallback remains for truly unparseable type names.

v3 cast rules (sqlite-cast-rules.lisp):
- byte[]  -> bytea (using byte-vector-to-bytea)  ORM blob type (#1231)
- byte    -> bytea (using byte-vector-to-bytea)  bare form
- int2    -> smallint
- int4    -> integer  (32-bit, distinct from int8)
- int8    -> bigint   (64-bit)

v4 type map (sqlite.clj):
- Rewrote sqlite-type-map with explicit entries for all sized aliases:
  int2->smallint, int4->integer, int8->bigint, float4->real, float8->double
  byte->bytea, byte[]->bytea
  number->numeric, long->bigint, character->text
- Lookup: exact match first, then longest-prefix match (sort-by key length
  desc) so 'int' never shadows 'integer', 'int4', or 'int8'.
- Removed the regex strip on [] — byte[] is now an explicit map entry.

Unit tests (sqlite_test.clj):
- 37 new assertions covering all type categories including the int4/int8
  distinction, byte[] ORM types, prefix fallback for varchar(255), and
  the unknown-type text fallback.
Root cause: after Mustache/env substitution, if a variable value contains
spaces (e.g. a SQLite path like '/my data/prod.sqlite'), the grammar's
source-uri rule  #'[^\s;]+'  stops at the first space and the parse fails.

Fix — two layers:

1. Grammar (grammar.clj): source-uri and pg-uri now accept an additional
   single-quoted alternative  <"'"> #"[^']+" <"'">  so that paths with
   spaces can be expressed as 'sqlite:///my data/file.db' without ambiguity.
   The angle-bracket syntax discards the delimiters; the inner string reaches
   parse-uri unchanged.

2. expand-env (parser.clj): when a {{VAR}} substitution produces a value
   containing whitespace or ';', the value is automatically wrapped in single
   quotes before the load-file string is fed to the parser. Users writing
   {{MY_PATH}} in a load file get the right behaviour without needing to write
   '{{MY_PATH}}' explicitly.

3. parse-uri (ast.clj): java.net.URI rejects spaces even in paths. Spaces are
   percent-encoded to %20 before URI construction; getPath/getSchemeSpecificPart
   decode them back, so the returned :path map value is the original unencoded
   string.

Tests added (parser_test.clj):
- grammar accepts single-quoted source-uri with spaces
- grammar accepts single-quoted pg-uri with spaces
- pre-quoted expanded value parses and yields the correct :path
- unquoted path without spaces continues to work (non-regression)

Closes: #1365
Grammar now accepts 'single-quoted' source URIs and pg-URIs, making
it possible to load a SQLite database whose file path contains spaces
or other characters that confuse the grammar's whitespace-delimited
tokenizer.

Changes:
- grammar.clj: add single-quoted alternative to source-uri and pg-uri
- ast.clj: percent-encode spaces in URI strings before java.net.URI
  construction (java.net.URI rejects literal spaces)
- parser.clj: remove the incorrect auto-quoting in expand-env; the
  correct pattern is to quote the whole URI in the load file:
    FROM 'sqlite:///{{DB_PATH}}'
  rather than embedding {{DB_PATH}} inside an unquoted sqlite:/// URI
- parser_test.clj: rename and update affected tests; add tests for
  the single-quoted grammar rules and for a var-expanded path
- tests/sqlite/spaced-path/: new regression test that loads a SQLite
  database named 'my db.sqlite' via SQLITE_DB env-var expansion

Closes #1365
The spaced-path integration test uses single-quoted source-uri syntax
and {{VAR}} expansion — both v4-only features. v3 pgloader cannot parse
the load file, so it must be skipped when running the v3 suite.

Added all-v3 target to sqlite/Makefile that runs only the common tests,
and updated the sqlite-v3 rule in tests/Makefile to invoke all-v3
inside the container (matching the pattern used by fixed-v3 and dbf-v3).
)

Added quoted-sqlite-uri grammar rule to v3's SQLite command parser.
When a path contains spaces it must be single-quoted in the load file:

  FROM 'sqlite:///{{DB_PATH}}'

The existing quoted-filename rule would strip quotes and pass the full
string 'sqlite:///path/with space' to parse-namestring, which SBCL
interprets as a pathname with 'sqlite:' as the device name — producing
an incorrect path like sqlite:/cwd/sqlite:/cwd/path/with space.

The new quoted-sqlite-uri rule matches 'sqlite://' first (before the
generic quoted-filename fallback), extracts the path portion after
sqlite://, and constructs the pathname directly via (pathname ...) so
SBCL never sees the sqlite: prefix as a device.

Integration test spaced-path now runs and passes under both v3 and v4.
Reverted the earlier workaround that skipped spaced-path from the v3
test run.
@dimitri
dimitri merged commit cb04c34 into master Jun 22, 2026
30 checks passed
@dimitri
dimitri deleted the fix/sqlite-issues branch June 22, 2026 16:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment