quarb-sql 0.73.0

SQL importer for the Quarb query engine
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
//! SQL importer for Quarb.
//!
//! Translates a SQL `SELECT` statement into an equivalent Quarb
//! query, following the mapping in the SQL cookbook — in reverse.
//! The translatable subset covers the single-statement query core;
//! everything else is refused with an
//! [`SqlError::Unsupported`] naming the construct, never silently
//! approximated.
//!
//! - `SELECT cols FROM t` → `/t/* | %(::col; …)`; `SELECT *` →
//!   the row nodes; `SELECT DISTINCT col` → `/t/*::col @| unique`,
//!   and over several columns the records, `| %(…) @| unique`.
//! - `WHERE` → a predicate that holds exactly where SQL finds the
//!   condition TRUE. SQL's logic has a third value for a comparison
//!   with a NULL, so `<>` is guarded (`::x != null && ::x != "GB"`)
//!   and a `NOT` is carried inward to the comparisons; `IS [NOT]
//!   NULL` → `= null` / `!= null`; `[NOT] IN (…)` → a chain of
//!   equalities; `[NOT] LIKE` → a regex, `%` a run of characters and
//!   `_` one.
//! - Aggregates (`COUNT(*)`, `SUM`/`AVG`/`MIN`/`MAX`) → `@|`
//!   reductions over the piped column, `COUNT(col)` →
//!   `count(--values)`; `GROUP BY k` → `| .k(::k) @|{.k}` with the
//!   aggregate folding by the pushed key and `HAVING` a filter
//!   after it.
//! - `JOIN t2 ON …` → the every-witness correlation, one thread for
//!   every pair as SQL has one row: `/t1/* <=>* /t2/*[::b =
//!   $$0::a]`, the FROM table driving, the ON in the joined
//!   expression's bracket, the joined side's columns read through
//!   the witness (`$$1::col`). `LEFT JOIN` → `<=>*?`, which keeps a
//!   driver with no pair, its WHERE a filter after the join; `RIGHT
//!   JOIN` is `LEFT` with the tables' places exchanged; `CROSS JOIN`
//!   is every pair. The ON is any condition.
//! - `UNION ALL` of single columns of different tables → a branch
//!   union, `/t/*::a || /u/*::b`; `UNION` the same and `@| unique`.
//! - `ORDER BY` → `@| sort_by(…)` (`DESC` via `@| reverse`);
//!   `LIMIT n` → `@| [..n]`.
//!
//! Refused: subqueries, window functions, `CASE`, a `FULL` join,
//! multi-join chains (one `JOIN` translates), `SELECT *` over a
//! join, `UNION ALL` over one table twice (a branch union keeps one
//! thread for a node two branches reach), a comparison with a NULL
//! literal, `HAVING` without `GROUP BY`, aggregate arithmetic
//! (`SUM(a) + 1`), and any non-`SELECT` statement.
//!
//! Each translation is held against SQLite's own answer over one
//! database (the differential rig of `tests/sql.rs`). What differs
//! between SQL engines themselves is reported as a
//! [`Translation::notes`] line rather than refused:
//!
//! - Quarb streams records (JSONL), not a result table.
//! - `LIKE` translates to a case-insensitive regex (matching
//!   MySQL's and SQLite's default folding; PostgreSQL's `LIKE`
//!   is case-sensitive).
//! - `ORDER BY` puts a NULL key last, first under `DESC`, as
//!   PostgreSQL does; SQLite and MySQL put it first.

mod export;
pub use export::{
    ColumnFact, ColumnTypes, Dialect, FloatSum, Partial, Pushdown, export, partial_pushdown,
    partial_pushdown_explained, partial_pushdown_keyed, pushdown, pushdown_explained,
};

use quarb::diagnostic as d;
use std::fmt::Write as _;

/// An error translating a SQL statement.
#[derive(Debug, thiserror::Error)]
pub enum SqlError {
    #[error("SQL syntax error: {0}")]
    Syntax(String),
    #[error("unsupported SQL construct: {0}")]
    Unsupported(String),
    /// A construct of SQL with no Quarb translation: the diagnostic
    /// code of the reason (`JOIN_NOT_TRANSLATED`; ruling 2026-11-17,
    /// one word per reason) and the reason.
    #[error("unsupported SQL construct: {1}")]
    NotTranslated(&'static str, String),
    /// SQL can spell the construct, but with provably different
    /// semantics (NULL propagation, collation, coercion) — the
    /// refusal names the divergence, not a missing feature.
    #[error("semantic mismatch: {0}")]
    Semantics(String),
    /// A part of a Quarb query the exporter leaves to the engine:
    /// the word a planner's refusal is told under, whether SQL
    /// could spell the part with other semantics (`mismatch`) or
    /// has no form for it, and the reason.
    #[error("{}: {reason}", class(*.mismatch))]
    NotPushed {
        word: Refusal,
        mismatch: bool,
        reason: String,
    },
}

/// The class an exporter's refusal prints before its reason.
fn class(mismatch: bool) -> &'static str {
    if mismatch {
        "semantic mismatch"
    } else {
        "unsupported SQL construct"
    }
}

impl SqlError {
    /// The word and the reason of an exporter's refusal; `None` for
    /// a syntax error and for an importer's error.
    pub fn not_pushed(&self) -> Option<(Refusal, &str)> {
        match self {
            SqlError::NotPushed { word, reason, .. } => Some((*word, reason)),
            _ => None,
        }
    }

    /// The code and the reason of an importer's refusal, and
    /// whether `--lenient` translates what was refused: never, the
    /// SQL bridge has no lenient form.
    pub fn not_translated(&self) -> Option<(&'static str, &str, bool)> {
        match self {
            SqlError::NotTranslated(code, reason) => Some((code, reason, false)),
            _ => None,
        }
    }
}

/// Why the exporter leaves a part of a Quarb query to the engine:
/// the first word of the diagnostic code a planner's refusal is told
/// under (`Sort` is `SORT_NOT_PUSHED`; ruling 2026-11-17, one word
/// per reason where `NOT_PUSHED` was the one kind). The word names
/// what the user wrote; the inventory (`langspec/diagnostics.tsv`)
/// files each under its remedy.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Refusal {
    /// A sort (`sort_by`, `top`, `reverse`): a database orders text
    /// by its own collation.
    Sort,
    /// A fold, a command over its groups, a HAVING.
    Fold,
    /// `unique`: DISTINCT returns rows in no stated order.
    Unique,
    /// A selection by position.
    Take,
    /// A regular expression, a substring test, a LIKE.
    Pattern,
    /// A condition that follows a reference or navigates.
    Resolution,
    /// Navigation beyond a table's rows.
    Walk,
    /// A read of the query's own state: a register, the topic, a
    /// capture, a mark, a pushed value.
    State,
    /// A correlation in a form with no JOIN.
    Join,
    /// A reducer over row nodes, or one SQL has no aggregate for.
    Reducer,
    /// A comparison that would follow the column's declared type
    /// where the engine follows each value.
    Type,
    /// A union of branches.
    Union,
    /// An operator, a command or a projection with no SQL form.
    Operator,
    /// A name or a literal with no spelling every dialect reads, or
    /// an empty query.
    Name,
    /// A leading condition a partial plan could not send ahead:
    /// what follows would see the difference.
    Condition,
    /// A partial plan with no leading condition to send ahead:
    /// nothing was refused, there was nothing to push, and no
    /// diagnostic tells of it.
    Nothing,
    /// A whole plan for a query that ends on row nodes: their
    /// locators are the scan's to write. Nothing the reader wrote
    /// is refused (the conditions go ahead through the partial
    /// plan), and no diagnostic tells of it.
    Nodes,
}

