openehr 0.2.0

openEHR Reference Model types, validation, paths, AQL parsing, and change-control security primitives
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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
//! Archetype Query Language: lexing, parsing, and static checking.
//!
//! AQL is openEHR's portable query language. It looks like SQL and is not:
//! `FROM` describes a **containment tree** over archetyped structures rather
//! than a set of tables, and paths address nodes inside those structures by
//! archetype node id.
//!
//! ```text
//! SELECT
//!     o/data[at0001]/events[at0006]/data[at0003]/items[at0004]/value/magnitude AS systolic,
//!     c/context/start_time AS taken
//! FROM EHR e[ehr_id/value=$ehrUid]
//!     CONTAINS COMPOSITION c[openEHR-EHR-COMPOSITION.encounter.v1]
//!         CONTAINS OBSERVATION o[openEHR-EHR-OBSERVATION.blood_pressure.v2]
//! WHERE o/data[at0001]/events[at0006]/data[at0003]/items[at0004]/value/magnitude >= 140
//! ORDER BY c/context/start_time DESC
//! LIMIT 5
//! ```
//!
//! # This is a front end, not a query engine
//!
//! This module turns AQL text into an [`AqlQuery`] and checks what can be
//! checked without data. It does **not** execute queries: executing AQL means
//! resolving archetype paths against a repository, and this crate has no
//! repository. Every function here is honest about that — nothing returns rows,
//! and nothing pretends a parse is a plan.
//!
//! What it is useful for:
//!
//! - rejecting a malformed query at the API edge, with an offset, rather than
//!   at the storage layer,
//! - finding the undefined-alias bug ([`AqlQuery::check`]) that AQL's syntax
//!   makes very easy and that no runtime reports usefully,
//! - enumerating the archetypes and parameters a query touches, before running
//!   it, which is what an authorisation check needs,
//! - re-rendering a query in a normal form ([`AqlQuery`]'s
//!   [`Display`](core::fmt::Display)).
//!
//! # Coverage
//!
//! Supported: `SELECT` with `DISTINCT` and `TOP`, aliases, aggregate and scalar
//! function calls, `FROM` with `CONTAINS` / `NOT CONTAINS` / `AND` / `OR` and
//! parentheses, archetype and standard predicates, `WHERE` with the comparison
//! operators, `AND` / `OR` / `NOT` / `EXISTS` / `MATCHES` / `LIKE`, parameters,
//! `ORDER BY` with `ASC` / `DESC`, and `LIMIT` / `OFFSET`.
//!
//! Not supported, and reported as a parse error rather than silently ignored:
//! `SELECT *`, `VERSION` and `TOP … FORWARD/BACKWARD` extensions, and
//! terminology-function subqueries. `spec/12-paths-and-query.md` `Q12.9`
//! records the list.

use core::fmt;

