yoavcloud commented on code in PR #1467:
URL: 
https://github.com/apache/datafusion-sqlparser-rs/pull/1467#discussion_r1800036847


##########
src/parser/mod.rs:
##########
@@ -11173,12 +11173,36 @@ impl<'a> Parser<'a> {
                     self.peek_token().location
                 )
             }
-            expr => self
-                .parse_optional_alias(keywords::RESERVED_FOR_COLUMN_ALIAS)
-                .map(|alias| match alias {
-                    Some(alias) => SelectItem::ExprWithAlias { expr, alias },
-                    None => SelectItem::UnnamedExpr(expr),
-                }),
+            expr => {
+                // Parse a [`SelectItem`] based on an [MsSql] syntax that uses 
the equal sign
+                // to denote an alias, for example: SELECT col_alias = col 
FROM tbl
+                // [MsSql]: 
https://learn.microsoft.com/en-us/sql/t-sql/queries/select-examples-transact-sql?view=sql-server-ver16#b-use-select-with-column-headings-and-calculations
+                let expr = if self.dialect.supports_eq_alias_assigment() {
+                    if let Expr::BinaryOp {
+                        ref left,
+                        op: BinaryOperator::Eq,
+                        ref right,
+                    } = expr
+                    {
+                        if let Expr::Identifier(alias) = left.as_ref() {
+                            return Ok(SelectItem::ExprWithAlias {
+                                expr: *right.clone(),
+                                alias: alias.clone(),
+                            });
+                        }
+                    }
+                    expr
+                } else {
+                    expr
+                };
+
+                // Parse the common AS keyword for aliasing a column
+                self.parse_optional_alias(keywords::RESERVED_FOR_COLUMN_ALIAS)
+                    .map(|alias| match alias {
+                        Some(alias) => SelectItem::ExprWithAlias { expr, alias 
},
+                        None => SelectItem::UnnamedExpr(expr),
+                    })

Review Comment:
   I don't think you can match to `Expr::Identifier` in L11182 because `left` 
is a boxed field. This further complicates the logic of the function because 
you now need another `if let` condition to check the value of `left`. The 
complexity ends up similar to what I suggested.
   
   I suggest going back to the helper function design, it cleanly encapsulates 
this rather edgy case of MsSql without complicating the main flow of parsing 
the common syntax for aliasing a column.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org
For additional commands, e-mail: github-h...@datafusion.apache.org

Reply via email to