-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec_write.go
More file actions
642 lines (621 loc) · 21.2 KB
/
Copy pathexec_write.go
File metadata and controls
642 lines (621 loc) · 21.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
package hazedb
import "fmt"
// mutJournal journals one mutation for the PK-pinned live write lanes. It is
// passed BY VALUE through the store's journaled methods — a stack copy, so a
// WAL-on write builds no per-call closure (a func() error closure would heap-
// allocate one per insert/update/delete). The zero value (db nil) journals
// nothing: memory-only DBs and the replay path use it. The store invokes the
// op method under the shard lock, preserving journal-before-apply.
type mutJournal struct {
db *DB
tableID uint16
// UPDATE lane: the changed ordinals (plan-owned, read-only) and the PK
// ordinal to read from the post-update row image.
ords []int
pkOrd int
// DELETE lane: the resolved PK cell (the UUID, never the raw arg).
pkVal Value
}
// live reports whether mutations must be journaled. The update lanes use it to
// skip the save/revert bookkeeping when there is no WAL.
func (j mutJournal) live() bool { return j.db != nil }
// insert appends an opInsert record for row. No-op on the zero value.
func (j mutJournal) insert(row Row) error {
if j.db == nil {
return nil
}
bp := j.db.scratch.get()
*bp = encodeInsertMutation(*bp, j.tableID, row)
werr := j.db.wal.writeRecord(recMutation, *bp)
j.db.scratch.put(bp)
return werr
}
// update appends an opUpdate record carrying nr's PK + the j.ords cells.
func (j mutJournal) update(nr Row) error {
if j.db == nil {
return nil
}
bp := j.db.scratch.get()
*bp = encodeUpdateMutation(*bp, j.tableID, nr[j.pkOrd], j.ords, nr)
werr := j.db.wal.writeRecord(recMutation, *bp)
j.db.scratch.put(bp)
return werr
}
// delete appends an opDelete record for j.pkVal.
func (j mutJournal) delete() error {
if j.db == nil {
return nil
}
bp := j.db.scratch.get()
*bp = encodeDeleteMutation(*bp, j.tableID, j.pkVal)
werr := j.db.wal.writeRecord(recMutation, *bp)
j.db.scratch.put(bp)
return werr
}
// buildRowFromTmpl materialises one row from a tuple's plan-time cell template:
// copies params from args (with API-boundary string→UUID coercion + validation),
// drops pre-validated literals straight in, and auto-generates the PK when
// omitted. Omitted columns stay zero, which is KindNull. NOT NULL on omitted
// columns is enforced at plan time, not here. The resolved row is what both the
// single-statement path and the transaction path journal, so replay reproduces
// the exact same row (including any auto-generated UUID).
func (db *DB) buildRowFromTmpl(pl *plan, tmpl []insCell, args []Value) (Row, error) {
return db.buildRowFromTmplInto(pl, tmpl, args, make(Row, len(pl.rt.def.def.Columns)))
}
// buildRowFromTmplInto is buildRowFromTmpl writing into a caller-provided row
// slice (len == column count, zeroed) instead of allocating one. A multi-row
// INSERT carves all its rows from a single backing []Value, so the batch costs
// one allocation instead of one per row.
func (db *DB) buildRowFromTmplInto(pl *plan, tmpl []insCell, args []Value, row Row) (Row, error) {
tbl := pl.rt
cols := tbl.def.def.Columns
pkOrd := tbl.def.pkOrdinal
pkProvided := false
// Stack-allocated (never escapes); referenced only by the fallback
// expression branch below. Declaring it unconditionally keeps args off the
// heap — a lazily-assigned *evalCtx makes escape analysis spill args.
ctx := &evalCtx{args: args}
for i := range tmpl {
c := &tmpl[i]
ord := c.ord
var v Value
if c.arg >= 0 {
if c.arg >= len(args) {
return nil, fmt.Errorf("%w: param index %d out of range", ErrParamMismatch, c.arg)
}
v = args[c.arg]
} else { // insCellExpr: arithmetic etc., evaluated per insert
ev, err := evalExpr(c.expr, ctx)
if err != nil {
return nil, err
}
v = ev
}
col := cols[ord]
// API-boundary coercion: a string destined for a UUID column is
// parsed into a UUID — storage only ever sees [16]byte.
if col.Type == TypeUUID && v.Kind == KindString {
u, perr := ParseUUID(v.Str())
if perr != nil {
return nil, perr
}
v = UUIDVal(u)
}
if err := validateValue(col, v); err != nil {
return nil, err
}
row[ord] = v
if ord == pkOrd {
pkProvided = true
}
}
// Auto-generate the PK when omitted. The resolved UUID is placed in the
// row before the WAL record is written, so replay reproduces it exactly.
if !pkProvided {
row[pkOrd] = UUIDVal(NewUUIDv7())
}
return row, nil
}
// buildInsertRow builds the row for a single-row INSERT (tuple 0); execInsert is
// its only caller. Multi-row INSERT and the transaction path build their per-tuple
// rows via buildRowFromTmpl directly.
func (db *DB) buildInsertRow(pl *plan, args []Value) (Row, error) {
return db.buildRowFromTmpl(pl, pl.insertTmpl[0], args)
}
// execInsert builds the row(s) and appends. Returns the count and an error.
// A multi-row INSERT (VALUES with >1 tuple) commits all rows atomically as one
// transaction — a duplicate PK anywhere fails the whole statement.
func (db *DB) execInsert(pl *plan, args []Value) (int, error) {
if len(pl.insertTmpl) > 1 {
return db.execInsertBatch(pl, args)
}
tbl := pl.rt
row, err := db.buildInsertRow(pl, args)
if err != nil {
return 0, err
}
// PK uniqueness + WAL append + apply run atomically under the shard
// lock (see insertJournaled). Ordering matters: a duplicate PK must be
// rejected before anything is journaled, and a WAL failure must abort
// before the row is applied.
var j mutJournal
if db.wal != nil {
j = mutJournal{db: db, tableID: tbl.tableID}
}
if err := tbl.insertJournaled(row, j); err != nil {
return 0, err
}
return 1, nil
}
// execInsertBatch builds every VALUES tuple's row, then commits them as one
// atomic transaction: a single TXN WAL envelope, each touched shard locked
// once, and intra-batch + against-store PK uniqueness enforced under those
// locks. This amortises the per-row envelope/lock/bufio overhead a sequence of
// single-row inserts would each pay. Reuses the transaction commit machinery.
func (db *DB) execInsertBatch(pl *plan, args []Value) (int, error) {
tbl := pl.rt
if len(pl.insertTmpl) > maxTxnMutations {
return 0, fmt.Errorf("%w: INSERT of %d rows exceeds the %d-row limit; split into smaller INSERTs",
ErrBatchTooLarge, len(pl.insertTmpl), maxTxnMutations)
}
pkOrd := tbl.def.pkOrdinal
// If the INSERT column list omits the PK, every row's PK is auto-generated
// (UUIDv7) and thus unique — no intra-batch duplicate is possible — so commit
// can skip the read-your-writes overlay and its per-row dup-check. The template
// columns are identical across tuples, so tuple 0 decides it.
autoPK := true
for i := range pl.insertTmpl[0] {
if pl.insertTmpl[0][i].ord == pkOrd {
autoPK = false
break
}
}
// Carve every row from one backing slice: each row is an ncols-wide, capped
// window into it, so the batch allocates the row storage once instead of per
// row. Capped so an in-place cell write or a later append never bleeds into a
// neighbouring row.
ncols := len(tbl.def.def.Columns)
backing := make([]Value, len(pl.insertTmpl)*ncols)
staged := make([]stagedMut, len(pl.insertTmpl))
for r := range pl.insertTmpl {
off := r * ncols
row, err := db.buildRowFromTmplInto(pl, pl.insertTmpl[r], args, backing[off:off+ncols:off+ncols])
if err != nil {
return 0, err
}
staged[r] = stagedMut{kind: opInsert, pk: row[pkOrd].UUID(), row: row}
}
tx := &Tx{db: db, rt: tbl, staged: staged, skipDup: autoPK}
if err := tx.commit(); err != nil {
return 0, err
}
return len(staged), nil
}
// --- execUpdate / execDelete: shared dispatch, locking & journaling ----------
//
// Both evaluate their per-row work, then dispatch on the WHERE shape:
//
// - PK-pinned one shard, mutated/tombstoned in place under its lock,
// journal-before-apply (the hot, allocation-free path).
// - Index-pinned the candidate set (index bucket ∪ dirty overlay) re-checked
// against the FULL WHERE; dirtyTooDenseForScan falls back to a
// scan when the overlay would cost more than scanning it.
// - Unpinned every shard lock held across journal+apply, the batch written
// as ONE TXN envelope so the statement is atomic on WAL failure
// or crash (a one-shard-at-a-time form diverges on replay).
//
// The candidate re-check uses a direct ctx predicate, not rowMatcher's compiled
// closure: that closure captures ctx and would force execPlan's argument buffer
// onto the heap on every PK/indexed write. The full-scan fallback instead copies
// args into an owned slice and uses the compiled matcher. encode/journalAll are
// nil for a memory-only DB, and journaling is always performed by the store under
// the lock(s) — never as a side effect of the match predicate.
// execUpdate evaluates the SET values once, then dispatches on the WHERE shape.
func (db *DB) execUpdate(pl *plan, args []Value) (int, error) {
st := pl.st.(*updateStmt)
tbl := pl.rt
ctx := &evalCtx{cols: tbl.def.colByName, args: args}
// Common hot path: one-column PK update. Avoid materialising a []Value
// SET buffer; compute and apply the single cell directly under the shard
// lock.
if pl.pkLookup && len(pl.updateOrdinals) == 1 {
ord := pl.updateOrdinals[0]
col := tbl.def.def.Columns[ord]
computeOne := func(r Row) (Value, error) {
ctx.row = r
v, err := evalExpr(st.sets[0].val, ctx)
if err != nil {
return Value{}, err
}
if err := validateValue(col, v); err != nil {
return Value{}, err
}
return v, nil
}
if !pl.setRowDependent {
v, err := computeOne(nil)
if err != nil {
return 0, err
}
computeOne = func(Row) (Value, error) { return v, nil }
}
keyVal, err := evalLitOrParamValue(pl.pkSource, args) // PK source is always a param/literal — no eval context needed
if err != nil {
return 0, err
}
if keyVal.IsNull() {
return 0, nil
}
var j mutJournal
if db.wal != nil {
j = mutJournal{db: db, tableID: tbl.tableID, ords: pl.updateOrdinals, pkOrd: tbl.def.pkOrdinal}
}
pk, err := coerceToUUID(keyVal)
if err != nil {
return 0, err
}
ok, err := tbl.updateByPKOneJournaled(pk, ord, computeOne, j)
if err != nil {
return 0, err
}
if ok {
return 1, nil
}
return 0, nil
}
// Index single-column fast path: one indexed-equality WHERE, a one-column SET,
// and no dirty overlay. Resolve the lone PK with lookupOne — no bucket copy
// (lookup's []UUID), no candidate set, and no SET buffer — then update in place
// under the shard lock, the same shape as the PK single-column path. lookupOne
// reports found/one against the merged view; with no dirty overlay that view is
// authoritative, so a single hit is the whole candidate set. A multi-hit bucket
// or any dirty row falls through to the general index path below (where a dirty
// PK could also be a candidate). updateOneByCandidate re-checks the full WHERE,
// so a residual conjunct beyond the indexed equality stays honoured.
if pl.idxLookup && len(pl.idxCols) == 1 && len(pl.updateOrdinals) == 1 && tbl.readDirtyCount.Load() == 0 {
if si := tbl.indexFor(pl.idxCols[0]); si != nil {
keyVal, err := evalLitOrParamValue(pl.idxSrcs[0], args) // index source is always a param/literal
if err != nil {
return 0, err
}
if keyVal.IsNull() {
return 0, nil
}
pk, found, one := si.lookupOne(keyOf(keyVal))
if !found {
return 0, nil
}
if one {
ord := pl.updateOrdinals[0]
col := tbl.def.def.Columns[ord]
computeOne := func(r Row) (Value, error) {
ctx.row = r
v, err := evalExpr(st.sets[0].val, ctx)
if err != nil {
return Value{}, err
}
if err := validateValue(col, v); err != nil {
return Value{}, err
}
return v, nil
}
if !pl.setRowDependent {
v, err := computeOne(nil)
if err != nil {
return 0, err
}
computeOne = func(Row) (Value, error) { return v, nil }
}
match := func(r Row) bool {
if st.where == nil {
return true
}
ctx.row = r
v, err := evalExpr(st.where, ctx)
return err == nil && truthy(v)
}
var j mutJournal
if db.wal != nil {
j = mutJournal{db: db, tableID: tbl.tableID, ords: pl.updateOrdinals, pkOrd: tbl.def.pkOrdinal}
}
return tbl.updateOneByCandidate(pk, ord, match, computeOne, j)
}
// found && !one: a multi-hit bucket → fall through to the general path.
}
}
// SET right-hand sides evaluate into a reused buffer. evalSet validates
// each result against its column. For constant SETs (no column ref) we
// evaluate once up front and hand back the same buffer for every row
// (allocation-free hot path); for row-dependent SETs (col = col - ?) we
// re-evaluate per row with the row in context.
buf := make([]Value, len(st.sets))
cols := tbl.def.def.Columns
evalSet := func(r Row) ([]Value, error) {
ctx.row = r
for i, a := range st.sets {
v, err := evalExpr(a.val, ctx)
if err != nil {
return nil, err
}
if err := validateValue(cols[pl.updateOrdinals[i]], v); err != nil {
return nil, err
}
buf[i] = v
}
return buf, nil
}
var compute func(Row) ([]Value, error)
if pl.setRowDependent {
compute = evalSet
} else {
if _, err := evalSet(nil); err != nil { // constant: evaluate once
return 0, err
}
compute = func(Row) ([]Value, error) { return buf, nil }
}
// PK-pinned fast path (updateByPKJournaled).
if pl.pkLookup {
keyVal, err := evalLitOrParamValue(pl.pkSource, args) // PK source is always a param/literal — no eval context needed
if err != nil {
return 0, err
}
if keyVal.IsNull() {
return 0, nil
}
var j mutJournal
if db.wal != nil {
j = mutJournal{db: db, tableID: tbl.tableID, ords: pl.updateOrdinals, pkOrd: tbl.def.pkOrdinal}
}
pk, err := coerceToUUID(keyVal)
if err != nil {
return 0, err
}
ok, err := tbl.updateByPKJournaled(pk, pl.updateOrdinals, compute, j)
if err != nil {
return 0, err
}
if ok {
return 1, nil
}
return 0, nil
}
// encode/journalAll for the candidate + full-scan paths (nil = memory-only).
var encode func(Row) []byte
var journalAll func([][]byte) error
if db.wal != nil {
encode = func(nr Row) []byte {
return encodeUpdateMutation(nil, tbl.tableID, nr[tbl.def.pkOrdinal], pl.updateOrdinals, nr)
}
journalAll = db.journalTxnBodies
}
// Index-pinned: re-check the candidate set against the full WHERE.
if pl.idxLookup && !tbl.dirtyTooDenseForScan() {
cand, ok, err := db.idxCandidates(pl, ctx)
if err != nil {
return 0, err
}
if !ok {
return 0, nil
}
// Use the index bucket directly when there is no dirty overlay; only
// materialise + dedup the candidate set when dirty candidates may overlap it.
pks := cand.pks
if len(cand.dirty) > 0 {
pks = nil
cand.emit(func(pk UUID) bool { pks = append(pks, pk); return false })
}
// Re-check the full WHERE on each candidate with a direct ctx predicate.
match := func(r Row) bool {
if st.where == nil {
return true
}
ctx.row = r
v, err := evalExpr(st.where, ctx)
return err == nil && truthy(v)
}
// Fast path — exactly one resolved candidate + single-column SET: update in
// place, no full-row clone (the bulk of the cost), mirroring the PK
// single-column path. This does NOT require a unique index; it activates
// whenever the candidate set happens to hold a single row (the full WHERE is
// still re-checked on it below).
if len(pks) == 1 && len(pl.updateOrdinals) == 1 {
ord := pl.updateOrdinals[0]
computeOne := func(r Row) (Value, error) {
v, cerr := compute(r)
if cerr != nil {
return Value{}, cerr
}
return v[0], nil
}
var j mutJournal
if db.wal != nil {
j = mutJournal{db: db, tableID: tbl.tableID, ords: pl.updateOrdinals, pkOrd: tbl.def.pkOrdinal}
}
return tbl.updateOneByCandidate(pks[0], ord, match, computeOne, j)
}
return tbl.updateByCandidates(pks, match, pl.updateOrdinals, compute, encode, journalAll)
}
// PK pinned inside an AND-chain with no index path: resolve the single PK and
// re-check the full WHERE on it via the same candidate machinery, instead of
// scanning. One candidate, so a single-column SET takes the update-in-place lane.
if pl.pkProbe != nil {
keyVal, err := evalExpr(pl.pkProbe, ctx)
if err != nil {
return 0, err
}
if keyVal.IsNull() {
return 0, nil
}
pk, err := coerceToUUID(keyVal)
if err != nil {
return 0, err
}
match := func(r Row) bool {
ctx.row = r
v, err := evalExpr(st.where, ctx)
return err == nil && truthy(v)
}
var j mutJournal
if db.wal != nil {
j = mutJournal{db: db, tableID: tbl.tableID, ords: pl.updateOrdinals, pkOrd: tbl.def.pkOrdinal}
}
if len(pl.updateOrdinals) == 1 {
ord := pl.updateOrdinals[0]
computeOne := func(r Row) (Value, error) {
v, cerr := compute(r)
if cerr != nil {
return Value{}, cerr
}
return v[0], nil
}
return tbl.updateOneByCandidate(pk, ord, match, computeOne, j)
}
return tbl.updateByCandidates([]UUID{pk}, match, pl.updateOrdinals, compute, encode, journalAll)
}
// Full-scan fallback: compiled matcher over an owned args copy.
matchArgs := make([]Value, len(args))
copy(matchArgs, args)
match := rowMatcher(st.where, &evalCtx{cols: tbl.def.colByName, args: matchArgs})
return tbl.updateWhereAll(match, pl.updateOrdinals, compute, encode, journalAll)
}
// execDelete dispatches on the WHERE shape. deleteByPKJournaled's PK path also
// closes a getByPK→deleteByPK TOCTOU under the one shard lock.
func (db *DB) execDelete(pl *plan, args []Value) (int, error) {
st := pl.st.(*deleteStmt)
tbl := pl.rt
ctx := &evalCtx{cols: tbl.def.colByName, args: args}
// PK-pinned fast path (deleteByPKJournaled).
if pl.pkLookup {
keyVal, err := evalLitOrParamValue(pl.pkSource, args) // PK source is always a param/literal
if err != nil {
return 0, err
}
if keyVal.IsNull() {
return 0, nil
}
pk, err := coerceToUUID(keyVal)
if err != nil {
return 0, err
}
var j mutJournal
if db.wal != nil {
// journal the resolved UUID, not the raw arg
j = mutJournal{db: db, tableID: tbl.tableID, pkVal: UUIDVal(pk)}
}
ok, err := tbl.deleteByPKJournaled(pk, j)
if err != nil {
return 0, err
}
if ok {
return 1, nil
}
return 0, nil
}
// encode/journalAll for the candidate + full-scan paths (nil = memory-only).
var encode func(Value) []byte
var journalAll func([][]byte) error
if db.wal != nil {
encode = func(pk Value) []byte {
return encodeDeleteMutation(nil, tbl.tableID, pk)
}
journalAll = db.journalTxnBodies
}
// Index-pinned: re-check the candidate set against the full WHERE.
if pl.idxLookup && !tbl.dirtyTooDenseForScan() {
// Re-check the full WHERE on each candidate with a direct ctx predicate.
// Scoped to this branch so the PK and full-scan paths don't build it.
match := func(r Row) bool {
if st.where == nil {
return true
}
ctx.row = r
v, err := evalExpr(st.where, ctx)
return err == nil && truthy(v)
}
// Single-row fast path: one indexed equality, no dirty overlay, exactly one
// index hit → delete it directly (one shard lock, single MUTATION record like
// the PK path), skipping the []UUID candidate slice + multi-shard machinery.
// match still re-checks the full WHERE, so a residual conjunct is honoured.
if len(pl.idxCols) == 1 && tbl.readDirtyCount.Load() == 0 {
keyVal, err := evalLitOrParamValue(pl.idxSrcs[0], args) // index source is always a param/literal — no eval context needed
if err != nil {
return 0, err
}
if keyVal.IsNull() {
return 0, nil
}
if si := tbl.indexFor(pl.idxCols[0]); si != nil {
pk, found, one := si.lookupOne(keyOf(keyVal))
if found && one {
var j mutJournal
if db.wal != nil {
j = mutJournal{db: db, tableID: tbl.tableID, pkVal: UUIDVal(pk)}
}
ok, err := tbl.deleteOneByCandidate(pk, match, j)
if err != nil {
return 0, err
}
if ok {
return 1, nil
}
return 0, nil
}
if !found {
return 0, nil // no index hit and no dirty overlay → nothing matches
}
// found && !one: multi-hit bucket → fall through to the candidate path.
}
}
cand, ok, err := db.idxCandidates(pl, ctx)
if err != nil {
return 0, err
}
if !ok {
return 0, nil
}
pks := cand.pks
if len(cand.dirty) > 0 {
pks = nil
cand.emit(func(pk UUID) bool { pks = append(pks, pk); return false })
}
return tbl.deleteByCandidates(pks, match, encode, journalAll)
}
// PK pinned inside an AND-chain with no index path: resolve the single PK and
// re-check the full WHERE on it (deleteOneByCandidate honours the residual),
// instead of scanning.
if pl.pkProbe != nil {
keyVal, err := evalExpr(pl.pkProbe, ctx)
if err != nil {
return 0, err
}
if keyVal.IsNull() {
return 0, nil
}
pk, err := coerceToUUID(keyVal)
if err != nil {
return 0, err
}
match := func(r Row) bool {
ctx.row = r
v, err := evalExpr(st.where, ctx)
return err == nil && truthy(v)
}
var j mutJournal
if db.wal != nil {
j = mutJournal{db: db, tableID: tbl.tableID, pkVal: UUIDVal(pk)}
}
ok, err := tbl.deleteOneByCandidate(pk, match, j)
if err != nil {
return 0, err
}
if ok {
return 1, nil
}
return 0, nil
}
// Full-scan fallback: compiled matcher over an owned args copy.
matchArgs := make([]Value, len(args))
copy(matchArgs, args)
match := rowMatcher(st.where, &evalCtx{cols: tbl.def.colByName, args: matchArgs})
return tbl.deleteWhereAll(match, encode, journalAll)
}