graphforge-plan 0.5.1

GraphForge DataFusion integration, optimizer rules, and custom plan nodes
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
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
//! GraphForge DataFusion integration, optimizer rules, and custom plan nodes.
//!
//! # Custom logical plan nodes (#577)
//!
//! Six graph-native operators cannot be expressed in relational algebra.  This
//! module defines their **logical plan stubs** — enough for the lowering layer
//! to produce a valid [`LogicalPlan`] that DataFusion accepts without panicking.
//! Physical implementations come in milestone 13 (Execution Baseline).
//!
//! | Node | Triggered by |
//! |---|---|
//! | [`VarLenExpandNode`] | `Expand` with `max_hops != Some(1)` |
//! | [`OptionalMatchNode`] | `Optional { child }` |
//! | [`PathUniqueNode`] | `Expand` with path-uniqueness flag |
//! | [`OntologyInferNode`] | `Expand` on transitive/symmetric relation |
//! | [`GraphMergeNode`] | `Merge { pattern }` |
//! | [`UnwindNode`] | `Unwind { list_expr, alias }` |
#![forbid(unsafe_code)]
// The `name()` methods return string literals but the trait signature requires
// `&str` tied to `&self`; the lint fires because the bound is unnecessary for
// the literal values.
#![allow(clippy::unnecessary_literal_bound)]

use std::cmp::Ordering;
use std::collections::HashMap;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::path::PathBuf;
use std::sync::Arc;

use datafusion::arrow::datatypes::{DataType, Field, Fields, Schema};
use datafusion::common::{DFSchema, DFSchemaRef, Result as DfResult, TableReference};
use datafusion::logical_expr::{Expr, ExprSchemable, LogicalPlan, UserDefinedLogicalNodeCore};

use graphforge_core::OntologyMode;
use graphforge_ir::{Direction, IrLiteral};

/// Implement `PartialOrd` for custom plan nodes that contain `DFSchemaRef`.
///
/// `DFSchemaRef` (`Arc<DFSchema>`) does not implement `PartialOrd`, so we
/// cannot derive it.  Returning `None` (incomparable) is the safe default
/// used by DataFusion's own test nodes.
macro_rules! impl_partial_ord {
    ($t:ty) => {
        impl PartialOrd for $t {
            fn partial_cmp(&self, _other: &Self) -> Option<Ordering> {
                None
            }
        }
    };
}

// ---------------------------------------------------------------------------
// Shared helpers
// ---------------------------------------------------------------------------

/// A placeholder output schema that mirrors the input schema.  Used by stubs
/// whose real output schema is determined at physical planning time.
fn passthrough_schema(inputs: &[&LogicalPlan]) -> DFSchemaRef {
    inputs
        .first()
        .map_or_else(|| Arc::new(DFSchema::empty()), |p| p.schema().clone())
}

/// Arrow field name of the variable-length edge-list column (qualified
/// `var_<edge_var>`).  openCypher binds the edge variable `r` in
/// `(a)-[r:KNOWS*1..3]->(b)` to the **list of relationships** along each path.
///
/// Lowercase ASCII so the dotted reference `col("var_<edge>.rels")` resolves —
/// DataFusion lowercases unquoted identifiers.
pub const VAR_LEN_EDGE_LIST_FIELD: &str = "rels";

/// The Arrow [`Field`] for the variable-length edge-list column: a nullable
/// `List<Struct<{edge_uuid, src_uuid, dst_uuid, rel_type, <props…>}>>`.
///
/// This is the **single source of truth** for the column's type, shared by the
/// lowerer (which puts it in [`VarLenExpandNode`]'s schema) and the physical
/// `VarLenExpandExec` (which produces it).  They must be byte-identical or the
/// positional `RecordBatch::try_new` at execution time fails the schema check.
///
/// The struct carries only **public** UUID identity + the relation type — never
/// the surrogate `edge_id`/`src_id`/`dst_id` — so the UUID-only output contract
/// holds (the graphforge-api Shaper passes the whole column through untouched).
///
/// `prop_fields` are the relation's persisted edge-property columns (#755),
/// appended after the four topology fields in order. They are discovered at
/// lowering time from `edge_properties/<REL>.parquet` (see
/// `lower_var_len_expand`); pass an empty slice for a topology-only struct
/// (wildcard `*`, or a relation with no persisted properties), which reproduces
/// the original four-field layout byte-for-byte.
#[must_use]
pub fn var_len_edge_list_field(prop_fields: &[Field]) -> Arc<Field> {
    let mut fields = vec![
        Field::new("edge_uuid", DataType::FixedSizeBinary(16), false),
        Field::new("src_uuid", DataType::FixedSizeBinary(16), false),
        Field::new("dst_uuid", DataType::FixedSizeBinary(16), false),
        Field::new("rel_type", DataType::Utf8, true),
    ];
    fields.extend(prop_fields.iter().cloned());
    let struct_fields = Fields::from(fields);
    // The item is NULLABLE even though the exec never emits a null element:
    // DataFusion's list kernels (`array_pop_front` for `tail(r)`, #1023) return
    // item-nullable lists, and the runtime asserts the kernel's output type
    // matches the type promised at planning time — a non-null item fails that
    // assertion the moment any list function touches the column.
    let item = Arc::new(Field::new("item", DataType::Struct(struct_fields), true));
    Arc::new(Field::new(
        VAR_LEN_EDGE_LIST_FIELD,
        DataType::List(item),
        true,
    ))
}

// ---------------------------------------------------------------------------
// VarLenExpandNode
// ---------------------------------------------------------------------------

/// Physical node for variable-length path expansion.
///
/// Triggered when `Expand` has `max_hops != Some(1)` — patterns like
/// `(a)-[:KNOWS*1..3]->(b)`.  These cannot be expressed as a finite join
/// sequence, so the physical layer ([`VarLenExpandExec`](../graphforge_exec) in M13)
/// performs an iterative BFS over the Parquet edge table.
///
/// # Baked execution context
///
/// Like [`GraphCreateNode`], the project `dir` and ontology `mode` are baked in
/// at lowering time because the physical-planning `ExtensionPlanner` only sees
/// the DataFusion session state, not the GraphForge project path.  The physical
/// node reads edges directly from `dir` at execution time.
///
/// # Output schema
///
/// The output extends the input schema with the **destination** node's columns
/// (`var_<dst>`-qualified [`TOPOLOGY_NODES_SCHEMA`](../graphforge_storage)), then a
/// trailing edge-list column (`var_<edge>`-qualified [`var_len_edge_list_field`])
/// binding the edge variable `r` to the openCypher *list of relationships* along
/// each path (#709).  The edge column is **last**; the physical node produces
/// columns in this exact order.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct VarLenExpandNode {
    /// Input plan (the source node scan / prior pipeline).
    pub input: Arc<LogicalPlan>,
    /// Name of the relation type to expand along (or `"*"` for wildcard).
    pub rel_type_name: String,
    /// Minimum number of hops.
    pub min_hops: u16,
    /// Maximum number of hops (`None` = unbounded).
    pub max_hops: Option<u16>,
    /// Source node pattern-variable id (the frontier seed in the input).
    pub src_var: u32,
    /// Destination node pattern-variable id (bound to the reached node).
    pub dst_var: u32,
    /// Edge pattern-variable id (bound to the per-path relationship list).
    pub edge_var: u32,
    /// Edge traversal direction.
    pub direction: Direction,
    /// Resolved relation type id (`TypeId.0`), or `None` for a wildcard.
    pub rel_ty: Option<u32>,
    /// Project directory the physical node reads edges from.
    pub dir: PathBuf,
    /// Ontology mode (drives typed vs exploratory edge-file routing).
    pub mode: OntologyMode,
    schema: DFSchemaRef,
}

impl VarLenExpandNode {
    /// Create a variable-length expand node.
    ///
    /// `dst_fields` is the destination node's column list (the storage layer's
    /// `TOPOLOGY_NODES_SCHEMA` fields), passed in by the lowerer so this crate
    /// need not depend on `graphforge-storage`.  They are qualified `var_<dst_var>` and
    /// appended to the input schema.  `edge_field` is the trailing edge-list
    /// column ([`var_len_edge_list_field`]), qualified `var_<edge_var>`.
    #[allow(clippy::too_many_arguments)]
    #[must_use]
    pub fn new(
        input: Arc<LogicalPlan>,
        rel_type_name: impl Into<String>,
        min_hops: u16,
        max_hops: Option<u16>,
        src_var: u32,
        dst_var: u32,
        edge_var: u32,
        direction: Direction,
        rel_ty: Option<u32>,
        dir: PathBuf,
        mode: OntologyMode,
        dst_fields: Vec<Arc<Field>>,
        edge_field: Arc<Field>,
    ) -> Self {
        let schema = Self::build_schema(&input, dst_var, edge_var, dst_fields, edge_field);
        Self {
            input,
            rel_type_name: rel_type_name.into(),
            min_hops,
            max_hops,
            src_var,
            dst_var,
            edge_var,
            direction,
            rel_ty,
            dir,
            mode,
            schema,
        }
    }

    /// Build the output [`DFSchema`]: the input's qualified fields, then the
    /// destination node's fields qualified `var_<dst_var>`, then the edge-list
    /// column qualified `var_<edge_var>` (last).
    fn build_schema(
        input: &Arc<LogicalPlan>,
        dst_var: u32,
        edge_var: u32,
        dst_fields: Vec<Arc<Field>>,
        edge_field: Arc<Field>,
    ) -> DFSchemaRef {
        let dst_qualifier = TableReference::bare(format!("var_{dst_var}"));
        let edge_qualifier = TableReference::bare(format!("var_{edge_var}"));
        let mut qualified: Vec<(Option<TableReference>, Arc<Field>)> = input
            .schema()
            .iter()
            .map(|(q, f)| (q.cloned(), Arc::clone(f)))
            .collect();
        qualified.extend(
            dst_fields
                .into_iter()
                .map(|f| (Some(dst_qualifier.clone()), f)),
        );
        // The edge-list column is appended LAST; the physical node produces
        // columns in this order.
        qualified.push((Some(edge_qualifier), edge_field));
        // Fail fast: a construction error here means the node would advertise a
        // schema missing its `var_<dst>`/`var_<edge>` columns, breaking
        // downstream resolution.  This only fails on a malformed field set (a
        // programmer error), so panic with context rather than degrading
        // silently.
        Arc::new(
            DFSchema::new_with_metadata(qualified, std::collections::HashMap::new())
                .expect("VarLenExpandNode schema must include qualified destination + edge fields"),
        )
    }
}

