Skip to content

Commit 22182a2

Browse files
committed
Add support for qualified column names in JOIN ... USING
1 parent e9498d5 commit 22182a2

File tree

5 files changed

+34
-6
lines changed

5 files changed

+34
-6
lines changed

src/ast/query.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2050,7 +2050,7 @@ pub enum JoinOperator {
20502050
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
20512051
pub enum JoinConstraint {
20522052
On(Expr),
2053-
Using(Vec<Ident>),
2053+
Using(Vec<ObjectName>),
20542054
Natural,
20552055
None,
20562056
}

src/ast/spans.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2008,7 +2008,7 @@ impl Spanned for JoinConstraint {
20082008
fn span(&self) -> Span {
20092009
match self {
20102010
JoinConstraint::On(expr) => expr.span(),
2011-
JoinConstraint::Using(vec) => union_spans(vec.iter().map(|i| i.span)),
2011+
JoinConstraint::Using(vec) => union_spans(vec.iter().map(|i| i.span())),
20122012
JoinConstraint::Natural => Span::empty(),
20132013
JoinConstraint::None => Span::empty(),
20142014
}

src/parser/mod.rs

+28-3
Original file line numberDiff line numberDiff line change
@@ -9342,12 +9342,37 @@ impl<'a> Parser<'a> {
93429342
optional: IsOptional,
93439343
allow_empty: bool,
93449344
) -> Result<Vec<Ident>, ParserError> {
9345+
self.parse_parenthesized_column_list_inner(optional, allow_empty, |p| p.parse_identifier())
9346+
}
9347+
9348+
/// Parses a parenthesized comma-separated list of unqualified, possibly quoted identifiers
9349+
pub fn parse_parenthesized_qualified_column_list(
9350+
&mut self,
9351+
optional: IsOptional,
9352+
allow_empty: bool,
9353+
) -> Result<Vec<ObjectName>, ParserError> {
9354+
self.parse_parenthesized_column_list_inner(optional, allow_empty, |p| {
9355+
p.parse_object_name(false)
9356+
})
9357+
}
9358+
9359+
/// Parses a parenthesized comma-separated list of columns using
9360+
/// the provided function to parse each element.
9361+
fn parse_parenthesized_column_list_inner<F, T>(
9362+
&mut self,
9363+
optional: IsOptional,
9364+
allow_empty: bool,
9365+
mut f: F,
9366+
) -> Result<Vec<T>, ParserError>
9367+
where
9368+
F: FnMut(&mut Parser) -> Result<T, ParserError>,
9369+
{
93459370
if self.consume_token(&Token::LParen) {
93469371
if allow_empty && self.peek_token().token == Token::RParen {
93479372
self.next_token();
93489373
Ok(vec![])
93499374
} else {
9350-
let cols = self.parse_comma_separated(|p| p.parse_identifier())?;
9375+
let cols = self.parse_comma_separated(|p| f(p))?;
93519376
self.expect_token(&Token::RParen)?;
93529377
Ok(cols)
93539378
}
@@ -9358,7 +9383,7 @@ impl<'a> Parser<'a> {
93589383
}
93599384
}
93609385

9361-
/// Parse a parenthesized comma-separated list of table alias column definitions.
9386+
/// Parses a parenthesized comma-separated list of table alias column definitions.
93629387
fn parse_table_alias_column_defs(&mut self) -> Result<Vec<TableAliasColumnDef>, ParserError> {
93639388
if self.consume_token(&Token::LParen) {
93649389
let cols = self.parse_comma_separated(|p| {
@@ -11853,7 +11878,7 @@ impl<'a> Parser<'a> {
1185311878
let constraint = self.parse_expr()?;
1185411879
Ok(JoinConstraint::On(constraint))
1185511880
} else if self.parse_keyword(Keyword::USING) {
11856-
let columns = self.parse_parenthesized_column_list(Mandatory, false)?;
11881+
let columns = self.parse_parenthesized_qualified_column_list(Mandatory, false)?;
1185711882
Ok(JoinConstraint::Using(columns))
1185811883
} else {
1185911884
Ok(JoinConstraint::None)

tests/sqlparser_common.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6541,7 +6541,7 @@ fn parse_joins_using() {
65416541
sample: None,
65426542
},
65436543
global: false,
6544-
join_operator: f(JoinConstraint::Using(vec!["c1".into()])),
6544+
join_operator: f(JoinConstraint::Using(vec![ObjectName(vec!["c1".into()])])),
65456545
}
65466546
}
65476547
// Test parsing of aliases

tests/sqlparser_snowflake.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2650,6 +2650,9 @@ fn asof_joins() {
26502650
"ON s.state = p.state ",
26512651
"ORDER BY s.observed",
26522652
));
2653+
2654+
// Snowflake allows fully-qualified column names inside USING
2655+
snowflake().verified_stmt("SELECT * FROM tbl1 AS t1 JOIN tbl2 AS t2 USING(t2.col1)");
26532656
}
26542657

26552658
#[test]

0 commit comments

Comments
 (0)