Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11695,6 +11695,11 @@ impl<'a> Parser<'a> {
// Note that for keywords to be properly handled here, they need to be
// added to `RESERVED_FOR_TABLE_ALIAS`, otherwise they may be parsed as
// a table alias.
let joins = self.parse_joins()?;
Ok(TableWithJoins { relation, joins })
}

fn parse_joins(&mut self) -> Result<Vec<Join>, ParserError> {
let mut joins = vec![];
loop {
let global = self.parse_keyword(Keyword::GLOBAL);
Expand Down Expand Up @@ -11823,7 +11828,16 @@ impl<'a> Parser<'a> {
}
_ => break,
};
let relation = self.parse_table_factor()?;
let mut relation = self.parse_table_factor()?;

if self.is_next_token_a_join() {
let joins = self.parse_joins()?;
relation = TableFactor::NestedJoin {
table_with_joins: Box::new(TableWithJoins { relation, joins }),
alias: None,
};
}

let join_constraint = self.parse_join_constraint(natural)?;
Join {
relation,
Expand All @@ -11833,7 +11847,21 @@ impl<'a> Parser<'a> {
};
joins.push(join);
}
Ok(TableWithJoins { relation, joins })
Ok(joins)
}

fn is_next_token_a_join(&self) -> bool {
matches!(
self.peek_token().token,
Token::Word(Word {
keyword: Keyword::JOIN
| Keyword::INNER
| Keyword::LEFT
| Keyword::RIGHT
| Keyword::FULL,
..
})
)
}

/// A table name or a parenthesized subquery, followed by optional `[AS] alias`
Expand Down
84 changes: 84 additions & 0 deletions tests/sqlparser_snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3516,3 +3516,87 @@ fn test_alter_session_followed_by_statement() {
_ => panic!("Unexpected statements: {:?}", stmts),
}
}

#[test]
fn test_nested_join_without_parentheses() {
let query = "SELECT DISTINCT p.product_id FROM orders AS o INNER JOIN customers AS c INNER JOIN products AS p ON p.customer_id = c.customer_id ON c.order_id = o.order_id";
assert_eq!(
only(
snowflake()
.verified_only_select_with_canonical(query, "")
.from
)
.joins,
vec![Join {
relation: TableFactor::NestedJoin {
table_with_joins: Box::new(TableWithJoins {
relation: TableFactor::Table {
name: ObjectName::from(vec![Ident::new("customers".to_string())]),
alias: Some(TableAlias {
name: Ident {
value: "c".to_string(),
quote_style: None,
span: Span::empty(),
},
columns: vec![],
}),
args: None,
with_hints: vec![],
version: None,
partitions: vec![],
with_ordinality: false,
json_path: None,
sample: None,
index_hints: vec![],
},
joins: vec![Join {
relation: TableFactor::Table {
name: ObjectName::from(vec![Ident::new("products".to_string())]),
alias: Some(TableAlias {
name: Ident {
value: "p".to_string(),
quote_style: None,
span: Span::empty(),
},
columns: vec![],
}),
args: None,
with_hints: vec![],
version: None,
partitions: vec![],
with_ordinality: false,
json_path: None,
sample: None,
index_hints: vec![],
},
global: false,
join_operator: JoinOperator::Inner(JoinConstraint::On(Expr::BinaryOp {
left: Box::new(Expr::CompoundIdentifier(vec![
Ident::new("p".to_string()),
Ident::new("customer_id".to_string())
])),
op: BinaryOperator::Eq,
right: Box::new(Expr::CompoundIdentifier(vec![
Ident::new("c".to_string()),
Ident::new("customer_id".to_string())
])),
})),
}]
}),
alias: None
},
global: false,
join_operator: JoinOperator::Inner(JoinConstraint::On(Expr::BinaryOp {
left: Box::new(Expr::CompoundIdentifier(vec![
Ident::new("c".to_string()),
Ident::new("order_id".to_string())
])),
op: BinaryOperator::Eq,
right: Box::new(Expr::CompoundIdentifier(vec![
Ident::new("o".to_string()),
Ident::new("order_id".to_string())
])),
}))
}],
);
}