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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
//! Tests for INDEX DDL statements (CREATE INDEX, DROP INDEX)

use vibesql_ast::Statement;

use crate::Parser;

#[test]
fn test_create_index_simple() {
    let sql = "CREATE INDEX idx ON users(email)";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok(), "Failed to parse: {:?}", result.err());

    match result.unwrap() {
        Statement::CreateIndex(stmt) => {
            assert_eq!(stmt.index_name, "idx");
            assert_eq!(stmt.table_name, "users");
            match &stmt.index_type {
                vibesql_ast::IndexType::BTree { unique } => assert!(!unique),
                other => panic!("Expected BTree index, got: {:?}", other),
            }
            assert_eq!(stmt.columns.len(), 1);
            assert_eq!(stmt.columns[0].expect_column_name(), "email");
            assert_eq!(stmt.columns[0].direction(), vibesql_ast::OrderDirection::Asc);
        }
        other => panic!("Expected CreateIndex, got: {:?}", other),
    }
}

#[test]
fn test_create_unique_index() {
    let sql = "CREATE UNIQUE INDEX idx ON users(email)";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok());

    match result.unwrap() {
        Statement::CreateIndex(stmt) => {
            match &stmt.index_type {
                vibesql_ast::IndexType::BTree { unique } => {
                    assert!(*unique, "Expected unique=true")
                }
                other => panic!("Expected BTree index, got: {:?}", other),
            }
            assert_eq!(stmt.index_name, "idx");
            assert_eq!(stmt.table_name, "users");
            assert_eq!(stmt.columns.len(), 1);
            assert_eq!(stmt.columns[0].expect_column_name(), "email");
            assert_eq!(stmt.columns[0].direction(), vibesql_ast::OrderDirection::Asc);
        }
        other => panic!("Expected CreateIndex, got: {:?}", other),
    }
}

#[test]
fn test_create_index_multi_column() {
    let sql = "CREATE INDEX idx ON users(first_name, last_name, email)";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok());

    match result.unwrap() {
        Statement::CreateIndex(stmt) => {
            assert_eq!(stmt.index_name, "idx");
            assert_eq!(stmt.table_name, "users");
            match &stmt.index_type {
                vibesql_ast::IndexType::BTree { unique } => assert!(!unique),
                other => panic!("Expected BTree index, got: {:?}", other),
            }
            assert_eq!(stmt.columns.len(), 3);
            assert_eq!(stmt.columns[0].expect_column_name(), "first_name");
            assert_eq!(stmt.columns[0].direction(), vibesql_ast::OrderDirection::Asc);
            assert_eq!(stmt.columns[1].expect_column_name(), "last_name");
            assert_eq!(stmt.columns[1].direction(), vibesql_ast::OrderDirection::Asc);
            assert_eq!(stmt.columns[2].expect_column_name(), "email");
            assert_eq!(stmt.columns[2].direction(), vibesql_ast::OrderDirection::Asc);
        }
        other => panic!("Expected CreateIndex, got: {:?}", other),
    }
}

#[test]
fn test_create_unique_index_multi_column() {
    let sql = "CREATE UNIQUE INDEX idx ON orders(customer_id, order_date)";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok());

    match result.unwrap() {
        Statement::CreateIndex(stmt) => {
            match &stmt.index_type {
                vibesql_ast::IndexType::BTree { unique } => assert!(*unique),
                other => panic!("Expected BTree index, got: {:?}", other),
            }
            assert_eq!(stmt.index_name, "idx");
            assert_eq!(stmt.table_name, "orders");
            assert_eq!(stmt.columns.len(), 2);
            assert_eq!(stmt.columns[0].expect_column_name(), "customer_id");
            assert_eq!(stmt.columns[0].direction(), vibesql_ast::OrderDirection::Asc);
            assert_eq!(stmt.columns[1].expect_column_name(), "order_date");
            assert_eq!(stmt.columns[1].direction(), vibesql_ast::OrderDirection::Asc);
        }
        other => panic!("Expected CreateIndex, got: {:?}", other),
    }
}

#[test]
fn test_create_index_mixed_case_identifiers() {
    let sql = "CREATE INDEX MyIndex ON MyTable(MyColumn)";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok());

    match result.unwrap() {
        Statement::CreateIndex(stmt) => {
            assert_eq!(stmt.index_name, "MyIndex");
            assert_eq!(stmt.table_name, "MyTable");
            assert_eq!(stmt.columns.len(), 1);
            assert_eq!(stmt.columns[0].expect_column_name(), "MyColumn");
            assert_eq!(stmt.columns[0].direction(), vibesql_ast::OrderDirection::Asc);
        }
        other => panic!("Expected CreateIndex, got: {:?}", other),
    }
}