impl Refusal {
    /// The diagnostic code; `None` for [`Refusal::Nothing`] and
    /// [`Refusal::Nodes`].
    pub fn code(self) -> Option<&'static str> {
        use quarb::diagnostic as d;
        Some(match self {
            Refusal::Sort => d::SORT_NOT_PUSHED,
            Refusal::Fold => d::FOLD_NOT_PUSHED,
            Refusal::Unique => d::UNIQUE_NOT_PUSHED,
            Refusal::Take => d::TAKE_NOT_PUSHED,
            Refusal::Pattern => d::PATTERN_NOT_PUSHED,
            Refusal::Resolution => d::RESOLUTION_NOT_PUSHED,
            Refusal::Walk => d::WALK_NOT_PUSHED,
            Refusal::State => d::STATE_NOT_PUSHED,
            Refusal::Join => d::JOIN_NOT_PUSHED,
            Refusal::Reducer => d::REDUCER_NOT_PUSHED,
            Refusal::Type => d::TYPE_NOT_PUSHED,
            Refusal::Union => d::UNION_NOT_PUSHED,
            Refusal::Operator => d::OPERATOR_NOT_PUSHED,
            Refusal::Name => d::NAME_NOT_PUSHED,
            Refusal::Condition => d::CONDITION_NOT_PUSHED,
            Refusal::Nothing | Refusal::Nodes => return None,
        })
    }
}

/// A successful translation: the Quarb query, plus notes on known
/// semantic divergences that apply to it.
#[derive(Debug)]
pub struct Translation {
    pub query: String,
    pub notes: Vec<String>,
}

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

#[derive(Debug, Clone, PartialEq)]
enum Tok {
    /// A bare identifier or keyword (uppercased for keywords).
    Word(String),
    /// A 'string literal'.
    Str(String),
    Num(String),
    Sym(char),
    /// `<=`, `>=`, `<>`, `!=`
    Op2(String),
}

fn lex(input: &str) -> Result<Vec<Tok>, SqlError> {
    let chars: Vec<char> = input.chars().collect();
    let mut out = Vec::new();
    let mut i = 0;
    while i < chars.len() {
        let c = chars[i];
        if c.is_whitespace() {
            i += 1;
            continue;
        }
        if c == '\'' {
            let mut s = String::new();
            i += 1;
            loop {
                match chars.get(i) {
                    Some('\'') if chars.get(i + 1) == Some(&'\'') => {
                        s.push('\'');
                        i += 2;
                    }
                    Some('\'') => {
                        i += 1;
                        break;
                    }
                    Some(&ch) => {
                        s.push(ch);
                        i += 1;
                    }
                    None => return Err(SqlError::Syntax("unterminated string".into())),
                }
            }
            out.push(Tok::Str(s));
            continue;
        }
        if c.is_ascii_digit() {
            let mut s = String::new();
            let mut dotted = false;
            while let Some(&ch) = chars.get(i) {
                if ch.is_ascii_digit() || (ch == '.' && !dotted) {
                    dotted |= ch == '.';
                    s.push(ch);
                    i += 1;
                } else {
                    break;
                }
            }
            out.push(Tok::Num(s));
            continue;
        }
        if c.is_alphabetic() || c == '_' || c == '"' || c == '`' {
            // Quoted identifiers keep their case; bare words are
            // identifiers-or-keywords (keywords match uppercased).
            if c == '"' || c == '`' {
                let quote = c;
                let mut s = String::new();
                let mut closed = false;
                i += 1;
                while let Some(&ch) = chars.get(i) {
                    i += 1;
                    if ch == quote {
                        closed = true;
                        break;
                    }
                    s.push(ch);
                }
                if !closed {
                    return Err(SqlError::Syntax("unterminated quoted identifier".into()));
                }
                out.push(Tok::Word(s));
                continue;
            }
            let mut s = String::new();
            while let Some(&ch) = chars.get(i) {
                if ch.is_alphanumeric() || ch == '_' {
                    s.push(ch);
                    i += 1;
                } else {
                    break;
                }
            }
            out.push(Tok::Word(s));
            continue;
        }
        if (c == '<' && matches!(chars.get(i + 1), Some('=') | Some('>')))
            || (c == '>' && chars.get(i + 1) == Some(&'='))
            || (c == '!' && chars.get(i + 1) == Some(&'='))
        {
            out.push(Tok::Op2(format!("{c}{}", chars[i + 1])));
            i += 2;
            continue;
        }
        if "(),.*=<>;-".contains(c) {
            out.push(Tok::Sym(c));
            i += 1;
            continue;
        }
        return Err(SqlError::Syntax(format!("unexpected character '{c}'")));
    }
    Ok(out)
}

// ---------------------------------------------------------------
// Parser / translator
// ---------------------------------------------------------------

/// A column reference: optionally table-qualified.
#[derive(Debug, Clone)]
struct ColRef {
    table: Option<String>,
    column: String,
}

#[derive(Debug, Clone)]
enum Scalar {
    Col(ColRef),
    Str(String),
    Num(String),
    Null,
}

#[derive(Debug)]
enum Cond {
    Cmp(ColRef, String, Scalar),
    /// An aggregate call compared to a scalar — HAVING only.
    AggCmp(Agg, Option<ColRef>, String, Scalar),
    Like(ColRef, String),
    IsNull(ColRef, bool),
    In(ColRef, Vec<Scalar>),
    And(Box<Cond>, Box<Cond>),
    Or(Box<Cond>, Box<Cond>),
    Not(Box<Cond>),
}

#[derive(Debug, Clone, PartialEq)]
enum Agg {
    Count,
    CountCol,
    Sum,
    Avg,
    Min,
    Max,
}

/// The aggregate a keyword names, if any.
fn agg_kw(w: &str) -> Option<Agg> {
    match w.to_ascii_uppercase().as_str() {
        "COUNT" => Some(Agg::Count),
        "SUM" => Some(Agg::Sum),
        "AVG" => Some(Agg::Avg),
        "MIN" => Some(Agg::Min),
        "MAX" => Some(Agg::Max),
        _ => None,
    }
}

#[derive(Debug)]
enum SelectItem {
    Star,
    Col(ColRef, Option<String>),
    Agg(Agg, Option<ColRef>, Option<String>),
}

struct Parser {
    toks: Vec<Tok>,
    pos: usize,
}

impl Parser {
    fn peek(&self) -> Option<&Tok> {
        self.toks.get(self.pos)
    }

    fn kw(&mut self, word: &str) -> bool {
        if matches!(self.peek(), Some(Tok::Word(w)) if w.eq_ignore_ascii_case(word)) {
            self.pos += 1;
            true
        } else {
            false
        }
    }

    fn expect_kw(&mut self, word: &str) -> Result<(), SqlError> {
        if self.kw(word) {
            Ok(())
        } else {
            Err(SqlError::Syntax(format!("expected {word}")))
        }
    }

    fn sym(&mut self, c: char) -> bool {
        if self.peek() == Some(&Tok::Sym(c)) {
            self.pos += 1;
            true
        } else {
            false
        }
    }

    fn ident(&mut self) -> Result<String, SqlError> {
        match self.peek() {
            Some(Tok::Word(w)) => {
                let w = w.clone();
                self.pos += 1;
                Ok(w)
            }
            other => Err(SqlError::Syntax(format!(
                "expected an identifier, found {other:?}"
            ))),
        }
    }

    fn col_ref(&mut self) -> Result<ColRef, SqlError> {
        let first = self.ident()?;
        if self.sym('.') {
            let column = self.ident()?;
            Ok(ColRef {
                table: Some(first),
                column,
            })
        } else {
            Ok(ColRef {
                table: None,
                column: first,
            })
        }
    }

