1
1
use crate :: {
2
- BetweenFrameSpec , BetweenFrameSpecType , Expression , FrameSpec , FrameSpecExclude , FrameSpecType ,
3
- FrameType , Function , FunctionArg , FunctionArgType , Identifier , Keyword , NullsOrdering ,
4
- Ordering , OrderingTerm , OverClause , Parser , ParsingError , TokenType , WindowDefinition ,
2
+ parser:: window_definition:: WindowDefinitionParser , Expression , Function , FunctionArg ,
3
+ FunctionArgType , Identifier , Keyword , OverClause , Parser , ParsingError , TokenType ,
5
4
} ;
6
5
7
6
use super :: ExpressionParser ;
@@ -13,22 +12,8 @@ pub trait FunctionParser {
13
12
/// Parse a function argument
14
13
fn parse_function_arg ( & mut self ) -> Result < FunctionArg , ParsingError > ;
15
14
16
- /// Parse a function ordering terms
17
- fn parse_function_ordering_terms ( & mut self ) -> Result < Vec < OrderingTerm > , ParsingError > ;
18
-
19
15
/// Parse a function filter clause
20
16
fn parse_function_filter_clause ( & mut self ) -> Result < Expression , ParsingError > ;
21
-
22
- /// Parse a function over clause
23
- fn parse_window_definition ( & mut self ) -> Result < WindowDefinition , ParsingError > ;
24
-
25
- /// Parse a frame spec
26
- fn parse_function_over_clause_frame_spec ( & mut self ) -> Result < FrameSpec , ParsingError > ;
27
-
28
- /// Parse a frame spec between clause
29
- fn parse_function_over_clause_frame_spec_between (
30
- & mut self ,
31
- ) -> Result < FrameSpecType , ParsingError > ;
32
17
}
33
18
34
19
impl < ' a > FunctionParser for Parser < ' a > {
@@ -122,7 +107,7 @@ impl<'a> FunctionParser for Parser<'a> {
122
107
123
108
match last_expression {
124
109
FunctionArgType :: Expression ( expression) => {
125
- let ordering_terms = self . parse_function_ordering_terms ( ) ?;
110
+ let ordering_terms = self . parse_ordering_terms ( ) ?;
126
111
let arg = FunctionArgType :: OrderedBy ( expression, ordering_terms) ;
127
112
function_argument. arguments . push ( arg) ;
128
113
}
@@ -144,54 +129,6 @@ impl<'a> FunctionParser for Parser<'a> {
144
129
}
145
130
}
146
131
147
- /// Parse a function ordering terms
148
- fn parse_function_ordering_terms ( & mut self ) -> Result < Vec < OrderingTerm > , ParsingError > {
149
- let mut ordering_terms = vec ! [ ] ;
150
-
151
- while let Ok ( expression) = self . parse_expression ( ) {
152
- // No need to check for CollateExpression, because it will be parsed as an Expression
153
-
154
- let mut ordering_term = OrderingTerm {
155
- expression : Box :: new ( expression) ,
156
- ordering : None ,
157
- nulls_ordering : None ,
158
- } ;
159
-
160
- if let Ok ( Keyword :: Asc ) = self . peek_as_keyword ( ) {
161
- ordering_term. ordering = Some ( Ordering :: Asc ) ;
162
- self . consume_as_keyword ( Keyword :: Asc ) ?;
163
- } else if let Ok ( Keyword :: Desc ) = self . peek_as_keyword ( ) {
164
- ordering_term. ordering = Some ( Ordering :: Desc ) ;
165
- self . consume_as_keyword ( Keyword :: Desc ) ?;
166
- }
167
-
168
- if let Ok ( Keyword :: Nulls ) = self . peek_as_keyword ( ) {
169
- self . consume_as_keyword ( Keyword :: Nulls ) ?;
170
-
171
- if let Ok ( Keyword :: First ) = self . peek_as_keyword ( ) {
172
- ordering_term. nulls_ordering = Some ( NullsOrdering :: First ) ;
173
- self . consume_as_keyword ( Keyword :: First ) ?;
174
- } else if let Ok ( Keyword :: Last ) = self . peek_as_keyword ( ) {
175
- ordering_term. nulls_ordering = Some ( NullsOrdering :: Last ) ;
176
- self . consume_as_keyword ( Keyword :: Last ) ?;
177
- } else {
178
- return Err ( ParsingError :: UnexpectedToken ( format ! (
179
- "Expected FIRST or LAST keyword, got: {}" ,
180
- self . peek_token( ) ?. token_type
181
- ) ) ) ;
182
- }
183
- }
184
-
185
- ordering_terms. push ( ordering_term) ;
186
-
187
- if self . consume_as ( TokenType :: Comma ) . is_err ( ) {
188
- break ;
189
- }
190
- }
191
-
192
- Ok ( ordering_terms)
193
- }
194
-
195
132
/// Parse a function filter clause
196
133
fn parse_function_filter_clause ( & mut self ) -> Result < Expression , ParsingError > {
197
134
// Consume the opening left parenthesis
@@ -205,165 +142,6 @@ impl<'a> FunctionParser for Parser<'a> {
205
142
self . consume_as ( TokenType :: RightParen ) ?;
206
143
Ok ( expression)
207
144
}
208
-
209
- /// Parse a function over clause
210
- fn parse_window_definition ( & mut self ) -> Result < WindowDefinition , ParsingError > {
211
- self . consume_as ( TokenType :: LeftParen ) ?;
212
-
213
- let mut over_clause = WindowDefinition :: default ( ) ;
214
- if let Ok ( base_window_name) = self . peek_as_id ( ) {
215
- over_clause. base_window_name = Some ( base_window_name. to_string ( ) ) ;
216
- self . consume_as_id ( ) ?;
217
- }
218
-
219
- if let Ok ( Keyword :: Partition ) = self . peek_as_keyword ( ) {
220
- self . consume_as_keyword ( Keyword :: Partition ) ?;
221
-
222
- self . consume_as_keyword ( Keyword :: By ) ?;
223
-
224
- while let Ok ( expression) = self . parse_expression ( ) {
225
- match over_clause. partition_by . as_mut ( ) {
226
- Some ( partition_by) => partition_by. push ( expression) ,
227
- None => over_clause. partition_by = Some ( vec ! [ expression] ) ,
228
- }
229
- if self . peek_as ( TokenType :: Comma ) . is_ok ( ) {
230
- self . consume_token ( ) ?;
231
- } else {
232
- break ;
233
- }
234
- }
235
- }
236
-
237
- if let Ok ( Keyword :: Order ) = self . peek_as_keyword ( ) {
238
- self . consume_as_keyword ( Keyword :: Order ) ?;
239
-
240
- self . consume_as_keyword ( Keyword :: By ) ?;
241
- let ordering_terms = self . parse_function_ordering_terms ( ) ?;
242
- over_clause. order_by = Some ( ordering_terms) ;
243
- }
244
-
245
- // frame spec
246
- if let Ok ( Keyword :: Range | Keyword :: Rows | Keyword :: Groups ) = self . peek_as_keyword ( ) {
247
- // do not consume the keyword, as it will be used in the frame spec parsing
248
- over_clause. frame_spec = Some ( self . parse_function_over_clause_frame_spec ( ) ?) ;
249
- }
250
-
251
- self . consume_as ( TokenType :: RightParen ) ?;
252
-
253
- Ok ( over_clause)
254
- }
255
-
256
- fn parse_function_over_clause_frame_spec ( & mut self ) -> Result < FrameSpec , ParsingError > {
257
- let frame_type = match self . peek_as_keyword ( ) ? {
258
- Keyword :: Range => FrameType :: Range ,
259
- Keyword :: Rows => FrameType :: Rows ,
260
- Keyword :: Groups => FrameType :: Groups ,
261
- _ => {
262
- return Err ( ParsingError :: UnexpectedToken ( format ! (
263
- "Expected frame type, got: {}" ,
264
- self . peek_token( ) ?. token_type
265
- ) ) )
266
- }
267
- } ;
268
-
269
- // consume the frame type token
270
- self . consume_token ( ) ?;
271
-
272
- let frame_spec_type: FrameSpecType = if self . consume_as_keyword ( Keyword :: Between ) . is_ok ( ) {
273
- self . parse_function_over_clause_frame_spec_between ( ) ?
274
- } else if self . consume_as_keyword ( Keyword :: Unbounded ) . is_ok ( ) {
275
- self . consume_as_keyword ( Keyword :: Preceding ) ?;
276
- FrameSpecType :: UnboundedPreceding
277
- } else if self . consume_as_keyword ( Keyword :: Current ) . is_ok ( ) {
278
- self . consume_as_keyword ( Keyword :: Row ) ?;
279
- FrameSpecType :: CurrentRow
280
- } else {
281
- let expression = self . parse_expression ( ) ?;
282
- self . consume_as_keyword ( Keyword :: Preceding ) ?;
283
- FrameSpecType :: Preceding ( Box :: new ( expression) )
284
- } ;
285
-
286
- let mut exclude = None ;
287
-
288
- if self . consume_as_keyword ( Keyword :: Exclude ) . is_ok ( ) {
289
- if self . consume_as_keyword ( Keyword :: No ) . is_ok ( ) {
290
- self . consume_as_keyword ( Keyword :: Others ) ?;
291
- exclude = Some ( FrameSpecExclude :: NoOthers ) ;
292
- } else if self . consume_as_keyword ( Keyword :: Current ) . is_ok ( ) {
293
- self . consume_as_keyword ( Keyword :: Row ) ?;
294
- exclude = Some ( FrameSpecExclude :: CurrentRow ) ;
295
- } else if self . consume_as_keyword ( Keyword :: Group ) . is_ok ( ) {
296
- exclude = Some ( FrameSpecExclude :: Group ) ;
297
- } else if self . consume_as_keyword ( Keyword :: Ties ) . is_ok ( ) {
298
- exclude = Some ( FrameSpecExclude :: Ties ) ;
299
- } else {
300
- return Err ( ParsingError :: UnexpectedToken ( format ! (
301
- "Expected Exclude type, got: {}" ,
302
- self . peek_token( ) ?. token_type
303
- ) ) ) ;
304
- }
305
- }
306
-
307
- Ok ( FrameSpec {
308
- frame_type,
309
- frame_spec_type,
310
- exclude,
311
- } )
312
- }
313
-
314
- fn parse_function_over_clause_frame_spec_between (
315
- & mut self ,
316
- ) -> Result < FrameSpecType , ParsingError > {
317
- let start = if self . consume_as_keyword ( Keyword :: Unbounded ) . is_ok ( ) {
318
- self . consume_as_keyword ( Keyword :: Preceding ) ?;
319
- BetweenFrameSpecType :: UnboundedPreceding
320
- } else if self . consume_as_keyword ( Keyword :: Current ) . is_ok ( ) {
321
- self . consume_as_keyword ( Keyword :: Row ) ?;
322
- BetweenFrameSpecType :: CurrentRow
323
- } else {
324
- let expression = self . parse_expression ( ) ?;
325
- match self . peek_as_keyword ( ) ? {
326
- Keyword :: Preceding => {
327
- self . consume_as_keyword ( Keyword :: Preceding ) ?;
328
- BetweenFrameSpecType :: Preceding ( Box :: new ( expression) )
329
- }
330
- Keyword :: Following => {
331
- self . consume_as_keyword ( Keyword :: Following ) ?;
332
- BetweenFrameSpecType :: Following ( Box :: new ( expression) )
333
- }
334
- _ => {
335
- return Err ( ParsingError :: UnexpectedToken ( format ! (
336
- "Expected PRECEDING or FOLLOWING keyword, got: {}" ,
337
- self . peek_token( ) ?. token_type
338
- ) ) ) ;
339
- }
340
- }
341
- } ;
342
-
343
- self . consume_as_keyword ( Keyword :: And ) ?;
344
-
345
- let end = if self . consume_as_keyword ( Keyword :: Unbounded ) . is_ok ( ) {
346
- self . consume_as_keyword ( Keyword :: Following ) ?;
347
- BetweenFrameSpecType :: UnboundedFollowing
348
- } else if self . consume_as_keyword ( Keyword :: Current ) . is_ok ( ) {
349
- self . consume_as_keyword ( Keyword :: Row ) ?;
350
- BetweenFrameSpecType :: CurrentRow
351
- } else {
352
- let expression = self . parse_expression ( ) ?;
353
-
354
- if self . consume_as_keyword ( Keyword :: Preceding ) . is_ok ( ) {
355
- BetweenFrameSpecType :: Preceding ( Box :: new ( expression) )
356
- } else if self . consume_as_keyword ( Keyword :: Following ) . is_ok ( ) {
357
- BetweenFrameSpecType :: Following ( Box :: new ( expression) )
358
- } else {
359
- return Err ( ParsingError :: UnexpectedToken ( format ! (
360
- "Expected PRECEDING or FOLLOWING keyword, got: {}" ,
361
- self . peek_token( ) ?. token_type
362
- ) ) ) ;
363
- }
364
- } ;
365
- Ok ( FrameSpecType :: Between ( BetweenFrameSpec { start, end } ) )
366
- }
367
145
}
368
146
369
147
#[ cfg( test) ]
0 commit comments