#[test]
fn test_create_index_single_column() {
    let sql = "CREATE INDEX pk ON users(id)";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok());

    match result.unwrap() {
        Statement::CreateIndex(stmt) => {
            assert_eq!(stmt.index_name, "pk");
            assert_eq!(stmt.table_name, "users");
            assert_eq!(stmt.columns.len(), 1);
            assert_eq!(stmt.columns[0].expect_column_name(), "id");
            assert_eq!(stmt.columns[0].direction(), vibesql_ast::OrderDirection::Asc);
        }
        other => panic!("Expected CreateIndex, got: {:?}", other),
    }
}

#[test]
fn test_create_index_with_desc() {
    let sql = "CREATE INDEX idx ON users(email DESC, created_at ASC)";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok());

    match result.unwrap() {
        Statement::CreateIndex(stmt) => {
            assert_eq!(stmt.index_name, "idx");
            assert_eq!(stmt.table_name, "users");
            match &stmt.index_type {
                vibesql_ast::IndexType::BTree { unique } => assert!(!unique),
                other => panic!("Expected BTree index, got: {:?}", other),
            }
            assert_eq!(stmt.columns.len(), 2);
            assert_eq!(stmt.columns[0].expect_column_name(), "email");
            assert_eq!(stmt.columns[0].direction(), vibesql_ast::OrderDirection::Desc);
            assert_eq!(stmt.columns[1].expect_column_name(), "created_at");
            assert_eq!(stmt.columns[1].direction(), vibesql_ast::OrderDirection::Asc);
        }
        other => panic!("Expected CreateIndex, got: {:?}", other),
    }
}

#[test]
fn test_drop_index_simple() {
    let sql = "DROP INDEX idx";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok());

    match result.unwrap() {
        Statement::DropIndex(stmt) => {
            assert_eq!(stmt.index_name, "idx");
        }
        other => panic!("Expected DropIndex, got: {:?}", other),
    }
}

#[test]
fn test_drop_index_mixed_case() {
    let sql = "DROP INDEX MyIndex";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok());

    match result.unwrap() {
        Statement::DropIndex(stmt) => {
            assert_eq!(stmt.index_name, "MyIndex");
        }
        other => panic!("Expected DropIndex, got: {:?}", other),
    }
}

// Error cases

#[test]
fn test_create_index_missing_index_name() {
    let sql = "CREATE INDEX ON table(col)";
    assert!(Parser::parse_sql(sql).is_err());
}

#[test]
fn test_create_index_missing_table_name() {
    let sql = "CREATE INDEX idx (col)";
    assert!(Parser::parse_sql(sql).is_err());
}

#[test]
fn test_create_index_empty_column_list() {
    let sql = "CREATE INDEX idx ON table()";
    assert!(Parser::parse_sql(sql).is_err());
}

#[test]
fn test_drop_index_missing_index_name() {
    let sql = "DROP INDEX";
    assert!(Parser::parse_sql(sql).is_err());
}

#[test]
fn test_create_spatial_index() {
    let sql = "CREATE SPATIAL INDEX idx_location ON places(geom)";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok(), "Failed to parse: {:?}", result.err());

    match result.unwrap() {
        Statement::CreateIndex(stmt) => {
            assert_eq!(stmt.index_name, "idx_location");
            assert_eq!(stmt.table_name, "places");
            assert!(
                matches!(stmt.index_type, vibesql_ast::IndexType::Spatial),
                "Expected Spatial index, got: {:?}",
                stmt.index_type
            );
            assert_eq!(stmt.columns.len(), 1);
            assert_eq!(stmt.columns[0].expect_column_name(), "geom");
        }
        other => panic!("Expected CreateIndex, got: {:?}", other),
    }
}

#[test]
fn test_create_spatial_index_if_not_exists() {
    let sql = "CREATE SPATIAL INDEX IF NOT EXISTS idx_boundary ON parcels(boundary)";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok());

    match result.unwrap() {
        Statement::CreateIndex(stmt) => {
            assert!(stmt.if_not_exists);
            assert_eq!(stmt.index_name, "idx_boundary");
            assert_eq!(stmt.table_name, "parcels");
            assert!(matches!(stmt.index_type, vibesql_ast::IndexType::Spatial));
            assert_eq!(stmt.columns.len(), 1);
            assert_eq!(stmt.columns[0].expect_column_name(), "boundary");
        }
        other => panic!("Expected CreateIndex, got: {:?}", other),
    }
}

