From 6e46e95fb27a0a43ef09950c535547978992dd7c Mon Sep 17 00:00:00 2001 From: "Eason(G Ray)" <30045503+Eason0729@users.noreply.github.com> Date: Wed, 25 Sep 2024 14:37:09 +0800 Subject: [PATCH 1/3] chore: make `parse_expr_with_alias` public --- src/parser/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 5d57347cf..6ae1c5c83 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -10290,7 +10290,7 @@ impl<'a> Parser<'a> { Ok(ExprWithAlias { expr, alias }) } - fn parse_expr_with_alias(&mut self) -> Result { + pub fn parse_expr_with_alias(&mut self) -> Result { let expr = self.parse_expr()?; let alias = if self.parse_keyword(Keyword::AS) { Some(self.parse_identifier(false)?) From ec7d5b9ab5e386a17106c1bc92444242ce426567 Mon Sep 17 00:00:00 2001 From: "Eason(G Ray)" <30045503+Eason0729@users.noreply.github.com> Date: Fri, 27 Sep 2024 09:45:35 +0800 Subject: [PATCH 2/3] docs: Add example usage to `parse_expr_with_alias` --- src/parser/mod.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 6ae1c5c83..84cdc610b 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -10289,6 +10289,19 @@ impl<'a> Parser<'a> { Ok(ExprWithAlias { expr, alias }) } + /// return new expression with alias + /// + /// 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 { let expr = self.parse_expr()?; From 6eb46c10a01b77654853116203a63d5cdac1e04b Mon Sep 17 00:00:00 2001 From: Andrew Lamb Date: Sun, 29 Sep 2024 05:55:04 -0400 Subject: [PATCH 3/3] Update src/parser/mod.rs Co-authored-by: Ifeanyi Ubah --- src/parser/mod.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 84cdc610b..ba7e0f2c9 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -10289,7 +10289,17 @@ impl<'a> Parser<'a> { Ok(ExprWithAlias { expr, alias }) } - /// return new expression with alias + /// Parses an expression with an optional alias + + /// Examples: + + /// ```sql + /// SUM(price) AS total_price + /// ``` + + /// ```sql + /// SUM(price) + /// ``` /// /// Example /// ```