orql 0.1.0

A toy SQL parser for a subset of the Oracle dialect.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
use std::ops::Deref;

use crate::ast::{ExprList, WindowSpec};

use super::{Condition, Duplicates, Expr, Hint, Ident, Identifier, Node, OrderBy};

#[derive(Debug)]
pub struct Select<'s, ID> {
    /// the `SELECT` query
    pub query: Query<'s, ID>,
    /// e.g. `SELECT ... FOR UPDATE ...` row locking
    ///
    /// Unlike Oracle, the parser will permit a `FOR UPDATE` clause even if
    /// used ... "with the following other constructs: the DISTINCT operator,
    /// CURSOR expression, set operators, group_by_clause, or aggregate
    /// functions."
    pub for_update: Option<ForUpdate<'s, ID>>,
}

#[derive(Debug)]
pub struct Query<'s, ID> {
    /// Common Table Expressions (CTEs) via `WITH`
    pub with: Option<With<'s, ID>>,
    /// The body of the query, e.g. `SELECT ... WHERE ... ORDER BY ...`
    pub body: QueryBody<'s, ID>,
}

#[derive(Debug)]
pub struct QueryBody<'s, ID> {
    /// the `SELECT` including `UNION`, `EXCEPT`, ...
    pub block: QueryBlock<'s, ID>,
    /// `ORDER BY ...`
    pub order_by: Option<OrderBy<'s, ID>>,
    /// `OFFSET ... FETCH ...`
    pub row_limit: RowLimit<'s, ID>,
}

#[derive(Debug)]
pub enum QueryBlock<'s, ID> {
    /// The actual query SELECT
    Select(QuerySelect<'s, ID>),
    /// A composition of two queries, e.g. `SELECT ... UNION ALL ... SELECT ...`
    // ~ cannot have the `order_by_clause` or `row_limiting_clause`
    Composed(
        Box<QueryBlock<'s, ID>>,
        ComposeOperator<ID>,
        Box<QueryBlock<'s, ID>>,
    ),
    /// A query (body, ie. without `WITH`) nested in paranthesis, e.g. `(SELECT ...)`
    Nested(Node<Box<QueryBody<'s, ID>>, ID>),
}

#[derive(Debug)]
pub struct ComposeOperator<ID> {
    /// the compose operation, e.g. `UNION`
    pub operation: Node<ComposeOperation, ID>,
    /// tells whether the `ALL` keyword was present
    pub all_token: Option<Node<(), ID>>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ComposeOperation {
    /// `UNION`
    Union,
    /// `INTERSECT`
    Intersect,
    /// `MINUS`
    Minus,
    /// `EXCEPT`
    Except,
}

#[derive(Debug)]
pub struct QuerySelect<'s, ID> {
    /// the `SELECT` keyword
    pub select_token: Node<(), ID>,
    /// a query optimizer hint, e.g. `
    pub hint: Option<Node<Hint<'s>, ID>>,
    /// optional `DISTINCT | UNIQUE | ALL` keyword
    pub duplicates: Option<Node<Duplicates, ID>>,
    /// the projection expressions
    pub projection: Vec<ProjectionItem<'s, ID>>,
    /// the `FROM ...`
    pub from: QueryTables<'s, ID>,
    /// the `WHERE ...` condition, if any
    pub selection: Option<WhereCondition<'s, ID>>,
    // XXX connect_by (hierarchical_query_clause)
    /// the `GROUP BY ...` clause
    pub group_by: Option<GroupBy<'s, ID>>,
    // XXX model_clause
    /// the `WINDOW ...` clause
    pub windows: Option<QueryWindows<'s, ID>>,
}

/// A projection item alias
#[derive(Debug)]
pub struct AsAlias<'s, ID> {
    /// the "AS" token
    pub as_token: Option<Node<(), ID>>,
    /// the alias name; might be `None` if the "AS" token was specified but
    /// the name itself was actually missing (which is possible in Oracle SQL)
    pub name: Option<Node<Ident<'s>, ID>>,
}

