vibesql-executor 0.2.0

Query execution engine for vibesql SQL database
Documentation
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
//! Foreign key constraint validation for UPDATE operations

use vibesql_storage::{Database, DeferredFkViolation, DeferredFkViolationKind};

use crate::errors::ExecutorError;

/// Validator for foreign key constraints
pub struct ForeignKeyValidator;

impl ForeignKeyValidator {
    /// Validate FOREIGN KEY constraints for a new row, returning any
    /// deferred violations rather than queueing them directly.
    ///
    /// Checks that all foreign key values in the row reference existing parent rows.
    /// NULL values in foreign keys are allowed (not considered violations).
    ///
    /// Phase C2 of #5085: when the constraint is `INITIALLY DEFERRED`
    /// or the session has `PRAGMA defer_foreign_keys=ON` (and a
    /// transaction is active), a missing parent row produces a
    /// `DeferredFkViolation` in the returned vector instead of an
    /// immediate `Err`. The caller must push the returned violations
    /// onto the transaction queue once any immutable borrow of
    /// `database` is released.
    pub fn collect_constraints(
        db: &Database,
        table_name: &str,
        row_values: &[vibesql_types::SqlValue],
    ) -> Result<Vec<DeferredFkViolation>, ExecutorError> {
        let mut deferred: Vec<DeferredFkViolation> = Vec::new();

        // Skip FK enforcement when PRAGMA foreign_keys is OFF (default)
        if !db.foreign_keys_enabled() {
            return Ok(deferred);
        }

        let session_defer = db.defer_foreign_keys();
        let in_txn = db.in_transaction();

        let schema = db
            .catalog
            .get_table(table_name)
            .ok_or_else(|| ExecutorError::TableNotFound(table_name.to_string()))?;

        for (fk_idx, fk) in schema.foreign_keys.iter().enumerate() {
            // Mismatch check runs before any row-existence test so that bad
            // FK targets are reported even when the parent table is empty.
            // Mismatch is never deferred (matches SQLite behaviour).
            if let Some((child, parent)) =
                crate::foreign_key_check::detect_fk_mismatch(db, table_name, fk)
            {
                return Err(ExecutorError::ForeignKeyMismatch { child, parent });
            }

            // Extract FK values from the new row
            let fk_values: Vec<vibesql_types::SqlValue> =
                fk.column_indices.iter().map(|&idx| row_values[idx].clone()).collect();

            // If any part of the foreign key is NULL, the constraint is not violated.
            if fk_values.iter().any(|v| v.is_null()) {
                continue;
            }

            // Check if the referenced key exists in the parent table
            let parent_table = db
                .get_table(&fk.parent_table)
                .ok_or_else(|| ExecutorError::TableNotFound(fk.parent_table.clone()))?;

            let parent_collations = crate::foreign_key_check::parent_collations_for_fk(db, fk);
            let parent_indices = crate::foreign_key_check::resolved_parent_indices_for_fk(db, fk);

            // Phase 1d follow-up (#5205): the parent-existence check on
            // the UPDATE path must honor MVCC visibility for the same
            // reason as INSERT — an uncommitted concurrent INSERT on the
            // parent must not satisfy this child's FK. Off-state
            // (`mvcc_enabled` OFF) preserves the previous `scan().iter()`
            // semantics (including bitmap-deleted physical rows).
            let snapshot = crate::mvcc::read_snapshot(db);
            let key_exists = {
                #[cfg(feature = "mvcc_enabled")]
                {
                    parent_table.scan_visible(&snapshot).any(|(_, parent_row)| {
                        parent_indices.iter().zip(&fk_values).enumerate().all(
                            |(i, (&parent_idx, fk_val))| match parent_row.get(parent_idx) {
                                Some(parent_val) => {
                                    crate::foreign_key_check::fk_values_equal(
                                        fk_val,
                                        parent_val,
                                        parent_collations.get(i).and_then(|c| c.as_deref()),
                                    )
                                }
                                None => false,
                            },
                        )
                    })
                }
                #[cfg(not(feature = "mvcc_enabled"))]
                {
                    let _ = &snapshot;
                    parent_table.scan().iter().any(|parent_row| {
                        parent_indices.iter().zip(&fk_values).enumerate().all(
                            |(i, (&parent_idx, fk_val))| match parent_row.get(parent_idx) {
                                Some(parent_val) => {
                                    crate::foreign_key_check::fk_values_equal(
                                        fk_val,
                                        parent_val,
                                        parent_collations.get(i).and_then(|c| c.as_deref()),
                                    )
                                }
                                None => false,
                            },
                        )
                    })
                }
            };

            if key_exists {
                continue;
            }

            let should_defer = in_txn && (fk.initially_deferred || session_defer);
            if should_defer {
                deferred.push(DeferredFkViolation {
                    child_table: table_name.to_string(),
                    fk_index: fk_idx,
                    child_row: row_values.to_vec(),
                    kind: DeferredFkViolationKind::ChildInsertOrUpdate,
                });
                continue;
            }

            return Err(ExecutorError::ConstraintViolation(format!(
                "FOREIGN KEY constraint \'{}\' violated: key ({}) not found in table \'{}\'",
                fk.name.as_deref().unwrap_or(""),
                fk.column_names.join(", "),
                fk.parent_table
            )));
        }

        Ok(deferred)
    }