    fn scalar(&mut self) -> Result<Scalar, SqlError> {
        match self.peek().cloned() {
            Some(Tok::Str(s)) => {
                self.pos += 1;
                Ok(Scalar::Str(s))
            }
            Some(Tok::Num(n)) => {
                self.pos += 1;
                Ok(Scalar::Num(n))
            }
            Some(Tok::Sym('-')) => {
                self.pos += 1;
                match self.peek().cloned() {
                    Some(Tok::Num(n)) => {
                        self.pos += 1;
                        Ok(Scalar::Num(format!("-{n}")))
                    }
                    _ => Err(SqlError::Syntax("expected a number after '-'".into())),
                }
            }
            Some(Tok::Word(w)) if w.eq_ignore_ascii_case("NULL") => {
                self.pos += 1;
                Ok(Scalar::Null)
            }
            Some(Tok::Word(_)) => Ok(Scalar::Col(self.col_ref()?)),
            other => Err(SqlError::Syntax(format!(
                "expected a value, found {other:?}"
            ))),
        }
    }

    // cond := and_cond (OR and_cond)*
    fn cond(&mut self) -> Result<Cond, SqlError> {
        let mut left = self.and_cond()?;
        while self.kw("OR") {
            let right = self.and_cond()?;
            left = Cond::Or(Box::new(left), Box::new(right));
        }
        Ok(left)
    }

    fn and_cond(&mut self) -> Result<Cond, SqlError> {
        let mut left = self.not_cond()?;
        while self.kw("AND") {
            let right = self.not_cond()?;
            left = Cond::And(Box::new(left), Box::new(right));
        }
        Ok(left)
    }

    fn not_cond(&mut self) -> Result<Cond, SqlError> {
        if self.kw("NOT") {
            return Ok(Cond::Not(Box::new(self.not_cond()?)));
        }
        if self.sym('(') {
            let inner = self.cond()?;
            if !self.sym(')') {
                return Err(SqlError::Syntax("expected ')'".into()));
            }
            return Ok(inner);
        }
        self.comparison()
    }

    /// An aggregate call's argument list: the parser is past the
    /// name, at `(`. Returns the aggregate (COUNT refined to
    /// CountCol) and its column, `None` for `(*)`.
    fn agg_call(&mut self, mut a: Agg) -> Result<(Agg, Option<ColRef>), SqlError> {
        self.pos += 1; // '('
        let col = if self.sym('*') {
            None
        } else {
            let c = self.col_ref()?;
            if a == Agg::Count {
                a = Agg::CountCol;
            }
            Some(c)
        };
        if !self.sym(')') {
            return Err(SqlError::Syntax("expected ')' after aggregate".into()));
        }
        Ok((a, col))
    }

    fn cmp_op(&mut self) -> Result<String, SqlError> {
        Ok(match self.peek().cloned() {
            Some(Tok::Sym('=')) => {
                self.pos += 1;
                "=".to_string()
            }
            Some(Tok::Sym('<')) => {
                self.pos += 1;
                "<".to_string()
            }
            Some(Tok::Sym('>')) => {
                self.pos += 1;
                ">".to_string()
            }
            Some(Tok::Op2(o)) => {
                self.pos += 1;
                match o.as_str() {
                    "<>" | "!=" => "!=".to_string(),
                    other => other.to_string(),
                }
            }
            other => {
                return Err(SqlError::Syntax(format!(
                    "expected an operator, found {other:?}"
                )));
            }
        })
    }

    fn comparison(&mut self) -> Result<Cond, SqlError> {
        // An aggregate call (`SUM(x) > 5`): meaningful in HAVING,
        // refused with its own message in WHERE.
        if let Some(Tok::Word(w)) = self.peek().cloned()
            && let Some(a) = agg_kw(&w)
            && matches!(self.toks.get(self.pos + 1), Some(Tok::Sym('(')))
        {
            self.pos += 1; // the name
            let (a, col) = self.agg_call(a)?;
            let op = self.cmp_op()?;
            let rhs = self.scalar()?;
            return Ok(Cond::AggCmp(a, col, op, rhs));
        }
        let col = self.col_ref()?;
        if self.kw("IS") {
            let not = self.kw("NOT");
            self.expect_kw("NULL")?;
            return Ok(Cond::IsNull(col, !not));
        }
        // `col NOT LIKE …`, `col NOT IN (…)`: the form, negated
        let word_is = |t: Option<&Tok>, words: &[&str]| matches!(t, Some(Tok::Word(w)) if words.iter().any(|x| w.eq_ignore_ascii_case(x)));
        let negated =
            word_is(self.peek(), &["NOT"]) && word_is(self.toks.get(self.pos + 1), &["LIKE", "IN"]);
        if negated {
            self.pos += 1;
        }
        let said = |c: Cond| {
            if negated { Cond::Not(Box::new(c)) } else { c }
        };
        if self.kw("LIKE") {
            match self.peek().cloned() {
                Some(Tok::Str(p)) => {
                    self.pos += 1;
                    return Ok(said(Cond::Like(col, p)));
                }
                _ => return Err(SqlError::Syntax("LIKE takes a string pattern".into())),
            }
        }
        if self.kw("IN") {
            if !self.sym('(') {
                return Err(SqlError::Syntax("IN takes a parenthesized list".into()));
            }
            let mut items = Vec::new();
            loop {
                items.push(self.scalar()?);
                if !self.sym(',') {
                    break;
                }
            }
            if !self.sym(')') {
                return Err(SqlError::Syntax("expected ')' after IN list".into()));
            }
            return Ok(said(Cond::In(col, items)));
        }
        let op = self.cmp_op()?;
        let rhs = self.scalar()?;
        Ok(Cond::Cmp(col, op, rhs))
    }
}

// ---------------------------------------------------------------
// Emission
// ---------------------------------------------------------------

/// How a JOIN treats a row with no pair.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum JoinKind {
    /// Every pair that satisfies the ON.
    Inner,
    /// The same, and the driver's rows that have no pair.
    Left,
    /// LEFT with the tables' places exchanged; resolved to `Left`
    /// before anything is emitted.
    Right,
    /// Every pair.
    Cross,
}

/// The tables in scope: (name, alias). The first is the FROM table
/// — the driver under driver-first correlation; the second the
/// JOIN table, whose witness the pipeline reads as `$$1`.
struct Scope {
    from: (String, Option<String>),
    join: Option<(String, Option<String>)>,
    /// Whether operands are being emitted inside the joined
    /// expression's ON bracket (driver = `$$0::col`, joined side
    /// bare) rather than in pipeline position (driver bare,
    /// joined side = `$$1::col`).
    on_clause: bool,
}

impl Scope {
    /// Whether `col` belongs to the FROM (left) table: qualified by
    /// its name or alias, or unqualified with no join.
    fn is_left(&self, col: &ColRef) -> Result<bool, SqlError> {
        let Some(q) = &col.table else {
            if self.join.is_some() {
                return Err(SqlError::NotTranslated(
                    d::JOIN_NOT_TRANSLATED,
                    format!("unqualified column '{}' in a JOIN (qualify it)", col.column),
                ));
            }
            return Ok(true);
        };
        let matches = |(name, alias): &(String, Option<String>)| {
            q.eq_ignore_ascii_case(name)
                || alias.as_ref().is_some_and(|a| q.eq_ignore_ascii_case(a))
        };
        if matches(&self.from) {
            Ok(true)
        } else if self.join.as_ref().is_some_and(matches) {
            Ok(false)
        } else {
            Err(SqlError::Syntax(format!("unknown table qualifier '{q}'")))
        }
    }

