vibesql-parser 0.2.0

SQL parser for vibesql database engine
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
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
use super::super::*;

// ========================================================================
// Constraint Tests - Table-level constraints
// ========================================================================

#[test]
fn test_parse_create_table_with_table_level_primary_key() {
    let result = Parser::parse_sql(
        "CREATE TABLE order_items (
            order_id INTEGER,
            product_id INTEGER,
            quantity INTEGER,
            PRIMARY KEY (order_id, product_id)
        );",
    );
    assert!(result.is_ok(), "Should parse table-level PRIMARY KEY");
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::CreateTable(create) => {
            assert_eq!(create.table_constraints.len(), 1);
            match &create.table_constraints[0] {
                vibesql_ast::TableConstraint {
                    kind: vibesql_ast::TableConstraintKind::PrimaryKey { columns, .. },
                    ..
                } => {
                    assert_eq!(columns.len(), 2);
                    assert_eq!(columns[0].expect_column_name(), "order_id");
                    assert_eq!(columns[1].expect_column_name(), "product_id");
                }
                _ => panic!("Expected PRIMARY KEY constraint"),
            }
        }
        _ => panic!("Expected CREATE TABLE statement"),
    }
}

#[test]
fn test_parse_create_table_with_foreign_key() {
    let result = Parser::parse_sql(
        "CREATE TABLE orders (
            id INTEGER PRIMARY KEY,
            customer_id INTEGER,
            FOREIGN KEY (customer_id) REFERENCES customers(id)
        );",
    );
    assert!(result.is_ok(), "Should parse FOREIGN KEY constraint");
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::CreateTable(create) => {
            assert_eq!(create.table_constraints.len(), 1);
            match &create.table_constraints[0] {
                vibesql_ast::TableConstraint {
                    kind:
                        vibesql_ast::TableConstraintKind::ForeignKey {
                            columns,
                            references_table,
                            references_columns,
                            on_delete,
                            on_update,
                            ..
                        },
                    ..
                } => {
                    assert_eq!(columns.len(), 1);
                    assert_eq!(columns[0], "customer_id");
                    assert_eq!(references_table, "customers");
                    assert_eq!(references_columns.len(), 1);
                    assert_eq!(references_columns[0], "id");
                    assert!(on_delete.is_none());
                    assert!(on_update.is_none());
                }
                _ => panic!("Expected FOREIGN KEY constraint"),
            }
        }
        _ => panic!("Expected CREATE TABLE statement"),
    }
}

#[test]
fn test_parse_create_table_with_foreign_key_on_delete_update() {
    let result = Parser::parse_sql(
        "CREATE TABLE child (
            id INT PRIMARY KEY,
            parent_id INT REFERENCES parent(id) ON DELETE CASCADE ON UPDATE SET NULL
        );",
    );
    assert!(result.is_ok(), "Should parse FOREIGN KEY with ON DELETE/UPDATE");
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::CreateTable(create) => {
            // Find the REFERENCES constraint in column constraints
            let column = &create.columns[1]; // parent_id column
            assert_eq!(column.constraints.len(), 1);
            match &column.constraints[0] {
                vibesql_ast::ColumnConstraint {
                    kind:
                        vibesql_ast::ColumnConstraintKind::References {
                            table,
                            column: col,
                            on_delete,
                            on_update,
                            ..
                        },
                    ..
                } => {
                    assert_eq!(table, "parent");
                    assert_eq!(col, &Some("id".to_string()));
                    assert_eq!(on_delete, &Some(vibesql_ast::ReferentialAction::Cascade));
                    assert_eq!(on_update, &Some(vibesql_ast::ReferentialAction::SetNull));
                }
                _ => panic!("Expected REFERENCES constraint"),
            }
        }
        _ => panic!("Expected CREATE TABLE statement"),
    }
}

#[test]
fn test_parse_create_table_with_table_foreign_key_on_delete_update() {
    let result = Parser::parse_sql(
        "CREATE TABLE orders (
            id INT PRIMARY KEY,
            customer_id INT,
            FOREIGN KEY (customer_id) REFERENCES customers(id) ON DELETE NO ACTION ON UPDATE SET DEFAULT
        );",
    );
    assert!(result.is_ok(), "Should parse table-level FOREIGN KEY with ON DELETE/UPDATE");
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::CreateTable(create) => {
            assert_eq!(create.table_constraints.len(), 1);
            match &create.table_constraints[0] {
                vibesql_ast::TableConstraint {
                    kind:
                        vibesql_ast::TableConstraintKind::ForeignKey {
                            columns,
                            references_table,
                            references_columns,
                            on_delete,
                            on_update,
                            ..
                        },
                    ..
                } => {
                    assert_eq!(columns.len(), 1);
                    assert_eq!(columns[0], "customer_id");
                    assert_eq!(references_table, "customers");
                    assert_eq!(references_columns.len(), 1);
                    assert_eq!(references_columns[0], "id");
                    assert_eq!(on_delete, &Some(vibesql_ast::ReferentialAction::NoAction));
                    assert_eq!(on_update, &Some(vibesql_ast::ReferentialAction::SetDefault));
                }
                _ => panic!("Expected FOREIGN KEY constraint"),
            }
        }
        _ => panic!("Expected CREATE TABLE statement"),
    }
}

