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
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
//! Tests for ALTER TABLE + columnar cache invalidation
//!
//! This module tests that ALTER TABLE operations correctly invalidate the columnar cache,
//! ensuring that subsequent reads via `Database::get_columnar()` return the updated data
//! rather than stale cached data with outdated schema or values.
//!
//! Related: #3933

use vibesql_ast::{
    AddColumnStmt, AlterColumnStmt, AlterTableStmt, ChangeColumnStmt, ColumnDef, DropColumnStmt,
    ModifyColumnStmt,
};
use vibesql_catalog::{ColumnSchema, TableSchema};
use vibesql_executor::{AlterTableExecutor, InsertExecutor};
use vibesql_storage::Database;
use vibesql_types::{DataType, SqlValue};

/// Sets up a simple test table for ALTER TABLE testing
fn setup_test_table(db: &mut Database) {
    let schema = TableSchema::with_primary_key(
        "test_table".to_string(),
        vec![
            ColumnSchema::new("id".to_string(), DataType::Integer, false),
            ColumnSchema::new("name".to_string(), DataType::Varchar { max_length: Some(50) }, true),
            ColumnSchema::new("value".to_string(), DataType::Integer, true),
        ],
        vec!["id".to_string()],
    );
    db.create_table(schema).unwrap();
}

/// Helper to insert a row into test_table
fn insert_row(db: &mut Database, id: i64, name: &str, value: i64) {
    let stmt = vibesql_ast::InsertStmt {
        with_clause: None,
        schema_name: None,
        schema_quoted: false,
        table_quoted: false,
        table_name: "test_table".to_string(),
        columns: vec![],
        source: vibesql_ast::InsertSource::Values(vec![vec![
            vibesql_ast::Expression::Literal(SqlValue::Integer(id)),
            vibesql_ast::Expression::Literal(SqlValue::Varchar(arcstr::ArcStr::from(name))),
            vibesql_ast::Expression::Literal(SqlValue::Integer(value)),
        ]]),
        conflict_clause: None,
        on_conflict: None,
        on_duplicate_key_update: None,
        returning: None,
    };
    InsertExecutor::execute(db, &stmt).unwrap();
}

/// Test that ADD COLUMN invalidates the columnar cache
///
/// This verifies that after adding a column:
/// 1. The columnar cache is invalidated
/// 2. Subsequent reads return data with the new column
/// 3. The new column contains the default value
#[test]
fn test_add_column_invalidates_columnar_cache() {
    let mut db = Database::new();
    setup_test_table(&mut db);

    // Insert initial data
    insert_row(&mut db, 1, "Widget", 100);
    insert_row(&mut db, 2, "Gadget", 200);

    // Warm the columnar cache
    let initial_columnar = db.get_columnar("test_table").unwrap().expect("Table should exist");
    assert_eq!(initial_columnar.row_count(), 2);
    assert_eq!(initial_columnar.column_count(), 3, "Should have 3 columns initially");

    // Verify initial cache statistics
    let initial_stats = db.columnar_cache_stats();
    assert_eq!(initial_stats.conversions, 1, "Should have done one conversion");

    // ADD COLUMN
    let add_col_stmt = AlterTableStmt::AddColumn(AddColumnStmt {
        table_name: "test_table".to_string(),
        column_def: ColumnDef {
            name: "new_col".to_string(),
            data_type: DataType::Integer,
            nullable: true,
            default_value: Some(Box::new(vibesql_ast::Expression::Literal(SqlValue::Integer(42)))),
            constraints: vec![],
            comment: None,
            generated_expr: None,
            is_exact_integer_type: false,
        },
    });
    AlterTableExecutor::execute(&add_col_stmt, &mut db).unwrap();

    // Get columnar data again - should reflect the new column
    let updated_columnar = db.get_columnar("test_table").unwrap().expect("Table should exist");

    // Verify the new column is present
    assert_eq!(updated_columnar.column_count(), 4, "Should have 4 columns after ADD COLUMN");
    assert_eq!(updated_columnar.row_count(), 2, "Should still have 2 rows");

    // Verify the new column contains the default value
    let new_col = updated_columnar.get_column("new_col").expect("New column should exist");
    for i in 0..2 {
        match new_col.get(i) {
            SqlValue::Integer(v) => assert_eq!(v, 42, "Default value should be 42"),
            other => panic!("Expected Integer, got {:?}", other),
        }
    }

    // Verify cache was invalidated and re-converted
    let final_stats = db.columnar_cache_stats();
    assert!(
        final_stats.conversions >= 2,
        "Should have re-converted after ADD COLUMN (conversions: {})",
        final_stats.conversions
    );
}

