Skip to content

Nested alias #550

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 14 additions & 2 deletions src/ast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,10 @@ pub enum TableFactor {
///
/// The parser may also accept non-standard nesting of bare tables for some
/// dialects, but the information about such nesting is stripped from AST.
NestedJoin(Box<TableWithJoins>),
NestedJoin {
table_with_joins: Box<TableWithJoins>,
alias: Option<TableAlias>,
},
}

impl fmt::Display for TableFactor {
Expand Down Expand Up @@ -438,7 +441,16 @@ impl fmt::Display for TableFactor {
}
Ok(())
}
TableFactor::NestedJoin(table_reference) => write!(f, "({})", table_reference),
TableFactor::NestedJoin {
table_with_joins,
alias,
} => {
write!(f, "({})", table_with_joins)?;
if let Some(alias) = alias {
write!(f, " AS {}", alias)?;
}
Ok(())
}
}
}
}
Expand Down
22 changes: 17 additions & 5 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3855,12 +3855,24 @@ impl<'a> Parser<'a> {
#[allow(clippy::if_same_then_else)]
if !table_and_joins.joins.is_empty() {
self.expect_token(&Token::RParen)?;
Ok(TableFactor::NestedJoin(Box::new(table_and_joins))) // (A)
} else if let TableFactor::NestedJoin(_) = &table_and_joins.relation {
let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
Ok(TableFactor::NestedJoin {
table_with_joins: Box::new(table_and_joins),
alias,
}) // (A)
} else if let TableFactor::NestedJoin {
table_with_joins: _,
alias: _,
} = &table_and_joins.relation
{
// (B): `table_and_joins` (what we found inside the parentheses)
// is a nested join `(foo JOIN bar)`, not followed by other joins.
self.expect_token(&Token::RParen)?;
Ok(TableFactor::NestedJoin(Box::new(table_and_joins)))
let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
Ok(TableFactor::NestedJoin {
table_with_joins: Box::new(table_and_joins),
alias,
})
} else if dialect_of!(self is SnowflakeDialect | GenericDialect) {
// Dialect-specific behavior: Snowflake diverges from the
// standard and from most of the other implementations by
Expand All @@ -3879,7 +3891,8 @@ impl<'a> Parser<'a> {
TableFactor::Derived { alias, .. }
| TableFactor::Table { alias, .. }
| TableFactor::UNNEST { alias, .. }
| TableFactor::TableFunction { alias, .. } => {
| TableFactor::TableFunction { alias, .. }
| TableFactor::NestedJoin { alias, .. } => {
// but not `FROM (mytable AS alias1) AS alias2`.
if let Some(inner_alias) = alias {
return Err(ParserError::ParserError(format!(
Expand All @@ -3892,7 +3905,6 @@ impl<'a> Parser<'a> {
// `(mytable AS alias)`
alias.replace(outer_alias);
}
TableFactor::NestedJoin(_) => unreachable!(),
};
}
// Do not store the extra set of parens in the AST
Expand Down
39 changes: 21 additions & 18 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3676,25 +3676,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
}
);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/test_utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ pub use sqlparser::test_utils::*;
#[macro_export]
macro_rules! nest {
($base:expr $(, $join:expr)*) => {
TableFactor::NestedJoin(Box::new(TableWithJoins {
TableFactor::NestedJoin { table_with_joins: Box::new(TableWithJoins {
relation: $base,
joins: vec![$(join($join)),*]
}))
}), alias: None}
};
}