impl_partial_ord!(VarLenExpandNode);

impl UserDefinedLogicalNodeCore for VarLenExpandNode {
    fn name(&self) -> &str {
        "VarLenExpand"
    }

    fn inputs(&self) -> Vec<&LogicalPlan> {
        vec![&self.input]
    }

    fn schema(&self) -> &DFSchemaRef {
        &self.schema
    }

    fn expressions(&self) -> Vec<Expr> {
        vec![]
    }

    fn fmt_for_explain(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let max = self.max_hops.map_or("*".to_owned(), |h| h.to_string());
        let arrow = match self.direction {
            Direction::Out => "->",
            Direction::In => "<-",
            Direction::Undirected => "--",
        };
        write!(
            f,
            "VarLenExpand: rel={}, hops={}..{}, dir={arrow}, edge=var_{}",
            self.rel_type_name, self.min_hops, max, self.edge_var
        )
    }

    fn with_exprs_and_inputs(&self, _exprs: Vec<Expr>, inputs: Vec<LogicalPlan>) -> DfResult<Self> {
        let input = Arc::new(
            inputs
                .into_iter()
                .next()
                .unwrap_or_else(|| (*self.input).clone()),
        );
        // Reuse the already-computed destination fields (strip the qualifier);
        // the input schema is the only piece that can change here.
        let dst_qual = format!("var_{}", self.dst_var);
        let dst_fields: Vec<Arc<Field>> = self
            .schema
            .iter()
            .filter(|(q, _)| q.map(TableReference::table) == Some(dst_qual.as_str()))
            .map(|(_, f)| Arc::clone(f))
            .collect();
        // The trailing edge-list column (exactly one field qualified `var_<edge>`).
        let edge_qual = format!("var_{}", self.edge_var);
        let edge_field = self
            .schema
            .iter()
            .find(|(q, _)| q.map(TableReference::table) == Some(edge_qual.as_str()))
            .map_or_else(|| var_len_edge_list_field(&[]), |(_, f)| Arc::clone(f));
        Ok(Self::new(
            input,
            self.rel_type_name.clone(),
            self.min_hops,
            self.max_hops,
            self.src_var,
            self.dst_var,
            self.edge_var,
            self.direction,
            self.rel_ty,
            self.dir.clone(),
            self.mode,
            dst_fields,
            edge_field,
        ))
    }
}

// ---------------------------------------------------------------------------
// ExpandNode (#763, #1248)
// ---------------------------------------------------------------------------

/// Physical node for adjacency-backed single-hop expansion (#763).
///
/// Emitted by the lowerer **instead of** the two-join chain whenever a project
/// read target is available. The physical `ExpandExec` (graphforge-exec) probes the
/// session adjacency provider per frontier row; that provider owns the
/// hit/miss/building fallback policy, so plan shape is stable across index
/// state and downstream `LIMIT` can cancel traversal work (#1248).
///
/// # Output schema (join-path parity)
///
/// Exactly what the join chain would have produced, in the same order: the
/// input's qualified fields, then the edge topology fields (typed or
/// exploratory/wildcard) qualified `var_<edge_var>`, then persisted
/// edge-property fields (forced **nullable** — the join path LEFT-joins them)
/// also under `var_<edge_var>`, then the destination node's topology fields
/// qualified `var_<dst_var>`. Destination type filtering and property joining
/// stay downstream in the binder's trailing `NodeScan{dst, ty}` (#789),
/// exactly as on the join path.
///
/// For `Undirected`, `ExpandExec` collapses the provider's duplicate self-loop
/// entries per input row, matching the relational union shape.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ExpandNode {
    /// Input plan (the source node scan / prior pipeline).
    pub input: Arc<LogicalPlan>,
    /// Name of the relation type to expand along (`"*"` for wildcard).
    pub rel_type_name: String,
    /// Source node pattern-variable id (the frontier seed in the input).
    pub src_var: u32,
    /// Destination node pattern-variable id (bound to the reached node).
    pub dst_var: u32,
    /// Edge pattern-variable id (bound per-column, like the join path).
    pub edge_var: u32,
    /// Edge traversal direction.
    pub direction: Direction,
    /// Resolved relation type id (`TypeId.0`), absent for wildcard expansion.
    pub rel_ty: Option<u32>,
    /// Project directory the physical node reads from.
    pub dir: PathBuf,
    /// Ontology mode controlling the persisted edge layout.
    pub mode: OntologyMode,
    /// How many of the `var_<edge_var>` fields are edge-property columns
    /// (the trailing ones); the rest are edge topology columns.
    pub edge_prop_count: usize,
    schema: DFSchemaRef,
}

impl ExpandNode {
    /// Create an adjacency-backed single-hop expand node.
    ///
    /// `edge_fields` are the typed edge table's topology columns and
    /// `edge_prop_fields` the relation's persisted property columns (the
    /// lowerer discovers them from `edge_properties/<REL>.parquet` and forces
    /// them nullable); `dst_fields` are the destination node's topology
    /// columns. Passed in so this crate need not depend on graphforge-storage.
    #[allow(clippy::too_many_arguments)]
    #[must_use]
    pub fn new(
        input: Arc<LogicalPlan>,
        rel_type_name: impl Into<String>,
        src_var: u32,
        dst_var: u32,
        edge_var: u32,
        direction: Direction,
        rel_ty: Option<u32>,
        dir: PathBuf,
        mode: OntologyMode,
        edge_fields: Vec<Arc<Field>>,
        edge_prop_fields: Vec<Arc<Field>>,
        dst_fields: Vec<Arc<Field>>,
    ) -> Self {
        let edge_prop_count = edge_prop_fields.len();
        let schema = Self::build_schema(
            &input,
            dst_var,
            edge_var,
            edge_fields,
            edge_prop_fields,
            dst_fields,
        );
        Self {
            input,
            rel_type_name: rel_type_name.into(),
            src_var,
            dst_var,
            edge_var,
            direction,
            rel_ty,
            dir,
            mode,
            edge_prop_count,
            schema,
        }
    }

    /// Build the output [`DFSchema`]: input qualified fields ++ edge topology
    /// fields (`var_<edge>`) ++ edge property fields (`var_<edge>`, forced
    /// nullable) ++ destination node fields (`var_<dst>`).
    fn build_schema(
        input: &Arc<LogicalPlan>,
        dst_var: u32,
        edge_var: u32,
        edge_fields: Vec<Arc<Field>>,
        edge_prop_fields: Vec<Arc<Field>>,
        dst_fields: Vec<Arc<Field>>,
    ) -> DFSchemaRef {
        let edge_qualifier = TableReference::bare(format!("var_{edge_var}"));
        let dst_qualifier = TableReference::bare(format!("var_{dst_var}"));
        let mut qualified: Vec<(Option<TableReference>, Arc<Field>)> = input
            .schema()
            .iter()
            .map(|(q, f)| (q.cloned(), Arc::clone(f)))
            .collect();
        qualified.extend(
            edge_fields
                .into_iter()
                .map(|f| (Some(edge_qualifier.clone()), f)),
        );
        // Property columns are nullable on the join path (LEFT join); force it
        // here so the schemas agree regardless of the file's declared
        // nullability.
        qualified.extend(edge_prop_fields.into_iter().map(|f| {
            let f = Arc::new(f.as_ref().clone().with_nullable(true));
            (Some(edge_qualifier.clone()), f)
        }));
        qualified.extend(
            dst_fields
                .into_iter()
                .map(|f| (Some(dst_qualifier.clone()), f)),
        );
        Arc::new(
            DFSchema::new_with_metadata(qualified, std::collections::HashMap::new())
                .expect("ExpandNode schema must include qualified edge + destination fields"),
        )
    }
}

impl_partial_ord!(ExpandNode);

impl UserDefinedLogicalNodeCore for ExpandNode {
    fn name(&self) -> &str {
        "Expand"
    }

    fn inputs(&self) -> Vec<&LogicalPlan> {
        vec![&self.input]
    }

    fn schema(&self) -> &DFSchemaRef {
        &self.schema
    }

    fn expressions(&self) -> Vec<Expr> {
        vec![]
    }

    fn fmt_for_explain(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let arrow = match self.direction {
            Direction::Out => "->",
            Direction::In => "<-",
            Direction::Undirected => "--",
        };
        write!(
            f,
            "Expand: rel={}, dir={arrow}, edge=var_{}",
            self.rel_type_name, self.edge_var
        )
    }

    fn with_exprs_and_inputs(&self, _exprs: Vec<Expr>, inputs: Vec<LogicalPlan>) -> DfResult<Self> {
        let input = Arc::new(
            inputs
                .into_iter()
                .next()
                .unwrap_or_else(|| (*self.input).clone()),
        );
        // Recover the edge/dst fields from this node's own schema by
        // qualifier; only the input can change under optimizer rewrites.
        let edge_qual = format!("var_{}", self.edge_var);
        let all_edge: Vec<Arc<Field>> = self
            .schema
            .iter()
            .filter(|(q, _)| q.map(TableReference::table) == Some(edge_qual.as_str()))
            .map(|(_, f)| Arc::clone(f))
            .collect();
        let topo_count = all_edge.len().saturating_sub(self.edge_prop_count);
        let edge_fields = all_edge[..topo_count].to_vec();
        let edge_prop_fields = all_edge[topo_count..].to_vec();
        let dst_qual = format!("var_{}", self.dst_var);
        let dst_fields: Vec<Arc<Field>> = self
            .schema
            .iter()
            .filter(|(q, _)| q.map(TableReference::table) == Some(dst_qual.as_str()))
            .map(|(_, f)| Arc::clone(f))
            .collect();
        Ok(Self::new(
            input,
            self.rel_type_name.clone(),
            self.src_var,
            self.dst_var,
            self.edge_var,
            self.direction,
            self.rel_ty,
            self.dir.clone(),
            self.mode,
            edge_fields,
            edge_prop_fields,
            dst_fields,
        ))
    }
}

