use super::*;
#[test]
fn test_parse_simple_join() {
let result = Parser::parse_sql("SELECT * FROM users JOIN orders ON users.id = orders.user_id;");
assert!(result.is_ok());
let stmt = result.unwrap();
match stmt {
vibesql_ast::Statement::Select(select) => {
assert!(select.from.is_some());
match select.from.as_ref().unwrap() {
vibesql_ast::FromClause::Join {
join_type,
left,
right,
condition,
natural,
..
} => {
assert_eq!(*join_type, vibesql_ast::JoinType::Inner);
assert!(!*natural);
match **left {
vibesql_ast::FromClause::Table { ref name, .. } if name == "users" => {}
_ => panic!("Expected left table to be 'users'"),
}
match **right {
vibesql_ast::FromClause::Table { ref name, .. } if name == "orders" => {}
_ => panic!("Expected right table to be 'orders'"),
}
assert!(condition.is_some());
}
_ => panic!("Expected JOIN in FROM clause"),
}
}
_ => panic!("Expected SELECT statement"),
}
}
#[test]
fn test_parse_inner_join() {
let result =
Parser::parse_sql("SELECT * FROM users INNER JOIN orders ON users.id = orders.user_id;");
assert!(result.is_ok());
let stmt = result.unwrap();
match stmt {
vibesql_ast::Statement::Select(select) => match select.from.as_ref().unwrap() {
vibesql_ast::FromClause::Join { join_type, .. } => {
assert_eq!(*join_type, vibesql_ast::JoinType::Inner);
}
_ => panic!("Expected JOIN"),
},
_ => panic!("Expected SELECT"),
}
}
#[test]
fn test_parse_comma_separated_from() {
let result = Parser::parse_sql("SELECT * FROM tab0, tab1 AS cor0;");
assert!(result.is_ok());
let stmt = result.unwrap();
match stmt {
vibesql_ast::Statement::Select(select) => {
assert!(select.from.is_some());
match select.from.as_ref().unwrap() {
vibesql_ast::FromClause::Join {
join_type,
left,
right,
condition,
natural,
..
} => {
assert_eq!(*join_type, vibesql_ast::JoinType::Cross);
assert!(!*natural);
match **left {
vibesql_ast::FromClause::Table { ref name, alias: None, .. }
if name == "tab0" => {}
_ => panic!("Expected left table to be 'tab0'"),
}
match **right {
vibesql_ast::FromClause::Table {
ref name, alias: Some(ref alias), ..
} if name == "tab1" && alias == "cor0" => {} _ => panic!("Expected right table to be 'tab1' with alias 'cor0'"),
}
assert!(condition.is_none());
}
_ => panic!("Expected JOIN in FROM clause"),
}
}
_ => panic!("Expected SELECT statement"),
}
}
#[test]
fn test_parse_left_join() {
let result =
Parser::parse_sql("SELECT * FROM users LEFT JOIN orders ON users.id = orders.user_id;");
assert!(result.is_ok());
let stmt = result.unwrap();
match stmt {
vibesql_ast::Statement::Select(select) => match select.from.as_ref().unwrap() {
vibesql_ast::FromClause::Join { join_type, .. } => {
assert_eq!(*join_type, vibesql_ast::JoinType::LeftOuter);
}
_ => panic!("Expected JOIN"),
},
_ => panic!("Expected SELECT"),
}
}
#[test]
fn test_parse_left_outer_join() {
let result = Parser::parse_sql(
"SELECT * FROM users LEFT OUTER JOIN orders ON users.id = orders.user_id;",
);
assert!(result.is_ok());
let stmt = result.unwrap();
match stmt {
vibesql_ast::Statement::Select(select) => match select.from.as_ref().unwrap() {
vibesql_ast::FromClause::Join { join_type, .. } => {
assert_eq!(*join_type, vibesql_ast::JoinType::LeftOuter);
}
_ => panic!("Expected JOIN"),
},
_ => panic!("Expected SELECT"),
}
}
#[test]
fn test_parse_right_join() {
let result =
Parser::parse_sql("SELECT * FROM users RIGHT JOIN orders ON users.id = orders.user_id;");
assert!(result.is_ok());
let stmt = result.unwrap();
match stmt {
vibesql_ast::Statement::Select(select) => match select.from.as_ref().unwrap() {
vibesql_ast::FromClause::Join { join_type, .. } => {
assert_eq!(*join_type, vibesql_ast::JoinType::RightOuter);
}
_ => panic!("Expected JOIN"),
},
_ => panic!("Expected SELECT"),
}
}
#[test]
fn test_parse_multiple_joins() {
let result = Parser::parse_sql(
"SELECT * FROM users JOIN orders ON users.id = orders.user_id JOIN products ON orders.product_id = products.id;"
);
assert!(result.is_ok());
let stmt = result.unwrap();
match stmt {
vibesql_ast::Statement::Select(select) => {
match select.from.as_ref().unwrap() {
vibesql_ast::FromClause::Join { left, .. } => {
match **left {
vibesql_ast::FromClause::Join { .. } => {} _ => panic!("Expected nested JOIN"),
}
}
_ => panic!("Expected JOIN"),
}
}
_ => panic!("Expected SELECT"),
}
}
#[test]
fn test_parse_join_using_single_column() {
let result = Parser::parse_sql("SELECT * FROM t1 JOIN t2 USING (id);");
assert!(result.is_ok());
let stmt = result.unwrap();
match stmt {
vibesql_ast::Statement::Select(select) => match select.from.as_ref().unwrap() {
vibesql_ast::FromClause::Join { join_type, using_columns, natural, .. } => {
assert_eq!(*join_type, vibesql_ast::JoinType::Inner);
assert!(!*natural);
assert!(using_columns.is_some());
let cols = using_columns.as_ref().unwrap();
assert_eq!(cols.len(), 1);
assert_eq!(cols[0], "id");
}
_ => panic!("Expected JOIN"),
},
_ => panic!("Expected SELECT"),
}
}
#[test]
fn test_parse_join_using_multiple_columns() {
let result = Parser::parse_sql("SELECT * FROM t1 LEFT JOIN t2 USING (id, name, value);");
assert!(result.is_ok());
let stmt = result.unwrap();
match stmt {
vibesql_ast::Statement::Select(select) => match select.from.as_ref().unwrap() {
vibesql_ast::FromClause::Join { join_type, using_columns, .. } => {
assert_eq!(*join_type, vibesql_ast::JoinType::LeftOuter);
assert!(using_columns.is_some());
let cols = using_columns.as_ref().unwrap();
assert_eq!(cols.len(), 3);
}
_ => panic!("Expected JOIN"),
},
_ => panic!("Expected SELECT"),
}
}
#[test]
fn test_parse_full_join_using() {
let result = Parser::parse_sql("SELECT * FROM t1 FULL OUTER JOIN t2 USING (id);");
assert!(result.is_ok());
let stmt = result.unwrap();
match stmt {
vibesql_ast::Statement::Select(select) => match select.from.as_ref().unwrap() {
vibesql_ast::FromClause::Join { join_type, using_columns, .. } => {
assert_eq!(*join_type, vibesql_ast::JoinType::FullOuter);
assert!(using_columns.is_some());
}
_ => panic!("Expected JOIN"),
},
_ => panic!("Expected SELECT"),
}
}
#[test]
fn test_parse_parenthesized_join() {
let result =
Parser::parse_sql("SELECT * FROM t1 JOIN (t2 JOIN t3 ON t2.id = t3.id) ON t1.id = t2.id;");
assert!(result.is_ok());
let stmt = result.unwrap();
match stmt {
vibesql_ast::Statement::Select(select) => {
match select.from.as_ref().unwrap() {
vibesql_ast::FromClause::Join { left, right, .. } => {
match **left {
vibesql_ast::FromClause::Table { ref name, .. } if name == "t1" => {}
_ => panic!("Expected left table to be t1"),
}
match **right {
vibesql_ast::FromClause::Join { .. } => {} _ => panic!("Expected right to be a JOIN"),
}
}
_ => panic!("Expected JOIN"),
}
}
_ => panic!("Expected SELECT"),
}
}
#[test]
fn test_parse_nested_parenthesized_join_with_using() {
let result = Parser::parse_sql(
"SELECT * FROM t3 FULL JOIN (t4 FULL JOIN (t5 FULL JOIN t6 USING (id)) USING(id)) USING(id);",
);
assert!(result.is_ok(), "Parse failed: {:?}", result.err());
let stmt = result.unwrap();
match stmt {
vibesql_ast::Statement::Select(select) => {
match select.from.as_ref().unwrap() {
vibesql_ast::FromClause::Join { join_type, using_columns, right, .. } => {
assert_eq!(*join_type, vibesql_ast::JoinType::FullOuter);
assert!(using_columns.is_some());
match right.as_ref() {
vibesql_ast::FromClause::Join { join_type: ref inner_type, .. } => {
assert_eq!(*inner_type, vibesql_ast::JoinType::FullOuter);
}
_ => panic!("Expected nested JOIN"),
}
}
_ => panic!("Expected FULL JOIN"),
}
}
_ => panic!("Expected SELECT"),
}
}
#[test]
fn test_parse_natural_join() {
let result = Parser::parse_sql("SELECT * FROM t1 NATURAL JOIN t2;");
assert!(result.is_ok(), "Parse failed: {:?}", result.err());
let stmt = result.unwrap();
match stmt {
vibesql_ast::Statement::Select(select) => match select.from.as_ref().unwrap() {
vibesql_ast::FromClause::Join {
join_type, natural, condition, using_columns, ..
} => {
assert_eq!(*join_type, vibesql_ast::JoinType::Inner);
assert!(*natural);
assert!(condition.is_none());
assert!(using_columns.is_none());
}
_ => panic!("Expected JOIN"),
},
_ => panic!("Expected SELECT"),
}
}
#[test]
fn test_parse_natural_left_join() {
let result = Parser::parse_sql("SELECT * FROM t1 NATURAL LEFT JOIN t2;");
assert!(result.is_ok(), "Parse failed: {:?}", result.err());
let stmt = result.unwrap();
match stmt {
vibesql_ast::Statement::Select(select) => match select.from.as_ref().unwrap() {
vibesql_ast::FromClause::Join { join_type, natural, .. } => {
assert_eq!(*join_type, vibesql_ast::JoinType::LeftOuter);
assert!(*natural);
}
_ => panic!("Expected JOIN"),
},
_ => panic!("Expected SELECT"),
}
}
#[test]
fn test_parse_natural_cross_join() {
let result = Parser::parse_sql("SELECT * FROM t1 NATURAL CROSS JOIN t2;");
assert!(result.is_ok(), "Parse failed: {:?}", result.err());
let stmt = result.unwrap();
match stmt {
vibesql_ast::Statement::Select(select) => match select.from.as_ref().unwrap() {
vibesql_ast::FromClause::Join {
join_type, natural, condition, using_columns, ..
} => {
assert_eq!(*join_type, vibesql_ast::JoinType::Cross);
assert!(*natural);
assert!(condition.is_none());
assert!(using_columns.is_none());
}
_ => panic!("Expected JOIN"),
},
_ => panic!("Expected SELECT"),
}
}
#[test]
fn test_parse_natural_cross_join_complex() {
let result = Parser::parse_sql(
"SELECT DISTINCT t1.c0, t3.c0 FROM t2 NATURAL CROSS JOIN t1 RIGHT JOIN t3 ON t1.c0;",
);
assert!(result.is_ok(), "Parse failed: {:?}", result.err());
}
#[test]
fn test_parse_outer_left_natural_join() {
let result = Parser::parse_sql("SELECT * FROM t1 OUTER LEFT NATURAL JOIN t2;");
assert!(result.is_ok(), "Parse failed: {:?}", result.err());
let stmt = result.unwrap();
match stmt {
vibesql_ast::Statement::Select(select) => match select.from.as_ref().unwrap() {
vibesql_ast::FromClause::Join { join_type, natural, .. } => {
assert_eq!(*join_type, vibesql_ast::JoinType::LeftOuter);
assert!(*natural);
}
_ => panic!("Expected JOIN"),
},
_ => panic!("Expected SELECT"),
}
}
#[test]
fn test_parse_left_natural_join() {
let result = Parser::parse_sql("SELECT * FROM t1 LEFT NATURAL JOIN t2;");
assert!(result.is_ok(), "Parse failed: {:?}", result.err());
let stmt = result.unwrap();
match stmt {
vibesql_ast::Statement::Select(select) => match select.from.as_ref().unwrap() {
vibesql_ast::FromClause::Join { join_type, natural, .. } => {
assert_eq!(*join_type, vibesql_ast::JoinType::LeftOuter);
assert!(*natural);
}
_ => panic!("Expected JOIN"),
},
_ => panic!("Expected SELECT"),
}
}
#[test]
fn test_parse_left_outer_natural_join() {
let result = Parser::parse_sql("SELECT * FROM t1 LEFT OUTER NATURAL JOIN t2;");
assert!(result.is_ok(), "Parse failed: {:?}", result.err());
let stmt = result.unwrap();
match stmt {
vibesql_ast::Statement::Select(select) => match select.from.as_ref().unwrap() {
vibesql_ast::FromClause::Join { join_type, natural, .. } => {
assert_eq!(*join_type, vibesql_ast::JoinType::LeftOuter);
assert!(*natural);
}
_ => panic!("Expected JOIN"),
},
_ => panic!("Expected SELECT"),
}
}
#[test]
fn test_parse_natural_left_outer_join() {
let result = Parser::parse_sql("SELECT * FROM t1 NATURAL LEFT OUTER JOIN t2;");
assert!(result.is_ok(), "Parse failed: {:?}", result.err());
let stmt = result.unwrap();
match stmt {
vibesql_ast::Statement::Select(select) => match select.from.as_ref().unwrap() {
vibesql_ast::FromClause::Join { join_type, natural, .. } => {
assert_eq!(*join_type, vibesql_ast::JoinType::LeftOuter);
assert!(*natural);
}
_ => panic!("Expected JOIN"),
},
_ => panic!("Expected SELECT"),
}
}
#[test]
fn test_parse_legacy_comma_join_on() {
let result = Parser::parse_sql("SELECT * FROM t1, t2 ON t1.a = t2.b;");
assert!(result.is_ok(), "Parse failed: {:?}", result.err());
let stmt = result.unwrap();
match stmt {
vibesql_ast::Statement::Select(select) => match select.from.as_ref().unwrap() {
vibesql_ast::FromClause::Join { join_type, condition, natural, .. } => {
assert_eq!(*join_type, vibesql_ast::JoinType::Inner);
assert!(!*natural);
assert!(condition.is_some(), "Expected ON condition");
}
_ => panic!("Expected JOIN"),
},
_ => panic!("Expected SELECT"),
}
}
#[test]
fn test_parse_legacy_comma_join_on_multiple_tables() {
let result = Parser::parse_sql("SELECT * FROM t1, t2 ON t1.a = t2.b, t3 ON t2.c = t3.d;");
assert!(result.is_ok(), "Parse failed: {:?}", result.err());
let stmt = result.unwrap();
match stmt {
vibesql_ast::Statement::Select(select) => {
match select.from.as_ref().unwrap() {
vibesql_ast::FromClause::Join { join_type, left, condition, .. } => {
assert_eq!(*join_type, vibesql_ast::JoinType::Inner);
assert!(condition.is_some());
match left.as_ref() {
vibesql_ast::FromClause::Join {
join_type: inner_type,
condition: inner_cond,
..
} => {
assert_eq!(*inner_type, vibesql_ast::JoinType::Inner);
assert!(inner_cond.is_some());
}
_ => panic!("Expected nested JOIN"),
}
}
_ => panic!("Expected JOIN"),
}
}
_ => panic!("Expected SELECT"),
}
}
#[test]
fn test_parse_comma_without_on_still_cross_join() {
let result = Parser::parse_sql("SELECT * FROM t1, t2;");
assert!(result.is_ok(), "Parse failed: {:?}", result.err());
let stmt = result.unwrap();
match stmt {
vibesql_ast::Statement::Select(select) => match select.from.as_ref().unwrap() {
vibesql_ast::FromClause::Join { join_type, condition, .. } => {
assert_eq!(*join_type, vibesql_ast::JoinType::Cross);
assert!(condition.is_none());
}
_ => panic!("Expected JOIN"),
},
_ => panic!("Expected SELECT"),
}
}