Skip to content
Merged
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
25 changes: 24 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10289,8 +10289,31 @@ impl<'a> Parser<'a> {

Ok(ExprWithAlias { expr, alias })
}
/// Parses an expression with an optional alias

fn parse_expr_with_alias(&mut self) -> Result<ExprWithAlias, ParserError> {
/// Examples:

/// ```sql
/// SUM(price) AS total_price
/// ```

/// ```sql
/// SUM(price)
/// ```
///
/// Example
/// ```
/// # use sqlparser::parser::{Parser, ParserError};
/// # use sqlparser::dialect::GenericDialect;
/// # fn main() ->Result<(), ParserError> {
/// let sql = r#"SUM("a") as "b""#;
/// let mut parser = Parser::new(&GenericDialect).try_with_sql(sql)?;
/// let expr_with_alias = parser.parse_expr_with_alias()?;
/// assert_eq!(Some("b".to_string()), expr_with_alias.alias.map(|x|x.value));
/// # Ok(())
/// # }

pub fn parse_expr_with_alias(&mut self) -> Result<ExprWithAlias, ParserError> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please also add some documentation to this function (perhaps with an example or two) now that it will become part of the public API?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry for missing that piece of document, I have updated it.

let expr = self.parse_expr()?;
let alias = if self.parse_keyword(Keyword::AS) {
Some(self.parse_identifier(false)?)
Expand Down
Loading