fix: CREATE UNIQUE INDEX actually enforces uniqueness (and persists)
CI / test (push) Has been cancelled
CI / verify (push) Has been cancelled
Clients CI / build-server (push) Has been cancelled
Clients CI / test-python (push) Has been cancelled
Clients CI / test-javascript (push) Has been cancelled
Clients CI / test-nim (push) Has been cancelled
Clients CI / test-rust (push) Has been cancelled

This commit is contained in:
2026-07-30 17:13:29 +03:00
parent 5858a9da17
commit e8f9cbc5bb
8 changed files with 122 additions and 8 deletions
+1
View File
@@ -387,6 +387,7 @@ type
ciColumns*: seq[string]
ciExpr*: Node
ciKind*: IndexKind
ciUnique*: bool
of nkDropIndex:
diName*: string
of nkFrom:
+3
View File
@@ -4,6 +4,7 @@
## Also hosts the AST-to-SQL serializer used for VIEW DDL persistence.
import std/strutils
import std/tables
import std/sets
import std/locks
import ../ast
import ../../storage/lsm
@@ -28,6 +29,7 @@ var restoreEnginesHook*: proc(ctx: ExecutionContext)
proc newExecutionContext*(db: LSMTree, registry: DatabaseRegistry = nil): ExecutionContext =
result = ExecutionContext(db: db, tables: initTable[string, TableDef](),
btrees: initTable[string, BTreeIndex[string, IndexEntry]](),
uniqueIndexes: initHashSet[string](),
views: initTable[string, Node](),
cteTables: initTable[string, seq[Row]](),
ftsIndexes: initTable[string, fts.InvertedIndex](),
@@ -161,6 +163,7 @@ proc cloneForConnection*(ctx: ExecutionContext): ExecutionContext =
svCopy[k] = v
result = ExecutionContext(db: ctx.db, tables: ctx.tables,
btrees: ctx.btrees, views: ctx.views,
uniqueIndexes: ctx.uniqueIndexes,
cteTables: initTable[string, seq[Row]](),
ftsIndexes: ctx.ftsIndexes,
vectorIndexes: ctx.vectorIndexes,
+23
View File
@@ -3,6 +3,7 @@
## Extracted from `executor.nim` (Task 9 of the executor split).
import std/strutils
import std/tables
import std/sets
import std/sequtils
import ../../storage/lsm
import ../../storage/btree
@@ -21,6 +22,28 @@ import rls
# Table storage
# ----------------------------------------------------------------------
proc violatesUniqueIndex*(ctx: ExecutionContext, table: string, fields: seq[string],
rowVals: seq[string], excludeLsmKey: string = ""): string =
## Returns the colKey of the first standalone UNIQUE index this row
## violates, or "" when the row is clean. idxVal is built with the exact
## convention of the CREATE INDEX population loop (getValue yields "\\N"
## for a missing column, values joined with "|"). excludeLsmKey lets UPDATE
## ignore the row's own existing entry.
if ctx.uniqueIndexes.len == 0: return ""
for colKey in ctx.uniqueIndexes:
if not colKey.startsWith(table & "."): continue
let idxCols = colKey[table.len + 1..^1].split(".")
var colVals: seq[string] = @[]
for c in idxCols:
colVals.add(getValue(rowVals, fields, c))
let idxVal = colVals.join("|")
if idxVal.len == 0 or isNull(idxVal): continue
if colKey notin ctx.btrees: continue
for entry in ctx.btrees[colKey].get(idxVal):
if entry.lsmKey != excludeLsmKey:
return colKey
return ""
proc execInsert*(ctx: ExecutionContext, table: string, fields: seq[string], values: seq[seq[string]],
kvPairs: var seq[(string, seq[byte])]): int =
if not hasPrivilege(ctx, table, "INSERT"):
+2
View File
@@ -1,5 +1,6 @@
## Executor types — shared by all exec/* modules and executor.nim
import std/tables
import std/sets
import std/locks
import ../ast
import ../ir
@@ -92,6 +93,7 @@ type
db*: LSMTree
tables*: Table[string, TableDef]
btrees*: Table[string, BTreeIndex[string, IndexEntry]]
uniqueIndexes*: HashSet[string] # colKeys (table.col[.col...]) of UNIQUE standalone B-tree indexes
views*: Table[string, Node] # view name -> SELECT AST
cteTables*: Table[string, seq[Row]] # CTE name -> rows
ftsIndexes*: Table[string, fts.InvertedIndex] # table.col -> FTS index
+31 -7
View File
@@ -5,6 +5,7 @@
import std/os
import std/strutils
import std/tables
import std/sets
import std/hashes
import std/sequtils
import std/algorithm
@@ -543,6 +544,13 @@ proc executeQueryImpl(ctx: ExecutionContext, astNode: Node, params: seq[WireValu
let (valid, errMsg) = validateConstraints(ctx, stmt.insTarget, mutableFields, mutableValues)
if not valid: return errResult(errMsg)
# Standalone UNIQUE index enforcement (same failure channel as
# validateConstraints: errResult before any row is written)
for rowVals in mutableValues:
let uCol = violatesUniqueIndex(ctx, stmt.insTarget, mutableFields, rowVals)
if uCol.len > 0:
return errResult("UNIQUE constraint violated: duplicate value for unique index '" & uCol & "'")
# Fire BEFORE INSERT triggers
var row = initTable[string, Value]()
if mutableValues.len > 0:
@@ -637,6 +645,11 @@ proc executeQueryImpl(ctx: ExecutionContext, astNode: Node, params: seq[WireValu
updValues.add("\\N")
let (valid, errMsg) = validateConstraints(ctx, stmt.updTarget, updFields, @[updValues], skipPkCheck = true)
if not valid: return errResult(errMsg)
# Standalone UNIQUE index enforcement — exclude this row's own entry
let uCol = violatesUniqueIndex(ctx, stmt.updTarget, updFields, updValues,
excludeLsmKey = stmt.updTarget & "." & old)
if uCol.len > 0:
return errResult("UNIQUE constraint violated: duplicate value for unique index '" & uCol & "'")
# FK ON UPDATE enforcement (parent side)
var refCols: seq[string] = @[]
for _, childTbl in ctx.tables:
@@ -858,7 +871,9 @@ proc executeQueryImpl(ctx: ExecutionContext, astNode: Node, params: seq[WireValu
var toDelete: seq[string] = @[]
for idxName in ctx.btrees.keys.toSeq():
if idxName.startsWith(dropName & "."): toDelete.add(idxName)
for idxName in toDelete: ctx.btrees.del(idxName)
for idxName in toDelete:
ctx.btrees.del(idxName)
ctx.uniqueIndexes.excl(idxName)
# Drop FTS/HNSW engine indexes for this table (in-memory entries)
var ftsToDelete: seq[string] = @[]
for key in ctx.ftsIndexes.keys.toSeq():
@@ -1387,17 +1402,24 @@ proc executeQueryImpl(ctx: ExecutionContext, astNode: Node, params: seq[WireValu
let idxVal = colVals.join("|")
if idxVal.len > 0 and not isNull(idxVal):
let lsmKey = if "$key" in row: stmt.ciTarget & "." & valueToString(row["$key"]) else: ""
if stmt.ciUnique and ctx.btrees[colKey].contains(idxVal):
# Duplicate data — abort without registering the index
ctx.btrees.del(colKey)
return errResult("UNIQUE constraint violated: duplicate value '" & idxVal &
"' for unique index '" & colKey & "'")
ctx.btrees[colKey].insert(idxVal, IndexEntry(lsmKey: lsmKey, rowValue: ""))
if stmt.ciUnique:
ctx.uniqueIndexes.incl(colKey)
# Persist reconstructed DDL so restoreEngines can rebuild the index
# from table data after a restart (replay re-writes the same key).
# Unnamed indexes: persist the nameless form (see FTS branch above).
# The CREATE INDEX AST does not track UNIQUE, so it is not preserved.
let uniqueKw = if stmt.ciUnique: "UNIQUE " else: ""
let btreeDdl = if stmt.ciName.len > 0:
"CREATE INDEX " & idxName & " ON " & stmt.ciTarget & " (" & stmt.ciColumns.join(", ") & ")"
"CREATE " & uniqueKw & "INDEX " & idxName & " ON " & stmt.ciTarget & " (" & stmt.ciColumns.join(", ") & ")"
else:
"CREATE INDEX ON " & stmt.ciTarget & " (" & stmt.ciColumns.join(", ") & ")"
"CREATE " & uniqueKw & "INDEX ON " & stmt.ciTarget & " (" & stmt.ciColumns.join(", ") & ")"
ctx.db.put(SchemaBtreeIndexPrefix & colKey, cast[seq[byte]](btreeDdl))
return okResult(msg="CREATE INDEX " & idxName & " on " & stmt.ciTarget)
return okResult(msg="CREATE " & uniqueKw & "INDEX " & idxName & " on " & stmt.ciTarget)
of nkDropIndex:
# Find and remove index by name from ctx.btrees
@@ -1411,14 +1433,16 @@ proc executeQueryImpl(ctx: ExecutionContext, astNode: Node, params: seq[WireValu
found = true
break
# A custom index name only appears in the persisted DDL — match it
# against the stored "CREATE INDEX <name> ON" text as well.
# against the stored "CREATE [UNIQUE] INDEX <name> ON" text as well.
let (hasDdl, ddl) = ctx.db.get(SchemaBtreeIndexPrefix & key)
if hasDdl and cast[string](ddl).startsWith("CREATE INDEX " & stmt.diName & " ON "):
if hasDdl and (cast[string](ddl).startsWith("CREATE INDEX " & stmt.diName & " ON ") or
cast[string](ddl).startsWith("CREATE UNIQUE INDEX " & stmt.diName & " ON ")):
targetKey = key
found = true
break
if found:
ctx.btrees.del(targetKey)
ctx.uniqueIndexes.excl(targetKey)
ctx.db.delete(SchemaBtreeIndexPrefix & targetKey)
return okResult(msg="DROP INDEX " & stmt.diName)
# FTS/HNSW engine indexes: in-memory maps are keyed by table.col, and a
+2 -1
View File
@@ -1327,7 +1327,8 @@ proc parseCreateIndex(p: var Parser): Node =
elif idxMethod == "ivfpq":
idxKind = ikIVFPQ
result = Node(kind: nkCreateIndex, ciName: idxName, ciTarget: tableName,
ciColumns: colNames, ciKind: idxKind, line: tok.line, col: tok.col)
ciColumns: colNames, ciKind: idxKind, ciUnique: isUnique,
line: tok.line, col: tok.col)
proc parseBeginTxn(p: var Parser): Node =
let tok = p.expect(tkBegin)