surrealdb-core 3.2.0

A scalable, distributed, collaborative, document-graph database, for the realtime web
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
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
//! Snapshot tests for the GQL → [`MatchPlan`] lowering.
//!
//! Each lowering test pins a deterministic, compact rendering of the lowered
//! [`MatchPlan`] (binding table, pattern, NNF-split predicates with their
//! dependency sets, and the output spec), verified against the construction
//! rules of `doc/gql/V2_DESIGN.md` §8. Predicate, column and order slots
//! render via [`Expr`]'s `ToSql` so the 3VL guard shapes stay literally
//! visible. Each rejection test pins both the error message and the source
//! slice its span covers.

use surrealdb_types::{SqlFormat, ToSql};

use crate::expr::Expr;
use crate::expr::match_plan::{
	BindingDef, BindingId, BindingKind, DetachMode, EdgeQuantifier, ExpandDirection,
	MatchClausePlan, MatchPlan, MatchStage, MutationStage, NodeStep, PatternPlan, UpdateData,
};
use crate::expr::plan::TopLevelExpr;
use crate::gql::{GqlParserSettings, lower, parse_str, parse_to_plan_with_settings};

// ------------------------------------------------------------------------
// Test harness: a compact, deterministic rendering of a `MatchPlan`.
// ------------------------------------------------------------------------

/// Parses and lowers a GQL query and returns the lowered [`MatchPlan`].
fn lower_plan(source: &str) -> MatchPlan {
	let query = match parse_str(source) {
		Ok(query) => query,
		Err(e) => panic!("failed to parse {source:?}: {:?}", e.render_on(source)),
	};
	let prepared = match lower(query) {
		Ok(prepared) => prepared,
		Err(e) => panic!("failed to lower {source:?}: {:?}", e.render_on(source)),
	};
	match prepared.0.expressions.as_slice() {
		[TopLevelExpr::Expr(Expr::Match(plan))] => (**plan).clone(),
		other => panic!("unexpected lowered plan for {source:?}: {other:?}"),
	}
}

/// Parses and lowers, returning the compact rendering of the lowered plan.
fn render(source: &str) -> String {
	render_plan(&lower_plan(source))
}

/// Renders an [`Expr`] (predicate / column / order slot) the way the IR does:
/// single-line via `ToSql`, so guard shapes stay diffable.
fn render_expr(expr: &Expr) -> String {
	let mut out = String::new();
	expr.fmt_sql(&mut out, SqlFormat::SingleLine);
	out
}

/// A compact, deterministic rendering of a [`MatchPlan`]:
///
/// ```text
/// bindings: a:Node k:Edge b:Node
/// MATCH (a:person)-[k:knows]->(b:person)
///   WHERE [k] k.since != NONE AND k.since != NULL AND k.since > 2020
/// RETURN a.name AS a_name, b.name AS b_name
/// ```
fn render_plan(plan: &MatchPlan) -> String {
	let mut out = String::new();
	out.push_str("bindings:");
	for binding in &plan.bindings {
		out.push(' ');
		out.push_str(&render_binding(binding));
	}
	out.push('\n');

	for stage in &plan.stages {
		match stage {
			MatchStage::Read(clause) => render_clause(plan, clause, &mut out),
			MatchStage::Mutate(mutation) => render_mutation(plan, mutation, &mut out),
		}
	}

	render_output(plan, &mut out);
	out
}

/// Renders one mutation stage as a compact, single-line step so interleaved
/// read/write programs stay diffable in the lowering snapshots.
fn render_mutation(plan: &MatchPlan, mutation: &MutationStage, out: &mut String) {
	let name = |id: BindingId| name_of(plan, id).to_owned();
	match mutation {
		MutationStage::Update {
			target,
			data,
		} => match data {
			UpdateData::Set(items) => {
				let assigns: Vec<String> = items
					.iter()
					.map(|(idiom, expr)| format!("{} = {}", idiom.to_sql(), render_expr(expr)))
					.collect();
				out.push_str(&format!("SET [{}] {}\n", name(*target), assigns.join(", ")));
			}
			UpdateData::Unset(paths) => {
				let paths: Vec<String> = paths.iter().map(|idiom| idiom.to_sql()).collect();
				out.push_str(&format!("REMOVE [{}] {}\n", name(*target), paths.join(", ")));
			}
			UpdateData::Content(expr) => {
				out.push_str(&format!("SET [{}] = {}\n", name(*target), render_expr(expr)));
			}
		},
		MutationStage::Delete {
			target,
			detach,
		} => {
			let mode = match detach {
				DetachMode::Detach => "DETACH ",
				DetachMode::NoDetach => "",
			};
			out.push_str(&format!("{mode}DELETE [{}]\n", name(*target)));
		}
		MutationStage::Insert(stage) => {
			let nodes: Vec<String> = stage
				.nodes
				.iter()
				.map(|node| format!("({}:{})", name(node.binding), node.label.as_str()))
				.collect();
			let edges: Vec<String> = stage
				.edges
				.iter()
				.map(|edge| {
					format!(
						"({})-[{}:{}]->({})",
						name(edge.from),
						name(edge.binding),
						edge.label.as_str(),
						name(edge.to),
					)
				})
				.collect();
			out.push_str("INSERT ");
			out.push_str(&nodes.join(" "));
			if !edges.is_empty() {
				if !nodes.is_empty() {
					out.push(' ');
				}
				out.push_str(&edges.join(" "));
			}
			out.push('\n');
		}
	}
}

/// Renders one binding as `name:Kind` (a hidden binding is suffixed `*`).
fn render_binding(binding: &BindingDef) -> String {
	let kind = match binding.kind {
		BindingKind::Node => "Node",
		BindingKind::Edge => "Edge",
		BindingKind::EdgeGroup => "EdgeGroup",
		BindingKind::Path => "Path",
	};
	let hidden = if binding.user_named {
		""
	} else {
		"*"
	};
	format!("{}:{kind}{hidden}", binding.name)
}

fn render_clause(plan: &MatchPlan, clause: &MatchClausePlan, out: &mut String) {
	// A clause is optional exactly when it carries a block id (the single source of
	// truth). Render the all-or-nothing block id alongside the OPTIONAL keyword so
	// the snapshots pin block grouping (clauses sharing an id are one left-outer
	// unit; distinct ids chain left-to-right); a mandatory clause renders neither.
	if let Some(group) = clause.optional_group {
		out.push_str(&format!("OPTIONAL#{group} "));
	}
	out.push_str("MATCH ");
	for (i, pattern) in clause.patterns.iter().enumerate() {
		if i > 0 {
			out.push_str(", ");
		}
		render_pattern(plan, pattern, out);
	}
	out.push('\n');
	for predicate in &clause.predicates {
		out.push_str("  WHERE [");
		out.push_str(&render_deps(plan, &predicate.deps));
		out.push_str("] ");
		out.push_str(&render_expr(&predicate.expr));
		out.push('\n');
	}
}

/// Renders the dependency set as space-joined binding names in id order.
fn render_deps(plan: &MatchPlan, deps: &[u32]) -> String {
	deps.iter()
		.map(|id| plan.bindings.get(*id as usize).map(|b| b.name.as_str()).unwrap_or("?"))
		.collect::<Vec<_>>()
		.join(" ")
}

fn render_pattern(plan: &MatchPlan, pattern: &PatternPlan, out: &mut String) {
	if let Some(path) = pattern.path_var {
		out.push_str(name_of(plan, path));
		out.push_str(" = ");
	}
	render_node(plan, &pattern.start, out);
	for (edge, node) in &pattern.steps {
		match edge.direction {
			ExpandDirection::Out => out.push('-'),
			ExpandDirection::In => out.push_str("<-"),
		}
		out.push('[');
		out.push_str(name_of(plan, edge.binding));
		if let Some(label) = &edge.label {
			out.push(':');
			out.push_str(label.as_str());
		}
		out.push(']');
		match edge.direction {
			ExpandDirection::Out => out.push_str("->"),
			ExpandDirection::In => out.push('-'),
		}
		if let Some(quantifier) = &edge.quantifier {
			render_quantifier(quantifier, out);
		}
		render_node(plan, node, out);
	}
}

fn render_node(plan: &MatchPlan, node: &NodeStep, out: &mut String) {
	out.push('(');
	out.push_str(name_of(plan, node.binding));
	if let Some(label) = &node.label {
		out.push(':');
		out.push_str(label.as_str());
	}
	out.push(')');
}

fn render_quantifier(quantifier: &EdgeQuantifier, out: &mut String) {
	out.push('{');
	out.push_str(&quantifier.min.to_string());
	out.push(',');
	if let Some(max) = quantifier.max {
		out.push_str(&max.to_string());
	}
	out.push('}');
}

fn render_output(plan: &MatchPlan, out: &mut String) {
	// The lowering test harness only lowers read queries, which always carry an
	// output spec.
	let output = plan.output.as_ref().expect("a read query carries a RETURN output");
	out.push_str("RETURN");
	if output.distinct {
		out.push_str(" DISTINCT");
	}
	for (i, column) in output.columns.iter().enumerate() {
		if i > 0 {
			out.push(',');
		}
		out.push(' ');
		out.push_str(&render_expr(&column.expr));
		out.push_str(" AS ");
		out.push_str(&column.name);
	}
	if let Some(keys) = &output.group_by {
		if keys.is_empty() {
			out.push_str("\nGROUP ALL");
		} else {
			out.push_str("\nGROUP BY");
			for (i, key) in keys.iter().enumerate() {
				if i > 0 {
					out.push(',');
				}
				out.push(' ');
				out.push_str(&render_expr(key));
			}
		}
	}
	if !output.order.is_empty() {
		out.push_str("\nORDER BY");
		for (i, order) in output.order.iter().enumerate() {
			if i > 0 {
				out.push(',');
			}
			out.push(' ');
			out.push_str(&render_expr(&order.expr));
			out.push_str(if order.ascending {
				" ASC"
			} else {
				" DESC"
			});
		}
	}
	if let Some(skip) = &output.skip {
		out.push_str("\nSKIP ");
		out.push_str(&render_expr(skip));
	}
	if let Some(limit) = &output.limit {
		out.push_str("\nLIMIT ");
		out.push_str(&render_expr(limit));
	}
}