// ---------------------------------------------------------------------------
// Lexer
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, PartialEq)]
enum Token {
    Word(String),
    /// A path written after an alias: `o/data[at0001]/value`.
    Path(String),
    String(String),
    Number(f64),
    Integer(i64),
    Parameter(String),
    Symbol(&'static str),
}

#[derive(Debug, Clone)]
struct Lexed {
    token: Token,
    offset: usize,
}

/// A failure to parse AQL.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("AQL parse error at offset {offset}: {reason}")]
pub struct AqlError {
    /// Byte offset into the query where parsing stopped.
    pub offset: usize,
    /// What was expected, or what was found and not supported.
    pub reason: String,
}

impl AqlError {
    fn new(offset: usize, reason: impl Into<String>) -> Self {
        Self {
            offset,
            reason: reason.into(),
        }
    }
}

#[allow(clippy::too_many_lines)]
fn lex(input: &str) -> Result<Vec<Lexed>, AqlError> {
    let bytes = input.as_bytes();
    let mut out = Vec::new();
    let mut i = 0usize;
    while i < bytes.len() {
        let c = bytes[i];
        if c.is_ascii_whitespace() {
            i += 1;
            continue;
        }
        let start = i;
        match c {
            b'\'' | b'"' => {
                let quote = c;
                i += 1;
                let mut value = String::new();
                loop {
                    if i >= bytes.len() {
                        return Err(AqlError::new(start, "unterminated string literal"));
                    }
                    if bytes[i] == b'\\' && i + 1 < bytes.len() {
                        value.push(bytes[i + 1] as char);
                        i += 2;
                        continue;
                    }
                    if bytes[i] == quote {
                        i += 1;
                        break;
                    }
                    value.push(bytes[i] as char);
                    i += 1;
                }
                out.push(Lexed {
                    token: Token::String(value),
                    offset: start,
                });
            }
            b'$' => {
                i += 1;
                let from = i;
                while i < bytes.len() && (bytes[i].is_ascii_alphanumeric() || bytes[i] == b'_') {
                    i += 1;
                }
                if i == from {
                    return Err(AqlError::new(start, "`$` with no parameter name"));
                }
                out.push(Lexed {
                    token: Token::Parameter(input[from..i].to_owned()),
                    offset: start,
                });
            }
            b'0'..=b'9' => {
                while i < bytes.len() && (bytes[i].is_ascii_digit() || bytes[i] == b'.') {
                    i += 1;
                }
                let text = &input[start..i];
                let token = if text.contains('.') {
                    Token::Number(
                        text.parse()
                            .map_err(|_| AqlError::new(start, "malformed number"))?,
                    )
                } else {
                    Token::Integer(
                        text.parse()
                            .map_err(|_| AqlError::new(start, "number does not fit in i64"))?,
                    )
                };
                out.push(Lexed {
                    token,
                    offset: start,
                });
            }
            c if c.is_ascii_alphabetic() || c == b'_' => {
                // A word may continue into a path (`o/data[at0001]/value`), and
                // scanning the two together keeps `/` out of the operator table.
                //
                // The subtlety is `[`. In a path it is a node predicate and
                // belongs to the token; after a bare alias in FROM
                // (`COMPOSITION c[openEHR-…]`) it opens a *class* predicate that
                // the parser must see. The rule below is therefore: absorb `[`
                // only once a `/` has been seen, because a class predicate never
                // follows a slash and a node predicate always does.
                let mut depth = 0usize;
                let mut seen_slash = false;
                while i < bytes.len() {
                    let b = bytes[i];
                    if b == b'[' {
                        if depth == 0 && !seen_slash {
                            break;
                        }
                        depth += 1;
                    } else if b == b']' {
                        if depth == 0 {
                            break;
                        }
                        depth -= 1;
                    } else if depth == 0 {
                        if b == b'/' {
                            seen_slash = true;
                        } else if !(b.is_ascii_alphanumeric()
                            || b == b'_'
                            || b == b'-'
                            || b == b'.')
                        {
                            break;
                        }
                    }
                    i += 1;
                }
                let text = &input[start..i];
                let token = if text.contains('/') {
                    Token::Path(text.to_owned())
                } else {
                    Token::Word(text.to_owned())
                };
                out.push(Lexed {
                    token,
                    offset: start,
                });
            }
            _ => {
                const SYMBOLS: [&str; 14] = [
                    ">=", "<=", "!=", "(", ")", "{", "}", ",", "=", ">", "<", "[", "]", "*",
                ];
                let Some(sym) = SYMBOLS.iter().find(|s| input[i..].starts_with(**s)) else {
                    return Err(AqlError::new(i, "unexpected character"));
                };
                i += sym.len();
                out.push(Lexed {
                    token: Token::Symbol(sym),
                    offset: start,
                });
            }
        }
    }
    Ok(out)
}

// ---------------------------------------------------------------------------
// AST
// ---------------------------------------------------------------------------

/// A literal value in a query.
#[derive(Debug, Clone, PartialEq)]
pub enum Literal {
    /// A quoted string.
    String(String),
    /// A whole number.
    Integer(i64),
    /// A number with a fractional part.
    Number(f64),
    /// `true` or `false`.
    Boolean(bool),
}

impl fmt::Display for Literal {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::String(v) => write!(f, "'{v}'"),
            Self::Integer(v) => write!(f, "{v}"),
            Self::Number(v) => write!(f, "{v}"),
            Self::Boolean(v) => write!(f, "{v}"),
        }
    }
}

/// A path rooted at a `FROM` alias: `o/data[at0001]/value/magnitude`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IdentifiedPath {
    /// The alias the path is rooted at.
    pub root: String,
    /// The path within it, without a leading `/`.
    pub path: Option<String>,
}

impl fmt::Display for IdentifiedPath {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.path {
            Some(p) => write!(f, "{}/{p}", self.root),
            None => f.write_str(&self.root),
        }
    }
}

/// Anything that can appear where a value is expected.
#[derive(Debug, Clone, PartialEq)]
pub enum Operand {
    /// A path into a matched object.
    Path(IdentifiedPath),
    /// A literal.
    Literal(Literal),
    /// A `$name` parameter.
    Parameter(String),
    /// A function call, such as `COUNT(...)` or `CURRENT_DATE_TIME()`.
    Function {
        /// The function's name, upper-cased.
        name: String,
        /// Its arguments.
        args: Vec<Operand>,
    },
}

impl fmt::Display for Operand {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Path(p) => write!(f, "{p}"),
            Self::Literal(l) => write!(f, "{l}"),
            Self::Parameter(name) => write!(f, "${name}"),
            Self::Function { name, args } => {
                write!(f, "{name}(")?;
                for (i, a) in args.iter().enumerate() {
                    if i > 0 {
                        write!(f, ", ")?;
                    }
                    write!(f, "{a}")?;
                }
                write!(f, ")")
            }
        }
    }
}

/// A comparison operator.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum CompareOp {
    /// `=`
    Equal,
    /// `!=`
    NotEqual,
    /// `>`
    Greater,
    /// `>=`
    GreaterOrEqual,
    /// `<`
    Less,
    /// `<=`
    LessOrEqual,
    /// `LIKE`
    Like,
    /// `MATCHES`
    Matches,
}

impl fmt::Display for CompareOp {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            Self::Equal => "=",
            Self::NotEqual => "!=",
            Self::Greater => ">",
            Self::GreaterOrEqual => ">=",
            Self::Less => "<",
            Self::LessOrEqual => "<=",
            Self::Like => "LIKE",
            Self::Matches => "MATCHES",
        })
    }
}