// ---------------------------------------------------------------------------
// OptionalMatchNode
// ---------------------------------------------------------------------------

/// Physical node for `OPTIONAL MATCH` (LEFT OUTER semantics with openCypher
/// null-shaping over a sub-plan).
///
/// The physical layer ([`OptionalMatchExec`](../graphforge_exec) in M13) left-joins the
/// `outer` (mandatory) input against the `optional` sub-plan on the shared
/// pattern variables ([`join_keys`](Self::join_keys)), preserving every outer
/// row and setting the optional-side columns to **null** when there is no match
/// — distinct from a SQL `LEFT JOIN` only in that the shared join-key columns
/// are not duplicated on the output (they belong to the outer side).
///
/// # Output schema
///
/// `outer` fields, followed by the `optional` fields that are **not** join keys,
/// each made nullable (an unmatched outer row nulls them all).
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct OptionalMatchNode {
    /// The outer (mandatory) input.
    pub outer: Arc<LogicalPlan>,
    /// The optional sub-plan.
    pub optional: Arc<LogicalPlan>,
    /// Shared-variable join keys as `(outer_col_idx, inner_col_idx)` pairs,
    /// resolved against the qualified input schemas at lowering time.
    pub join_keys: Vec<(usize, usize)>,
    /// Inner (optional-side) column indices to append to the output, in order.
    /// Excludes **every** shared-variable column — not merely the join-key
    /// columns — because a shared variable's whole node (e.g. `a` in
    /// `MATCH (a) OPTIONAL MATCH (a)-[:R]->(b)`) is carried by the outer side;
    /// appending its columns again would duplicate the `var_<shared>` fields.
    pub inner_keep_idx: Vec<usize>,
    schema: DFSchemaRef,
}

impl OptionalMatchNode {
    /// Create an optional-match node.
    ///
    /// `inner_keep_idx` lists the optional plan's column indices to append to
    /// the output (every column of a shared variable already excluded — those
    /// live on the outer side). Passed by the lowerer, which resolves them
    /// against the qualified inner schema, so this crate needs no
    /// `graphforge-rel`/`graphforge-storage` dependency. The node appends the corresponding
    /// fields to the outer schema as **nullable** columns (an unmatched outer
    /// row nulls them all).
    #[must_use]
    pub fn new(
        outer: Arc<LogicalPlan>,
        optional: Arc<LogicalPlan>,
        join_keys: Vec<(usize, usize)>,
        inner_keep_idx: Vec<usize>,
    ) -> Self {
        let schema = Self::build_schema(&outer, &optional, &inner_keep_idx);
        Self {
            outer,
            optional,
            join_keys,
            inner_keep_idx,
            schema,
        }
    }

    /// Build the output [`DFSchema`]: outer fields, then the kept optional
    /// fields (by [`inner_keep_idx`](Self::inner_keep_idx)), each made nullable.
    fn build_schema(
        outer: &Arc<LogicalPlan>,
        optional: &Arc<LogicalPlan>,
        inner_keep_idx: &[usize],
    ) -> DFSchemaRef {
        let mut qualified: Vec<(Option<TableReference>, Arc<Field>)> = outer
            .schema()
            .iter()
            .map(|(q, f)| (q.cloned(), Arc::clone(f)))
            .collect();
        let inner: Vec<(Option<TableReference>, Arc<Field>)> = optional
            .schema()
            .iter()
            .map(|(q, f)| (q.cloned(), Arc::clone(f)))
            .collect();
        qualified.extend(inner_keep_idx.iter().map(|&i| {
            let (q, f) = &inner[i];
            // Unmatched outer rows null these columns, so they must be nullable
            // regardless of the inner plan's nullability (idempotent).
            let nullable = Arc::new(f.as_ref().clone().with_nullable(true));
            (q.clone(), nullable)
        }));
        Arc::new(
            DFSchema::new_with_metadata(qualified, std::collections::HashMap::new())
                .expect("OptionalMatchNode schema must be constructible from outer + inner fields"),
        )
    }
}

impl_partial_ord!(OptionalMatchNode);

impl UserDefinedLogicalNodeCore for OptionalMatchNode {
    fn name(&self) -> &str {
        "OptionalMatch"
    }

    fn inputs(&self) -> Vec<&LogicalPlan> {
        vec![&self.outer, &self.optional]
    }

    fn schema(&self) -> &DFSchemaRef {
        &self.schema
    }

    fn expressions(&self) -> Vec<Expr> {
        vec![]
    }

    fn fmt_for_explain(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "OptionalMatch: keys={}", self.join_keys.len())
    }

    fn with_exprs_and_inputs(
        &self,
        _exprs: Vec<Expr>,
        mut inputs: Vec<LogicalPlan>,
    ) -> DfResult<Self> {
        // Only the inputs can change here; the join keys and the kept-inner
        // column indices are preserved (the schema is rebuilt from them).
        let optional = Arc::new(inputs.pop().unwrap_or_else(|| (*self.optional).clone()));
        let outer = Arc::new(inputs.pop().unwrap_or_else(|| (*self.outer).clone()));
        Ok(Self::new(
            outer,
            optional,
            self.join_keys.clone(),
            self.inner_keep_idx.clone(),
        ))
    }
}

// ---------------------------------------------------------------------------
// PathUniqueNode
// ---------------------------------------------------------------------------

/// Logical stub for path-uniqueness filtering (eliminates paths that visit
/// the same node or edge more than once).
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct PathUniqueNode {
    /// Input plan.
    pub input: Arc<LogicalPlan>,
    schema: DFSchemaRef,
}

impl PathUniqueNode {
    /// Create a new path-uniqueness stub.
    #[must_use]
    pub fn new(input: Arc<LogicalPlan>) -> Self {
        let schema = passthrough_schema(&[&input]);
        Self { input, schema }
    }
}

impl_partial_ord!(PathUniqueNode);

impl UserDefinedLogicalNodeCore for PathUniqueNode {
    fn name(&self) -> &str {
        "PathUnique"
    }

    fn inputs(&self) -> Vec<&LogicalPlan> {
        vec![&self.input]
    }

    fn schema(&self) -> &DFSchemaRef {
        &self.schema
    }

    fn expressions(&self) -> Vec<Expr> {
        vec![]
    }

    fn fmt_for_explain(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "PathUnique")
    }

    fn with_exprs_and_inputs(&self, _exprs: Vec<Expr>, inputs: Vec<LogicalPlan>) -> DfResult<Self> {
        let input = Arc::new(
            inputs
                .into_iter()
                .next()
                .unwrap_or_else(|| (*self.input).clone()),
        );
        Ok(Self::new(input))
    }
}

// ---------------------------------------------------------------------------
// OntologyInferNode
// ---------------------------------------------------------------------------

/// Logical stub for ontology-driven semantic inference (transitive /
/// symmetric closure expansion).
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct OntologyInferNode {
    /// Input plan.
    pub input: Arc<LogicalPlan>,
    /// The relation type whose semantic flags trigger inference.
    pub rel_type_name: String,
    /// The ontology rule that triggered inference, e.g. `transitive:KNOWS` (#605).
    pub rule_id: String,
    /// The confidence policy for the derived facts, e.g. `conservative_min` (#605).
    pub confidence_model: String,
    schema: DFSchemaRef,
}

impl OntologyInferNode {
    /// Create a new ontology-inference node carrying its rule (#605).
    #[must_use]
    pub fn new(
        input: Arc<LogicalPlan>,
        rel_type_name: impl Into<String>,
        rule_id: impl Into<String>,
        confidence_model: impl Into<String>,
    ) -> Self {
        let schema = passthrough_schema(&[&input]);
        Self {
            input,
            rel_type_name: rel_type_name.into(),
            rule_id: rule_id.into(),
            confidence_model: confidence_model.into(),
            schema,
        }
    }
}

impl_partial_ord!(OntologyInferNode);

impl UserDefinedLogicalNodeCore for OntologyInferNode {
    fn name(&self) -> &str {
        "OntologyInfer"
    }

    fn inputs(&self) -> Vec<&LogicalPlan> {
        vec![&self.input]
    }

    fn schema(&self) -> &DFSchemaRef {
        &self.schema
    }

    fn expressions(&self) -> Vec<Expr> {
        vec![]
    }

    fn fmt_for_explain(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "OntologyInfer: rel={} rule_id={}",
            self.rel_type_name, self.rule_id
        )
    }

    fn with_exprs_and_inputs(&self, _exprs: Vec<Expr>, inputs: Vec<LogicalPlan>) -> DfResult<Self> {
        let input = Arc::new(
            inputs
                .into_iter()
                .next()
                .unwrap_or_else(|| (*self.input).clone()),
        );
        Ok(Self::new(
            input,
            self.rel_type_name.clone(),
            self.rule_id.clone(),
            self.confidence_model.clone(),
        ))
    }
}

// ---------------------------------------------------------------------------
// GraphMergeNode
// ---------------------------------------------------------------------------

/// Logical stub for `MERGE` (match-or-create write semantics).
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct GraphMergeNode {
    schema: DFSchemaRef,
}

impl GraphMergeNode {
    /// Create a new graph-merge stub.
    #[must_use]
    pub fn new() -> Self {
        Self {
            schema: Arc::new(DFSchema::empty()),
        }
    }
}

impl_partial_ord!(GraphMergeNode);

impl Default for GraphMergeNode {
    fn default() -> Self {
        Self::new()
    }
}

impl UserDefinedLogicalNodeCore for GraphMergeNode {
    fn name(&self) -> &str {
        "GraphMerge"
    }

    fn inputs(&self) -> Vec<&LogicalPlan> {
        vec![]
    }

    fn schema(&self) -> &DFSchemaRef {
        &self.schema
    }

    fn expressions(&self) -> Vec<Expr> {
        vec![]
    }

    fn fmt_for_explain(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "GraphMerge")
    }

    fn with_exprs_and_inputs(
        &self,
        _exprs: Vec<Expr>,
        _inputs: Vec<LogicalPlan>,
    ) -> DfResult<Self> {
        Ok(Self::new())
    }
}

// ---------------------------------------------------------------------------
// GraphCreateNode
// ---------------------------------------------------------------------------