fn name_of(plan: &MatchPlan, id: u32) -> &str {
	plan.bindings.get(id as usize).map(|b| b.name.as_str()).unwrap_or("?")
}

/// Parses successfully, then lowers expecting an error; returns the rendered
/// error and the source slices covered by the error's spans.
fn lower_err(source: &str) -> (String, Vec<String>) {
	let query = match parse_str(source) {
		Ok(query) => query,
		Err(e) => panic!("failed to parse {source:?}: {:?}", e.render_on(source)),
	};
	let error = lower(query).expect_err("lowering should have failed");
	let rendered = format!("{:?}", error.render_on(source));
	let mut slices = Vec::new();
	error.update_spans(|span| {
		let range = span.to_range();
		slices.push(source[range.start as usize..range.end as usize].to_owned());
	});
	(rendered, slices)
}

/// Asserts that lowering fails with a message containing `message` and a span
/// covering exactly `slice` in the source.
#[track_caller]
fn assert_rejects(source: &str, message: &str, slice: &str) {
	let (rendered, slices) = lower_err(source);
	assert!(
		rendered.contains(message),
		"error for {source:?} does not contain {message:?}: {rendered}"
	);
	assert!(
		slices.iter().any(|s| s == slice),
		"error spans for {source:?} cover {slices:?}, not {slice:?}"
	);
}

// ------------------------------------------------------------------------
// Worked examples (V2_DESIGN §8 / §6 canonical queries).
// ------------------------------------------------------------------------

#[test]
fn single_node_bare_variable() {
	assert_eq!(
		render("MATCH (n:person) RETURN n"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 RETURN n AS n"
	);
}

#[test]
fn one_hop_edge_predicate_dotted_columns() {
	assert_eq!(
		render(
			"MATCH (a:person)-[k:knows]->(b:person) WHERE k.since > 2020 \
			 RETURN a.name AS a_name, b.name AS b_name"
		),
		"bindings: a:Node k:Edge b:Node\n\
		 MATCH (a:person)-[k:knows]->(b:person)\n\
		 \x20\x20WHERE [k] k.since != NONE AND k.since != NULL AND k.since > 2020\n\
		 RETURN a.name AS a_name, b.name AS b_name"
	);
}

#[test]
fn anonymous_edge_gets_hidden_binding() {
	assert_eq!(
		render("MATCH (a:person)-[:knows]->(b:person) RETURN a.name, b.name"),
		"bindings: a:Node __e0:Edge* b:Node\n\
		 MATCH (a:person)-[__e0:knows]->(b:person)\n\
		 RETURN a.name AS a.name, b.name AS b.name"
	);
}

#[test]
fn anonymous_far_node_gets_hidden_binding() {
	assert_eq!(
		render("MATCH (a:person)-[k:knows]->(:person) RETURN k"),
		"bindings: a:Node k:Edge __v0:Node*\n\
		 MATCH (a:person)-[k:knows]->(__v0:person)\n\
		 RETURN k AS k"
	);
}

#[test]
fn path_var_quantified_range() {
	assert_eq!(
		render("MATCH p = (a:person)-[:knows]->{1,3}(b:person) RETURN p, b ORDER BY a.age"),
		"bindings: a:Node __e0:EdgeGroup* b:Node p:Path\n\
		 MATCH p = (a:person)-[__e0:knows]->{1,3}(b:person)\n\
		 RETURN p AS p, b AS b\n\
		 ORDER BY a.age ASC"
	);
}

// ------------------------------------------------------------------------
// Multi-pattern, sequential MATCH, repeated-node-variable joins (PR-B).
//
// The lowering only declares the shared bindings (a reused node variable is one
// binding) and the conjunct deps; it does not decide the join. A reused node
// variable keeps the id of its first declaration, so it appears once in the
// binding table and twice in the pattern rendering.
// ------------------------------------------------------------------------

#[test]
fn multi_pattern_shared_node_is_one_binding() {
	// `b` is declared once (in the first pattern) and reused in the second, so
	// it is a single Node binding — the planner's join key on `b`.
	assert_eq!(
		render("MATCH (a:person)-[:x]->(b), (c:person)-[:y]->(b) RETURN a, c"),
		"bindings: a:Node __e0:Edge* b:Node c:Node __e1:Edge*\n\
		 MATCH (a:person)-[__e0:x]->(b), (c:person)-[__e1:y]->(b)\n\
		 RETURN a AS a, c AS c"
	);
}

#[test]
fn multi_pattern_cartesian_no_shared_variable() {
	// Two fully-disjoint patterns: no shared binding ⇒ the planner takes the
	// cartesian product. The lowering just emits both patterns.
	assert_eq!(
		render("MATCH (a:person), (b:city) RETURN a, b"),
		"bindings: a:Node b:Node\n\
		 MATCH (a:person), (b:city)\n\
		 RETURN a AS a, b AS b"
	);
}

#[test]
fn multi_pattern_three_patterns_chained_on_shared_nodes() {
	assert_eq!(
		render(
			"MATCH (a:person)-[:knows]->(b:person), (b)-[:knows]->(c:person), \
			 (c)-[:knows]->(d:person) RETURN a, d"
		),
		"bindings: a:Node __e0:Edge* b:Node __e1:Edge* c:Node __e2:Edge* d:Node\n\
		 MATCH (a:person)-[__e0:knows]->(b:person), (b)-[__e1:knows]->(c:person), \
		 (c)-[__e2:knows]->(d:person)\n\
		 RETURN a AS a, d AS d"
	);
}

#[test]
fn multi_hop_pattern_keeps_chain_whole() {
	// v1/PR-A rejected multi-hop; the chain now lowers whole (the planner chains
	// the Expands).
	assert_eq!(
		render("MATCH (a:person)-[:knows]->(b:person)-[:knows]->(c:person) RETURN a, c"),
		"bindings: a:Node __e0:Edge* b:Node __e1:Edge* c:Node\n\
		 MATCH (a:person)-[__e0:knows]->(b:person)-[__e1:knows]->(c:person)\n\
		 RETURN a AS a, c AS c"
	);
}

#[test]
fn sequential_match_clauses_share_a_node() {
	// Two MATCH statements joined on `b` (R1: identical to a comma pattern).
	// Each clause is its own line; `b` is one Node binding shared between them.
	assert_eq!(
		render(
			"MATCH (a:person)-[k:knows]->(b:person) MATCH (b:person)-[k2:likes]->(c:person) \
			 RETURN a, c"
		),
		"bindings: a:Node k:Edge b:Node k2:Edge c:Node\n\
		 MATCH (a:person)-[k:knows]->(b:person)\n\
		 MATCH (b:person)-[k2:likes]->(c:person)\n\
		 RETURN a AS a, c AS c"
	);
}

#[test]
fn sequential_match_clauses_cartesian() {
	// Sequential MATCH with no shared variable: cartesian product (R1).
	assert_eq!(
		render("MATCH (a:person) MATCH (b:city) RETURN a, b"),
		"bindings: a:Node b:Node\n\
		 MATCH (a:person)\n\
		 MATCH (b:city)\n\
		 RETURN a AS a, b AS b"
	);
}

#[test]
fn repeated_node_variable_across_patterns_is_a_join_key() {
	// A repeated node variable is no longer an error; it resolves to the SAME
	// binding, which the planner equi-joins on.
	assert_eq!(
		render("MATCH (a:person)-[:knows]->(b:person), (a)-[:likes]->(c:person) RETURN b, c"),
		"bindings: a:Node __e0:Edge* b:Node __e1:Edge* c:Node\n\
		 MATCH (a:person)-[__e0:knows]->(b:person), (a)-[__e1:likes]->(c:person)\n\
		 RETURN b AS b, c AS c"
	);
}

#[test]
fn repeated_node_variable_within_one_pattern_emits_id_equality() {
	// A node variable repeated WITHIN a single chain (the self-loop
	// `(a)-[:knows]->(a)`) cannot be a shared binding — there is no join to
	// materialise the equality, and reusing the id would let the target overwrite
	// the source on the row. The lowering rewrites the repeat to a fresh hidden
	// node `__v0` and emits the implied `a.id = __v0.id` equality conjunct
	// (V2_DESIGN §2). Without this, the self-loop over-returns every edge.
	assert_eq!(
		render("MATCH (a:person)-[:knows]->(a) RETURN a.name"),
		"bindings: a:Node __e0:Edge* __v0:Node*\n\
		 MATCH (a:person)-[__e0:knows]->(__v0)\n\
		 \x20\x20WHERE [a __v0] a.id = __v0.id\n\
		 RETURN a.name AS a.name"
	);
}

#[test]
fn repeated_node_variable_thrice_within_one_pattern_chains_equalities() {
	// Three occurrences of `a` on one chain ⇒ two fresh hidden nodes and two
	// equalities, transitively forcing all three equal.
	assert_eq!(
		render("MATCH (a:person)-[:knows]->(a)-[:knows]->(a) RETURN a.name"),
		"bindings: a:Node __e0:Edge* __v0:Node* __e1:Edge* __v1:Node*\n\
		 MATCH (a:person)-[__e0:knows]->(__v0)-[__e1:knows]->(__v1)\n\
		 \x20\x20WHERE [a __v0] a.id = __v0.id\n\
		 \x20\x20WHERE [a __v1] a.id = __v1.id\n\
		 RETURN a.name AS a.name"
	);
}

#[test]
fn cross_pattern_predicate_deps_span_both_patterns() {
	// A WHERE conjunct referencing bindings from two patterns lands as one
	// predicate whose deps span both (the planner filters it post-join).
	assert_eq!(
		render(
			"MATCH (a:person)-[:x]->(b:person), (c:person)-[:y]->(b) WHERE a.age > c.age \
			 RETURN a, c"
		),
		"bindings: a:Node __e0:Edge* b:Node c:Node __e1:Edge*\n\
		 MATCH (a:person)-[__e0:x]->(b:person), (c:person)-[__e1:y]->(b)\n\
		 \x20\x20WHERE [a c] a.age != NONE AND a.age != NULL AND c.age != NONE AND c.age != NULL \
		 AND a.age > c.age\n\
		 RETURN a AS a, c AS c"
	);
}

#[test]
fn sequential_clause_predicate_may_reference_an_earlier_clause() {
	// The second clause's WHERE references `a` (bound by the first clause): it
	// is owned by the second clause but its deps span the first. The lowering
	// just records the deps; the planner places it post-join.
	assert_eq!(
		render(
			"MATCH (a:person)-[:knows]->(b:person) MATCH (b:person)-[:likes]->(c:person) \
			 WHERE a.age > c.age RETURN a, c"
		),
		"bindings: a:Node __e0:Edge* b:Node __e1:Edge* c:Node\n\
		 MATCH (a:person)-[__e0:knows]->(b:person)\n\
		 MATCH (b:person)-[__e1:likes]->(c:person)\n\
		 \x20\x20WHERE [a c] a.age != NONE AND a.age != NULL AND c.age != NONE AND c.age != NULL \
		 AND a.age > c.age\n\
		 RETURN a AS a, c AS c"
	);
}

// ------------------------------------------------------------------------
// OPTIONAL MATCH (R3): left-outer clauses. The lowering tags each clause with
// `optional` and a per-block `optional_group` (rendered `OPTIONAL#<id>`); the
// planner left-joins clauses sharing one id as ONE all-or-nothing unit, and
// chains distinct ids left-to-right. Clauses sharing an id are one block; a
// plain `OPTIONAL MATCH` is a block of one. Inside-optional predicates attach
// to the optional clause (pre-null); a later clause's predicate that merely
// references an optional binding attaches to that later clause (post-null).
// ------------------------------------------------------------------------

#[test]
fn optional_clause_is_tagged_optional() {
	// Worked tree (iii): a plain `OPTIONAL MATCH` after a mandatory clause. The
	// optional clause is tagged `optional` (rendered `OPTIONAL#0`); `b`/`k` are
	// optional-bound. The leading optional pattern reuses the bound `a`.
	assert_eq!(
		render("MATCH (a:person) OPTIONAL MATCH (a)-[k:knows]->(b:person) RETURN a.name, b.name"),
		"bindings: a:Node k:Edge b:Node\n\
		 MATCH (a:person)\n\
		 OPTIONAL#0 MATCH (a)-[k:knows]->(b:person)\n\
		 RETURN a.name AS a.name, b.name AS b.name"
	);
}

#[test]
fn optional_clause_expands_unlabeled_edge_off_bound_var() {
	// An optional clause whose only anchor is the bound `a` (no labeled element):
	// realisable as a bound-variable expansion because the clause is optional (a
	// mandatory leading clause with this shape would be rejected as unanchorable).
	assert_eq!(
		render("MATCH (a:person) OPTIONAL MATCH (a)-[k]->(b:person) RETURN a, b"),
		"bindings: a:Node k:Edge b:Node\n\
		 MATCH (a:person)\n\
		 OPTIONAL#0 MATCH (a)-[k]->(b:person)\n\
		 RETURN a AS a, b AS b"
	);
}

#[test]
fn chained_optionals_get_distinct_block_ids() {
	// Two plain `OPTIONAL MATCH` clauses chain left-to-right: each is its own
	// block (distinct ids #0, #1), so the planner left-joins them in sequence.
	assert_eq!(
		render(
			"MATCH (a:person) OPTIONAL MATCH (a)-[:knows]->(b:person) \
			 OPTIONAL MATCH (a)-[:likes]->(c:person) RETURN a"
		),
		"bindings: a:Node __e0:Edge* b:Node __e1:Edge* c:Node\n\
		 MATCH (a:person)\n\
		 OPTIONAL#0 MATCH (a)-[__e0:knows]->(b:person)\n\
		 OPTIONAL#1 MATCH (a)-[__e1:likes]->(c:person)\n\
		 RETURN a AS a"
	);
}

#[test]
fn optional_brace_block_shares_one_block_id() {
	// A brace block `OPTIONAL { MATCH …; MATCH … }` is ONE all-or-nothing unit:
	// both inner clauses share `OPTIONAL#0`, so the planner left-joins the WHOLE
	// block subplan as a unit (not per inner clause).
	assert_eq!(
		render(
			"MATCH (a:person) OPTIONAL { MATCH (a)-[:knows]->(b:person) \
			 MATCH (b)-[:knows]->(c:person) } RETURN a"
		),
		"bindings: a:Node __e0:Edge* b:Node __e1:Edge* c:Node\n\
		 MATCH (a:person)\n\
		 OPTIONAL#0 MATCH (a)-[__e0:knows]->(b:person)\n\
		 OPTIONAL#0 MATCH (b)-[__e1:knows]->(c:person)\n\
		 RETURN a AS a"
	);
}

#[test]
fn optional_paren_block_is_a_block_of_one() {
	// The paren form is the brace form's twin; a single inner clause is a block
	// of one (`OPTIONAL#0`), identical in the IR to a plain `OPTIONAL MATCH`.
	assert_eq!(
		render("MATCH (a:person) OPTIONAL ( MATCH (a)-[:knows]->(b:person) ) RETURN a"),
		"bindings: a:Node __e0:Edge* b:Node\n\
		 MATCH (a:person)\n\
		 OPTIONAL#0 MATCH (a)-[__e0:knows]->(b:person)\n\
		 RETURN a AS a"
	);
}

#[test]
fn nested_optional_block_ids_are_innermost() {
	// A nested `OPTIONAL` inside a block: each block mints its own id in textual
	// order, and a clause carries the INNERMOST block it sits directly within.
	// `b` (block #0) is one unit relative to `a`; `c` (block #1) is one unit
	// relative to `a`/`b`. Distinct ids ⇒ the planner chains the two left-joins.
	assert_eq!(
		render(
			"MATCH (a:person) OPTIONAL { MATCH (a)-[:knows]->(b:person) \
			 OPTIONAL MATCH (b)-[:likes]->(c:person) } RETURN a"
		),
		"bindings: a:Node __e0:Edge* b:Node __e1:Edge* c:Node\n\
		 MATCH (a:person)\n\
		 OPTIONAL#0 MATCH (a)-[__e0:knows]->(b:person)\n\
		 OPTIONAL#1 MATCH (b)-[__e1:likes]->(c:person)\n\
		 RETURN a AS a"
	);
}

#[test]
fn optional_clause_followed_by_mandatory_clause() {
	// A mandatory clause after an optional one stays mandatory (no block id); the
	// optional clause keeps its own block. Pins that `optional`/`optional_group`
	// are per-clause, not sticky.
	assert_eq!(
		render(
			"MATCH (a:person) OPTIONAL MATCH (a)-[:knows]->(b:person) \
			 MATCH (a)-[:likes]->(c:person) RETURN a"
		),
		"bindings: a:Node __e0:Edge* b:Node __e1:Edge* c:Node\n\
		 MATCH (a:person)\n\
		 OPTIONAL#0 MATCH (a)-[__e0:knows]->(b:person)\n\
		 MATCH (a)-[__e1:likes]->(c:person)\n\
		 RETURN a AS a"
	);
}

// --- OPTIONAL × guards: the nullable() amendment (V2_DESIGN §8). ---

#[test]
fn inside_optional_predicate_guards_optional_binding() {
	// A predicate WRITTEN inside the optional (pre-null, R3) is owned by the
	// optional clause. `b.age` is a property on the optional `b`, guarded as
	// before — the row is excluded when `b` missed (`b.age` → NONE).
	assert_eq!(
		render(
			"MATCH (a:person) OPTIONAL MATCH (a)-[:knows]->(b:person) WHERE b.age > 18 RETURN a"
		),
		"bindings: a:Node __e0:Edge* b:Node\n\
		 MATCH (a:person)\n\
		 OPTIONAL#0 MATCH (a)-[__e0:knows]->(b:person)\n\
		 \x20\x20WHERE [b] b.age != NONE AND b.age != NULL AND b.age > 18\n\
		 RETURN a AS a"
	);
}

#[test]
fn bare_optional_variable_is_nullable_in_ordering_comparison() {
	// THE amendment, made visible: a bare optional-bound variable `b` is now a
	// guard atom in an ordering comparison — `b != NONE AND b != NULL` — which a
	// mandatory bare variable (`a`) never gets. Without the amendment `b > a`
	// would carry no guard and a pre-null `b` (Null) would sort below `a` and
	// wrongly survive.
	assert_eq!(
		render("MATCH (a:person) OPTIONAL MATCH (a)-[:knows]->(b:person) WHERE b > a RETURN a"),
		"bindings: a:Node __e0:Edge* b:Node\n\
		 MATCH (a:person)\n\
		 OPTIONAL#0 MATCH (a)-[__e0:knows]->(b:person)\n\
		 \x20\x20WHERE [a b] b != NONE AND b != NULL AND b > a\n\
		 RETURN a AS a"
	);
}

#[test]
fn both_optional_bare_equality_is_guarded() {
	// Both sides optional bare variables (chained optionals): `=` deviates only
	// when BOTH sides can be null (`NULL = NULL` is TRUE in SurrealQL but UNKNOWN
	// in GQL), and the amendment now makes both sides nullable, so the guard pair
	// is emitted and a double-miss row is excluded.
	assert_eq!(
		render(
			"MATCH (a:person) OPTIONAL MATCH (a)-[:knows]->(b:person) \
			 OPTIONAL MATCH (a)-[:likes]->(c:person) WHERE b = c RETURN a"
		),
		"bindings: a:Node __e0:Edge* b:Node __e1:Edge* c:Node\n\
		 MATCH (a:person)\n\
		 OPTIONAL#0 MATCH (a)-[__e0:knows]->(b:person)\n\
		 OPTIONAL#1 MATCH (a)-[__e1:likes]->(c:person)\n\
		 \x20\x20WHERE [b c] b != NONE AND b != NULL AND c != NONE AND c != NULL AND b = c\n\
		 RETURN a AS a"
	);
}

#[test]
fn one_sided_optional_equality_needs_no_guard() {
	// `b = c` with `b` optional but `c` mandatory: a one-sided null already
	// compares unequal and excludes the row, so `=` needs no guard (E8c). The
	// amendment makes `nullable(b)` true, but the `Eq` rule still only guards when
	// BOTH sides are nullable — so this correctly stays guard-free. The predicate
	// is owned by the mandatory clause that introduces `c` (post-null, R3).
	assert_eq!(
		render(
			"MATCH (a:person) OPTIONAL MATCH (a)-[:knows]->(b:person) \
			 MATCH (c:person) WHERE b = c RETURN a"
		),
		"bindings: a:Node __e0:Edge* b:Node c:Node\n\
		 MATCH (a:person)\n\
		 OPTIONAL#0 MATCH (a)-[__e0:knows]->(b:person)\n\
		 MATCH (c:person)\n\
		 \x20\x20WHERE [b c] b = c\n\
		 RETURN a AS a"
	);
}

#[test]
fn optional_binding_is_null_test() {
	// `b IS NULL` is two-valued and needs no guard; on an optional miss `b` is
	// `Value::Null`, so `b = NULL OR b = NONE` is TRUE — the unmatched rows are
	// exactly the ones it selects (R3 / the optional-miss-is-Null rule).
	assert_eq!(
		render("MATCH (a:person) OPTIONAL MATCH (a)-[:knows]->(b:person) WHERE b IS NULL RETURN a"),
		"bindings: a:Node __e0:Edge* b:Node\n\
		 MATCH (a:person)\n\
		 OPTIONAL#0 MATCH (a)-[__e0:knows]->(b:person)\n\
		 \x20\x20WHERE [b] b = NULL OR b = NONE\n\
		 RETURN a AS a"
	);
}

#[test]
fn later_clause_predicate_referencing_optional_binding_is_post_null() {
	// A predicate in a LATER mandatory clause that references an optional binding
	// is owned by that later clause (post-null, R3). The deps span the optional
	// `b` and the mandatory `c`; the planner places it post-join. The property
	// `b.age` (optional `b`) keeps its guard, so a missed `b` excludes the row.
	assert_eq!(
		render(
			"MATCH (a:person) OPTIONAL MATCH (a)-[:knows]->(b:person) \
			 MATCH (c:person) WHERE c.age > b.age RETURN c"
		),
		"bindings: a:Node __e0:Edge* b:Node c:Node\n\
		 MATCH (a:person)\n\
		 OPTIONAL#0 MATCH (a)-[__e0:knows]->(b:person)\n\
		 MATCH (c:person)\n\
		 \x20\x20WHERE [b c] c.age != NONE AND c.age != NULL AND b.age != NONE AND b.age != NULL \
		 AND c.age > b.age\n\
		 RETURN c AS c"
	);
}

// ------------------------------------------------------------------------
// Directions and labels.
// ------------------------------------------------------------------------

#[test]
fn left_direction_uses_in() {
	assert_eq!(
		render("MATCH (a:person)<-[k:knows]-(b:person) RETURN b.name"),
		"bindings: a:Node k:Edge b:Node\n\
		 MATCH (a:person)<-[k:knows]-(b:person)\n\
		 RETURN b.name AS b.name"
	);
}

#[test]
fn unlabeled_edge() {
	assert_eq!(
		render("MATCH (a:person)-[k]->(b) RETURN k"),
		"bindings: a:Node k:Edge b:Node\n\
		 MATCH (a:person)-[k]->(b)\n\
		 RETURN k AS k"
	);
}

#[test]
fn labeled_far_node_only() {
	assert_eq!(
		render("MATCH (a:person)-[k]->(b:person) RETURN k"),
		"bindings: a:Node k:Edge b:Node\n\
		 MATCH (a:person)-[k]->(b:person)\n\
		 RETURN k AS k"
	);
}

// ------------------------------------------------------------------------
// Quantifiers: the full set is legal (R6).
// ------------------------------------------------------------------------

#[test]
fn quantifier_fixed() {
	assert_eq!(
		render("MATCH (a:person)-[:knows]->{2}(b:person) RETURN b"),
		"bindings: a:Node __e0:EdgeGroup* b:Node\n\
		 MATCH (a:person)-[__e0:knows]->{2,2}(b:person)\n\
		 RETURN b AS b"
	);
}

#[test]
fn quantifier_range() {
	assert_eq!(
		render("MATCH (a:person)-[:knows]->{1,3}(b:person) RETURN b"),
		"bindings: a:Node __e0:EdgeGroup* b:Node\n\
		 MATCH (a:person)-[__e0:knows]->{1,3}(b:person)\n\
		 RETURN b AS b"
	);
}

#[test]
fn quantifier_star_is_zero_to_unbounded() {
	assert_eq!(
		render("MATCH (a:person)-[:knows]->*(b:person) RETURN b"),
		"bindings: a:Node __e0:EdgeGroup* b:Node\n\
		 MATCH (a:person)-[__e0:knows]->{0,}(b:person)\n\
		 RETURN b AS b"
	);
}

#[test]
fn quantifier_plus_is_one_to_unbounded() {
	assert_eq!(
		render("MATCH (a:person)-[:knows]->+(b:person) RETURN b"),
		"bindings: a:Node __e0:EdgeGroup* b:Node\n\
		 MATCH (a:person)-[__e0:knows]->{1,}(b:person)\n\
		 RETURN b AS b"
	);
}

#[test]
fn quantifier_question_is_zero_to_one() {
	assert_eq!(
		render("MATCH (a:person)-[:knows]->?(b:person) RETURN b"),
		"bindings: a:Node __e0:EdgeGroup* b:Node\n\
		 MATCH (a:person)-[__e0:knows]->{0,1}(b:person)\n\
		 RETURN b AS b"
	);
}

#[test]
fn quantifier_min_two_is_legal() {
	// v1 rejected `{2}`/`{2,3}`; v2's per-path traversal makes them legal.
	assert_eq!(
		render("MATCH (a:person)-[:knows]->{2,4}(b:person) RETURN b"),
		"bindings: a:Node __e0:EdgeGroup* b:Node\n\
		 MATCH (a:person)-[__e0:knows]->{2,4}(b:person)\n\
		 RETURN b AS b"
	);
}

#[test]
fn quantifier_zero_min_is_legal() {
	// v1 rejected `{0,3}`; v2 emits the zero-length path (R6).
	assert_eq!(
		render("MATCH (a:person)-[:knows]->{0,3}(b:person) RETURN b"),
		"bindings: a:Node __e0:EdgeGroup* b:Node\n\
		 MATCH (a:person)-[__e0:knows]->{0,3}(b:person)\n\
		 RETURN b AS b"
	);
}

#[test]
fn quantifier_unbounded_lower_bound_is_legal() {
	// v1 rejected `{2,}`; v2 terminates the unbounded form via edge-uniqueness.
	assert_eq!(
		render("MATCH (a:person)-[:knows]->{2,}(b:person) RETURN b"),
		"bindings: a:Node __e0:EdgeGroup* b:Node\n\
		 MATCH (a:person)-[__e0:knows]->{2,}(b:person)\n\
		 RETURN b AS b"
	);
}

#[test]
fn quantifier_missing_min_defaults_to_zero() {
	assert_eq!(
		render("MATCH (a:person)-[:knows]->{,3}(b:person) RETURN b"),
		"bindings: a:Node __e0:EdgeGroup* b:Node\n\
		 MATCH (a:person)-[__e0:knows]->{0,3}(b:person)\n\
		 RETURN b AS b"
	);
}

#[test]
fn quantified_edge_with_variable_is_a_group() {
	// v1 rejected an edge variable under a quantifier; v2 binds an edge group (R4).
	assert_eq!(
		render("MATCH (a:person)-[k:knows]->{1,3}(b:person) RETURN k"),
		"bindings: a:Node k:EdgeGroup b:Node\n\
		 MATCH (a:person)-[k:knows]->{1,3}(b:person)\n\
		 RETURN k AS k"
	);
}

#[test]
fn quantified_left_direction() {
	assert_eq!(
		render("MATCH (a:person)<-[:knows]-{1,2}(b:person) RETURN b"),
		"bindings: a:Node __e0:EdgeGroup* b:Node\n\
		 MATCH (a:person)<-[__e0:knows]-{1,2}(b:person)\n\
		 RETURN b AS b"
	);
}

// ------------------------------------------------------------------------
// Predicate placement is the planner's job: the lowering emits flat conjuncts
// with their dependency sets (no anchor/edge/post classification).
// ------------------------------------------------------------------------

#[test]
fn anchor_only_conjunct_deps_anchor() {
	assert_eq!(
		render("MATCH (a:person)-[k:knows]->(b:person) WHERE a.age > 18 RETURN k"),
		"bindings: a:Node k:Edge b:Node\n\
		 MATCH (a:person)-[k:knows]->(b:person)\n\
		 \x20\x20WHERE [a] a.age != NONE AND a.age != NULL AND a.age > 18\n\
		 RETURN k AS k"
	);
}

#[test]
fn cross_variable_conjunct_deps_both() {
	assert_eq!(
		render("MATCH (a:person)-[k:knows]->(b:person) WHERE a.age > b.age RETURN k"),
		"bindings: a:Node k:Edge b:Node\n\
		 MATCH (a:person)-[k:knows]->(b:person)\n\
		 \x20\x20WHERE [a b] a.age != NONE AND a.age != NULL AND b.age != NONE AND b.age != NULL \
		 AND a.age > b.age\n\
		 RETURN k AS k"
	);
}

#[test]
fn bare_whole_record_reference_deps_both() {
	assert_eq!(
		render("MATCH (a:person)-[k:knows]->(b:person) WHERE b = a RETURN k"),
		"bindings: a:Node k:Edge b:Node\n\
		 MATCH (a:person)-[k:knows]->(b:person)\n\
		 \x20\x20WHERE [a b] b = a\n\
		 RETURN k AS k"
	);
}

#[test]
fn and_splits_into_separate_conjuncts() {
	assert_eq!(
		render(
			"MATCH (a:person)-[k:knows]->(b:person) WHERE a.age > 18 AND k.since > 2020 RETURN b"
		),
		"bindings: a:Node k:Edge b:Node\n\
		 MATCH (a:person)-[k:knows]->(b:person)\n\
		 \x20\x20WHERE [a] a.age != NONE AND a.age != NULL AND a.age > 18\n\
		 \x20\x20WHERE [k] k.since != NONE AND k.since != NULL AND k.since > 2020\n\
		 RETURN b AS b"
	);
}

#[test]
fn or_conjunct_is_not_split() {
	assert_eq!(
		render(
			"MATCH (a:person)-[k:knows]->(b:person) WHERE a.age > 60 OR k.since > 2020 RETURN k"
		),
		"bindings: a:Node k:Edge b:Node\n\
		 MATCH (a:person)-[k:knows]->(b:person)\n\
		 \x20\x20WHERE [a k] a.age != NONE AND a.age != NULL AND a.age > 60 OR k.since != NONE \
		 AND k.since != NULL AND k.since > 2020\n\
		 RETURN k AS k"
	);
}

#[test]
fn edge_property_map() {
	assert_eq!(
		render("MATCH (a:person)-[k:knows {since: 2020}]->(b:person) RETURN k"),
		"bindings: a:Node k:Edge b:Node\n\
		 MATCH (a:person)-[k:knows]->(b:person)\n\
		 \x20\x20WHERE [k] k.since = 2020\n\
		 RETURN k AS k"
	);
}

#[test]
fn far_node_property_map() {
	assert_eq!(
		render("MATCH (a:person)-[:knows]->(b:person {city: 'London'}) RETURN a"),
		"bindings: a:Node __e0:Edge* b:Node\n\
		 MATCH (a:person)-[__e0:knows]->(b:person)\n\
		 \x20\x20WHERE [b] b.city = 'London'\n\
		 RETURN a AS a"
	);
}

#[test]
fn anonymous_far_node_property_map_uses_hidden_binding() {
	assert_eq!(
		render("MATCH (a:person)-[:knows]->(:person {city: 'London'}) RETURN a"),
		"bindings: a:Node __e0:Edge* __v0:Node*\n\
		 MATCH (a:person)-[__e0:knows]->(__v0:person)\n\
		 \x20\x20WHERE [__v0] __v0.city = 'London'\n\
		 RETURN a AS a"
	);
}

#[test]
fn node_property_map_no_guard_for_literal() {
	assert_eq!(
		render("MATCH (n:person {city: 'London'}) RETURN n"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 \x20\x20WHERE [n] n.city = 'London'\n\
		 RETURN n AS n"
	);
}

#[test]
fn inline_node_where() {
	assert_eq!(
		render("MATCH (n:person WHERE n.age > 18) RETURN n"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 \x20\x20WHERE [n] n.age != NONE AND n.age != NULL AND n.age > 18\n\
		 RETURN n AS n"
	);
}

#[test]
fn inline_edge_where() {
	assert_eq!(
		render("MATCH (a:person)-[k:knows WHERE k.since > 2020]->(b:person) RETURN k"),
		"bindings: a:Node k:Edge b:Node\n\
		 MATCH (a:person)-[k:knows]->(b:person)\n\
		 \x20\x20WHERE [k] k.since != NONE AND k.since != NULL AND k.since > 2020\n\
		 RETURN k AS k"
	);
}

#[test]
fn inline_far_node_where_cross_variable() {
	assert_eq!(
		render("MATCH (a:person)-[:knows]->(b:person WHERE b.age < a.age) RETURN a"),
		"bindings: a:Node __e0:Edge* b:Node\n\
		 MATCH (a:person)-[__e0:knows]->(b:person)\n\
		 \x20\x20WHERE [a b] b.age != NONE AND b.age != NULL AND a.age != NONE AND a.age != NULL \
		 AND b.age < a.age\n\
		 RETURN a AS a"
	);
}

#[test]
fn predicate_source_merge_order() {
	// Explicit WHERE first, then inline element WHEREs, then property maps.
	assert_eq!(
		render("MATCH (n:person {city: 'London'}) WHERE n.age > 18 RETURN n"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 \x20\x20WHERE [n] n.age != NONE AND n.age != NULL AND n.age > 18\n\
		 \x20\x20WHERE [n] n.city = 'London'\n\
		 RETURN n AS n"
	);
}

#[test]
fn bare_anchor_equality_uniform_addressing() {
	assert_eq!(
		render("MATCH (n:person) WHERE n = $x RETURN n"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 \x20\x20WHERE [n] n = $x\n\
		 RETURN n AS n"
	);
}

// ------------------------------------------------------------------------
// Three-valued logic guards and NNF (kept verbatim from v1).
// ------------------------------------------------------------------------

#[test]
fn ordering_guard_param_operand() {
	assert_eq!(
		render("MATCH (n:person) WHERE n.age = $min RETURN n"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 \x20\x20WHERE [n] n.age != NONE AND n.age != NULL AND $min != NONE AND $min != NULL \
		 AND n.age = $min\n\
		 RETURN n AS n"
	);
}

#[test]
fn ordering_guard_both_params() {
	assert_eq!(
		render("MATCH (n:person) WHERE $a < $b RETURN n"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 \x20\x20WHERE [] $a != NONE AND $a != NULL AND $b != NONE AND $b != NULL AND $a < $b\n\
		 RETURN n AS n"
	);
}

#[test]
fn ordering_guard_deduplicates_atoms() {
	assert_eq!(
		render("MATCH (n:person) WHERE n.age > n.age RETURN n"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 \x20\x20WHERE [n] n.age != NONE AND n.age != NULL AND n.age > n.age\n\
		 RETURN n AS n"
	);
}

#[test]
fn ordering_guard_arithmetic_operand() {
	assert_eq!(
		render("MATCH (n:person) WHERE n.age + 1 > 18 RETURN n"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 \x20\x20WHERE [n] n.age != NONE AND n.age != NULL AND n.age + 1 > 18\n\
		 RETURN n AS n"
	);
}

#[test]
fn equality_with_literal_needs_no_guard() {
	assert_eq!(
		render("MATCH (n:person) WHERE n.name = 'A' RETURN n"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 \x20\x20WHERE [n] n.name = 'A'\n\
		 RETURN n AS n"
	);
}

#[test]
fn equality_with_null_literal_is_guarded() {
	assert_eq!(
		render("MATCH (n:person) WHERE n.age = null RETURN n"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 \x20\x20WHERE [n] n.age != NONE AND n.age != NULL AND n.age = NULL\n\
		 RETURN n AS n"
	);
}

#[test]
fn inequality_guards_one_sided() {
	assert_eq!(
		render("MATCH (n:person) WHERE n.name <> 'A' RETURN n"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 \x20\x20WHERE [n] n.name != NONE AND n.name != NULL AND n.name != 'A'\n\
		 RETURN n AS n"
	);
}

#[test]
fn nnf_pushes_not_through_or() {
	assert_eq!(
		render("MATCH (n:person) WHERE NOT (n.age > 18 OR n.name = 'A') RETURN n"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 \x20\x20WHERE [n] n.age != NONE AND n.age != NULL AND n.age <= 18\n\
		 \x20\x20WHERE [n] n.name != NONE AND n.name != NULL AND n.name != 'A'\n\
		 RETURN n AS n"
	);
}

#[test]
fn nnf_pushes_not_through_and() {
	assert_eq!(
		render("MATCH (n:person) WHERE NOT (n.age > 18 AND n.flag) RETURN n"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 \x20\x20WHERE [n] n.age != NONE AND n.age != NULL AND n.age <= 18 OR n.flag = false\n\
		 RETURN n AS n"
	);
}

#[test]
fn nnf_double_negation() {
	assert_eq!(
		render("MATCH (n:person) WHERE NOT NOT n.flag RETURN n"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 \x20\x20WHERE [n] n.flag = true\n\
		 RETURN n AS n"
	);
}

#[test]
fn nnf_not_complements_comparison() {
	assert_eq!(
		render("MATCH (n:person) WHERE NOT n.age > 18 RETURN n"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 \x20\x20WHERE [n] n.age != NONE AND n.age != NULL AND n.age <= 18\n\
		 RETURN n AS n"
	);
}

#[test]
fn bare_nullable_boolean_tests_true() {
	assert_eq!(
		render("MATCH (n:person) WHERE n.flag RETURN n"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 \x20\x20WHERE [n] n.flag = true\n\
		 RETURN n AS n"
	);
}

#[test]
fn bare_param_predicate_tests_true() {
	assert_eq!(
		render("MATCH (n:person) WHERE $flag RETURN n"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 \x20\x20WHERE [] $flag = true\n\
		 RETURN n AS n"
	);
}

#[test]
fn boolean_literal_predicate() {
	assert_eq!(
		render("MATCH (n:person) WHERE true RETURN n"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 \x20\x20WHERE [] true\n\
		 RETURN n AS n"
	);
	assert_eq!(
		render("MATCH (n:person) WHERE NOT true RETURN n"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 \x20\x20WHERE [] false\n\
		 RETURN n AS n"
	);
}

#[test]
fn is_null_test() {
	assert_eq!(
		render("MATCH (n:person) WHERE n.age IS NULL RETURN n"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 \x20\x20WHERE [n] n.age = NULL OR n.age = NONE\n\
		 RETURN n AS n"
	);
}

#[test]
fn is_not_null_test() {
	assert_eq!(
		render("MATCH (n:person) WHERE n.age IS NOT NULL RETURN n"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 \x20\x20WHERE [n] n.age != NULL AND n.age != NONE\n\
		 RETURN n AS n"
	);
}

#[test]
fn is_true_false_tests() {
	assert_eq!(
		render("MATCH (n:person) WHERE n.flag IS TRUE RETURN n"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 \x20\x20WHERE [n] n.flag = true\n\
		 RETURN n AS n"
	);
	assert_eq!(
		render("MATCH (n:person) WHERE n.flag IS NOT FALSE RETURN n"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 \x20\x20WHERE [n] n.flag != false\n\
		 RETURN n AS n"
	);
}

#[test]
fn is_unknown_tests() {
	assert_eq!(
		render("MATCH (n:person) WHERE n.flag IS UNKNOWN RETURN n"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 \x20\x20WHERE [n] n.flag = NULL OR n.flag = NONE\n\
		 RETURN n AS n"
	);
}

#[test]
fn not_negates_truth_test() {
	assert_eq!(
		render("MATCH (n:person) WHERE NOT (n.flag IS TRUE) RETURN n"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 \x20\x20WHERE [n] n.flag != true\n\
		 RETURN n AS n"
	);
}

#[test]
fn guards_for_cross_variable_comparison() {
	// The edge-scope hazard from v1 (`out.age < $parent.age` unguarded) is now
	// uniform binding addressing, still guarded.
	assert_eq!(
		render("MATCH (a:person)-[:knows]->(b:person) WHERE b.age < a.age RETURN a"),
		"bindings: a:Node __e0:Edge* b:Node\n\
		 MATCH (a:person)-[__e0:knows]->(b:person)\n\
		 \x20\x20WHERE [a b] b.age != NONE AND b.age != NULL AND a.age != NONE AND a.age != NULL \
		 AND b.age < a.age\n\
		 RETURN a AS a"
	);
}

// ------------------------------------------------------------------------
// RETURN, RETURN *, columns, DISTINCT.
// ------------------------------------------------------------------------

#[test]
fn return_star_single_node() {
	assert_eq!(
		render("MATCH (n:person) RETURN *"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 RETURN n AS n"
	);
}

#[test]
fn return_star_expands_alphabetically() {
	assert_eq!(
		render("MATCH (b:person)-[a:knows]->(c:person) RETURN *"),
		"bindings: b:Node a:Edge c:Node\n\
		 MATCH (b:person)-[a:knows]->(c:person)\n\
		 RETURN a AS a, b AS b, c AS c"
	);
}

#[test]
fn return_star_skips_anonymous_elements() {
	assert_eq!(
		render("MATCH (a:person)-[:knows]->(b:person) RETURN *"),
		"bindings: a:Node __e0:Edge* b:Node\n\
		 MATCH (a:person)-[__e0:knows]->(b:person)\n\
		 RETURN a AS a, b AS b"
	);
}

#[test]
fn return_star_includes_group_and_path_vars() {
	assert_eq!(
		render("MATCH p = (a:person)-[k:knows]->{1,2}(b:person) RETURN *"),
		"bindings: a:Node k:EdgeGroup b:Node p:Path\n\
		 MATCH p = (a:person)-[k:knows]->{1,2}(b:person)\n\
		 RETURN a AS a, b AS b, k AS k, p AS p"
	);
}

#[test]
fn concat_lowers_to_add() {
	assert_eq!(
		render("MATCH (n:person) RETURN n.name || 'x' AS y"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 RETURN n.name + 'x' AS y"
	);
}

#[test]
fn arithmetic_and_sign_operators() {
	assert_eq!(
		render("MATCH (n:person) RETURN n.age * 2 - 1 AS x, -n.age AS y"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 RETURN n.age * 2 - 1 AS x, -n.age AS y"
	);
}

#[test]
fn list_and_map_literals() {
	assert_eq!(
		render("MATCH (n:person) RETURN [n.age, 1] AS lst, {a: n.age} AS mp"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 RETURN [n.age, 1] AS lst, { a: n.age } AS mp"
	);
}

#[test]
fn comparison_in_value_position_is_unguarded() {
	assert_eq!(
		render("MATCH (n:person) RETURN n.age > 18 AS adult"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 RETURN n.age > 18 AS adult"
	);
}

#[test]
fn distinct_flag_set() {
	assert_eq!(
		render("MATCH (n:person) RETURN DISTINCT n.name, n.age ORDER BY n.name DESC"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 RETURN DISTINCT n.name AS n.name, n.age AS n.age\n\
		 ORDER BY `n.name` DESC"
	);
}

#[test]
fn distinct_order_by_returned_column_allowed() {
	assert_eq!(
		render("MATCH (n:person) RETURN DISTINCT n.name ORDER BY n.name"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 RETURN DISTINCT n.name AS n.name\n\
		 ORDER BY `n.name` ASC"
	);
}

#[test]
fn nested_property_chains() {
	assert_eq!(
		render("MATCH (n:person) RETURN n.address.city AS city"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 RETURN n.address.city AS city"
	);
}

#[test]
fn quoted_identifiers_lower_like_plain_ones() {
	assert_eq!(
		render("MATCH (\"n\":person) RETURN `n`.name AS \"the name\""),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 RETURN n.name AS the name"
	);
}

// ------------------------------------------------------------------------
// ORDER BY: non-DISTINCT keys may reference any binding (R7).
// ------------------------------------------------------------------------

#[test]
fn order_by_alias() {
	// Non-DISTINCT: the key names the `name` column, so it sorts on that
	// column's underlying binding-row expression (`n.name`) pre-projection.
	assert_eq!(
		render("MATCH (n:person) RETURN n.name AS name ORDER BY name DESC"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 RETURN n.name AS name\n\
		 ORDER BY n.name DESC"
	);
}

#[test]
fn order_by_returned_column_lowers_full_expr() {
	// Non-DISTINCT: the key lowers as a full binding-row expression.
	assert_eq!(
		render("MATCH (a:person)-[:knows]->(b:person) RETURN a.name ORDER BY a.name"),
		"bindings: a:Node __e0:Edge* b:Node\n\
		 MATCH (a:person)-[__e0:knows]->(b:person)\n\
		 RETURN a.name AS a.name\n\
		 ORDER BY a.name ASC"
	);
}

#[test]
fn order_by_non_returned_binding_is_allowed() {
	// v1 rejected this; R7 relaxes it for non-DISTINCT queries.
	assert_eq!(
		render("MATCH (n:person) RETURN n.name ORDER BY n.age DESC"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 RETURN n.name AS n.name\n\
		 ORDER BY n.age DESC"
	);
}

#[test]
fn order_by_non_returned_post_split_binding_is_allowed() {
	assert_eq!(
		render("MATCH (a:person)-[k:knows]->(b:person) RETURN a ORDER BY k.since"),
		"bindings: a:Node k:Edge b:Node\n\
		 MATCH (a:person)-[k:knows]->(b:person)\n\
		 RETURN a AS a\n\
		 ORDER BY k.since ASC"
	);
}

#[test]
fn order_by_computed_expression() {
	assert_eq!(
		render("MATCH (n:person) RETURN n.age + 1 ORDER BY n.age + 1"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 RETURN n.age + 1 AS n.age + 1\n\
		 ORDER BY n.age + 1 ASC"
	);
}

// ------------------------------------------------------------------------
// SKIP / LIMIT.
// ------------------------------------------------------------------------

#[test]
fn skip_limit_parameters() {
	assert_eq!(
		render("MATCH (n:person) RETURN n SKIP $s LIMIT $l"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 RETURN n AS n\n\
		 SKIP $s\n\
		 LIMIT $l"
	);
}

#[test]
fn offset_synonym_for_skip() {
	assert_eq!(
		render("MATCH (n:person) RETURN n OFFSET 2 LIMIT 3"),
		"bindings: n:Node\n\
		 MATCH (n:person)\n\
		 RETURN n AS n\n\
		 SKIP 2\n\
		 LIMIT 3"
	);
}

// ------------------------------------------------------------------------
// Public API entry points.
// ------------------------------------------------------------------------

#[test]
fn parse_to_plan_with_settings_end_to_end() {
	let prepared =
		parse_to_plan_with_settings("MATCH (n:person) RETURN n", GqlParserSettings::default())
			.expect("should lower");
	assert_eq!(prepared.to_sql(), "MATCH (n:person) RETURN n AS n");
}

#[test]
fn prepared_query_to_sql_renders_via_match_plan() {
	let prepared = parse_to_plan_with_settings(
		"MATCH (a:person)-[k:knows]->(b:person) WHERE k.since > 2020 RETURN a.name AS x",
		GqlParserSettings::default(),
	)
	.expect("should lower");
	assert_eq!(
		prepared.to_sql(),
		"MATCH (a:person)-[k:knows]->(b:person) WHERE k.since != NONE AND k.since != NULL AND \
		 k.since > 2020 RETURN a.name AS x"
	);
}

#[test]
fn prepared_query_debug_renders_via_match_plan() {
	let prepared =
		parse_to_plan_with_settings("MATCH (n:person) RETURN n", GqlParserSettings::default())
			.expect("should lower");
	let debug = format!("{prepared:?}");
	assert!(debug.contains("MATCH (n:person) RETURN n AS n"), "unexpected debug: {debug}");
}

#[test]
fn parse_with_capabilities_renders_errors_like_surrealql() {
	use crate::cnf::CommonConfig;
	use crate::dbs::Capabilities;
	use crate::dbs::capabilities::Targets;
	let error = crate::gql::parse_with_capabilities(
		"MATCH (n:person) RETURN m",
		&Capabilities::all().with_experimental(Targets::All),
		&CommonConfig::default(),
	)
	.expect_err("should fail");
	let rendered = format!("{error}");
	assert!(rendered.contains("Parse error"), "unexpected error: {rendered}");
	assert!(rendered.contains("Unknown variable `m`"), "unexpected error: {rendered}");
}

#[test]
fn parse_with_capabilities_enforces_the_experimental_gate() {
	use crate::cnf::CommonConfig;
	use crate::dbs::Capabilities;
	let error = crate::gql::parse_with_capabilities(
		"MATCH (n:person) RETURN n",
		&Capabilities::all(),
		&CommonConfig::default(),
	)
	.expect_err("should fail");
	assert!(
		format!("{error}").contains("Experimental capability `gql` is not enabled"),
		"unexpected error: {error}"
	);
}

#[test]
fn deep_linear_chains_lower_without_overflowing() {
	// The lowering must process linear chains (binary spines, `NOT` prefixes,
	// property postfixes) without machine-stack recursion (the reblessive
	// guarantee). Such chains exceed the default parse-time expression budget,
	// so the deep cases raise the limit explicitly. The NOT chain collapses
	// under NNF to a bounded predicate and the property chain to a flat idiom,
	// so both render fine; a deep binary spine lowers to an equally deep tree
	// whose recursive rendering is a depth shared with syn-parsed SurrealQL, so
	// it is kept modest and only the lowering (not the rendering) is asserted.
	let deep = GqlParserSettings {
		expr_recursion_limit: 100_000,
		..Default::default()
	};

	let nots = format!("MATCH (n:person) WHERE {} n.flag RETURN n", "NOT ".repeat(50_001));
	let prepared =
		parse_to_plan_with_settings(&nots, deep.clone()).expect("NOT chain should lower");
	assert!(prepared.to_sql().contains("n.flag = false"), "unexpected: {}", prepared.to_sql());

	let props = format!("MATCH (n:person) RETURN n{} AS x", ".p".repeat(50_000));
	assert!(render(&props).contains("RETURN n.p.p.p"));

	// The binary spine lowers without machine-stack recursion; rendering a
	// pathologically deep tree (the recursive `sql::Expr` → `expr::Expr`
	// conversion and the `ToSql` walk) is a depth shared with syn-parsed
	// SurrealQL and out of scope here, so the spine depth is kept modest and
	// rendered only to confirm the shape.
	let spine = format!("MATCH (n:person) RETURN 1{} AS x", " + 1".repeat(100));
	let prepared = parse_to_plan_with_settings(&spine, deep).expect("binary spine should lower");
	assert!(prepared.to_sql().contains("RETURN 1 + 1 + "));
}

// ------------------------------------------------------------------------
// Rejections — PR-A survivors (kept verbatim from v1).
// ------------------------------------------------------------------------

#[test]
fn rejects_missing_match() {
	assert_rejects("RETURN 1", "A query without a MATCH clause is not supported yet", "RETURN 1");
}

#[test]
fn rejects_undirected_edges() {
	assert_rejects(
		"MATCH (a:person)~(b:person) RETURN a",
		"Undirected and multi-directional edge patterns are not supported yet",
		"~",
	);
}

#[test]
fn rejects_any_direction_edges() {
	assert_rejects(
		"MATCH (a:person)-(b:person) RETURN a",
		"Undirected and multi-directional edge patterns are not supported yet",
		"-",
	);
}

#[test]
fn rejects_left_or_right_edges() {
	assert_rejects(
		"MATCH (a:person)<->(b:person) RETURN a",
		"Undirected and multi-directional edge patterns are not supported yet",
		"<->",
	);
}

#[test]
fn rejects_left_or_undirected_edges() {
	assert_rejects(
		"MATCH (a:person)<~(b:person) RETURN a",
		"Undirected and multi-directional edge patterns are not supported yet",
		"<~",
	);
}

#[test]
fn rejects_undirected_or_right_edges() {
	assert_rejects(
		"MATCH (a:person)~>(b:person) RETURN a",
		"Undirected and multi-directional edge patterns are not supported yet",
		"~>",
	);
}

#[test]
fn rejects_full_form_undirected_edges() {
	assert_rejects(
		"MATCH (a:person)-[k:knows]-(b:person) RETURN a",
		"Undirected and multi-directional edge patterns are not supported yet",
		"-[k:knows]-",
	);
}

#[test]
fn rejects_node_label_disjunction() {
	assert_rejects(
		"MATCH (n:person|admin) RETURN n",
		"Label expressions (`!`, `&`, `|`, `%`) are not supported yet",
		"person|admin",
	);
}

#[test]
fn rejects_node_label_wildcard() {
	assert_rejects(
		"MATCH (n:%) RETURN n",
		"Label expressions (`!`, `&`, `|`, `%`) are not supported yet",
		"%",
	);
}

#[test]
fn rejects_node_label_negation() {
	assert_rejects(
		"MATCH (n:!person) RETURN n",
		"Label expressions (`!`, `&`, `|`, `%`) are not supported yet",
		"!person",
	);
}

#[test]
fn rejects_edge_label_expression() {
	assert_rejects(
		"MATCH (a:person)-[k:knows|likes]->(b:person) RETURN a",
		"Label expressions (`!`, `&`, `|`, `%`) are not supported yet",
		"knows|likes",
	);
}

#[test]
fn rejects_unanchorable_pattern() {
	// Generalised from PR-A's leftmost-labelled-node message to the V2_DESIGN
	// wording: a pattern needs a label or a reused earlier variable.
	assert_rejects(
		"MATCH (n) RETURN n",
		"Cannot choose a starting table for this pattern: label at least one node or reuse a \
		 variable bound by an earlier pattern",
		"(n)",
	);
}

#[test]
fn rejects_unanchorable_second_pattern() {
	// The first pattern anchors on `person`; the second is wholly unlabelled
	// and shares no variable with the first, so it cannot be anchored.
	assert_rejects(
		"MATCH (a:person), (x)-[e]->(y) RETURN a",
		"Cannot choose a starting table for this pattern: label at least one node or reuse a \
		 variable bound by an earlier pattern",
		"(x)",
	);
}

#[test]
fn rejects_empty_quantifier_range() {
	assert_rejects(
		"MATCH (a:person)-[:knows]->{3,2}(b:person) RETURN a",
		"The quantifier maximum must not be smaller than its minimum",
		"{3,2}",
	);
}

#[test]
fn lowers_count_star_to_group_all() {
	// A bare `count(*)` with no GROUP BY is GROUP ALL: a zero-argument `count`
	// folded over a single group.
	let rendered = render("MATCH (n:person) RETURN count(*)");
	assert!(rendered.contains("count()"), "{rendered}");
	assert!(rendered.contains("GROUP ALL"), "{rendered}");
}

#[test]
fn lowers_group_by_with_aggregates() {
	// Each GQL aggregate maps onto its SurrealDB accumulator; the grouping key is
	// preserved and a GROUP BY clause is emitted.
	let rendered = render(
		"MATCH (n:person) RETURN n.city AS c, count(*) AS total, sum(n.age) AS s, \
		 avg(n.age) AS a, min(n.age) AS lo, max(n.age) AS hi GROUP BY n.city",
	);
	assert!(rendered.contains("GROUP BY n.city"), "{rendered}");
	assert!(rendered.contains("math::sum(n.age)"), "{rendered}");
	assert!(rendered.contains("math::mean(n.age)"), "{rendered}");
	assert!(rendered.contains("math::min(n.age)"), "{rendered}");
	assert!(rendered.contains("math::max(n.age)"), "{rendered}");
}

#[test]
fn lowers_collect_to_array_group() {
	let rendered =
		render("MATCH (n:person) RETURN n.city AS c, collect(n.name) AS names GROUP BY n.city");
	assert!(rendered.contains("array::group(n.name)"), "{rendered}");
}

#[test]
fn lowers_count_field_with_non_null_guard() {
	// GQL `count(x)` counts non-null `x`: lowered as `count(x != NONE AND x != NULL)`
	// so SurrealDB's truthy count yields the non-null count.
	let rendered = render("MATCH (n:person) RETURN count(n.age) AS c");
	assert!(rendered.contains("n.age != NONE"), "{rendered}");
	assert!(rendered.contains("n.age != NULL"), "{rendered}");
	assert!(rendered.contains("GROUP ALL"), "{rendered}");
}

#[test]
fn rejects_distinct_in_aggregate() {
	assert_rejects(
		"MATCH (n:person) RETURN count(DISTINCT n)",
		"DISTINCT/ALL inside an aggregate is not supported yet",
		"count(DISTINCT n)",
	);
}

#[test]
fn rejects_aggregate_in_where() {
	assert_rejects(
		"MATCH (n:person) WHERE count(n) > 0 RETURN n",
		"Aggregate functions are only allowed in RETURN items and ORDER BY keys",
		"count(n)",
	);
}

#[test]
fn rejects_ungrouped_non_aggregate_column() {
	// `n.age` is neither a grouping key, an aggregate, nor determined by `n.name`.
	assert_rejects(
		"MATCH (n:person) RETURN n.name, n.age GROUP BY n.name",
		"must be a GROUP BY key, an aggregate, or determined by the GROUP BY keys",
		"n.age",
	);
}

#[test]
fn lowers_functionally_dependent_column() {
	// `GROUP BY a` (the whole node) determines `a.name`, so it projects without
	// an aggregate (the planner emits its first value per group).
	let rendered = render("MATCH (a:person) RETURN a AS who, a.name AS nm GROUP BY a");
	assert!(rendered.contains("GROUP BY a"), "{rendered}");
	assert!(rendered.contains("a.name AS nm"), "{rendered}");
}

#[test]
fn rejects_uncovered_dependent_column() {
	// Grouping by `a.name` does NOT determine `a.age`.
	assert_rejects(
		"MATCH (a:person) RETURN a.name AS nm, a.age AS ag GROUP BY a.name",
		"must be a GROUP BY key, an aggregate, or determined by the GROUP BY keys",
		"a.age",
	);
}

#[test]
fn lowers_order_by_non_projected_group_key() {
	// A non-DISTINCT aggregating query may ORDER BY a grouping key it does not
	// project; the lowering materialises a hidden sort-only column.
	let rendered = render("MATCH (a:person) RETURN count(*) AS c GROUP BY a.name ORDER BY a.name");
	assert!(rendered.contains("a.name AS __order0"), "{rendered}");
	assert!(rendered.contains("ORDER BY __order0 ASC"), "{rendered}");
}

#[test]
fn rejects_distinct_order_by_non_return_item() {
	assert_rejects(
		"MATCH (n:person) RETURN DISTINCT n.name ORDER BY n.age",
		"With RETURN DISTINCT, ORDER BY may only reference returned columns",
		"n.age",
	);
}

#[test]
fn rejects_unsupported_aggregate() {
	assert_rejects(
		"MATCH (n:person) RETURN stddev_pop(n.age)",
		"Aggregate functions are not supported yet",
		"stddev_pop(n.age)",
	);
}

#[test]
fn rejects_non_aggregate_functions() {
	assert_rejects(
		"MATCH (n:person) RETURN upper(n.name)",
		"The function `upper` is not supported yet",
		"upper(n.name)",
	);
}

#[test]
fn rejects_function_calls_in_where() {
	assert_rejects(
		"MATCH (n:person) WHERE upper(n.name) = 'A' RETURN n",
		"The function `upper` is not supported yet",
		"upper(n.name)",
	);
}

#[test]
fn rejects_nulls_first() {
	assert_rejects(
		"MATCH (n:person) RETURN n.age ORDER BY n.age NULLS FIRST",
		"`NULLS FIRST`/`NULLS LAST` ordering is not supported yet",
		"n.age NULLS FIRST",
	);
}

#[test]
fn rejects_nulls_last() {
	assert_rejects(
		"MATCH (n:person) RETURN n.age ORDER BY n.age DESC NULLS LAST",
		"`NULLS FIRST`/`NULLS LAST` ordering is not supported yet",
		"n.age DESC NULLS LAST",
	);
}

#[test]
fn rejects_duplicate_verbatim_columns() {
	assert_rejects(
		"MATCH (n:person) RETURN n.name, n.name",
		"Duplicate column name `n.name`",
		"n.name",
	);
}

#[test]
fn rejects_duplicate_aliases() {
	assert_rejects(
		"MATCH (n:person) RETURN n.age AS x, n.name AS x",
		"Duplicate column name `x`",
		"x",
	);
}

#[test]
fn rejects_dunder_variables() {
	assert_rejects(
		"MATCH (__n:person) RETURN __n",
		"Variable names starting with `__` are reserved for internal use",
		"__n",
	);
}

#[test]
fn rejects_dunder_aliases() {
	assert_rejects(
		"MATCH (n:person) RETURN n.age AS __x",
		"Aliases starting with `__` are reserved for internal use",
		"__x",
	);
}

#[test]
fn rejects_dunder_parameters() {
	assert_rejects(
		"MATCH (n:person) WHERE n.age > $__p RETURN n",
		"Parameter names starting with `__` are reserved for internal use",
		"$__p",
	);
}

#[test]
fn rejects_engine_reserved_parameters() {
	assert_rejects(
		"MATCH (n:person) WHERE n.age > $parent RETURN n",
		"The parameter name `$parent` is reserved by the engine",
		"$parent",
	);
	assert_rejects(
		"MATCH (n:person) RETURN n LIMIT $auth",
		"The parameter name `$auth` is reserved by the engine",
		"$auth",
	);
}

#[test]
fn rejects_xor_in_predicates() {
	assert_rejects(
		"MATCH (n:person) WHERE n.flag XOR n.old RETURN n",
		"`XOR` is not supported yet",
		"n.flag XOR n.old",
	);
}

#[test]
fn rejects_xor_in_value_position() {
	assert_rejects(
		"MATCH (n:person) RETURN n.flag XOR true AS x",
		"`XOR` is not supported yet",
		"n.flag XOR true",
	);
}

#[test]
fn rejects_unknown_variables() {
	assert_rejects("MATCH (n:person) RETURN m", "Unknown variable `m`", "m");
	assert_rejects("MATCH (n:person) WHERE m.age > 1 RETURN n", "Unknown variable `m`", "m");
}

#[test]
fn rejects_node_variable_reused_as_edge() {
	// Reusing a node variable as an edge is a kind mismatch (it stays rejected;
	// only a node-as-node reuse flips to a join).
	assert_rejects(
		"MATCH (n:person)-[n]->(b:person) RETURN b",
		"Variable `n` is already bound as a node but reused as an edge",
		"n",
	);
}

#[test]
fn rejects_repeated_edge_variable() {
	// A repeated edge variable is rejected under DIFFERENT EDGES (R2): the
	// equi-join would always be empty.
	assert_rejects(
		"MATCH (a:person)-[k:knows]->(b:person), (c:person)-[k:knows]->(d:person) RETURN a",
		"Edge variable `k` cannot be repeated",
		"k",
	);
}

#[test]
fn rejects_edge_variable_reused_as_node() {
	assert_rejects(
		"MATCH (a:person)-[k:knows]->(b:person), (k:person) RETURN a",
		"Variable `k` is already bound as an edge but reused as a node",
		"k",
	);
}

#[test]
fn rejects_return_star_without_variables() {
	assert_rejects(
		"MATCH (:person) RETURN *",
		"RETURN * requires at least one named pattern variable",
		"RETURN *",
	);
}

// ------------------------------------------------------------------------
// Rejections — message change and new rejections (V2_DESIGN §8 ledger).
// ------------------------------------------------------------------------

#[test]
fn rejects_optional_rebind_in_mandatory_clause() {
	// `b` is first bound inside the OPTIONAL, then re-declared in a later
	// MANDATORY clause. On a miss `b` is `Value::Null` (R3), so a mandatory
	// pattern cannot anchor / join on it: the optional-rebind rejection
	// (V2_DESIGN §1). Re-declaring it INSIDE the same / a deeper optional is fine.
	assert_rejects(
		"MATCH (a:person) OPTIONAL MATCH (a)-[:knows]->(b:person) MATCH (b:person) RETURN a",
		"Variable `b` was first bound inside an OPTIONAL and cannot be re-declared outside it",
		"b",
	);
}

#[test]
fn correlated_mandatory_binding_referenced_inside_optional_is_allowed() {
	// The reverse of the rebind rejection: a MANDATORY binding (`a`) reused inside
	// an OPTIONAL is a correlated constraint, not a rebind — it resolves to the
	// existing mandatory binding and lowers fine.
	assert_eq!(
		render("MATCH (a:person) OPTIONAL MATCH (a)-[:knows]->(b:person) RETURN a"),
		"bindings: a:Node __e0:Edge* b:Node\n\
		 MATCH (a:person)\n\
		 OPTIONAL#0 MATCH (a)-[__e0:knows]->(b:person)\n\
		 RETURN a AS a"
	);
}

#[test]
fn rejects_query_leading_with_optional() {
	// `OPTIONAL` is a left-outer join against the preceding binding table, so a
	// query cannot start with one (there is nothing to join against). The planner
	// relies on the first fold unit being mandatory, so the lowering rejects this
	// cleanly rather than letting it reach a planner internal error.
	assert_rejects(
		"OPTIONAL MATCH (a:person) RETURN a",
		"A query cannot start with OPTIONAL MATCH",
		"OPTIONAL MATCH (a:person)",
	);
}

#[test]
fn rejects_optional_leading_multi_hop_bound_var_expansion() {
	// An OPTIONAL block's leading clause whose only anchor is the bound `a` (no
	// labelled element) is realised off the OUTER accumulator by the single-hop
	// `OptionalExpand` only. A MULTI-hop bound-variable expansion off the outer
	// accumulator has no operator (it is neither a self-rootable subplan nor a
	// single-hop fast path), so it is rejected at lowering rather than reaching a
	// planner internal error.
	assert_rejects(
		"MATCH (a:person) OPTIONAL MATCH (a)-[k1]->(x)-[k2]->(b) RETURN a",
		"This MATCH pattern shape is not supported yet",
		"(a)",
	);
}

#[test]
fn rejects_optional_leading_quantified_bound_var_expansion() {
	// Likewise a QUANTIFIED bound-variable expansion leading an OPTIONAL block: a
	// quantified hop is a `PathExpand` (not the single-hop `OptionalExpand` fast
	// path), and an unlabelled quantified start cannot self-root a standalone
	// subplan — so it is rejected at lowering.
	assert_rejects(
		"MATCH (a:person) OPTIONAL MATCH (a)-[:knows]->{1,3}(b:person) RETURN a",
		"This MATCH pattern shape is not supported yet",
		"(a)",
	);
}

#[test]
fn optional_block_continuation_multi_hop_expansion_lowers() {
	// The mirror of the rejection above: a MULTI-hop bound-variable expansion is
	// fine when it expands off a WITHIN-block binding (here the block's own `b`),
	// because that is a within-subplan expansion the planner realises as a chain of
	// Expands — not a correlated leading hop off the outer accumulator.
	assert_eq!(
		render(
			"MATCH (a:person) OPTIONAL { MATCH (a)-[:knows]->(b:person) \
			 MATCH (b)-[k1]->(x)-[k2]->(c) } RETURN a"
		),
		"bindings: a:Node __e0:Edge* b:Node k1:Edge x:Node k2:Edge c:Node\n\
		 MATCH (a:person)\n\
		 OPTIONAL#0 MATCH (a)-[__e0:knows]->(b:person)\n\
		 OPTIONAL#0 MATCH (b)-[k1]->(x)-[k2]->(c)\n\
		 RETURN a AS a"
	);
}

#[test]
fn rejects_property_access_on_group_variable() {
	assert_rejects(
		"MATCH (a:person)-[k:knows]->{1,3}(b:person) RETURN k.since",
		"Property access on a group or path variable is not supported yet",
		"since",
	);
}

#[test]
fn rejects_property_access_on_path_variable() {
	assert_rejects(
		"MATCH p = (a:person)-[:knows]->(b:person) RETURN p.length",
		"Property access on a group or path variable is not supported yet",
		"length",
	);
}

#[test]
fn rejects_property_map_on_quantified_edge_group() {
	// A property map on a quantified edge is property access on a group.
	assert_rejects(
		"MATCH (a:person)-[k:knows {since: 2020}]->{1,3}(b:person) RETURN b",
		"Property access on a group or path variable is not supported yet",
		"since",
	);
}

#[test]
fn rejects_cross_variable_predicate_inside_quantified_edge() {
	assert_rejects(
		"MATCH (a:person)-[k:knows WHERE k.since > a.age]->{1,3}(b:person) RETURN b",
		"A predicate inside a quantified edge may only reference that edge",
		"k.since > a.age",
	);
}