iffyio commented on code in PR #1467:
URL:
https://github.com/apache/datafusion-sqlparser-rs/pull/1467#discussion_r1801494903
##########
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:
this works as its own match clause for example:
```rust
Expr::BinaryOp {
left,
op: BinaryOperator::Eq,
right,
} if self.dialect.supports_eq_alias_assigment() &&
matches!(left.as_ref(), Expr::Identifier(_)) => {
let Expr::Identifier(alias) = *left else {
return parser_err!(
"BUG: expected identifier expression as alias",
self.peek_token().location
);
};
Ok(SelectItem::ExprWithAlias {
expr: *right,
alias,
})
}
```
I think the least desirable option involves the expression cloning, i.e. the
`right.clone()` I think we want to avoid in any case
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]