rudb-parse 0.1.1

The SQL lexer, parser and AST, with a textual form that round trips.
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
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
//! From the parse tree to the AST.
//!
//! This is the one module that reads rule names out of the vendored grammar, and that is deliberate
//! containment: an upstream bump that renames a rule breaks a match arm here and nothing else in
//! the repository. `spec/04-architecture.md` section 4.5 says this transformer is ours and has to
//! be total over the rule table, and total is the load bearing word. Every rule reaches a defined
//! answer. For the ones this milestone covers that answer is an AST node, and for the rest it is a
//! `Not implemented` error naming the construct, which is what DuckDB itself answers for syntax it
//! parses and does not support. There is no arm that panics and none that silently drops a clause,
//! because a dropped clause is a wrong answer and a wrong answer is worse than an error.
//!
//! The mechanism that makes it tractable is the default arm. Two thirds of the parse tree is the
//! expression precedence chain, twenty rules of the form `X <- Y Tail*` that exist to make the
//! grammar unambiguous and that carry no meaning once it has been parsed. Rather than name all
//! twenty, the expression walker handles the case where a rule matched something interesting and
//! otherwise descends through any node with exactly one child. That is not a shortcut. It is the
//! statement that a rule with one child said nothing, which is true of every chain link, and it
//! means the twenty first precedence level upstream adds costs us nothing.

use std::collections::HashMap;

use rudb_common::{Error, Result};

use crate::ast::{
    Ast, BinaryOp, CaseArm, Distinct, Expr, ExprRef, JoinKind, LiteralKind, Nulls, Order,
    OrderItem, Quantifier, Query, QueryBody, QueryRef, Select, SelectRef, SetOp, Slice, Source,
    SourceRef, Statement, StrRef, Target, UnaryOp,
};
use crate::generated::rules::PROGRAM;
use crate::matcher::{NONE, Tree, parse_tokens};
use crate::token::{Kind, Token};
use crate::tokenize::tokenize;

/// Parse a script and transform it into the AST.
///
/// The tokens are produced once and handed to both halves. Calling [`crate::parse`] here instead
/// would be shorter and would tokenize the query a second time, which `cargo xtask bench` prices
/// at about a tenth of the whole front end.
pub fn parse_ast(query: &str) -> Result<Ast> {
    let tokens = tokenize(query)?;
    let tree = parse_tokens(query, &tokens, PROGRAM, true)?;
    transform(query, &tokens, &tree)
}

/// Transform a parse tree that has already been produced.
pub fn transform(query: &str, tokens: &[Token], tree: &Tree) -> Result<Ast> {
    let mut transform =
        Transform { query, tokens, tree, ast: Ast::default(), interned: HashMap::new() };
    transform.program(tree.root())?;
    Ok(transform.ast)
}

struct Transform<'a> {
    query: &'a str,
    tokens: &'a [Token],
    tree: &'a Tree,
    ast: Ast,
    interned: HashMap<String, StrRef>,
}

impl<'a> Transform<'a> {
    // The parts that walk the parse tree without caring what it says.

