@@ -3534,7 +3534,9 @@ impl<'a> Parser<'a> {
3534
3534
pub fn parse_all_or_distinct ( & mut self ) -> Result < Option < Distinct > , ParserError > {
3535
3535
let loc = self . peek_token ( ) . location ;
3536
3536
let all = self . parse_keyword ( Keyword :: ALL ) ;
3537
- let distinct = self . parse_keyword ( Keyword :: DISTINCT ) ;
3537
+ let distinct = self
3538
+ . parse_one_of_keywords ( & [ Keyword :: DISTINCT , Keyword :: DISTINCTROW ] )
3539
+ . is_some ( ) ;
3538
3540
if !distinct {
3539
3541
return Ok ( None ) ;
3540
3542
}
@@ -9174,12 +9176,14 @@ impl<'a> Parser<'a> {
9174
9176
None
9175
9177
} ;
9176
9178
9177
- let distinct = self . parse_all_or_distinct ( ) ? ;
9178
-
9179
- let top = if self . parse_keyword ( Keyword :: TOP ) {
9180
- Some ( self . parse_top ( ) ? )
9179
+ let ( distinct, top ) = if self . dialect . expects_top_before_distinct ( ) {
9180
+ let top = self . maybe_parse_top ( true ) ? ;
9181
+ let distinct = self . parse_all_or_distinct ( ) ? ;
9182
+ ( distinct , top )
9181
9183
} else {
9182
- None
9184
+ let distinct = self . parse_all_or_distinct ( ) ?;
9185
+ let top = self . maybe_parse_top ( false ) ?;
9186
+ ( distinct, top)
9183
9187
} ;
9184
9188
9185
9189
let projection = self . parse_projection ( ) ?;
@@ -11491,7 +11495,14 @@ impl<'a> Parser<'a> {
11491
11495
11492
11496
/// Parse a TOP clause, MSSQL equivalent of LIMIT,
11493
11497
/// that follows after `SELECT [DISTINCT]`.
11494
- pub fn parse_top ( & mut self ) -> Result < Top , ParserError > {
11498
+ pub fn maybe_parse_top (
11499
+ & mut self ,
11500
+ is_before_distinct : bool ,
11501
+ ) -> Result < Option < Top > , ParserError > {
11502
+ if !self . parse_keyword ( Keyword :: TOP ) {
11503
+ return Ok ( None ) ;
11504
+ }
11505
+
11495
11506
let quantity = if self . consume_token ( & Token :: LParen ) {
11496
11507
let quantity = self . parse_expr ( ) ?;
11497
11508
self . expect_token ( & Token :: RParen ) ?;
@@ -11509,11 +11520,12 @@ impl<'a> Parser<'a> {
11509
11520
11510
11521
let with_ties = self . parse_keywords ( & [ Keyword :: WITH , Keyword :: TIES ] ) ;
11511
11522
11512
- Ok ( Top {
11523
+ Ok ( Some ( Top {
11513
11524
with_ties,
11514
11525
percent,
11515
11526
quantity,
11516
- } )
11527
+ is_before_distinct,
11528
+ } ) )
11517
11529
}
11518
11530
11519
11531
/// Parse a LIMIT clause
0 commit comments