Skip to content

Commit 1dd7d26

Browse files
authored
Add support for parsing MsSql alias with equals (#1467)
1 parent 7c20d4a commit 1dd7d26

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

src/dialect/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,17 @@ pub trait Dialect: Debug + Any {
561561
fn supports_asc_desc_in_column_definition(&self) -> bool {
562562
false
563563
}
564+
565+
/// Returns true if this dialect supports treating the equals operator `=` within a `SelectItem`
566+
/// as an alias assignment operator, rather than a boolean expression.
567+
/// For example: the following statements are equivalent for such a dialect:
568+
/// ```sql
569+
/// SELECT col_alias = col FROM tbl;
570+
/// SELECT col_alias AS col FROM tbl;
571+
/// ```
572+
fn supports_eq_alias_assigment(&self) -> bool {
573+
false
574+
}
564575
}
565576

566577
/// 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

+18
Original file line numberDiff line numberDiff line change
@@ -11181,6 +11181,24 @@ impl<'a> Parser<'a> {
1118111181
self.peek_token().location
1118211182
)
1118311183
}
11184+
Expr::BinaryOp {
11185+
left,
11186+
op: BinaryOperator::Eq,
11187+
right,
11188+
} if self.dialect.supports_eq_alias_assigment()
11189+
&& matches!(left.as_ref(), Expr::Identifier(_)) =>
11190+
{
11191+
let Expr::Identifier(alias) = *left else {
11192+
return parser_err!(
11193+
"BUG: expected identifier expression as alias",
11194+
self.peek_token().location
11195+
);
11196+
};
11197+
Ok(SelectItem::ExprWithAlias {
11198+
expr: *right,
11199+
alias,
11200+
})
11201+
}
1118411202
expr => self
1118511203
.parse_optional_alias(keywords::RESERVED_FOR_COLUMN_ALIAS)
1118611204
.map(|alias| match alias {

tests/sqlparser_common.rs

+17
Original file line numberDiff line numberDiff line change
@@ -11432,3 +11432,20 @@ fn test_any_some_all_comparison() {
1143211432
verified_stmt("SELECT c1 FROM tbl WHERE c1 <> SOME(SELECT c2 FROM tbl)");
1143311433
verified_stmt("SELECT 1 = ANY(WITH x AS (SELECT 1) SELECT * FROM x)");
1143411434
}
11435+
11436+
#[test]
11437+
fn test_alias_equal_expr() {
11438+
let dialects = all_dialects_where(|d| d.supports_eq_alias_assigment());
11439+
let sql = r#"SELECT some_alias = some_column FROM some_table"#;
11440+
let expected = r#"SELECT some_column AS some_alias FROM some_table"#;
11441+
let _ = dialects.one_statement_parses_to(sql, expected);
11442+
11443+
let sql = r#"SELECT some_alias = (a*b) FROM some_table"#;
11444+
let expected = r#"SELECT (a * b) AS some_alias FROM some_table"#;
11445+
let _ = dialects.one_statement_parses_to(sql, expected);
11446+
11447+
let dialects = all_dialects_where(|d| !d.supports_eq_alias_assigment());
11448+
let sql = r#"SELECT x = (a * b) FROM some_table"#;
11449+
let expected = r#"SELECT x = (a * b) FROM some_table"#;
11450+
let _ = dialects.one_statement_parses_to(sql, expected);
11451+
}

0 commit comments

Comments
 (0)