    /// The text a node covers.
    fn text(&self, node: u32) -> &'a str {
        self.tree.text(node, self.query, self.tokens)
    }

    /// The name of the rule a node is.
    fn name(&self, node: u32) -> &'static str {
        self.tree.name(node)
    }

    /// The children of a node.
    ///
    /// Returned with the tree's lifetime rather than the borrow of `self`, so that the caller can
    /// iterate it while calling the `&mut self` methods that build the arena. Copying the `&Tree`
    /// out first is what buys that, and it is why every walker here starts by doing so.
    fn kids(&self, node: u32) -> impl Iterator<Item = u32> + use<'a> {
        let tree = self.tree;
        tree.children(node)
    }

    /// How many children a node has.
    fn count(&self, node: u32) -> usize {
        self.kids(node).count()
    }

    /// The n'th child, or `NONE`.
    fn nth(&self, node: u32, n: usize) -> u32 {
        self.kids(node).nth(n).unwrap_or(NONE)
    }

    /// The first child, or `NONE`.
    fn first(&self, node: u32) -> u32 {
        self.nth(node, 0)
    }

    /// The first child named `name`, or `NONE`.
    ///
    /// Optional parts of a sequence do not leave a placeholder behind, so `SimpleSelect` with a
    /// `WHERE` and no `GROUP BY` has the where clause as its second child and a `SimpleSelect` with
    /// neither has something else there. Positional indexing into an optional sequence is the
    /// single easiest way to write a transformer that is subtly wrong, so nothing here does it.
    fn find(&self, node: u32, name: &str) -> u32 {
        self.kids(node).find(|&kid| self.name(kid) == name).unwrap_or(NONE)
    }

    /// Every leaf of a subtree, in order.
    ///
    /// A leaf is a rule that matched only terminals, which for a name is the identifier itself. It
    /// is how all thirty odd spellings of a qualified name collapse into one walk: whether the
    /// parse said `SchemaQualification ReservedTableQualification ReservedColumnName` or
    /// `IdentifierDot IdentifierDot ColumnName`, the leaves are the parts in order.
    fn leaves(&self, node: u32, out: &mut Vec<u32>) {
        let mut any = false;
        for kid in self.kids(node) {
            any = true;
            self.leaves(kid, &mut *out);
        }
        if !any {
            out.push(node);
        }
    }

    // The parts that build the arena.

    /// Intern a string, returning its index.
    fn intern(&mut self, text: &str) -> StrRef {
        if let Some(&index) = self.interned.get(text) {
            return index;
        }
        let index = u32::try_from(self.ast.strings.len())
            .map_err(|_| Error::internal("more than four billion strings in one query"))
            .unwrap_or(NONE);
        self.ast.strings.push(text.to_string());
        self.interned.insert(text.to_string(), index);
        index
    }

    /// Push an expression and return its index.
    fn push(&mut self, expr: Expr) -> ExprRef {
        let index = self.ast.exprs.len() as u32;
        self.ast.exprs.push(expr);
        index
    }

    /// Push a from item and return its index.
    fn push_source(&mut self, source: Source) -> SourceRef {
        let index = self.ast.sources.len() as u32;
        self.ast.sources.push(source);
        index
    }

    /// Push a query and return its index.
    fn push_query(&mut self, query: Query) -> QueryRef {
        let index = self.ast.queries.len() as u32;
        self.ast.queries.push(query);
        index
    }

    /// Push a select and return its index.
    fn push_select(&mut self, select: Select) -> SelectRef {
        let index = self.ast.selects.len() as u32;
        self.ast.selects.push(select);
        index
    }

    /// Turn a vector of expressions into a slice of the expression list arena.
    fn expr_slice(&mut self, items: Vec<ExprRef>) -> Slice {
        let start = self.ast.expr_lists.len() as u32;
        self.ast.expr_lists.extend(items);
        Slice { start, len: self.ast.expr_lists.len() as u32 - start }
    }

    /// Turn a vector of strings into a slice of the name arena.
    fn part_slice(&mut self, items: Vec<StrRef>) -> Slice {
        let start = self.ast.parts.len() as u32;
        self.ast.parts.extend(items);
        Slice { start, len: self.ast.parts.len() as u32 - start }
    }

    /// The error for a construct the transformer does not cover yet.
    ///
    /// Both halves matter. The text is what the user wrote, which is the only part they can act on,
    /// and the rule name is what we act on, because it is the exact grammar rule to go implement.
    fn unsupported<T>(&self, node: u32) -> Result<T> {
        let text = self.text(node);
        let text = if text.chars().count() > 60 {
            let cut = text.char_indices().nth(60).map_or(text.len(), |(at, _)| at);
            format!("{}...", &text[..cut])
        } else {
            text.to_string()
        };
        Err(Error::not_implemented(format!(
            "{text} is not supported yet, the grammar rule is {}",
            self.name(node)
        )))
    }

    // Names.

    /// One identifier out of a subtree, with the quoting and any trailing dot removed.
    fn identifier(&mut self, node: u32) -> StrRef {
        let mut leaves = Vec::new();
        self.leaves(node, &mut leaves);
        let text = leaves.last().map_or("", |&leaf| self.text(leaf));
        let text = unquote(text.strip_suffix('.').unwrap_or(text));
        self.intern(&text)
    }

    /// Every part of a qualified name, outermost first.
    fn name_parts(&mut self, node: u32) -> Slice {
        let mut leaves = Vec::new();
        self.leaves(node, &mut leaves);
        let mut parts = Vec::with_capacity(leaves.len());
        for leaf in leaves {
            let text = self.text(leaf);
            // A node that covers no tokens is an optional part that was not written, and a bare
            // `*` is the star and not a name part. Neither is a component of anything.
            if text.is_empty() || text == "*" {
                continue;
            }
            let text = unquote(text.strip_suffix('.').unwrap_or(text));
            let interned = self.intern(&text);
            parts.push(interned);
        }
        self.part_slice(parts)
    }

    // Statements.

    /// `Program <- TopLevelStatement*`.
    fn program(&mut self, node: u32) -> Result<()> {
        for top in self.kids(node) {
            // A script that ends in a semicolon produces a last `TopLevelStatement` whose only
            // child is the end of input, because the grammar says `Statement? (';'+ / EndOfInput)`
            // and both halves of that are happy to match nothing. It is a real node and it is not a
            // statement, so it is dropped here rather than pretended away in the matcher.
            let Some(statement) = self.kids(top).find(|&kid| self.name(kid) == "Statement") else {
                continue;
            };
            let statement = self.statement(statement)?;
            self.ast.statements.push(statement);
        }
        Ok(())
    }

    /// `Statement <- SelectStatement / ...`, twenty seven alternatives of which one is done.
    fn statement(&mut self, node: u32) -> Result<Statement> {
        let inner = self.first(node);
        match self.name(inner) {
            "SelectStatement" => {
                let query = self.query(self.first(inner))?;
                Ok(Statement::Query(query))
            }
            _ => self.unsupported(inner),
        }
    }

    /// `SelectStatementInternal <- WithClause? SelectSetOpChain ResultModifiers?`.
    fn query(&mut self, node: u32) -> Result<QueryRef> {
        if self.find(node, "WithClause") != NONE {
            return self.unsupported(self.find(node, "WithClause"));
        }
        let chain = self.find(node, "SelectSetOpChain");
        if chain == NONE {
            return self.unsupported(node);
        }
        let query = self.set_op_chain(chain)?;
        let modifiers = self.find(node, "ResultModifiers");
        if modifiers != NONE {
            self.result_modifiers(query, modifiers)?;
        }
        Ok(query)
    }

    /// `SelectSetOpChain <- IntersectChain SelectSetOpChainTail*`, left associative.
    fn set_op_chain(&mut self, node: u32) -> Result<QueryRef> {
        let mut kids = self.kids(node);
        let head = kids.next().unwrap_or(NONE);
        let mut left = self.intersect_chain(head)?;
        for tail in kids {
            // `SelectSetOpChainTail <- SetopClause IntersectChain`.
            let clause = self.first(tail);
            let (op, quantifier, by_name) = self.setop_clause(clause)?;
            let right = self.intersect_chain(self.nth(tail, 1))?;
            left = self.push_query(Query::bare(QueryBody::SetOp {
                op,
                quantifier,
                by_name,
                left,
                right,
            }));
        }
        Ok(left)
    }

    /// `IntersectChain <- SelectAtom IntersectChainTail*`, which binds tighter than union.
    fn intersect_chain(&mut self, node: u32) -> Result<QueryRef> {
        let mut kids = self.kids(node);
        let head = kids.next().unwrap_or(NONE);
        let mut left = self.select_atom(head)?;
        for tail in kids {
            // `IntersectChainTail <- SetIntersectClause SelectAtom`.
            let clause = self.first(tail);
            let quantifier = self.quantifier(self.find(clause, "DistinctOrAll"));
            let right = self.select_atom(self.nth(tail, 1))?;
            left = self.push_query(Query::bare(QueryBody::SetOp {
                op: SetOp::Intersect,
                quantifier,
                by_name: false,
                left,
                right,
            }));
        }
        Ok(left)
    }

    /// `SetopClause <- SetopType DistinctOrAll? ByName?`.
    fn setop_clause(&mut self, node: u32) -> Result<(SetOp, Quantifier, bool)> {
        let kind = self.find(node, "SetopType");
        let op = match self.name(self.first(kind)) {
            "SetopUnion" => SetOp::Union,
            "SetopExcept" => SetOp::Except,
            _ => return self.unsupported(kind),
        };
        let quantifier = self.quantifier(self.find(node, "DistinctOrAll"));
        Ok((op, quantifier, self.find(node, "ByName") != NONE))
    }

    /// `DistinctOrAll <- DistinctKeyword / AllKeyword`, absent included.
    fn quantifier(&self, node: u32) -> Quantifier {
        if node == NONE {
            return Quantifier::Unstated;
        }
        match self.name(self.first(node)) {
            "DistinctKeyword" => Quantifier::Distinct,
            "AllKeyword" => Quantifier::All,
            _ => Quantifier::Unstated,
        }
    }

    /// `SelectAtom <- SelectParens / SelectStatementType`.
    fn select_atom(&mut self, node: u32) -> Result<QueryRef> {
        let inner = self.first(node);
        match self.name(inner) {
            // `SelectParens <- Parens(SelectStatementInternal)`, so the parens buy a query that
            // carries its own order by and limit and nothing else.
            "SelectParens" => self.query(self.first(inner)),
            "SelectStatementType" => {
                let kind = self.first(inner);
                match self.name(kind) {
                    "OptionalParensSimpleSelect" => {
                        let select = self.simple_select(self.unwrap_parens(kind))?;
                        Ok(self.push_query(Query::bare(QueryBody::Select(select))))
                    }
                    _ => self.unsupported(kind),
                }
            }
            _ => self.unsupported(inner),
        }
    }

    /// `OptionalParensSimpleSelect <- SimpleSelectParens / SimpleSelect`, down to the select.
    fn unwrap_parens(&self, node: u32) -> u32 {
        let mut node = self.first(node);
        while self.name(node) == "SimpleSelectParens" {
            node = self.first(node);
        }
        node
    }

    /// `ResultModifiers <- OrderByClause? LimitOffset?`.
    fn result_modifiers(&mut self, query: QueryRef, node: u32) -> Result<()> {
        let order = self.find(node, "OrderByClause");
        if order != NONE {
            let (items, all) = self.order_by(order)?;
            let start = self.ast.order_items.len() as u32;
            self.ast.order_items.extend(items);
            self.ast.queries[query as usize].order_by =
                Slice { start, len: self.ast.order_items.len() as u32 - start };
            self.ast.queries[query as usize].order_by_all = all;
        }
        let limit = self.find(node, "LimitOffset");
        if limit != NONE {
            self.limit_offset(query, self.first(limit))?;
        }
        Ok(())
    }

    /// The four spellings of a limit and an offset, in either order and either one alone.
    fn limit_offset(&mut self, query: QueryRef, node: u32) -> Result<()> {
        match self.name(node) {
            "LimitOffsetClause" | "OffsetLimitClause" => {
                let limit = self.find(node, "LimitClause");
                if limit != NONE {
                    self.limit(query, limit)?;
                }
                let offset = self.find(node, "OffsetClause");
                if offset != NONE {
                    self.offset(query, offset)?;
                }
                Ok(())
            }
            _ => self.unsupported(node),
        }
    }

    /// `LimitClause <- 'LIMIT' LimitValue`.
    fn limit(&mut self, query: QueryRef, node: u32) -> Result<()> {
        let value = self.first(node);
        let inner = self.first(value);
        match self.name(inner) {
            // `LIMIT ALL` is no limit at all, which is what an absent limit already means.
            "LimitAll" => Ok(()),
            // `LimitExpression <- Expression '%'?`. The percent sign is a terminal so it leaves no
            // node behind, and the only thing that says it was written is the text of the rule that
            // matched it.
            "LimitExpression" => {
                let expr = self.expr(self.first(inner))?;
                self.ast.queries[query as usize].limit = expr;
                self.ast.queries[query as usize].limit_percent = self.text(inner).ends_with('%');
                Ok(())
            }
            "LimitLiteralPercent" => {
                let expr = self.expr(self.first(inner))?;
                self.ast.queries[query as usize].limit = expr;
                self.ast.queries[query as usize].limit_percent = true;
                Ok(())
            }
            _ => self.unsupported(inner),
        }
    }

    /// `OffsetClause <- 'OFFSET' OffsetValue`, where `OffsetValue <- Expression RowOrRows?`.
    fn offset(&mut self, query: QueryRef, node: u32) -> Result<()> {
        let value = self.first(node);
        let expr = self.expr(self.first(value))?;
        self.ast.queries[query as usize].offset = expr;
        Ok(())
    }

    /// `SimpleSelect <- SelectFrom WhereClause? GroupByClause? HavingClause? WindowClause?
    /// QualifyClause? SampleClause?`.
    fn simple_select(&mut self, node: u32) -> Result<SelectRef> {
        for name in ["WindowClause", "QualifyClause", "SampleClause"] {
            let clause = self.find(node, name);
            if clause != NONE {
                return self.unsupported(clause);
            }
        }
        let mut select = Select::empty();
        self.select_from(&mut select, self.first(node))?;
        let filter = self.find(node, "WhereClause");
        if filter != NONE {
            select.filter = self.expr(self.first(filter))?;
        }
        let group = self.find(node, "GroupByClause");
        if group != NONE {
            self.group_by(&mut select, self.first(group))?;
        }
        let having = self.find(node, "HavingClause");
        if having != NONE {
            select.having = self.expr(self.first(having))?;
        }
        Ok(self.push_select(select))
    }

    /// `SelectFrom <- SelectFromClause / FromSelectClause`, which is `SELECT ... FROM ...` and
    /// DuckDB's `FROM ... SELECT ...` written the other way round.
    fn select_from(&mut self, select: &mut Select, node: u32) -> Result<()> {
        let clause = self.first(node);
        let targets = self.find(clause, "SelectClause");
        let from = self.find(clause, "FromClause");
        if from != NONE {
            select.from = self.sources(from)?;
        }
        if targets == NONE {
            // `FROM t` on its own. DuckDB reads it as `SELECT * FROM t`, and inventing the star
            // here rather than in the binder keeps the binder from having to know the shape of the
            // clause that was missing.
            let star = self.push(Expr::Star { qualifier: Slice::default() });
            let start = self.ast.targets.len() as u32;
            self.ast.targets.push(Target { expr: star, alias: NONE });
            select.targets = Slice { start, len: 1 };
            return Ok(());
        }
        self.select_clause(select, targets)
    }

    /// `SelectClause <- 'SELECT' DistinctClause? TargetList?`.
    fn select_clause(&mut self, select: &mut Select, node: u32) -> Result<()> {
        let distinct = self.find(node, "DistinctClause");
        if distinct != NONE {
            let inner = self.first(distinct);
            select.distinct = match self.name(inner) {
                // `SELECT ALL` is the default spelled out.
                "DistinctAll" => Distinct::No,
                "DistinctOn" => {
                    let on = self.find(inner, "DistinctOnTargets");
                    if on == NONE {
                        Distinct::Yes
                    } else {
                        let mut items = Vec::new();
                        for kid in self.kids(on) {
                            items.push(self.expr(kid)?);
                        }
                        Distinct::On(self.expr_slice(items))
                    }
                }
                _ => return self.unsupported(inner),
            };
        }
        let list = self.find(node, "TargetList");
        if list == NONE {
            return Ok(());
        }
        let mut targets = Vec::new();
        for kid in self.kids(list) {
            targets.push(self.target(kid)?);
        }
        let start = self.ast.targets.len() as u32;
        self.ast.targets.extend(targets);
        select.targets = Slice { start, len: self.ast.targets.len() as u32 - start };
        Ok(())
    }

    /// `AliasedExpression <- ColIdExpression / ExpressionAsCollabel / ExpressionOptIdentifier`.
    fn target(&mut self, node: u32) -> Result<Target> {
        let inner = self.first(node);
        match self.name(inner) {
            // `ColIdExpression <- ColId ':' Expression`, the alias written first.
            "ColIdExpression" => {
                let alias = self.identifier(self.first(inner));
                let expr = self.expr(self.nth(inner, 1))?;
                Ok(Target { expr, alias })
            }
            "ExpressionAsCollabel" => {
                let expr = self.expr(self.first(inner))?;
                let alias = self.identifier(self.nth(inner, 1));
                Ok(Target { expr, alias })
            }
            "ExpressionOptIdentifier" => {
                let expr = self.expr(self.first(inner))?;
                let alias =
                    if self.count(inner) > 1 { self.identifier(self.nth(inner, 1)) } else { NONE };
                Ok(Target { expr, alias })
            }
            _ => self.unsupported(inner),
        }
    }

    /// `GroupByClause <- 'GROUP' 'BY' GroupByExpressions`.
    fn group_by(&mut self, select: &mut Select, node: u32) -> Result<()> {
        let inner = self.first(node);
        match self.name(inner) {
            "GroupByAll" => {
                select.group_by_all = true;
                Ok(())
            }
            "GroupByList" => {
                let mut items = Vec::new();
                for kid in self.kids(inner) {
                    // `GroupByExpression <- EmptyGroupingItem / CubeOrRollupClause /
                    // GroupingSetsClause / GroupByBaseExpression`.
                    let expression = self.first(kid);
                    if self.name(expression) != "GroupByBaseExpression" {
                        return self.unsupported(expression);
                    }
                    items.push(self.expr(self.first(expression))?);
                }
                select.group_by = self.expr_slice(items);
                Ok(())
            }
            _ => self.unsupported(inner),
        }
    }

    /// `OrderByClause <- 'ORDER' 'BY' OrderByExpressions`, where `OrderByExpressions <- OrderByAll
    /// / OrderByExpressionList`.
    fn order_by(&mut self, node: u32) -> Result<(Vec<OrderItem>, bool)> {
        let inner = self.first(self.first(node));
        match self.name(inner) {
            "OrderByAll" => {
                let (order, nulls) = self.sort_options(inner);
                Ok((vec![OrderItem { expr: NONE, order, nulls }], true))
            }
            "OrderByExpressionList" => {
                let mut items = Vec::new();
                for kid in self.kids(inner) {
                    // `OrderByExpression <- Expression DescOrAsc? NullsFirstOrLast?`.
                    let expr = self.expr(self.first(kid))?;
                    let (order, nulls) = self.sort_options(kid);
                    items.push(OrderItem { expr, order, nulls });
                }
                Ok((items, false))
            }
            _ => self.unsupported(inner),
        }
    }

    /// The direction and the null placement of one sort key, either of which may be unwritten.
    fn sort_options(&self, node: u32) -> (Order, Nulls) {
        let direction = self.find(node, "DescOrAsc");
        let order = if direction == NONE {
            Order::Unstated
        } else if self.name(self.first(direction)) == "DescendingOrder" {
            Order::Descending
        } else {
            Order::Ascending
        };
        let placement = self.find(node, "NullsFirstOrLast");
        let nulls = if placement == NONE {
            Nulls::Unstated
        } else if self.name(self.first(placement)) == "NullsFirst" {
            Nulls::First
        } else {
            Nulls::Last
        };
        (order, nulls)
    }

    // From clauses.

    /// `FromClause <- 'FROM' List(TableRef)`.
    fn sources(&mut self, node: u32) -> Result<Slice> {
        let mut items = Vec::new();
        for kid in self.kids(node) {
            items.push(self.table_ref(kid)?);
        }
        let start = self.ast.source_lists.len() as u32;
        self.ast.source_lists.extend(items);
        Ok(Slice { start, len: self.ast.source_lists.len() as u32 - start })
    }

    /// `TableRef <- InnerTableRef JoinOrPivot*`, left associative like the set operators.
    fn table_ref(&mut self, node: u32) -> Result<SourceRef> {
        let mut kids = self.kids(node);
        let head = kids.next().unwrap_or(NONE);
        let mut left = self.inner_table_ref(head)?;
        for tail in kids {
            let clause = self.first(tail);
            if self.name(clause) != "JoinClause" {
                return self.unsupported(clause);
            }
            left = self.join(left, self.first(clause))?;
        }
        Ok(left)
    }

    /// `InnerTableRef <- ValuesRef / TableFunction / TableSubquery / BaseTableRef / ParensTableRef`.
    fn inner_table_ref(&mut self, node: u32) -> Result<SourceRef> {
        let inner = if self.name(node) == "InnerTableRef" { self.first(node) } else { node };
        match self.name(inner) {
            "BaseTableRef" => {
                if self.find(inner, "TableAliasColon") != NONE {
                    return self.unsupported(inner);
                }
                for name in ["AtClause", "SampleClause"] {
                    let clause = self.find(inner, name);
                    if clause != NONE {
                        return self.unsupported(clause);
                    }
                }
                let name = self.name_parts(self.find(inner, "BaseTableName"));
                let (alias, columns) = self.table_alias(self.find(inner, "TableAlias"));
                Ok(self.push_source(Source::Table { name, alias, columns }))
            }
            "TableSubquery" => {
                if self.find(inner, "TableAliasColon") != NONE
                    || self.find(inner, "Lateral") != NONE
                {
                    return self.unsupported(inner);
                }
                // `SubqueryReference <- Parens(SelectStatementInternal)`.
                let reference = self.find(inner, "SubqueryReference");
                let query = self.query(self.first(reference))?;
                let (alias, columns) = self.table_alias(self.find(inner, "TableAlias"));
                Ok(self.push_source(Source::Subquery { query, alias, columns }))
            }
            "ParensTableRef" => {
                if self.find(inner, "TableAliasColon") != NONE
                    || self.find(inner, "SampleClause") != NONE
                    || self.find(inner, "TableAlias") != NONE
                {
                    return self.unsupported(inner);
                }
                self.table_ref(self.find(inner, "TableRef"))
            }
            _ => self.unsupported(inner),
        }
    }

    /// `TableAlias <- TableAliasAs / TableAliasWithoutAs`, either with a column alias list.
    fn table_alias(&mut self, node: u32) -> (StrRef, Slice) {
        if node == NONE {
            return (NONE, Slice::default());
        }
        let inner = self.first(node);
        let alias = self.identifier(self.first(inner));
        let list = self.find(inner, "ColumnAliases");
        if list == NONE {
            return (alias, Slice::default());
        }
        let mut columns = Vec::new();
        for kid in self.kids(list) {
            let name = self.identifier(kid);
            columns.push(name);
        }
        (alias, self.part_slice(columns))
    }

    /// `JoinClause <- JoinByClause / RegularJoinClause / JoinWithoutOnClause / NearestJoinClause`.
    fn join(&mut self, left: SourceRef, node: u32) -> Result<SourceRef> {
        match self.name(node) {
            // `RegularJoinClause <- Asof? JoinType? 'JOIN' TableRef JoinQualifier`.
            "RegularJoinClause" => {
                if self.find(node, "Asof") != NONE {
                    return self.unsupported(node);
                }
                let kind = self.join_type(self.find(node, "JoinType"));
                let right = self.table_ref(self.find(node, "TableRef"))?;
                let (on, using) = self.join_qualifier(self.find(node, "JoinQualifier"))?;
                Ok(self.push_source(Source::Join { left, right, kind, natural: false, on, using }))
            }
            // `JoinWithoutOnClause <- JoinPrefix 'JOIN' InnerTableRef`, which is cross, natural and
            // positional. Those three are exactly the joins that carry no condition.
            "JoinWithoutOnClause" => {
                let prefix = self.first(self.find(node, "JoinPrefix"));
                let (kind, natural) = match self.name(prefix) {
                    "CrossJoinPrefix" => (JoinKind::Cross, false),
                    "PositionalJoinPrefix" => (JoinKind::Positional, false),
                    "NaturalJoinPrefix" => (self.join_type(self.find(prefix, "JoinType")), true),
                    _ => return self.unsupported(prefix),
                };
                let right = self.inner_table_ref(self.find(node, "InnerTableRef"))?;
                Ok(self.push_source(Source::Join {
                    left,
                    right,
                    kind,
                    natural,
                    on: NONE,
                    using: Slice::default(),
                }))
            }
            _ => self.unsupported(node),
        }
    }

    /// `JoinType <- FullJoin / LeftJoin / RightJoin / SemiJoin / AntiJoin / InnerJoin`, absent
    /// meaning inner, which is what SQL has always meant by a bare `JOIN`.
    fn join_type(&self, node: u32) -> JoinKind {
        if node == NONE {
            return JoinKind::Inner;
        }
        match self.name(self.first(node)) {
            "FullJoin" => JoinKind::Full,
            "LeftJoin" => JoinKind::Left,
            "RightJoin" => JoinKind::Right,
            "SemiJoin" => JoinKind::Semi,
            "AntiJoin" => JoinKind::Anti,
            _ => JoinKind::Inner,
        }
    }

    /// `JoinQualifier <- OnClause / UsingClause`.
    fn join_qualifier(&mut self, node: u32) -> Result<(ExprRef, Slice)> {
        let inner = self.first(node);
        match self.name(inner) {
            "OnClause" => Ok((self.expr(self.first(inner))?, Slice::default())),
            "UsingClause" => {
                let mut columns = Vec::new();
                for kid in self.kids(inner) {
                    let name = self.identifier(kid);
                    columns.push(name);
                }
                Ok((NONE, self.part_slice(columns)))
            }
            _ => self.unsupported(inner),
        }
    }

    // Expressions.

    /// One expression, from wherever in the precedence chain it starts.
    ///
    /// The loop is the whole design. A rule that says something gets an arm, a rule with exactly
    /// one child said nothing and is stepped through, and anything else is an error naming itself.
    /// The chain rules never get an arm for their one child case, which is why adding a precedence
    /// level upstream costs nothing here.
    fn expr(&mut self, node: u32) -> Result<ExprRef> {
        let mut node = node;
        loop {
            let count = self.count(node);
            let name = self.name(node);
            match name {
                "LogicalOrExpression" if count > 1 => return self.logical(node, BinaryOp::Or),
                "LogicalAndExpression" if count > 1 => return self.logical(node, BinaryOp::And),
                "LogicalNotExpression" if count > 1 => return self.logical_not(node),
                "IsExpression" if count > 1 => return self.is_expression(node),
                "BetweenInLikeExpression" if count > 1 => return self.between_in_like(node),
                "PrefixExpression" if count > 1 => return self.prefix(node),
                "BaseExpression" if count > 1 => return self.indirection(node),
                "LambdaArrowExpression"
                | "IsDistinctFromExpression"
                | "ComparisonExpression"
                | "OtherOperatorExpression"
                | "BitwiseExpression"
                | "AdditiveExpression"
                | "MultiplicativeExpression"
                | "ExponentiationExpression"
                | "CollateExpression"
                | "AtTimeZoneExpression"
                    if count > 1 =>
                {
                    return self.tail_chain(node);
                }
                "ColumnReference" => {
                    let name = self.name_parts(node);
                    return Ok(self.push(Expr::Column { name }));
                }
                "StarExpression" => return self.star(node),
                "NumberLiteral" => {
                    let text = self.text(node).to_string();
                    let text = self.intern(&text);
                    return Ok(self.push(Expr::Literal { kind: LiteralKind::Number, text }));
                }
                "StringLiteral" => {
                    let text = self.string_value(node);
                    let text = self.intern(&text);
                    return Ok(self.push(Expr::Literal { kind: LiteralKind::String, text }));
                }
                "NullLiteral" | "TrueLiteral" | "FalseLiteral" => {
                    let kind = match name {
                        "NullLiteral" => LiteralKind::Null,
                        "TrueLiteral" => LiteralKind::True,
                        _ => LiteralKind::False,
                    };
                    return Ok(self.push(Expr::Literal { kind, text: NONE }));
                }
                "FunctionExpression" => return self.function(node),
                "CastExpression" => return self.cast(node),
                "CaseExpression" => return self.case(node),
                "ParenthesisExpression" => return self.row(node),
                "SubqueryExpression" => return self.subquery(node),
                _ if count == 1 => node = self.first(node),
                _ => return self.unsupported(node),
            }
        }
    }

    /// `X <- Y XTail*` where `XTail <- Operator Y`, the shape ten precedence levels share.
    fn tail_chain(&mut self, node: u32) -> Result<ExprRef> {
        let mut kids = self.kids(node);
        let head = kids.next().unwrap_or(NONE);
        let mut left = self.expr(head)?;
        for tail in kids {
            let operator = self.first(tail);
            let op = self.binary_op(operator)?;
            // `ComparisonExpressionTail <- ComparisonOperator NotExpression? BetweenInLikeExpression`
            // is the one tail with an optional middle, so the operand is the last child and not the
            // second one. Taking the last is right for every tail and wrong for none.
            let operand = self.kids(tail).last().unwrap_or(NONE);
            if self.count(tail) > 2 {
                return self.unsupported(tail);
            }
            let right = self.expr(operand)?;
            left = self.push(Expr::Binary { op, left, right });
        }
        Ok(left)
    }

    /// Which infix operator a tail's operator node is.
    fn binary_op(&mut self, node: u32) -> Result<BinaryOp> {
        // The operator rules nest: `ComparisonOperator` over `OperatorGreaterThan` over the symbol
        // itself. Every one of them covers the same tokens, so the text is the same at every level
        // and reading it once at the top is enough. The name is not, which is why the bottom of the
        // chain is walked to as well: `OtherOperator` says nothing and `OperatorLiteral` says
        // everything, and they are three levels apart.
        let mut leaf = node;
        while self.count(leaf) == 1 {
            leaf = self.first(leaf);
        }
        let text = self.text(node);
        let upper = text.to_ascii_uppercase();
        let op = match upper.as_str() {
            "OR" => BinaryOp::Or,
            "AND" => BinaryOp::And,
            "=" | "==" => BinaryOp::Eq,
            "!=" | "<>" => BinaryOp::NotEq,
            "<" => BinaryOp::Lt,
            ">" => BinaryOp::Gt,
            "<=" => BinaryOp::LtEq,
            ">=" => BinaryOp::GtEq,
            "+" => BinaryOp::Add,
            "-" => BinaryOp::Subtract,
            "*" => BinaryOp::Multiply,
            "/" => BinaryOp::Divide,
            "//" => BinaryOp::IntegerDivide,
            "%" => BinaryOp::Modulo,
            "^" | "**" => BinaryOp::Power,
            "&" => BinaryOp::BitAnd,
            "|" => BinaryOp::BitOr,
            "<<" => BinaryOp::ShiftLeft,
            ">>" => BinaryOp::ShiftRight,
            "||" => BinaryOp::Concat,
            "COLLATE" => BinaryOp::Collate,
            "->" => BinaryOp::Arrow,
            "->>" => BinaryOp::LongArrow,
            "@>" => BinaryOp::Contains,
            "<@" => BinaryOp::ContainedBy,
            "&&" => BinaryOp::Overlaps,
            "^@" => BinaryOp::StartsWith,
            "<<=" => BinaryOp::InetContainedByOrEq,
            ">>=" => BinaryOp::InetContainsOrEq,
            _ if self.name(leaf) == "AtTimeZoneOperator" => BinaryOp::AtTimeZone,
            // `IsDistinctFromOp <- 'IS' 'NOT'? 'DISTINCT' 'FROM'`, told apart by the middle word,
            // which is not in the tree because keywords are terminals.
            _ if self.name(leaf) == "IsDistinctFromOp" => {
                if upper.split_whitespace().any(|word| word == "NOT") {
                    BinaryOp::IsNotDistinctFrom
                } else {
                    BinaryOp::IsDistinctFrom
                }
            }
            // `OperatorLiteral` is the open end of the operator set. Its body in the grammar text
            // says `Identifier`, but it is one of the 24 rules whose body the matcher does not
            // walk and the matcher it is overridden to is the bare operator one, so what it
            // actually accepts is any run of operator characters that is not already a token.
            // `a <=> b` is such a run, DuckDB resolves it as a two argument function of that name,
            // and rejecting it here would reject SQL DuckDB accepts.
            _ if self.name(leaf) == "OperatorLiteral" => {
                let interned = self.intern(text);
                BinaryOp::Named(interned)
            }
            _ => return self.unsupported(node),
        };
        Ok(op)
    }

    /// `LogicalOrExpression <- LogicalAndExpression LogicalOrExpressionTail*`, and the `AND` twin.
    ///
    /// Separate from the other tails because the tail here is `'OR' LogicalAndExpression` with the
    /// keyword as a terminal, so there is no operator node to read and the operator is the rule.
    fn logical(&mut self, node: u32, op: BinaryOp) -> Result<ExprRef> {
        let mut kids = self.kids(node);
        let head = kids.next().unwrap_or(NONE);
        let mut left = self.expr(head)?;
        for tail in kids {
            let right = self.expr(self.first(tail))?;
            left = self.push(Expr::Binary { op, left, right });
        }
        Ok(left)
    }

    /// `LogicalNotExpression <- NotExpression? IsExpression`, where `NotExpression <- NotKeyword+`.
    ///
    /// The plus matters. `NOT NOT x` is two nodes in the parse tree and two negations in the AST,
    /// and folding them here would be an optimizer decision taken in the parser.
    fn logical_not(&mut self, node: u32) -> Result<ExprRef> {
        let negations = self.count(self.first(node));
        let mut expr = self.expr(self.nth(node, 1))?;
        for _ in 0..negations {
            expr = self.push(Expr::Unary { op: UnaryOp::Not, operand: expr });
        }
        Ok(expr)
    }

    /// `IsExpression <- IsDistinctFromExpression IsTest*`, the postfix null and boolean tests.
    fn is_expression(&mut self, node: u32) -> Result<ExprRef> {
        let mut kids = self.kids(node);
        let head = kids.next().unwrap_or(NONE);
        let mut expr = self.expr(head)?;
        for test in kids {
            let inner = self.first(test);
            let negated = self.text(inner).to_ascii_uppercase().contains("NOT");
            let op = match self.name(inner) {
                "NotNull" => UnaryOp::IsNotNull,
                "IsNull" => UnaryOp::IsNull,
                // `IsLiteral <- 'IS' 'NOT'? IsLiteralValue`, and the value rule is one more level
                // down again because it is a choice of four and not four alternatives inlined.
                "IsLiteral" => match self.name(self.first(self.first(inner))) {
                    "NullLiteral" if negated => UnaryOp::IsNotNull,
                    "NullLiteral" => UnaryOp::IsNull,
                    "TrueLiteral" if negated => UnaryOp::IsNotTrue,
                    "TrueLiteral" => UnaryOp::IsTrue,
                    "FalseLiteral" if negated => UnaryOp::IsNotFalse,
                    "FalseLiteral" => UnaryOp::IsFalse,
                    "UnknownLiteral" if negated => UnaryOp::IsNotUnknown,
                    "UnknownLiteral" => UnaryOp::IsUnknown,
                    _ => return self.unsupported(inner),
                },
                _ => return self.unsupported(inner),
            };
            expr = self.push(Expr::Unary { op, operand: expr });
        }
        Ok(expr)
    }

    /// `BetweenInLikeExpression <- OtherOperatorExpression BetweenInLikeOp?`.
    fn between_in_like(&mut self, node: u32) -> Result<ExprRef> {
        let operand = self.expr(self.first(node))?;
        // `BetweenInLikeOp <- 'NOT'? BetweenInLikeOpExpression`. The `NOT` is a terminal, so what
        // says it was written is that the op node covers a token the inner node does not.
        let op = self.nth(node, 1);
        let negated = self.text(op).to_ascii_uppercase().starts_with("NOT");
        let inner = self.first(self.first(op));
        match self.name(inner) {
            // `BetweenClause <- 'BETWEEN' x 'AND' y`.
            "BetweenClause" => {
                let low = self.expr(self.first(inner))?;
                let high = self.expr(self.nth(inner, 1))?;
                Ok(self.push(Expr::Between { operand, low, high, negated }))
            }
            // `InClause <- 'IN' InExpression`.
            "InClause" => {
                let expression = self.first(self.first(inner));
                match self.name(expression) {
                    "InExpressionList" => {
                        let mut items = Vec::new();
                        for kid in self.kids(expression) {
                            items.push(self.expr(kid)?);
                        }
                        let list = self.expr_slice(items);
                        Ok(self.push(Expr::In { operand, list, negated }))
                    }
                    _ => self.unsupported(expression),
                }
            }
            // `LikeClause <- LikeVariations x EscapeClause?`.
            "LikeClause" => {
                if self.find(inner, "EscapeClause") != NONE {
                    return self.unsupported(inner);
                }
                let variation = self.name(self.first(self.first(inner)));
                let op = match (variation, negated) {
                    ("LikeToken", false) | ("NotLikeOp", true) => BinaryOp::Like,
                    ("LikeToken", true) | ("NotLikeOp", false) => BinaryOp::NotLike,
                    ("ILikeToken", false) | ("NotILikeOp", true) => BinaryOp::ILike,
                    ("ILikeToken", true) | ("NotILikeOp", false) => BinaryOp::NotILike,
                    // Glob and the bare regex match have no negated spelling of their own in
                    // `LikeVariations`, so a `NOT` in front of either stays an explicit negation.
                    ("GlobToken", _) => BinaryOp::Glob,
                    ("RegexMatchToken", _) => BinaryOp::Regex,
                    ("SimilarToToken", false) | ("NotSimilarToOp", true) => BinaryOp::SimilarTo,
                    ("SimilarToToken", true) | ("NotSimilarToOp", false) => BinaryOp::NotSimilarTo,
                    ("RegexInsensitiveMatchToken", false)
                    | ("NotRegexInsensitiveMatchOp", true) => BinaryOp::RegexInsensitive,
                    ("RegexInsensitiveMatchToken", true)
                    | ("NotRegexInsensitiveMatchOp", false) => BinaryOp::NotRegexInsensitive,
                    _ => return self.unsupported(inner),
                };
                let right = self.expr(self.nth(inner, 1))?;
                let expr = self.push(Expr::Binary { op, left: operand, right });
                // The like family folds its negation into the operator because it has a spelling
                // for the negated form. Glob and regex do not, so theirs stays where it was.
                if negated && matches!(op, BinaryOp::Glob | BinaryOp::Regex) {
                    return Ok(self.push(Expr::Unary { op: UnaryOp::Not, operand: expr }));
                }
                Ok(expr)
            }
            _ => self.unsupported(inner),
        }
    }

    /// `PrefixExpression <- PrefixOperator* BaseExpression`, applied right to left.
    fn prefix(&mut self, node: u32) -> Result<ExprRef> {
        let kids: Vec<u32> = self.kids(node).collect();
        let mut expr = self.expr(kids[kids.len() - 1])?;
        for &operator in kids[..kids.len() - 1].iter().rev() {
            let op = match self.name(self.first(operator)) {
                "MinusPrefixOperator" => UnaryOp::Negate,
                "PlusPrefixOperator" => UnaryOp::Plus,
                "TildePrefixOperator" => UnaryOp::BitNot,
                _ => return self.unsupported(operator),
            };
            expr = self.push(Expr::Unary { op, operand: expr });
        }
        Ok(expr)
    }

    /// `BaseExpression <- SingleExpression IndirectionList?`, the postfix chain.
    fn indirection(&mut self, node: u32) -> Result<ExprRef> {
        let mut expr = self.expr(self.first(node))?;
        for step in self.kids(self.nth(node, 1)) {
            let inner = self.first(step);
            expr = match self.name(inner) {
                // `CastOperator <- '::' Type`.
                "CastOperator" => {
                    let text = self.text(self.first(inner)).to_string();
                    let ty = self.intern(&text);
                    self.push(Expr::Cast { operand: expr, ty, try_cast: false })
                }
                "DotOperator" => {
                    let dot = self.first(inner);
                    match self.name(dot) {
                        // `DotColumnOperator <- '.' ColLabel`, which DuckDB resolves as a call of
                        // `struct_extract`. Writing it as that call rather than as its own node
                        // keeps the binder from needing a rule for a thing that is already a
                        // function.
                        "DotColumnOperator" => {
                            let field = self.identifier(self.first(dot));
                            let text = self.ast.string(field).to_string();
                            let literal = self.intern(&text);
                            let key = self
                                .push(Expr::Literal { kind: LiteralKind::String, text: literal });
                            let name = self.function_name("struct_extract");
                            let args = self.expr_slice(vec![expr, key]);
                            self.push(Expr::Function { name, args, distinct: false })
                        }
                        // `DotMethodOperator <- '.' MethodExpression`, where `x.f(a)` is `f(x, a)`.
                        "DotMethodOperator" => {
                            let method = self.first(dot);
                            let text = self.text(self.first(method)).to_string();
                            let text = unquote(&text);
                            let name = self.function_name(&text);
                            let mut args = vec![expr];
                            let list = self.find(method, "MethodExpressionArguments");
                            if list != NONE {
                                let inner = self.first(list);
                                let arguments = self.find(inner, "MethodFunctionArguments");
                                if arguments != NONE {
                                    for kid in self.kids(arguments) {
                                        args.push(self.argument(kid)?);
                                    }
                                }
                            }
                            let args = self.expr_slice(args);
                            self.push(Expr::Function { name, args, distinct: false })
                        }
                        _ => return self.unsupported(dot),
                    }
                }
                // `SliceExpression <- '[' SliceBound ']'`, one index or a range.
                "SliceExpression" => {
                    let bound = self.first(inner);
                    let has_end = self.find(bound, "EndSliceBound") != NONE;
                    let has_step = self.find(bound, "StepSliceBound") != NONE;
                    if has_end || has_step {
                        return self.unsupported(inner);
                    }
                    let index = self.expr(self.first(bound))?;
                    let name = self.function_name("array_extract");
                    let args = self.expr_slice(vec![expr, index]);
                    self.push(Expr::Function { name, args, distinct: false })
                }
                // `PostfixOperator <- '!'`.
                "PostfixOperator" => {
                    self.push(Expr::Unary { op: UnaryOp::Factorial, operand: expr })
                }
                _ => return self.unsupported(inner),
            };
        }
        Ok(expr)
    }

    /// A one part function name, for the calls the transformer invents rather than reads.
    fn function_name(&mut self, name: &str) -> Slice {
        let interned = self.intern(name);
        self.part_slice(vec![interned])
    }

    /// `StarExpression <- StarQualifierList? '*' ExcludeList? ReplaceList? RenameList?`.
    fn star(&mut self, node: u32) -> Result<ExprRef> {
        for name in ["ExcludeList", "ReplaceList", "RenameList"] {
            let list = self.find(node, name);
            if list != NONE {
                return self.unsupported(list);
            }
        }
        let qualifier = self.find(node, "StarQualifierList");
        let qualifier =
            if qualifier == NONE { Slice::default() } else { self.name_parts(qualifier) };
        Ok(self.push(Expr::Star { qualifier }))
    }

    /// `FunctionExpression <- FunctionIdentifier FunctionExpressionArguments WithinGroupClause?
    /// FilterClause? ExportClause? OverClause?`.
    fn function(&mut self, node: u32) -> Result<ExprRef> {
        for name in ["WithinGroupClause", "FilterClause", "ExportClause", "OverClause"] {
            let clause = self.find(node, name);
            if clause != NONE {
                return self.unsupported(clause);
            }
        }
        let name = self.name_parts(self.first(node));
        // `FunctionExpressionArguments <- Parens(FunctionExpressionArgumentList)` and
        // `FunctionExpressionArgumentList <- DistinctOrAll? FunctionArgumentList? OrderByClause?
        // IgnoreOrRespectNulls?`, so a call with no arguments still has both wrappers.
        let list = self.first(self.nth(node, 1));
        for name in ["OrderByClause", "IgnoreOrRespectNulls"] {
            let clause = self.find(list, name);
            if clause != NONE {
                return self.unsupported(clause);
            }
        }
        let distinct = self.quantifier(self.find(list, "DistinctOrAll")) == Quantifier::Distinct;
        let mut args = Vec::new();
        let arguments = self.find(list, "FunctionArgumentList");
        if arguments != NONE {
            for kid in self.kids(arguments) {
                args.push(self.argument(kid)?);
            }
        }
        let args = self.expr_slice(args);
        Ok(self.push(Expr::Function { name, args, distinct }))
    }

    /// `FunctionArgument <- NamedFunctionArgument / PositionalFunctionArgument`.
    fn argument(&mut self, node: u32) -> Result<ExprRef> {
        let inner = self.first(node);
        match self.name(inner) {
            "PositionalFunctionArgument" => self.expr(self.first(inner)),
            _ => self.unsupported(inner),
        }
    }

    /// `CastExpression <- CastOrTryCast Parens(CastArguments)`.
    fn cast(&mut self, node: u32) -> Result<ExprRef> {
        let try_cast = self.name(self.first(self.first(node))) == "TryCastKeyword";
        // `CastArguments <- Expression 'AS' Type`.
        let arguments = self.nth(node, 1);
        let operand = self.expr(self.first(arguments))?;
        let text = self.text(self.nth(arguments, 1)).to_string();
        let ty = self.intern(&text);
        Ok(self.push(Expr::Cast { operand, ty, try_cast }))
    }

    /// `CaseExpression <- 'CASE' Expression? CaseWhenThen+ CaseElse? 'END'`.
    fn case(&mut self, node: u32) -> Result<ExprRef> {
        let mut operand = NONE;
        let mut arms = Vec::new();
        let mut otherwise = NONE;
        for kid in self.kids(node) {
            match self.name(kid) {
                // `CaseWhenThen <- 'WHEN' Expression 'THEN' Expression`.
                "CaseWhenThen" => {
                    let when = self.expr(self.first(kid))?;
                    let then = self.expr(self.nth(kid, 1))?;
                    arms.push(CaseArm { when, then });
                }
                // `CaseElse <- 'ELSE' Expression`.
                "CaseElse" => otherwise = self.expr(self.first(kid))?,
                // The bare `Expression` before the first `WHEN`, which makes it a simple case.
                _ => operand = self.expr(kid)?,
            }
        }
        let start = self.ast.case_arms.len() as u32;
        self.ast.case_arms.extend(arms);
        let arms = Slice { start, len: self.ast.case_arms.len() as u32 - start };
        Ok(self.push(Expr::Case { operand, arms, otherwise }))
    }

    /// `ParenthesisExpression <- Parens(List(Expression)?)`, which is a row value.
    ///
    /// One item is not a row. `(a)` is `a` in every dialect and reading it as a one column row
    /// would change what `(a) = (b)` means.
    fn row(&mut self, node: u32) -> Result<ExprRef> {
        let mut items = Vec::new();
        for kid in self.kids(node) {
            items.push(self.expr(kid)?);
        }
        if items.len() == 1 {
            return Ok(items[0]);
        }
        let items = self.expr_slice(items);
        Ok(self.push(Expr::Row { items }))
    }

    /// `SubqueryExpression <- SubqueryNot? SubqueryExists? SubqueryReference`.
    fn subquery(&mut self, node: u32) -> Result<ExprRef> {
        if self.find(node, "SubqueryNot") != NONE || self.find(node, "SubqueryExists") != NONE {
            return self.unsupported(node);
        }
        let reference = self.find(node, "SubqueryReference");
        let query = self.query(self.first(reference))?;
        Ok(self.push(Expr::Subquery { query }))
    }

    /// The value of a string literal, with the quotes gone and the escapes resolved.
    ///
    /// A literal can be several tokens. `'a' 'b'` on two lines is one literal that is `ab`, which is
    /// the SQL standard's rule and DuckDB's, so the node is decoded token by token rather than by
    /// taking its text and stripping the outside.
    fn string_value(&self, node: u32) -> String {
        let span = self.tree.node(node);
        let mut value = String::new();
        for token in &self.tokens[span.start as usize..span.end as usize] {
            if token.kind != Kind::String {
                continue;
            }
            let text = token.text(self.query);
            match text.strip_prefix('\'').and_then(|rest| rest.strip_suffix('\'')) {
                Some(body) => value.push_str(&body.replace("''", "'")),
                None => value.push_str(text),
            }
        }
        value
    }
}

