-
Notifications
You must be signed in to change notification settings - Fork 605
Support NestedJoin
with an alias
#551
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3446,32 +3446,45 @@ fn parse_joins_using() { | |
|
||
#[test] | ||
fn parse_natural_join() { | ||
fn natural_join(f: impl Fn(JoinConstraint) -> JoinOperator) -> Join { | ||
fn natural_join(f: impl Fn(JoinConstraint) -> JoinOperator, alias: Option<TableAlias>) -> Join { | ||
Join { | ||
relation: TableFactor::Table { | ||
name: ObjectName(vec![Ident::new("t2")]), | ||
alias: None, | ||
alias, | ||
args: None, | ||
with_hints: vec![], | ||
}, | ||
join_operator: f(JoinConstraint::Natural), | ||
} | ||
} | ||
|
||
// if not specified, inner join as default | ||
assert_eq!( | ||
only(&verified_only_select("SELECT * FROM t1 NATURAL JOIN t2").from).joins, | ||
vec![natural_join(JoinOperator::Inner)] | ||
vec![natural_join(JoinOperator::Inner, None)] | ||
); | ||
// left join explicitly | ||
assert_eq!( | ||
only(&verified_only_select("SELECT * FROM t1 NATURAL LEFT JOIN t2").from).joins, | ||
vec![natural_join(JoinOperator::LeftOuter)] | ||
vec![natural_join(JoinOperator::LeftOuter, None)] | ||
); | ||
|
||
// right join explicitly | ||
assert_eq!( | ||
only(&verified_only_select("SELECT * FROM t1 NATURAL RIGHT JOIN t2").from).joins, | ||
vec![natural_join(JoinOperator::RightOuter)] | ||
vec![natural_join(JoinOperator::RightOuter, None)] | ||
); | ||
|
||
// full join explicitly | ||
assert_eq!( | ||
only(&verified_only_select("SELECT * FROM t1 NATURAL FULL JOIN t2").from).joins, | ||
vec![natural_join(JoinOperator::FullOuter)] | ||
vec![natural_join(JoinOperator::FullOuter, None)] | ||
); | ||
|
||
// natural join another table with alias | ||
assert_eq!( | ||
only(&verified_only_select("SELECT * FROM t1 NATURAL JOIN t2 AS t3").from).joins, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is an interesting one -- it adds the alias to the entire Is that what other implementations do? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it alias There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. perhaps i should delete this test? i thought at we need to protect the case that it incorrectly alias the whole natural join to t3 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is a good test and that we should leave it in |
||
vec![natural_join(JoinOperator::Inner, table_alias("t3"))] | ||
); | ||
|
||
let sql = "SELECT * FROM t1 natural"; | ||
|
@@ -3519,6 +3532,21 @@ fn parse_join_nesting() { | |
from.joins, | ||
vec![join(nest!(nest!(nest!(table("b"), table("c")))))] | ||
); | ||
|
||
let sql = "SELECT * FROM (a NATURAL JOIN b) AS c"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this makes sense to me 👍 |
||
let select = verified_only_select(sql); | ||
let from = only(select.from); | ||
assert_eq!( | ||
from.relation, | ||
TableFactor::NestedJoin { | ||
table_with_joins: Box::new(TableWithJoins { | ||
relation: table("a"), | ||
joins: vec![join(table("b"))], | ||
}), | ||
alias: table_alias("c") | ||
} | ||
); | ||
assert_eq!(from.joins, vec![]); | ||
} | ||
|
||
#[test] | ||
|
@@ -3676,25 +3704,28 @@ fn parse_derived_tables() { | |
let from = only(select.from); | ||
assert_eq!( | ||
from.relation, | ||
TableFactor::NestedJoin(Box::new(TableWithJoins { | ||
relation: TableFactor::Derived { | ||
lateral: false, | ||
subquery: Box::new(verified_query("(SELECT 1) UNION (SELECT 2)")), | ||
alias: Some(TableAlias { | ||
name: "t1".into(), | ||
columns: vec![], | ||
}) | ||
}, | ||
joins: vec![Join { | ||
relation: TableFactor::Table { | ||
name: ObjectName(vec!["t2".into()]), | ||
alias: None, | ||
args: None, | ||
with_hints: vec![], | ||
TableFactor::NestedJoin { | ||
table_with_joins: Box::new(TableWithJoins { | ||
relation: TableFactor::Derived { | ||
lateral: false, | ||
subquery: Box::new(verified_query("(SELECT 1) UNION (SELECT 2)")), | ||
alias: Some(TableAlias { | ||
name: "t1".into(), | ||
columns: vec![], | ||
}) | ||
}, | ||
join_operator: JoinOperator::Inner(JoinConstraint::Natural), | ||
}], | ||
})) | ||
joins: vec![Join { | ||
relation: TableFactor::Table { | ||
name: ObjectName(vec!["t2".into()]), | ||
alias: None, | ||
args: None, | ||
with_hints: vec![], | ||
}, | ||
join_operator: JoinOperator::Inner(JoinConstraint::Natural), | ||
}], | ||
}), | ||
alias: None | ||
} | ||
); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,10 +131,9 @@ fn test_single_table_in_parenthesis_with_alias() { | |
"SELECT * FROM (a AS alias1 NATURAL JOIN b AS c)", | ||
); | ||
|
||
let res = snowflake_and_generic().parse_sql_statements("SELECT * FROM (a NATURAL JOIN b) c"); | ||
assert_eq!( | ||
ParserError::ParserError("Expected end of statement, found: c".to_string()), | ||
res.unwrap_err() | ||
snowflake_and_generic().one_statement_parses_to( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice |
||
"SELECT * FROM (a NATURAL JOIN b) c", | ||
"SELECT * FROM (a NATURAL JOIN b) AS c", | ||
); | ||
|
||
let res = snowflake().parse_sql_statements("SELECT * FROM (a b) c"); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