/// A `WHERE` condition.
#[derive(Debug, Clone, PartialEq)]
pub enum Expr {
    /// `lhs op rhs`
    Compare {
        /// Left operand.
        lhs: Operand,
        /// Operator.
        op: CompareOp,
        /// Right operand.
        rhs: Operand,
    },
    /// `lhs MATCHES {a, b, c}` — written with a value set.
    MatchesSet {
        /// The path being tested.
        lhs: Operand,
        /// The permitted values.
        values: Vec<Operand>,
    },
    /// `EXISTS path`
    Exists(IdentifiedPath),
    /// `a AND b`
    And(Box<Expr>, Box<Expr>),
    /// `a OR b`
    Or(Box<Expr>, Box<Expr>),
    /// `NOT a`
    Not(Box<Expr>),
}

impl fmt::Display for Expr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Compare { lhs, op, rhs } => write!(f, "{lhs} {op} {rhs}"),
            Self::MatchesSet { lhs, values } => {
                write!(f, "{lhs} MATCHES {{")?;
                for (i, v) in values.iter().enumerate() {
                    if i > 0 {
                        write!(f, ", ")?;
                    }
                    write!(f, "{v}")?;
                }
                write!(f, "}}")
            }
            Self::Exists(p) => write!(f, "EXISTS {p}"),
            Self::And(a, b) => write!(f, "({a} AND {b})"),
            Self::Or(a, b) => write!(f, "({a} OR {b})"),
            Self::Not(a) => write!(f, "NOT ({a})"),
        }
    }
}

/// A predicate attached to a class in `FROM`.
#[derive(Debug, Clone, PartialEq)]
pub enum Predicate {
    /// The `[openEHR-EHR-OBSERVATION.x.v2]` shorthand.
    Archetype(String),
    /// A full condition, such as `[ehr_id/value=$ehrUid]`.
    Standard(Box<Expr>),
}

impl fmt::Display for Predicate {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Archetype(id) => write!(f, "[{id}]"),
            Self::Standard(e) => write!(f, "[{e}]"),
        }
    }
}

/// One RM class in the containment tree.
#[derive(Debug, Clone, PartialEq)]
pub struct ClassExpr {
    /// The RM class name, such as `COMPOSITION`.
    pub rm_type: String,
    /// The alias bound to it, if any.
    pub alias: Option<String>,
    /// The predicate narrowing it, if any.
    pub predicate: Option<Predicate>,
}

impl fmt::Display for ClassExpr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.rm_type)?;
        if let Some(alias) = &self.alias {
            write!(f, " {alias}")?;
        }
        if let Some(predicate) = &self.predicate {
            write!(f, "{predicate}")?;
        }
        Ok(())
    }
}

/// The `FROM` containment tree.
#[derive(Debug, Clone, PartialEq)]
pub enum From {
    /// A class on its own.
    Class(ClassExpr),
    /// `left CONTAINS right`, or `left NOT CONTAINS right`.
    Contains {
        /// The containing side.
        left: Box<From>,
        /// Whether the containment is negated.
        negated: bool,
        /// The contained side.
        right: Box<From>,
    },
    /// `a AND b`
    And(Box<From>, Box<From>),
    /// `a OR b`
    Or(Box<From>, Box<From>),
}

impl From {
    /// Every class in the tree, in the order they appear.
    #[must_use]
    pub fn classes(&self) -> Vec<&ClassExpr> {
        let mut out = Vec::new();
        self.collect_classes(&mut out);
        out
    }

    fn collect_classes<'a>(&'a self, out: &mut Vec<&'a ClassExpr>) {
        match self {
            Self::Class(c) => out.push(c),
            Self::Contains { left, right, .. } | Self::And(left, right) | Self::Or(left, right) => {
                left.collect_classes(out);
                right.collect_classes(out);
            }
        }
    }
}

impl fmt::Display for From {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Class(c) => write!(f, "{c}"),
            Self::Contains {
                left,
                negated,
                right,
            } => {
                let not = if *negated { "NOT " } else { "" };
                write!(f, "{left} {not}CONTAINS {right}")
            }
            Self::And(a, b) => write!(f, "({a} AND {b})"),
            Self::Or(a, b) => write!(f, "({a} OR {b})"),
        }
    }
}

/// One projected column.
#[derive(Debug, Clone, PartialEq)]
pub struct SelectColumn {
    /// What is projected.
    pub expr: Operand,
    /// The name it is projected as.
    pub alias: Option<String>,
}

impl fmt::Display for SelectColumn {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.expr)?;
        if let Some(alias) = &self.alias {
            write!(f, " AS {alias}")?;
        }
        Ok(())
    }
}

/// Sort direction.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Direction {
    /// Ascending, the default.
    Ascending,
    /// Descending.
    Descending,
}

/// One `ORDER BY` term.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OrderBy {
    /// The path to sort on.
    pub path: IdentifiedPath,
    /// The direction.
    pub direction: Direction,
}

impl fmt::Display for OrderBy {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.path)?;
        if self.direction == Direction::Descending {
            write!(f, " DESC")?;
        }
        Ok(())
    }
}