    /// Check that no child tables reference a row that is about to be deleted or updated.
    ///
    /// This is called before updating a primary key to ensure referential integrity.
    /// If foreign keys have ON UPDATE CASCADE, this function will propagate the update to child
    /// rows.
    pub fn check_no_child_references(
        db: &mut Database,
        parent_table_name: &str,
        parent_row: &vibesql_storage::Row,
        new_parent_row: &vibesql_storage::Row,
    ) -> Result<(), ExecutorError> {
        // Skip FK enforcement when PRAGMA foreign_keys is OFF (default)
        if !db.foreign_keys_enabled() {
            return Ok(());
        }

        let parent_schema = db
            .catalog
            .get_table(parent_table_name)
            .ok_or_else(|| ExecutorError::TableNotFound(parent_table_name.to_string()))?;

        // This check is only meaningful if the parent table has a primary key.
        let pk_indices = match parent_schema.get_primary_key_indices() {
            Some(indices) => indices,
            None => return Ok(()),
        };

        let old_parent_key_values: Vec<vibesql_types::SqlValue> =
            pk_indices.iter().map(|&idx| parent_row.values[idx].clone()).collect();

        let new_parent_key_values: Vec<vibesql_types::SqlValue> =
            pk_indices.iter().map(|&idx| new_parent_row.values[idx].clone()).collect();

        // Optimization: Check if any table in the database has foreign keys at all
        // If not, skip the expensive scan of all tables
        let has_any_fks = db.catalog.list_tables().iter().any(|table_name| {
            db.catalog
                .get_table(table_name)
                .map(|schema| !schema.foreign_keys.is_empty())
                .unwrap_or(false)
        });

        if !has_any_fks {
            return Ok(());
        }

        // Phase C2 of #5085: cache session defer + transaction state.
        let session_defer = db.defer_foreign_keys();
        let in_txn = db.in_transaction();

        // Collect cascade updates to apply after scanning (to avoid borrow
        // checker issues). Each entry carries the row index, the OLD child
        // row, and the NEW child row so the cascade can fire the child
        // table's BEFORE/AFTER UPDATE triggers with both pseudo-rows (#5440).
        // Each entry also carries the FK constraint + its index so that a
        // cascade-fired BEFORE UPDATE trigger RAISE(IGNORE) (SkipRow) can run
        // the statement-end orphan FK check on the surviving row (#5465).
        #[allow(clippy::type_complexity)]
        let mut cascade_updates: Vec<(
            String,
            vibesql_catalog::ForeignKeyConstraint,
            usize,
            Vec<(usize, vibesql_storage::Row, vibesql_storage::Row)>,
        )> = Vec::new();

        // Phase C2: collected NO ACTION orphans to queue after scanning
        // (queueing requires &mut Database, which conflicts with the
        // catalog/table borrows held inside the scan loop).
        let mut deferred_parent_orphans: Vec<(
            String,
            vibesql_catalog::ForeignKeyConstraint,
            usize,
            Vec<(usize, vibesql_storage::Row)>,
        )> = Vec::new();

        // Scan all tables in the database to find foreign keys that reference this table.
        for table_name in db.catalog.list_tables() {
            let child_schema = db.catalog.get_table(&table_name).unwrap().clone();

            // Skip tables without foreign keys (optimization)
            if child_schema.foreign_keys.is_empty() {
                continue;
            }

            for (fk_idx, fk) in child_schema.foreign_keys.iter().enumerate() {
                // Use case-insensitive comparison for SQL identifier matching
                if !fk.parent_table.eq_ignore_ascii_case(parent_table_name) {
                    continue;
                }

                // Resolve parent-side collations before borrowing the
                // child table so the FK comparison honors NOCASE/RTRIM
                // on the parent key (#5147).
                let parent_collations =
                    crate::foreign_key_check::parent_collations_for_fk(db, fk);

                // Get the child table and find matching rows.
                //
                // Phase 1d follow-up (#5205): the child-reference scan
                // must respect MVCC visibility. A child row that has been
                // deleted by a concurrent committed transaction must not
                // count as "referencing" the parent under our snapshot.
                // Equally, our own in-txn child writes participate via
                // the widened BEGIN-time snapshot (#5223). Off-state
                // (`mvcc_enabled` OFF) preserves the pre-MVCC behavior of
                // walking every physical row via `scan().iter()`.
                let snapshot = crate::mvcc::read_snapshot(db);
                let child_table = db.get_table(&table_name).unwrap();
                let matches_fk = |child_row: &vibesql_storage::Row| -> bool {
                    let child_fk_values: Vec<vibesql_types::SqlValue> = fk
                        .column_indices
                        .iter()
                        .map(|&col_idx| child_row.values[col_idx].clone())
                        .collect();
                    child_fk_values
                        .iter()
                        .zip(&old_parent_key_values)
                        .enumerate()
                        .all(|(i, (cv, pv))| {
                            crate::foreign_key_check::fk_values_equal(
                                cv,
                                pv,
                                parent_collations.get(i).and_then(|c| c.as_deref()),
                            )
                        })
                };
                let matching_rows: Vec<(usize, vibesql_storage::Row)> = {
                    #[cfg(feature = "mvcc_enabled")]
                    {
                        child_table
                            .scan_visible(&snapshot)
                            .filter_map(|(idx, child_row)| {
                                if matches_fk(child_row) {
                                    Some((idx, child_row.clone()))
                                } else {
                                    None
                                }
                            })
                            .collect()
                    }
                    #[cfg(not(feature = "mvcc_enabled"))]
                    {
                        let _ = &snapshot;
                        child_table
                            .scan()
                            .iter()
                            .enumerate()
                            .filter_map(|(idx, child_row)| {
                                if matches_fk(child_row) {
                                    Some((idx, child_row.clone()))
                                } else {
                                    None
                                }
                            })
                            .collect()
                    }
                };

                if !matching_rows.is_empty() {
                    // Check the referential action
                    match fk.on_update {
                        vibesql_catalog::ReferentialAction::Cascade => {
                            // Prepare cascade updates for this table, keeping
                            // the OLD row so child UPDATE triggers see it.
                            let updated_rows: Vec<(
                                usize,
                                vibesql_storage::Row,
                                vibesql_storage::Row,
                            )> = matching_rows
                                .into_iter()
                                .map(|(row_idx, old_child_row)| {
                                    let mut child_row = old_child_row.clone();
                                    // Update the FK columns to match the new parent key
                                    for (fk_col_idx, new_parent_val) in
                                        fk.column_indices.iter().zip(&new_parent_key_values)
                                    {
                                        child_row.values[*fk_col_idx] = new_parent_val.clone();
                                    }
                                    (row_idx, old_child_row, child_row)
                                })
                                .collect();

                            cascade_updates.push((
                                table_name.clone(),
                                fk.clone(),
                                fk_idx,
                                updated_rows,
                            ));
                        }
                        vibesql_catalog::ReferentialAction::SetNull => {
                            // Set child FK columns to NULL
                            let updated_rows: Vec<(
                                usize,
                                vibesql_storage::Row,
                                vibesql_storage::Row,
                            )> = matching_rows
                                .into_iter()
                                .map(|(row_idx, old_child_row)| {
                                    let mut child_row = old_child_row.clone();
                                    // Set FK columns to NULL
                                    for &fk_col_idx in &fk.column_indices {
                                        child_row.values[fk_col_idx] =
                                            vibesql_types::SqlValue::Null;
                                    }
                                    (row_idx, old_child_row, child_row)
                                })
                                .collect();

                            cascade_updates.push((
                                table_name.clone(),
                                fk.clone(),
                                fk_idx,
                                updated_rows,
                            ));
                        }
                        vibesql_catalog::ReferentialAction::SetDefault => {
                            // Set child FK columns to their default values by evaluating
                            // expressions First, collect default
                            // expressions (clone to avoid holding borrow)
                            let default_exprs: Vec<Option<vibesql_ast::Expression>> = fk
                                .column_indices
                                .iter()
                                .map(|&fk_col_idx| {
                                    child_schema.columns[fk_col_idx].default_value.clone()
                                })
                                .collect();

                            // Evaluate default values for each FK column
                            let mut default_values: Vec<vibesql_types::SqlValue> = Vec::new();
                            for default_expr_opt in default_exprs {
                                let default_value = if let Some(default_expr) = default_expr_opt {
                                    // Evaluate the default expression
                                    match default_expr {
                                        vibesql_ast::Expression::NextValue { sequence_name } => {
                                            // Get the next value from the sequence
                                            let seq =
                                                db.catalog
                                                    .get_sequence_mut(&sequence_name)
                                                    .map_err(|e| {
                                                        ExecutorError::UnsupportedExpression(
                                                            format!("Sequence error: {:?}", e),
                                                        )
                                                    })?;
                                            let next_val = seq.next_value().map_err(|e| {
                                                ExecutorError::ConstraintViolation(format!(
                                                    "Sequence error: {}",
                                                    e
                                                ))
                                            })?;
                                            vibesql_types::SqlValue::Integer(next_val)
                                        }
                                        _ => crate::insert::defaults::evaluate_default_expression(
                                            &default_expr,
                                        )?,
                                    }
                                } else {
                                    // No default value defined, use NULL
                                    vibesql_types::SqlValue::Null
                                };
                                default_values.push(default_value);
                            }

                            // Apply default values to matching rows
                            let updated_rows: Vec<(
                                usize,
                                vibesql_storage::Row,
                                vibesql_storage::Row,
                            )> = matching_rows
                                .into_iter()
                                .map(|(row_idx, old_child_row)| {
                                    let mut child_row = old_child_row.clone();
                                    // Set FK columns to their default values
                                    for (i, &fk_col_idx) in fk.column_indices.iter().enumerate() {
                                        child_row.values[fk_col_idx] = default_values[i].clone();
                                    }
                                    (row_idx, old_child_row, child_row)
                                })
                                .collect();

                            cascade_updates.push((
                                table_name.clone(),
                                fk.clone(),
                                fk_idx,
                                updated_rows,
                            ));
                        }
                        vibesql_catalog::ReferentialAction::Restrict => {
                            // RESTRICT is immediate by default, but the
                            // session pragma `defer_foreign_keys=ON`
                            // delays it until COMMIT (EVIDENCE-OF
                            // R-18981-16292; see fkey6-3.2.3). Per-
                            // constraint INITIALLY DEFERRED does *not*
                            // defer RESTRICT — only the session pragma
                            // does. Mirrors the DELETE-side handling in
                            // delete/integrity.rs.
                            let should_defer = in_txn && session_defer;
                            if should_defer {
                                deferred_parent_orphans.push((
                                    table_name.clone(),
                                    fk.clone(),
                                    fk_idx,
                                    matching_rows.clone(),
                                ));
                            } else {
                                return Err(ExecutorError::ConstraintViolation(format!(
                                    "FOREIGN KEY constraint violation: cannot update a parent row when a foreign key constraint exists. The conflict occurred in table \'{}\', constraint \'{}\'.",
                                    table_name,
                                    fk.name.as_deref().unwrap_or(""),
                                )));
                            }
                        }
                        vibesql_catalog::ReferentialAction::NoAction => {
                            // NO ACTION can be deferred (Phase C2 of
                            // #5085). When deferred, queue every
                            // orphaned child row for COMMIT-time
                            // re-validation; otherwise fail immediately.
                            let should_defer = in_txn && (fk.initially_deferred || session_defer);
                            if should_defer {
                                deferred_parent_orphans.push((
                                    table_name.clone(),
                                    fk.clone(),
                                    fk_idx,
                                    matching_rows.clone(),
                                ));
                            } else {
                                return Err(ExecutorError::ConstraintViolation(format!(
                                    "FOREIGN KEY constraint violation: cannot update a parent row when a foreign key constraint exists. The conflict occurred in table \'{}\', constraint \'{}\'.",
                                    table_name,
                                    fk.name.as_deref().unwrap_or(""),
                                )));
                            }
                        }
                    }
                }
            }
        }

        // Apply cascade updates. Phase 1c (Issue #5150 / #5136): stamp
        // xmin on every cascade-update new row version. Off-state is a
        // no-op. We capture the txn id once outside the per-table loop
        // since the txn doesn't change within an UPDATE.
        //
        // FK cascade fires the child table's BEFORE/AFTER UPDATE row
        // triggers for every cascaded row, matching sqlite3 3.51 (#5440).
        // sqlite3 fires these regardless of the `recursive_triggers`
        // pragma; the depth-16 RecursionGuard inside the firing helpers
        // bounds multi-level FK chains. Per row the order is
        //   BEFORE child trigger -> update -> AFTER child trigger
        // (verified against sqlite3 3.51.0). A RAISE(IGNORE) (SkipRow) in a
        // BEFORE trigger abandons that child row's cascade update; a
        // RAISE(ABORT|FAIL|ROLLBACK) propagates as Err and aborts the
        // whole statement.
        let txn_id = db.transaction_id();
        for (table_name, fk, fk_idx, mut updates) in cascade_updates {
            for (_row_idx, _old_row, new_row) in updates.iter_mut() {
                vibesql_storage::stamp_xmin_for_write(new_row, txn_id);
                new_row.xmax = None;
            }

            let has_child_update_triggers = db
                .catalog
                .get_triggers_for_table(&table_name, Some(vibesql_ast::TriggerEvent::Update(None)))
                .next()
                .is_some();

            for (row_idx, old_row, new_row) in updates {
                // BEFORE UPDATE row triggers on the child.
                if has_child_update_triggers {
                    let outcome = crate::TriggerFirer::execute_before_triggers(
                        db,
                        &table_name,
                        vibesql_ast::TriggerEvent::Update(None),
                        Some(&old_row),
                        Some(&new_row),
                    )?;
                    if outcome == crate::TriggerOutcome::SkipRow {
                        // RAISE(IGNORE) abandons this cascade update: the
                        // surviving child keeps its OLD FK value, which
                        // references the parent key that is about to change
                        // (the parent UPDATE is applied after this cascade
                        // returns). That is an orphaned FK reference and must
                        // trip the statement-end FK check, matching sqlite3
                        // 3.51 (#5465). Immediate FK -> raise now (statement
                        // savepoint rolls the statement back); deferred FK ->
                        // queue the surviving OLD row for the COMMIT re-check.
                        let should_defer = in_txn && (fk.initially_deferred || session_defer);
                        if should_defer {
                            db.queue_deferred_fk_violation(DeferredFkViolation {
                                child_table: table_name.clone(),
                                fk_index: fk_idx,
                                child_row: old_row.values.to_vec(),
                                kind: DeferredFkViolationKind::ChildInsertOrUpdate,
                            });
                        } else {
                            return Err(ExecutorError::ConstraintViolation(
                                "FOREIGN KEY constraint failed".to_string(),
                            ));
                        }
                        continue;
                    }
                }

                let child_table = db.get_table_mut(&table_name).unwrap();
                child_table
                    .update_row(row_idx, new_row.clone())
                    .map_err(|e| ExecutorError::StorageError(e.to_string()))?;

                // AFTER UPDATE row triggers fire once the row is updated.
                if has_child_update_triggers {
                    let _after = crate::TriggerFirer::execute_after_triggers(
                        db,
                        &table_name,
                        vibesql_ast::TriggerEvent::Update(None),
                        Some(&old_row),
                        Some(&new_row),
                    )?;
                }
            }
            // Rebuild indexes after updates (following the same pattern as DELETE operations)
            db.rebuild_indexes(&table_name);
        }

        // Phase C2 of #5085: queue NO ACTION orphans onto the deferred
        // FK queue for COMMIT-time re-validation.
        for (table_name, _fk, fk_idx, matching_rows) in deferred_parent_orphans {
            for (_row_idx, child_row) in matching_rows {
                db.queue_deferred_fk_violation(DeferredFkViolation {
                    child_table: table_name.clone(),
                    fk_index: fk_idx,
                    child_row: child_row.values.to_vec(),
                    kind: DeferredFkViolationKind::ChildInsertOrUpdate,
                });
            }
        }

        Ok(())
    }
}