Skip to content

Commit

Permalink
Try to inline the function based on code review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
yoavcloud committed Oct 14, 2024
1 parent 787d1a1 commit cb7eeed
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
5 changes: 5 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,11 @@ pub trait Dialect: Debug + Any {
fn supports_asc_desc_in_column_definition(&self) -> bool {
false
}

/// For example: SELECT col_alias = col FROM tbl
fn supports_eq_alias_assigment(&self) -> bool {
false
}
}

/// This represents the operators for which precedence must be defined
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,8 @@ impl Dialect for MsSqlDialect {
fn supports_connect_by(&self) -> bool {
true
}

fn supports_eq_alias_assigment(&self) -> bool {
true
}
}
47 changes: 22 additions & 25 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11174,11 +11174,29 @@ impl<'a> Parser<'a> {
)
}
expr => {
if dialect_of!(self is MsSqlDialect) {
if let Some(select_item) = self.parse_mssql_alias_with_equal(&expr) {
return Ok(select_item);
// 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 },
Expand All @@ -11188,27 +11206,6 @@ impl<'a> Parser<'a> {
}
}

/// 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
/// <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>
fn parse_mssql_alias_with_equal(&mut self, expr: &Expr) -> Option<SelectItem> {
if let Expr::BinaryOp {
left, op, right, ..
} = expr
{
if op == &BinaryOperator::Eq {
if let Expr::Identifier(ref alias) = **left {
return Some(SelectItem::ExprWithAlias {
expr: *right.clone(),
alias: alias.clone(),
});
}
}
}

None
}

/// Parse an [`WildcardAdditionalOptions`] information for wildcard select items.
///
/// If it is not possible to parse it, will return an option.
Expand Down

0 comments on commit cb7eeed

Please sign in to comment.