Skip to content

Add support for qualified column names in JOIN ... USING #1663

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

Merged
merged 2 commits into from
Jan 19, 2025
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
2 changes: 1 addition & 1 deletion src/ast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2050,7 +2050,7 @@ pub enum JoinOperator {
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum JoinConstraint {
On(Expr),
Using(Vec<Ident>),
Using(Vec<ObjectName>),
Natural,
None,
}
Expand Down
2 changes: 1 addition & 1 deletion src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2008,7 +2008,7 @@ impl Spanned for JoinConstraint {
fn span(&self) -> Span {
match self {
JoinConstraint::On(expr) => expr.span(),
JoinConstraint::Using(vec) => union_spans(vec.iter().map(|i| i.span)),
JoinConstraint::Using(vec) => union_spans(vec.iter().map(|i| i.span())),
JoinConstraint::Natural => Span::empty(),
JoinConstraint::None => Span::empty(),
}
Expand Down
31 changes: 28 additions & 3 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9342,12 +9342,37 @@ impl<'a> Parser<'a> {
optional: IsOptional,
allow_empty: bool,
) -> Result<Vec<Ident>, ParserError> {
self.parse_parenthesized_column_list_inner(optional, allow_empty, |p| p.parse_identifier())
}

/// Parses a parenthesized comma-separated list of unqualified, possibly quoted identifiers
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The doc comment doesn't seem to match the behavior (unqualified vs qualified)? also it could be nice if the doc highlights that the parenthesis is also expected in the input (maybe with an example)

pub fn parse_parenthesized_qualified_column_list(
&mut self,
optional: IsOptional,
allow_empty: bool,
) -> Result<Vec<ObjectName>, ParserError> {
self.parse_parenthesized_column_list_inner(optional, allow_empty, |p| {
p.parse_object_name(false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the in_table_clause be configurable here? I thinking it's supposed to be set to true when parsing a USING clause given the behavior being fixed

})
}

/// Parses a parenthesized comma-separated list of columns using
/// the provided function to parse each element.
fn parse_parenthesized_column_list_inner<F, T>(
&mut self,
optional: IsOptional,
allow_empty: bool,
mut f: F,
) -> Result<Vec<T>, ParserError>
where
F: FnMut(&mut Parser) -> Result<T, ParserError>,
{
if self.consume_token(&Token::LParen) {
if allow_empty && self.peek_token().token == Token::RParen {
self.next_token();
Ok(vec![])
} else {
let cols = self.parse_comma_separated(|p| p.parse_identifier())?;
let cols = self.parse_comma_separated(|p| f(p))?;
self.expect_token(&Token::RParen)?;
Ok(cols)
}
Expand All @@ -9358,7 +9383,7 @@ impl<'a> Parser<'a> {
}
}

/// Parse a parenthesized comma-separated list of table alias column definitions.
/// Parses a parenthesized comma-separated list of table alias column definitions.
fn parse_table_alias_column_defs(&mut self) -> Result<Vec<TableAliasColumnDef>, ParserError> {
if self.consume_token(&Token::LParen) {
let cols = self.parse_comma_separated(|p| {
Expand Down Expand Up @@ -11853,7 +11878,7 @@ impl<'a> Parser<'a> {
let constraint = self.parse_expr()?;
Ok(JoinConstraint::On(constraint))
} else if self.parse_keyword(Keyword::USING) {
let columns = self.parse_parenthesized_column_list(Mandatory, false)?;
let columns = self.parse_parenthesized_qualified_column_list(Mandatory, false)?;
Ok(JoinConstraint::Using(columns))
} else {
Ok(JoinConstraint::None)
Expand Down
2 changes: 1 addition & 1 deletion tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6541,7 +6541,7 @@ fn parse_joins_using() {
sample: None,
},
global: false,
join_operator: f(JoinConstraint::Using(vec!["c1".into()])),
join_operator: f(JoinConstraint::Using(vec![ObjectName(vec!["c1".into()])])),
}
}
// Test parsing of aliases
Expand Down
3 changes: 3 additions & 0 deletions tests/sqlparser_snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2650,6 +2650,9 @@ fn asof_joins() {
"ON s.state = p.state ",
"ORDER BY s.observed",
));

// Snowflake allows fully-qualified column names inside USING
snowflake().verified_stmt("SELECT * FROM tbl1 AS t1 JOIN tbl2 AS t2 USING(t2.col1)");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can add this scenario to the parse_joins_using test in common.rs instead?

}

#[test]
Expand Down
Loading