Skip to content

Commit b68aae0

Browse files
romanbRoman Borschel
andauthored
Fix COLLATE parsing after compound identifiers (#2294)
Co-authored-by: Roman Borschel <roman@cluvio.com>
1 parent 1421b87 commit b68aae0

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

src/parser/mod.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,16 @@ impl<'a> Parser<'a> {
13971397

13981398
expr = self.parse_compound_expr(expr, vec![])?;
13991399

1400+
// Parse an optional collation cast operator following `expr`.
1401+
//
1402+
// For example (MSSQL): t1.a COLLATE Latin1_General_CI_AS
1403+
if !self.in_column_definition_state() && self.parse_keyword(Keyword::COLLATE) {
1404+
expr = Expr::Collate {
1405+
expr: Box::new(expr),
1406+
collation: self.parse_object_name(false)?,
1407+
};
1408+
}
1409+
14001410
debug!("prefix: {expr:?}");
14011411
loop {
14021412
let next_precedence = self.get_next_precedence()?;
@@ -1962,14 +1972,7 @@ impl<'a> Parser<'a> {
19621972
_ => self.expected_at("an expression", next_token_index),
19631973
}?;
19641974

1965-
if !self.in_column_definition_state() && self.parse_keyword(Keyword::COLLATE) {
1966-
Ok(Expr::Collate {
1967-
expr: Box::new(expr),
1968-
collation: self.parse_object_name(false)?,
1969-
})
1970-
} else {
1971-
Ok(expr)
1972-
}
1975+
Ok(expr)
19731976
}
19741977

19751978
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
@@ -2875,6 +2875,11 @@ fn parse_mssql_update_with_output_into() {
28752875
);
28762876
}
28772877

2878+
#[test]
2879+
fn test_collate_on_compound_identifier() {
2880+
ms_and_generic().verified_stmt("SELECT t1.a COLLATE Latin1_General_CI_AS FROM t1");
2881+
}
2882+
28782883
#[test]
28792884
fn parse_mssql_money_constants() {
28802885
ms().verified_only_select("SELECT CEILING($123.45)");

0 commit comments

Comments
 (0)