/// Test that DROP COLUMN invalidates the columnar cache
///
/// This verifies that after dropping a column:
/// 1. The columnar cache is invalidated
/// 2. Subsequent reads return data without the dropped column
#[test]
fn test_drop_column_invalidates_columnar_cache() {
    let mut db = Database::new();
    setup_test_table(&mut db);

    // Insert initial data
    insert_row(&mut db, 1, "Widget", 100);
    insert_row(&mut db, 2, "Gadget", 200);

    // Warm the columnar cache
    let initial_columnar = db.get_columnar("test_table").unwrap().expect("Table should exist");
    assert_eq!(initial_columnar.column_count(), 3, "Should have 3 columns initially");
    assert!(initial_columnar.get_column("value").is_some(), "value column should exist");

    let initial_stats = db.columnar_cache_stats();
    assert_eq!(initial_stats.conversions, 1);

    // DROP COLUMN
    let drop_col_stmt = AlterTableStmt::DropColumn(DropColumnStmt {
        table_name: "test_table".to_string(),
        column_name: "value".to_string(),
        if_exists: false,
    });
    AlterTableExecutor::execute(&drop_col_stmt, &mut db).unwrap();

    // Get columnar data again - should not have the dropped column
    let updated_columnar = db.get_columnar("test_table").unwrap().expect("Table should exist");

    // Verify the column was dropped
    assert_eq!(updated_columnar.column_count(), 2, "Should have 2 columns after DROP COLUMN");
    assert!(updated_columnar.get_column("value").is_none(), "value column should no longer exist");
    assert!(updated_columnar.get_column("id").is_some(), "id column should still exist");
    assert!(updated_columnar.get_column("name").is_some(), "name column should still exist");

    // Verify cache was invalidated and re-converted
    let final_stats = db.columnar_cache_stats();
    assert!(
        final_stats.conversions >= 2,
        "Should have re-converted after DROP COLUMN (conversions: {})",
        final_stats.conversions
    );
}

/// Test that MODIFY COLUMN invalidates the columnar cache
///
/// This verifies that after modifying a column's type:
/// 1. The columnar cache is invalidated
/// 2. Subsequent reads return data with the new type
#[test]
fn test_modify_column_invalidates_columnar_cache() {
    let mut db = Database::new();
    setup_test_table(&mut db);

    // Insert initial data
    insert_row(&mut db, 1, "Widget", 100);
    insert_row(&mut db, 2, "Gadget", 200);

    // Warm the columnar cache
    let initial_columnar = db.get_columnar("test_table").unwrap().expect("Table should exist");
    assert_eq!(initial_columnar.row_count(), 2);

    let initial_stats = db.columnar_cache_stats();
    assert_eq!(initial_stats.conversions, 1);

    // MODIFY COLUMN - change value from Integer to BigInt
    let modify_col_stmt = AlterTableStmt::ModifyColumn(ModifyColumnStmt {
        table_name: "test_table".to_string(),
        column_name: "value".to_string(),
        new_column_def: ColumnDef {
            name: "value".to_string(),
            data_type: DataType::Bigint,
            nullable: true,
            default_value: None,
            constraints: vec![],
            comment: None,
            generated_expr: None,
            is_exact_integer_type: false,
        },
    });
    AlterTableExecutor::execute(&modify_col_stmt, &mut db).unwrap();

    // Get columnar data again - should reflect the type change
    let updated_columnar = db.get_columnar("test_table").unwrap().expect("Table should exist");
    assert_eq!(updated_columnar.row_count(), 2, "Should still have 2 rows");

    // Verify values are accessible and correct
    let value_col = updated_columnar.get_column("value").expect("value column should exist");
    let values: Vec<i64> = (0..2)
        .filter_map(|i| match value_col.get(i) {
            SqlValue::Integer(v) | SqlValue::Bigint(v) => Some(v),
            _ => None,
        })
        .collect();
    assert!(values.contains(&100), "Value 100 should exist");
    assert!(values.contains(&200), "Value 200 should exist");

    // Verify cache was invalidated and re-converted
    let final_stats = db.columnar_cache_stats();
    assert!(
        final_stats.conversions >= 2,
        "Should have re-converted after MODIFY COLUMN (conversions: {})",
        final_stats.conversions
    );
}

