@@ -1302,13 +1302,9 @@ impl<'a> Parser<'a> {
1302
1302
}
1303
1303
1304
1304
fn try_parse_expr_sub_query ( & mut self ) -> Result < Option < Expr > , ParserError > {
1305
- if self
1306
- . parse_one_of_keywords ( & [ Keyword :: SELECT , Keyword :: WITH ] )
1307
- . is_none ( )
1308
- {
1305
+ if !self . peek_sub_query ( ) {
1309
1306
return Ok ( None ) ;
1310
1307
}
1311
- self . prev_token ( ) ;
1312
1308
1313
1309
Ok ( Some ( Expr :: Subquery ( self . parse_boxed_query ( ) ?) ) )
1314
1310
}
@@ -1334,12 +1330,7 @@ impl<'a> Parser<'a> {
1334
1330
1335
1331
// Snowflake permits a subquery to be passed as an argument without
1336
1332
// an enclosing set of parens if it's the only argument.
1337
- if dialect_of ! ( self is SnowflakeDialect )
1338
- && self
1339
- . parse_one_of_keywords ( & [ Keyword :: WITH , Keyword :: SELECT ] )
1340
- . is_some ( )
1341
- {
1342
- self . prev_token ( ) ;
1333
+ if dialect_of ! ( self is SnowflakeDialect ) && self . peek_sub_query ( ) {
1343
1334
let subquery = self . parse_boxed_query ( ) ?;
1344
1335
self . expect_token ( & Token :: RParen ) ?;
1345
1336
return Ok ( Expr :: Function ( Function {
@@ -2639,10 +2630,21 @@ impl<'a> Parser<'a> {
2639
2630
} ;
2640
2631
2641
2632
if let Some ( op) = regular_binary_operator {
2642
- if let Some ( keyword) = self . parse_one_of_keywords ( & [ Keyword :: ANY , Keyword :: ALL ] ) {
2633
+ if let Some ( keyword) =
2634
+ self . parse_one_of_keywords ( & [ Keyword :: ANY , Keyword :: ALL , Keyword :: SOME ] )
2635
+ {
2643
2636
self . expect_token ( & Token :: LParen ) ?;
2644
- let right = self . parse_subexpr ( precedence) ?;
2645
- self . expect_token ( & Token :: RParen ) ?;
2637
+ let right = if self . peek_sub_query ( ) {
2638
+ // We have a subquery ahead (SELECT\WITH ...) need to rewind and
2639
+ // use the parenthesis for parsing the subquery as an expression.
2640
+ self . prev_token ( ) ; // LParen
2641
+ self . parse_subexpr ( precedence) ?
2642
+ } else {
2643
+ // Non-subquery expression
2644
+ let right = self . parse_subexpr ( precedence) ?;
2645
+ self . expect_token ( & Token :: RParen ) ?;
2646
+ right
2647
+ } ;
2646
2648
2647
2649
if !matches ! (
2648
2650
op,
@@ -2667,10 +2669,11 @@ impl<'a> Parser<'a> {
2667
2669
compare_op : op,
2668
2670
right : Box :: new ( right) ,
2669
2671
} ,
2670
- Keyword :: ANY => Expr :: AnyOp {
2672
+ Keyword :: ANY | Keyword :: SOME => Expr :: AnyOp {
2671
2673
left : Box :: new ( expr) ,
2672
2674
compare_op : op,
2673
2675
right : Box :: new ( right) ,
2676
+ is_some : keyword == Keyword :: SOME ,
2674
2677
} ,
2675
2678
_ => unreachable ! ( ) ,
2676
2679
} )
@@ -10507,11 +10510,7 @@ impl<'a> Parser<'a> {
10507
10510
vec ! [ ]
10508
10511
} ;
10509
10512
PivotValueSource :: Any ( order_by)
10510
- } else if self
10511
- . parse_one_of_keywords ( & [ Keyword :: SELECT , Keyword :: WITH ] )
10512
- . is_some ( )
10513
- {
10514
- self . prev_token ( ) ;
10513
+ } else if self . peek_sub_query ( ) {
10515
10514
PivotValueSource :: Subquery ( self . parse_query ( ) ?)
10516
10515
} else {
10517
10516
PivotValueSource :: List ( self . parse_comma_separated ( Self :: parse_expr_with_alias) ?)
@@ -12177,6 +12176,18 @@ impl<'a> Parser<'a> {
12177
12176
pub fn into_tokens ( self ) -> Vec < TokenWithLocation > {
12178
12177
self . tokens
12179
12178
}
12179
+
12180
+ /// Returns true if the next keyword indicates a sub query, i.e. SELECT or WITH
12181
+ fn peek_sub_query ( & mut self ) -> bool {
12182
+ if self
12183
+ . parse_one_of_keywords ( & [ Keyword :: SELECT , Keyword :: WITH ] )
12184
+ . is_some ( )
12185
+ {
12186
+ self . prev_token ( ) ;
12187
+ return true ;
12188
+ }
12189
+ false
12190
+ }
12180
12191
}
12181
12192
12182
12193
impl Word {
0 commit comments