#[test]
fn test_parse_create_table_with_foreign_key_on_delete_only() {
    let result = Parser::parse_sql(
        "CREATE TABLE child (
            id INT PRIMARY KEY,
            parent_id INT REFERENCES parent(id) ON DELETE SET DEFAULT
        );",
    );
    assert!(result.is_ok(), "Should parse FOREIGN KEY with ON DELETE only");
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::CreateTable(create) => {
            let column = &create.columns[1];
            match &column.constraints[0] {
                vibesql_ast::ColumnConstraint {
                    kind: vibesql_ast::ColumnConstraintKind::References { on_delete, on_update, .. },
                    ..
                } => {
                    assert_eq!(on_delete, &Some(vibesql_ast::ReferentialAction::SetDefault));
                    assert!(on_update.is_none());
                }
                _ => panic!("Expected REFERENCES constraint"),
            }
        }
        _ => panic!("Expected CREATE TABLE statement"),
    }
}

#[test]
fn test_parse_create_table_with_table_level_unique() {
    let result = Parser::parse_sql(
        "CREATE TABLE users (
            id INTEGER PRIMARY KEY,
            email VARCHAR(100),
            username VARCHAR(50),
            UNIQUE (email, username)
        );",
    );
    assert!(result.is_ok(), "Should parse table-level UNIQUE constraint");
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::CreateTable(create) => {
            assert_eq!(create.table_constraints.len(), 1);
            match &create.table_constraints[0] {
                vibesql_ast::TableConstraint {
                    kind: vibesql_ast::TableConstraintKind::Unique { columns, .. },
                    ..
                } => {
                    assert_eq!(columns.len(), 2);
                    assert_eq!(columns[0].expect_column_name(), "email");
                    assert_eq!(columns[1].expect_column_name(), "username");
                }
                _ => panic!("Expected UNIQUE constraint"),
            }
        }
        _ => panic!("Expected CREATE TABLE statement"),
    }
}

#[test]
fn test_parse_create_table_with_table_level_check() {
    let result = Parser::parse_sql(
        "CREATE TABLE products (
            price NUMERIC(10, 2),
            discount NUMERIC(10, 2),
            CHECK (discount < price)
        );",
    );
    assert!(result.is_ok(), "Should parse table-level CHECK constraint");
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::CreateTable(create) => {
            assert_eq!(create.table_constraints.len(), 1);
            assert!(matches!(
                create.table_constraints[0],
                vibesql_ast::TableConstraint {
                    kind: vibesql_ast::TableConstraintKind::Check { .. },
                    ..
                }
            ));
        }
        _ => panic!("Expected CREATE TABLE statement"),
    }
}

#[test]
fn test_parse_create_table_with_indexed_column_prefix() {
    // Test from issue #1620: MySQL indexed column prefix syntax
    let result = Parser::parse_sql("CREATE TABLE t7(a TEXT, UNIQUE (a(1)))");
    assert!(result.is_ok(), "Should parse UNIQUE with column prefix: {:?}", result.err());
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::CreateTable(create) => {
            assert_eq!(create.table_name, "t7");
            assert_eq!(create.table_constraints.len(), 1);
            match &create.table_constraints[0] {
                vibesql_ast::TableConstraint {
                    kind: vibesql_ast::TableConstraintKind::Unique { columns, .. },
                    ..
                } => {
                    assert_eq!(columns.len(), 1);
                    assert_eq!(columns[0].expect_column_name(), "a");
                    assert_eq!(columns[0].prefix_length(), Some(1));
                }
                _ => panic!("Expected UNIQUE constraint"),
            }
        }
        _ => panic!("Expected CREATE TABLE statement"),
    }
}