/// A parsed AQL query.
///
/// ```
/// use openehr::aql::AqlQuery;
///
/// let q: AqlQuery = "
///     SELECT c/uid/value AS id
///     FROM EHR e[ehr_id/value=$ehrUid]
///         CONTAINS COMPOSITION c[openEHR-EHR-COMPOSITION.encounter.v1]
///     WHERE c/context/start_time > '2026-01-01'
///     ORDER BY c/context/start_time DESC
///     LIMIT 10
/// ".parse().unwrap();
///
/// assert_eq!(q.parameters(), vec!["ehrUid"]);
/// assert_eq!(q.archetype_ids(), vec!["openEHR-EHR-COMPOSITION.encounter.v1"]);
/// assert_eq!(q.limit, Some(10));
/// q.check().unwrap();
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct AqlQuery {
    /// Whether duplicate rows are removed.
    pub distinct: bool,
    /// The `TOP n` limit, if present.
    pub top: Option<u64>,
    /// The projected columns.
    pub columns: Vec<SelectColumn>,
    /// The containment tree.
    pub from: From,
    /// The filter.
    pub where_clause: Option<Expr>,
    /// The sort terms.
    pub order_by: Vec<OrderBy>,
    /// The row limit.
    pub limit: Option<u64>,
    /// The row offset.
    pub offset: Option<u64>,
}

impl AqlQuery {
    /// Every alias bound in `FROM`.
    #[must_use]
    pub fn aliases(&self) -> Vec<&str> {
        self.from
            .classes()
            .into_iter()
            .filter_map(|c| c.alias.as_deref())
            .collect()
    }

    /// Every archetype id named by an archetype predicate, in order.
    ///
    /// This is what an authorisation check wants before the query runs: which
    /// archetypes the caller is asking to read.
    #[must_use]
    pub fn archetype_ids(&self) -> Vec<&str> {
        self.from
            .classes()
            .into_iter()
            .filter_map(|c| match &c.predicate {
                Some(Predicate::Archetype(id)) => Some(id.as_str()),
                _ => None,
            })
            .collect()
    }

    /// Every `$parameter` the query uses, deduplicated, in first-use order.
    #[must_use]
    pub fn parameters(&self) -> Vec<&str> {
        let mut out: Vec<&str> = Vec::new();
        for c in &self.columns {
            walk_operand_parameters(&c.expr, &mut out);
        }
        for class in self.from.classes() {
            if let Some(Predicate::Standard(e)) = &class.predicate {
                walk_expr_parameters(e, &mut out);
            }
        }
        if let Some(w) = &self.where_clause {
            walk_expr_parameters(w, &mut out);
        }
        out
    }

    /// Checks what can be checked without a repository.
    ///
    /// Reports paths rooted at an alias that `FROM` does not bind. This is the
    /// error AQL's syntax makes easiest to write — rename a class alias and
    /// miss one `SELECT` column — and hardest to see, because a query with an
    /// undefined alias is syntactically perfect and returns nothing.
    ///
    /// # Errors
    ///
    /// Returns [`AqlError`] naming the first unbound alias. The offset is 0:
    /// the check runs on the AST, after positions are gone.
    ///
    /// ```
    /// use openehr::aql::AqlQuery;
    ///
    /// let q: AqlQuery = "SELECT o/value FROM COMPOSITION c".parse().unwrap();
    /// // Parses cleanly, returns nothing, and would be debugged at 3am.
    /// assert!(q.check().is_err());
    /// ```
    pub fn check(&self) -> Result<(), AqlError> {
        let aliases = self.aliases();
        let mut unbound: Option<String> = None;
        let mut check_path = |p: &IdentifiedPath| {
            if unbound.is_none() && !aliases.contains(&p.root.as_str()) {
                unbound = Some(p.root.clone());
            }
        };
        for c in &self.columns {
            if let Operand::Path(p) = &c.expr {
                check_path(p);
            }
        }
        for o in &self.order_by {
            check_path(&o.path);
        }
        if let Some(w) = &self.where_clause {
            for p in collect_paths(w) {
                check_path(p);
            }
        }
        match unbound {
            None => Ok(()),
            Some(alias) => Err(AqlError::new(
                0,
                format!("path is rooted at `{alias}`, which FROM does not bind"),
            )),
        }
    }
}

/// Collects `$parameter` names from an operand, deduplicating as it goes.
///
/// Free functions rather than closures: a closure capturing `&mut out` cannot
/// recurse, and the AST is recursive in three places.
fn walk_operand_parameters<'a>(op: &'a Operand, out: &mut Vec<&'a str>) {
    match op {
        Operand::Parameter(name) => {
            if !out.contains(&name.as_str()) {
                out.push(name);
            }
        }
        Operand::Function { args, .. } => {
            for a in args {
                walk_operand_parameters(a, out);
            }
        }
        Operand::Path(_) | Operand::Literal(_) => {}
    }
}

/// Collects `$parameter` names from a condition.
fn walk_expr_parameters<'a>(expr: &'a Expr, out: &mut Vec<&'a str>) {
    match expr {
        Expr::Compare { lhs, rhs, .. } => {
            walk_operand_parameters(lhs, out);
            walk_operand_parameters(rhs, out);
        }
        Expr::MatchesSet { lhs, values } => {
            walk_operand_parameters(lhs, out);
            for v in values {
                walk_operand_parameters(v, out);
            }
        }
        Expr::And(a, b) | Expr::Or(a, b) => {
            walk_expr_parameters(a, out);
            walk_expr_parameters(b, out);
        }
        Expr::Not(a) => walk_expr_parameters(a, out),
        Expr::Exists(_) => {}
    }
}