// ============================================================================
// Expression/Functional Index Tests
// ============================================================================

#[test]
fn test_create_index_expression_function_call() {
    // SQLite/PostgreSQL style functional index
    let sql = "CREATE INDEX idx_lower_name ON users((lower(name)))";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok(), "Failed to parse: {:?}", result.err());

    match result.unwrap() {
        Statement::CreateIndex(stmt) => {
            assert_eq!(stmt.index_name, "idx_lower_name");
            assert_eq!(stmt.table_name, "users");
            assert_eq!(stmt.columns.len(), 1);
            assert!(stmt.columns[0].is_expression());
            assert!(stmt.columns[0].get_expression().is_some());
            assert_eq!(stmt.columns[0].direction(), vibesql_ast::OrderDirection::Asc);
        }
        other => panic!("Expected CreateIndex, got: {:?}", other),
    }
}

#[test]
fn test_create_index_expression_arithmetic() {
    // Arithmetic expression index
    let sql = "CREATE INDEX idx_sum ON numbers((a + b))";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok(), "Failed to parse: {:?}", result.err());

    match result.unwrap() {
        Statement::CreateIndex(stmt) => {
            assert_eq!(stmt.index_name, "idx_sum");
            assert_eq!(stmt.table_name, "numbers");
            assert_eq!(stmt.columns.len(), 1);
            assert!(stmt.columns[0].is_expression());
        }
        other => panic!("Expected CreateIndex, got: {:?}", other),
    }
}

#[test]
fn test_create_index_expression_with_desc() {
    // Expression index with DESC
    let sql = "CREATE INDEX idx_lower_name ON users((lower(name)) DESC)";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok(), "Failed to parse: {:?}", result.err());

    match result.unwrap() {
        Statement::CreateIndex(stmt) => {
            assert_eq!(stmt.columns.len(), 1);
            assert!(stmt.columns[0].is_expression());
            assert_eq!(stmt.columns[0].direction(), vibesql_ast::OrderDirection::Desc);
        }
        other => panic!("Expected CreateIndex, got: {:?}", other),
    }
}

#[test]
fn test_create_index_mixed_columns_and_expressions() {
    // Mix of regular columns and expression indexes
    let sql = "CREATE INDEX idx_mixed ON t(a, (lower(b)), c DESC)";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok(), "Failed to parse: {:?}", result.err());

    match result.unwrap() {
        Statement::CreateIndex(stmt) => {
            assert_eq!(stmt.columns.len(), 3);

            // First: column 'a'
            assert!(!stmt.columns[0].is_expression());
            assert_eq!(stmt.columns[0].expect_column_name(), "a");

            // Second: expression lower(b)
            assert!(stmt.columns[1].is_expression());
            assert!(stmt.columns[1].column_name().is_none());

            // Third: column 'c' DESC
            assert!(!stmt.columns[2].is_expression());
            assert_eq!(stmt.columns[2].expect_column_name(), "c");
            assert_eq!(stmt.columns[2].direction(), vibesql_ast::OrderDirection::Desc);
        }
        other => panic!("Expected CreateIndex, got: {:?}", other),
    }
}

#[test]
fn test_create_unique_index_expression() {
    // Unique expression index
    let sql = "CREATE UNIQUE INDEX idx_unique_lower ON users((lower(email)))";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok(), "Failed to parse: {:?}", result.err());

    match result.unwrap() {
        Statement::CreateIndex(stmt) => {
            match &stmt.index_type {
                vibesql_ast::IndexType::BTree { unique } => {
                    assert!(*unique, "Expected unique=true");
                }
                other => panic!("Expected BTree index, got: {:?}", other),
            }
            assert!(stmt.columns[0].is_expression());
        }
        other => panic!("Expected CreateIndex, got: {:?}", other),
    }
}

#[test]
fn test_create_index_expression_coalesce() {
    // Expression with COALESCE
    let sql = "CREATE INDEX idx_coalesce ON t((coalesce(a, b, 0)))";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok(), "Failed to parse: {:?}", result.err());

    match result.unwrap() {
        Statement::CreateIndex(stmt) => {
            assert_eq!(stmt.columns.len(), 1);
            assert!(stmt.columns[0].is_expression());
        }
        other => panic!("Expected CreateIndex, got: {:?}", other),
    }
}