#[derive(Debug)]
pub enum ProjectionItem<'s, ID> {
    Expr(Expr<'s, ID>, Option<AsAlias<'s, ID>>),
    Wildcard(ProjectionWildcard<'s, ID>),
}

#[derive(Debug)]
pub enum ProjectionWildcard<'s, ID> {
    /// A simple (single, standalone) `*` wildcard
    Simple(Node<(), ID>),
    /// A qualified wildcard, e.g. `t.*`
    Qualified(Identifier<'s, ID>, Node<(), ID>),
}

#[derive(Debug)]
pub struct QueryTables<'s, ID> {
    /// the `FROM` keyword itself
    pub from_token: Node<(), ID>,
    /// `FROM <table-expr>, <table-expr>, ...`
    pub tables: Vec<QueryTable<'s, ID>>,
}

#[derive(Debug)]
pub enum QueryTable<'s, ID> {
    Table(QueryTableWithJoins<'s, ID>),
    // XXX InlineAnalyticalView(..)
}

#[derive(Debug)]
pub struct QueryTableWithJoins<'s, ID> {
    /// the table like expression being queried
    pub table: AliasedQueryTable<'s, ID>,
    /// ... with more, additional joined table expressions
    pub joins: Vec<TableJoin<'s, ID>>,
}

/// The type of an alias of a query table.
///
/// See [AliasedQueryTable::alias].
pub type QueryTableAlias<'s, ID> = Option<Node<Ident<'s>, ID>>;

/// A query table expression with an alias.
#[derive(Debug)]
pub struct AliasedQueryTable<'s, ID> {
    /// `FROM <table-reference> ...`
    pub table: QueryTableType<'s, ID>,
    /// `FROM ... <alias>`
    pub alias: QueryTableAlias<'s, ID>,
}

#[derive(Debug)]
pub enum QueryTableType<'s, ID> {
    /// e.g. `FROM (dual f)`
    Nested(Node<Box<QueryTable<'s, ID>>, ID>),
    /// e.g. `FROM <table-expression>`
    Expr(QueryTableExpression<'s, ID>),
    /// e.g. `FROM ... ONLY (<table-expression>)`
    Only {
        only_token: Node<(), ID>,
        expression: Node<QueryTableExpression<'s, ID>, ID>,
    },
    /// e.g. `FROM CONTAINERS(table_name)`
    Containers(ContainersTable<'s, ID>),
    /// e.g. `FROM SHARDS(table_name)`
    Shards(ShardsTable<'s, ID>),
}

#[derive(Debug)]
pub struct ContainersTable<'s, ID> {
    /// the `CONTAINERS` keyword
    pub containers_token: Node<(), ID>,
    /// the references table / view identifiers nested in parentheses
    pub table: Node<Identifier<'s, ID>, ID>,
}

#[derive(Debug)]
pub struct ShardsTable<'s, ID> {
    /// the `SHARDS` keyword
    pub shards_token: Node<(), ID>,
    /// the references table / view identifiers nested in parentheses
    pub table: Node<Identifier<'s, ID>, ID>,
}

// XXX this is a bit simplified
// see <https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/SELECT.html#GUID-CFA006CA-6FF1-4972-821E-6996142A51C6__I2126073> for the full scale
#[derive(Debug)]
pub enum QueryTableExpression<'s, ID> {
    /// e.g. `FROM table_name` or `FROM schema.table`
    Name(Identifier<'s, ID>),
    /// e.g. `FROM (SELECT 1 FROM DUAL)`
    SubQuery(SubQuery<'s, ID>),
    /// e.g. `FROM TABLE(<expr>) (+)`
    Table(TableCollection<'s, ID>),
    // XXX there's also this form: `FROM func(param)` where `func` returns a
    // collection expression similarly to the table-collection-expression but
    // without the `TABLE(..)`
}

#[derive(Debug)]
pub struct SubQuery<'s, ID> {
    /// e.g. `FROM LATERAL (...)`
    pub lateral_token: Option<Node<(), ID>>,
    /// e.g. `FROM ... ([WITH ...] SELECT 1 FROM DUAL)`
    pub query: Node<Box<Query<'s, ID>>, ID>,
    // XXX subquery_restirction_clause
}

#[derive(Debug)]
pub struct TableCollection<'s, ID> {
    /// the `TABLE` token
    pub table_token: Node<(), ID>,
    /// the collection expression, e.g. `(<expr>)`
    pub expr: Node<Expr<'s, ID>, ID>,
    /// an optional `(+)` to denote an outer join with the table collection;
    /// the outer `Node` denotes the parentheses, the inner one the `plus`
    /// token.
    pub as_outer_join: Option<Node<Node<(), ID>, ID>>,
}

/// The different types of joins together with the (right) join side
#[derive(Debug)]
pub enum TableJoin<'s, ID> {
    Inner(InnerTableJoin<'s, ID>),
    Outer(OuterTableJoin<'s, ID>),
    Apply(ApplyTableJoin<'s, ID>),
}

impl<'s, ID> From<InnerTableJoin<'s, ID>> for TableJoin<'s, ID> {
    fn from(value: InnerTableJoin<'s, ID>) -> Self {
        Self::Inner(value)
    }
}

impl<'s, ID> From<OuterTableJoin<'s, ID>> for TableJoin<'s, ID> {
    fn from(value: OuterTableJoin<'s, ID>) -> Self {
        Self::Outer(value)
    }
}

impl<'s, ID> From<ApplyTableJoin<'s, ID>> for TableJoin<'s, ID> {
    fn from(value: ApplyTableJoin<'s, ID>) -> Self {
        Self::Apply(value)
    }
}