/// Test that CHANGE COLUMN (rename + modify) invalidates the columnar cache
///
/// This verifies that after changing a column's name and type:
/// 1. The columnar cache is invalidated
/// 2. Subsequent reads return data with the new column name
#[test]
fn test_change_column_invalidates_columnar_cache() {
    let mut db = Database::new();
    setup_test_table(&mut db);

    // Insert initial data
    insert_row(&mut db, 1, "Widget", 100);
    insert_row(&mut db, 2, "Gadget", 200);

    // Warm the columnar cache
    let initial_columnar = db.get_columnar("test_table").unwrap().expect("Table should exist");
    assert!(initial_columnar.get_column("value").is_some(), "value column should exist");
    assert!(initial_columnar.get_column("amount").is_none(), "amount column should not exist yet");

    let initial_stats = db.columnar_cache_stats();
    assert_eq!(initial_stats.conversions, 1);

    // CHANGE COLUMN - rename 'value' to 'amount' and change type
    let change_col_stmt = AlterTableStmt::ChangeColumn(ChangeColumnStmt {
        table_name: "test_table".to_string(),
        old_column_name: "value".to_string(),
        new_column_def: ColumnDef {
            name: "amount".to_string(),
            data_type: DataType::Bigint,
            nullable: true,
            default_value: None,
            constraints: vec![],
            comment: None,
            generated_expr: None,
            is_exact_integer_type: false,
        },
    });
    AlterTableExecutor::execute(&change_col_stmt, &mut db).unwrap();

    // Get columnar data again - should have new column name
    let updated_columnar = db.get_columnar("test_table").unwrap().expect("Table should exist");

    // Verify the column was renamed
    assert!(
        updated_columnar.get_column("value").is_none(),
        "old 'value' column should no longer exist"
    );
    assert!(updated_columnar.get_column("amount").is_some(), "new 'amount' column should exist");

    // Verify values are preserved
    let amount_col = updated_columnar.get_column("amount").expect("amount column should exist");
    let amounts: Vec<i64> = (0..2)
        .filter_map(|i| match amount_col.get(i) {
            SqlValue::Integer(v) | SqlValue::Bigint(v) => Some(v),
            _ => None,
        })
        .collect();
    assert!(amounts.contains(&100), "Value 100 should exist in renamed column");
    assert!(amounts.contains(&200), "Value 200 should exist in renamed column");

    // Verify cache was invalidated and re-converted
    let final_stats = db.columnar_cache_stats();
    assert!(
        final_stats.conversions >= 2,
        "Should have re-converted after CHANGE COLUMN (conversions: {})",
        final_stats.conversions
    );
}