    /// The operand for `col`. In pipeline position the driver's
    /// columns are bare and the joined table's ride its witness
    /// (`$$1::col`); inside the ON bracket the joined table's are
    /// bare and the driver is reached as `$$0::col`.
    fn operand(&self, col: &ColRef) -> Result<String, SqlError> {
        let key = quarb_key(&col.column)?;
        Ok(match (self.join.is_some(), self.on_clause) {
            (false, _) => format!("::{key}"),
            (true, false) if self.is_left(col)? => format!("::{key}"),
            (true, false) => format!("$$1::{key}"),
            (true, true) if self.is_left(col)? => format!("$$0::{key}"),
            (true, true) => format!("::{key}"),
        })
    }

    /// Whether an operand, inside the ON bracket, reads the driver
    /// (`$$0::col`): a comparison with it is a key comparison of the
    /// join, which the engine leaves unknown for a null on either
    /// side.
    fn keyed(&self, operand: &str) -> bool {
        self.on_clause && operand.starts_with("$$")
    }

    /// Whether `c` tests a column of the FROM table for NULL: the one
    /// condition a join's bracket cannot hold.
    fn tests_driver_null(&self, c: &Cond) -> bool {
        match c {
            Cond::IsNull(col, _) => self.is_left(col).unwrap_or(false),
            Cond::And(a, b) | Cond::Or(a, b) => {
                self.tests_driver_null(a) || self.tests_driver_null(b)
            }
            Cond::Not(a) => self.tests_driver_null(a),
            _ => false,
        }
    }

    /// This scope, switched to ON-bracket emission.
    fn on(&self) -> Scope {
        Scope {
            from: self.from.clone(),
            join: self.join.clone(),
            on_clause: true,
        }
    }
}

/// A SQL string as a Quarb string literal: double-quoted, with the
/// Quarb lexer's `\"`, `\\`, `\$`, and `` \` `` escapes applied so
/// the content survives verbatim (`$` would otherwise read as an
/// interpolation).
fn quarb_str(s: &str) -> String {
    let mut out = String::with_capacity(s.len() + 2);
    out.push('"');
    for c in s.chars() {
        if matches!(c, '"' | '\\' | '$' | '`') {
            out.push('\\');
        }
        out.push(c);
    }
    out.push('"');
    out
}

/// A SQL identifier as a Quarb name (a step matcher or projection
/// key): bare when it lexes as one, single-quoted otherwise (a
/// quoted SQL identifier can hold spaces or keywords). A name with
/// a quote in it does not translate.
fn quarb_key(name: &str) -> Result<String, SqlError> {
    let bare = !name.is_empty()
        && name
            .chars()
            .all(|c| c.is_alphanumeric() || matches!(c, '.' | '-' | '_' | '+'))
        && !name.starts_with('.')
        && !matches!(name, "and" | "or" | "not");
    if bare {
        return Ok(name.to_string());
    }
    if name.contains('\'') {
        return Err(SqlError::NotTranslated(
            d::NAME_NOT_TRANSLATED,
            format!("the identifier {name:?} (a quote inside a quoted identifier)"),
        ));
    }
    Ok(format!("'{name}'"))
}

/// A scalar as a Quarb operand; columns resolve through the scope.
fn scalar_text(s: &Scalar, scope: &Scope) -> Result<String, SqlError> {
    Ok(match s {
        Scalar::Col(c) => scope.operand(c)?,
        Scalar::Str(v) => quarb_str(v),
        Scalar::Num(n) => n.clone(),
        Scalar::Null => "null".to_string(),
    })
}

/// The operator that holds exactly where `op` fails, both sides
/// present.
fn complement(op: &str) -> &'static str {
    match op {
        "=" => "!=",
        "!=" => "=",
        "<" => ">=",
        "<=" => ">",
        ">" => "<=",
        _ => "<",
    }
}

/// A LIKE pattern as a regex literal. The simple forms keep their
/// short spelling (`%x%` is a search, `x%` and `%x` anchor one end);
/// any other pattern is anchored at both ends, `%` any run of
/// characters and `_` any one, a line break among them.
fn like_regex(pat: &str) -> String {
    let inner = pat.trim_matches('%');
    if !inner.contains('%') && !inner.contains('_') {
        let esc = regex_escape(inner);
        return match (pat.starts_with('%'), pat.ends_with('%')) {
            (true, true) => format!("(/(?i){esc}/)"),
            (false, true) => format!("(/(?i)^{esc}/)"),
            (true, false) => format!("(/(?i){esc}$/)"),
            (false, false) => format!("(/(?i)^{esc}$/)"),
        };
    }
    let mut body = String::new();
    for c in pat.chars() {
        match c {
            '%' => body.push_str(".*"),
            '_' => body.push('.'),
            c => body.push_str(&regex_escape(&c.to_string())),
        }
    }
    format!("(/(?is)^{body}$/)")
}