/// An "inner join"
#[derive(Debug)]
pub enum InnerTableJoin<'s, ID> {
    /// e.g. `table1 INNER JOIN table2 ON (..) | USING (..)`
    Inner(InnerJoinedTable<'s, ID>),
    /// e.g. `table1 CROSS JOIN table2`
    Cross(CrossInnerJoinedTable<'s, ID>),
    /// e.g. `table1 NATURAL [INNER] JOIN table2`
    Natural(NaturalInnerJoinedTable<'s, ID>),
}

impl<'s, ID> From<InnerJoinedTable<'s, ID>> for InnerTableJoin<'s, ID> {
    fn from(value: InnerJoinedTable<'s, ID>) -> Self {
        Self::Inner(value)
    }
}

impl<'s, ID> From<InnerJoinedTable<'s, ID>> for TableJoin<'s, ID> {
    fn from(value: InnerJoinedTable<'s, ID>) -> Self {
        Self::Inner(value.into())
    }
}

impl<'s, ID> From<CrossInnerJoinedTable<'s, ID>> for InnerTableJoin<'s, ID> {
    fn from(value: CrossInnerJoinedTable<'s, ID>) -> Self {
        Self::Cross(value)
    }
}

impl<'s, ID> From<CrossInnerJoinedTable<'s, ID>> for TableJoin<'s, ID> {
    fn from(value: CrossInnerJoinedTable<'s, ID>) -> Self {
        Self::Inner(value.into())
    }
}

impl<'s, ID> From<NaturalInnerJoinedTable<'s, ID>> for InnerTableJoin<'s, ID> {
    fn from(value: NaturalInnerJoinedTable<'s, ID>) -> Self {
        Self::Natural(value)
    }
}

impl<'s, ID> From<NaturalInnerJoinedTable<'s, ID>> for TableJoin<'s, ID> {
    fn from(value: NaturalInnerJoinedTable<'s, ID>) -> Self {
        Self::Inner(value.into())
    }
}

/// A `[INNER] JOIN <table> ON|USING ...`
#[derive(Debug)]
pub struct InnerJoinedTable<'s, ID> {
    /// the `INNER` keyword, if it present
    pub inner_token: Option<Node<(), ID>>,
    /// the `JOIN` keyword itself
    pub join_token: Node<(), ID>,
    /// the table (or table expression) being joined
    pub table: Box<AliasedQueryTable<'s, ID>>,
    /// the condition being joined on
    pub condition: JoinCondition<'s, ID>,
}

