@@ -151,12 +151,12 @@ impl Parser {
151
151
}
152
152
153
153
/// Parse a new expression
154
- pub fn parse_expr ( & mut self ) -> Result < ASTNode , ParserError > {
154
+ pub fn parse_expr ( & mut self ) -> Result < Expr , ParserError > {
155
155
self . parse_subexpr ( 0 )
156
156
}
157
157
158
158
/// Parse tokens until the precedence changes
159
- pub fn parse_subexpr ( & mut self , precedence : u8 ) -> Result < ASTNode , ParserError > {
159
+ pub fn parse_subexpr ( & mut self , precedence : u8 ) -> Result < Expr , ParserError > {
160
160
debug ! ( "parsing expr" ) ;
161
161
let mut expr = self . parse_prefix ( ) ?;
162
162
debug ! ( "prefix: {:?}" , expr) ;
@@ -173,7 +173,7 @@ impl Parser {
173
173
}
174
174
175
175
/// Parse an expression prefix
176
- pub fn parse_prefix ( & mut self ) -> Result < ASTNode , ParserError > {
176
+ pub fn parse_prefix ( & mut self ) -> Result < Expr , ParserError > {
177
177
let tok = self
178
178
. next_token ( )
179
179
. ok_or_else ( || ParserError :: ParserError ( "Unexpected EOF" . to_string ( ) ) ) ?;
@@ -185,16 +185,16 @@ impl Parser {
185
185
}
186
186
"CASE" => self . parse_case_expression ( ) ,
187
187
"CAST" => self . parse_cast_expression ( ) ,
188
- "DATE" => Ok ( ASTNode :: SQLValue ( Value :: Date ( self . parse_literal_string ( ) ?) ) ) ,
188
+ "DATE" => Ok ( Expr :: SQLValue ( Value :: Date ( self . parse_literal_string ( ) ?) ) ) ,
189
189
"EXISTS" => self . parse_exists_expression ( ) ,
190
190
"EXTRACT" => self . parse_extract_expression ( ) ,
191
191
"INTERVAL" => self . parse_literal_interval ( ) ,
192
- "NOT" => Ok ( ASTNode :: SQLUnaryOp {
192
+ "NOT" => Ok ( Expr :: SQLUnaryOp {
193
193
op : SQLUnaryOperator :: Not ,
194
194
expr : Box :: new ( self . parse_subexpr ( Self :: UNARY_NOT_PREC ) ?) ,
195
195
} ) ,
196
- "TIME" => Ok ( ASTNode :: SQLValue ( Value :: Time ( self . parse_literal_string ( ) ?) ) ) ,
197
- "TIMESTAMP" => Ok ( ASTNode :: SQLValue ( Value :: Timestamp (
196
+ "TIME" => Ok ( Expr :: SQLValue ( Value :: Time ( self . parse_literal_string ( ) ?) ) ) ,
197
+ "TIMESTAMP" => Ok ( Expr :: SQLValue ( Value :: Timestamp (
198
198
self . parse_literal_string ( ) ?,
199
199
) ) ) ,
200
200
// Here `w` is a word, check if it's a part of a multi-part
@@ -217,25 +217,25 @@ impl Parser {
217
217
}
218
218
}
219
219
if ends_with_wildcard {
220
- Ok ( ASTNode :: SQLQualifiedWildcard ( id_parts) )
220
+ Ok ( Expr :: SQLQualifiedWildcard ( id_parts) )
221
221
} else if self . consume_token ( & Token :: LParen ) {
222
222
self . prev_token ( ) ;
223
223
self . parse_function ( SQLObjectName ( id_parts) )
224
224
} else {
225
- Ok ( ASTNode :: SQLCompoundIdentifier ( id_parts) )
225
+ Ok ( Expr :: SQLCompoundIdentifier ( id_parts) )
226
226
}
227
227
}
228
- _ => Ok ( ASTNode :: SQLIdentifier ( w. as_sql_ident ( ) ) ) ,
228
+ _ => Ok ( Expr :: SQLIdentifier ( w. as_sql_ident ( ) ) ) ,
229
229
} ,
230
230
} , // End of Token::SQLWord
231
- Token :: Mult => Ok ( ASTNode :: SQLWildcard ) ,
231
+ Token :: Mult => Ok ( Expr :: SQLWildcard ) ,
232
232
tok @ Token :: Minus | tok @ Token :: Plus => {
233
233
let op = if tok == Token :: Plus {
234
234
SQLUnaryOperator :: Plus
235
235
} else {
236
236
SQLUnaryOperator :: Minus
237
237
} ;
238
- Ok ( ASTNode :: SQLUnaryOp {
238
+ Ok ( Expr :: SQLUnaryOp {
239
239
op,
240
240
expr : Box :: new ( self . parse_subexpr ( Self :: PLUS_MINUS_PREC ) ?) ,
241
241
} )
@@ -250,9 +250,9 @@ impl Parser {
250
250
Token :: LParen => {
251
251
let expr = if self . parse_keyword ( "SELECT" ) || self . parse_keyword ( "WITH" ) {
252
252
self . prev_token ( ) ;
253
- ASTNode :: SQLSubquery ( Box :: new ( self . parse_query ( ) ?) )
253
+ Expr :: SQLSubquery ( Box :: new ( self . parse_query ( ) ?) )
254
254
} else {
255
- ASTNode :: SQLNested ( Box :: new ( self . parse_expr ( ) ?) )
255
+ Expr :: SQLNested ( Box :: new ( self . parse_expr ( ) ?) )
256
256
} ;
257
257
self . expect_token ( & Token :: RParen ) ?;
258
258
Ok ( expr)
@@ -261,7 +261,7 @@ impl Parser {
261
261
} ?;
262
262
263
263
if self . parse_keyword ( "COLLATE" ) {
264
- Ok ( ASTNode :: SQLCollate {
264
+ Ok ( Expr :: SQLCollate {
265
265
expr : Box :: new ( expr) ,
266
266
collation : self . parse_object_name ( ) ?,
267
267
} )
@@ -270,7 +270,7 @@ impl Parser {
270
270
}
271
271
}
272
272
273
- pub fn parse_function ( & mut self , name : SQLObjectName ) -> Result < ASTNode , ParserError > {
273
+ pub fn parse_function ( & mut self , name : SQLObjectName ) -> Result < Expr , ParserError > {
274
274
self . expect_token ( & Token :: LParen ) ?;
275
275
let all = self . parse_keyword ( "ALL" ) ;
276
276
let distinct = self . parse_keyword ( "DISTINCT" ) ;
@@ -306,7 +306,7 @@ impl Parser {
306
306
None
307
307
} ;
308
308
309
- Ok ( ASTNode :: SQLFunction ( SQLFunction {
309
+ Ok ( Expr :: SQLFunction ( SQLFunction {
310
310
name,
311
311
args,
312
312
over,
@@ -366,7 +366,7 @@ impl Parser {
366
366
}
367
367
}
368
368
369
- pub fn parse_case_expression ( & mut self ) -> Result < ASTNode , ParserError > {
369
+ pub fn parse_case_expression ( & mut self ) -> Result < Expr , ParserError > {
370
370
let mut operand = None ;
371
371
if !self . parse_keyword ( "WHEN" ) {
372
372
operand = Some ( Box :: new ( self . parse_expr ( ) ?) ) ;
@@ -388,7 +388,7 @@ impl Parser {
388
388
None
389
389
} ;
390
390
self . expect_keyword ( "END" ) ?;
391
- Ok ( ASTNode :: SQLCase {
391
+ Ok ( Expr :: SQLCase {
392
392
operand,
393
393
conditions,
394
394
results,
@@ -397,33 +397,33 @@ impl Parser {
397
397
}
398
398
399
399
/// Parse a SQL CAST function e.g. `CAST(expr AS FLOAT)`
400
- pub fn parse_cast_expression ( & mut self ) -> Result < ASTNode , ParserError > {
400
+ pub fn parse_cast_expression ( & mut self ) -> Result < Expr , ParserError > {
401
401
self . expect_token ( & Token :: LParen ) ?;
402
402
let expr = self . parse_expr ( ) ?;
403
403
self . expect_keyword ( "AS" ) ?;
404
404
let data_type = self . parse_data_type ( ) ?;
405
405
self . expect_token ( & Token :: RParen ) ?;
406
- Ok ( ASTNode :: SQLCast {
406
+ Ok ( Expr :: SQLCast {
407
407
expr : Box :: new ( expr) ,
408
408
data_type,
409
409
} )
410
410
}
411
411
412
412
/// Parse a SQL EXISTS expression e.g. `WHERE EXISTS(SELECT ...)`.
413
- pub fn parse_exists_expression ( & mut self ) -> Result < ASTNode , ParserError > {
413
+ pub fn parse_exists_expression ( & mut self ) -> Result < Expr , ParserError > {
414
414
self . expect_token ( & Token :: LParen ) ?;
415
- let exists_node = ASTNode :: SQLExists ( Box :: new ( self . parse_query ( ) ?) ) ;
415
+ let exists_node = Expr :: SQLExists ( Box :: new ( self . parse_query ( ) ?) ) ;
416
416
self . expect_token ( & Token :: RParen ) ?;
417
417
Ok ( exists_node)
418
418
}
419
419
420
- pub fn parse_extract_expression ( & mut self ) -> Result < ASTNode , ParserError > {
420
+ pub fn parse_extract_expression ( & mut self ) -> Result < Expr , ParserError > {
421
421
self . expect_token ( & Token :: LParen ) ?;
422
422
let field = self . parse_date_time_field ( ) ?;
423
423
self . expect_keyword ( "FROM" ) ?;
424
424
let expr = self . parse_expr ( ) ?;
425
425
self . expect_token ( & Token :: RParen ) ?;
426
- Ok ( ASTNode :: SQLExtract {
426
+ Ok ( Expr :: SQLExtract {
427
427
field,
428
428
expr : Box :: new ( expr) ,
429
429
} )
@@ -462,7 +462,7 @@ impl Parser {
462
462
/// 6. `INTERVAL '1:1' HOUR (5) TO MINUTE (5)`
463
463
///
464
464
/// Note that we do not currently attempt to parse the quoted value.
465
- pub fn parse_literal_interval ( & mut self ) -> Result < ASTNode , ParserError > {
465
+ pub fn parse_literal_interval ( & mut self ) -> Result < Expr , ParserError > {
466
466
// The SQL standard allows an optional sign before the value string, but
467
467
// it is not clear if any implementations support that syntax, so we
468
468
// don't currently try to parse it. (The sign can instead be included
@@ -504,7 +504,7 @@ impl Parser {
504
504
}
505
505
} ;
506
506
507
- Ok ( ASTNode :: SQLValue ( Value :: Interval {
507
+ Ok ( Expr :: SQLValue ( Value :: Interval {
508
508
value,
509
509
leading_field,
510
510
leading_precision,
@@ -514,7 +514,7 @@ impl Parser {
514
514
}
515
515
516
516
/// Parse an operator following an expression
517
- pub fn parse_infix ( & mut self , expr : ASTNode , precedence : u8 ) -> Result < ASTNode , ParserError > {
517
+ pub fn parse_infix ( & mut self , expr : Expr , precedence : u8 ) -> Result < Expr , ParserError > {
518
518
debug ! ( "parsing infix" ) ;
519
519
let tok = self . next_token ( ) . unwrap ( ) ; // safe as EOF's precedence is the lowest
520
520
@@ -547,7 +547,7 @@ impl Parser {
547
547
} ;
548
548
549
549
if let Some ( op) = regular_binary_operator {
550
- Ok ( ASTNode :: SQLBinaryOp {
550
+ Ok ( Expr :: SQLBinaryOp {
551
551
left : Box :: new ( expr) ,
552
552
op,
553
553
right : Box :: new ( self . parse_subexpr ( precedence) ?) ,
@@ -556,9 +556,9 @@ impl Parser {
556
556
match k. keyword . as_ref ( ) {
557
557
"IS" => {
558
558
if self . parse_keyword ( "NULL" ) {
559
- Ok ( ASTNode :: SQLIsNull ( Box :: new ( expr) ) )
559
+ Ok ( Expr :: SQLIsNull ( Box :: new ( expr) ) )
560
560
} else if self . parse_keywords ( vec ! [ "NOT" , "NULL" ] ) {
561
- Ok ( ASTNode :: SQLIsNotNull ( Box :: new ( expr) ) )
561
+ Ok ( Expr :: SQLIsNotNull ( Box :: new ( expr) ) )
562
562
} else {
563
563
self . expected ( "NULL or NOT NULL after IS" , self . peek_token ( ) )
564
564
}
@@ -586,17 +586,17 @@ impl Parser {
586
586
}
587
587
588
588
/// Parses the parens following the `[ NOT ] IN` operator
589
- pub fn parse_in ( & mut self , expr : ASTNode , negated : bool ) -> Result < ASTNode , ParserError > {
589
+ pub fn parse_in ( & mut self , expr : Expr , negated : bool ) -> Result < Expr , ParserError > {
590
590
self . expect_token ( & Token :: LParen ) ?;
591
591
let in_op = if self . parse_keyword ( "SELECT" ) || self . parse_keyword ( "WITH" ) {
592
592
self . prev_token ( ) ;
593
- ASTNode :: SQLInSubquery {
593
+ Expr :: SQLInSubquery {
594
594
expr : Box :: new ( expr) ,
595
595
subquery : Box :: new ( self . parse_query ( ) ?) ,
596
596
negated,
597
597
}
598
598
} else {
599
- ASTNode :: SQLInList {
599
+ Expr :: SQLInList {
600
600
expr : Box :: new ( expr) ,
601
601
list : self . parse_expr_list ( ) ?,
602
602
negated,
@@ -607,13 +607,13 @@ impl Parser {
607
607
}
608
608
609
609
/// Parses `BETWEEN <low> AND <high>`, assuming the `BETWEEN` keyword was already consumed
610
- pub fn parse_between ( & mut self , expr : ASTNode , negated : bool ) -> Result < ASTNode , ParserError > {
610
+ pub fn parse_between ( & mut self , expr : Expr , negated : bool ) -> Result < Expr , ParserError > {
611
611
// Stop parsing subexpressions for <low> and <high> on tokens with
612
612
// precedence lower than that of `BETWEEN`, such as `AND`, `IS`, etc.
613
613
let low = self . parse_subexpr ( Self :: BETWEEN_PREC ) ?;
614
614
self . expect_keyword ( "AND" ) ?;
615
615
let high = self . parse_subexpr ( Self :: BETWEEN_PREC ) ?;
616
- Ok ( ASTNode :: SQLBetween {
616
+ Ok ( Expr :: SQLBetween {
617
617
expr : Box :: new ( expr) ,
618
618
negated,
619
619
low : Box :: new ( low) ,
@@ -622,8 +622,8 @@ impl Parser {
622
622
}
623
623
624
624
/// Parse a postgresql casting style which is in the form of `expr::datatype`
625
- pub fn parse_pg_cast ( & mut self , expr : ASTNode ) -> Result < ASTNode , ParserError > {
626
- Ok ( ASTNode :: SQLCast {
625
+ pub fn parse_pg_cast ( & mut self , expr : Expr ) -> Result < Expr , ParserError > {
626
+ Ok ( Expr :: SQLCast {
627
627
expr : Box :: new ( expr) ,
628
628
data_type : self . parse_data_type ( ) ?,
629
629
} )
@@ -1132,8 +1132,8 @@ impl Parser {
1132
1132
Ok ( values)
1133
1133
}
1134
1134
1135
- fn parse_sql_value ( & mut self ) -> Result < ASTNode , ParserError > {
1136
- Ok ( ASTNode :: SQLValue ( self . parse_value ( ) ?) )
1135
+ fn parse_sql_value ( & mut self ) -> Result < Expr , ParserError > {
1136
+ Ok ( Expr :: SQLValue ( self . parse_value ( ) ?) )
1137
1137
}
1138
1138
1139
1139
fn parse_tab_value ( & mut self ) -> Result < Vec < Option < String > > , ParserError > {
@@ -1826,8 +1826,8 @@ impl Parser {
1826
1826
}
1827
1827
1828
1828
/// Parse a comma-delimited list of SQL expressions
1829
- pub fn parse_expr_list ( & mut self ) -> Result < Vec < ASTNode > , ParserError > {
1830
- let mut expr_list: Vec < ASTNode > = vec ! [ ] ;
1829
+ pub fn parse_expr_list ( & mut self ) -> Result < Vec < Expr > , ParserError > {
1830
+ let mut expr_list: Vec < Expr > = vec ! [ ] ;
1831
1831
loop {
1832
1832
expr_list. push ( self . parse_expr ( ) ?) ;
1833
1833
if !self . consume_token ( & Token :: Comma ) {
@@ -1837,7 +1837,7 @@ impl Parser {
1837
1837
Ok ( expr_list)
1838
1838
}
1839
1839
1840
- pub fn parse_optional_args ( & mut self ) -> Result < Vec < ASTNode > , ParserError > {
1840
+ pub fn parse_optional_args ( & mut self ) -> Result < Vec < Expr > , ParserError > {
1841
1841
if self . consume_token ( & Token :: RParen ) {
1842
1842
Ok ( vec ! [ ] )
1843
1843
} else {
@@ -1852,18 +1852,18 @@ impl Parser {
1852
1852
let mut projections: Vec < SQLSelectItem > = vec ! [ ] ;
1853
1853
loop {
1854
1854
let expr = self . parse_expr ( ) ?;
1855
- if let ASTNode :: SQLWildcard = expr {
1855
+ if let Expr :: SQLWildcard = expr {
1856
1856
projections. push ( SQLSelectItem :: Wildcard ) ;
1857
- } else if let ASTNode :: SQLQualifiedWildcard ( prefix) = expr {
1857
+ } else if let Expr :: SQLQualifiedWildcard ( prefix) = expr {
1858
1858
projections. push ( SQLSelectItem :: QualifiedWildcard ( SQLObjectName ( prefix) ) ) ;
1859
1859
} else {
1860
1860
// `expr` is a regular SQL expression and can be followed by an alias
1861
1861
if let Some ( alias) =
1862
1862
self . parse_optional_alias ( keywords:: RESERVED_FOR_COLUMN_ALIAS ) ?
1863
1863
{
1864
- projections. push ( SQLSelectItem :: ExpressionWithAlias { expr, alias } ) ;
1864
+ projections. push ( SQLSelectItem :: ExprWithAlias { expr, alias } ) ;
1865
1865
} else {
1866
- projections. push ( SQLSelectItem :: UnnamedExpression ( expr) ) ;
1866
+ projections. push ( SQLSelectItem :: UnnamedExpr ( expr) ) ;
1867
1867
}
1868
1868
}
1869
1869
@@ -1897,20 +1897,20 @@ impl Parser {
1897
1897
}
1898
1898
1899
1899
/// Parse a LIMIT clause
1900
- pub fn parse_limit ( & mut self ) -> Result < Option < ASTNode > , ParserError > {
1900
+ pub fn parse_limit ( & mut self ) -> Result < Option < Expr > , ParserError > {
1901
1901
if self . parse_keyword ( "ALL" ) {
1902
1902
Ok ( None )
1903
1903
} else {
1904
1904
self . parse_literal_uint ( )
1905
- . map ( |n| Some ( ASTNode :: SQLValue ( Value :: Long ( n) ) ) )
1905
+ . map ( |n| Some ( Expr :: SQLValue ( Value :: Long ( n) ) ) )
1906
1906
}
1907
1907
}
1908
1908
1909
1909
/// Parse an OFFSET clause
1910
- pub fn parse_offset ( & mut self ) -> Result < ASTNode , ParserError > {
1910
+ pub fn parse_offset ( & mut self ) -> Result < Expr , ParserError > {
1911
1911
let value = self
1912
1912
. parse_literal_uint ( )
1913
- . map ( |n| ASTNode :: SQLValue ( Value :: Long ( n) ) ) ?;
1913
+ . map ( |n| Expr :: SQLValue ( Value :: Long ( n) ) ) ?;
1914
1914
self . expect_one_of_keywords ( & [ "ROW" , "ROWS" ] ) ?;
1915
1915
Ok ( value)
1916
1916
}
0 commit comments