/// A node to create, fully resolved (no expression-arena coupling).
///
/// Built by the relational lowering layer from a `graphforge_ir::CreateNodeSpec`:
/// label IDs are paired with their resolved names and property maps are
/// evaluated to literal key/value pairs, so the execution layer needs no
/// access to the IR arena or ontology.
#[derive(Debug, Clone, PartialEq)]
pub struct ResolvedNodeSpec {
    /// Pattern variable id (`VarId.0`).
    pub var: u32,
    /// Complete resolved label type-id set, in pattern order.
    pub label_ids: Vec<u32>,
    /// Complete resolved label-name set, in pattern order.
    pub label_names: Vec<String>,
    /// Literal property key/value pairs (constant or constant-folded values).
    pub properties: Vec<(String, IrLiteral)>,
    /// Row-dependent property values that could not be folded to a literal
    /// (e.g. `{n: x}` from a driving `UNWIND`/`MATCH`): each is a DataFusion
    /// `Expr` over the input columns, evaluated per minted row by the execution
    /// layer (#814). Surfaced through the node's `expressions()` so the
    /// optimizer rewrites the columns they reference.
    pub computed_properties: Vec<(String, Expr)>,
    /// `true` when this var was bound by a preceding `MATCH`/`WITH` — the
    /// executor **references** the matched node (its identity comes from the
    /// input row) rather than minting a new one (#703).
    pub is_reference: bool,
}

/// An edge to create, fully resolved.  See [`ResolvedNodeSpec`].
#[derive(Debug, Clone, PartialEq)]
pub struct ResolvedEdgeSpec {
    /// Pattern variable id of the edge.
    pub var: u32,
    /// Source node variable id.
    pub src: u32,
    /// Destination node variable id.
    pub dst: u32,
    /// Resolved relation type id, if any.
    pub rel_type_id: Option<u32>,
    /// Resolved relation type name (for file routing), if known.
    pub rel_type_name: Option<String>,
    /// Edge direction.
    pub direction: Direction,
    /// Literal property key/value pairs (constant or constant-folded values).
    ///
    /// Persisted as of #784: the execution layer writes these via
    /// `GraphWriter::set_edge_properties` to `edge_properties/<REL_TYPE>.parquet`
    /// (keyed by `edge_uuid`), and the read side joins them back. An edge with
    /// properties but no relation type is rejected (the props would be
    /// unreadable), so a non-empty map implies `rel_type_name` is `Some`.
    pub properties: Vec<(String, IrLiteral)>,
    /// Row-dependent property values evaluated per minted row (#814); see
    /// [`ResolvedNodeSpec::computed_properties`].
    pub computed_properties: Vec<(String, Expr)>,
}

/// Logical node for `CREATE`: a write specification driven by an input plan.
///
/// Carries the resolved node/edge specs plus the project directory and ontology
/// mode needed to drive a writer.  The directory is baked in at lowering time
/// because the physical-planning layer (an `ExtensionPlanner`) only sees the
/// DataFusion session state, not the GraphForge project path.
///
/// # Input
///
/// The CREATE runs **once per input row** (#703): a standalone `CREATE (:X)`
/// lowers over the implicit single unit row (so it creates exactly once), while
/// a mixed `MATCH … CREATE …` runs the write once per matched row, referencing
/// MATCH-bound vars' identities (`ResolvedNodeSpec::is_reference`) and minting
/// the rest. The input columns are **consumed** (for cardinality + referenced
/// identities), not projected — the node still emits only the write summary.
///
/// Output schema is a one-row write summary: `nodes_created` / `edges_created`
/// (`UInt64`).
#[derive(Debug, Clone)]
pub struct GraphCreateNode {
    /// Input plan whose rows drive the writes (the implicit unit row for a
    /// standalone CREATE; the MATCH results for a mixed pipeline).
    pub input: Arc<LogicalPlan>,
    /// Nodes to create, in pattern order.
    pub nodes: Vec<ResolvedNodeSpec>,
    /// Edges to create, in pattern order.
    pub edges: Vec<ResolvedEdgeSpec>,
    /// Target project directory.
    pub dir: PathBuf,
    /// Ontology mode (drives writer edge / property routing).
    pub mode: OntologyMode,
    /// Output schema: the one-row write summary in summary mode, or the
    /// created-entity row schema in emit-rows mode (#814).
    schema: DFSchemaRef,
    /// `true` when the node emits created-entity rows (write-result RETURN);
    /// `false` for the terminal one-row summary.
    emit_rows: bool,
}

impl GraphCreateNode {
    /// Build the write-summary Arrow schema
    /// (`nodes_created`, `edges_created`, `properties_set`, `labels_added`).
    #[must_use]
    pub fn summary_schema() -> Arc<Schema> {
        Arc::new(Schema::new(vec![
            Field::new("nodes_created", DataType::UInt64, false),
            Field::new("edges_created", DataType::UInt64, false),
            Field::new("properties_set", DataType::UInt64, false),
            Field::new("labels_added", DataType::UInt64, false),
        ]))
    }

    /// Create a new graph-create node over `input` in **summary** mode: the node
    /// emits the one-row write summary (a terminal `CREATE`).
    #[must_use]
    pub fn new(
        input: Arc<LogicalPlan>,
        nodes: Vec<ResolvedNodeSpec>,
        edges: Vec<ResolvedEdgeSpec>,
        dir: PathBuf,
        mode: OntologyMode,
    ) -> Self {
        let schema = Arc::new(
            DFSchema::try_from(Self::summary_schema())
                .expect("write-summary schema is always valid"),
        );
        Self {
            input,
            nodes,
            edges,
            dir,
            mode,
            schema,
            emit_rows: false,
        }
    }

    /// Create a node in **emit-rows** mode (write-result RETURN, #814): instead
    /// of the summary, it emits one row per input row carrying the input columns
    /// plus the created variables' `var_<n>`-qualified identity + property columns
    /// (`output_schema`), so a following `RETURN`/`WITH` can read them. Side
    /// effects are reported out of band (the exec's tally), since a relation
    /// cannot also carry the summary.
    #[must_use]
    pub fn new_emitting(
        input: Arc<LogicalPlan>,
        nodes: Vec<ResolvedNodeSpec>,
        edges: Vec<ResolvedEdgeSpec>,
        dir: PathBuf,
        mode: OntologyMode,
        output_schema: DFSchemaRef,
    ) -> Self {
        Self {
            input,
            nodes,
            edges,
            dir,
            mode,
            schema: output_schema,
            emit_rows: true,
        }
    }

    /// Whether this node emits created-entity rows (`true`) rather than the
    /// one-row write summary (`false`).
    #[must_use]
    pub fn emits_rows(&self) -> bool {
        self.emit_rows
    }
}

impl_partial_ord!(GraphCreateNode);

// `IrLiteral` is `PartialEq` (so we derive `PartialEq`) but not `Eq`/`Hash`
// (it carries `f64`).  `UserDefinedLogicalNodeCore` requires `Eq + Hash`, so we
// provide them by hand: `Eq` is the empty marker (literal property values are
// never NaN-reflexivity-sensitive in practice — matching DataFusion's own
// float-bearing nodes), and `Hash` normalises each literal (floats via
// `to_bits`).  `schema` is excluded from both (it is derived from the rest).
impl PartialEq for GraphCreateNode {
    fn eq(&self, other: &Self) -> bool {
        self.input == other.input
            && self.nodes == other.nodes
            && self.edges == other.edges
            && self.dir == other.dir
            && self.mode == other.mode
            && self.emit_rows == other.emit_rows
    }
}

impl Eq for GraphCreateNode {}

fn hash_literal<H: Hasher>(lit: &IrLiteral, state: &mut H) {
    match lit {
        IrLiteral::Null => 0u8.hash(state),
        IrLiteral::Bool(b) => {
            1u8.hash(state);
            b.hash(state);
        }
        IrLiteral::Int(i) => {
            2u8.hash(state);
            i.hash(state);
        }
        IrLiteral::Float(f) => {
            3u8.hash(state);
            f.to_bits().hash(state);
        }
        IrLiteral::Str(s) => {
            4u8.hash(state);
            s.hash(state);
        }
        IrLiteral::Uuid(uuid) => {
            14u8.hash(state);
            uuid.hash(state);
        }
        IrLiteral::Duration {
            months,
            days,
            seconds,
            nanos,
        } => {
            5u8.hash(state);
            months.hash(state);
            days.hash(state);
            seconds.hash(state);
            nanos.hash(state);
        }
        IrLiteral::DateTime(t) => {
            6u8.hash(state);
            t.hash(state);
        }
        IrLiteral::Date(d) => {
            7u8.hash(state);
            d.hash(state);
        }
        IrLiteral::LocalDateTime { days, nanos } => {
            8u8.hash(state);
            days.hash(state);
            nanos.hash(state);
        }
        IrLiteral::Time(n) => {
            9u8.hash(state);
            n.hash(state);
        }
        IrLiteral::ZonedTime { nanos, offset } => {
            10u8.hash(state);
            nanos.hash(state);
            offset.hash(state);
        }
        IrLiteral::ZonedDateTime {
            days,
            nanos,
            offset,
            zone,
        } => {
            11u8.hash(state);
            days.hash(state);
            nanos.hash(state);
            offset.hash(state);
            zone.hash(state);
        }
        IrLiteral::List(items) => {
            12u8.hash(state);
            items.len().hash(state);
            for it in items {
                hash_literal(it, state);
            }
        }
        IrLiteral::Map(entries) => {
            13u8.hash(state);
            entries.len().hash(state);
            for (key, value) in entries {
                key.hash(state);
                hash_literal(value, state);
            }
        }
    }
}

fn hash_props<H: Hasher>(props: &[(String, IrLiteral)], state: &mut H) {
    props.len().hash(state);
    for (k, v) in props {
        k.hash(state);
        hash_literal(v, state);
    }
}

// Computed-property values are `Expr` (not `Hash`); hash the count and keys and
// let `PartialEq` disambiguate the exprs (matching `GraphSetNode::hash`).
fn hash_computed<H: Hasher>(props: &[(String, Expr)], state: &mut H) {
    props.len().hash(state);
    for (k, _) in props {
        k.hash(state);
    }
}

impl Hash for GraphCreateNode {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.input.hash(state);
        self.nodes.len().hash(state);
        for n in &self.nodes {
            n.var.hash(state);
            n.label_ids.hash(state);
            n.label_names.hash(state);
            n.is_reference.hash(state);
            hash_props(&n.properties, state);
            hash_computed(&n.computed_properties, state);
        }
        self.edges.len().hash(state);
        for e in &self.edges {
            e.var.hash(state);
            e.src.hash(state);
            e.dst.hash(state);
            e.rel_type_id.hash(state);
            e.rel_type_name.hash(state);
            e.direction.hash(state);
            hash_props(&e.properties, state);
            hash_computed(&e.computed_properties, state);
        }
        self.dir.hash(state);
        self.mode.hash(state);
        self.emit_rows.hash(state);
    }
}

impl UserDefinedLogicalNodeCore for GraphCreateNode {
    fn name(&self) -> &str {
        "GraphCreate"
    }

    fn inputs(&self) -> Vec<&LogicalPlan> {
        vec![&self.input]
    }

    fn schema(&self) -> &DFSchemaRef {
        &self.schema
    }

    fn expressions(&self) -> Vec<Expr> {
        // Surface every row-dependent property value expr (nodes then edges, in
        // spec order) so the optimizer sees the columns they reference and
        // round-trips them through `with_exprs_and_inputs`.
        self.nodes
            .iter()
            .flat_map(|n| n.computed_properties.iter())
            .chain(self.edges.iter().flat_map(|e| e.computed_properties.iter()))
            .map(|(_, e)| e.clone())
            .collect()
    }

    fn fmt_for_explain(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "GraphCreate: nodes={}, edges={}",
            self.nodes.len(),
            self.edges.len()
        )
    }

    fn with_exprs_and_inputs(&self, exprs: Vec<Expr>, inputs: Vec<LogicalPlan>) -> DfResult<Self> {
        let input = Arc::new(
            inputs
                .into_iter()
                .next()
                .unwrap_or_else(|| (*self.input).clone()),
        );
        // Reinstall the computed-property exprs positionally in the same
        // nodes-then-edges order `expressions()` emits; a short/empty `exprs`
        // (e.g. a probe call) keeps the originals.
        let mut nodes = self.nodes.clone();
        let mut edges = self.edges.clone();
        let total: usize = nodes
            .iter()
            .map(|n| n.computed_properties.len())
            .sum::<usize>()
            + edges
                .iter()
                .map(|e| e.computed_properties.len())
                .sum::<usize>();
        if exprs.len() == total {
            let mut it = exprs.into_iter();
            for n in &mut nodes {
                for (_, e) in &mut n.computed_properties {
                    *e = it.next().expect("count checked above");
                }
            }
            for e in &mut edges {
                for (_, x) in &mut e.computed_properties {
                    *x = it.next().expect("count checked above");
                }
            }
        }
        // Preserve emit-rows mode + its output schema (a plain `new` would reset
        // to summary mode and drop the created-rows schema).
        if self.emit_rows {
            Ok(Self::new_emitting(
                input,
                nodes,
                edges,
                self.dir.clone(),
                self.mode,
                self.schema.clone(),
            ))
        } else {
            Ok(Self::new(input, nodes, edges, self.dir.clone(), self.mode))
        }
    }
}

// ---------------------------------------------------------------------------
// GraphDeleteNode
// ---------------------------------------------------------------------------

/// One resolved `DELETE` target: a bound variable and whether it is an edge.
///
/// The binder records only the `VarId`; the lowering layer resolves whether the
/// var is a node or an edge by inspecting the input schema (which identity
/// column — `node_uuid` vs `edge_uuid` — it carries), so the execution layer
/// reads the right identity column without re-deriving it.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct DeleteTarget {
    /// Pattern variable id (`VarId.0`).
    pub var: u32,
    /// `true` if the variable is an edge (delete the edge row); `false` for a
    /// node (delete the node row, and incident edges when `detach`).
    pub is_edge: bool,
}

/// Logical node for `DELETE` / `DETACH DELETE` (#740): a delete specification
/// driven by an input plan (the preceding `MATCH`).
///
/// Like [`GraphCreateNode`], the project directory and ontology mode are baked
/// in at lowering time (the `ExtensionPlanner` sees only the DataFusion session
/// state). The input rows supply the identities of the entities to delete; the
/// node emits a one-row write summary `nodes_deleted` / `edges_deleted`.
#[derive(Debug, Clone)]
pub struct GraphDeleteNode {
    /// Input plan whose rows carry the matched entities' identities.
    pub input: Arc<LogicalPlan>,
    /// The variables to delete, with their resolved node/edge kind.
    pub targets: Vec<DeleteTarget>,
    /// `true` for `DETACH DELETE` (also remove a node's incident edges).
    pub detach: bool,
    /// Target project directory.
    pub dir: PathBuf,
    /// Ontology mode (drives writer edge / property routing).
    pub mode: OntologyMode,
    schema: DFSchemaRef,
}

impl GraphDeleteNode {
    /// Build the write-summary Arrow schema (`nodes_deleted`, `edges_deleted`).
    #[must_use]
    pub fn summary_schema() -> Arc<Schema> {
        Arc::new(Schema::new(vec![
            Field::new("nodes_deleted", DataType::UInt64, false),
            Field::new("edges_deleted", DataType::UInt64, false),
        ]))
    }

    /// Create a new graph-delete node over `input`.
    #[must_use]
    pub fn new(
        input: Arc<LogicalPlan>,
        targets: Vec<DeleteTarget>,
        detach: bool,
        dir: PathBuf,
        mode: OntologyMode,
    ) -> Self {
        let schema = Arc::new(
            DFSchema::try_from(Self::summary_schema())
                .expect("write-summary schema is always valid"),
        );
        Self {
            input,
            targets,
            detach,
            dir,
            mode,
            schema,
        }
    }
}

impl_partial_ord!(GraphDeleteNode);

// `schema` is derived from the rest, so it is excluded from `PartialEq`/`Hash`
// (it is not part of the node's logical identity).
impl PartialEq for GraphDeleteNode {
    fn eq(&self, other: &Self) -> bool {
        self.input == other.input
            && self.targets == other.targets
            && self.detach == other.detach
            && self.dir == other.dir
            && self.mode == other.mode
    }
}

impl Eq for GraphDeleteNode {}

impl Hash for GraphDeleteNode {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.input.hash(state);
        self.targets.hash(state);
        self.detach.hash(state);
        self.dir.hash(state);
        self.mode.hash(state);
    }
}

impl UserDefinedLogicalNodeCore for GraphDeleteNode {
    fn name(&self) -> &str {
        "GraphDelete"
    }

    fn inputs(&self) -> Vec<&LogicalPlan> {
        vec![&self.input]
    }

    fn schema(&self) -> &DFSchemaRef {
        &self.schema
    }

    fn expressions(&self) -> Vec<Expr> {
        vec![]
    }

    fn fmt_for_explain(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "GraphDelete: targets={}, detach={}",
            self.targets.len(),
            self.detach
        )
    }

    fn with_exprs_and_inputs(&self, _exprs: Vec<Expr>, inputs: Vec<LogicalPlan>) -> DfResult<Self> {
        let input = Arc::new(
            inputs
                .into_iter()
                .next()
                .unwrap_or_else(|| (*self.input).clone()),
        );
        Ok(Self::new(
            input,
            self.targets.clone(),
            self.detach,
            self.dir.clone(),
            self.mode,
        ))
    }
}

// ---------------------------------------------------------------------------
// GraphSetNode / GraphRemoveNode
// ---------------------------------------------------------------------------

/// One resolved `SET n.prop = <expr>` target (#791).
///
/// Like [`DeleteTarget`], the node/edge kind (`is_edge`) is resolved by the
/// lowering layer from the input schema. `value` is the lowered DataFusion
/// expression for the assigned value — evaluated per matched row at execution
/// time (mirrors how [`UnwindNode`] carries its `list_expr`).
///
/// The property-file **stem** is resolved per row in the exec layer, not baked
/// here: a node uses its `type_id` (→ `_untyped` in Exploratory mode, else the
/// entity name from [`GraphSetNode::type_id_to_entity_name`]); an edge uses its
/// `rel_type_name` column (edge property files are keyed by relation name in
/// every mode).
#[derive(Debug, Clone, PartialEq)]
pub struct SetTarget {
    /// Pattern variable id (`VarId.0`).
    pub var: u32,
    /// `true` if the variable is an edge (write to `edge_properties/`); `false`
    /// for a node (write to `properties/`).
    pub is_edge: bool,
    /// The property name being assigned.
    pub prop_name: String,
    /// The value expression, evaluated per matched row in the exec layer.
    pub value: Expr,
}

/// One resolved `REMOVE n.prop` target (#791) — the value-less dual of
/// [`SetTarget`].
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct RemoveTarget {
    /// Pattern variable id (`VarId.0`).
    pub var: u32,
    /// `true` if the variable is an edge.
    pub is_edge: bool,
    /// The property name being removed.
    pub prop_name: String,
}

/// Logical node for `SET <prop> = <expr>` (#791): a property-write driven by an
/// input plan (the preceding `MATCH`).
///
/// Mirrors [`GraphDeleteNode`] — project directory and ontology mode are baked
/// in at lowering time. Each target's value expression is evaluated per matched
/// row by the physical layer, the resulting per-row literal written to the
/// entity's property file. Node-target file stems are resolved per row from the
/// row's `type_id` via [`type_id_to_entity_name`](Self::type_id_to_entity_name)
/// (handles an untyped `MATCH (n)` whose rows span several entity types). The
/// node emits a one-row summary `properties_set`.
#[derive(Debug, Clone)]
pub struct GraphSetNode {
    /// Input plan whose rows carry the matched entities' identities + columns.
    pub input: Arc<LogicalPlan>,
    /// The property assignments, with resolved node/edge kind + value expr.
    pub targets: Vec<SetTarget>,
    /// Maps a node `type_id` to its property-file entity stem, for per-row node
    /// stem resolution (an empty map / missing id falls back to `_untyped`).
    pub type_id_to_entity_name: HashMap<u32, String>,
    /// Target project directory.
    pub dir: PathBuf,
    /// Ontology mode (drives node property-file routing).
    pub mode: OntologyMode,
    schema: DFSchemaRef,
}