/// A `CROSS JOIN <table>`
#[derive(Debug)]
pub struct CrossInnerJoinedTable<'s, ID> {
    /// the `CROSS` keyword
    pub cross_token: Node<(), ID>,
    /// the `JOIN` keyword
    pub join_token: Node<(), ID>,
    /// the table (or table expression) being joined
    pub table: Box<AliasedQueryTable<'s, ID>>,
}

/// A `NATURAL [INNER] JOIN <table>`
#[derive(Debug)]
pub struct NaturalInnerJoinedTable<'s, ID> {
    /// the `NATURAL` keyword
    pub natural_token: Node<(), ID>,
    /// the optional `INNER` keyword
    pub inner_token: Option<Node<(), ID>>,
    /// the `JOIN` keyword
    pub join_token: Node<(), ID>,
    /// the table (or table expression) being joined
    pub table: Box<AliasedQueryTable<'s, ID>>,
}

/// A `[NATURAL] (FULL|LEFT|RIGHT) [OUTER] JOIN [ON ...|USING ...]` clause
#[derive(Debug)]
pub struct OuterTableJoin<'s, ID> {
    /// the type of the join, e.g. `LEFT`, `RIGHT`, or `FULL`
    pub type_token: Node<OuterJoinType, ID>,
    /// the optional `OUTER` token
    pub outer_token: Option<Node<(), ID>>,
    /// the `JOIN` keyword
    pub join_token: Node<(), ID>,
    /// the table (or table expression) being joined
    pub table: Box<AliasedQueryTable<'s, ID>>,
    /// an optional `PARTITION BY` on one of the join sides
    pub partition_by: Option<OuterTableJoinParitionBy<'s, ID>>,
    /// the join condition, either implicit by `NATURAL` or an explicit, user
    /// provided condition
    pub condition: OuterJoinCondition<'s, ID>,
}

/// A `PARTITION BY` applied to one of the sides of an outer join;
///
/// Note: a "partition by" clause is _not_ valid for a `FULL` outer join
#[derive(Debug)]
pub enum OuterTableJoinParitionBy<'s, ID> {
    /// with the `PARTITION BY` applying to the left side of the join,
    /// e.g. `SELECT * FROM left_table PARTITION BY (<expr>) LEFT JOIN right_table ON (<condition>)`
    Left(PartitionBy<'s, ID>),
    /// with the `PARTITION BY` applying to the right side of the join,
    /// e.g. `SELECT * FROM left_table LEFT JOIN right_table PARTITION BY (<expr>) ON (<condition>)`
    Right(PartitionBy<'s, ID>),
}

/// `PARTITION BY (<expr>, <expr>)` of an
/// [outer join](OuterTableJoin::partition_by) or
/// an analytic [function call](crate::ast::FunctionParams::over)
#[derive(Debug)]
pub struct PartitionBy<'s, ID> {
    /// the `PARTITION` keyword
    pub partition_token: Node<(), ID>,
    /// the `BY` keyword
    pub by_token: Node<(), ID>,
    /// the partition expressions, e.g. `<expr>, <expr>` or `(<expr>, <expr>)`
    pub expressions: PartitionByExprs<'s, ID>,
}

