Repository navigation
Fix SQLite issues: Infinity values, datetime('now'), FK column quoting, mssql:// URL parser - #1710
Merged
Merged
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-stringnow guards fordouble-floatinfinities and emits the strings'Infinity'/'-Infinity'that PostgreSQL accepts forfloat/real/double precisioncolumns. SQLite can store IEEE 754 infinity (1e309); previously SBCL'sprinc-to-stringon 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 stringdatetime('now','localtime'). Replaced with a regex that matches anydatetime('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 inwhen-not, silently dropping the default. Now maps to the string"CURRENT_TIMESTAMP"thatformat-defaultalready knows to emit unquoted.Closes: #1348
#1256 — FK column names not quoted, breaks reserved-word column names (v3)
src/pgsql/pgsql-ddl.lisp: theformat-create-sqlFKEY 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 namedreferences) 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-targetis the rule used when pgloader is invoked aspgloader SOURCE TARGET. It previously triedload-sqlite-commandfirst; sincesqlite-urifalls back tomaybe-quoted-filename(which accepts any string including:,/,@characters), it swallowed a baremssql://…URI before MSSQL even got a chance. Reordered to tryload-mssql-commandandload-mysql-commandbeforeload-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-namerule now allows digits so thatint4,int8,float4,float8parse as a single token instead of failing.sqlite-array-suffixrule consumes a trailing[]and appends it to the returned type name, so cast rules can match the full string"byte[]"explicitly.handler-casefallback covers any remaining unparseable type names by returning"text".v3 (
src/sources/sqlite/sqlite-cast-rules.lisp): added explicit cast rules forbyte[]→bytea,byte→bytea,int2→smallint,int4→integer,int8→bigint.v4 (
clojure/src/pgloader/source/sqlite.clj): rewrotesqlite-type-mapwith 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): thesource-uriandpg-urigrammar rules now accept a single-quoted alternative ('…') in addition to the existing unquoted forms. This lets users write:where
DB_PATHmay 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-urinow percent-encodes literal spaces before constructing ajava.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 fromexpand-env— quoting just the substituted value (e.g.'my path') inside asqlite:///prefix producedsqlite:///'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 namedmy db.sqliteviaSQLITE_DBenv-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 newspaced-pathtest)