Skip to content

Commit c7d8c33

Browse files
author
Roman Borschel
committed
Fix COLLATE parsing after compound identifiers
Move the COLLATE parsing from the end of `parse_prefix` to `parse_subexpr` after `parse_compound_expr`, so that `expr COLLATE collation` is handled correctly when `expr` is a compound identifier (e.g. `t1.col COLLATE Latin1_General_CI_AS`).
1 parent 913cf0e commit c7d8c33

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

src/parser/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,6 +1394,13 @@ impl<'a> Parser<'a> {
13941394

13951395
expr = self.parse_compound_expr(expr, vec![])?;
13961396

1397+
if !self.in_column_definition_state() && self.parse_keyword(Keyword::COLLATE) {
1398+
expr = Expr::Collate {
1399+
expr: Box::new(expr),
1400+
collation: self.parse_object_name(false)?,
1401+
};
1402+
}
1403+
13971404
debug!("prefix: {expr:?}");
13981405
loop {
13991406
let next_precedence = self.get_next_precedence()?;
@@ -1935,14 +1942,7 @@ impl<'a> Parser<'a> {
19351942
_ => self.expected_at("an expression", next_token_index),
19361943
}?;
19371944

1938-
if !self.in_column_definition_state() && self.parse_keyword(Keyword::COLLATE) {
1939-
Ok(Expr::Collate {
1940-
expr: Box::new(expr),
1941-
collation: self.parse_object_name(false)?,
1942-
})
1943-
} else {
1944-
Ok(expr)
1945-
}
1945+
Ok(expr)
19461946
}
19471947

19481948
fn parse_geometric_type(&mut self, kind: GeometricTypeKind) -> Result<Expr, ParserError> {

tests/sqlparser_mssql.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2860,3 +2860,8 @@ fn parse_mssql_update_with_output_into() {
28602860
"UPDATE employees SET salary = salary * 1.1 OUTPUT INSERTED.id, DELETED.salary, INSERTED.salary INTO @changes WHERE department = 'Engineering'",
28612861
);
28622862
}
2863+
2864+
#[test]
2865+
fn test_collate_on_compound_identifier() {
2866+
tsql().verified_stmt("SELECT t1.a COLLATE Latin1_General_CI_AS FROM t1");
2867+
}

0 commit comments

Comments
 (0)