/// A list of expressions for of [`PartitionBy`] clause.
///
/// # Note
///
/// * A `PARTITION BY` as part of an "outer join" always encloses its
///   expressions in parentheses and, therefore, represented by
///   [PartitionByExprs::List].
/// * On the other hand, a `PARTITION BY` used in an anlytical function, is
///   _not_ enclosing the expression in parentheses and, therefore, represted
///   by [PartitionByExprs::Exprs].
///
/// The parses enforces these rules.
#[derive(Debug)]
pub enum PartitionByExprs<'s, ID> {
    /// a comma separated sequence of expressions without being enclosed in
    /// parentheses, e.g. `1, 2, 3`
    Exprs(Vec<Expr<'s, ID>>),
    /// a comma separated list of expressions enclosed in parentheses,
    /// e.g. `(1, 2, 3)`
    List(ExprList<'s, ID>),
}

impl<'s, ID> Deref for PartitionByExprs<'s, ID> {
    type Target = [Expr<'s, ID>];

    fn deref(&self) -> &Self::Target {
        match self {
            PartitionByExprs::Exprs(exprs) => exprs,
            PartitionByExprs::List(list) => list,
        }
    }
}

#[derive(Debug)]
pub enum OuterJoinCondition<'s, ID> {
    /// A `NATURAL` join (without an explicit join condition)
    ///
    /// The contained node refers to the `NATURAL` keyword
    Natural { natural_token: Node<(), ID> },
    /// A regular join with an optional, explicit condition
    Regular(Option<JoinCondition<'s, ID>>),
}

/// The type of an outer join
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum OuterJoinType {
    /// `FULL`
    Full,
    /// `LEFT`
    Left,
    /// `RIGHT`
    Right,
}

#[derive(Debug)]
pub enum JoinCondition<'s, ID> {
    /// e.g. `JOIN ... ON (1 = 1)`
    On(JoinConditionOn<'s, ID>),
    /// e.g. `JOIN ... USING (a, b)`
    Using(JoinConditionUsing<'s, ID>),
}

/// A [JoinCondition::On]
#[derive(Debug)]
pub struct JoinConditionOn<'s, ID> {
    /// the `ON` keyword
    pub on_token: Node<(), ID>,
    /// the condition itself
    pub condition: Condition<'s, ID>,
}

/// A [JoinCondition::Using]
#[derive(Debug)]
pub struct JoinConditionUsing<'s, ID> {
    /// the `USING` keyword
    pub using_token: Node<(), ID>,
    /// list of column common to both table on which to join
    pub columns: Node<Vec<Node<Ident<'s>, ID>>, ID>,
}

/// A `<table1> (CROSS | OUTER) APPLY <table2>` join
#[derive(Debug)]
pub struct ApplyTableJoin<'s, ID> {
    /// the type of the `APPLY` join
    pub type_token: Node<ApplyJoinType, ID>,
    /// the `APPLY` token
    pub apply_token: Node<(), ID>,
    /// the joined table or value collection
    pub table: ApplyJoinTable<'s, ID>,
}

/// The different types of a [TableJoin::Apply] join.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ApplyJoinType {
    /// e.g. `CROSS APPLY ...`; equivalent to a `INNER JOIN LATERAL`
    Cross,
    /// e.g. `OUTER APPLY ...`; equivalent to a `LEFT JOIN LATERAL`
    Outer,
}

/// A joined table (expression) via [TableJoin::Apply]
#[derive(Debug)]
pub enum ApplyJoinTable<'s, ID> {
    /// e.g. `CROSS APPLY (SELECT ...) t`
    Table(Box<AliasedQueryTable<'s, ID>>),
    /// e.g. `CROSS APPLY my_function()` with `my_function()` returning a collection
    Expr(Expr<'s, ID>),
}

/// The `WITH` clause
#[derive(Debug)]
pub struct With<'s, ID> {
    /// the `WITH` keyword
    pub with_keyword: Node<(), ID>,
    // XXX plsql_declarations
    /// the common table expressions; at least one provided
    pub ctes: Vec<CteTable<'s, ID>>,
}

#[derive(Debug)]
pub enum CteTable<'s, ID> {
    SubQuery(CteSubQuery<'s, ID>),
    // XXX analytical view (subav_factoring_clauses)
}

#[derive(Debug)]
pub struct CteSubQuery<'s, ID> {
    /// the name of the CTE
    pub name: Node<Ident<'s>, ID>,
    /// optional column aliases, e.g. `WITH cte_name (column_alias1, column_alias2) AS ...`
    pub column_aliases: Option<Node<Vec<Node<Ident<'s>, ID>>, ID>>,
    /// the `AS` token, e.g. `WITH cte_name AS (...`
    pub as_token: Node<(), ID>,
    /// the CTE query itself, e.g. `WITH cte_name AS (SELECT 1 FROM dual) ...`;
    /// the node represents the braces encompasing the query
    pub query: Node<Box<QueryBody<'s, ID>>, ID>,
    // XXX search_clause
    // XXX cycle_clause
}

