use super::super::*;
#[test]
fn test_scalar_subquery_in_where_clause() {
let mut db = vibesql_storage::Database::new();
let schema = vibesql_catalog::TableSchema::new(
"employees".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(100) },
false,
),
vibesql_catalog::ColumnSchema::new(
"salary".to_string(),
vibesql_types::DataType::Integer,
false,
),
],
);
db.create_table(schema).unwrap();
db.insert_row(
"employees",
vibesql_storage::Row::new(vec![
vibesql_types::SqlValue::Integer(1),
vibesql_types::SqlValue::Varchar(arcstr::ArcStr::from("Alice")),
vibesql_types::SqlValue::Integer(50000),
]),
)
.unwrap();
db.insert_row(
"employees",
vibesql_storage::Row::new(vec![
vibesql_types::SqlValue::Integer(2),
vibesql_types::SqlValue::Varchar(arcstr::ArcStr::from("Bob")),
vibesql_types::SqlValue::Integer(60000),
]),
)
.unwrap();
db.insert_row(
"employees",
vibesql_storage::Row::new(vec![
vibesql_types::SqlValue::Integer(3),
vibesql_types::SqlValue::Varchar(arcstr::ArcStr::from("Charlie")),
vibesql_types::SqlValue::Integer(70000),
]),
)
.unwrap();
let subquery = Box::new(vibesql_ast::SelectStmt {
into_table: None,
into_variables: None,
with_clause: None,
set_operation: None,
values: None,
distinct: false,
select_list: vec![vibesql_ast::SelectItem::Expression {
expr: vibesql_ast::Expression::Function {
name: vibesql_ast::FunctionIdentifier::new("AVG"),
args: vec![vibesql_ast::Expression::ColumnRef(
vibesql_ast::ColumnIdentifier::simple("salary", false),
)],
character_unit: None,
},
alias: None,
source_text: None,
}],
from: Some(vibesql_ast::FromClause::Table {
index_hint: None,
quoted: false,
name: "employees".to_string(),
alias: None,
column_aliases: None,
}),
where_clause: None,
group_by: None,
having: None,
window_definitions: None,
order_by: None,
limit: None,
offset: None,
});
let stmt = vibesql_ast::SelectStmt {
into_table: None,
into_variables: None,
with_clause: None,
set_operation: None,
values: None,
distinct: false,
select_list: vec![vibesql_ast::SelectItem::Wildcard { alias: None }],
from: Some(vibesql_ast::FromClause::Table {
index_hint: None,
quoted: false,
name: "employees".to_string(),
alias: None,
column_aliases: None,
}),
where_clause: Some(vibesql_ast::Expression::BinaryOp {
left: Box::new(vibesql_ast::Expression::ColumnRef(
vibesql_ast::ColumnIdentifier::simple("salary", false),
)),
op: vibesql_ast::BinaryOperator::GreaterThan,
right: Box::new(vibesql_ast::Expression::ScalarSubquery(subquery)),
}),
group_by: None,
having: None,
window_definitions: None,
order_by: None,
limit: None,
offset: None,
};
let executor = SelectExecutor::new(&db);
let result = executor.execute(&stmt).unwrap();
assert_eq!(result.len(), 1);
assert_eq!(result[0].values[0], vibesql_types::SqlValue::Integer(3));
assert_eq!(
result[0].values[1],
vibesql_types::SqlValue::Varchar(arcstr::ArcStr::from("Charlie"))
);
assert_eq!(result[0].values[2], vibesql_types::SqlValue::Integer(70000));
}
#[test]
fn test_scalar_subquery_in_select_list() {
let mut db = vibesql_storage::Database::new();
let schema = vibesql_catalog::TableSchema::new(
"employees".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(100) },
false,
),
vibesql_catalog::ColumnSchema::new(
"salary".to_string(),
vibesql_types::DataType::Integer,
false,
),
],
);
db.create_table(schema).unwrap();
db.insert_row(
"employees",
vibesql_storage::Row::new(vec![
vibesql_types::SqlValue::Integer(1),
vibesql_types::SqlValue::Varchar(arcstr::ArcStr::from("Alice")),
vibesql_types::SqlValue::Integer(50000),
]),
)
.unwrap();
db.insert_row(
"employees",
vibesql_storage::Row::new(vec![
vibesql_types::SqlValue::Integer(2),
vibesql_types::SqlValue::Varchar(arcstr::ArcStr::from("Bob")),
vibesql_types::SqlValue::Integer(70000),
]),
)
.unwrap();
let subquery = Box::new(vibesql_ast::SelectStmt {
into_table: None,
into_variables: None,
with_clause: None,
set_operation: None,
values: None,
distinct: false,
select_list: vec![vibesql_ast::SelectItem::Expression {
expr: vibesql_ast::Expression::Function {
name: vibesql_ast::FunctionIdentifier::new("MAX"),
args: vec![vibesql_ast::Expression::ColumnRef(
vibesql_ast::ColumnIdentifier::simple("salary", false),
)],
character_unit: None,
},
alias: None,
source_text: None,
}],
from: Some(vibesql_ast::FromClause::Table {
index_hint: None,
quoted: false,
name: "employees".to_string(),
alias: None,
column_aliases: None,
}),
where_clause: None,
group_by: None,
having: None,
window_definitions: None,
order_by: None,
limit: None,
offset: None,
});
let stmt = vibesql_ast::SelectStmt {
into_table: None,
into_variables: None,
with_clause: None,
set_operation: None,
values: None,
distinct: false,
select_list: vec![
vibesql_ast::SelectItem::Expression {
expr: vibesql_ast::Expression::ColumnRef(vibesql_ast::ColumnIdentifier::simple(
"name", false,
)),
alias: None,
source_text: None,
},
vibesql_ast::SelectItem::Expression {
expr: vibesql_ast::Expression::ColumnRef(vibesql_ast::ColumnIdentifier::simple(
"salary", false,
)),
alias: None,
source_text: None,
},
vibesql_ast::SelectItem::Expression {
expr: vibesql_ast::Expression::ScalarSubquery(subquery),
alias: Some("max_sal".to_string()),
source_text: None,
},
],
from: Some(vibesql_ast::FromClause::Table {
index_hint: None,
quoted: false,
name: "employees".to_string(),
alias: None,
column_aliases: None,
}),
where_clause: None,
group_by: None,
having: None,
window_definitions: None,
order_by: None,
limit: None,
offset: None,
};
let executor = SelectExecutor::new(&db);
let result = executor.execute(&stmt).unwrap();
assert_eq!(result.len(), 2);
assert_eq!(
result[0].values[0],
vibesql_types::SqlValue::Varchar(arcstr::ArcStr::from("Alice"))
);
assert_eq!(result[0].values[1], vibesql_types::SqlValue::Integer(50000));
assert_eq!(result[0].values[2], vibesql_types::SqlValue::Integer(70000)); assert_eq!(result[1].values[0], vibesql_types::SqlValue::Varchar(arcstr::ArcStr::from("Bob")));
assert_eq!(result[1].values[1], vibesql_types::SqlValue::Integer(70000));
assert_eq!(result[1].values[2], vibesql_types::SqlValue::Integer(70000)); }
#[test]
fn test_scalar_subquery_returns_null_when_empty() {
let mut db = vibesql_storage::Database::new();
let schema = vibesql_catalog::TableSchema::new(
"employees".to_string(),
vec![vibesql_catalog::ColumnSchema::new(
"id".to_string(),
vibesql_types::DataType::Integer,
false,
)],
);
db.create_table(schema).unwrap();
db.insert_row(
"employees",
vibesql_storage::Row::new(vec![vibesql_types::SqlValue::Integer(1)]),
)
.unwrap();
let subquery = Box::new(vibesql_ast::SelectStmt {
into_table: None,
into_variables: None,
with_clause: None,
set_operation: None,
values: None,
distinct: false,
select_list: vec![vibesql_ast::SelectItem::Expression {
expr: vibesql_ast::Expression::ColumnRef(vibesql_ast::ColumnIdentifier::simple(
"id", false,
)),
alias: None,
source_text: None,
}],
from: Some(vibesql_ast::FromClause::Table {
index_hint: None,
quoted: false,
name: "employees".to_string(),
alias: None,
column_aliases: None,
}),
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(
999,
))),
}),
group_by: None,
having: None,
window_definitions: None,
order_by: None,
limit: None,
offset: None,
});
let stmt = vibesql_ast::SelectStmt {
into_table: None,
into_variables: None,
with_clause: None,
set_operation: None,
values: None,
distinct: false,
select_list: vec![vibesql_ast::SelectItem::Expression {
expr: vibesql_ast::Expression::ScalarSubquery(subquery),
alias: Some("missing_id".to_string()),
source_text: None,
}],
from: Some(vibesql_ast::FromClause::Table {
index_hint: None,
quoted: false,
name: "employees".to_string(),
alias: None,
column_aliases: None,
}),
where_clause: None,
group_by: None,
having: None,
window_definitions: None,
order_by: None,
limit: None,
offset: None,
};
let executor = SelectExecutor::new(&db);
let result = executor.execute(&stmt).unwrap();
assert_eq!(result.len(), 1);
assert_eq!(result[0].values[0], vibesql_types::SqlValue::Null);
}