#[test]
fn test_parse_create_table_with_primary_key_prefix() {
    let result = Parser::parse_sql("CREATE TABLE t8(name VARCHAR(100), PRIMARY KEY (name(50)))");
    assert!(result.is_ok(), "Should parse PRIMARY KEY with column prefix: {:?}", result.err());
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::CreateTable(create) => {
            assert_eq!(create.table_constraints.len(), 1);
            match &create.table_constraints[0] {
                vibesql_ast::TableConstraint {
                    kind: vibesql_ast::TableConstraintKind::PrimaryKey { columns, .. },
                    ..
                } => {
                    assert_eq!(columns.len(), 1);
                    assert_eq!(columns[0].expect_column_name(), "name");
                    assert_eq!(columns[0].prefix_length(), Some(50));
                }
                _ => panic!("Expected PRIMARY KEY constraint"),
            }
        }
        _ => panic!("Expected CREATE TABLE statement"),
    }
}

#[test]
fn test_parse_create_index_with_column_prefix() {
    let result = Parser::parse_sql("CREATE INDEX idx1 ON users (email(50))");
    assert!(result.is_ok(), "Should parse CREATE INDEX with column prefix: {:?}", result.err());
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::CreateIndex(create_idx) => {
            assert_eq!(create_idx.index_name, "idx1");
            assert_eq!(create_idx.table_name, "users");
            assert_eq!(create_idx.columns.len(), 1);
            assert_eq!(create_idx.columns[0].expect_column_name(), "email");
            assert_eq!(create_idx.columns[0].prefix_length(), Some(50));
        }
        _ => panic!("Expected CREATE INDEX statement"),
    }
}

// ========================================================================
// Prefix Length Validation Tests
// ========================================================================

#[test]
fn test_parse_unique_constraint_prefix_zero_fails() {
    let result = Parser::parse_sql("CREATE TABLE t1(a TEXT, UNIQUE (a(0)))");
    assert!(result.is_err(), "Should reject prefix length of 0");
    let err = result.unwrap_err();
    assert!(
        err.message.contains("Prefix length must be at least 1"),
        "Expected minimum validation error, got: {}",
        err.message
    );
}

#[test]
fn test_parse_unique_constraint_prefix_too_large_fails() {
    let result = Parser::parse_sql("CREATE TABLE t1(a TEXT, UNIQUE (a(10001)))");
    assert!(result.is_err(), "Should reject prefix length > 10000");
    let err = result.unwrap_err();
    assert!(
        err.message.contains("Prefix length must not exceed 10000"),
        "Expected maximum validation error, got: {}",
        err.message
    );
}

#[test]
fn test_parse_primary_key_prefix_zero_fails() {
    let result = Parser::parse_sql("CREATE TABLE t1(a TEXT, PRIMARY KEY (a(0)))");
    assert!(result.is_err(), "Should reject prefix length of 0");
    let err = result.unwrap_err();
    assert!(
        err.message.contains("Prefix length must be at least 1"),
        "Expected minimum validation error, got: {}",
        err.message
    );
}

#[test]
fn test_parse_primary_key_prefix_too_large_fails() {
    let result = Parser::parse_sql("CREATE TABLE t1(a TEXT, PRIMARY KEY (a(99999999)))");
    assert!(result.is_err(), "Should reject prefix length > 10000");
    let err = result.unwrap_err();
    assert!(
        err.message.contains("Prefix length must not exceed 10000"),
        "Expected maximum validation error, got: {}",
        err.message
    );
}

#[test]
fn test_parse_create_index_prefix_zero_fails() {
    let result = Parser::parse_sql("CREATE INDEX idx1 ON users (email(0))");
    assert!(result.is_err(), "Should reject prefix length of 0");
    let err = result.unwrap_err();
    assert!(
        err.message.contains("length cannot be 0"),
        "Expected zero length validation error, got: {}",
        err.message
    );
}

#[test]
fn test_parse_create_index_prefix_too_large_fails() {
    let result = Parser::parse_sql("CREATE INDEX idx1 ON users (email(10001))");
    assert!(result.is_err(), "Should reject prefix length > 10000");
    let err = result.unwrap_err();
    assert!(
        err.message.contains("too long") || err.message.contains("exceed"),
        "Expected max length validation error, got: {}",
        err.message
    );
}

#[test]
fn test_parse_unique_constraint_prefix_boundary_values() {
    // Test minimum valid value: 1
    let result = Parser::parse_sql("CREATE TABLE t1(a TEXT, UNIQUE (a(1)))");
    assert!(result.is_ok(), "Should accept prefix length of 1: {:?}", result.err());

    // Test maximum valid value: 10000
    let result = Parser::parse_sql("CREATE TABLE t2(a TEXT, UNIQUE (a(10000)))");
    assert!(result.is_ok(), "Should accept prefix length of 10000: {:?}", result.err());
}