/// Condition to a [QueryBlock] by which to restrict / filter the selections'
/// data; specified by a `WHERE ...` in a SQL statement
#[derive(Debug)]
pub struct WhereCondition<'s, ID> {
    /// the `WHERE` keyword
    pub where_token: Node<(), ID>,
    /// the `<condition>` itself
    pub condition: Condition<'s, ID>,
}

#[derive(Debug)]
pub struct GroupBy<'s, ID> {
    /// the `GROUP` keyword
    pub group_token: Node<(), ID>,
    /// the `BY` keyword
    pub by_token: Node<(), ID>,
    /// the grouping expressions
    pub group_type: GroupByType<'s, ID>,
    /// the `HAVING ...` clause filtering the groups
    pub having: Option<HavingCondition<'s, ID>>,
}

#[derive(Debug)]
pub enum GroupByType<'s, ID> {
    /// a grouping directly by a list of expressions
    Exprs(GroupByExprs<'s, ID>),
    // XXX ROLLUP / CUBE
    // XXX GROUPING SETS
}

#[derive(Debug)]
pub enum GroupByExprs<'s, ID> {
    /// e.g. `GROUP BY ()`; the outer node denotes the surrounding parentheses
    /// while the inner node denotes the "inner space" of them, possibly
    /// associated with comments.
    All(Node<Node<(), ID>, ID>),
    /// a list of expressions to group by, e.g `GROUP BY id, name`
    Exprs(Vec<Expr<'s, ID>>),
    /// just like [GroupByExprs::Exprs] but enclosed by parantheses
    /// possibly associated with comments, e.g. `GROUP BY (id, name)`
    Nested(Node<Vec<Expr<'s, ID>>, ID>),
}

/// The `HAVING` clause of a [GroupBy] definition.
#[derive(Debug)]
pub struct HavingCondition<'s, ID> {
    /// the `HAVING` keyword
    pub having_token: Node<(), ID>,
    /// the restricting condition
    pub condition: Condition<'s, ID>,
}

/// A `FOR UPDATE ...` of a [`SELECT`](Select) statement.
#[derive(Debug)]
pub struct ForUpdate<'s, ID> {
    /// the `FOR` keyword
    pub for_token: Node<(), ID>,
    /// the `UPDATE` keyword
    pub update_token: Node<(), ID>,
    /// the lock row `OF col1, col2, ...` columns
    pub of_columns: Option<ForUpdateOfColumns<'s, ID>>,
    /// lock options, e.g. `NOWAIT`
    pub lock_opts: Option<ForUpdateLockOptions<'s, ID>>,
}

/// List of columns in an [ForUpdate] clause identifying the rows' tables to be locked.
///
/// "…Oracle will lock only the rows in those tables that contain any of the specified columns.…"
/// — [source](https://www.oreilly.com/library/view/oracle-pl-sql-best/0596001215/re70.html)
#[derive(Debug)]
pub struct ForUpdateOfColumns<'s, ID> {
    /// the `OF` keyword
    pub of_token: Node<(), ID>,
    /// the list of columns identifying row tables to lock; valid with at
    /// least one column identifier
    pub columns: Vec<Identifier<'s, ID>>,
}

/// Options on acquiring a lock through [`FOR UPDATE`](ForUpdate)
#[derive(Debug)]
pub enum ForUpdateLockOptions<'s, ID> {
    /// e.g. `SKIP LOCKED`
    Skip {
        /// the `SKIP` keyword ...
        skip_token: Node<(), ID>,
        /// the `LOCKED` keyword
        locked_token: Node<(), ID>,
    },
    /// e.g. `NOWAIT`
    NoWait {
        /// the `NOWAIT` token
        nowait_token: Node<(), ID>,
    },
    /// e.g. `WAIT 10`
    Wait {
        /// the `WAIT` keyword
        wait_token: Node<(), ID>,
        /// the duration in seconds to wait for the lock to get acquired;
        /// integer without a sign, only decimals with possibly leading zeros
        duration: Node<&'s str, ID>,
    },
}