/// Strip the quoting off an identifier.
///
/// DuckDB does not fold identifier case at any point, quoted or not, so this only removes the
/// quotes and resolves the doubled ones. Anything else would be the parser deciding what a name is.
fn unquote(text: &str) -> String {
    match text.strip_prefix('"').and_then(|rest| rest.strip_suffix('"')) {
        Some(body) => body.replace("\"\"", "\""),
        None => text.to_string(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::corpus::CORPUS;
    use crate::matcher::parse;

    /// The AST written back out as text, which is what the assertions below read.
    ///
    /// Not a SQL printer and not trying to be. It is deliberately not valid SQL: operators are
    /// spelled with the name of the variant and every binary node is parenthesised, so that a test
    /// asserting on this text is asserting on the shape of the tree and not on a formatting choice.
    /// `a - b - c` and `a - (b - c)` have to look different here or the test that tells them apart
    /// is not a test.
    fn show(ast: &Ast, expr: ExprRef) -> String {
        if expr == NONE {
            return "-".to_string();
        }
        let list = |slice: Slice| {
            ast.expr_list(slice).iter().map(|&item| show(ast, item)).collect::<Vec<_>>().join(", ")
        };
        match ast.expr(expr) {
            Expr::Star { qualifier } if qualifier.is_empty() => "*".to_string(),
            Expr::Star { qualifier } => format!("{}.*", ast.name_text(qualifier)),
            Expr::Column { name } => ast.name_text(name),
            Expr::Literal { kind, text } => match kind {
                LiteralKind::Number => ast.string(text).to_string(),
                LiteralKind::String => format!("'{}'", ast.string(text)),
                other => format!("{other:?}").to_uppercase(),
            },
            Expr::Unary { op, operand } => format!("({op:?} {})", show(ast, operand)),
            Expr::Binary { op, left, right } => {
                let op = match op {
                    BinaryOp::Named(name) => ast.string(name).to_string(),
                    other => format!("{other:?}"),
                };
                format!("({} {op} {})", show(ast, left), show(ast, right))
            }
            Expr::Function { name, args, distinct } => {
                let distinct = if distinct { "DISTINCT " } else { "" };
                format!("{}({distinct}{})", ast.name_text(name), list(args))
            }
            Expr::Cast { operand, ty, try_cast } => {
                let word = if try_cast { "TRY_CAST" } else { "CAST" };
                format!("{word}({} AS {})", show(ast, operand), ast.string(ty))
            }
            Expr::Case { operand, arms, otherwise } => {
                let arms = ast
                    .arm_list(arms)
                    .iter()
                    .map(|arm| format!("WHEN {} THEN {}", show(ast, arm.when), show(ast, arm.then)))
                    .collect::<Vec<_>>()
                    .join(" ");
                format!("CASE {} {arms} ELSE {} END", show(ast, operand), show(ast, otherwise))
            }
            Expr::Between { operand, low, high, negated } => {
                let not = if negated { "NOT " } else { "" };
                format!(
                    "({not}{} BETWEEN {} AND {})",
                    show(ast, operand),
                    show(ast, low),
                    show(ast, high)
                )
            }
            Expr::In { operand, list: items, negated } => {
                let not = if negated { "NOT " } else { "" };
                format!("({not}{} IN [{}])", show(ast, operand), list(items))
            }
            Expr::Row { items } => format!("ROW({})", list(items)),
            Expr::Subquery { query } => format!("({})", show_query(ast, query)),
        }
    }

    /// One from item written back out.
    fn show_source(ast: &Ast, source: SourceRef) -> String {
        let alias = |alias: StrRef| match alias {
            NONE => String::new(),
            other => format!(" AS {}", ast.string(other)),
        };
        match ast.source(source) {
            Source::Table { name, alias: name_alias, .. } => {
                format!("{}{}", ast.name_text(name), alias(name_alias))
            }
            Source::Subquery { query, alias: query_alias, .. } => {
                format!("({}){}", show_query(ast, query), alias(query_alias))
            }
            Source::Join { left, right, kind, natural, on, using } => {
                let natural = if natural { "NATURAL " } else { "" };
                let on = if on == NONE { String::new() } else { format!(" ON {}", show(ast, on)) };
                let using = if using.is_empty() {
                    String::new()
                } else {
                    format!(" USING ({})", ast.name_text(using))
                };
                format!(
                    "({} {natural}{kind:?} JOIN {}{on}{using})",
                    show_source(ast, left),
                    show_source(ast, right)
                )
            }
        }
    }

    /// One query written back out.
    fn show_query(ast: &Ast, index: QueryRef) -> String {
        let query = ast.query(index);
        let list = |slice: Slice| {
            ast.expr_list(slice).iter().map(|&item| show(ast, item)).collect::<Vec<_>>().join(", ")
        };
        let mut out = match query.body {
            QueryBody::SetOp { op, quantifier, by_name, left, right } => {
                let by_name = if by_name { " BY NAME" } else { "" };
                format!(
                    "({} {op:?} {quantifier:?}{by_name} {})",
                    show_query(ast, left),
                    show_query(ast, right)
                )
            }
            QueryBody::Select(index) => {
                let select = ast.select(index);
                let distinct = match select.distinct {
                    Distinct::No => String::new(),
                    Distinct::Yes => " DISTINCT".to_string(),
                    Distinct::On(on) => format!(" DISTINCT ON ({})", list(on)),
                };
                let targets = ast
                    .target_list(select.targets)
                    .iter()
                    .map(|target| match target.alias {
                        NONE => show(ast, target.expr),
                        alias => format!("{} AS {}", show(ast, target.expr), ast.string(alias)),
                    })
                    .collect::<Vec<_>>()
                    .join(", ");
                let mut out = format!("SELECT{distinct} {targets}");
                if !select.from.is_empty() {
                    let from = ast
                        .source_list(select.from)
                        .iter()
                        .map(|&source| show_source(ast, source))
                        .collect::<Vec<_>>()
                        .join(", ");
                    out += &format!(" FROM {from}");
                }
                if select.filter != NONE {
                    out += &format!(" WHERE {}", show(ast, select.filter));
                }
                if select.group_by_all {
                    out += " GROUP BY ALL";
                } else if !select.group_by.is_empty() {
                    out += &format!(" GROUP BY {}", list(select.group_by));
                }
                if select.having != NONE {
                    out += &format!(" HAVING {}", show(ast, select.having));
                }
                out
            }
        };
        if query.order_by_all {
            out += " ORDER BY ALL";
        } else if !query.order_by.is_empty() {
            let items = ast
                .order_list(query.order_by)
                .iter()
                .map(|item| format!("{} {:?} {:?}", show(ast, item.expr), item.order, item.nulls))
                .collect::<Vec<_>>()
                .join(", ");
            out += &format!(" ORDER BY {items}");
        }
        if query.limit != NONE {
            let percent = if query.limit_percent { "%" } else { "" };
            out += &format!(" LIMIT {}{percent}", show(ast, query.limit));
        }
        if query.offset != NONE {
            out += &format!(" OFFSET {}", show(ast, query.offset));
        }
        out
    }

    /// One statement, transformed and written back out.
    fn round(query: &str) -> String {
        let ast = parse_ast(query).unwrap_or_else(|error| panic!("{query}: {error}"));
        assert_eq!(ast.statements.len(), 1, "{query} is one statement");
        let Statement::Query(index) = ast.statements[0];
        show_query(&ast, index)
    }

    #[test]
    fn the_query_m0_has_to_run_transforms() {
        assert_eq!(round("SELECT * FROM t WHERE x > 5"), "SELECT * FROM t WHERE (x Gt 5)");
    }

    #[test]
    fn every_statement_in_the_corpus_gets_a_defined_answer() {
        // The point of the test is the word defined. Forty of these are statement kinds and
        // clauses this milestone does not cover, and the requirement is not that they work, it is
        // that they fail by saying so. A panic, a silently dropped clause or an internal error
        // would each be a different bug and all three would be invisible without this.
        let mut done = 0;
        for query in CORPUS {
            match parse_ast(query) {
                Ok(ast) => {
                    assert_eq!(ast.statements.len(), 1, "{query}");
                    done += 1;
                }
                Err(error) => {
                    let message = error.to_string();
                    assert!(
                        message.starts_with("Not implemented Error"),
                        "{query} failed with {message}, which is not a not-implemented error"
                    );
                }
            }
        }
        // Not an assertion about the right number. It is a ratchet: this only moves up, and the
        // day it moves down somebody has taken a construct out without meaning to.
        assert!(done >= 19, "only {done} of the corpus transforms, which is fewer than it was");
    }

    #[test]
    fn the_ast_is_far_smaller_than_the_parse_tree() {
        let query = CORPUS[4];
        let tree = parse(query).unwrap();
        let ast = parse_ast(query).unwrap();
        // The twenty precedence levels are the difference. Every one of them is a node in the
        // parse tree for every expression at every depth, and none of them survives into the AST.
        assert!(
            ast.node_count() * 20 < tree.arena_len(),
            "{} ast nodes against {} parse nodes",
            ast.node_count(),
            tree.arena_len()
        );
    }

    #[test]
    fn precedence_comes_out_of_the_chain_and_into_the_tree() {
        assert_eq!(round("SELECT 1 + 2 * 3"), "SELECT (1 Add (2 Multiply 3))");
        assert_eq!(round("SELECT (1 + 2) * 3"), "SELECT ((1 Add 2) Multiply 3)");
        assert_eq!(round("SELECT 1 + 2 + 3"), "SELECT ((1 Add 2) Add 3)");
        assert_eq!(round("SELECT 1 - 2 - 3"), "SELECT ((1 Subtract 2) Subtract 3)");
        assert_eq!(
            round("SELECT a OR b AND c"),
            "SELECT (a Or (b And c))",
            "and binds tighter than or"
        );
    }

    #[test]
    fn a_double_negation_is_two_nodes_and_not_none() {
        // Folding it would be an optimizer decision and this is not the optimizer. It also would
        // not be safe in general: `NOT NOT x` on a null is still null and on a non boolean it is
        // still an error, and both of those have to survive to the binder to be reported.
        assert_eq!(round("SELECT NOT NOT a"), "SELECT (Not (Not a))");
    }

    #[test]
    fn a_parenthesised_single_expression_is_not_a_row() {
        assert_eq!(round("SELECT (a)"), "SELECT a");
        assert_eq!(round("SELECT (a, b)"), "SELECT ROW(a, b)");
    }

    #[test]
    fn the_three_ways_to_write_an_alias_all_arrive() {
        assert_eq!(round("SELECT a AS b"), "SELECT a AS b");
        assert_eq!(round("SELECT a b"), "SELECT a AS b");
        assert_eq!(round("SELECT b: a"), "SELECT a AS b");
        assert_eq!(round("SELECT a"), "SELECT a", "and no alias when none was written");
    }

    #[test]
    fn a_from_with_no_select_selects_everything() {
        // DuckDB's own shorthand. Inventing the star here rather than in the binder means the
        // binder never has to know that the clause it is looking at was the one that was missing.
        assert_eq!(round("FROM t"), "SELECT * FROM t");
        assert_eq!(round("FROM t SELECT a"), "SELECT a FROM t");
    }

    #[test]
    fn joins_nest_to_the_left() {
        assert_eq!(
            round("SELECT * FROM a JOIN b ON a.i = b.i LEFT JOIN c USING (k)"),
            "SELECT * FROM ((a Inner JOIN b ON (a.i Eq b.i)) Left JOIN c USING (k))"
        );
        assert_eq!(
            round("SELECT * FROM a NATURAL JOIN b"),
            "SELECT * FROM (a NATURAL Inner JOIN b)"
        );
        assert_eq!(round("SELECT * FROM a CROSS JOIN b"), "SELECT * FROM (a Cross JOIN b)");
        assert_eq!(
            round("SELECT * FROM a POSITIONAL JOIN b"),
            "SELECT * FROM (a Positional JOIN b)"
        );
        assert_eq!(round("SELECT * FROM a, b"), "SELECT * FROM a, b", "a comma is not a join node");
    }

    #[test]
    fn a_qualified_name_keeps_its_parts_however_it_was_spelled() {
        // Five grammar rules can produce a column reference and they disagree about which
        // component is a schema and which is a table. None of that is decidable without the
        // catalog, so the AST holds the parts and the binder decides.
        assert_eq!(round("SELECT a"), "SELECT a");
        assert_eq!(round("SELECT t.a"), "SELECT t.a");
        assert_eq!(round("SELECT s.t.a"), "SELECT s.t.a");
        assert_eq!(round("SELECT c.s.t.a"), "SELECT c.s.t.a");
        assert_eq!(round("SELECT * FROM s.t"), "SELECT * FROM s.t");
    }

    #[test]
    fn a_star_can_be_qualified() {
        assert_eq!(round("SELECT *"), "SELECT *");
        assert_eq!(round("SELECT t.*"), "SELECT t.*");
        assert_eq!(round("SELECT s.t.*"), "SELECT s.t.*");
    }

    #[test]
    fn a_quoted_identifier_keeps_its_case_and_loses_its_quotes() {
        // DuckDB does not fold identifier case at any point, quoted or not, which the tokenizer
        // work established by reading the source. So the only thing to do here is take the quotes
        // off and resolve the doubled ones.
        let ast = parse_ast("SELECT \"Mixed Case\", \"a\"\"b\"").unwrap();
        assert_eq!(ast.strings[0], "Mixed Case");
        assert_eq!(ast.strings[1], "a\"b");
    }

    #[test]
    fn a_string_literal_is_decoded_and_adjacent_ones_are_joined() {
        assert_eq!(round("SELECT 'it''s'"), "SELECT 'it's'");
        assert_eq!(round("SELECT 'a'\n'b'"), "SELECT 'ab'", "the standard's adjacency rule");
    }

    #[test]
    fn the_null_and_boolean_tests_are_postfix_unary_operators() {
        assert_eq!(round("SELECT x IS NULL"), "SELECT (IsNull x)");
        assert_eq!(round("SELECT x IS NOT NULL"), "SELECT (IsNotNull x)");
        assert_eq!(round("SELECT x ISNULL"), "SELECT (IsNull x)");
        assert_eq!(round("SELECT x NOTNULL"), "SELECT (IsNotNull x)");
        assert_eq!(round("SELECT x IS TRUE"), "SELECT (IsTrue x)");
        assert_eq!(round("SELECT x IS NOT FALSE"), "SELECT (IsNotFalse x)");
        assert_eq!(round("SELECT x IS DISTINCT FROM y"), "SELECT (x IsDistinctFrom y)");
        assert_eq!(round("SELECT x IS NOT DISTINCT FROM y"), "SELECT (x IsNotDistinctFrom y)");
    }

    #[test]
    fn the_like_family_folds_its_negation_into_the_operator() {
        assert_eq!(round("SELECT x LIKE 'a'"), "SELECT (x Like 'a')");
        assert_eq!(round("SELECT x NOT LIKE 'a'"), "SELECT (x NotLike 'a')");
        assert_eq!(round("SELECT x ILIKE 'a'"), "SELECT (x ILike 'a')");
        assert_eq!(round("SELECT x ~~ 'a'"), "SELECT (x Like 'a')", "the operator spelling");
        assert_eq!(round("SELECT x !~~ 'a'"), "SELECT (x NotLike 'a')");
        assert_eq!(round("SELECT x SIMILAR TO 'a'"), "SELECT (x SimilarTo 'a')");
        // Glob has no negated operator to fold into, so the negation stays where it was written.
        assert_eq!(round("SELECT x NOT GLOB 'a'"), "SELECT (Not (x Glob 'a'))");
    }

    #[test]
    fn between_and_in_carry_their_negation_as_a_flag() {
        assert_eq!(round("SELECT x BETWEEN 1 AND 2"), "SELECT (x BETWEEN 1 AND 2)");
        assert_eq!(round("SELECT x NOT BETWEEN 1 AND 2"), "SELECT (NOT x BETWEEN 1 AND 2)");
        assert_eq!(round("SELECT x IN (1, 2)"), "SELECT (x IN [1, 2])");
        assert_eq!(round("SELECT x NOT IN (1, 2)"), "SELECT (NOT x IN [1, 2])");
    }

    #[test]
    fn both_spellings_of_a_cast_are_the_same_node() {
        assert_eq!(round("SELECT CAST(x AS BIGINT)"), "SELECT CAST(x AS BIGINT)");
        assert_eq!(round("SELECT x::BIGINT"), "SELECT CAST(x AS BIGINT)");
        assert_eq!(round("SELECT TRY_CAST(x AS BIGINT)"), "SELECT TRY_CAST(x AS BIGINT)");
        assert_eq!(
            round("SELECT x::DECIMAL(18, 3)"),
            "SELECT CAST(x AS DECIMAL(18, 3))",
            "the type is kept as text because parsing it is the type system's job"
        );
    }

    #[test]
    fn a_case_keeps_its_arms_in_order() {
        assert_eq!(
            round("SELECT CASE WHEN a THEN 1 WHEN b THEN 2 ELSE 3 END"),
            "SELECT CASE - WHEN a THEN 1 WHEN b THEN 2 ELSE 3 END"
        );
        assert_eq!(
            round("SELECT CASE x WHEN 1 THEN 'a' END"),
            "SELECT CASE x WHEN 1 THEN 'a' ELSE - END",
            "a simple case keeps the operand and a missing else is not an implicit null yet"
        );
    }

    #[test]
    fn a_field_access_and_a_method_call_are_ordinary_function_calls() {
        // Which is what DuckDB makes of them too. Giving each its own AST node would mean the
        // binder needs a rule for something the function resolver already handles.
        assert_eq!(round("SELECT (f(x)).y"), "SELECT struct_extract(f(x), 'y')");
        assert_eq!(round("SELECT a[1]"), "SELECT array_extract(a, 1)");
    }

    #[test]
    fn an_aggregate_keeps_its_distinct() {
        assert_eq!(round("SELECT count(*)"), "SELECT count(*)");
        assert_eq!(round("SELECT count(DISTINCT x)"), "SELECT count(DISTINCT x)");
        assert_eq!(round("SELECT count(ALL x)"), "SELECT count(x)");
        assert_eq!(round("SELECT main.count(x)"), "SELECT main.count(x)");
    }

    #[test]
    fn the_modifiers_hang_off_the_query_and_not_off_the_select() {
        // `a UNION b ORDER BY x` sorts the union. Putting the order by on the select would have
        // made that unrepresentable, which is why the grammar puts it outside the chain and why
        // the AST follows.
        assert_eq!(
            round("SELECT 1 UNION ALL SELECT 2 ORDER BY 1"),
            "(SELECT 1 Union All SELECT 2) ORDER BY 1 Unstated Unstated"
        );
        assert_eq!(
            round("SELECT a FROM t UNION SELECT b FROM u EXCEPT SELECT c FROM v"),
            "((SELECT a FROM t Union Unstated SELECT b FROM u) Except Unstated SELECT c FROM v)",
            "set operators are left associative"
        );
        assert_eq!(
            round("SELECT 1 UNION SELECT 2 INTERSECT SELECT 3"),
            "(SELECT 1 Union Unstated (SELECT 2 Intersect Unstated SELECT 3))",
            "and intersect binds tighter than the other two"
        );
    }

    #[test]
    fn the_sort_and_limit_clauses_keep_what_was_written() {
        assert_eq!(
            round("SELECT a FROM t ORDER BY a"),
            "SELECT a FROM t ORDER BY a Unstated Unstated"
        );
        assert_eq!(
            round("SELECT a FROM t ORDER BY a DESC NULLS LAST"),
            "SELECT a FROM t ORDER BY a Descending Last"
        );
        assert_eq!(round("SELECT a FROM t ORDER BY ALL"), "SELECT a FROM t ORDER BY ALL");
        assert_eq!(round("SELECT a FROM t GROUP BY ALL"), "SELECT a FROM t GROUP BY ALL");
        assert_eq!(round("SELECT a FROM t LIMIT 10 OFFSET 5"), "SELECT a FROM t LIMIT 10 OFFSET 5");
        assert_eq!(round("SELECT a FROM t OFFSET 5 LIMIT 10"), "SELECT a FROM t LIMIT 10 OFFSET 5");
        assert_eq!(round("SELECT a FROM t LIMIT 10%"), "SELECT a FROM t LIMIT 10%");
        assert_eq!(round("SELECT a FROM t LIMIT ALL"), "SELECT a FROM t", "which is no limit");
    }

    #[test]
    fn a_subquery_appears_in_both_places_it_can() {
        assert_eq!(
            round("SELECT * FROM (SELECT x FROM t) AS s"),
            "SELECT * FROM (SELECT x FROM t) AS s"
        );
        assert_eq!(round("SELECT (SELECT 1)"), "SELECT (SELECT 1)");
    }

    #[test]
    fn distinct_on_keeps_its_expressions() {
        assert_eq!(round("SELECT DISTINCT a"), "SELECT DISTINCT a");
        assert_eq!(round("SELECT ALL a"), "SELECT a", "which is the default written out");
        assert_eq!(round("SELECT DISTINCT ON (a, b) a"), "SELECT DISTINCT ON (a, b) a");
    }

    #[test]
    fn an_operator_the_dialect_does_not_name_is_kept_by_name() {
        // The grammar text says `OperatorLiteral <- Identifier`, which reads as though any bare
        // word could be written infix. It cannot. That rule is one of the 24 the matcher overrides
        // and it is overridden to the bare operator matcher, so what it takes is a run of operator
        // characters. Believing the body here would have produced a transformer that accepted
        // `a foo b`, which DuckDB rejects.
        assert_eq!(round("SELECT a <=> b"), "SELECT (a <=> b)");
        assert!(parse_ast("SELECT a foo b").is_err(), "a bare word is not an operator");
    }

    #[test]
    fn a_script_is_a_list_of_statements() {
        let ast = parse_ast("SELECT 1; SELECT 2;").unwrap();
        assert_eq!(ast.statements.len(), 2);
        // A trailing semicolon makes an empty top level statement in the parse tree, because the
        // grammar's `Statement? (';'+ / EndOfInput)` is happy with nothing on both sides. It is
        // dropped here rather than pretended away in the matcher.
        let Statement::Query(second) = ast.statements[1];
        assert_eq!(show_query(&ast, second), "SELECT 2");
    }

    #[test]
    fn an_unsupported_construct_names_itself_and_what_was_written() {
        let error = parse_ast("CREATE TABLE t (a INTEGER)").unwrap_err().to_string();
        assert!(error.starts_with("Not implemented Error"), "{error}");
        assert!(error.contains("CREATE TABLE t (a INTEGER)"), "{error}");
        assert!(error.contains("CreateStatement"), "{error}");
    }

    #[test]
    fn a_long_construct_is_cut_short_in_the_message() {
        let query =
            format!("CREATE TABLE t AS SELECT {} FROM u", "averylongcolumnname, ".repeat(8));
        let error = parse_ast(&query).unwrap_err().to_string();
        assert!(error.contains("..."), "{error}");
        assert!(error.len() < 200, "{error}");
    }

    #[test]
    fn the_transformer_never_panics_on_anything_the_matcher_accepts() {
        // The matcher accepts a good deal that means nothing, because the grammar does. Every one
        // of these parses and none of them is a statement this milestone covers, and the contract
        // is that the answer is an error either way.
        for query in [
            "SELECT",
            "FROM t SELECT",
            "SELECT * FROM t WHERE",
            "SELECT ()",
            "SELECT a FROM t GROUP BY ()",
        ] {
            let answer = parse_ast(query);
            if let Err(error) = answer {
                let message = error.to_string();
                assert!(
                    message.starts_with("Not implemented Error")
                        || message.starts_with("Parser Error"),
                    "{query} failed with {message}"
                );
            }
        }
    }

    #[test]
    fn interning_means_a_name_written_twice_is_stored_once() {
        let ast = parse_ast("SELECT a, a, a FROM t WHERE a = a").unwrap();
        assert_eq!(ast.strings.iter().filter(|text| *text == "a").count(), 1);
    }
}