/// Test that ALTER COLUMN SET NOT NULL invalidates the columnar cache
///
/// While SET NOT NULL only changes schema metadata, the columnar representation
/// may encode nullability, so we should invalidate for safety.
#[test]
fn test_alter_column_set_not_null_invalidates_cache() {
    let mut db = Database::new();
    setup_test_table(&mut db);

    // Insert data (no NULLs so SET NOT NULL will succeed)
    insert_row(&mut db, 1, "Widget", 100);
    insert_row(&mut db, 2, "Gadget", 200);

    // Warm the columnar cache
    let _ = db.get_columnar("test_table").unwrap().expect("Table should exist");
    let initial_stats = db.columnar_cache_stats();
    assert_eq!(initial_stats.conversions, 1);

    // ALTER COLUMN SET NOT NULL
    let alter_stmt = AlterTableStmt::AlterColumn(AlterColumnStmt::SetNotNull {
        table_name: "test_table".to_string(),
        column_name: "value".to_string(),
    });
    AlterTableExecutor::execute(&alter_stmt, &mut db).unwrap();

    // Get columnar data again
    let updated_columnar = db.get_columnar("test_table").unwrap().expect("Table should exist");
    assert_eq!(updated_columnar.row_count(), 2);

    // Verify cache was invalidated and re-converted
    let final_stats = db.columnar_cache_stats();
    assert!(
        final_stats.conversions >= 2,
        "Should have re-converted after ALTER COLUMN SET NOT NULL (conversions: {})",
        final_stats.conversions
    );
}

/// Test that ALTER COLUMN DROP NOT NULL invalidates the columnar cache
#[test]
fn test_alter_column_drop_not_null_invalidates_cache() {
    let mut db = Database::new();

    // Create table with NOT NULL column
    let schema = TableSchema::with_primary_key(
        "test_table".to_string(),
        vec![
            ColumnSchema::new("id".to_string(), DataType::Integer, false),
            ColumnSchema::new("required_val".to_string(), DataType::Integer, false), // NOT NULL
        ],
        vec!["id".to_string()],
    );
    db.create_table(schema).unwrap();

    // Insert data
    let stmt = vibesql_ast::InsertStmt {
        with_clause: None,
        schema_name: None,
        schema_quoted: false,
        table_quoted: false,
        table_name: "test_table".to_string(),
        columns: vec![],
        source: vibesql_ast::InsertSource::Values(vec![vec![
            vibesql_ast::Expression::Literal(SqlValue::Integer(1)),
            vibesql_ast::Expression::Literal(SqlValue::Integer(100)),
        ]]),
        conflict_clause: None,
        on_conflict: None,
        on_duplicate_key_update: None,
        returning: None,
    };
    InsertExecutor::execute(&mut db, &stmt).unwrap();

    // Warm the columnar cache
    let _ = db.get_columnar("test_table").unwrap().expect("Table should exist");
    let initial_stats = db.columnar_cache_stats();
    assert_eq!(initial_stats.conversions, 1);

    // ALTER COLUMN DROP NOT NULL
    let alter_stmt = AlterTableStmt::AlterColumn(AlterColumnStmt::DropNotNull {
        table_name: "test_table".to_string(),
        column_name: "required_val".to_string(),
    });
    AlterTableExecutor::execute(&alter_stmt, &mut db).unwrap();

    // Get columnar data again
    let _ = db.get_columnar("test_table").unwrap().expect("Table should exist");

    // Verify cache was invalidated and re-converted
    let final_stats = db.columnar_cache_stats();
    assert!(
        final_stats.conversions >= 2,
        "Should have re-converted after ALTER COLUMN DROP NOT NULL (conversions: {})",
        final_stats.conversions
    );
}

/// Test that ALTER COLUMN SET DEFAULT invalidates the columnar cache
///
/// While SET DEFAULT only changes schema metadata, we invalidate for consistency.
#[test]
fn test_alter_column_set_default_invalidates_cache() {
    let mut db = Database::new();
    setup_test_table(&mut db);

    insert_row(&mut db, 1, "Widget", 100);

    // Warm the columnar cache
    let _ = db.get_columnar("test_table").unwrap().expect("Table should exist");
    let initial_stats = db.columnar_cache_stats();
    assert_eq!(initial_stats.conversions, 1);

    // ALTER COLUMN SET DEFAULT
    let alter_stmt = AlterTableStmt::AlterColumn(AlterColumnStmt::SetDefault {
        table_name: "test_table".to_string(),
        column_name: "value".to_string(),
        default: vibesql_ast::Expression::Literal(SqlValue::Integer(999)),
    });
    AlterTableExecutor::execute(&alter_stmt, &mut db).unwrap();

    // Get columnar data again
    let _ = db.get_columnar("test_table").unwrap().expect("Table should exist");

    // Verify cache was invalidated and re-converted
    let final_stats = db.columnar_cache_stats();
    assert!(
        final_stats.conversions >= 2,
        "Should have re-converted after ALTER COLUMN SET DEFAULT (conversions: {})",
        final_stats.conversions
    );
}

