@@ -66,6 +66,7 @@ impl<'a> SelectStatementParser for Parser<'a> {
66
66
from : self . parse_select_from_clause ( ) ?,
67
67
where_clause : self . parse_where_clause ( ) ?,
68
68
group_by : self . parse_group_by_clause ( ) ?,
69
+ having : self . parse_having_clause ( ) ?,
69
70
..Default :: default ( )
70
71
} ;
71
72
@@ -358,7 +359,11 @@ impl<'a> SelectStatementParser for Parser<'a> {
358
359
}
359
360
360
361
fn parse_having_clause ( & mut self ) -> Result < Option < Box < Expression > > , ParsingError > {
361
- todo ! ( )
362
+ if self . consume_as_keyword ( Keyword :: Having ) . is_ok ( ) {
363
+ Ok ( Some ( Box :: new ( self . parse_expression ( ) ?) ) )
364
+ } else {
365
+ Ok ( None )
366
+ }
362
367
}
363
368
}
364
369
@@ -423,6 +428,24 @@ mod test_utils {
423
428
..Default :: default ( )
424
429
} )
425
430
}
431
+
432
+ pub fn select_statement_with_having_clause ( having : Expression ) -> SelectStatementType {
433
+ SelectStatementType :: Select ( SelectStatement {
434
+ distinct_type : DistinctType :: None ,
435
+ columns : vec ! [ SelectItem :: Expression ( Expression :: Identifier (
436
+ Identifier :: Wildcard ,
437
+ ) ) ] ,
438
+ from : Some ( SelectFrom :: Table ( SelectFromTable {
439
+ table_id : Identifier :: Single ( "table_1" . to_string ( ) ) ,
440
+ alias : None ,
441
+ indexed_type : None ,
442
+ } ) ) ,
443
+ where_clause : None ,
444
+ group_by : None ,
445
+ having : Some ( Box :: new ( having) ) ,
446
+ ..Default :: default ( )
447
+ } )
448
+ }
426
449
}
427
450
428
451
#[ cfg( test) ]
@@ -1318,3 +1341,27 @@ mod test_select_group_by_clause {
1318
1341
) ;
1319
1342
}
1320
1343
}
1344
+
1345
+ #[ cfg( test) ]
1346
+ mod test_select_having_clause {
1347
+ use super :: test_utils:: select_statement_with_having_clause;
1348
+ use crate :: expression:: test_utils:: {
1349
+ binary_op_expression, identifier_expression, numeric_literal_expression,
1350
+ } ;
1351
+ use crate :: parser:: test_utils:: * ;
1352
+ use crate :: { BinaryOp , Statement } ;
1353
+
1354
+ #[ test]
1355
+ fn test_select_having_clause ( ) {
1356
+ let expected_statement = select_statement_with_having_clause ( binary_op_expression (
1357
+ BinaryOp :: GreaterThan ,
1358
+ identifier_expression ( & [ "col1" ] ) ,
1359
+ numeric_literal_expression ( "1" ) ,
1360
+ ) ) ;
1361
+
1362
+ run_sunny_day_test (
1363
+ "SELECT * FROM table_1 HAVING col1 > 1" ,
1364
+ Statement :: Select ( expected_statement) ,
1365
+ ) ;
1366
+ }
1367
+ }
0 commit comments