DEV Community

Devanshu Biswas
Devanshu Biswas

Posted on

CREATE TABLE p(x FLOATING POINT) Is an INTEGER Column

Everyone knows a SQLite column's declared type is only a hint. Almost nobody knows what the hint does.

CREATE TABLE p(x FLOATING POINT);   -- this is an INTEGER column
Enter fullscreen mode Exit fullscreen mode

Rule 1 of the affinity algorithm looks for INT anywhere in the name. FLOATING POINT contains INT — inside "PO*INT*" — so rule 1 fires and rule 4 never gets to see the FLOA.

What decides is the position in the rule list, not the position in the name.

Check any type name: https://dev48.infy.uk/solve/day75-sqlite-type-affinity.html

The model everyone carries is wrong 27.98% of the time

Over 1,226 type names checked against the library itself, the intuition the first marker in the name wins is wrong 343 times.

And it is not confined to contrived names. Of 66 type names that really occur in ported DDL, 26 get an affinity that contradicts what the name means:

declared type affinity consequence
STRING NUMERIC '42' is stored as the integer 42
VARBINARY NUMERIC same
UUID NUMERIC same
JSON NUMERIC same

None of those contain CHAR, CLOB or TEXT, so rule 2 never fires, and NUMERIC is the fallthrough. A column you declared to hold strings will silently convert anything that looks like a number.

The page ships a real writer

There is a real affinity engine, a real record codec, and a writer that emits a database file the actual library will open — because a page that shipped a precompressed blob would be proving nothing about either half.

27,474 verifier asserts, 2,247 in-page assertions, 0 failures. Vanilla JavaScript, one file, no build step.

Top comments (0)