Skip to content

Move the key and check rules of a table into one module #2716

Open
0 of 1 issue completed
Open

Description

@mathiasrw

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 on pk.pkaddr, pk.newpkaddr, uk.ukaddr and uk.newukaddr.
  • table.delete (line 652) throws Something wrong with primary key index on table when the row is not indexed.
  • rebuildKeyIndex and syncKeyIndexes (lines 448 and 458) are local to execute. syncKeyIndexes notices a replaced table.data but not rows pushed into the same array.

This also fixes #1005.

What to change

  1. 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 syncKeyIndexes in a module-level WeakMap from table to {data, length}. Record the length after every insert and delete through the rules, and rebuild when table.data.length differs.

  2. table.insert, table.update and table.delete call these functions. Keep the order of the steps and every error text. insert keeps passing {} as params to the checks, update keeps passing params. table.deleteall uses rebuildKeyIndex.

  3. Remove the UPDATE-only fallback. Keep old and new keys in local variables, not on pk and uk.

  4. Leave triggers, identities and defaults where they are. They move in Pass the rows of INSERT, UPDATE and DELETE through one write pipeline #2650.

  5. Leave the storage engine branches of INSERT, UPDATE and DELETE as they are.

What stays the same

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.data replaced 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.js has no own rebuildKeyIndex, syncKeyIndexes or 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.

Sub-issues

Sub-issues

0 of 1 Issues completed
Loading 1 sub-issues

Activity

  1. added this to the v4.x Focus milestone on Nov 26, 2026
  2. changed the title [-]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[/+] on Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions