use vibesql_catalog::{ColumnSchema, TableSchema};
use vibesql_executor::InsertExecutor;
use vibesql_storage::Database;
use vibesql_types::{DataType, SqlValue};
fn setup_products_table(db: &mut Database) {
let schema = TableSchema::with_primary_key(
"products".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("price".to_string(), DataType::Integer, true),
],
vec!["id".to_string()], );
db.create_table(schema).unwrap();
}
fn insert_product(db: &mut Database, id: i64, name: &str, price: i64) {
let stmt = vibesql_ast::InsertStmt {
with_clause: None,
schema_name: None,
schema_quoted: false,
table_quoted: false,
table_name: "products".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(price)),
]]),
conflict_clause: None,
on_conflict: None,
on_duplicate_key_update: None,
returning: None,
};
InsertExecutor::execute(db, &stmt).unwrap();
}
fn replace_product(db: &mut Database, id: i64, name: &str, price: i64) {
let stmt = vibesql_ast::InsertStmt {
with_clause: None,
schema_name: None,
schema_quoted: false,
table_quoted: false,
table_name: "products".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(price)),
]]),
conflict_clause: Some(vibesql_ast::ConflictClause::Replace),
on_conflict: None,
on_duplicate_key_update: None,
returning: None,
};
InsertExecutor::execute(db, &stmt).unwrap();
}
#[test]
fn test_replace_invalidates_columnar_cache() {
let mut db = Database::new();
setup_products_table(&mut db);
insert_product(&mut db, 1, "Widget", 100);
insert_product(&mut db, 2, "Gadget", 200);
insert_product(&mut db, 3, "Gizmo", 300);
let initial_columnar = db.get_columnar("products").unwrap().expect("Table should exist");
assert_eq!(initial_columnar.row_count(), 3);
let initial_stats = db.columnar_cache_stats();
assert_eq!(initial_stats.conversions, 1, "Should have done one conversion");
let price_col = initial_columnar.get_column("price").expect("Column should exist");
let prices: Vec<i64> = (0..3)
.map(|i| match price_col.get(i) {
SqlValue::Integer(v) => v,
other => panic!("Expected Integer, got {:?}", other),
})
.collect();
assert_eq!(prices, vec![100, 200, 300], "Initial prices should be 100, 200, 300");
replace_product(&mut db, 1, "Super Widget", 150);
let updated_columnar = db.get_columnar("products").unwrap().expect("Table should exist");
assert_eq!(updated_columnar.row_count(), 3, "Should still have 3 rows");
let updated_price_col = updated_columnar.get_column("price").expect("Column should exist");
let updated_prices: Vec<i64> = (0..3)
.map(|i| match updated_price_col.get(i) {
SqlValue::Integer(v) => v,
other => panic!("Expected Integer, got {:?}", other),
})
.collect();
assert!(!updated_prices.contains(&100), "Old price 100 should no longer exist after REPLACE");
assert!(updated_prices.contains(&150), "New price 150 should exist after REPLACE");
assert!(updated_prices.contains(&200), "Unchanged price 200 should still exist");
assert!(updated_prices.contains(&300), "Unchanged price 300 should still exist");
let final_stats = db.columnar_cache_stats();
assert!(
final_stats.conversions >= 2,
"Should have re-converted after REPLACE (conversions: {})",
final_stats.conversions
);
}
#[test]
fn test_replace_invalidates_prewarmed_cache() {
let mut db = Database::new();
setup_products_table(&mut db);
insert_product(&mut db, 1, "Item A", 50);
insert_product(&mut db, 2, "Item B", 75);
let warmed = db.pre_warm_columnar_cache(&["products"]).unwrap();
assert_eq!(warmed, 1, "Should warm 1 table");
let initial_columnar = db.get_columnar("products").unwrap().expect("Table should exist");
assert_eq!(initial_columnar.row_count(), 2);
let stats_after_get = db.columnar_cache_stats();
assert_eq!(stats_after_get.hits, 1, "Second get should be a cache hit");
replace_product(&mut db, 1, "Item A Updated", 999);
let updated_columnar = db.get_columnar("products").unwrap().expect("Table should exist");
assert_eq!(updated_columnar.row_count(), 2, "Should still have 2 rows");
let price_col = updated_columnar.get_column("price").expect("Column should exist");
let has_new_price = (0..2).any(|i| matches!(price_col.get(i), SqlValue::Integer(999)));
assert!(has_new_price, "New price 999 should be visible after REPLACE");
let has_old_price = (0..2).any(|i| matches!(price_col.get(i), SqlValue::Integer(50)));
assert!(!has_old_price, "Old price 50 should NOT be visible after REPLACE");
}
#[test]
fn test_replace_unique_constraint_invalidates_cache() {
let mut db = Database::new();
let schema = TableSchema::with_all_constraints(
"users".to_string(),
vec![
ColumnSchema::new("id".to_string(), DataType::Integer, false),
ColumnSchema::new(
"name".to_string(),
DataType::Varchar { max_length: Some(50) },
false,
),
ColumnSchema::new("score".to_string(), DataType::Integer, true),
],
Some(vec!["id".to_string()]), vec![vec!["name".to_string()]], );
db.create_table(schema).unwrap();
let insert = |db: &mut Database, id: i64, name: &str, score: i64| {
let stmt = vibesql_ast::InsertStmt {
with_clause: None,
schema_name: None,
schema_quoted: false,
table_quoted: false,
table_name: "users".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(score)),
]]),
conflict_clause: None,
on_conflict: None,
on_duplicate_key_update: None,
returning: None,
};
InsertExecutor::execute(db, &stmt).unwrap();
};
insert(&mut db, 1, "alice", 100);
insert(&mut db, 2, "bob", 200);
let _ = db.get_columnar("users").unwrap();
let replace_stmt = vibesql_ast::InsertStmt {
with_clause: None,
schema_name: None,
schema_quoted: false,
table_quoted: false,
table_name: "users".to_string(),
columns: vec![],
source: vibesql_ast::InsertSource::Values(vec![vec![
vibesql_ast::Expression::Literal(SqlValue::Integer(3)), vibesql_ast::Expression::Literal(SqlValue::Varchar(arcstr::ArcStr::from("alice"))),
vibesql_ast::Expression::Literal(SqlValue::Integer(999)), ]]),
conflict_clause: Some(vibesql_ast::ConflictClause::Replace),
on_conflict: None,
on_duplicate_key_update: None,
returning: None,
};
InsertExecutor::execute(&mut db, &replace_stmt).unwrap();
let columnar = db.get_columnar("users").unwrap().expect("Table should exist");
assert_eq!(columnar.row_count(), 2, "Should still have 2 rows");
let score_col = columnar.get_column("score").expect("Column should exist");
let has_new_score = (0..2).any(|i| matches!(score_col.get(i), SqlValue::Integer(999)));
assert!(
has_new_score,
"Alice's new score 999 should be visible after REPLACE on UNIQUE conflict"
);
let has_old_score = (0..2).any(|i| matches!(score_col.get(i), SqlValue::Integer(100)));
assert!(!has_old_score, "Alice's old score 100 should NOT be visible after REPLACE");
}
#[test]
fn test_replace_no_conflict_works_with_cache() {
let mut db = Database::new();
setup_products_table(&mut db);
insert_product(&mut db, 1, "Existing", 100);
let _ = db.get_columnar("products").unwrap();
replace_product(&mut db, 2, "New Item", 200);
let columnar = db.get_columnar("products").unwrap().expect("Table should exist");
assert_eq!(columnar.row_count(), 2, "Should have 2 rows");
let price_col = columnar.get_column("price").expect("Column should exist");
let prices: Vec<i64> = (0..2)
.filter_map(|i| match price_col.get(i) {
SqlValue::Integer(v) => Some(v),
_ => None,
})
.collect();
assert!(prices.contains(&100), "Original price 100 should exist");
assert!(prices.contains(&200), "New price 200 should exist");
}