r/SQL • • 4d ago

SQLite Belay - formal verification for SQLite statements

https://vihren.dev/belay/
7 Upvotes

5 comments sorted by

1

u/Hour-Measurement-835 4d ago

The check I'd want first is the boring table rebuild. SQLite's ALTER TABLE can't change a column's type, so most real migrations are: create the new table, INSERT INTO new SELECT * FROM old, drop, rename. That INSERT applies the new column's affinity to every row on the way in.

Tried it just now on 3.50.4: a TEXT column holding '007' and 'abc', rebuilt as INTEGER. Afterwards it holds 7 and 'abc', and typeof() says integer and text.

The annoying part is that WHERE code = '007' still returns the row, because the comparison picks up the column's affinity too. WHERE code || '' = '007' returns nothing. So "migration preserves Q" holds for one query and fails for its near twin, and only a model that carries affinity through INSERT ... SELECT and through comparisons can tell them apart.

Is affinity in the Lean model yet, or does it assume STRICT-style typing for now?

0

u/neprotivo 4d ago

Affinity is in the model. Non-STRICT tables are the default case, and STRICT is modelled as the exception.

I ran your example. On '007' and 'abc' the model gives the same as SQLite: 7 and 'abc' after the rebuild, integer and text from typeof(), the row for WHERE code = '007', and nothing for WHERE code || '' = '007'.

Then I asked the checker whether the rebuild preserves each query on every database with that schema, not only on those two rows. For the || query it answered false and printed a counterexample, a single row with '007', together with a script that replays it in SQLite:

CREATE TABLE items (id INTEGER PRIMARY KEY, code TEXT);
INSERT INTO "items" ("id", "code") VALUES (2, '007');

The plain query is the more interesting one, because it is not preserved either. Take a row whose code is the text '7'. Before the rebuild, '7' = '007' is a text comparison and it is false. After the rebuild the stored value is the integer 7, the literal '007'also becomes 7, and the row matches. So one twin loses a row and the other gains a row that was never there.

The second example was not found by the automatic counterexample search Belay has. An agent found it and then ran it through the model to verify the result. I'll file this as an issue to improve the counterexample search.

I ran this on 3.51.0, not on 3.50.4. The repository has a test for the same kind of rebuild, with a zip code '01234' that becomes 1234: https://github.com/vihren-dev/belay/blob/main/tests/surface/Schema/Tests/RebuildAffinity.lean

1

u/Hour-Measurement-835 4d ago

Fair, I had the plain query down as the twin that holds, and the '7' row kills that. Checked it here too: after the rebuild both '007' and '7' come back for WHERE code = '007'.

The same pair breaks the migration itself once code is UNIQUE. Before the rebuild they're two distinct TEXT values. On the way into the new table both become the integer 7, and on 3.50.4 the INSERT ... SELECT stops with UNIQUE constraint failed: items_new.code and the new table's left empty. Loud, at least. You find out before anything ships.

The quiet one is the workaround you'd reach for after seeing that error, INSERT OR IGNORE INTO items_new SELECT * FROM items. It goes through, the new table holds (1, 7), and id 2 is just gone with nothing logged. Only a row count comparison afterwards would notice.

That feels like something the checker could state as its own property, separate from any query: no two distinct stored values in the old column land on the same value under the new column's affinity. If that fails on some database, the plain rebuild aborts on it when there's a UNIQUE, and the OR IGNORE rebuild loses a row on it. I haven't checked what OR REPLACE does to the id that survives.

2

u/MongooseWonderful198 8h ago

For the OR REPLACE case: an in-memory SQLite 3.53.4 fixture with (1, '007') and (2, '7'), copied into code INTEGER UNIQUE with ORDER BY id ASC, left (2, 7). DESC left (1, 7). Both copies succeeded with one row.

SQLite documents that REPLACE deletes the existing rows causing the uniqueness conflict before inserting the current row. So I'd check preservation of SELECT id, code ..., alongside success and uniqueness. Explicit ordering makes this counterexample reproducible. This tested SQLite only; I haven't run the case through Belay.

1

u/Hour-Measurement-835 8h ago

Thanks for running it. Drop the ORDER BY and (1, '7'), (2, '007') keeps id 2, but id 1 if the old table has an extra column and an index on code. That's 3.50.4, not sure newer picks the same scan.