Repository navigation
Description
What happens today
The rules of a memory table are closures that CREATE TABLE builds in src/statements/createtable.js.
table.insert(line 467) runs triggers, identities,table.checks(line 525), NOT NULL (line 535),syncKeyIndexes, the primary and unique key lookups with OR REPLACE and IGNORE (lines 541-575), the push and the key index update (lines 610-620).table.update(line 713) does the same checks in another order with other error texts, has its own index rebuild fallback (lines 720-735), and keeps old and new keys onpk.pkaddr,pk.newpkaddr,uk.ukaddranduk.newukaddr.table.delete(line 652) throwsSomething wrong with primary key index on tablewhen the row is not indexed.rebuildKeyIndexandsyncKeyIndexes(lines 448 and 458) are local toexecute.syncKeyIndexesnotices a replacedtable.databut not rows pushed into the same array.
This also fixes #1005.
What to change
-
Add
src/statements/tablerules.js. Move the code, do not write it again.export function syncKeyIndexes(table) // rebuild pk and unique indexes when table.data was // replaced or its length changed outside the rules export function rebuildKeyIndex(table, key) export function checkRow(table, r, params) // table.checks, then NOT NULL; same errors as today export function keyConflict(table, r, skip) // {kind: 'pk' | 'uk', row} for the first key of r that // is taken by another row than skip, or undefined export function indexRow(table, r) // add r to the pk and unique indexes export function unindexRow(table, r) // remove r; returns false when r was not indexed
Keep the state of
syncKeyIndexesin a module-levelWeakMapfrom table to{data, length}. Record the length after every insert and delete through the rules, and rebuild whentable.data.lengthdiffers. -
table.insert,table.updateandtable.deletecall these functions. Keep the order of the steps and every error text.insertkeeps passing{}asparamsto the checks,updatekeeps passingparams.table.deleteallusesrebuildKeyIndex. -
Remove the UPDATE-only fallback. Keep old and new keys in local variables, not on
pkanduk. -
Leave triggers, identities and defaults where they are. They move in Pass the rows of INSERT, UPDATE and DELETE through one write pipeline #2650.
-
Leave the storage engine branches of INSERT, UPDATE and DELETE as they are.
What stays the same
- Every result for memory tables, every error text, and the order in which rules are checked.
- Storage engines (LOCALSTORAGE, FILESTORAGE, INDEXEDDB) store every row as today, without key or check enforcement (Give every storage engine one storage interface that saves the whole table definition and writes through the pipeline #2681, UNIQUE key doesn't work with localStorage engine. #566, UNIQUE with FILESTORAGE not Working! #1148, Foreign key via
REFERENCESdoes not work withFILESTORAGE#1846). - Results change only for Something wrong with primary key index on table - Not expected #1005.
Done when
- A new test file named after this issue covers PRIMARY KEY, composite PRIMARY KEY, column UNIQUE, table UNIQUE with an expression (
UNIQUE (ROUND(a))), CHECK, NOT NULL, INSERT OR REPLACE and INSERT IGNORE, each for INSERT and UPDATE, with the error texts. - It covers
table.datareplaced by a new array and rows pushed into the same array, for INSERT, UPDATE and DELETE. - It pins that a FILESTORAGE and a LOCALSTORAGE table still store a duplicate key and a row that breaks a CHECK.
src/statements/createtable.jshas no ownrebuildKeyIndex,syncKeyIndexesor key lookup code.- The full test suite and the CI checks (dialect test, parity check, run-state check, binding check) pass, and the pull request lists every parity difference.
Fixes
Fixes #1005.
Depends on
Nothing, can start now.
Activity
[-]Review #2341 for code improvements: the key index is rebuilt in UPDATE only[/-][+]Move the key and check rules of a table into one module[/+]