/// Collects every path referenced by a condition.
fn walk_expr_paths<'a>(expr: &'a Expr, out: &mut Vec<&'a IdentifiedPath>) {
    match expr {
        Expr::Compare { lhs, rhs, .. } => {
            if let Operand::Path(p) = lhs {
                out.push(p);
            }
            if let Operand::Path(p) = rhs {
                out.push(p);
            }
        }
        Expr::MatchesSet { lhs, .. } => {
            if let Operand::Path(p) = lhs {
                out.push(p);
            }
        }
        Expr::Exists(p) => out.push(p),
        Expr::And(a, b) | Expr::Or(a, b) => {
            walk_expr_paths(a, out);
            walk_expr_paths(b, out);
        }
        Expr::Not(a) => walk_expr_paths(a, out),
    }
}

fn collect_paths(expr: &Expr) -> Vec<&IdentifiedPath> {
    let mut out = Vec::new();
    walk_expr_paths(expr, &mut out);
    out
}

impl fmt::Display for AqlQuery {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("SELECT ")?;
        if self.distinct {
            f.write_str("DISTINCT ")?;
        }
        if let Some(top) = self.top {
            write!(f, "TOP {top} ")?;
        }
        for (i, c) in self.columns.iter().enumerate() {
            if i > 0 {
                f.write_str(", ")?;
            }
            write!(f, "{c}")?;
        }
        write!(f, " FROM {}", self.from)?;
        if let Some(w) = &self.where_clause {
            write!(f, " WHERE {w}")?;
        }
        if !self.order_by.is_empty() {
            f.write_str(" ORDER BY ")?;
            for (i, o) in self.order_by.iter().enumerate() {
                if i > 0 {
                    f.write_str(", ")?;
                }
                write!(f, "{o}")?;
            }
        }
        if let Some(limit) = self.limit {
            write!(f, " LIMIT {limit}")?;
        }
        if let Some(offset) = self.offset {
            write!(f, " OFFSET {offset}")?;
        }
        Ok(())
    }
}

impl core::str::FromStr for AqlQuery {
    type Err = AqlError;

    /// # Errors
    ///
    /// Returns [`AqlError`] with the byte offset at which parsing stopped.
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Parser::new(lex(s)?, s.len()).query()
    }
}

// ---------------------------------------------------------------------------
// Parser
// ---------------------------------------------------------------------------

struct Parser {
    tokens: Vec<Lexed>,
    pos: usize,
    end: usize,
}

impl Parser {
    fn new(tokens: Vec<Lexed>, end: usize) -> Self {
        Self {
            tokens,
            pos: 0,
            end,
        }
    }

    fn offset(&self) -> usize {
        self.tokens.get(self.pos).map_or(self.end, |t| t.offset)
    }

    fn peek(&self) -> Option<&Token> {
        self.tokens.get(self.pos).map(|t| &t.token)
    }

    fn next_token(&mut self) -> Option<Token> {
        let t = self.tokens.get(self.pos).map(|t| t.token.clone());
        if t.is_some() {
            self.pos += 1;
        }
        t
    }

    fn peek_keyword(&self, word: &str) -> bool {
        matches!(self.peek(), Some(Token::Word(w)) if w.eq_ignore_ascii_case(word))
    }

    fn eat_keyword(&mut self, word: &str) -> bool {
        if self.peek_keyword(word) {
            self.pos += 1;
            true
        } else {
            false
        }
    }

    fn expect_keyword(&mut self, word: &str) -> Result<(), AqlError> {
        if self.eat_keyword(word) {
            Ok(())
        } else {
            Err(AqlError::new(self.offset(), format!("expected `{word}`")))
        }
    }

    fn eat_symbol(&mut self, sym: &str) -> bool {
        if matches!(self.peek(), Some(Token::Symbol(s)) if *s == sym) {
            self.pos += 1;
            true
        } else {
            false
        }
    }

    fn expect_symbol(&mut self, sym: &str) -> Result<(), AqlError> {
        if self.eat_symbol(sym) {
            Ok(())
        } else {
            Err(AqlError::new(self.offset(), format!("expected `{sym}`")))
        }
    }

    fn query(&mut self) -> Result<AqlQuery, AqlError> {
        self.expect_keyword("SELECT")?;
        let distinct = self.eat_keyword("DISTINCT");
        let top = if self.eat_keyword("TOP") {
            Some(self.integer()?)
        } else {
            None
        };
        if self.eat_symbol("*") {
            return Err(AqlError::new(
                self.offset(),
                "`SELECT *` is not supported: AQL projections must name paths (Q12.9)",
            ));
        }
        let mut columns = vec![self.select_column()?];
        while self.eat_symbol(",") {
            columns.push(self.select_column()?);
        }

        self.expect_keyword("FROM")?;
        let from = self.containment()?;

        let where_clause = if self.eat_keyword("WHERE") {
            Some(self.expr()?)
        } else {
            None
        };

        let mut order_by = Vec::new();
        if self.eat_keyword("ORDER") {
            self.expect_keyword("BY")?;
            loop {
                let path = self.identified_path()?;
                let direction = if self.eat_keyword("DESC") {
                    Direction::Descending
                } else {
                    let _ = self.eat_keyword("ASC");
                    Direction::Ascending
                };
                order_by.push(OrderBy { path, direction });
                if !self.eat_symbol(",") {
                    break;
                }
            }
        }

        let limit = if self.eat_keyword("LIMIT") {
            Some(self.integer()?)
        } else {
            None
        };
        let offset = if self.eat_keyword("OFFSET") {
            Some(self.integer()?)
        } else {
            None
        };

        if self.pos < self.tokens.len() {
            return Err(AqlError::new(self.offset(), "unexpected trailing input"));
        }

        Ok(AqlQuery {
            distinct,
            top,
            columns,
            from,
            where_clause,
            order_by,
            limit,
            offset,
        })
    }