/// A condition as a Quarb predicate that holds exactly where SQL
/// finds it TRUE (`truth`), or exactly where SQL finds it FALSE.
///
/// SQL's logic has a third value, UNKNOWN, for a comparison with a
/// NULL, and a row passes WHERE only on TRUE. Quarb's is two-valued:
/// a NULL differs from 'GB' (`!=` holds), two NULLs are equal, and
/// the negation of a comparison that failed holds. So a negation is
/// carried inward to the comparisons (`NOT (a AND b)` is where `a`
/// is FALSE or `b` is), and a comparison that would hold for a NULL
/// is guarded: `x <> 'GB'` is `::x != null && ::x != "GB"`. The
/// ordering comparisons need no guard: none holds for a NULL.
///
/// Inside a join's bracket the engine reads a comparison with the
/// driver as SQL does, unknown for a null on either side (spec:
/// Correlation), so such a comparison takes no guard there; and a
/// test of the driver's column for NULL cannot stand there at all
/// ([`Scope::keyed`]).
fn emit_cond(
    c: &Cond,
    truth: bool,
    scope: &Scope,
    notes: &mut Vec<String>,
) -> Result<String, SqlError> {
    Ok(match c {
        Cond::Cmp(col, op, rhs) => {
            if matches!(rhs, Scalar::Null) {
                return Err(SqlError::NotTranslated(
                    d::NULL_NOT_TRANSLATED,
                    format!(
                        "a comparison with NULL ('{} {op} NULL' is never true in SQL: write IS \
                     NULL or IS NOT NULL)",
                        col.column
                    ),
                ));
            }
            let op = if truth { op.as_str() } else { complement(op) };
            let lhs = scope.operand(col)?;
            let r = scalar_text(rhs, scope)?;
            match (op, matches!(rhs, Scalar::Col(_))) {
                // a key comparison of a join condition is unknown
                // for a null on either side, as SQL's is
                _ if scope.keyed(&lhs) || scope.keyed(&r) => format!("{lhs} {op} {r}"),
                ("!=", false) => format!("{lhs} != null && {lhs} != {r}"),
                ("!=", true) => format!("{lhs} != null && {r} != null && {lhs} != {r}"),
                ("=", true) => format!("{lhs} != null && {lhs} = {r}"),
                _ => format!("{lhs} {op} {r}"),
            }
        }
        Cond::AggCmp(..) => {
            return Err(SqlError::NotTranslated(
                d::AGGREGATE_NOT_TRANSLATED,
                "an aggregate in WHERE (SQL puts it in HAVING)".into(),
            ));
        }
        Cond::Like(col, pat) => {
            let lhs = scope.operand(col)?;
            let note = "LIKE: translated to a case-insensitive regex (SQL's default \
                        ASCII case folding)";
            if !notes.iter().any(|n| n == note) {
                notes.push(note.to_string());
            }
            let regex = like_regex(pat);
            if truth {
                format!("{lhs} == {regex}")
            } else if scope.keyed(&lhs) {
                format!("{lhs} != {regex}")
            } else {
                format!("{lhs} != null && !({lhs} == {regex})")
            }
        }
        Cond::IsNull(col, is_null) => {
            // `= null` / `!= null`: Quarb's `value_eq` treats NULL
            // as an ordinary value, so these are exactly SQL's
            // `IS [NOT] NULL` — truthiness (`not ::c`) would also
            // drop 0 and ''.
            let lhs = scope.operand(col)?;
            if scope.keyed(&lhs) {
                return Err(SqlError::NotTranslated(
                    d::JOIN_NOT_TRANSLATED,
                    format!(
                        "IS NULL on '{}', the driving table's column, inside ON (a join condition \
                     leaves a null key unknown: state it in WHERE)",
                        col.column
                    ),
                ));
            }
            if *is_null == truth {
                format!("{lhs} = null")
            } else {
                format!("{lhs} != null")
            }
        }
        Cond::In(col, items) => {
            let lhs = scope.operand(col)?;
            let has_null = items.iter().any(|i| matches!(i, Scalar::Null));
            let listed: Vec<&Scalar> = items
                .iter()
                .filter(|i| !matches!(i, Scalar::Null))
                .collect();
            if truth {
                // a NULL in the list matches nothing
                if listed.is_empty() {
                    return Err(SqlError::NotTranslated(
                        d::NULL_NOT_TRANSLATED,
                        "IN over a list of NULL alone (never true in SQL)".into(),
                    ));
                }
                let parts: Vec<String> = listed
                    .iter()
                    .map(|s| {
                        let r = scalar_text(s, scope)?;
                        let guarded =
                            matches!(s, Scalar::Col(_)) && !scope.keyed(&lhs) && !scope.keyed(&r);
                        Ok(if guarded {
                            format!("{lhs} != null && {lhs} = {r}")
                        } else {
                            format!("{lhs} = {r}")
                        })
                    })
                    .collect::<Result<_, SqlError>>()?;
                format!("({})", parts.join(" || "))
            } else {
                if has_null {
                    return Err(SqlError::NotTranslated(
                        d::NULL_NOT_TRANSLATED,
                        "NOT IN over a list with NULL (never true in SQL)".into(),
                    ));
                }
                let mut parts = Vec::new();
                if !scope.keyed(&lhs) {
                    parts.push(format!("{lhs} != null"));
                }
                for s in listed {
                    let r = scalar_text(s, scope)?;
                    if matches!(s, Scalar::Col(_)) && !scope.keyed(&lhs) && !scope.keyed(&r) {
                        parts.push(format!("{r} != null"));
                    }
                    parts.push(format!("{lhs} != {r}"));
                }
                parts.join(" && ")
            }
        }
        // AND is TRUE where both are, FALSE where either is; OR the
        // other way round. `&&` binds tighter than `||`, and a
        // disjunction carries its own parentheses.
        Cond::And(a, b) | Cond::Or(a, b) => {
            let (a, b) = (
                emit_cond(a, truth, scope, notes)?,
                emit_cond(b, truth, scope, notes)?,
            );
            if matches!(c, Cond::And(..)) == truth {
                format!("{a} && {b}")
            } else {
                format!("({a} || {b})")
            }
        }
        Cond::Not(a) => emit_cond(a, !truth, scope, notes)?,
    })
}

fn regex_escape(s: &str) -> String {
    s.chars()
        .flat_map(|c| {
            if "\\.+*?()|[]{}^$/".contains(c) {
                vec!['\\', c]
            } else {
                vec![c]
            }
        })
        .collect()
}

/// The name an aggregate's field takes where the statement gives
/// it no alias: the reducer's word.
fn agg_word(a: &Agg) -> &'static str {
    match a {
        Agg::Count | Agg::CountCol => "count",
        other => agg_fn(other),
    }
}

/// The reducer an aggregate is, as the query writes it.
fn agg_fn(a: &Agg) -> &'static str {
    match a {
        Agg::Count => "count",
        Agg::CountCol => "count(--values)",
        Agg::Sum => "sum",
        Agg::Avg => "mean",
        Agg::Min => "min",
        Agg::Max => "max",
    }
}

/// Translate one SQL `SELECT` statement to a Quarb query.
pub fn translate(sql: &str) -> Result<Translation, SqlError> {
    let toks = lex(sql.trim().trim_end_matches(';'))?;
    // A UNION outside every parenthesis joins whole SELECTs.
    let mut depth = 0usize;
    let mut cuts = Vec::new();
    for (i, t) in toks.iter().enumerate() {
        match t {
            Tok::Sym('(') => depth += 1,
            Tok::Sym(')') => depth = depth.saturating_sub(1),
            Tok::Word(w) if depth == 0 && w.eq_ignore_ascii_case("UNION") => cuts.push(i),
            _ => {}
        }
    }
    if !cuts.is_empty() {
        return translate_union(&toks, &cuts);
    }
    translate_select(toks)
}

/// `SELECT col FROM t [WHERE …] UNION [ALL] SELECT col FROM u …`,
/// with an ORDER BY and a LIMIT of the whole after the last: a
/// branch union of the columns, `/t/*[…]::col || /u/*[…]::col`.
///
/// UNION removes duplicate values, and `@| unique` after the union
/// does. UNION ALL keeps every row, and the branch union keeps one
/// thread for a node two branches both reach: over two different
/// tables no node is shared and the translation is exact; over one
/// table twice a row both SELECTs take would answer once where SQL
/// answers twice, so that form is refused.
fn translate_union(toks: &[Tok], cuts: &[usize]) -> Result<Translation, SqlError> {
    let mut notes = Vec::new();
    let mut all = None;
    let mut parts: Vec<&[Tok]> = Vec::new();
    let mut from = 0;
    for &cut in cuts {
        parts.push(&toks[from..cut]);
        let is_all =
            matches!(toks.get(cut + 1), Some(Tok::Word(w)) if w.eq_ignore_ascii_case("ALL"));
        if all.is_some_and(|a| a != is_all) {
            return Err(SqlError::NotTranslated(
                d::UNION_NOT_TRANSLATED,
                "UNION and UNION ALL in one statement".into(),
            ));
        }
        all = Some(is_all);
        from = cut + 1 + usize::from(is_all);
    }
    parts.push(&toks[from..]);
    let all = all.unwrap_or(false);

    let mut branches = Vec::new();
    let mut tables: Vec<String> = Vec::new();
    let mut first_name = String::new();
    let mut tail = String::new();
    let last = parts.len() - 1;
    for (i, part) in parts.iter().enumerate() {
        let mut p = Parser {
            toks: part.to_vec(),
            pos: 0,
        };
        let shape = || {
            SqlError::NotTranslated(
                d::UNION_NOT_TRANSLATED,
                "a UNION of anything but single columns ('SELECT col FROM t [WHERE …]' on \
                 each side)"
                    .into(),
            )
        };
        p.expect_kw("SELECT").map_err(|_| shape())?;
        if p.kw("DISTINCT") || p.sym('*') {
            return Err(shape());
        }
        if matches!(p.peek(), Some(Tok::Word(w)) if agg_kw(w).is_some())
            && matches!(p.toks.get(p.pos + 1), Some(Tok::Sym('(')))
        {
            return Err(shape());
        }
        let col = p.col_ref()?;
        let alias = p.kw("AS").then(|| p.ident()).transpose()?;
        if p.sym(',') {
            return Err(shape());
        }
        p.expect_kw("FROM")?;
        let table = p.ident()?;
        let table_alias = if p.kw("AS") {
            Some(p.ident()?)
        } else {
            match p.peek() {
                Some(Tok::Word(w))
                    if !["WHERE", "ORDER", "LIMIT"].contains(&w.to_ascii_uppercase().as_str()) =>
                {
                    Some(p.ident()?)
                }
                _ => None,
            }
        };
        let scope = Scope {
            from: (table.clone(), table_alias),
            join: None,
            on_clause: false,
        };
        let cond = p.kw("WHERE").then(|| p.cond()).transpose()?;
        if i == 0 {
            first_name = alias.unwrap_or_else(|| col.column.clone());
        }
        if i == last {
            // the whole union's order and limit
            if p.kw("ORDER") {
                p.expect_kw("BY")?;
                let named = match p.peek().cloned() {
                    Some(Tok::Num(n)) => {
                        p.pos += 1;
                        n == "1"
                    }
                    _ => p.col_ref()?.column.eq_ignore_ascii_case(&first_name),
                };
                if !named {
                    return Err(SqlError::NotTranslated(
                        d::UNION_NOT_TRANSLATED,
                        format!(
                            "ORDER BY of a UNION by anything but its column ('{first_name}', or 1)"
                        ),
                    ));
                }
                let desc = p.kw("DESC");
                if !desc {
                    p.kw("ASC");
                }
                tail.push_str(" @| sort");
                if desc {
                    tail.push_str(" @| reverse");
                }
            }
            if p.kw("LIMIT") {
                match p.peek().cloned() {
                    Some(Tok::Num(n)) => {
                        p.pos += 1;
                        write!(tail, " @| [..{n}]").unwrap();
                    }
                    _ => return Err(SqlError::Syntax("LIMIT takes a number".into())),
                }
            }
        }
        if p.peek().is_some() {
            return Err(shape());
        }
        if all && tables.iter().any(|t| t.eq_ignore_ascii_case(&table)) {
            return Err(SqlError::NotTranslated(
                d::UNION_NOT_TRANSLATED,
                format!(
                    "UNION ALL over the table '{table}' twice (a row both SELECTs take would \
                 answer once: a branch union keeps one thread for a node)"
                ),
            ));
        }
        tables.push(table.clone());
        let mut branch = format!("/{}/*", quarb_key(&table)?);
        if let Some(c) = &cond {
            write!(branch, "[{}]", emit_cond(c, true, &scope, &mut notes)?).unwrap();
        }
        write!(branch, "{}", scope.operand(&col)?).unwrap();
        branches.push(branch);
    }
    let mut q = branches.join(" || ");
    if !all {
        q.push_str(" @| unique");
    }
    q.push_str(&tail);
    Ok(Translation { query: q, notes })
}

