1
1
use crate :: {
2
2
BetweenFrameSpec , BetweenFrameSpecType , Expression , FrameSpec , FrameSpecExclude , FrameSpecType ,
3
3
FrameType , Function , FunctionArg , FunctionArgType , Identifier , Keyword , NullsOrdering ,
4
- Ordering , OrderingTerm , Parser , ParsingError , TokenType , WindowDefinition ,
4
+ Ordering , OrderingTerm , OverClause , Parser , ParsingError , TokenType , WindowDefinition ,
5
5
} ;
6
6
7
7
use super :: ExpressionParser ;
@@ -20,7 +20,7 @@ pub trait FunctionParser {
20
20
fn parse_function_filter_clause ( & mut self ) -> Result < Expression , ParsingError > ;
21
21
22
22
/// Parse a function over clause
23
- fn parse_function_over_clause ( & mut self ) -> Result < WindowDefinition , ParsingError > ;
23
+ fn parse_window_definition ( & mut self ) -> Result < WindowDefinition , ParsingError > ;
24
24
25
25
/// Parse a frame spec
26
26
fn parse_function_over_clause_frame_spec ( & mut self ) -> Result < FrameSpec , ParsingError > ;
@@ -56,7 +56,14 @@ impl<'a> FunctionParser for Parser<'a> {
56
56
57
57
if let Ok ( Keyword :: Over ) = self . peek_as_keyword ( ) {
58
58
self . consume_as_keyword ( Keyword :: Over ) ?;
59
- function. over_clause = Some ( self . parse_function_over_clause ( ) ?) ;
59
+
60
+ function. over_clause = if let Ok ( identifier) = self . consume_as_id ( ) {
61
+ Some ( OverClause :: WindowName ( identifier. to_string ( ) ) )
62
+ } else {
63
+ Some ( OverClause :: WindowDefinition (
64
+ self . parse_window_definition ( ) ?,
65
+ ) )
66
+ } ;
60
67
}
61
68
62
69
Ok ( Expression :: Function ( function) )
@@ -200,21 +207,12 @@ impl<'a> FunctionParser for Parser<'a> {
200
207
}
201
208
202
209
/// Parse a function over clause
203
- fn parse_function_over_clause ( & mut self ) -> Result < WindowDefinition , ParsingError > {
204
- if let Ok ( identifier) = self . peek_as_id ( ) {
205
- let over_clause = WindowDefinition {
206
- window_name : Some ( identifier. to_string ( ) ) ,
207
- ..Default :: default ( )
208
- } ;
209
- self . consume_as_id ( ) ?;
210
- return Ok ( over_clause) ;
211
- }
212
-
210
+ fn parse_window_definition ( & mut self ) -> Result < WindowDefinition , ParsingError > {
213
211
self . consume_as ( TokenType :: LeftParen ) ?;
214
212
215
213
let mut over_clause = WindowDefinition :: default ( ) ;
216
214
if let Ok ( base_window_name) = self . peek_as_id ( ) {
217
- over_clause. window_name = Some ( base_window_name. to_string ( ) ) ;
215
+ over_clause. base_window_name = Some ( base_window_name. to_string ( ) ) ;
218
216
self . consume_as_id ( ) ?;
219
217
}
220
218
@@ -373,7 +371,7 @@ mod function_expression_tests {
373
371
use crate :: {
374
372
expression:: test_utils:: * , BetweenFrameSpec , BetweenFrameSpecType , BinaryOp , FrameSpec ,
375
373
FrameSpecExclude , FrameSpecType , FrameType , FunctionArg , FunctionArgType , NullsOrdering ,
376
- Ordering , OrderingTerm , WindowDefinition ,
374
+ Ordering , OrderingTerm , OverClause , WindowDefinition ,
377
375
} ;
378
376
379
377
#[ test]
@@ -543,16 +541,14 @@ mod function_expression_tests {
543
541
arguments : vec ! [ FunctionArgType :: Expression ( numeric_literal_expression( "1" ) ) ] ,
544
542
} ;
545
543
546
- let over_clause = WindowDefinition {
547
- window_name : Some ( "a" . to_string ( ) ) ,
548
- partition_by : None ,
549
- order_by : None ,
550
- frame_spec : None ,
551
- } ;
552
-
553
544
run_sunny_day_expression_test (
554
545
"SELECT abc(1) over a;" ,
555
- & function_expression ( "abc" , expected_arg, None , Some ( over_clause) ) ,
546
+ & function_expression (
547
+ "abc" ,
548
+ expected_arg,
549
+ None ,
550
+ Some ( OverClause :: WindowName ( "a" . to_string ( ) ) ) ,
551
+ ) ,
556
552
) ;
557
553
}
558
554
@@ -564,15 +560,20 @@ mod function_expression_tests {
564
560
} ;
565
561
566
562
let over_clause = WindowDefinition {
567
- window_name : None ,
563
+ base_window_name : None ,
568
564
partition_by : Some ( vec ! [ numeric_literal_expression( "1" ) ] ) ,
569
565
order_by : None ,
570
566
frame_spec : None ,
571
567
} ;
572
568
573
569
run_sunny_day_expression_test (
574
570
"SELECT abc(1) over (partition by 1);" ,
575
- & function_expression ( "abc" , expected_arg, None , Some ( over_clause) ) ,
571
+ & function_expression (
572
+ "abc" ,
573
+ expected_arg,
574
+ None ,
575
+ Some ( OverClause :: WindowDefinition ( over_clause) ) ,
576
+ ) ,
576
577
) ;
577
578
}
578
579
@@ -584,7 +585,7 @@ mod function_expression_tests {
584
585
} ;
585
586
586
587
let over_clause = WindowDefinition {
587
- window_name : None ,
588
+ base_window_name : None ,
588
589
partition_by : None ,
589
590
order_by : Some ( vec ! [ OrderingTerm {
590
591
expression: Box :: new( numeric_literal_expression( "1" ) ) ,
@@ -596,7 +597,12 @@ mod function_expression_tests {
596
597
597
598
run_sunny_day_expression_test (
598
599
"SELECT abc(1) over (order by 1 asc nulls last);" ,
599
- & function_expression ( "abc" , expected_arg, None , Some ( over_clause) ) ,
600
+ & function_expression (
601
+ "abc" ,
602
+ expected_arg,
603
+ None ,
604
+ Some ( OverClause :: WindowDefinition ( over_clause) ) ,
605
+ ) ,
600
606
) ;
601
607
}
602
608
@@ -608,7 +614,7 @@ mod function_expression_tests {
608
614
} ;
609
615
610
616
let over_clause = WindowDefinition {
611
- window_name : Some ( "a" . to_string ( ) ) ,
617
+ base_window_name : Some ( "a" . to_string ( ) ) ,
612
618
partition_by : None ,
613
619
order_by : None ,
614
620
frame_spec : Some ( FrameSpec {
@@ -625,7 +631,7 @@ mod function_expression_tests {
625
631
626
632
run_sunny_day_expression_test (
627
633
"SELECT abc(1) over (a groups between 1 preceding and current row exclude current row);" ,
628
- & function_expression ( "abc" , expected_arg, None , Some ( over_clause) ) ,
634
+ & function_expression ( "abc" , expected_arg, None , Some ( OverClause :: WindowDefinition ( over_clause) ) ) ,
629
635
) ;
630
636
}
631
637
}
0 commit comments