Skip to content

Commit cb7eeed

Browse files
committed
Try to inline the function based on code review feedback
1 parent 787d1a1 commit cb7eeed

File tree

3 files changed

+31
-25
lines changed

3 files changed

+31
-25
lines changed

src/dialect/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,11 @@ pub trait Dialect: Debug + Any {
561561
fn supports_asc_desc_in_column_definition(&self) -> bool {
562562
false
563563
}
564+
565+
/// For example: SELECT col_alias = col FROM tbl
566+
fn supports_eq_alias_assigment(&self) -> bool {
567+
false
568+
}
564569
}
565570

566571
/// This represents the operators for which precedence must be defined

src/dialect/mssql.rs

+4
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,8 @@ impl Dialect for MsSqlDialect {
4949
fn supports_connect_by(&self) -> bool {
5050
true
5151
}
52+
53+
fn supports_eq_alias_assigment(&self) -> bool {
54+
true
55+
}
5256
}

src/parser/mod.rs

+22-25
Original file line numberDiff line numberDiff line change
@@ -11174,11 +11174,29 @@ impl<'a> Parser<'a> {
1117411174
)
1117511175
}
1117611176
expr => {
11177-
if dialect_of!(self is MsSqlDialect) {
11178-
if let Some(select_item) = self.parse_mssql_alias_with_equal(&expr) {
11179-
return Ok(select_item);
11177+
// Parse a [`SelectItem`] based on an [MsSql] syntax that uses the equal sign
11178+
// to denote an alias, for example: SELECT col_alias = col FROM tbl
11179+
// [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
11180+
let expr = if self.dialect.supports_eq_alias_assigment() {
11181+
if let Expr::BinaryOp {
11182+
ref left,
11183+
op: BinaryOperator::Eq,
11184+
ref right,
11185+
} = expr
11186+
{
11187+
if let Expr::Identifier(alias) = left.as_ref() {
11188+
return Ok(SelectItem::ExprWithAlias {
11189+
expr: *right.clone(),
11190+
alias: alias.clone(),
11191+
});
11192+
}
1118011193
}
11181-
}
11194+
expr
11195+
} else {
11196+
expr
11197+
};
11198+
11199+
// Parse the common AS keyword for aliasing a column
1118211200
self.parse_optional_alias(keywords::RESERVED_FOR_COLUMN_ALIAS)
1118311201
.map(|alias| match alias {
1118411202
Some(alias) => SelectItem::ExprWithAlias { expr, alias },
@@ -11188,27 +11206,6 @@ impl<'a> Parser<'a> {
1118811206
}
1118911207
}
1119011208

11191-
/// Parse a [`SelectItem`] based on an MsSql syntax that uses the equal sign
11192-
/// to denote an alias, for example: SELECT col_alias = col FROM tbl
11193-
/// <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>
11194-
fn parse_mssql_alias_with_equal(&mut self, expr: &Expr) -> Option<SelectItem> {
11195-
if let Expr::BinaryOp {
11196-
left, op, right, ..
11197-
} = expr
11198-
{
11199-
if op == &BinaryOperator::Eq {
11200-
if let Expr::Identifier(ref alias) = **left {
11201-
return Some(SelectItem::ExprWithAlias {
11202-
expr: *right.clone(),
11203-
alias: alias.clone(),
11204-
});
11205-
}
11206-
}
11207-
}
11208-
11209-
None
11210-
}
11211-
1121211209
/// Parse an [`WildcardAdditionalOptions`] information for wildcard select items.
1121311210
///
1121411211
/// If it is not possible to parse it, will return an option.

0 commit comments

Comments
 (0)