Skip to content

Commit 0a2c232

Browse files
committed
add alias for nestedjoin
1 parent 2a04212 commit 0a2c232

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

src/ast/query.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,10 @@ pub enum TableFactor {
375375
///
376376
/// The parser may also accept non-standard nesting of bare tables for some
377377
/// dialects, but the information about such nesting is stripped from AST.
378-
NestedJoin(Box<TableWithJoins>),
378+
NestedJoin {
379+
table_with_joins: Box<TableWithJoins>,
380+
alias: Option<TableAlias>,
381+
},
379382
}
380383

381384
impl fmt::Display for TableFactor {
@@ -438,7 +441,16 @@ impl fmt::Display for TableFactor {
438441
}
439442
Ok(())
440443
}
441-
TableFactor::NestedJoin(table_reference) => write!(f, "({})", table_reference),
444+
TableFactor::NestedJoin {
445+
table_with_joins,
446+
alias,
447+
} => {
448+
write!(f, "({})", table_with_joins)?;
449+
if let Some(alias) = alias {
450+
write!(f, " AS {}", alias)?;
451+
}
452+
Ok(())
453+
}
442454
}
443455
}
444456
}

src/parser.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3855,12 +3855,24 @@ impl<'a> Parser<'a> {
38553855
#[allow(clippy::if_same_then_else)]
38563856
if !table_and_joins.joins.is_empty() {
38573857
self.expect_token(&Token::RParen)?;
3858-
Ok(TableFactor::NestedJoin(Box::new(table_and_joins))) // (A)
3859-
} else if let TableFactor::NestedJoin(_) = &table_and_joins.relation {
3858+
let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
3859+
Ok(TableFactor::NestedJoin {
3860+
table_with_joins: Box::new(table_and_joins),
3861+
alias,
3862+
}) // (A)
3863+
} else if let TableFactor::NestedJoin {
3864+
table_with_joins: _,
3865+
alias: _,
3866+
} = &table_and_joins.relation
3867+
{
38603868
// (B): `table_and_joins` (what we found inside the parentheses)
38613869
// is a nested join `(foo JOIN bar)`, not followed by other joins.
38623870
self.expect_token(&Token::RParen)?;
3863-
Ok(TableFactor::NestedJoin(Box::new(table_and_joins)))
3871+
let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
3872+
Ok(TableFactor::NestedJoin {
3873+
table_with_joins: Box::new(table_and_joins),
3874+
alias,
3875+
})
38643876
} else if dialect_of!(self is SnowflakeDialect | GenericDialect) {
38653877
// Dialect-specific behavior: Snowflake diverges from the
38663878
// standard and from most of the other implementations by
@@ -3879,7 +3891,8 @@ impl<'a> Parser<'a> {
38793891
TableFactor::Derived { alias, .. }
38803892
| TableFactor::Table { alias, .. }
38813893
| TableFactor::UNNEST { alias, .. }
3882-
| TableFactor::TableFunction { alias, .. } => {
3894+
| TableFactor::TableFunction { alias, .. }
3895+
| TableFactor::NestedJoin { alias, .. } => {
38833896
// but not `FROM (mytable AS alias1) AS alias2`.
38843897
if let Some(inner_alias) = alias {
38853898
return Err(ParserError::ParserError(format!(
@@ -3892,7 +3905,6 @@ impl<'a> Parser<'a> {
38923905
// `(mytable AS alias)`
38933906
alias.replace(outer_alias);
38943907
}
3895-
TableFactor::NestedJoin(_) => unreachable!(),
38963908
};
38973909
}
38983910
// Do not store the extra set of parens in the AST

tests/sqlparser_common.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3676,7 +3676,7 @@ fn parse_derived_tables() {
36763676
let from = only(select.from);
36773677
assert_eq!(
36783678
from.relation,
3679-
TableFactor::NestedJoin(Box::new(TableWithJoins {
3679+
TableFactor::NestedJoin {table_with_joins: Box::new(TableWithJoins {
36803680
relation: TableFactor::Derived {
36813681
lateral: false,
36823682
subquery: Box::new(verified_query("(SELECT 1) UNION (SELECT 2)")),
@@ -3694,7 +3694,7 @@ fn parse_derived_tables() {
36943694
},
36953695
join_operator: JoinOperator::Inner(JoinConstraint::Natural),
36963696
}],
3697-
}))
3697+
}), alias: None}
36983698
);
36993699
}
37003700

tests/test_utils/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ pub use sqlparser::test_utils::*;
2626
#[macro_export]
2727
macro_rules! nest {
2828
($base:expr $(, $join:expr)*) => {
29-
TableFactor::NestedJoin(Box::new(TableWithJoins {
29+
TableFactor::NestedJoin { table_with_joins: Box::new(TableWithJoins {
3030
relation: $base,
3131
joins: vec![$(join($join)),*]
32-
}))
32+
}), alias: None}
3333
};
3434
}

0 commit comments

Comments
 (0)