Skip to content

Commit 92ea0f4

Browse files
committed
Go back to helper function
1 parent cb7eeed commit 92ea0f4

File tree

4 files changed

+45
-34
lines changed

4 files changed

+45
-34
lines changed

src/dialect/mod.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,13 @@ pub trait Dialect: Debug + Any {
562562
false
563563
}
564564

565-
/// For example: SELECT col_alias = col FROM tbl
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+
/// ```
566572
fn supports_eq_alias_assigment(&self) -> bool {
567573
false
568574
}

src/parser/mod.rs

+26-22
Original file line numberDiff line numberDiff line change
@@ -11174,29 +11174,11 @@ impl<'a> Parser<'a> {
1117411174
)
1117511175
}
1117611176
expr => {
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-
}
11177+
if self.dialect.supports_eq_alias_assigment() {
11178+
if let Some(select_item) = Self::maybe_unpack_alias_assignment(&expr) {
11179+
return Ok(select_item);
1119311180
}
11194-
expr
11195-
} else {
11196-
expr
11197-
};
11198-
11199-
// Parse the common AS keyword for aliasing a column
11181+
}
1120011182
self.parse_optional_alias(keywords::RESERVED_FOR_COLUMN_ALIAS)
1120111183
.map(|alias| match alias {
1120211184
Some(alias) => SelectItem::ExprWithAlias { expr, alias },
@@ -12212,6 +12194,28 @@ impl<'a> Parser<'a> {
1221212194
}
1221312195
false
1221412196
}
12197+
12198+
/// Parse a [`SelectItem`] based on an [MsSql] syntax that uses the equal sign
12199+
/// to denote an alias, for example: SELECT col_alias = col FROM tbl
12200+
/// [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>
12201+
fn maybe_unpack_alias_assignment(expr: &Expr) -> Option<SelectItem> {
12202+
if let Expr::BinaryOp {
12203+
left,
12204+
op: BinaryOperator::Eq,
12205+
right,
12206+
..
12207+
} = expr
12208+
{
12209+
if let Expr::Identifier(ref alias) = **left {
12210+
return Some(SelectItem::ExprWithAlias {
12211+
expr: *right.clone(),
12212+
alias: alias.clone(),
12213+
});
12214+
}
12215+
}
12216+
12217+
None
12218+
}
1221512219
}
1221612220

1221712221
impl Word {

tests/sqlparser_common.rs

+12
Original file line numberDiff line numberDiff line change
@@ -11408,3 +11408,15 @@ fn test_any_some_all_comparison() {
1140811408
verified_stmt("SELECT c1 FROM tbl WHERE c1 <> SOME(SELECT c2 FROM tbl)");
1140911409
verified_stmt("SELECT 1 = ANY(WITH x AS (SELECT 1) SELECT * FROM x)");
1141011410
}
11411+
11412+
#[test]
11413+
fn test_alias_equal_expr() {
11414+
let dialects = all_dialects_where(|d| d.supports_eq_alias_assigment());
11415+
let sql = r#"SELECT some_alias = some_column FROM some_table"#;
11416+
let expected = r#"SELECT some_column AS some_alias FROM some_table"#;
11417+
let _ = dialects.one_statement_parses_to(sql, expected);
11418+
11419+
let sql = r#"SELECT some_alias = (a*b) FROM some_table"#;
11420+
let expected = r#"SELECT (a * b) AS some_alias FROM some_table"#;
11421+
let _ = dialects.one_statement_parses_to(sql, expected);
11422+
}

tests/sqlparser_mssql.rs

-11
Original file line numberDiff line numberDiff line change
@@ -1024,17 +1024,6 @@ fn parse_create_table_with_identity_column() {
10241024
}
10251025
}
10261026

1027-
#[test]
1028-
fn test_alias_equal_expr() {
1029-
let sql = r#"SELECT some_alias = some_column FROM some_table"#;
1030-
let expected = r#"SELECT some_column AS some_alias FROM some_table"#;
1031-
let _ = ms().one_statement_parses_to(sql, expected);
1032-
1033-
let sql = r#"SELECT some_alias = (a*b) FROM some_table"#;
1034-
let expected = r#"SELECT (a * b) AS some_alias FROM some_table"#;
1035-
let _ = ms().one_statement_parses_to(sql, expected);
1036-
}
1037-
10381027
fn ms() -> TestedDialects {
10391028
TestedDialects {
10401029
dialects: vec![Box::new(MsSqlDialect {})],

0 commit comments

Comments
 (0)