/// One `SELECT`, its tokens lexed.
fn translate_select(toks: Vec<Tok>) -> Result<Translation, SqlError> {
    let mut p = Parser { toks, pos: 0 };
    let mut notes = Vec::new();

    p.expect_kw("SELECT").map_err(|_| {
        SqlError::NotTranslated(
            d::STATEMENT_NOT_TRANSLATED,
            "only SELECT statements translate".into(),
        )
    })?;
    let distinct = p.kw("DISTINCT");

    // The select list.
    let mut items = Vec::new();
    loop {
        if p.sym('*') {
            items.push(SelectItem::Star);
        } else if let Some(Tok::Word(w)) = p.peek().cloned() {
            if let Some(a) = agg_kw(&w)
                && matches!(p.toks.get(p.pos + 1), Some(Tok::Sym('(')))
            {
                p.pos += 1; // the name
                let (a, col) = p.agg_call(a)?;
                let alias = p.kw("AS").then(|| p.ident()).transpose()?;
                items.push(SelectItem::Agg(a, col, alias));
            } else {
                let col = p.col_ref()?;
                let alias = p.kw("AS").then(|| p.ident()).transpose()?;
                items.push(SelectItem::Col(col, alias));
            }
        } else {
            return Err(SqlError::Syntax("expected a select item".into()));
        }
        if !p.sym(',') {
            break;
        }
    }

    p.expect_kw("FROM")?;
    let from_table = p.ident()?;
    // Keywords that end the FROM clause and so cannot be a bare
    // table alias.
    const CLAUSE_KEYWORDS: &[&str] = &[
        "JOIN", "INNER", "LEFT", "RIGHT", "FULL", "CROSS", "OUTER", "ON", "WHERE", "GROUP",
        "ORDER", "LIMIT", "HAVING", "UNION",
    ];
    let from_alias = if p.kw("AS") {
        Some(p.ident()?)
    } else {
        match p.peek() {
            Some(Tok::Word(w)) if !CLAUSE_KEYWORDS.contains(&w.to_ascii_uppercase().as_str()) => {
                Some(p.ident()?)
            }
            _ => None,
        }
    };

    // One optional JOIN. An inner join is every pair that satisfies
    // its ON (`<=>*`); LEFT keeps the FROM table's rows that have no
    // pair, the joined side null (`<=>*?`); RIGHT is LEFT with the
    // tables' places exchanged; CROSS is every pair. FULL has no
    // correlation that keeps the unmatched rows of both tables.
    if p.kw("FULL") {
        return Err(SqlError::NotTranslated(
            d::JOIN_NOT_TRANSLATED,
            "a FULL JOIN (no correlation keeps the unmatched rows of both tables)".into(),
        ));
    }
    let outer = if p.kw("LEFT") {
        Some(JoinKind::Left)
    } else if p.kw("RIGHT") {
        Some(JoinKind::Right)
    } else {
        None
    };
    if outer.is_some() {
        p.kw("OUTER");
    }
    let cross = outer.is_none() && p.kw("CROSS");
    let mut join = None;
    let mut join_on = None;
    let mut kind = JoinKind::Inner;
    if outer.is_some()
        || cross
        || p.kw("INNER")
        || matches!(p.peek(), Some(Tok::Word(w)) if w.eq_ignore_ascii_case("JOIN"))
    {
        p.expect_kw("JOIN")?;
        let t = p.ident()?;
        let alias = if p.kw("AS") {
            Some(p.ident()?)
        } else {
            match p.peek() {
                Some(Tok::Word(w))
                    if !CLAUSE_KEYWORDS.contains(&w.to_ascii_uppercase().as_str()) =>
                {
                    Some(p.ident()?)
                }
                _ => None,
            }
        };
        if cross {
            kind = JoinKind::Cross;
            if matches!(p.peek(), Some(Tok::Word(w)) if w.eq_ignore_ascii_case("ON")) {
                return Err(SqlError::NotTranslated(
                    d::JOIN_NOT_TRANSLATED,
                    "CROSS JOIN with an ON clause (a join with a condition is JOIN … ON)".into(),
                ));
            }
        } else {
            kind = outer.unwrap_or(JoinKind::Inner);
            p.expect_kw("ON")?;
            join_on = Some(p.cond()?);
        }
        join = Some((t, alias));
        if matches!(p.peek(), Some(Tok::Word(w))
            if ["JOIN", "INNER", "LEFT", "RIGHT", "FULL", "CROSS"]
                .contains(&w.to_ascii_uppercase().as_str()))
        {
            return Err(SqlError::NotTranslated(
                d::JOIN_NOT_TRANSLATED,
                "more than one JOIN (chain resolutions with '-->' instead)".into(),
            ));
        }
    }
    // RIGHT JOIN: the joined table drives, and the FROM table is the
    // side that may be missing.
    let (from_table, from_alias, join) = match (kind, join) {
        (JoinKind::Right, Some((t, alias))) => {
            kind = JoinKind::Left;
            (t, alias, Some((from_table, from_alias)))
        }
        (_, join) => (from_table, from_alias, join),
    };

    let scope = Scope {
        from: (from_table.clone(), from_alias),
        join: join.clone(),
        on_clause: false,
    };

    let where_cond = p.kw("WHERE").then(|| p.cond()).transpose()?;
    let group_by = p
        .kw("GROUP")
        .then(|| {
            p.expect_kw("BY")?;
            p.col_ref()
        })
        .transpose()?;
    let having = p.kw("HAVING").then(|| p.cond()).transpose()?;
    let order_by = p
        .kw("ORDER")
        .then(|| -> Result<(ColRef, bool), SqlError> {
            p.expect_kw("BY")?;
            let c = p.col_ref()?;
            let desc = p.kw("DESC");
            if !desc {
                p.kw("ASC");
            }
            Ok((c, desc))
        })
        .transpose()?;
    let limit = p
        .kw("LIMIT")
        .then(|| match p.peek().cloned() {
            Some(Tok::Num(n)) => {
                p.pos += 1;
                Ok(n)
            }
            _ => Err(SqlError::Syntax("LIMIT takes a number".into())),
        })
        .transpose()?;
    if let Some(t) = p.peek() {
        return Err(SqlError::NotTranslated(
            d::CONSTRUCT_NOT_TRANSLATED,
            format!("trailing SQL after the query ({t:?})"),
        ));
    }
    if order_by.is_some() {
        notes.push(
            "ORDER BY: a NULL key sorts last, first under DESC (as PostgreSQL orders it; \
             SQLite and MySQL put it first)"
                .to_string(),
        );
    }
    if group_by.is_none() && having.is_some() {
        return Err(SqlError::NotTranslated(
            d::HAVING_NOT_TRANSLATED,
            "HAVING without GROUP BY (a whole-table group)".into(),
        ));
    }

    // ---- emit ----
    let mut q = String::new();
    if let Some((jt, _)) = &join {
        // `SELECT *` over a join is the columns of both tables; the
        // row nodes would be the driver's alone.
        if items.iter().any(|i| matches!(i, SelectItem::Star)) {
            return Err(SqlError::NotTranslated(
                d::JOIN_NOT_TRANSLATED,
                "SELECT * over a JOIN (name the columns: a row node is one table's row)".into(),
            ));
        }
        // The driver is the correlation context; the joined table
        // the result context, its ON in the bracket, where the
        // driver is `$$0`. Every pair is a thread (`<=>*`), as every
        // pair is a row; `<=>*?` keeps a driver with no pair.
        let arrow = if kind == JoinKind::Left {
            "<=>*?"
        } else {
            "<=>*"
        };
        write!(
            q,
            "/{}/* {arrow} /{}/*",
            quarb_key(&from_table)?,
            quarb_key(jt)?
        )
        .unwrap();
        let mut inside: Vec<String> = Vec::new();
        if let Some(on) = &join_on {
            inside.push(emit_cond(on, true, &scope.on(), &mut notes)?);
        }
        // WHERE reads the joined rows. For an inner join that is the
        // ON's own filter, and it rides the bracket (where a source
        // takes the whole as one JOIN); after an outer join it must
        // see the rows the join kept with no pair, and a test of the
        // driver's column for NULL cannot stand in a bracket: there
        // it follows the join as a filter.
        let after = where_cond
            .as_ref()
            .filter(|w| kind == JoinKind::Left || scope.tests_driver_null(w));
        if after.is_none()
            && let Some(w) = &where_cond
        {
            inside.push(emit_cond(w, true, &scope.on(), &mut notes)?);
        }
        if !inside.is_empty() {
            write!(q, "[{}]", inside.join(" && ")).unwrap();
        }
        if let Some(w) = after {
            // (the thread filter in its canonical form, after the
            // aggregation pipe)
            write!(q, " @| [{}]", emit_cond(w, true, &scope, &mut notes)?).unwrap();
        }
    } else {
        write!(q, "/{}/*", quarb_key(&from_table)?).unwrap();
        if let Some(w) = &where_cond {
            write!(q, "[{}]", emit_cond(w, true, &scope, &mut notes)?).unwrap();
        }
    }

    // GROUP BY: the aggregate rides the plain pipe.
    if let Some(k) = &group_by {
        if distinct {
            return Err(SqlError::NotTranslated(
                d::DISTINCT_NOT_TRANSLATED,
                "SELECT DISTINCT with GROUP BY".into(),
            ));
        }
        let aggs: Vec<&SelectItem> = items
            .iter()
            .filter(|i| matches!(i, SelectItem::Agg(..)))
            .collect();
        if aggs.len() != 1 {
            return Err(SqlError::NotTranslated(
                d::GROUP_NOT_TRANSLATED,
                "GROUP BY translates with exactly one aggregate in the select list".into(),
            ));
        }
        let SelectItem::Agg(a, col, alias) = aggs[0] else {
            unreachable!()
        };
        // Any plain select item must be the group key — anything
        // else would silently vanish from the translation.
        let mut key_alias = None;
        for item in &items {
            match item {
                SelectItem::Col(c, ka) if c.column.eq_ignore_ascii_case(&k.column) => {
                    key_alias = ka.clone();
                }
                SelectItem::Col(c, _) => {
                    return Err(SqlError::NotTranslated(
                        d::GROUP_NOT_TRANSLATED,
                        format!(
                            "the non-aggregate column '{}' is not the GROUP BY key",
                            c.column
                        ),
                    ));
                }
                SelectItem::Star => {
                    return Err(SqlError::NotTranslated(
                        d::GROUP_NOT_TRANSLATED,
                        "SELECT * with GROUP BY".into(),
                    ));
                }
                SelectItem::Agg(..) => {}
            }
        }
        // The key is pushed under its name (the alias, else the
        // column) and the reduction folds by it: `| .k(::k) @|{.k}
        // sum`. SQL's NULL-key group is the fold's null partition.
        let key_name = key_alias.clone().unwrap_or_else(|| k.column.clone());
        if !plain_register(&key_name) {
            return Err(SqlError::NotTranslated(
                d::NAME_NOT_TRANSLATED,
                format!("the GROUP BY key {key_name:?} (not a plain register name)"),
            ));
        }
        write!(q, " | .{key_name}({})", scope.operand(k)?).unwrap();
        // The column an aggregate reads is piped to it; COUNT(col)
        // counts its values other than NULL, `count(--values)`.
        if let Some(c) = col {
            write!(q, " | {}", scope.operand(c)?).unwrap();
        }
        let name = alias.clone().unwrap_or_else(|| agg_word(a).to_string());
        if !plain_register(&name) {
            return Err(SqlError::NotTranslated(
                d::NAME_NOT_TRANSLATED,
                format!("the aggregate alias {name:?} (not a plain register name)"),
            ));
        }
        write!(q, " @|{{.{key_name}}} {} | .{name}", agg_fn(a)).unwrap();
        if let Some(h) = &having {
            // HAVING refers to the aggregate (an aggregate call,
            // its alias, or its function name) or the group key.
            let key_field = key_alias.as_deref().unwrap_or(&k.column);
            let cond = emit_having(h, a, col.as_ref(), &name, &k.column, key_field)?;
            write!(q, " @| [{cond}]").unwrap();
        }
        write!(q, " | %.").unwrap();
        // ORDER BY / LIMIT after the grouped form: the sort key is
        // a field of the group record, the key or the aggregate.
        if let Some((c, desc)) = &order_by {
            let field = if c.column.eq_ignore_ascii_case(&k.column) {
                key_name.clone()
            } else if c.column.eq_ignore_ascii_case(&name) {
                name.clone()
            } else {
                return Err(SqlError::NotTranslated(
                    d::SORT_NOT_TRANSLATED,
                    format!(
                        "ORDER BY '{}': neither the GROUP BY key nor the aggregate",
                        c.column
                    ),
                ));
            };
            write!(q, " @| sort_by(:{field})").unwrap();
            if *desc {
                q.push_str(" @| reverse");
            }
        }
        if let Some(n) = &limit {
            write!(q, " @| [..{n}]").unwrap();
        }
        return Ok(Translation { query: q, notes });
    } else if items.iter().any(|i| matches!(i, SelectItem::Agg(..))) {
        // Bare aggregates over the whole table.
        if items.len() != 1 {
            return Err(SqlError::NotTranslated(
                d::GROUP_NOT_TRANSLATED,
                "mixing aggregates and columns without GROUP BY".into(),
            ));
        }
        if distinct {
            return Err(SqlError::NotTranslated(
                d::DISTINCT_NOT_TRANSLATED,
                "SELECT DISTINCT with an aggregate".into(),
            ));
        }
        let SelectItem::Agg(a, col, _) = &items[0] else {
            unreachable!()
        };
        // The column an aggregate reads is piped to it; COUNT(col)
        // counts its values other than NULL, `count(--values)`.
        if let Some(c) = col {
            write!(q, " | {}", scope.operand(c)?).unwrap();
        }
        write!(q, " @| {}", agg_fn(a)).unwrap();
    } else {
        // Plain column selection: sort, then DISTINCT's dedup
        // (stable, so the sorted order survives), then LIMIT —
        // SQL applies DISTINCT before ORDER BY and LIMIT.
        if let Some((c, desc)) = &order_by {
            write!(q, " @| sort_by({})", scope.operand(c)?).unwrap();
            if *desc {
                q.push_str(" @| reverse");
            }
        }
        if distinct {
            match items.as_slice() {
                [SelectItem::Star] => {
                    return Err(SqlError::NotTranslated(
                        d::DISTINCT_NOT_TRANSLATED,
                        "SELECT DISTINCT *".into(),
                    ));
                }
                [SelectItem::Col(c, _)] => {
                    write!(q, " | {} @| unique", scope.operand(c)?).unwrap();
                }
                // several columns: the rows as records, of which
                // `unique` keeps the first of each (two NULLs are
                // one value there, as they are under DISTINCT)
                _ => {
                    let fields = record_fields(&items, &scope)?;
                    write!(q, " | %({}) @| unique", fields.join("; ")).unwrap();
                    notes.push("the result streams as records (JSONL), not a table".to_string());
                }
            }
            if let Some(n) = &limit {
                write!(q, " @| [..{n}]").unwrap();
            }
            return Ok(Translation { query: q, notes });
        }
        if let Some(n) = &limit {
            write!(q, " @| [..{n}]").unwrap();
        }
        if items.len() == 1 && matches!(items[0], SelectItem::Star) {
            // row nodes as-is
            notes.push("SELECT *: the result is the row nodes (their locators print)".to_string());
        } else {
            let fields = record_fields(&items, &scope)?;
            write!(q, " | %({})", fields.join("; ")).unwrap();
            notes.push("the result streams as records (JSONL), not a table".to_string());
        }
        return Ok(Translation { query: q, notes });
    }

    // ORDER BY / LIMIT after the bare aggregate form.
    if let Some((c, desc)) = &order_by {
        write!(q, " @| sort_by(::{})", quarb_key(&c.column)?).unwrap();
        if *desc {
            q.push_str(" @| reverse");
        }
    }
    if let Some(n) = &limit {
        write!(q, " @| [..{n}]").unwrap();
    }
    Ok(Translation { query: q, notes })
}

