mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-10-11 14:32:04 +00:00
Support aliasing columns
A table alias can specify new names for the columns within the aliased table, in addition to a new name for the table itself.
This commit is contained in:
parent
73ed685879
commit
9abcac350e
4 changed files with 60 additions and 14 deletions
|
@ -1208,6 +1208,23 @@ impl Parser {
|
|||
}
|
||||
}
|
||||
|
||||
/// Parse `AS identifier` when the AS is describing a table-valued object,
|
||||
/// like in `... FROM generate_series(1, 10) AS t (col)`. In this case
|
||||
/// the alias is allowed to optionally name the columns in the table, in
|
||||
/// addition to the table itself.
|
||||
pub fn parse_optional_table_alias(
|
||||
&mut self,
|
||||
reserved_kwds: &[&str],
|
||||
) -> Result<Option<TableAlias>, ParserError> {
|
||||
match self.parse_optional_alias(reserved_kwds)? {
|
||||
Some(name) => {
|
||||
let columns = self.parse_parenthesized_column_list(Optional)?;
|
||||
Ok(Some(TableAlias { name, columns }))
|
||||
}
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse one or more identifiers with the specified separator between them
|
||||
pub fn parse_list_of_ids(&mut self, separator: &Token) -> Result<Vec<SQLIdent>, ParserError> {
|
||||
let mut idents = vec![];
|
||||
|
@ -1491,7 +1508,7 @@ impl Parser {
|
|||
if self.consume_token(&Token::LParen) {
|
||||
let subquery = Box::new(self.parse_query()?);
|
||||
self.expect_token(&Token::RParen)?;
|
||||
let alias = self.parse_optional_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
|
||||
let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
|
||||
Ok(TableFactor::Derived {
|
||||
lateral,
|
||||
subquery,
|
||||
|
@ -1507,7 +1524,7 @@ impl Parser {
|
|||
} else {
|
||||
vec![]
|
||||
};
|
||||
let alias = self.parse_optional_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
|
||||
let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
|
||||
// MSSQL-specific table hints:
|
||||
let mut with_hints = vec![];
|
||||
if self.parse_keyword("WITH") {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue