kglite 0.16.8

Pure-Rust embedded Cypher knowledge graph engine with in-memory, mmap, and disk storage, and agent-facing schema introspection
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
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
//! Rewriting simplifications — fold OR→IN, push LIMIT/DISTINCT into MATCH,
//! rewrite text_score.

use super::super::ast::*;
use crate::datatypes::values::Value;
use crate::graph::core::pattern_matching::{Pattern, PatternElement, PropertyMatcher};
use crate::graph::schema::DirGraph;
use std::collections::{HashMap, HashSet};

/// Fold OR chains of equalities on the same `variable.property` into a single
/// IN predicate: `n.name = 'A' OR n.name = 'B'` → `n.name IN ['A', 'B']`.
///
/// This enables predicate pushdown into MATCH patterns and index
/// acceleration, which is why a second `push_where_into_match` pass is
/// registered immediately after this one — the first runs before it and
/// cannot see the IN predicates this creates.
///
/// Why-bail: only equalities on one and the same property, with a literal or
/// parameter on the other side, fold. A chain across *different* properties
/// reaches the executor as a genuine one-level-per-term predicate tree, the
/// shape that costs the most stack (see `super::super::stack_probe`).
///
/// As a *planner* pass it cannot rescue a chain long enough to exhaust the
/// parser's nesting budget: the parse fails first, which is why the budget
/// error names the `IN [...]` rewrite.
pub(super) fn fold_or_to_in(query: &mut CypherQuery) {
    for clause in &mut query.clauses {
        match clause {
            Clause::Where(ref mut w) => {
                w.predicate = fold_or_to_in_pred(&w.predicate);
            }
            // An OPTIONAL MATCH carries its predicate in-clause; folding it
            // here keeps both spellings of one filter on the same plan.
            Clause::Match(ref mut m) | Clause::OptionalMatch(ref mut m) => {
                if let Some(ref mut w) = m.where_clause {
                    w.predicate = fold_or_to_in_pred(&w.predicate);
                }
            }
            _ => {}
        }
    }
}

fn collect_match_bound_vars(m: &MatchClause, out: &mut HashSet<String>) {
    for pattern in &m.patterns {
        for el in &pattern.elements {
            match el {
                PatternElement::Node(np) => {
                    if let Some(v) = &np.variable {
                        out.insert(v.clone());
                    }
                }
                PatternElement::Edge(ep) => {
                    if let Some(v) = &ep.variable {
                        out.insert(v.clone());
                    }
                }
            }
        }
    }
}

/// `count(v)` over a *mandatorily-bound* node/edge variable equals `count(*)`:
/// `v` is always present, so there is no need to materialize the full node/edge
/// value per row (each property cloned into a map) just to test non-null and
/// count it. Rewriting to `count(*)` lets the cheap count plan apply and lets
/// the MATCH avoid retaining the variable's binding for every path — the
/// dominant cost of deep-path counts like `… RETURN count(n5)`.
///
/// Guards (correctness): non-distinct `count`; single `Variable` argument; the
/// variable is bound by a non-OPTIONAL `MATCH` and by no `OPTIONAL MATCH`
/// (OPTIONAL can leave it NULL); and the query has no `WITH` (where the name
/// could be re-projected as a nullable scalar). The original output column name
/// is preserved via an explicit alias.
pub(super) fn rewrite_count_bound_var_to_star(query: &mut CypherQuery) {
    // Bail if any clause could reference the count expression by its original
    // form (ORDER BY) or re-project the variable (WITH).
    if query
        .clauses
        .iter()
        .any(|c| matches!(c, Clause::With(_) | Clause::OrderBy(_)))
    {
        return;
    }

    let mut mandatory: HashSet<String> = HashSet::new();
    let mut optional: HashSet<String> = HashSet::new();
    for c in &query.clauses {
        match c {
            Clause::Match(m) => collect_match_bound_vars(m, &mut mandatory),
            Clause::OptionalMatch(m) => collect_match_bound_vars(m, &mut optional),
            _ => {}
        }
    }
    if mandatory.is_empty() {
        return;
    }

    for c in &mut query.clauses {
        if let Clause::Return(r) = c {
            // Multi-item / grouped / HAVING shapes can reference the original
            // `count(v)` elsewhere; restricting to a single-item terminal
            // RETURN keeps the rewrite trivially safe and still covers the hot
            // shapes (deep-path / rel-type counts).
            if r.distinct || r.having.is_some() || r.items.len() != 1 {
                continue;
            }
            let item = &mut r.items[0];
            let rewrite_var = match &item.expression {
                Expression::FunctionCall {
                    name,
                    args,
                    distinct,
                } if !*distinct && name.eq_ignore_ascii_case("count") && args.len() == 1 => {
                    match &args[0] {
                        Expression::Variable(v)
                            if mandatory.contains(v) && !optional.contains(v) =>
                        {
                            Some(v.clone())
                        }
                        _ => None,
                    }
                }
                _ => None,
            };
            if let Some(v) = rewrite_var {
                if item.alias.is_none() {
                    item.alias = Some(format!("count({})", v));
                }
                if let Expression::FunctionCall { args, .. } = &mut item.expression {
                    args[0] = Expression::Star;
                }
            }
        }
    }
}

pub(super) fn fold_or_to_in_pred(pred: &Predicate) -> Predicate {
    match pred {
        Predicate::Or(_, _) => {
            let mut equalities: Vec<(String, String, Expression)> = Vec::new();
            let mut other_preds: Vec<Predicate> = Vec::new();
            collect_or_equalities(pred, &mut equalities, &mut other_preds);

            let mut groups: std::collections::HashMap<(String, String), Vec<Expression>> =
                std::collections::HashMap::new();
            for (var, prop, val_expr) in equalities {
                groups.entry((var, prop)).or_default().push(val_expr);
            }

            let mut result_preds: Vec<Predicate> = Vec::new();

            for ((var, prop), values) in groups {
                if values.len() >= 2 {
                    result_preds.push(Predicate::In {
                        expr: Expression::PropertyAccess {
                            variable: var,
                            property: prop,
                        },
                        list: values,
                    });
                } else {
                    result_preds.push(Predicate::Comparison {
                        left: Expression::PropertyAccess {
                            variable: var,
                            property: prop,
                        },
                        operator: ComparisonOp::Equals,
                        right: values.into_iter().next().unwrap(),
                    });
                }
            }

            for p in other_preds {
                result_preds.push(fold_or_to_in_pred(&p));
            }

            if result_preds.len() == 1 {
                result_preds.pop().unwrap()
            } else {
                let mut combined = result_preds.pop().unwrap();
                for p in result_preds.into_iter().rev() {
                    combined = Predicate::Or(Box::new(p), Box::new(combined));
                }
                combined
            }
        }
        Predicate::And(l, r) => Predicate::And(
            Box::new(fold_or_to_in_pred(l)),
            Box::new(fold_or_to_in_pred(r)),
        ),
        Predicate::Not(inner) => Predicate::Not(Box::new(fold_or_to_in_pred(inner))),
        other => other.clone(),
    }
}

pub(super) fn collect_or_equalities(
    pred: &Predicate,
    equalities: &mut Vec<(String, String, Expression)>,
    others: &mut Vec<Predicate>,
) {
    match pred {
        Predicate::Or(left, right) => {
            collect_or_equalities(left, equalities, others);
            collect_or_equalities(right, equalities, others);
        }
        Predicate::Comparison {
            left,
            operator: ComparisonOp::Equals,
            right,
        } => {
            if let Expression::PropertyAccess { variable, property } = left {
                if matches!(right, Expression::Literal(_) | Expression::Parameter(_)) {
                    equalities.push((variable.clone(), property.clone(), right.clone()));
                    return;
                }
            }
            if let Expression::PropertyAccess { variable, property } = right {
                if matches!(left, Expression::Literal(_) | Expression::Parameter(_)) {
                    equalities.push((variable.clone(), property.clone(), left.clone()));
                    return;
                }
            }
            others.push(pred.clone());
        }
        other => {
            others.push(other.clone());
        }
    }
}

/// Recognise `RETURN/WITH …group keys + aggregates… LIMIT N` (without
/// an intervening `ORDER BY`) and stamp `group_limit_hint = N` on the
/// projection clause. The aggregator then stops creating new groups
/// after `N` distinct keys are seen — rows for already-collected keys
/// continue to feed their aggregates so `collect()` / `sum()` / etc.
/// complete correctly.
///
/// **Why-bail.** ORDER BY between the projection and LIMIT changes the
/// answer (you need every group to find the top N), so the pass leaves
/// those queries to the materialised path. DISTINCT on the projection
/// is left alone for the same reason — the executor's DISTINCT-after-
/// projection step needs all groups to dedup against. `RETURN … HAVING`
/// and a `WITH`'s inline `WHERE`/`HAVING` bail for the same reason and
/// it is not cosmetic: both `execute_with` and the streaming pipeline
/// run the group-limited projection *first* and filter after, so a
/// capped group set can drop qualifying groups the LIMIT still had room
/// for (`WITH k, collect(x) AS xs WHERE size(xs) > 1 LIMIT 5` returned
/// 2 rows where 5 qualified).
///
/// **Triggering shape — Wikidata hub-anchor case.**
///
/// ```text
/// MATCH (x)-[:P31]->(hub {nid: 'Q11424'})
/// OPTIONAL MATCH (x)-[:P27]->(country)
/// RETURN x.title AS x, collect(DISTINCT country.title) AS countries
/// LIMIT 15
/// ```
///
/// Pre-fix the materialised path expanded all 340k :P31 inbound rows,
/// 340k OPTIONAL P27 expansions, 309k group buckets, then truncated to
/// 15 — 547ms warm, 64s cold.
pub(super) fn push_limit_into_aggregate(query: &mut CypherQuery, _graph: &DirGraph) {
    use super::super::ast::is_aggregate_expression;

    let mut i = 0;
    while i + 1 < query.clauses.len() {
        let limit_n = match &query.clauses[i + 1] {
            Clause::Limit(l) => match &l.count {
                Expression::Literal(Value::Int64(n)) if *n > 0 => *n as usize,
                _ => {
                    i += 1;
                    continue;
                }
            },
            _ => {
                i += 1;
                continue;
            }
        };

        // Require both a group key and an aggregate: pure-aggregate (no group
        // keys) and pure-projection (no aggregates) shapes don't benefit.
        let (has_group_key, has_agg) = match &query.clauses[i] {
            Clause::Return(r) => {
                if r.distinct || r.having.is_some() {
                    (false, false)
                } else {
                    let g = r
                        .items
                        .iter()
                        .any(|it| !is_aggregate_expression(&it.expression));
                    let a = r
                        .items
                        .iter()
                        .any(|it| is_aggregate_expression(&it.expression));
                    (g, a)
                }
            }
            Clause::With(w) => {
                // `where_clause` also carries `WITH … HAVING` — the parser
                // folds both spellings into it.
                if w.distinct || w.where_clause.is_some() {
                    (false, false)
                } else {
                    let g = w
                        .items
                        .iter()
                        .any(|it| !is_aggregate_expression(&it.expression));
                    let a = w
                        .items
                        .iter()
                        .any(|it| is_aggregate_expression(&it.expression));
                    (g, a)
                }
            }
            _ => {
                i += 1;
                continue;
            }
        };

        if !has_group_key || !has_agg {
            i += 1;
            continue;
        }

        // The LIMIT clause stays in the plan as a final safety net.
        match &mut query.clauses[i] {
            Clause::Return(r) => r.group_limit_hint = Some(limit_n),
            Clause::With(w) => w.group_limit_hint = Some(limit_n),
            _ => unreachable!(),
        }

        i += 1;
    }
}

/// Precondition: a single-MATCH query whose terminal `RETURN ... LIMIT n`
/// follows with no intervening cardinality-changing clause (a `WITH ... LIMIT`
/// projection is not matched). Pattern: stamps `limit_hint = n` onto the MATCH
/// so the pattern executor stops expanding once n rows exist, and removes the
/// now-redundant LIMIT clause. Why-bail: multi-MATCH queries and
/// correlated/filtered comma-pattern shapes interact incorrectly with the
/// per-row `max_matches` bound and can return fewer rows than LIMIT requests
/// (see the safety notes in the body); only the provably-safe shapes are
/// rewritten.
pub(super) fn push_limit_into_match(query: &mut CypherQuery, _graph: &DirGraph) {
    if query.clauses.len() < 3 {
        return;
    }
    let mut i = 0;
    while i + 2 < query.clauses.len() {
        // Look for MATCH → RETURN → LIMIT  or  MATCH → WHERE → RETURN → LIMIT
        let (has_where, return_offset, limit_offset) = if i + 3 < query.clauses.len()
            && matches!(&query.clauses[i], Clause::Match(_))
            && matches!(&query.clauses[i + 1], Clause::Where(_))
            && matches!(&query.clauses[i + 2], Clause::Return(_))
            && matches!(&query.clauses[i + 3], Clause::Limit(_))
        {
            (true, i + 2, i + 3)
        } else if matches!(
            (&query.clauses[i], &query.clauses[i + 1]),
            (Clause::Match(_), Clause::Return(_))
        ) && i + 2 < query.clauses.len()
            && matches!(&query.clauses[i + 2], Clause::Limit(_))
        {
            (false, i + 1, i + 2)
        } else {
            i += 1;
            continue;
        };

        let safe = if let Clause::Return(r) = &query.clauses[return_offset] {
            !r.distinct
                && !r
                    .items
                    .iter()
                    .any(|item| super::super::ast::is_aggregate_expression(&item.expression))
                && !r
                    .items
                    .iter()
                    .any(|item| super::super::ast::is_window_expression(&item.expression))
        } else {
            false
        };
        if !safe {
            i += 1;
            continue;
        }

        let limit_val = if let Clause::Limit(l) = &query.clauses[limit_offset] {
            match &l.count {
                Expression::Literal(Value::Int64(n)) if *n > 0 => Some(*n as usize),
                _ => None,
            }
        } else {
            None
        };
        let Some(limit) = limit_val else {
            i += 1;
            continue;
        };

        // Only push the LIMIT hint into MATCH when this is the FIRST and ONLY
        // MATCH clause and its pattern shape cannot under-fill after an early
        // cap. Two unsafe shapes:
        //
        // 1. Multi-MATCH (separate `MATCH ... MATCH` clauses): routes through
        //    `execute_match`'s subsequent-MATCH path, where the per-row pattern
        //    executor's `max_matches=remaining` interacts incorrectly with the
        //    outer row loop and produces fewer rows than the LIMIT requests
        //    (regression seen on 3-MATCH + WHERE on last-MATCH variable + LIMIT N
        //    queries — see `test_limit_pushdown_multi_match_safety`).
        // 2. Correlated/filtered multi-pattern within ONE MATCH (comma-separated patterns:
        //    `MATCH (p)-[:T]->(q), (p)-[:T]->(r)`): same row-loop interaction
        //    surfaces because each pattern's expansion is separately bounded
        //    by the limit_hint, so the cartesian's surviving cross-product
        //    can fall short of LIMIT (regression seen on self-join + WHERE
        //    + LIMIT — caught by the differential harness).
        // A strict exception is safe: an unfiltered comma-list of node-only
        // patterns is a pure cartesian product. Every prefix row has the same
        // independent choices in the next pattern, so retaining any N rows at
        // each stage still yields N valid final rows (LIMIT has no ordering
        // contract here). This avoids materialising millions of pairs merely
        // to return the first handful.
        let is_first_match = i == 0;
        let only_match = !query
            .clauses
            .iter()
            .skip(i + 1)
            .any(|c| matches!(c, Clause::Match(_) | Clause::OptionalMatch(_)));
        let limit_safe_pattern_shape = match &query.clauses[i] {
            Clause::Match(m) => {
                m.patterns.len() == 1
                    || (!has_where
                        && m.patterns.len() > 1
                        && m.patterns.iter().all(|pattern| {
                            matches!(pattern.elements.as_slice(), [PatternElement::Node(_)])
                        }))
            }
            _ => false,
        };
        if !is_first_match || !only_match || !limit_safe_pattern_shape {
            i += 1;
            continue;
        }

        // The executor inlines pushable WHERE predicates into the pattern
        // (`push_where_into_match` runs earlier in the optimizer), so the
        // hint is exact in both with-WHERE and without-WHERE cases.
        if let Clause::Match(ref mut m) = query.clauses[i] {
            m.limit_hint = Some(limit);
        }
        query.clauses.remove(limit_offset);
    }
}

/// The single node variable an *aggregate-only* projection reads, when every
/// item is a multiplicity-invariant aggregate over that one variable.
///
/// The escape analysis behind the aggregate route of
/// [`push_distinct_into_match`], and a **whitelist on item shape**, not a
/// variable walk: an item qualifies only as `agg(v)` or `agg(v.prop)` where
/// `agg` is `min`/`max`/`count(DISTINCT …)`/`collect(DISTINCT …)`, so a
/// variable escapes to the consumer iff it is the `v` this returns.
/// `RETURN p.id, count(DISTINCT f)` is rejected because `p.id` is not an
/// aggregate at all, which is what keeps its answer per-source; `RETURN
/// count(DISTINCT f) + 1` because the item is an addition, not an aggregate
/// call. Anything the whitelist does not recognise is rejected, so a new
/// `Expression` variant cannot silently smuggle a second variable past it.
fn aggregate_only_dedup_var(items: &[ReturnItem]) -> Option<String> {
    if items.is_empty() {
        return None;
    }
    let mut var: Option<String> = None;
    for item in items {
        let Expression::FunctionCall {
            name,
            args,
            distinct,
        } = &item.expression
        else {
            return None;
        };
        let nm = name.to_lowercase();
        let invariant = match nm.as_str() {
            "min" | "max" => true,
            "count" | "collect" => *distinct,
            _ => false,
        };
        if !invariant {
            return None;
        }
        let [arg] = args.as_slice() else {
            return None;
        };
        let referenced = match arg {
            Expression::Variable(v) => v.as_str(),
            Expression::PropertyAccess { variable, .. } => variable.as_str(),
            _ => return None,
        };
        match &var {
            None => var = Some(referenced.to_string()),
            Some(prev) if prev == referenced => {}
            Some(_) => return None,
        }
    }
    var
}

/// Push a DISTINCT hint into MATCH when its consumer reads exactly one node
/// variable and collapses row multiplicity.
///
/// Two routes reach the same hint, which lets the executor deduplicate pattern
/// matches by that variable's `NodeIndex` **during** expansion instead of
/// materialising `sources x targets` matches and deduplicating afterwards:
///
/// - `RETURN DISTINCT c2.id` / `RETURN DISTINCT c2.id, c2.name` — every item is
///   a bare variable or property access on one variable.
/// - `RETURN count(DISTINCT f)` / `RETURN count(DISTINCT f), min(f.age)` — every
///   item is a multiplicity-invariant aggregate over one variable
///   ([`aggregate_only_dedup_var`]). Without this route a multi-source k-hop
///   feeding `count(DISTINCT f)` built one row per (source, target) pair, so its
///   peak memory followed the source count rather than the reachable set.
///
/// Both routes require a **single-pattern** MATCH. With comma patterns the hint
/// is applied to the first pattern's matches, before the remaining patterns
/// join: dropping a duplicate keeps one arbitrary representative, and a later
/// pattern (or the clause's own WHERE, which is not fused for a multi-pattern
/// MATCH) can reject exactly that representative while the dropped one would
/// have survived — losing the target entirely.
///
/// Pattern matched: MATCH → [WHERE] → RETURN.
pub(super) fn push_distinct_into_match(query: &mut CypherQuery) {
    for i in 0..query.clauses.len() {
        let match_idx = match &query.clauses[i] {
            Clause::Match(_) => i,
            _ => continue,
        };

        let return_idx = if match_idx + 1 < query.clauses.len() {
            match &query.clauses[match_idx + 1] {
                Clause::Return(_) => match_idx + 1,
                Clause::Where(_) if match_idx + 2 < query.clauses.len() => {
                    if matches!(&query.clauses[match_idx + 2], Clause::Return(_)) {
                        match_idx + 2
                    } else {
                        continue;
                    }
                }
                _ => continue,
            }
        } else {
            continue;
        };

        let Clause::Return(r) = &query.clauses[return_idx] else {
            continue;
        };
        let hint = distinct_route_var(r)
            .map(|var| DistinctNodeHint {
                var,
                aggregate_only: false,
            })
            .or_else(|| {
                aggregate_only_dedup_var(&r.items).map(|var| DistinctNodeHint {
                    var,
                    aggregate_only: true,
                })
            });

        let Some(hint) = hint else {
            continue;
        };
        let dv = &hint.var;
        // Node variable, single-pattern MATCH only — with comma patterns the
        // hint would deduplicate before the join (see the doc comment).
        if let Clause::Match(ref mc) = &query.clauses[match_idx] {
            if mc.patterns.len() != 1 {
                continue;
            }
            let is_node_var = mc.patterns.iter().any(|p| {
                p.elements.iter().any(|e| {
                    if let crate::graph::core::pattern_matching::PatternElement::Node(np) = e {
                        np.variable.as_deref() == Some(dv.as_str())
                    } else {
                        false
                    }
                })
            });
            if !is_node_var {
                continue;
            }
        }
        if let Clause::Match(ref mut mc) = query.clauses[match_idx] {
            mc.distinct_node_hint = Some(hint);
        }
    }
}

/// The `RETURN DISTINCT <one variable>` route: every item is a bare variable or
/// a property access, all naming the same variable, and none is an aggregate
/// (`RETURN DISTINCT a, count(b)` groups before it dedups, so `count(b)` moves
/// with the rows a target-dedup would drop).
fn distinct_route_var(r: &ReturnClause) -> Option<String> {
    if !r.distinct {
        return None;
    }
    if r.items
        .iter()
        .any(|item| super::super::ast::is_aggregate_expression(&item.expression))
    {
        return None;
    }
    let mut var: Option<&str> = None;
    for item in &r.items {
        let v = match &item.expression {
            Expression::PropertyAccess { variable, .. } => variable.as_str(),
            Expression::Variable(v) => v.as_str(),
            _ => return None,
        };
        match var {
            None => var = Some(v),
            Some(prev) if prev == v => {}
            Some(_) => return None,
        }
    }
    var.map(String::from)
}

/// Collected texts that the caller must embed before execution.
///
/// Each entry is `(param_name, query_text)` — the caller embeds the text and
/// inserts the resulting vector into the params map under `param_name`.
pub struct TextScoreRewrite {
    pub texts_to_embed: Vec<(String, String)>,
}

/// Walk the AST and rewrite all `text_score(node, col, query_text)` calls
/// to `vector_score(node, col_emb, $__ts_N)`.
///
/// The query argument can be a string literal or a `$parameter` bound to a
/// string — that text is collected so the caller can embed it and inject the
/// resulting vector into the params map before optimization — **or** it can
/// already be a vector (a list literal, or a `$parameter` bound to a
/// `Value::List`), in which case it passes through untouched and nothing is
/// collected. With an empty collection the caller consults no embedder, so
/// `text_score` with a caller-supplied query vector needs no embedding model.
pub fn rewrite_text_score(
    query: &mut CypherQuery,
    params: &HashMap<String, Value>,
) -> Result<TextScoreRewrite, String> {
    let mut collector = TextScoreCollector {
        counter: 0,
        texts_to_embed: Vec::new(),
    };

    for clause in &mut query.clauses {
        collector.rewrite_clause(clause, params)?;
    }

    Ok(TextScoreRewrite {
        texts_to_embed: collector.texts_to_embed,
    })
}

struct TextScoreCollector {
    counter: usize,
    texts_to_embed: Vec<(String, String)>,
}

/// Classify `text_score`'s query argument: `Some(text)` to embed, or `None`
/// when the argument is already a vector and passes through untouched.
///
/// A string stays *text* even when it looks like `"[1.0, 2.0]"`; the legacy
/// JSON-string vector form is a `vector_score` compatibility path only (see
/// CYPHER.md).
fn text_score_query_arg(
    arg: &Expression,
    params: &HashMap<String, Value>,
) -> Result<Option<String>, String> {
    match arg {
        Expression::Literal(Value::String(s)) => Ok(Some(s.clone())),
        Expression::ListLiteral(_) | Expression::Literal(Value::List(_)) => Ok(None),
        Expression::Parameter(param_name) => match params.get(param_name.as_str()) {
            Some(Value::String(s)) => Ok(Some(s.clone())),
            Some(Value::List(_)) => Ok(None),
            Some(_) => Err(format!(
                "text_score(): parameter ${} must be a string or a list of numbers",
                param_name
            )),
            None => Err(format!("text_score(): parameter ${} not found", param_name)),
        },
        _ => Err("text_score(): third argument must be a string literal, \
                  a list of numbers, or a $parameter"
            .into()),
    }
}

impl TextScoreCollector {
    /// Rewrite every expression a clause can carry. One arm per clause kind;
    /// the write-side property maps and SET-item lists are shared helpers
    /// because CREATE and MERGE spell them identically.
    fn rewrite_clause(
        &mut self,
        clause: &mut Clause,
        params: &HashMap<String, Value>,
    ) -> Result<(), String> {
        match clause {
            Clause::Return(r) => {
                for item in &mut r.items {
                    self.rewrite_expr(&mut item.expression, params)?;
                }
            }
            Clause::Where(w) => {
                self.rewrite_pred(&mut w.predicate, params)?;
            }
            Clause::Match(m) | Clause::OptionalMatch(m) => {
                if let Some(ref mut wh) = m.where_clause {
                    self.rewrite_pred(&mut wh.predicate, params)?;
                }
            }
            Clause::With(w) => {
                for item in &mut w.items {
                    self.rewrite_expr(&mut item.expression, params)?;
                }
                if let Some(ref mut wh) = w.where_clause {
                    self.rewrite_pred(&mut wh.predicate, params)?;
                }
            }
            Clause::OrderBy(o) => {
                for item in &mut o.items {
                    self.rewrite_expr(&mut item.expression, params)?;
                }
            }
            Clause::Unwind(u) => {
                self.rewrite_expr(&mut u.expression, params)?;
            }
            Clause::Delete(d) => {
                for expr in &mut d.expressions {
                    self.rewrite_expr(expr, params)?;
                }
            }
            Clause::Set(s) => self.rewrite_set_items(&mut s.items, params)?,
            Clause::Create(c) => {
                for pattern in &mut c.patterns {
                    self.rewrite_write_pattern(pattern, params)?;
                }
            }
            Clause::Merge(m) => {
                self.rewrite_write_pattern(&mut m.pattern, params)?;
                for items in [m.on_create.as_mut(), m.on_match.as_mut()]
                    .into_iter()
                    .flatten()
                {
                    self.rewrite_set_items(items, params)?;
                }
            }
            Clause::Skip(s) => {
                self.rewrite_expr(&mut s.count, params)?;
            }
            Clause::Limit(l) => {
                self.rewrite_expr(&mut l.count, params)?;
            }
            // Remove: no expressions
            // Fused clauses: don't exist yet (created by optimize, which runs after rewrite)
            _ => {}
        }
        Ok(())
    }

    fn rewrite_write_pattern(
        &mut self,
        pattern: &mut CreatePattern,
        params: &HashMap<String, Value>,
    ) -> Result<(), String> {
        for element in &mut pattern.elements {
            let properties = match element {
                CreateElement::Node(n) => &mut n.properties,
                CreateElement::Edge(e) => &mut e.properties,
            };
            for (_, expr) in properties {
                self.rewrite_expr(expr, params)?;
            }
        }
        Ok(())
    }

    fn rewrite_set_items(
        &mut self,
        items: &mut [SetItem],
        params: &HashMap<String, Value>,
    ) -> Result<(), String> {
        for item in items {
            match item {
                SetItem::Property { expression, .. } | SetItem::Map { expression, .. } => {
                    self.rewrite_expr(expression, params)?;
                }
                SetItem::Label { .. } => {}
            }
        }
        Ok(())
    }
    /// Rewrite one `text_score(node, col, query [, metric])` call in place into
    /// `vector_score(node, '{col}_emb', query [, metric])`. A text query is
    /// replaced by the `$__ts_N` parameter the caller embeds into; a vector
    /// query is left exactly as written — see [`text_score_query_arg`].
    fn rewrite_text_score_call(
        &mut self,
        name: &mut String,
        args: &mut [Expression],
        params: &HashMap<String, Value>,
    ) -> Result<(), String> {
        if args.len() != 3 && args.len() != 4 {
            return Err(
                "text_score() requires 3 arguments: (node, text_column, query_text) \
                 with optional 4th metric argument"
                    .into(),
            );
        }

        let col_name = match &args[1] {
            Expression::Literal(Value::String(s)) => s.clone(),
            _ => {
                return Err(
                    "text_score(): second argument must be a string literal column name".into(),
                );
            }
        };
        let query_text = text_score_query_arg(&args[2], params)?;

        *name = "vector_score".to_string();
        args[1] = Expression::Literal(Value::String(crate::graph::embeddings::store_name(
            &col_name,
        )));

        if let Some(query_text) = query_text {
            args[2] = Expression::Parameter(self.param_for_text(query_text));
        }
        Ok(())
    }

    /// The `$__ts_N` parameter that will carry `query_text`'s vector. Two
    /// calls with the same text share one parameter, so the embedder sees
    /// each distinct query once.
    fn param_for_text(&mut self, query_text: String) -> String {
        if let Some((existing, _)) = self.texts_to_embed.iter().find(|(_, t)| t == &query_text) {
            return existing.clone();
        }
        let pname = format!("__ts_{}", self.counter);
        self.counter += 1;
        self.texts_to_embed.push((pname.clone(), query_text));
        pname
    }

    fn rewrite_expr(
        &mut self,
        expr: &mut Expression,
        params: &HashMap<String, Value>,
    ) -> Result<(), String> {
        match expr {
            Expression::FunctionCall { name, args, .. } if name == "text_score" => {
                self.rewrite_text_score_call(name, args, params)
            }
            Expression::FunctionCall { args, .. } => {
                for arg in args.iter_mut() {
                    self.rewrite_expr(arg, params)?;
                }
                Ok(())
            }
            Expression::Add(l, r)
            | Expression::Subtract(l, r)
            | Expression::Multiply(l, r)
            | Expression::Divide(l, r)
            | Expression::Modulo(l, r)
            | Expression::Concat(l, r) => {
                self.rewrite_expr(l, params)?;
                self.rewrite_expr(r, params)?;
                Ok(())
            }
            Expression::Negate(inner) => self.rewrite_expr(inner, params),
            Expression::ListLiteral(items) => {
                for item in items.iter_mut() {
                    self.rewrite_expr(item, params)?;
                }
                Ok(())
            }
            Expression::Case {
                operand,
                when_clauses,
                else_expr,
            } => {
                if let Some(op) = operand {
                    self.rewrite_expr(op, params)?;
                }
                for (cond, result) in when_clauses.iter_mut() {
                    match cond {
                        CaseCondition::Expression(e) => self.rewrite_expr(e, params)?,
                        CaseCondition::Predicate(p) => self.rewrite_pred(p, params)?,
                    }
                    self.rewrite_expr(result, params)?;
                }
                if let Some(el) = else_expr {
                    self.rewrite_expr(el, params)?;
                }
                Ok(())
            }
            Expression::IndexAccess { expr, index } => {
                self.rewrite_expr(expr, params)?;
                self.rewrite_expr(index, params)?;
                Ok(())
            }
            Expression::ListSlice { expr, start, end } => {
                self.rewrite_expr(expr, params)?;
                if let Some(s) = start {
                    self.rewrite_expr(s, params)?;
                }
                if let Some(e) = end {
                    self.rewrite_expr(e, params)?;
                }
                Ok(())
            }
            Expression::ListComprehension {
                list_expr,
                filter,
                map_expr,
                ..
            } => {
                self.rewrite_expr(list_expr, params)?;
                if let Some(f) = filter {
                    self.rewrite_pred(f, params)?;
                }
                if let Some(m) = map_expr {
                    self.rewrite_expr(m, params)?;
                }
                Ok(())
            }
            Expression::MapProjection { items, .. } => {
                for item in items.iter_mut() {
                    if let MapProjectionItem::Alias { expr, .. } = item {
                        self.rewrite_expr(expr, params)?;
                    }
                }
                Ok(())
            }
            Expression::MapLiteral(entries) => {
                for (_, expr) in entries.iter_mut() {
                    self.rewrite_expr(expr, params)?;
                }
                Ok(())
            }
            Expression::PropertyAccess { .. }
            | Expression::Variable(_)
            | Expression::Literal(_)
            | Expression::Parameter(_)
            | Expression::Star => Ok(()),
            Expression::IsNull(inner) | Expression::IsNotNull(inner) => {
                self.rewrite_expr(inner, params)
            }
            Expression::QuantifiedList {
                list_expr, filter, ..
            } => {
                self.rewrite_expr(list_expr, params)?;
                self.rewrite_pred(filter, params)?;
                Ok(())
            }
            Expression::WindowFunction {
                partition_by,
                order_by,
                ..
            } => {
                for expr in partition_by.iter_mut() {
                    self.rewrite_expr(expr, params)?;
                }
                for item in order_by.iter_mut() {
                    self.rewrite_expr(&mut item.expression, params)?;
                }
                Ok(())
            }
            Expression::PredicateExpr(pred) => self.rewrite_pred(pred, params),
            Expression::ExprPropertyAccess { expr, .. } => self.rewrite_expr(expr, params),
            Expression::CountSubquery { where_clause, .. } => {
                // Patterns don't carry text_score() calls; only the
                // optional WHERE predicate might. Rewrite it if present.
                if let Some(pred) = where_clause.as_deref_mut() {
                    self.rewrite_pred(pred, params)?;
                }
                Ok(())
            }
            Expression::Reduce {
                init,
                list_expr,
                body,
                ..
            } => {
                self.rewrite_expr(init, params)?;
                self.rewrite_expr(list_expr, params)?;
                self.rewrite_expr(body, params)?;
                Ok(())
            }
        }
    }

    fn rewrite_pred(
        &mut self,
        pred: &mut Predicate,
        params: &HashMap<String, Value>,
    ) -> Result<(), String> {
        match pred {
            Predicate::Comparison { left, right, .. } => {
                self.rewrite_expr(left, params)?;
                self.rewrite_expr(right, params)?;
                Ok(())
            }
            Predicate::And(l, r) | Predicate::Or(l, r) | Predicate::Xor(l, r) => {
                self.rewrite_pred(l, params)?;
                self.rewrite_pred(r, params)?;
                Ok(())
            }
            Predicate::Not(inner) => self.rewrite_pred(inner, params),
            Predicate::IsNull(e) | Predicate::IsNotNull(e) => self.rewrite_expr(e, params),
            Predicate::In { expr, list } => {
                self.rewrite_expr(expr, params)?;
                for item in list.iter_mut() {
                    self.rewrite_expr(item, params)?;
                }
                Ok(())
            }
            Predicate::InLiteralSet { expr, .. } => self.rewrite_expr(expr, params),
            Predicate::StartsWith { expr, pattern }
            | Predicate::EndsWith { expr, pattern }
            | Predicate::Contains { expr, pattern } => {
                self.rewrite_expr(expr, params)?;
                self.rewrite_expr(pattern, params)?;
                Ok(())
            }
            Predicate::Exists { .. } => Ok(()),
            Predicate::InExpression { expr, list_expr } => {
                self.rewrite_expr(expr, params)?;
                self.rewrite_expr(list_expr, params)?;
                Ok(())
            }
            Predicate::LabelCheck { .. } => Ok(()),
        }
    }
}

/// Rewrite `Match-Match-Return(group, aggregate) [OrderBy] [Limit]` into
/// `Match-Match-With(group_keys, aggregate)-Return(project) [OrderBy] [Limit]`.
///
/// The `RETURN` form is what users naturally write for a cohort top-K
/// query:
/// ```cypher
/// MATCH (p)-[:P27]->({id:20}) MATCH (p)-[r]->()
/// RETURN p.title, count(r) AS d ORDER BY d DESC LIMIT 10
/// ```
/// Without this rewrite, `fuse_match_return_aggregate` only handles a
/// **single** MATCH and `fuse_match_with_aggregate` only fires on the
/// `WITH(aggregate)` shape, so the query falls off the fused-top-K path
/// and runs ~14× slower than the equivalent
/// `WITH p.title AS t, count(r) AS d RETURN t, d` form.
///
/// **Important**: the WITH groups by *each non-aggregate RETURN
/// expression*, not by the source variable. `RETURN p.city,
/// count(c)` groups per city (the user-written expression), not per
/// p (the variable). The earlier shape — `WITH p, count(c)` —
/// over-finely grouped (one row per p) and produced silently wrong
/// counts when the property had duplicates across p instances. The
/// harness in `tests/test_cypher_differential.py` caught this for
/// `MATCH (p) MATCH (c) RETURN p.city, count(c)`.
///
/// Conditions (any miss → no rewrite):
/// - Exactly two consecutive Match clauses (no OPTIONAL, no path
///   assignments) followed by Return.
/// - The Return has at least one aggregate item AND at least one
///   non-aggregate item.
/// - Every non-aggregate item is `Variable(v)` or `PropertyAccess
///   { variable: v, … }` for the same single variable `v`. (Lets the
///   downstream `fuse_match_with_aggregate` planner reason about the
///   group keys against the join graph.)
/// - Every aggregate item has a user-supplied alias (so the rewritten
///   Return can refer to it by name, and ORDER BY targets remain
///   stable).
/// - No HAVING / DISTINCT on the Return (those interact with the WITH
///   semantics in ways the simple rewrite would change).
pub(super) fn desugar_multi_match_return_aggregate(query: &mut CypherQuery) {
    use super::super::ast::is_aggregate_expression;

    // Locate the `Match, Match, Return` window. Allow optional ORDER BY /
    // LIMIT after — they pass through unchanged.
    let mut return_idx = None;
    for i in 0..query.clauses.len().saturating_sub(2) {
        let m1_ok = matches!(
            &query.clauses[i],
            Clause::Match(m) if m.path_assignments.is_empty()
        );
        let m2_ok = matches!(
            &query.clauses[i + 1],
            Clause::Match(m) if m.path_assignments.is_empty()
        );
        let r_ok = matches!(&query.clauses[i + 2], Clause::Return(_));
        if m1_ok && m2_ok && r_ok {
            return_idx = Some(i + 2);
            break;
        }
    }
    let r_idx = match return_idx {
        Some(idx) => idx,
        None => return,
    };

    // Snapshot the Return to avoid borrow conflicts with the mutation below.
    let (orig_items, distinct, having) = match &query.clauses[r_idx] {
        Clause::Return(r) => (r.items.clone(), r.distinct, r.having.clone()),
        _ => return,
    };
    if distinct || having.is_some() {
        return;
    }

    let mut group_var: Option<String> = None;
    let mut all_aggs_aliased = true;
    let mut has_agg = false;
    let mut has_non_agg = false;
    for item in &orig_items {
        if is_aggregate_expression(&item.expression) {
            has_agg = true;
            if item.alias.is_none() {
                all_aggs_aliased = false;
                break;
            }
            continue;
        }
        has_non_agg = true;
        let v = match &item.expression {
            Expression::Variable(v) => v.clone(),
            Expression::PropertyAccess { variable, .. } => variable.clone(),
            _ => return,
        };
        match &group_var {
            Some(prev) if prev != &v => return,
            _ => group_var = Some(v),
        }
    }
    if !has_agg || !has_non_agg || !all_aggs_aliased {
        return;
    }
    if group_var.is_none() {
        return;
    }

    // Non-aggregate items move into the WITH under synthetic aliases. Pushing
    // the property expressions rather than the source variable is what
    // preserves Cypher's grouping semantics (see the doc comment), but puts the
    // variable out of scope, so the new RETURN must reference WITH outputs by
    // alias, carrying the user's display name in the alias slot.
    let mut with_items: Vec<ReturnItem> = Vec::with_capacity(orig_items.len());
    let mut new_return_items: Vec<ReturnItem> = Vec::with_capacity(orig_items.len());
    for (idx, item) in orig_items.iter().enumerate() {
        if is_aggregate_expression(&item.expression) {
            let alias = item.alias.clone().expect("aliased above");
            with_items.push(item.clone());
            new_return_items.push(ReturnItem {
                expression: Expression::Variable(alias.clone()),
                alias: Some(alias),
            });
        } else {
            let internal = format!("__dgr_grp_{idx}");
            with_items.push(ReturnItem {
                expression: item.expression.clone(),
                alias: Some(internal.clone()),
            });
            new_return_items.push(ReturnItem {
                expression: Expression::Variable(internal),
                alias: item.alias.clone().or_else(|| {
                    // Preserve the column name the unfused path would produce.
                    Some(default_column_name(&item.expression))
                }),
            });
        }
    }

    let new_with = Clause::With(WithClause {
        items: with_items,
        distinct: false,
        where_clause: None,
        group_limit_hint: None,
    });
    let new_return = Clause::Return(ReturnClause {
        items: new_return_items,
        distinct: false,
        having: None,
        lazy_eligible: false,
        group_limit_hint: None,
    });
    query.clauses[r_idx] = new_with;
    query.clauses.insert(r_idx + 1, new_return);
}

/// The display name an unaliased RETURN item would surface as.
fn default_column_name(expr: &Expression) -> String {
    match expr {
        Expression::Variable(v) => v.clone(),
        Expression::PropertyAccess { variable, property } => format!("{variable}.{property}"),
        // Fall back to a debug rendering for shapes that don't appear
        // in the desugar's accepted items (the caller bails on those).
        other => format!("{other:?}"),
    }
}

/// Strip a `WITH x [, y, ...]` clause that's a pure projection and is a
/// no-op for everything that follows it. Removing such a clause turns
/// `Match-With(p)-Match-…` into `Match-Match-…`, which existing fusion
/// passes (`fuse_match_with_aggregate`, `fuse_match_return_aggregate`,
/// the multi-MATCH desugar) can then collapse into a streaming form.
///
/// Motivating case — the cohort top-K idiom:
/// ```cypher
/// MATCH (p)-[:P27]->({id:20}) WITH p MATCH (p)-[r]->()
/// RETURN p.title, count(r) AS d ORDER BY d DESC LIMIT 10
/// ```
/// Without the fold, the `WITH p` blocks fusion and the executor
/// materialises ~3.7M edge bindings before aggregating. With the fold,
/// the same query collapses into the fused top-K path and runs ~25×
/// faster at warm cache.
///
/// Fold conditions (any miss → keep the WITH):
/// - The WITH has no DISTINCT, no inline WHERE, no aggregates, no item
///   aliases that rename the source variable.
/// - The next clause is **not** ORDER BY / SKIP / LIMIT — those bind
///   to the WITH textually and must keep the projection scope.
/// - Every variable referenced anywhere downstream of the WITH appears
///   in the WITH's projection list. (If the user references a variable
///   that the WITH was hiding, the original query was a Cypher scope
///   error; we don't silently make it work.)
pub(super) fn fold_pass_through_with(query: &mut CypherQuery) {
    let mut i = 0;
    while i < query.clauses.len() {
        let projected = match pass_through_projection(&query.clauses[i]) {
            Some(p) => p,
            None => {
                i += 1;
                continue;
            }
        };

        // These bind to the WITH's row context; folding would re-attach them
        // to a different scope.
        if matches!(
            query.clauses.get(i + 1),
            Some(Clause::OrderBy(_)) | Some(Clause::Skip(_)) | Some(Clause::Limit(_))
        ) {
            i += 1;
            continue;
        }

        // Only references to variables bound *before* this WITH can be hidden
        // by it; anything introduced after the WITH is unaffected by the fold.
        let mut pre_with_bound: HashSet<String> = HashSet::new();
        for c in &query.clauses[..i] {
            collect_introduced_variables(c, &mut pre_with_bound);
        }

        let mut downstream_refs: HashSet<String> = HashSet::new();
        for c in &query.clauses[i + 1..] {
            collect_clause_variables(c, &mut downstream_refs);
        }

        let safe = downstream_refs
            .iter()
            .filter(|v| pre_with_bound.contains(*v))
            .all(|v| projected.contains(v));

        if safe {
            query.clauses.remove(i);
            // Don't advance i — re-examine the new clauses[i].
        } else {
            i += 1;
        }
    }
}

/// Collect the variable names *introduced* (newly bound) by `clause` into
/// `out`: MATCH / OPTIONAL MATCH pattern variables (node / edge / path), WITH
/// and RETURN aliases (and bare-`Variable` pass-throughs), UNWIND aliases, and
/// a nested `CALL { }` subquery's terminal RETURN output columns.
pub(crate) fn collect_introduced_variables(clause: &Clause, out: &mut HashSet<String>) {
    match clause {
        Clause::Match(m) | Clause::OptionalMatch(m) => {
            for pat in &m.patterns {
                for elem in &pat.elements {
                    match elem {
                        PatternElement::Node(np) => {
                            if let Some(v) = &np.variable {
                                out.insert(v.clone());
                            }
                        }
                        PatternElement::Edge(ep) => {
                            if let Some(v) = &ep.variable {
                                out.insert(v.clone());
                            }
                        }
                    }
                }
            }
            for pa in &m.path_assignments {
                out.insert(pa.variable.clone());
            }
        }
        Clause::With(w) => {
            for item in &w.items {
                let name = item.alias.clone().or_else(|| match &item.expression {
                    Expression::Variable(v) => Some(v.clone()),
                    _ => None,
                });
                if let Some(n) = name {
                    out.insert(n);
                }
            }
        }
        Clause::Return(r) => {
            // A RETURN can be non-terminal in a subquery body, and its output
            // columns are the declared scope for anything that follows (a
            // CALL { } body's terminal RETURN feeds the outer scope).
            for item in &r.items {
                let name = item.alias.clone().or_else(|| match &item.expression {
                    Expression::Variable(v) => Some(v.clone()),
                    _ => None,
                });
                if let Some(n) = name {
                    out.insert(n);
                }
            }
        }
        Clause::Unwind(u) => {
            out.insert(u.alias.clone());
        }
        Clause::LoadCsv(l) => {
            out.insert(l.variable.clone());
        }
        Clause::CallSubquery { body, .. } => {
            // A nested CALL { } introduces its body's terminal RETURN
            // columns into the outer scope (§1.2 rule 3). Those names are
            // declared for clauses that follow this CallSubquery — including
            // a later correlated CALL { } that imports them.
            if let Some(Clause::Return(r)) = body.clauses.last() {
                for item in &r.items {
                    out.insert(super::super::executor::return_item_column_name(item));
                }
            }
        }
        Clause::Call(_) | Clause::Create(_) | Clause::Merge(_) => {
            // CALL (procedure) / CREATE / MERGE can introduce variables, but
            // those forms don't appear in the shapes these callers target.
            // Be conservative: don't claim to know what they bind.
        }
        _ => {}
    }
}

/// The set of variable names *declared* (newly bound) by the clauses in
/// `clauses`, in order. A thin accumulator over
/// [`collect_introduced_variables`] — used by correlated `CALL { }`
/// import validation to distinguish "never declared" (typo → error) from
/// "declared upstream but absent/null in this row" (seed per the NULL-
/// import paths).
pub(crate) fn declared_variables(clauses: &[Clause]) -> HashSet<String> {
    let mut out = HashSet::new();
    for clause in clauses {
        collect_introduced_variables(clause, &mut out);
    }
    out
}

/// The projected variable names if `clause` is a pass-through WITH: each item
/// a bare `Variable(v)`, no DISTINCT, no inline WHERE, no aggregate.
fn pass_through_projection(clause: &Clause) -> Option<HashSet<String>> {
    let w = match clause {
        Clause::With(w) => w,
        _ => return None,
    };
    if w.distinct || w.where_clause.is_some() {
        return None;
    }
    let mut out = HashSet::with_capacity(w.items.len());
    for item in &w.items {
        if super::super::ast::is_aggregate_expression(&item.expression) {
            return None;
        }
        let var = match &item.expression {
            Expression::Variable(v) => v,
            _ => return None,
        };
        // Aliasing to the same name is a no-op; aliasing to a different
        // name renames the variable and is not a pass-through.
        if let Some(alias) = &item.alias {
            if alias != var {
                return None;
            }
        }
        out.insert(var.clone());
    }
    Some(out)
}

/// Insert the names of the variables `clause` references into `out`.
///
/// Not exhaustive over clause kinds: the write / procedure clauses and the
/// fused shapes contribute nothing, so a caller that needs "this variable is
/// definitely unreferenced" must first check
/// [`unwind_scope_refs_are_enumerable`].
fn collect_clause_variables(clause: &Clause, out: &mut HashSet<String>) {
    match clause {
        Clause::Match(m) | Clause::OptionalMatch(m) => {
            collect_pattern_refs(&m.patterns, out);
            for pa in &m.path_assignments {
                out.insert(pa.variable.clone());
            }
            // A variable referenced only by the clause's own WHERE is still
            // referenced: without this the pass-through-WITH fold would drop
            // the projection an `OPTIONAL MATCH … WHERE w.x = 1` depends on.
            if let Some(wc) = &m.where_clause {
                collect_predicate_refs(&wc.predicate, out);
            }
        }
        Clause::Where(w) => collect_predicate_refs(&w.predicate, out),
        Clause::With(w) => {
            for item in &w.items {
                collect_expression_refs(&item.expression, out);
            }
            if let Some(wh) = &w.where_clause {
                collect_predicate_refs(&wh.predicate, out);
            }
        }
        Clause::Return(r) => {
            for item in &r.items {
                collect_expression_refs(&item.expression, out);
            }
            if let Some(p) = &r.having {
                collect_predicate_refs(p, out);
            }
        }
        Clause::OrderBy(ob) => {
            for item in &ob.items {
                collect_expression_refs(&item.expression, out);
            }
        }
        Clause::Skip(s) => collect_expression_refs(&s.count, out),
        Clause::Limit(l) => collect_expression_refs(&l.count, out),
        Clause::Unwind(u) => collect_expression_refs(&u.expression, out),
        // The source expression may reference a parameter (`FROM $path`);
        // the bound row variable is recorded for the same
        // barrier-correctness reason as FOREACH's loop variable below.
        Clause::LoadCsv(l) => {
            collect_expression_refs(&l.source, out);
            out.insert(l.variable.clone());
        }
        Clause::Union(u) => {
            for c in &u.query.clauses {
                collect_clause_variables(c, out);
            }
        }
        Clause::CallSubquery { import, body } => {
            // A correlated `CALL { }` REFERENCES its imported outer variables
            // (its leading WITH was lifted into `import` at parse time, so
            // they appear nowhere else). Without them recorded,
            // `fold_pass_through_with` would fold away a `WITH p` that a later
            // `CALL { WITH q ... }` depends on, silently re-exposing the
            // dropped `q`. The body's clauses are walked too, so a body
            // reference to an imported name counts; body-internal variables
            // leak into `out` harmlessly — they can't collide with a pre-WITH
            // projection name.
            for name in import {
                out.insert(name.clone());
            }
            for c in &body.clauses {
                collect_clause_variables(c, out);
            }
        }
        Clause::Foreach {
            variable,
            list,
            body,
        } => {
            // The loop variable is body-internal, but recording it is
            // harmless — it can't collide with a pre-WITH projection name.
            collect_expression_refs(list, out);
            out.insert(variable.clone());
            for c in body {
                collect_clause_variables(c, out);
            }
        }
        Clause::Call(_)
        | Clause::Create(_)
        | Clause::Set(_)
        | Clause::Delete(_)
        | Clause::Remove(_)
        | Clause::Merge(_)
        // Schema DDL binds and references no query variables.
        | Clause::Schema(_)
        | Clause::FusedOptionalMatchAggregate { .. }
        | Clause::FusedVectorScoreTopK { .. }
        | Clause::FusedMatchReturnAggregate { .. }
        | Clause::FusedMatchWithAggregate { .. }
        | Clause::FusedOrderByTopK { .. }
        | Clause::FusedCountAll { .. }
        | Clause::FusedCountAllEdges { .. }
        | Clause::FusedCountByType { .. }
        | Clause::FusedCountEdgesByType { .. }
        | Clause::FusedCountTypedNode { .. }
        | Clause::FusedCountTypedEdge { .. }
        | Clause::FusedCountAnchoredEdges { .. }
        | Clause::FusedNodeScanAggregate { .. }
        | Clause::FusedNodeScanTopK { .. }
        | Clause::SpatialJoin { .. } => {
            // These contribute no references: the write / procedure clauses
            // read names this walk does not model, and the fused shapes are
            // built after this pass runs. See
            // `unwind_scope_refs_are_enumerable` for why that omission is
            // safe for `fold_pass_through_with` and not for
            // `narrow_unwind_source`.
        }
    }
}

fn collect_pattern_refs(patterns: &[Pattern], out: &mut HashSet<String>) {
    for pat in patterns {
        for elem in &pat.elements {
            match elem {
                PatternElement::Node(np) => {
                    if let Some(v) = &np.variable {
                        out.insert(v.clone());
                    }
                    if let Some(props) = &np.properties {
                        for matcher in props.values() {
                            collect_property_matcher_refs(matcher, out);
                        }
                    }
                }
                PatternElement::Edge(ep) => {
                    if let Some(v) = &ep.variable {
                        out.insert(v.clone());
                    }
                    if let Some(props) = &ep.properties {
                        for matcher in props.values() {
                            collect_property_matcher_refs(matcher, out);
                        }
                    }
                }
            }
        }
    }
}

fn collect_property_matcher_refs(m: &PropertyMatcher, out: &mut HashSet<String>) {
    match m {
        PropertyMatcher::EqualsVar(name) => {
            out.insert(name.clone());
        }
        PropertyMatcher::EqualsNodeProp { var, .. } => {
            out.insert(var.clone());
        }
        _ => {}
    }
}

fn collect_predicate_refs(pred: &Predicate, out: &mut HashSet<String>) {
    match pred {
        Predicate::Comparison { left, right, .. } => {
            collect_expression_refs(left, out);
            collect_expression_refs(right, out);
        }
        Predicate::And(a, b) | Predicate::Or(a, b) | Predicate::Xor(a, b) => {
            collect_predicate_refs(a, out);
            collect_predicate_refs(b, out);
        }
        Predicate::Not(p) => collect_predicate_refs(p, out),
        Predicate::IsNull(e) | Predicate::IsNotNull(e) => collect_expression_refs(e, out),
        Predicate::In { expr, list } => {
            collect_expression_refs(expr, out);
            for e in list {
                collect_expression_refs(e, out);
            }
        }
        Predicate::InLiteralSet { expr, .. } => collect_expression_refs(expr, out),
        Predicate::StartsWith { expr, pattern }
        | Predicate::EndsWith { expr, pattern }
        | Predicate::Contains { expr, pattern } => {
            collect_expression_refs(expr, out);
            collect_expression_refs(pattern, out);
        }
        Predicate::Exists {
            patterns,
            where_clause,
            ..
        } => {
            collect_pattern_refs(patterns, out);
            if let Some(p) = where_clause {
                collect_predicate_refs(p, out);
            }
        }
        Predicate::InExpression { expr, list_expr } => {
            collect_expression_refs(expr, out);
            collect_expression_refs(list_expr, out);
        }
        Predicate::LabelCheck { variable, .. } => {
            out.insert(variable.clone());
        }
    }
}

/// Collect every variable an expression *reads*.
///
/// A pure AST utility shared with the executor (`helpers::grouping_variables`)
/// and with `schema_check`'s post-aggregation ORDER BY scope. Over-collection
/// is safe for every caller; under-collection is not.
pub(crate) fn collect_expression_refs(expr: &Expression, out: &mut HashSet<String>) {
    match expr {
        Expression::Variable(v) => {
            out.insert(v.clone());
        }
        Expression::PropertyAccess { variable, .. } => {
            out.insert(variable.clone());
        }
        Expression::MapProjection { variable, items } => {
            out.insert(variable.clone());
            for item in items {
                if let MapProjectionItem::Alias { expr, .. } = item {
                    collect_expression_refs(expr, out);
                }
            }
        }
        Expression::Literal(_) | Expression::Star | Expression::Parameter(_) => {}
        Expression::FunctionCall { args, .. } => {
            for a in args {
                collect_expression_refs(a, out);
            }
        }
        Expression::Add(a, b)
        | Expression::Subtract(a, b)
        | Expression::Multiply(a, b)
        | Expression::Divide(a, b)
        | Expression::Modulo(a, b)
        | Expression::Concat(a, b) => {
            collect_expression_refs(a, out);
            collect_expression_refs(b, out);
        }
        Expression::Negate(e) | Expression::IsNull(e) | Expression::IsNotNull(e) => {
            collect_expression_refs(e, out);
        }
        Expression::ListLiteral(items) => {
            for e in items {
                collect_expression_refs(e, out);
            }
        }
        Expression::Case {
            operand,
            when_clauses,
            else_expr,
        } => {
            if let Some(o) = operand {
                collect_expression_refs(o, out);
            }
            for (cond, result) in when_clauses {
                match cond {
                    CaseCondition::Predicate(p) => collect_predicate_refs(p, out),
                    CaseCondition::Expression(e) => collect_expression_refs(e, out),
                }
                collect_expression_refs(result, out);
            }
            if let Some(e) = else_expr {
                collect_expression_refs(e, out);
            }
        }
        Expression::ListComprehension {
            variable: _bound,
            list_expr,
            filter,
            map_expr,
        } => {
            collect_expression_refs(list_expr, out);
            if let Some(p) = filter {
                collect_predicate_refs(p, out);
            }
            if let Some(e) = map_expr {
                collect_expression_refs(e, out);
            }
        }
        Expression::IndexAccess { expr, index } => {
            collect_expression_refs(expr, out);
            collect_expression_refs(index, out);
        }
        Expression::ListSlice { expr, start, end } => {
            collect_expression_refs(expr, out);
            if let Some(s) = start {
                collect_expression_refs(s, out);
            }
            if let Some(e) = end {
                collect_expression_refs(e, out);
            }
        }
        Expression::MapLiteral(pairs) => {
            for (_, e) in pairs {
                collect_expression_refs(e, out);
            }
        }
        Expression::QuantifiedList {
            variable: _bound,
            list_expr,
            filter,
            ..
        } => {
            collect_expression_refs(list_expr, out);
            collect_predicate_refs(filter, out);
        }
        Expression::Reduce {
            init,
            list_expr,
            body,
            ..
        } => {
            collect_expression_refs(init, out);
            collect_expression_refs(list_expr, out);
            collect_expression_refs(body, out);
        }
        Expression::PredicateExpr(p) => collect_predicate_refs(p, out),
        Expression::ExprPropertyAccess { expr, .. } => collect_expression_refs(expr, out),
        Expression::WindowFunction {
            partition_by,
            order_by,
            ..
        } => {
            for e in partition_by {
                collect_expression_refs(e, out);
            }
            for item in order_by {
                collect_expression_refs(&item.expression, out);
            }
        }
        Expression::CountSubquery {
            patterns,
            where_clause,
            ..
        } => {
            collect_pattern_refs(patterns, out);
            if let Some(p) = where_clause {
                collect_predicate_refs(p, out);
            }
        }
    }
}