impl GraphSetNode {
    /// Build the write-summary Arrow schema (`properties_set`).
    #[must_use]
    pub fn summary_schema() -> Arc<Schema> {
        Arc::new(Schema::new(vec![Field::new(
            "properties_set",
            DataType::UInt64,
            false,
        )]))
    }

    /// Create a new graph-set node over `input`.
    #[must_use]
    pub fn new(
        input: Arc<LogicalPlan>,
        targets: Vec<SetTarget>,
        type_id_to_entity_name: HashMap<u32, String>,
        dir: PathBuf,
        mode: OntologyMode,
    ) -> Self {
        let schema = Arc::new(
            DFSchema::try_from(Self::summary_schema())
                .expect("write-summary schema is always valid"),
        );
        Self {
            input,
            targets,
            type_id_to_entity_name,
            dir,
            mode,
            schema,
        }
    }
}

impl_partial_ord!(GraphSetNode);

// `schema` is derived; excluded from `PartialEq`/`Hash` (not part of logical
// identity). The value exprs ARE part of identity, so they are included.
impl PartialEq for GraphSetNode {
    fn eq(&self, other: &Self) -> bool {
        self.input == other.input
            && self.targets == other.targets
            && self.dir == other.dir
            && self.mode == other.mode
            && self.type_id_to_entity_name == other.type_id_to_entity_name
    }
}

impl Eq for GraphSetNode {}

impl Hash for GraphSetNode {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.input.hash(state);
        // `SetTarget` is not `Hash` (it holds an `Expr`, which is not `Hash`);
        // hash the stable scalar fields and let `PartialEq` disambiguate the
        // value exprs. Plan-node hashing only needs to be consistent with `Eq`
        // for buckets, not collision-free.
        for t in &self.targets {
            t.var.hash(state);
            t.is_edge.hash(state);
            t.prop_name.hash(state);
        }
        self.dir.hash(state);
        self.mode.hash(state);
    }
}

impl UserDefinedLogicalNodeCore for GraphSetNode {
    fn name(&self) -> &str {
        "GraphSet"
    }

    fn inputs(&self) -> Vec<&LogicalPlan> {
        vec![&self.input]
    }

    fn schema(&self) -> &DFSchemaRef {
        &self.schema
    }

    fn expressions(&self) -> Vec<Expr> {
        // Surface each target's value expr so the optimizer sees the columns it
        // references (and round-trips them through `with_exprs_and_inputs`).
        self.targets.iter().map(|t| t.value.clone()).collect()
    }

    fn fmt_for_explain(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "GraphSet: targets={}", self.targets.len())
    }

    fn with_exprs_and_inputs(&self, exprs: Vec<Expr>, inputs: Vec<LogicalPlan>) -> DfResult<Self> {
        let input = Arc::new(
            inputs
                .into_iter()
                .next()
                .unwrap_or_else(|| (*self.input).clone()),
        );
        // Reinstall value exprs positionally; an empty/short `exprs` (e.g. a
        // probe call) keeps the originals.
        let mut targets = self.targets.clone();
        if exprs.len() == targets.len() {
            for (t, e) in targets.iter_mut().zip(exprs) {
                t.value = e;
            }
        }
        Ok(Self::new(
            input,
            targets,
            self.type_id_to_entity_name.clone(),
            self.dir.clone(),
            self.mode,
        ))
    }
}

/// Logical node for `REMOVE <prop>` (#791): the value-less dual of
/// [`GraphSetNode`]. Emits a one-row summary `properties_removed`.
#[derive(Debug, Clone)]
pub struct GraphRemoveNode {
    /// Input plan whose rows carry the matched entities' identities + columns.
    pub input: Arc<LogicalPlan>,
    /// The properties to remove, with resolved node/edge kind.
    pub targets: Vec<RemoveTarget>,
    /// Maps a node `type_id` to its property-file entity stem (see
    /// [`GraphSetNode::type_id_to_entity_name`]).
    pub type_id_to_entity_name: HashMap<u32, String>,
    /// Target project directory.
    pub dir: PathBuf,
    /// Ontology mode (drives node property-file routing).
    pub mode: OntologyMode,
    schema: DFSchemaRef,
}

impl GraphRemoveNode {
    /// Build the write-summary Arrow schema (`properties_removed`).
    #[must_use]
    pub fn summary_schema() -> Arc<Schema> {
        Arc::new(Schema::new(vec![Field::new(
            "properties_removed",
            DataType::UInt64,
            false,
        )]))
    }

    /// Create a new graph-remove node over `input`.
    #[must_use]
    pub fn new(
        input: Arc<LogicalPlan>,
        targets: Vec<RemoveTarget>,
        type_id_to_entity_name: HashMap<u32, String>,
        dir: PathBuf,
        mode: OntologyMode,
    ) -> Self {
        let schema = Arc::new(
            DFSchema::try_from(Self::summary_schema())
                .expect("write-summary schema is always valid"),
        );
        Self {
            input,
            targets,
            type_id_to_entity_name,
            dir,
            mode,
            schema,
        }
    }
}

impl_partial_ord!(GraphRemoveNode);

impl PartialEq for GraphRemoveNode {
    fn eq(&self, other: &Self) -> bool {
        self.input == other.input
            && self.targets == other.targets
            && self.dir == other.dir
            && self.mode == other.mode
            && self.type_id_to_entity_name == other.type_id_to_entity_name
    }
}

impl Eq for GraphRemoveNode {}

impl Hash for GraphRemoveNode {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.input.hash(state);
        self.targets.hash(state);
        self.dir.hash(state);
        self.mode.hash(state);
    }
}

impl UserDefinedLogicalNodeCore for GraphRemoveNode {
    fn name(&self) -> &str {
        "GraphRemove"
    }

    fn inputs(&self) -> Vec<&LogicalPlan> {
        vec![&self.input]
    }

    fn schema(&self) -> &DFSchemaRef {
        &self.schema
    }

    fn expressions(&self) -> Vec<Expr> {
        vec![]
    }

    fn fmt_for_explain(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "GraphRemove: targets={}", self.targets.len())
    }

    fn with_exprs_and_inputs(&self, _exprs: Vec<Expr>, inputs: Vec<LogicalPlan>) -> DfResult<Self> {
        let input = Arc::new(
            inputs
                .into_iter()
                .next()
                .unwrap_or_else(|| (*self.input).clone()),
        );
        Ok(Self::new(
            input,
            self.targets.clone(),
            self.type_id_to_entity_name.clone(),
            self.dir.clone(),
            self.mode,
        ))
    }
}

// ---------------------------------------------------------------------------
// UnwindNode
// ---------------------------------------------------------------------------

/// Physical node for `UNWIND` — explodes a list expression into one row per
/// element, binding each element to an alias variable.
///
/// The physical layer ([`UnwindExec`](../graphforge_exec) in M13) evaluates `list_expr`
/// per input row and emits one output row per list element (null/empty list →
/// zero rows). The output extends the input schema with a single `alias`-named,
/// `alias`-qualified column of the element type (nullable — elements may be
/// null).
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct UnwindNode {
    /// Input plan.
    pub input: Arc<LogicalPlan>,
    /// The list expression to iterate.
    pub list_expr: Expr,
    /// The alias variable qualifier bound to each element (e.g. `var_0`).
    pub alias: String,
    element_field: Arc<Field>,
    schema: DFSchemaRef,
}

impl UnwindNode {
    /// Create an unwind node.
    ///
    /// `element_field` is the unwound element's column (its type comes from the
    /// list element type, resolved by the lowerer so this crate needs no
    /// `graphforge-rel` dependency). It is appended to the input schema qualified by
    /// `alias` and forced nullable (UNWIND elements may be null).
    #[must_use]
    pub fn new(
        input: Arc<LogicalPlan>,
        list_expr: Expr,
        alias: impl Into<String>,
        element_field: &Field,
    ) -> Self {
        let alias = alias.into();
        // The element column is named after the alias and forced nullable
        // (UNWIND elements may be null); build_schema appends it unqualified so a
        // bare `RETURN x` (lowered to `col(alias)`) resolves it.
        let element_field = Arc::new(element_field.clone().with_name(&alias).with_nullable(true));
        let schema = Self::build_schema(&input, &alias, Arc::clone(&element_field));
        Self {
            input,
            list_expr,
            alias,
            element_field,
            schema,
        }
    }

    /// Build the output [`DFSchema`]: the input's qualified fields followed by
    /// the element column (already named after the alias, nullable), appended
    /// **unqualified** so a bare `UNWIND … AS x RETURN x` — which lowers `x` to
    /// `col(alias)`, an unqualified `Column` — resolves it.
    fn build_schema(
        input: &Arc<LogicalPlan>,
        alias: &str,
        element_field: Arc<Field>,
    ) -> DFSchemaRef {
        let mut qualified: Vec<(Option<TableReference>, Arc<Field>)> = input
            .schema()
            .iter()
            .map(|(q, f)| (q.cloned(), Arc::clone(f)))
            .collect();
        if let DataType::Struct(fields) = element_field.data_type()
            && fields.iter().any(|field| {
                matches!(
                    field.name().as_str(),
                    "node_uuid" | "edge_uuid" | "src_uuid" | "dst_uuid" | "nodes" | "relationships"
                )
            })
        {
            let alias_ref = TableReference::bare(alias.to_owned());
            qualified.extend(fields.iter().map(|field| {
                (
                    Some(alias_ref.clone()),
                    Arc::new(field.as_ref().clone().with_nullable(true)),
                )
            }));
        } else {
            qualified.push((None, element_field));
        }
        Arc::new(
            DFSchema::new_with_metadata(qualified, std::collections::HashMap::new())
                .expect("UnwindNode schema must include the element column"),
        )
    }

    /// Recover the appended element field from the output schema (last column),
    /// for reconstructing the node in [`with_exprs_and_inputs`].
    fn element_field(&self) -> Arc<Field> {
        Arc::clone(&self.element_field)
    }

    fn bound_element_field(&self, list_expr: &Expr, input: &LogicalPlan) -> Arc<Field> {
        match list_expr.get_type(input.schema().as_ref()) {
            Ok(
                DataType::List(field)
                | DataType::LargeList(field)
                | DataType::FixedSizeList(field, _),
            ) => Arc::new(
                field
                    .as_ref()
                    .clone()
                    .with_name(&self.alias)
                    .with_nullable(true),
            ),
            _ => self.element_field(),
        }
    }
}