    fn integer(&mut self) -> Result<u64, AqlError> {
        let offset = self.offset();
        match self.next_token() {
            Some(Token::Integer(v)) if v >= 0 => Ok(u64::try_from(v).unwrap_or(0)),
            _ => Err(AqlError::new(offset, "expected a non-negative integer")),
        }
    }

    fn select_column(&mut self) -> Result<SelectColumn, AqlError> {
        let expr = self.operand()?;
        let alias = if self.eat_keyword("AS") {
            let offset = self.offset();
            match self.next_token() {
                Some(Token::Word(w)) => Some(w),
                _ => return Err(AqlError::new(offset, "expected an alias after `AS`")),
            }
        } else {
            None
        };
        Ok(SelectColumn { expr, alias })
    }

    fn operand(&mut self) -> Result<Operand, AqlError> {
        let offset = self.offset();
        match self.next_token() {
            Some(Token::String(v)) => Ok(Operand::Literal(Literal::String(v))),
            Some(Token::Integer(v)) => Ok(Operand::Literal(Literal::Integer(v))),
            Some(Token::Number(v)) => Ok(Operand::Literal(Literal::Number(v))),
            Some(Token::Parameter(name)) => Ok(Operand::Parameter(name)),
            Some(Token::Path(text)) => Ok(Operand::Path(split_path(&text))),
            Some(Token::Word(w)) => {
                if w.eq_ignore_ascii_case("true") {
                    return Ok(Operand::Literal(Literal::Boolean(true)));
                }
                if w.eq_ignore_ascii_case("false") {
                    return Ok(Operand::Literal(Literal::Boolean(false)));
                }
                if self.eat_symbol("(") {
                    let mut args = Vec::new();
                    if !self.eat_symbol(")") {
                        loop {
                            args.push(self.operand()?);
                            if !self.eat_symbol(",") {
                                break;
                            }
                        }
                        self.expect_symbol(")")?;
                    }
                    return Ok(Operand::Function {
                        name: w.to_uppercase(),
                        args,
                    });
                }
                Ok(Operand::Path(IdentifiedPath {
                    root: w,
                    path: None,
                }))
            }
            _ => Err(AqlError::new(offset, "expected a value or a path")),
        }
    }

    fn identified_path(&mut self) -> Result<IdentifiedPath, AqlError> {
        let offset = self.offset();
        match self.next_token() {
            Some(Token::Path(text)) => Ok(split_path(&text)),
            Some(Token::Word(w)) => Ok(IdentifiedPath {
                root: w,
                path: None,
            }),
            _ => Err(AqlError::new(offset, "expected a path")),
        }
    }

    fn containment(&mut self) -> Result<From, AqlError> {
        let mut left = self.containment_primary()?;
        loop {
            if self.peek_keyword("CONTAINS") || self.peek_keyword("NOT") {
                let negated = self.eat_keyword("NOT");
                if negated && !self.peek_keyword("CONTAINS") {
                    return Err(AqlError::new(
                        self.offset(),
                        "expected `CONTAINS` after `NOT`",
                    ));
                }
                self.expect_keyword("CONTAINS")?;
                let right = self.containment()?;
                left = From::Contains {
                    left: Box::new(left),
                    negated,
                    right: Box::new(right),
                };
                continue;
            }
            if self.eat_keyword("AND") {
                let right = self.containment()?;
                left = From::And(Box::new(left), Box::new(right));
                continue;
            }
            if self.eat_keyword("OR") {
                let right = self.containment()?;
                left = From::Or(Box::new(left), Box::new(right));
                continue;
            }
            break;
        }
        Ok(left)
    }

    fn containment_primary(&mut self) -> Result<From, AqlError> {
        if self.eat_symbol("(") {
            let inner = self.containment()?;
            self.expect_symbol(")")?;
            return Ok(inner);
        }
        let offset = self.offset();
        let Some(Token::Word(rm_type)) = self.next_token() else {
            return Err(AqlError::new(offset, "expected an RM class name"));
        };
        if rm_type.eq_ignore_ascii_case("VERSION") {
            return Err(AqlError::new(
                offset,
                "the VERSION class extension is not supported (Q12.9)",
            ));
        }
        // An alias is a bare word that is not one of the words that continue
        // the FROM clause. Checking the keyword list here is what lets
        // `CONTAINS COMPOSITION c CONTAINS OBSERVATION o` parse without
        // treating `CONTAINS` as an alias.
        let alias = match self.peek() {
            Some(Token::Word(w))
                if ![
                    "CONTAINS", "NOT", "AND", "OR", "WHERE", "ORDER", "LIMIT", "OFFSET",
                ]
                .iter()
                .any(|k| w.eq_ignore_ascii_case(k)) =>
            {
                self.next_token();
                match self.tokens[self.pos - 1].token.clone() {
                    Token::Word(w) => Some(w),
                    _ => None,
                }
            }
            _ => None,
        };
        let predicate = if self.eat_symbol("[") {
            let p = self.predicate()?;
            self.expect_symbol("]")?;
            Some(p)
        } else {
            None
        };
        Ok(From::Class(ClassExpr {
            rm_type,
            alias,
            predicate,
        }))
    }