/// Test ON CONFLICT clause for table-level constraints (SQLite extension)
#[test]
fn test_parse_on_conflict_table_constraints() {
    // Test table-level PRIMARY KEY with ON CONFLICT IGNORE
    let result =
        Parser::parse_sql("CREATE TABLE t1(a INT, b INT, PRIMARY KEY (a, b) ON CONFLICT IGNORE);");
    assert!(result.is_ok(), "Should parse table-level PRIMARY KEY with ON CONFLICT");
    let stmt = result.unwrap();
    match stmt {
        vibesql_ast::Statement::CreateTable(create) => {
            assert_eq!(create.table_constraints.len(), 1);
            match &create.table_constraints[0].kind {
                vibesql_ast::TableConstraintKind::PrimaryKey { columns, on_conflict } => {
                    assert_eq!(columns.len(), 2);
                    assert_eq!(*on_conflict, Some(vibesql_ast::ConflictClause::Ignore));
                }
                _ => panic!("Expected PrimaryKey constraint"),
            }
        }
        _ => panic!("Expected CREATE TABLE statement"),
    }

    // Test table-level UNIQUE with ON CONFLICT REPLACE
    let result = Parser::parse_sql(
        "CREATE TABLE t2(email TEXT, code TEXT, UNIQUE (email) ON CONFLICT REPLACE);",
    );
    assert!(result.is_ok(), "Should parse table-level UNIQUE with ON CONFLICT");
    let stmt = result.unwrap();
    match stmt {
        vibesql_ast::Statement::CreateTable(create) => {
            assert_eq!(create.table_constraints.len(), 1);
            match &create.table_constraints[0].kind {
                vibesql_ast::TableConstraintKind::Unique { columns, on_conflict } => {
                    assert_eq!(columns.len(), 1);
                    assert_eq!(*on_conflict, Some(vibesql_ast::ConflictClause::Replace));
                }
                _ => panic!("Expected Unique constraint"),
            }
        }
        _ => panic!("Expected CREATE TABLE statement"),
    }

    // Test table-level UNIQUE with ON CONFLICT ABORT
    let result =
        Parser::parse_sql("CREATE TABLE t3(a INT, b INT, UNIQUE (a, b) ON CONFLICT ABORT);");
    assert!(result.is_ok(), "Should parse table-level UNIQUE with ON CONFLICT ABORT");
    let stmt = result.unwrap();
    match stmt {
        vibesql_ast::Statement::CreateTable(create) => match &create.table_constraints[0].kind {
            vibesql_ast::TableConstraintKind::Unique { on_conflict, .. } => {
                assert_eq!(*on_conflict, Some(vibesql_ast::ConflictClause::Abort));
            }
            _ => panic!("Expected Unique constraint"),
        },
        _ => panic!("Expected CREATE TABLE statement"),
    }

    // Test table-level PRIMARY KEY with ON CONFLICT FAIL
    let result = Parser::parse_sql("CREATE TABLE t4(id INT, PRIMARY KEY (id) ON CONFLICT FAIL);");
    assert!(result.is_ok(), "Should parse table-level PRIMARY KEY with ON CONFLICT FAIL");
    let stmt = result.unwrap();
    match stmt {
        vibesql_ast::Statement::CreateTable(create) => match &create.table_constraints[0].kind {
            vibesql_ast::TableConstraintKind::PrimaryKey { on_conflict, .. } => {
                assert_eq!(*on_conflict, Some(vibesql_ast::ConflictClause::Fail));
            }
            _ => panic!("Expected PrimaryKey constraint"),
        },
        _ => panic!("Expected CREATE TABLE statement"),
    }
}

// ========================================================================
// DEFERRABLE Constraint Tests (Issue #4990) - Table-level FOREIGN KEY
// ========================================================================

/// Test DEFERRABLE constraint on table-level FOREIGN KEY
#[test]
fn test_parse_foreign_key_deferrable() {
    let result =
        Parser::parse_sql("CREATE TABLE t(a INTEGER, FOREIGN KEY(a) REFERENCES p(x) DEFERRABLE)");
    assert!(result.is_ok(), "Should parse FOREIGN KEY with DEFERRABLE: {:?}", result.err());
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::CreateTable(create) => {
            assert_eq!(create.table_constraints.len(), 1);
            match &create.table_constraints[0].kind {
                vibesql_ast::TableConstraintKind::ForeignKey {
                    columns,
                    references_table,
                    references_columns,
                    deferral,
                    ..
                } => {
                    assert_eq!(columns, &["a".to_string()]);
                    assert_eq!(references_table, "p");
                    assert_eq!(references_columns, &["x".to_string()]);
                    let deferral = deferral.expect("Should have deferral");
                    assert!(deferral.is_deferrable, "Should be deferrable");
                    assert!(!deferral.initially_deferred, "Should default to INITIALLY IMMEDIATE");
                }
                _ => panic!("Expected FOREIGN KEY constraint"),
            }
        }
        _ => panic!("Expected CREATE TABLE statement"),
    }
}