/// The fields of the record a select list of columns projects: a
/// column under its own name, an aliased one under its alias, and a
/// joined table's column under its SQL qualifier, so that the two
/// sides of a join keep distinct keys.
fn record_fields(items: &[SelectItem], scope: &Scope) -> Result<Vec<String>, SqlError> {
    let mut fields = Vec::new();
    for item in items {
        match item {
            SelectItem::Star => {
                return Err(SqlError::NotTranslated(
                    d::CONSTRUCT_NOT_TRANSLATED,
                    "mixing * with named columns".into(),
                ));
            }
            SelectItem::Col(c, alias) => {
                let op = scope.operand(c)?;
                match alias {
                    Some(a) => fields.push(format!("{} = {op}", quarb_str(a))),
                    None if op.starts_with("$$")
                        && op[2..].starts_with(|c: char| c.is_ascii_digit()) =>
                    {
                        let name = match &c.table {
                            Some(t) => format!("{t}.{}", c.column),
                            None => c.column.clone(),
                        };
                        fields.push(format!("{} = {op}", quarb_str(&name)));
                    }
                    None => fields.push(op),
                }
            }
            SelectItem::Agg(..) => {
                return Err(SqlError::NotTranslated(
                    d::GROUP_NOT_TRANSLATED,
                    "an aggregate beside plain columns in this position".into(),
                ));
            }
        }
    }
    Ok(fields)
}