    fn predicate(&mut self) -> Result<Predicate, AqlError> {
        // An archetype shorthand is a single bare word containing `-` and `.`
        // followed immediately by `]`. Anything else is a condition.
        if let Some(Token::Word(w) | Token::Path(w)) = self.peek() {
            let looks_archetype = w.contains('-') && w.contains('.');
            let next_is_close = matches!(
                self.tokens.get(self.pos + 1).map(|t| &t.token),
                Some(Token::Symbol("]"))
            );
            if looks_archetype && next_is_close {
                let id = w.clone();
                self.pos += 1;
                return Ok(Predicate::Archetype(id));
            }
        }
        Ok(Predicate::Standard(Box::new(self.expr()?)))
    }

    fn expr(&mut self) -> Result<Expr, AqlError> {
        let mut left = self.expr_and()?;
        while self.eat_keyword("OR") {
            let right = self.expr_and()?;
            left = Expr::Or(Box::new(left), Box::new(right));
        }
        Ok(left)
    }

    fn expr_and(&mut self) -> Result<Expr, AqlError> {
        let mut left = self.expr_unary()?;
        while self.eat_keyword("AND") {
            let right = self.expr_unary()?;
            left = Expr::And(Box::new(left), Box::new(right));
        }
        Ok(left)
    }

    fn expr_unary(&mut self) -> Result<Expr, AqlError> {
        if self.eat_keyword("NOT") {
            return Ok(Expr::Not(Box::new(self.expr_unary()?)));
        }
        if self.eat_keyword("EXISTS") {
            return Ok(Expr::Exists(self.identified_path()?));
        }
        if self.eat_symbol("(") {
            let inner = self.expr()?;
            self.expect_symbol(")")?;
            return Ok(inner);
        }
        let lhs = self.operand()?;
        if self.eat_keyword("MATCHES") {
            // openEHR writes value sets in braces; real queries in the wild
            // also use parentheses. Both are accepted, and the closing bracket
            // must match the opening one so that `{a, b)` is an error rather
            // than a silently accepted set.
            let close = if self.eat_symbol("{") {
                "}"
            } else if self.eat_symbol("(") {
                ")"
            } else {
                return Err(AqlError::new(
                    self.offset(),
                    "expected `{` or `(` after MATCHES",
                ));
            };
            let mut values = Vec::new();
            if !self.eat_symbol(close) {
                loop {
                    values.push(self.operand()?);
                    if !self.eat_symbol(",") {
                        break;
                    }
                }
                self.expect_symbol(close)?;
            }
            return Ok(Expr::MatchesSet { lhs, values });
        }
        let offset = self.offset();
        let op = if self.eat_keyword("LIKE") {
            CompareOp::Like
        } else {
            match self.next_token() {
                Some(Token::Symbol("=")) => CompareOp::Equal,
                Some(Token::Symbol("!=")) => CompareOp::NotEqual,
                Some(Token::Symbol(">")) => CompareOp::Greater,
                Some(Token::Symbol(">=")) => CompareOp::GreaterOrEqual,
                Some(Token::Symbol("<")) => CompareOp::Less,
                Some(Token::Symbol("<=")) => CompareOp::LessOrEqual,
                _ => return Err(AqlError::new(offset, "expected a comparison operator")),
            }
        };
        let rhs = self.operand()?;
        Ok(Expr::Compare { lhs, op, rhs })
    }
}