/// e.g. `SELECT ... OFFSET 10 ROWS FETCH 20 ROWS ONLY`
///
/// Both, `OFFSET` and `FETCH` are optional, but never `None` at the same
/// time.
#[derive(Debug)]
pub struct RowLimit<'s, ID> {
    /// e.g. `... OFFSET <n> ROWS`
    pub offset: Option<RowLimitOffset<'s, ID>>,
    /// e.g. `... FETCH 10 PERCENT ROWS WITH TIES`
    pub fetch: Option<RowLimitFetch<'s, ID>>,
}

/// The "offset" definition of a [row limit](RowLimit) clause
#[derive(Debug)]
pub struct RowLimitOffset<'s, ID> {
    /// the `OFFSET` keyword
    pub offset_token: Node<(), ID>,
    /// the number of rows to skip; typically an integer literal by can be any
    /// expression evaluating to a number
    pub offset_value: Expr<'s, ID>,
    /// the `ROW` or `ROWS` keyword
    pub row_token: Node<RowKeyword, ID>,
}

/// The "limit" definition of a [row limit](RowLimit) clause
#[derive(Debug)]
pub struct RowLimitFetch<'s, ID> {
    /// the `FETCH` keyword
    pub fetch_token: Node<(), ID>,
    /// the `FIRST | NEXT` keyword following `FETCH`
    pub first_token: Node<FetchFirstKeyword, ID>,
    /// the `ROW` or `ROWS` keyword
    pub row_token: Node<RowKeyword, ID>,
    /// the number or percent of rows to fetch
    pub fetch_amount: FetchAmount<'s, ID>,
    /// corresponding to `ONLY` | WITH TIES` keywords
    pub fetch_amount_spec: FetchAmountSpec<ID>,
}

/// the `FIRST | NEXT` keywords part of a [RowLimitFetch] clause; all variants are
/// interchanglable and have the same meaning
#[derive(Debug, Copy, Clone)]
pub enum FetchFirstKeyword {
    /// the `FIRST` keyword
    First,
    /// the `NEXT` keyword
    Next,
}

/// The definition of the count or percentages in a [RowLimitFetch] clause
#[derive(Debug)]
pub enum FetchAmount<'s, ID> {
    /// the count of rows to return; generally a numeric literal but can be
    /// any expression evaluating to a number
    Count(Expr<'s, ID>),
    /// percentage of rows to return
    Percent {
        /// the percentage value of rows to return; generally a numeric
        /// literal but can be any expression evaulating to a number
        amount: Expr<'s, ID>,
        /// the `PERCENT` keyword
        percent_token: Node<(), ID>,
    },
}

/// Details regarding a [row limiting's fetch](RowLimitFetch)
#[derive(Debug)]
pub enum FetchAmountSpec<ID> {
    Only {
        only_token: Node<(), ID>,
    },
    WithTies {
        with_token: Node<(), ID>,
        ties_token: Node<(), ID>,
    },
}

/// The `ROW(S)` keyword part of a [RowLimit] clause; all variants are
/// interchanglable and have the same meaning
#[derive(Debug, Copy, Clone)]
pub enum RowKeyword {
    /// the `ROW` keyword
    Row,
    /// the `ROWS` keyword
    Rows,
}

/// A query's defined [`WINDOW's`](QuerySelect::windows) for reference in analytical function calls
#[derive(Debug)]
pub struct QueryWindows<'s, ID> {
    /// the `WINDOW` keyword
    pub window_token: Node<(), ID>,
    /// the list of defined query windows
    pub windows: Vec<QueryWindow<'s, ID>>,
}

/// A window defintion as part of a query's `WINDOW` clause,
/// e.g. `window_name AS (partition by column)`
#[derive(Debug)]
pub struct QueryWindow<'s, ID> {
    /// the window's defined name
    pub name: Node<Ident<'s>, ID>,
    /// the `AS` keyword
    pub as_token: Node<(), ID>,
    /// the window specification
    pub window: Node<WindowSpec<'s, ID>, ID>,
}