#[test]
fn test_create_index_expression_complex() {
    // Complex expression
    let sql = "CREATE INDEX idx_complex ON t((a * 2 + b - 1))";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok(), "Failed to parse: {:?}", result.err());

    match result.unwrap() {
        Statement::CreateIndex(stmt) => {
            assert_eq!(stmt.columns.len(), 1);
            assert!(stmt.columns[0].is_expression());
        }
        other => panic!("Expected CreateIndex, got: {:?}", other),
    }
}

// ============================================================================
// Expression Indexes Without Outer Parentheses (Issue #4715)
// ============================================================================
// SQLite allows expression indexes without the outer parentheses, e.g.:
// CREATE INDEX idx ON t(abs(b)) -- instead of ((abs(b)))
// This should NOT be confused with prefix length syntax: name(10)

#[test]
fn test_create_index_expression_function_no_parens() {
    // SQLite-style: abs(b) without extra outer parens
    // This was previously misinterpreted as column "abs" with prefix length "b"
    let sql = "CREATE INDEX t1b ON t1(abs(b))";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok(), "Failed to parse: {:?}", result.err());

    match result.unwrap() {
        Statement::CreateIndex(stmt) => {
            assert_eq!(stmt.index_name, "t1b");
            assert_eq!(stmt.table_name, "t1");
            assert_eq!(stmt.columns.len(), 1);
            assert!(stmt.columns[0].is_expression(), "Expected expression index, got column");
            assert_eq!(stmt.columns[0].direction(), vibesql_ast::OrderDirection::Asc);
        }
        other => panic!("Expected CreateIndex, got: {:?}", other),
    }
}

#[test]
fn test_create_unique_index_expression_no_parens() {
    // Test case from issue #4715
    let sql = "CREATE UNIQUE INDEX t1b ON t1(abs(b))";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok(), "Failed to parse: {:?}", result.err());

    match result.unwrap() {
        Statement::CreateIndex(stmt) => {
            match &stmt.index_type {
                vibesql_ast::IndexType::BTree { unique } => {
                    assert!(*unique, "Expected unique=true");
                }
                other => panic!("Expected BTree index, got: {:?}", other),
            }
            assert!(stmt.columns[0].is_expression());
        }
        other => panic!("Expected CreateIndex, got: {:?}", other),
    }
}

#[test]
fn test_create_index_expression_lower_no_parens() {
    // Common case: lower() function for case-insensitive index
    let sql = "CREATE INDEX idx_lower ON users(lower(email))";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok(), "Failed to parse: {:?}", result.err());

    match result.unwrap() {
        Statement::CreateIndex(stmt) => {
            assert_eq!(stmt.columns.len(), 1);
            assert!(stmt.columns[0].is_expression());
        }
        other => panic!("Expected CreateIndex, got: {:?}", other),
    }
}

#[test]
fn test_create_index_expression_with_desc_no_parens() {
    // Expression index with DESC, no outer parens
    let sql = "CREATE INDEX idx ON t(abs(x) DESC)";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok(), "Failed to parse: {:?}", result.err());

    match result.unwrap() {
        Statement::CreateIndex(stmt) => {
            assert_eq!(stmt.columns.len(), 1);
            assert!(stmt.columns[0].is_expression());
            assert_eq!(stmt.columns[0].direction(), vibesql_ast::OrderDirection::Desc);
        }
        other => panic!("Expected CreateIndex, got: {:?}", other),
    }
}

#[test]
fn test_create_index_mixed_expression_no_parens_and_columns() {
    // Mix of expression (no outer parens) and regular columns
    let sql = "CREATE INDEX idx ON t(a, lower(b), c DESC)";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok(), "Failed to parse: {:?}", result.err());

    match result.unwrap() {
        Statement::CreateIndex(stmt) => {
            assert_eq!(stmt.columns.len(), 3);

            // First: column 'a'
            assert!(!stmt.columns[0].is_expression());
            assert_eq!(stmt.columns[0].expect_column_name(), "a");

            // Second: expression lower(b)
            assert!(stmt.columns[1].is_expression());

            // Third: column 'c' DESC
            assert!(!stmt.columns[2].is_expression());
            assert_eq!(stmt.columns[2].expect_column_name(), "c");
            assert_eq!(stmt.columns[2].direction(), vibesql_ast::OrderDirection::Desc);
        }
        other => panic!("Expected CreateIndex, got: {:?}", other),
    }
}

