mod common;
use common::setup_users_table as setup_test_table;
use vibesql_executor::{ExecutorError, InsertExecutor};
#[test]
fn test_insert_from_select_basic() {
let mut db = vibesql_storage::Database::new();
setup_test_table(&mut db);
let insert_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(vibesql_types::SqlValue::Integer(1)),
vibesql_ast::Expression::Literal(vibesql_types::SqlValue::Varchar(
arcstr::ArcStr::from("Alice"),
)),
]]),
conflict_clause: None,
on_conflict: None,
on_duplicate_key_update: None,
returning: None,
};
InsertExecutor::execute(&mut db, &insert_stmt).unwrap();
let schema = vibesql_catalog::TableSchema::new(
"users_backup".to_string(),
vec![
vibesql_catalog::ColumnSchema::new(
"id".to_string(),
vibesql_types::DataType::Integer,
false,
),
vibesql_catalog::ColumnSchema::new(
"name".to_string(),
vibesql_types::DataType::Varchar { max_length: Some(50) },
true,
),
],
);
db.create_table(schema).unwrap();
let select_stmt = vibesql_ast::SelectStmt {
with_clause: None,
distinct: false,
select_list: vec![vibesql_ast::SelectItem::Wildcard { alias: None }],
into_table: None,
into_variables: None,
from: Some(vibesql_ast::FromClause::Table {
index_hint: None,
name: "users".to_string(),
alias: None,
column_aliases: None,
quoted: false,
}),
where_clause: None,
group_by: None,
having: None,
window_definitions: None,
order_by: None,
limit: None,
offset: None,
set_operation: None,
values: None,
};
let insert_select_stmt = vibesql_ast::InsertStmt {
with_clause: None,
schema_name: None,
schema_quoted: false,
table_quoted: false,
table_name: "users_backup".to_string(),
columns: vec![], source: vibesql_ast::InsertSource::Select(Box::new(select_stmt)),
conflict_clause: None,
on_conflict: None,
on_duplicate_key_update: None,
returning: None,
};
let rows = InsertExecutor::execute(&mut db, &insert_select_stmt).unwrap();
assert_eq!(rows, 1);
let table = db.get_table("users_backup").unwrap();
assert_eq!(table.row_count(), 1);
}
#[test]
fn test_insert_from_select_with_where() {
let mut db = vibesql_storage::Database::new();
setup_test_table(&mut db);
let insert_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(vibesql_types::SqlValue::Integer(1)),
vibesql_ast::Expression::Literal(vibesql_types::SqlValue::Varchar(
arcstr::ArcStr::from("Alice"),
)),
],
vec![
vibesql_ast::Expression::Literal(vibesql_types::SqlValue::Integer(2)),
vibesql_ast::Expression::Literal(vibesql_types::SqlValue::Varchar(
arcstr::ArcStr::from("Bob"),
)),
],
]),
conflict_clause: None,
on_conflict: None,
on_duplicate_key_update: None,
returning: None,
};
InsertExecutor::execute(&mut db, &insert_stmt).unwrap();
let schema = vibesql_catalog::TableSchema::new(
"active_users".to_string(),
vec![
vibesql_catalog::ColumnSchema::new(
"id".to_string(),
vibesql_types::DataType::Integer,
false,
),
vibesql_catalog::ColumnSchema::new(
"name".to_string(),
vibesql_types::DataType::Varchar { max_length: Some(50) },
true,
),
],
);
db.create_table(schema).unwrap();
let select_stmt = vibesql_ast::SelectStmt {
with_clause: None,
distinct: false,
select_list: vec![vibesql_ast::SelectItem::Wildcard { alias: None }],
into_table: None,
into_variables: None,
from: Some(vibesql_ast::FromClause::Table {
index_hint: None,
name: "users".to_string(),
alias: None,
column_aliases: None,
quoted: false,
}),
where_clause: Some(vibesql_ast::Expression::BinaryOp {
left: Box::new(vibesql_ast::Expression::ColumnRef(
vibesql_ast::ColumnIdentifier::simple("id", false),
)),
op: vibesql_ast::BinaryOperator::Equal,
right: Box::new(vibesql_ast::Expression::Literal(vibesql_types::SqlValue::Integer(1))),
}),
group_by: None,
having: None,
window_definitions: None,
order_by: None,
limit: None,
offset: None,
set_operation: None,
values: None,
};
let insert_select_stmt = vibesql_ast::InsertStmt {
with_clause: None,
schema_name: None,
schema_quoted: false,
table_quoted: false,
table_name: "active_users".to_string(),
columns: vec![],
source: vibesql_ast::InsertSource::Select(Box::new(select_stmt)),
conflict_clause: None,
on_conflict: None,
on_duplicate_key_update: None,
returning: None,
};
let rows = InsertExecutor::execute(&mut db, &insert_select_stmt).unwrap();
assert_eq!(rows, 1);
let table = db.get_table("active_users").unwrap();
assert_eq!(table.row_count(), 1);
}
#[test]
fn test_insert_from_select_column_mismatch() {
let mut db = vibesql_storage::Database::new();
setup_test_table(&mut db);
let insert_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(vibesql_types::SqlValue::Integer(1)),
vibesql_ast::Expression::Literal(vibesql_types::SqlValue::Varchar(
arcstr::ArcStr::from("Alice"),
)),
]]),
conflict_clause: None,
on_conflict: None,
on_duplicate_key_update: None,
returning: None,
};
InsertExecutor::execute(&mut db, &insert_stmt).unwrap();
let schema = vibesql_catalog::TableSchema::new(
"wrong_table".to_string(),
vec![
vibesql_catalog::ColumnSchema::new(
"id".to_string(),
vibesql_types::DataType::Integer,
false,
),
vibesql_catalog::ColumnSchema::new(
"name".to_string(),
vibesql_types::DataType::Varchar { max_length: Some(50) },
true,
),
vibesql_catalog::ColumnSchema::new(
"extra".to_string(),
vibesql_types::DataType::Integer,
true,
),
],
);
db.create_table(schema).unwrap();
let select_stmt = vibesql_ast::SelectStmt {
with_clause: None,
distinct: false,
select_list: vec![vibesql_ast::SelectItem::Wildcard { alias: None }],
into_table: None,
into_variables: None,
from: Some(vibesql_ast::FromClause::Table {
index_hint: None,
name: "users".to_string(),
alias: None,
column_aliases: None,
quoted: false,
}),
where_clause: None,
group_by: None,
having: None,
window_definitions: None,
order_by: None,
limit: None,
offset: None,
set_operation: None,
values: None,
};
let insert_select_stmt = vibesql_ast::InsertStmt {
with_clause: None,
schema_name: None,
schema_quoted: false,
table_quoted: false,
table_name: "wrong_table".to_string(),
columns: vec![], source: vibesql_ast::InsertSource::Select(Box::new(select_stmt)),
conflict_clause: None,
on_conflict: None,
on_duplicate_key_update: None,
returning: None,
};
let result = InsertExecutor::execute(&mut db, &insert_select_stmt);
assert!(result.is_err());
assert!(matches!(result.unwrap_err(), ExecutorError::InsertColumnCountMismatch { .. }));
}
#[test]
fn test_insert_from_select_with_aggregates() {
let mut db = vibesql_storage::Database::new();
let schema = vibesql_catalog::TableSchema::new(
"sales".to_string(),
vec![
vibesql_catalog::ColumnSchema::new(
"id".to_string(),
vibesql_types::DataType::Integer,
false,
),
vibesql_catalog::ColumnSchema::new(
"amount".to_string(),
vibesql_types::DataType::Integer,
false,
),
],
);
db.create_table(schema).unwrap();
let insert_stmt = vibesql_ast::InsertStmt {
with_clause: None,
schema_name: None,
schema_quoted: false,
table_quoted: false,
table_name: "sales".to_string(),
columns: vec![],
source: vibesql_ast::InsertSource::Values(vec![
vec![
vibesql_ast::Expression::Literal(vibesql_types::SqlValue::Integer(1)),
vibesql_ast::Expression::Literal(vibesql_types::SqlValue::Integer(100)),
],
vec![
vibesql_ast::Expression::Literal(vibesql_types::SqlValue::Integer(2)),
vibesql_ast::Expression::Literal(vibesql_types::SqlValue::Integer(200)),
],
]),
conflict_clause: None,
on_conflict: None,
on_duplicate_key_update: None,
returning: None,
};
InsertExecutor::execute(&mut db, &insert_stmt).unwrap();
let summary_schema = vibesql_catalog::TableSchema::new(
"summary".to_string(),
vec![
vibesql_catalog::ColumnSchema::new(
"total".to_string(),
vibesql_types::DataType::Integer,
false,
),
vibesql_catalog::ColumnSchema::new(
"count".to_string(),
vibesql_types::DataType::Integer,
false,
),
],
);
db.create_table(summary_schema).unwrap();
let select_stmt = vibesql_ast::SelectStmt {
with_clause: None,
distinct: false,
select_list: vec![
vibesql_ast::SelectItem::Expression {
expr: vibesql_ast::Expression::Function {
name: vibesql_ast::FunctionIdentifier::new("SUM"),
args: vec![vibesql_ast::Expression::ColumnRef(
vibesql_ast::ColumnIdentifier::simple("amount", false),
)],
character_unit: None,
},
alias: None,
source_text: None,
},
vibesql_ast::SelectItem::Expression {
expr: vibesql_ast::Expression::Function {
name: vibesql_ast::FunctionIdentifier::new("COUNT"),
args: vec![vibesql_ast::Expression::Wildcard],
character_unit: None,
},
alias: None,
source_text: None,
},
],
into_table: None,
into_variables: None,
from: Some(vibesql_ast::FromClause::Table {
index_hint: None,
name: "sales".to_string(),
alias: None,
column_aliases: None,
quoted: false,
}),
where_clause: None,
group_by: None,
having: None,
window_definitions: None,
order_by: None,
limit: None,
offset: None,
set_operation: None,
values: None,
};
let insert_select_stmt = vibesql_ast::InsertStmt {
with_clause: None,
schema_name: None,
schema_quoted: false,
table_quoted: false,
table_name: "summary".to_string(),
columns: vec![],
source: vibesql_ast::InsertSource::Select(Box::new(select_stmt)),
conflict_clause: None,
on_conflict: None,
on_duplicate_key_update: None,
returning: None,
};
let rows = InsertExecutor::execute(&mut db, &insert_select_stmt).unwrap();
assert_eq!(rows, 1);
let table = db.get_table("summary").unwrap();
assert_eq!(table.row_count(), 1);
}