impl_partial_ord!(UnwindNode);

impl UserDefinedLogicalNodeCore for UnwindNode {
    fn name(&self) -> &str {
        "Unwind"
    }

    fn inputs(&self) -> Vec<&LogicalPlan> {
        vec![&self.input]
    }

    fn schema(&self) -> &DFSchemaRef {
        &self.schema
    }

    fn expressions(&self) -> Vec<Expr> {
        vec![self.list_expr.clone()]
    }

    fn fmt_for_explain(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Unwind: alias={}", self.alias)
    }

    fn with_exprs_and_inputs(
        &self,
        mut exprs: Vec<Expr>,
        inputs: Vec<LogicalPlan>,
    ) -> DfResult<Self> {
        let list_expr = exprs.pop().unwrap_or_else(|| self.list_expr.clone());
        let input = Arc::new(
            inputs
                .into_iter()
                .next()
                .unwrap_or_else(|| (*self.input).clone()),
        );
        let element_field = self.bound_element_field(&list_expr, &input);
        Ok(Self::new(
            input,
            list_expr,
            self.alias.clone(),
            &element_field,
        ))
    }
}

// ---------------------------------------------------------------------------
// Prevent-predicate-push-down override for all stubs
// ---------------------------------------------------------------------------

// All stubs inherit the default (block all push-down) which is safe.

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use datafusion::logical_expr::{Extension, LogicalPlan};

    fn empty_plan() -> Arc<LogicalPlan> {
        use datafusion::logical_expr::LogicalPlanBuilder;
        Arc::new(LogicalPlanBuilder::empty(false).build().unwrap())
    }

    fn wrap(node: impl UserDefinedLogicalNodeCore) -> LogicalPlan {
        LogicalPlan::Extension(Extension {
            node: Arc::new(node),
        })
    }

    /// Two representative destination-node fields (mirrors the storage layer's
    /// `TOPOLOGY_NODES_SCHEMA`, kept minimal for tests).
    fn dst_node_fields() -> Vec<Arc<Field>> {
        vec![
            Arc::new(Field::new("node_id", DataType::UInt64, false)),
            Arc::new(Field::new("type_id", DataType::UInt32, false)),
        ]
    }

    /// Build a `VarLenExpandNode` with test defaults (src=0, dst=1, edge=2, Out).
    fn var_len(rel: &str, min_hops: u16, max_hops: Option<u16>) -> VarLenExpandNode {
        VarLenExpandNode::new(
            empty_plan(),
            rel,
            min_hops,
            max_hops,
            0,
            1,
            2,
            Direction::Out,
            Some(7),
            PathBuf::from("/tmp/gf"),
            OntologyMode::Strict,
            dst_node_fields(),
            var_len_edge_list_field(&[]),
        )
    }

    /// A `LogicalPlan` scan over `fields` qualified `var_<var>`, for tests that
    /// need an optional sub-plan with real columns.
    fn scan_plan(var: u32, fields: Vec<Field>) -> Arc<LogicalPlan> {
        use datafusion::logical_expr::LogicalPlanBuilder;
        use datafusion::logical_expr::logical_plan::LogicalTableSource;
        let schema = Arc::new(Schema::new(fields));
        Arc::new(
            LogicalPlanBuilder::scan(
                format!("var_{var}"),
                Arc::new(LogicalTableSource::new(schema)),
                None,
            )
            .unwrap()
            .build()
            .unwrap(),
        )
    }

    /// Build an `OptionalMatchNode` over an empty outer and the given optional
    /// sub-plan, keeping the inner columns named by `inner_keep_idx`, for tests.
    fn opt_match(
        optional: Arc<LogicalPlan>,
        join_keys: Vec<(usize, usize)>,
        inner_keep_idx: Vec<usize>,
    ) -> OptionalMatchNode {
        OptionalMatchNode::new(empty_plan(), optional, join_keys, inner_keep_idx)
    }

    /// Build an `UnwindNode` with the given list expr + alias, defaulting the
    /// element column to a nullable Int64 named `elem`.
    fn unwind(list_expr: datafusion::logical_expr::Expr, alias: &str) -> UnwindNode {
        let element_field = Field::new("elem", DataType::Int64, true);
        UnwindNode::new(empty_plan(), list_expr, alias, &element_field)
    }

    #[test]
    fn var_len_expand_name() {
        let n = var_len("KNOWS", 1, Some(3));
        assert_eq!(UserDefinedLogicalNodeCore::name(&n), "VarLenExpand");
        assert_eq!(UserDefinedLogicalNodeCore::inputs(&n).len(), 1);
    }

    #[test]
    fn var_len_expand_schema_extends_input_with_dst_and_edge_fields() {
        use datafusion::common::TableReference;
        // empty input has 0 fields; output = 0 + 2 dst fields + 1 edge-list col.
        let n = var_len("KNOWS", 1, Some(2));
        let schema = UserDefinedLogicalNodeCore::schema(&n);
        assert_eq!(schema.fields().len(), 3);
        // Destination columns are qualified `var_1` and resolvable by name.
        let dst = TableReference::bare("var_1");
        assert!(schema.field_with_qualified_name(&dst, "node_id").is_ok());
        assert!(schema.field_with_qualified_name(&dst, "type_id").is_ok());
        // The edge-list column is qualified `var_2` (edge_var) and is a List.
        let edge = TableReference::bare("var_2");
        let edge_field = schema
            .field_with_qualified_name(&edge, VAR_LEN_EDGE_LIST_FIELD)
            .expect("edge-list column present");
        assert!(matches!(edge_field.data_type(), DataType::List(_)));
    }

    #[test]
    fn optional_match_name() {
        let n = opt_match(empty_plan(), vec![], vec![]);
        assert_eq!(UserDefinedLogicalNodeCore::name(&n), "OptionalMatch");
        assert_eq!(UserDefinedLogicalNodeCore::inputs(&n).len(), 2);
    }

    #[test]
    fn optional_match_schema_appends_nullable_inner_fields() {
        // empty outer (0 fields) + an optional sub-plan with two `var_1` columns,
        // both kept, no join keys → 2 nullable output cols.
        let optional = scan_plan(
            1,
            vec![
                Field::new("node_id", DataType::UInt64, false),
                Field::new("type_id", DataType::UInt32, false),
            ],
        );
        let n = opt_match(optional, vec![], vec![0, 1]);
        let schema = UserDefinedLogicalNodeCore::schema(&n);
        assert_eq!(schema.fields().len(), 2);
        // Inner columns are made nullable for null-shaping.
        assert!(schema.field(0).is_nullable());
        assert!(schema.field(1).is_nullable());
        let var1 = TableReference::bare("var_1");
        assert!(schema.field_with_qualified_name(&var1, "node_id").is_ok());
    }

    #[test]
    fn optional_match_schema_excludes_shared_var_columns() {
        // Regression (#718): a shared variable's columns are carried by the outer
        // side, so `inner_keep_idx` excludes them — only the genuinely new inner
        // column (here index 2) is appended, avoiding duplicate `var_0` fields.
        let optional = scan_plan(
            7,
            vec![
                Field::new("node_id", DataType::UInt64, false),
                Field::new("type_id", DataType::UInt32, false),
                Field::new("payload", DataType::UInt64, false),
            ],
        );
        let n = opt_match(optional, vec![(0, 0)], vec![2]);
        let schema = UserDefinedLogicalNodeCore::schema(&n);
        assert_eq!(schema.fields().len(), 1, "only the non-shared col is kept");
        assert_eq!(schema.field(0).name(), "payload");
        assert!(schema.field(0).is_nullable());
    }

    #[test]
    fn path_unique_name() {
        let n = PathUniqueNode::new(empty_plan());
        assert_eq!(UserDefinedLogicalNodeCore::name(&n), "PathUnique");
        assert_eq!(UserDefinedLogicalNodeCore::inputs(&n).len(), 1);
    }

    #[test]
    fn ontology_infer_name() {
        let n = OntologyInferNode::new(
            empty_plan(),
            "MANAGES",
            "transitive:MANAGES",
            "conservative_min",
        );
        assert_eq!(UserDefinedLogicalNodeCore::name(&n), "OntologyInfer");
        assert_eq!(UserDefinedLogicalNodeCore::inputs(&n).len(), 1);
    }

    #[test]
    fn graph_merge_name() {
        let n = GraphMergeNode::new();
        assert_eq!(UserDefinedLogicalNodeCore::name(&n), "GraphMerge");
        assert_eq!(UserDefinedLogicalNodeCore::inputs(&n).len(), 0);
    }

    #[test]
    fn unwind_name() {
        use datafusion::logical_expr::lit;
        let n = unwind(lit(1i64), "x");
        assert_eq!(UserDefinedLogicalNodeCore::name(&n), "Unwind");
        assert_eq!(UserDefinedLogicalNodeCore::inputs(&n).len(), 1);
        assert_eq!(UserDefinedLogicalNodeCore::expressions(&n).len(), 1);
    }

    #[test]
    fn unwind_schema_appends_element_column() {
        use datafusion::logical_expr::lit;
        // empty input (0 fields) + 1 element column → 1 nullable col named `x`.
        let n = unwind(lit(1i64), "x");
        let schema = UserDefinedLogicalNodeCore::schema(&n);
        assert_eq!(schema.fields().len(), 1);
        assert!(
            schema.field(0).is_nullable(),
            "unwound element column is nullable"
        );
        // The element column is named after the alias and UNQUALIFIED, so a bare
        // `RETURN x` (lowered to `col("x")`) resolves it.
        assert_eq!(schema.field(0).name(), "x");
        assert!(schema.field_with_unqualified_name("x").is_ok());
    }

    #[test]
    fn graph_create_name_and_wrap() {
        let node = GraphCreateNode::new(
            empty_plan(),
            vec![ResolvedNodeSpec {
                var: 0,
                label_ids: vec![3],
                label_names: vec!["Person".to_owned()],
                properties: vec![("name".to_owned(), IrLiteral::Str("Alice".to_owned()))],
                computed_properties: vec![],
                is_reference: false,
            }],
            vec![],
            PathBuf::from("/tmp/gf"),
            OntologyMode::Strict,
        );
        assert_eq!(UserDefinedLogicalNodeCore::name(&node), "GraphCreate");
        // Input-driven now (one CREATE per input row).
        assert_eq!(UserDefinedLogicalNodeCore::inputs(&node).len(), 1);
        // Summary schema: nodes_created + edges_created + properties_set + labels_added.
        assert_eq!(UserDefinedLogicalNodeCore::schema(&node).fields().len(), 4);
        // Wraps as an Extension that DataFusion accepts.
        assert!(matches!(wrap(node), LogicalPlan::Extension(_)));
    }

    #[test]
    fn graph_create_eq_and_hash_are_consistent() {
        use std::collections::hash_map::DefaultHasher;

        let mk = || {
            GraphCreateNode::new(
                empty_plan(),
                vec![ResolvedNodeSpec {
                    var: 0,
                    label_ids: vec![],
                    label_names: vec![],
                    properties: vec![("score".to_owned(), IrLiteral::Float(1.5))],
                    computed_properties: vec![],
                    is_reference: false,
                }],
                vec![],
                PathBuf::from("/tmp/gf"),
                OntologyMode::Exploratory,
            )
        };
        let (a, b) = (mk(), mk());
        assert_eq!(a, b);
        let mut ha = DefaultHasher::new();
        let mut hb = DefaultHasher::new();
        a.hash(&mut ha);
        b.hash(&mut hb);
        assert_eq!(ha.finish(), hb.finish());
    }

    fn set_node(value: Expr) -> GraphSetNode {
        GraphSetNode::new(
            empty_plan(),
            vec![SetTarget {
                var: 0,
                is_edge: false,
                prop_name: "age".to_owned(),
                value,
            }],
            HashMap::new(),
            PathBuf::from("/tmp/gf"),
            OntologyMode::Exploratory,
        )
    }

    fn remove_node() -> GraphRemoveNode {
        GraphRemoveNode::new(
            empty_plan(),
            vec![RemoveTarget {
                var: 0,
                is_edge: false,
                prop_name: "age".to_owned(),
            }],
            HashMap::new(),
            PathBuf::from("/tmp/gf"),
            OntologyMode::Exploratory,
        )
    }

    #[test]
    fn graph_set_name_schema_and_expr_surface() {
        use datafusion::logical_expr::lit;
        let n = set_node(lit(42i64));
        assert_eq!(UserDefinedLogicalNodeCore::name(&n), "GraphSet");
        assert_eq!(UserDefinedLogicalNodeCore::inputs(&n).len(), 1);
        // Summary schema: properties_set.
        assert_eq!(UserDefinedLogicalNodeCore::schema(&n).fields().len(), 1);
        // The value expr is surfaced so the optimizer sees its referenced cols.
        assert_eq!(UserDefinedLogicalNodeCore::expressions(&n).len(), 1);
        assert!(matches!(wrap(n), LogicalPlan::Extension(_)));
    }

    #[test]
    fn graph_set_round_trips_value_expr() {
        use datafusion::logical_expr::{col, lit};
        // A runtime value expr (`var_0.age + 1`) must survive the expr round-trip
        // through `with_exprs_and_inputs` (used by the optimizer).
        let value = col("var_0.age") + lit(1i64);
        let n = set_node(value.clone());
        let exprs = UserDefinedLogicalNodeCore::expressions(&n);
        assert_eq!(exprs, vec![value.clone()]);
        let rebuilt = UserDefinedLogicalNodeCore::with_exprs_and_inputs(&n, exprs, vec![]).unwrap();
        assert_eq!(rebuilt.targets[0].value, value);
        assert_eq!(n, rebuilt);
    }

    #[test]
    fn graph_set_eq_distinguishes_value_expr() {
        use datafusion::logical_expr::lit;
        // Two SET nodes differing only in the value expr must NOT compare equal
        // (the value is part of logical identity).
        assert_ne!(set_node(lit(1i64)), set_node(lit(2i64)));
    }

    #[test]
    fn graph_remove_name_schema_and_no_exprs() {
        let n = remove_node();
        assert_eq!(UserDefinedLogicalNodeCore::name(&n), "GraphRemove");
        assert_eq!(UserDefinedLogicalNodeCore::inputs(&n).len(), 1);
        assert_eq!(UserDefinedLogicalNodeCore::schema(&n).fields().len(), 1);
        // REMOVE carries no value expression.
        assert!(UserDefinedLogicalNodeCore::expressions(&n).is_empty());
        assert!(matches!(wrap(n), LogicalPlan::Extension(_)));
    }

    #[test]
    fn graph_remove_eq_and_hash_are_consistent() {
        use std::collections::hash_map::DefaultHasher;
        let (a, b) = (remove_node(), remove_node());
        assert_eq!(a, b);
        let mut ha = DefaultHasher::new();
        let mut hb = DefaultHasher::new();
        a.hash(&mut ha);
        b.hash(&mut hb);
        assert_eq!(ha.finish(), hb.finish());
    }

    #[test]
    fn all_stubs_wrap_as_extension() {
        use datafusion::logical_expr::lit;
        let plans = vec![
            wrap(var_len("*", 1, None)),
            wrap(opt_match(empty_plan(), vec![], vec![])),
            wrap(PathUniqueNode::new(empty_plan())),
            wrap(OntologyInferNode::new(
                empty_plan(),
                "REL",
                "transitive:REL",
                "conservative_min",
            )),
            wrap(GraphMergeNode::new()),
            wrap(unwind(lit(1i64), "x")),
            wrap(set_node(lit(1i64))),
            wrap(remove_node()),
        ];
        for p in &plans {
            assert!(
                matches!(p, LogicalPlan::Extension(_)),
                "expected Extension, got {p:?}"
            );
        }
    }

    #[test]
    fn fmt_for_explain_is_non_empty() {
        use datafusion::logical_expr::lit;

        // Thin Display adapter that calls fmt_for_explain directly.
        struct ExplainWrapper<'a, T: UserDefinedLogicalNodeCore>(&'a T);
        impl<T: UserDefinedLogicalNodeCore> fmt::Display for ExplainWrapper<'_, T> {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                self.0.fmt_for_explain(f)
            }
        }
        fn explain<T: UserDefinedLogicalNodeCore>(n: &T) -> String {
            ExplainWrapper(n).to_string()
        }

        assert!(!explain(&var_len("KNOWS", 1, Some(3))).is_empty());
        assert!(!explain(&opt_match(empty_plan(), vec![], vec![])).is_empty());
        assert!(!explain(&PathUniqueNode::new(empty_plan())).is_empty());
        assert!(
            !explain(&OntologyInferNode::new(
                empty_plan(),
                "MANAGES",
                "transitive:MANAGES",
                "conservative_min"
            ))
            .is_empty()
        );
        assert!(!explain(&GraphMergeNode::new()).is_empty());
        assert!(!explain(&unwind(lit(1i64), "x")).is_empty());

        // Spot-check actual explain output content.
        assert!(explain(&var_len("KNOWS", 1, Some(3))).contains("KNOWS"));
        assert!(
            explain(&OntologyInferNode::new(
                empty_plan(),
                "MANAGES",
                "transitive:MANAGES",
                "conservative_min"
            ))
            .contains("MANAGES")
        );
        assert!(explain(&unwind(lit(1i64), "x")).contains("x"));
    }

    // -----------------------------------------------------------------------
    // ExpandNode (#763)
    // -----------------------------------------------------------------------

    fn expand_node(edge_prop_fields: Vec<Arc<Field>>) -> ExpandNode {
        let edge_fields = vec![
            Arc::new(Field::new(
                "edge_uuid",
                DataType::FixedSizeBinary(16),
                false,
            )),
            Arc::new(Field::new("edge_id", DataType::UInt64, false)),
            Arc::new(Field::new("src_id", DataType::UInt64, false)),
            Arc::new(Field::new("dst_id", DataType::UInt64, false)),
        ];
        ExpandNode::new(
            empty_plan(),
            "KNOWS",
            0,
            2,
            1,
            Direction::Out,
            Some(7),
            PathBuf::from("/tmp/p"),
            OntologyMode::Strict,
            edge_fields,
            edge_prop_fields,
            dst_node_fields(),
        )
    }

    #[test]
    fn expand_node_schema_order_qualifiers_and_prop_nullability() {
        use datafusion::common::TableReference;

        // A non-nullable property field on disk must come out NULLABLE
        // (LEFT-join parity with the join path).
        let node = expand_node(vec![Arc::new(Field::new("since", DataType::Int64, false))]);
        let schema = UserDefinedLogicalNodeCore::schema(&node);
        // Order: (empty input) ++ var_1 edge topology ++ var_1 props ++ var_2 dst.
        let names: Vec<String> = schema
            .iter()
            .map(|(q, f)| {
                format!(
                    "{}.{}",
                    q.map(TableReference::table).unwrap_or("?"),
                    f.name()
                )
            })
            .collect();
        assert_eq!(
            names,
            [
                "var_1.edge_uuid",
                "var_1.edge_id",
                "var_1.src_id",
                "var_1.dst_id",
                "var_1.since",
                "var_2.node_id",
                "var_2.type_id",
            ]
        );
        let edge = TableReference::bare("var_1");
        let since = schema.field_with_qualified_name(&edge, "since").unwrap();
        assert!(since.is_nullable(), "prop columns are LEFT-join nullable");
        let uuid = schema
            .field_with_qualified_name(&edge, "edge_uuid")
            .unwrap();
        assert!(!uuid.is_nullable(), "topology columns keep non-null");
        assert_eq!(node.edge_prop_count, 1);
    }

    #[test]
    fn expand_node_with_exprs_and_inputs_round_trips() {
        let node = expand_node(vec![Arc::new(Field::new("since", DataType::Int64, true))]);
        let rebuilt = UserDefinedLogicalNodeCore::with_exprs_and_inputs(
            &node,
            vec![],
            vec![(*empty_plan()).clone()],
        )
        .unwrap();
        assert_eq!(
            UserDefinedLogicalNodeCore::schema(&rebuilt).as_arrow(),
            UserDefinedLogicalNodeCore::schema(&node).as_arrow(),
            "schema survives optimizer-style input replacement"
        );
        assert_eq!(rebuilt.edge_prop_count, 1);
        assert_eq!(rebuilt.rel_type_name, "KNOWS");
    }
}