fn split_path(text: &str) -> IdentifiedPath {
    match text.split_once('/') {
        Some((root, path)) => IdentifiedPath {
            root: root.to_owned(),
            path: Some(path.to_owned()),
        },
        None => IdentifiedPath {
            root: text.to_owned(),
            path: None,
        },
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const BLOOD_PRESSURE: &str = "
        SELECT
            o/data[at0001]/events[at0006]/data[at0003]/items[at0004]/value/magnitude AS systolic,
            o/data[at0001]/events[at0006]/data[at0003]/items[at0005]/value/magnitude AS diastolic,
            c/context/start_time AS taken
        FROM EHR e[ehr_id/value=$ehrUid]
            CONTAINS COMPOSITION c[openEHR-EHR-COMPOSITION.encounter.v1]
                CONTAINS OBSERVATION o[openEHR-EHR-OBSERVATION.blood_pressure.v2]
        WHERE o/data[at0001]/events[at0006]/data[at0003]/items[at0004]/value/magnitude >= 140
            OR o/data[at0001]/events[at0006]/data[at0003]/items[at0005]/value/magnitude >= 90
        ORDER BY c/context/start_time DESC
        LIMIT 5
    ";

    #[test]
    fn the_canonical_blood_pressure_query_parses() {
        let q: AqlQuery = BLOOD_PRESSURE.parse().unwrap();
        assert_eq!(q.columns.len(), 3);
        assert_eq!(q.aliases(), vec!["e", "c", "o"]);
        assert_eq!(
            q.archetype_ids(),
            vec![
                "openEHR-EHR-COMPOSITION.encounter.v1",
                "openEHR-EHR-OBSERVATION.blood_pressure.v2"
            ]
        );
        assert_eq!(q.parameters(), vec!["ehrUid"]);
        assert_eq!(q.limit, Some(5));
        assert_eq!(q.order_by[0].direction, Direction::Descending);
        q.check().unwrap();
    }

    #[test]
    fn a_parsed_query_reparses_from_its_own_rendering() {
        let q: AqlQuery = BLOOD_PRESSURE.parse().unwrap();
        let rendered = q.to_string();
        let again: AqlQuery = rendered.parse().unwrap_or_else(|e| {
            panic!("re-parse failed: {e}\nrendered: {rendered}");
        });
        // Full structural equality, not a spot check: the rendering normalises
        // whitespace and adds parentheses (see spec/audit.md A-05), and the
        // point of the requirement is that none of that changes the query.
        assert_eq!(again, q);
    }

    #[test]
    fn an_undefined_alias_is_reported_although_the_query_is_well_formed() {
        // The bug this catches: rename `o` to `obs` in FROM and miss one
        // SELECT column. The query parses, runs, and returns nothing.
        let q: AqlQuery = "SELECT o/value FROM COMPOSITION c CONTAINS OBSERVATION obs"
            .parse()
            .unwrap();
        let err = q.check().unwrap_err();
        assert!(err.reason.contains("`o`"), "{err}");

        let good: AqlQuery = "SELECT obs/value FROM COMPOSITION c CONTAINS OBSERVATION obs"
            .parse()
            .unwrap();
        assert!(good.check().is_ok());
    }

    #[test]
    fn aggregates_and_distinct_parse() {
        let q: AqlQuery = "
            SELECT DISTINCT MAX(o/data/events/data/items/value/magnitude) AS peak, COUNT(c) AS n
            FROM EHR CONTAINS COMPOSITION c CONTAINS OBSERVATION o
        "
        .parse()
        .unwrap();
        assert!(q.distinct);
        assert!(matches!(q.columns[0].expr, Operand::Function { .. }));
        if let Operand::Function { name, .. } = &q.columns[0].expr {
            assert_eq!(name, "MAX");
        }
    }

    #[test]
    fn not_contains_parses_and_keeps_its_negation() {
        let q: AqlQuery = "
            SELECT e/ehr_id/value
            FROM EHR e CONTAINS COMPOSITION c[openEHR-EHR-COMPOSITION.referral.v1]
                NOT CONTAINS OBSERVATION o[openEHR-EHR-OBSERVATION.lab_test.v1]
        "
        .parse()
        .unwrap();
        let rendered = q.to_string();
        assert!(rendered.contains("NOT CONTAINS"), "{rendered}");
    }

    #[test]
    fn like_and_offset_parse() {
        let q: AqlQuery = "
            SELECT DISTINCT c/name/value AS n
            FROM EHR e[ehr_id/value=$ehrUid] CONTAINS COMPOSITION c
            WHERE c/context/start_time LIKE '2019-0?-*'
            ORDER BY c/context/start_time
            LIMIT 10 OFFSET 10
        "
        .parse()
        .unwrap();
        assert_eq!(q.offset, Some(10));
        assert_eq!(q.order_by[0].direction, Direction::Ascending);
        assert!(matches!(
            q.where_clause,
            Some(Expr::Compare {
                op: CompareOp::Like,
                ..
            })
        ));
    }

    #[test]
    fn unsupported_constructs_are_refused_and_say_so() {
        for (text, needle) in [
            ("SELECT * FROM COMPOSITION c", "SELECT *"),
            ("SELECT c/uid FROM VERSION v", "VERSION"),
        ] {
            let err = text.parse::<AqlQuery>().unwrap_err();
            assert!(err.reason.contains(needle), "{err}");
            // And the refusal points at the spec section that records it.
            assert!(err.reason.contains("Q12.9"), "{err}");
        }
    }

    #[test]
    fn malformed_queries_report_an_offset() {
        for text in [
            "SELECT",
            "SELECT c/uid",
            "SELECT c/uid FROM",
            "SELECT c/uid FROM COMPOSITION c WHERE",
            "SELECT c/uid FROM COMPOSITION c LIMIT",
            "SELECT c/uid FROM COMPOSITION c EXTRA",
            "SELECT 'unterminated FROM COMPOSITION c",
        ] {
            assert!(text.parse::<AqlQuery>().is_err(), "accepted {text:?}");
        }
    }

    #[test]
    fn parameters_are_collected_from_every_clause_and_deduplicated() {
        let q: AqlQuery = "
            SELECT c/uid/value
            FROM EHR e[ehr_id/value=$ehrUid] CONTAINS COMPOSITION c
            WHERE c/context/start_time > $since AND c/name/value = $name
        "
        .parse()
        .unwrap();
        assert_eq!(q.parameters(), vec!["ehrUid", "since", "name"]);
    }

    #[test]
    fn keywords_are_case_insensitive() {
        let lower: AqlQuery = "select c/uid from COMPOSITION c limit 1".parse().unwrap();
        let upper: AqlQuery = "SELECT c/uid FROM COMPOSITION c LIMIT 1".parse().unwrap();
        assert_eq!(lower, upper);
    }
}