/// Test that ALTER COLUMN DROP DEFAULT invalidates the columnar cache
#[test]
fn test_alter_column_drop_default_invalidates_cache() {
    let mut db = Database::new();

    // Create table with default value
    let mut col_schema = ColumnSchema::new("value".to_string(), DataType::Integer, true);
    col_schema.set_default(vibesql_ast::Expression::Literal(SqlValue::Integer(0)));

    let schema = TableSchema::with_primary_key(
        "test_table".to_string(),
        vec![ColumnSchema::new("id".to_string(), DataType::Integer, false), col_schema],
        vec!["id".to_string()],
    );
    db.create_table(schema).unwrap();

    // Insert data
    let stmt = vibesql_ast::InsertStmt {
        with_clause: None,
        schema_name: None,
        schema_quoted: false,
        table_quoted: false,
        table_name: "test_table".to_string(),
        columns: vec![],
        source: vibesql_ast::InsertSource::Values(vec![vec![
            vibesql_ast::Expression::Literal(SqlValue::Integer(1)),
            vibesql_ast::Expression::Literal(SqlValue::Integer(100)),
        ]]),
        conflict_clause: None,
        on_conflict: None,
        on_duplicate_key_update: None,
        returning: None,
    };
    InsertExecutor::execute(&mut db, &stmt).unwrap();

    // Warm the columnar cache
    let _ = db.get_columnar("test_table").unwrap().expect("Table should exist");
    let initial_stats = db.columnar_cache_stats();
    assert_eq!(initial_stats.conversions, 1);

    // ALTER COLUMN DROP DEFAULT
    let alter_stmt = AlterTableStmt::AlterColumn(AlterColumnStmt::DropDefault {
        table_name: "test_table".to_string(),
        column_name: "value".to_string(),
    });
    AlterTableExecutor::execute(&alter_stmt, &mut db).unwrap();

    // Get columnar data again
    let _ = db.get_columnar("test_table").unwrap().expect("Table should exist");

    // Verify cache was invalidated and re-converted
    let final_stats = db.columnar_cache_stats();
    assert!(
        final_stats.conversions >= 2,
        "Should have re-converted after ALTER COLUMN DROP DEFAULT (conversions: {})",
        final_stats.conversions
    );
}

/// Test that multiple ALTER operations in sequence properly invalidate cache
#[test]
fn test_multiple_alter_operations_invalidate_cache() {
    let mut db = Database::new();
    setup_test_table(&mut db);

    insert_row(&mut db, 1, "Widget", 100);
    insert_row(&mut db, 2, "Gadget", 200);

    // Warm cache
    let _ = db.get_columnar("test_table").unwrap();

    // ADD COLUMN
    let add_stmt = AlterTableStmt::AddColumn(AddColumnStmt {
        table_name: "test_table".to_string(),
        column_def: ColumnDef {
            name: "extra".to_string(),
            data_type: DataType::Integer,
            nullable: true,
            default_value: Some(Box::new(vibesql_ast::Expression::Literal(SqlValue::Integer(0)))),
            constraints: vec![],
            comment: None,
            generated_expr: None,
            is_exact_integer_type: false,
        },
    });
    AlterTableExecutor::execute(&add_stmt, &mut db).unwrap();

    // Check columnar has new column
    let columnar1 = db.get_columnar("test_table").unwrap().expect("Table should exist");
    assert_eq!(columnar1.column_count(), 4);

    // DROP COLUMN (the original 'value' column)
    let drop_stmt = AlterTableStmt::DropColumn(DropColumnStmt {
        table_name: "test_table".to_string(),
        column_name: "value".to_string(),
        if_exists: false,
    });
    AlterTableExecutor::execute(&drop_stmt, &mut db).unwrap();

    // Check columnar reflects drop
    let columnar2 = db.get_columnar("test_table").unwrap().expect("Table should exist");
    assert_eq!(columnar2.column_count(), 3);
    assert!(columnar2.get_column("value").is_none());
    assert!(columnar2.get_column("extra").is_some());

    // Final stats should show multiple conversions
    let final_stats = db.columnar_cache_stats();
    assert!(
        final_stats.conversions >= 3,
        "Should have multiple conversions after multiple ALTER operations (conversions: {})",
        final_stats.conversions
    );
}