/// Test NOT DEFERRABLE constraint on table-level FOREIGN KEY
#[test]
fn test_parse_foreign_key_not_deferrable() {
    let result = Parser::parse_sql(
        "CREATE TABLE t(a INTEGER, FOREIGN KEY(a) REFERENCES p(x) NOT DEFERRABLE)",
    );
    assert!(result.is_ok(), "Should parse FOREIGN KEY with NOT DEFERRABLE: {:?}", result.err());
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::CreateTable(create) => match &create.table_constraints[0].kind {
            vibesql_ast::TableConstraintKind::ForeignKey { deferral, .. } => {
                let deferral = deferral.expect("Should have deferral");
                assert!(!deferral.is_deferrable, "Should NOT be deferrable");
            }
            _ => panic!("Expected FOREIGN KEY constraint"),
        },
        _ => panic!("Expected CREATE TABLE statement"),
    }
}

/// Test DEFERRABLE INITIALLY DEFERRED on table-level FOREIGN KEY
#[test]
fn test_parse_foreign_key_deferrable_initially_deferred() {
    let result = Parser::parse_sql(
        "CREATE TABLE t(a INTEGER, FOREIGN KEY(a) REFERENCES p(x) DEFERRABLE INITIALLY DEFERRED)",
    );
    assert!(result.is_ok(), "Should parse DEFERRABLE INITIALLY DEFERRED: {:?}", result.err());
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::CreateTable(create) => match &create.table_constraints[0].kind {
            vibesql_ast::TableConstraintKind::ForeignKey { deferral, .. } => {
                let deferral = deferral.expect("Should have deferral");
                assert!(deferral.is_deferrable, "Should be deferrable");
                assert!(deferral.initially_deferred, "Should be INITIALLY DEFERRED");
            }
            _ => panic!("Expected FOREIGN KEY constraint"),
        },
        _ => panic!("Expected CREATE TABLE statement"),
    }
}

/// Test DEFERRABLE INITIALLY IMMEDIATE on table-level FOREIGN KEY
#[test]
fn test_parse_foreign_key_deferrable_initially_immediate() {
    let result = Parser::parse_sql(
        "CREATE TABLE t(a INTEGER, FOREIGN KEY(a) REFERENCES p(x) DEFERRABLE INITIALLY IMMEDIATE)",
    );
    assert!(result.is_ok(), "Should parse DEFERRABLE INITIALLY IMMEDIATE: {:?}", result.err());
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::CreateTable(create) => match &create.table_constraints[0].kind {
            vibesql_ast::TableConstraintKind::ForeignKey { deferral, .. } => {
                let deferral = deferral.expect("Should have deferral");
                assert!(deferral.is_deferrable, "Should be deferrable");
                assert!(!deferral.initially_deferred, "Should be INITIALLY IMMEDIATE");
            }
            _ => panic!("Expected FOREIGN KEY constraint"),
        },
        _ => panic!("Expected CREATE TABLE statement"),
    }
}

/// Test DEFERRABLE with ON DELETE/UPDATE actions on table-level FOREIGN KEY
#[test]
fn test_parse_foreign_key_deferrable_with_actions() {
    let result = Parser::parse_sql(
        "CREATE TABLE t(a INTEGER, FOREIGN KEY(a) REFERENCES p(x) ON DELETE CASCADE ON UPDATE SET NULL DEFERRABLE INITIALLY DEFERRED)"
    );
    assert!(
        result.is_ok(),
        "Should parse with ON DELETE/UPDATE and DEFERRABLE: {:?}",
        result.err()
    );
    let stmt = result.unwrap();

    match stmt {
        vibesql_ast::Statement::CreateTable(create) => match &create.table_constraints[0].kind {
            vibesql_ast::TableConstraintKind::ForeignKey {
                on_delete, on_update, deferral, ..
            } => {
                assert_eq!(on_delete, &Some(vibesql_ast::ReferentialAction::Cascade));
                assert_eq!(on_update, &Some(vibesql_ast::ReferentialAction::SetNull));
                let deferral = deferral.expect("Should have deferral");
                assert!(deferral.is_deferrable);
                assert!(deferral.initially_deferred);
            }
            _ => panic!("Expected FOREIGN KEY constraint"),
        },
        _ => panic!("Expected CREATE TABLE statement"),
    }
}