/// A name usable as a bare Quarb register (`.name` / `$.name`).
fn plain_register(name: &str) -> bool {
    !name.is_empty()
        && name
            .chars()
            .next()
            .is_some_and(|c| c.is_alphabetic() || c == '_')
        && name.chars().all(|c| c.is_alphanumeric() || c == '_')
}

/// A HAVING condition: a single comparison whose left side names
/// the (single) aggregate — an aggregate call matching the select
/// list's, its alias, or its function name (it is the topic's
/// pushed register, `$_`) — or the group key (`$.key`).
fn emit_having(
    c: &Cond,
    agg: &Agg,
    agg_col: Option<&ColRef>,
    agg_name: &str,
    key: &str,
    key_field: &str,
) -> Result<String, SqlError> {
    let rhs_text = |rhs: &Scalar| -> Result<String, SqlError> {
        match rhs {
            Scalar::Col(_) => Err(SqlError::NotTranslated(
                d::HAVING_NOT_TRANSLATED,
                "HAVING compares against a literal".into(),
            )),
            Scalar::Str(v) => Ok(quarb_str(v)),
            Scalar::Num(n) => Ok(n.clone()),
            Scalar::Null => Ok("null".to_string()),
        }
    };
    match c {
        Cond::AggCmp(a, col, op, rhs) => {
            let same_col = match (col, agg_col) {
                (None, None) => true,
                (Some(x), Some(y)) => x.column.eq_ignore_ascii_case(&y.column),
                _ => false,
            };
            if a != agg || !same_col {
                return Err(SqlError::NotTranslated(
                    d::HAVING_NOT_TRANSLATED,
                    "HAVING refers to an aggregate not in the select list".into(),
                ));
            }
            Ok(format!("$_ {op} {}", rhs_text(rhs)?))
        }
        Cond::Cmp(col, op, rhs) => {
            let lhs = if col.column.eq_ignore_ascii_case(agg_name) {
                "$_".to_string()
            } else if col.column.eq_ignore_ascii_case(key) {
                if !plain_register(key_field) {
                    return Err(SqlError::NotTranslated(
                        d::NAME_NOT_TRANSLATED,
                        format!(
                            "the group key {key_field:?} in HAVING (not a plain register name)"
                        ),
                    ));
                }
                format!("$.{key_field}")
            } else {
                return Err(SqlError::NotTranslated(
                    d::HAVING_NOT_TRANSLATED,
                    format!(
                        "the HAVING column '{}' (name the aggregate or the group key)",
                        col.column
                    ),
                ));
            };
            Ok(format!("{lhs} {op} {}", rhs_text(rhs)?))
        }
        _ => Err(SqlError::NotTranslated(
            d::HAVING_NOT_TRANSLATED,
            "HAVING translates for a single comparison".into(),
        )),
    }
}