/// Test that RENAME TABLE invalidates the columnar cache for both old and new names
#[test]
fn test_rename_table_invalidates_columnar_cache() {
    let mut db = Database::new();
    setup_test_table(&mut db);

    insert_row(&mut db, 1, "Widget", 100);
    insert_row(&mut db, 2, "Gadget", 200);

    // Warm the columnar cache for the old table name
    let _ = db.get_columnar("test_table").unwrap().expect("Table should exist");
    let initial_stats = db.columnar_cache_stats();
    assert_eq!(initial_stats.conversions, 1);

    // RENAME TABLE
    let rename_stmt = vibesql_ast::AlterTableStmt::RenameTable(vibesql_ast::RenameTableStmt {
        table_name: "test_table".to_string(),
        new_table_name: "renamed_table".to_string(),
    });
    AlterTableExecutor::execute(&rename_stmt, &mut db).unwrap();

    // Old table should no longer exist
    assert!(db.get_columnar("test_table").unwrap().is_none());

    // New table should have the data
    let renamed_columnar =
        db.get_columnar("renamed_table").unwrap().expect("Renamed table should exist");
    assert_eq!(renamed_columnar.row_count(), 2);
    assert_eq!(renamed_columnar.column_count(), 3);

    // Verify values are preserved
    let value_col = renamed_columnar.get_column("value").expect("value column should exist");
    let values: Vec<i64> = (0..2)
        .filter_map(|i| match value_col.get(i) {
            SqlValue::Integer(v) => Some(v),
            _ => None,
        })
        .collect();
    assert!(values.contains(&100), "Value 100 should exist after rename");
    assert!(values.contains(&200), "Value 200 should exist after rename");
}

/// Test pre-warmed cache is properly invalidated by ALTER TABLE
#[test]
fn test_alter_invalidates_prewarmed_cache() {
    let mut db = Database::new();
    setup_test_table(&mut db);

    insert_row(&mut db, 1, "Widget", 100);

    // Pre-warm the cache
    let warmed = db.pre_warm_columnar_cache(&["test_table"]).unwrap();
    assert_eq!(warmed, 1);

    // Verify it's cached (second access should be a hit)
    let _ = db.get_columnar("test_table").unwrap();
    let stats_after_get = db.columnar_cache_stats();
    assert_eq!(stats_after_get.hits, 1, "Second get should be a cache hit");

    // ADD COLUMN to invalidate
    let add_stmt = AlterTableStmt::AddColumn(AddColumnStmt {
        table_name: "test_table".to_string(),
        column_def: ColumnDef {
            name: "new_col".to_string(),
            data_type: DataType::Integer,
            nullable: true,
            default_value: None,
            constraints: vec![],
            comment: None,
            generated_expr: None,
            is_exact_integer_type: false,
        },
    });
    AlterTableExecutor::execute(&add_stmt, &mut db).unwrap();

    // Should get fresh data with new column
    let columnar = db.get_columnar("test_table").unwrap().expect("Table should exist");
    assert_eq!(columnar.column_count(), 4, "Should have 4 columns after ADD");
    assert!(columnar.get_column("new_col").is_some(), "New column should be visible");
}