#[test]
fn test_create_index_prefix_length_still_works() {
    // Ensure prefix length syntax still works: column_name(integer)
    let sql = "CREATE INDEX idx ON users(name(10))";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok(), "Failed to parse: {:?}", result.err());

    match result.unwrap() {
        Statement::CreateIndex(stmt) => {
            assert_eq!(stmt.columns.len(), 1);
            assert!(!stmt.columns[0].is_expression());
            assert_eq!(stmt.columns[0].expect_column_name(), "name");
            assert_eq!(stmt.columns[0].prefix_length(), Some(10));
        }
        other => panic!("Expected CreateIndex, got: {:?}", other),
    }
}

#[test]
fn test_create_index_multi_arg_function_no_parens() {
    // Multi-argument function
    let sql = "CREATE INDEX idx ON t(substr(name, 1, 3))";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok(), "Failed to parse: {:?}", result.err());

    match result.unwrap() {
        Statement::CreateIndex(stmt) => {
            assert_eq!(stmt.columns.len(), 1);
            assert!(stmt.columns[0].is_expression());
        }
        other => panic!("Expected CreateIndex, got: {:?}", other),
    }
}

#[test]
fn test_create_index_nested_function_no_parens() {
    // Nested function calls
    let sql = "CREATE INDEX idx ON t(lower(trim(name)))";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok(), "Failed to parse: {:?}", result.err());

    match result.unwrap() {
        Statement::CreateIndex(stmt) => {
            assert_eq!(stmt.columns.len(), 1);
            assert!(stmt.columns[0].is_expression());
        }
        other => panic!("Expected CreateIndex, got: {:?}", other),
    }
}

// Partial-index WHERE clause parsing (issue #5091).
//
// SQLite supports `CREATE INDEX ... WHERE <predicate>` for partial indexes.
// The parser captures the predicate expression so downstream validation can
// reject misuses (e.g. window functions in the predicate).

#[test]
fn test_create_index_with_where_clause() {
    let sql = "CREATE INDEX idx ON t(a) WHERE b > 5";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok(), "Failed to parse: {:?}", result.err());

    match result.unwrap() {
        Statement::CreateIndex(stmt) => {
            assert_eq!(stmt.index_name, "idx");
            assert_eq!(stmt.columns.len(), 1);
            assert!(stmt.where_clause.is_some(), "expected WHERE clause to be parsed");
        }
        other => panic!("Expected CreateIndex, got: {:?}", other),
    }
}

#[test]
fn test_create_index_without_where_clause_keeps_none() {
    let sql = "CREATE INDEX idx ON t(a)";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok(), "Failed to parse: {:?}", result.err());

    match result.unwrap() {
        Statement::CreateIndex(stmt) => {
            assert!(stmt.where_clause.is_none(), "WHERE clause should default to None");
        }
        other => panic!("Expected CreateIndex, got: {:?}", other),
    }
}

#[test]
fn test_create_unique_index_with_where_clause() {
    // SQLite allows partial UNIQUE indexes.
    let sql = "CREATE UNIQUE INDEX idx ON t(a) WHERE b IS NOT NULL";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok(), "Failed to parse: {:?}", result.err());

    match result.unwrap() {
        Statement::CreateIndex(stmt) => {
            match &stmt.index_type {
                vibesql_ast::IndexType::BTree { unique } => assert!(*unique),
                other => panic!("Expected BTree(unique=true), got: {:?}", other),
            }
            assert!(stmt.where_clause.is_some(), "expected WHERE clause to be parsed");
        }
        other => panic!("Expected CreateIndex, got: {:?}", other),
    }
}

#[test]
fn test_create_index_with_window_function_in_where_rejected_at_validation() {
    // The parser itself accepts the WHERE expression — semantic rejection of
    // window functions happens in the executor's validation layer (see issue
    // #5091, test window1-11.1). Verify the AST captures the call so that
    // `find_window_function_in_expression` has something to walk.
    let sql = "CREATE INDEX idx ON t(a) WHERE sum(b) OVER ()";
    let result = Parser::parse_sql(sql);
    assert!(result.is_ok(), "Failed to parse: {:?}", result.err());

    match result.unwrap() {
        Statement::CreateIndex(stmt) => {
            assert!(stmt.where_clause.is_some(), "expected WHERE clause to be parsed");
        }
        other => panic!("Expected CreateIndex, got: {:?}", other),
    }
}