/// Can the whole reference set of `clause` be enumerated by
/// [`collect_clause_variables`]?
///
/// [`collect_clause_variables`] answers "which variables does this clause
/// mention" for the clause kinds it models, and contributes **nothing** for
/// the write clauses (`CREATE` / `SET` / `MERGE` / `DELETE` / `REMOVE` /
/// `CALL`) and the fused shapes. For [`fold_pass_through_with`] that omission
/// is harmless — a variable it would miss is one the query could not have had
/// in scope anyway. For `narrow_unwind_source` it is **not**: a missed
/// reference means dropping a binding that a later clause still reads, which
/// is a wrong answer rather than a lost optimisation.
///
/// So this pass asks the stricter question and refuses to reason about any
/// clause kind whose references are not fully enumerated. `false` here always
/// costs at most an optimisation.
fn unwind_scope_refs_are_enumerable(clause: &Clause) -> bool {
    // `RETURN *` / `WITH *` name no variable in the AST: the executor expands
    // the star from the *runtime row's* `projected.keys()`
    // (`executor/return_clause.rs`). So a star item references every binding in
    // scope, including the one this pass wants to drop, while
    // `collect_clause_variables` reports nothing for it. Treat it as
    // unenumerable — without this guard `UNWIND ns AS m RETURN *` silently
    // loses its `ns` column.
    let has_star = |items: &[ReturnItem]| {
        items
            .iter()
            .any(|i| matches!(i.expression, Expression::Star))
    };
    match clause {
        Clause::Return(r) if has_star(&r.items) => return false,
        Clause::With(w) if has_star(&w.items) => return false,
        _ => {}
    }

    matches!(
        clause,
        Clause::Match(_)
            | Clause::OptionalMatch(_)
            | Clause::Where(_)
            | Clause::With(_)
            | Clause::Return(_)
            | Clause::OrderBy(_)
            | Clause::Skip(_)
            | Clause::Limit(_)
            | Clause::Unwind(_)
            | Clause::CallSubquery { .. }
    )
    // `Clause::Union` is deliberately absent. A UNION branch that ends without
    // an explicit RETURN has its columns auto-detected from the *runtime row's*
    // key set (`executor/call_clause.rs`), which is the same implicit,
    // row-derived contract as `RETURN *`: dropping a binding silently changes
    // the column set without any clause naming it. Cheap to refuse, and it
    // removes the whole question.
}

/// **Pass helper:** mark each `UNWIND <var> AS alias` whose source binding no
/// downstream clause can observe, so the executor takes the list by move
/// instead of cloning it into every expanded row.
///
/// # Precondition
/// The UNWIND source is a bare [`Expression::Variable`]. A computed source
/// (`UNWIND range(..)`, `UNWIND $param`, `UNWIND [a, b]`) is already not bound
/// in the row, so it never had the problem and is left alone.
///
/// # Pattern matched
/// `... UNWIND v AS alias ...` where every clause after the UNWIND has fully
/// enumerable references (see [`unwind_scope_refs_are_enumerable`]) and none
/// of them mentions `v`.
///
/// # Rewrite
/// Sets `UnwindClause::consume_source`. No clause is added, removed or
/// reordered, and the rewrite is invisible to results — it only changes
/// whether a dead binding is copied `n` times.
///
/// # Why bail
/// - source is not a bare variable → nothing is duplicated; no win available.
/// - `v == alias` → the alias rebinds the same name; downstream references
///   mean the *element*, and the conservative check below already refuses,
///   but the explicit guard keeps the reasoning local.
/// - any downstream clause is a write / fused / procedure clause → its
///   references are not enumerable, so we cannot prove `v` is dead.
/// - `v` is mentioned downstream → the binding is live; copying is required.
pub(super) fn narrow_unwind_source(query: &mut CypherQuery) {
    for i in 0..query.clauses.len() {
        let Clause::Unwind(u) = &query.clauses[i] else {
            continue;
        };
        let Expression::Variable(var) = &u.expression else {
            continue;
        };
        if *var == u.alias {
            continue;
        }
        let var = var.clone();

        let tail = &query.clauses[i + 1..];
        if !tail.iter().all(unwind_scope_refs_are_enumerable) {
            continue;
        }
        let mut downstream: HashSet<String> = HashSet::new();
        for c in tail {
            collect_clause_variables(c, &mut downstream);
        }
        if downstream.contains(&var) {
            continue;
        }

        if let Clause::Unwind(u) = &mut query.clauses[i] {
            u.consume_source = true;
        }
    }
}

#[cfg(test)]
mod narrow_unwind_source_tests {
    use super::*;
    use crate::graph::languages::cypher::parser::parse_cypher;

    /// Whether `narrow_unwind_source` marked the (single) UNWIND clause.
    fn narrows(query: &str) -> bool {
        let mut q = parse_cypher(query).expect("fixture parses");
        narrow_unwind_source(&mut q);
        let marks: Vec<bool> = q
            .clauses
            .iter()
            .filter_map(|c| match c {
                Clause::Unwind(u) => Some(u.consume_source),
                _ => None,
            })
            .collect();
        assert!(!marks.is_empty(), "fixture has no UNWIND clause");
        marks[0]
    }

    #[test]
    fn narrows_when_source_is_dead_after_the_unwind() {
        assert!(narrows(
            "MATCH (p:Person) WITH collect(p.age) AS ns UNWIND ns AS m RETURN m"
        ));
    }

    #[test]
    fn bails_when_source_is_read_downstream() {
        assert!(
            !narrows(
                "MATCH (p:Person) WITH collect(p.age) AS ns UNWIND ns AS m \
                 RETURN m, size(ns) AS c"
            ),
            "the source list is still read by RETURN — dropping it loses data"
        );
    }

    /// `RETURN *` names no variable in the AST; the executor expands it from
    /// the runtime row's `projected` keys. Without the star guard the pass
    /// would call the binding dead and silently drop a returned column.
    #[test]
    fn bails_on_return_star() {
        assert!(
            !narrows("MATCH (p:Person) WITH collect(p.age) AS ns UNWIND ns AS m RETURN *"),
            "RETURN * observes every binding, including the UNWIND source"
        );
    }

    #[test]
    fn bails_on_with_star() {
        assert!(!narrows(
            "MATCH (p:Person) WITH collect(p.age) AS ns UNWIND ns AS m WITH * RETURN m"
        ));
    }

    #[test]
    fn bails_when_a_later_unwind_rereads_the_list() {
        assert!(!narrows(
            "MATCH (p:Person) WITH collect(p.age) AS ns UNWIND ns AS a UNWIND ns AS b RETURN a, b"
        ));
    }

    /// Write clauses contribute no references to `collect_clause_variables`
    /// (see `unwind_scope_refs_are_enumerable`), so the pass must refuse to
    /// reason about them rather than read the silence as "dead".
    #[test]
    fn bails_when_a_downstream_clause_has_unenumerable_refs() {
        assert!(
            !narrows(
                "MATCH (p:Person) WITH collect(p.age) AS ns UNWIND ns AS m \
                 CREATE (:Tag {v: m, all: ns})"
            ),
            "a write clause's references are not enumerable — cannot prove the source is dead"
        );
    }

    /// A UNION branch without an explicit RETURN derives its columns from the
    /// runtime row's keys, so dropping a binding changes the column set with no
    /// clause naming it — the same implicit contract as `RETURN *`.
    #[test]
    fn bails_on_a_downstream_union() {
        assert!(!narrows(
            "MATCH (p:Person) WITH collect(p.age) AS ns UNWIND ns AS m RETURN m \
             UNION MATCH (q:Person) RETURN q.age AS m"
        ));
    }

    #[test]
    fn ignores_a_computed_source() {
        // Nothing is bound in the row, so there is nothing to narrow.
        assert!(!narrows("UNWIND range(0, 10) AS m RETURN m"));
        assert!(!narrows("UNWIND [1, 2, 3] AS m RETURN m"));
    }

    #[test]
    fn ignores_a_self_rebinding_alias() {
        assert!(!narrows(
            "MATCH (p:Person) WITH collect(p.age) AS ns UNWIND ns AS ns RETURN ns"
        ));
    }

    #[test]
    fn narrows_when_only_the_alias_is_used_downstream() {
        assert!(narrows(
            "MATCH (p:Person) WITH collect(p.age) AS ns UNWIND ns AS m \
             WITH m WHERE m > 2 RETURN collect(m) AS back"
        ));
    }
}