lora-database 0.2.0

LoraDB — embeddable in-memory graph database with Cypher query support.
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
//! Benchmarks for advanced query features and additional function coverage.
//!
//! Run with: `cargo bench -p lora-server --bench advanced_benchmarks`
//!
//! Categories:
//!   1. union_queries — UNION / UNION ALL
//!   2. optional_match — OPTIONAL MATCH (isolated)
//!   3. list_predicates — any(), all(), none(), single()
//!   4. string_functions — substring, split, trim, toUpper, left, right, etc.
//!   5. math_functions — ceil, floor, round, sign, pow, log, trig
//!   6. type_conversion — toInteger, toFloat, toBoolean, toString, valueType
//!   7. list_functions — head, tail, last, reverse, range, size
//!   8. path_functions — nodes(), relationships(), length() on paths
//!   9. regex_matching — =~ operator
//!  10. with_piping — multipart queries with WITH chaining
//!  11. recommendation — realistic recommendation/e-commerce workloads

mod fixtures;

use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use fixtures::*;
use lora_database::{ExecuteOptions, ResultFormat};
use std::hint::black_box;
use std::time::Duration;

fn opts() -> Option<ExecuteOptions> {
    Some(ExecuteOptions {
        format: ResultFormat::Rows,
    })
}

/// Same timing rationale as `engine_benchmarks::bench_config`: default
/// Criterion timing was an order of magnitude more than needed.
fn bench_config() -> Criterion {
    Criterion::default()
        .warm_up_time(Duration::from_millis(500))
        .measurement_time(Duration::from_millis(2_000))
        .sample_size(50)
}

// ===================================================================
// 1. UNION — UNION / UNION ALL
// ===================================================================

fn bench_union(c: &mut Criterion) {
    let mut group = c.benchmark_group("union");

    // Throughput unit: *rows feeding the UNION per query*. For the 1k benches
    // that is Scale::SMALL nodes scanned per branch × 2 branches.
    group.throughput(Throughput::Elements((Scale::SMALL as u64) * 2));

    // --- simple UNION (deduplicating) ---
    {
        let db = build_node_graph(Scale::SMALL);
        group.bench_function("union_two_queries_1k", |b| {
            b.iter(|| {
                black_box(
                    db.service
                        .execute(
                            "MATCH (n:Node) WHERE n.value < 30 RETURN n.id AS id \
                             UNION \
                             MATCH (n:Node) WHERE n.value > 70 RETURN n.id AS id",
                            opts(),
                        )
                        .unwrap(),
                );
            });
        });
    }

    // --- UNION ALL (no dedup) ---
    {
        let db = build_node_graph(Scale::SMALL);
        group.bench_function("union_all_two_queries_1k", |b| {
            b.iter(|| {
                black_box(
                    db.service
                        .execute(
                            "MATCH (n:Node) WHERE n.value < 30 RETURN n.id AS id \
                             UNION ALL \
                             MATCH (n:Node) WHERE n.value > 70 RETURN n.id AS id",
                            opts(),
                        )
                        .unwrap(),
                );
            });
        });
    }

    // --- UNION with different labels ---
    // Org fixture has 6 Person + 3 City + 2 Project = 11 rows unioned.
    {
        let db = build_org_graph();
        group.throughput(Throughput::Elements(9));
        group.bench_function("union_different_labels", |b| {
            b.iter(|| {
                black_box(
                    db.service
                        .execute(
                            "MATCH (p:Person) RETURN p.name AS name \
                             UNION \
                             MATCH (c:City) RETURN c.name AS name",
                            opts(),
                        )
                        .unwrap(),
                );
            });
        });
    }

    // --- triple UNION ---
    // 6 Person + 3 City + 2 Project = 11 rows across 3 branches.
    {
        let db = build_org_graph();
        group.throughput(Throughput::Elements(11));
        group.bench_function("union_triple", |b| {
            b.iter(|| {
                black_box(
                    db.service
                        .execute(
                            "MATCH (p:Person) RETURN p.name AS name, 'person' AS kind \
                             UNION ALL \
                             MATCH (c:City) RETURN c.name AS name, 'city' AS kind \
                             UNION ALL \
                             MATCH (pr:Project) RETURN pr.name AS name, 'project' AS kind",
                            opts(),
                        )
                        .unwrap(),
                );
            });
        });
    }

    group.finish();
}

// ===================================================================
// 2. OPTIONAL MATCH — isolated benchmarks
// ===================================================================

fn bench_optional_match(c: &mut Criterion) {
    let mut group = c.benchmark_group("optional_match");

    // Throughput unit: *rows produced by the outer MATCH*. OPTIONAL MATCH
    // scales with that outer cardinality.

    // --- OPTIONAL MATCH where most match ---
    // 200 Person nodes in the outer MATCH.
    {
        let db = build_social_graph(200, 4);
        group.throughput(Throughput::Elements(200));
        group.bench_function("optional_mostly_matched_200", |b| {
            b.iter(|| {
                black_box(
                    db.service
                        .execute(
                            "MATCH (p:Person) \
                             OPTIONAL MATCH (p)-[:KNOWS]->(f:Person) \
                             RETURN p.id, count(f) AS friends",
                            opts(),
                        )
                        .unwrap(),
                );
            });
        });
    }

    // --- OPTIONAL MATCH where many are null ---
    // 6 Person nodes from the org graph.
    {
        let db = build_org_graph();
        group.throughput(Throughput::Elements(6));
        group.bench_function("optional_sparse_match", |b| {
            b.iter(|| {
                black_box(
                    db.service
                        .execute(
                            "MATCH (p:Person) \
                             OPTIONAL MATCH (p)-[:MANAGES]->(sub:Person) \
                             RETURN p.name, sub.name",
                            opts(),
                        )
                        .unwrap(),
                );
            });
        });
    }

    // --- multiple OPTIONAL MATCH ---
    {
        let db = build_org_graph();
        group.throughput(Throughput::Elements(6));
        group.bench_function("double_optional_match", |b| {
            b.iter(|| {
                black_box(
                    db.service
                        .execute(
                            "MATCH (p:Person) \
                             OPTIONAL MATCH (p)-[:MANAGES]->(sub:Person) \
                             OPTIONAL MATCH (p)-[:ASSIGNED_TO]->(pr:Project) \
                             RETURN p.name, sub.name, pr.name",
                            opts(),
                        )
                        .unwrap(),
                );
            });
        });
    }

    // --- OPTIONAL MATCH at scale ---
    // Dropped the 1k tier: OPTIONAL MATCH over KNOWS gets expensive per-iter
    // and the 200/500 pair already captures the scaling trend.
    for &size in &[200usize, 500] {
        let db = build_social_graph(size, 4);
        group.throughput(Throughput::Elements(size as u64));
        group.bench_with_input(
            BenchmarkId::new("optional_match_scale", size),
            &size,
            |b, _| {
                b.iter(|| {
                    black_box(
                        db.service
                            .execute(
                                "MATCH (p:Person) \
                                 OPTIONAL MATCH (p)-[:KNOWS]->(f:Person) \
                                 WHERE f.age > 40 \
                                 RETURN p.id, collect(f.id) AS older_friends",
                                opts(),
                            )
                            .unwrap(),
                    );
                });
            },
        );
    }

    group.finish();
}

// ===================================================================
// 3. LIST PREDICATES — any(), all(), none(), single()
// ===================================================================

fn bench_list_predicates(c: &mut Criterion) {
    let mut group = c.benchmark_group("list_predicates");

    // Throughput unit: *list elements evaluated per iteration*. The small
    // literal-list benches use the list length; graph-backed benches use
    // the row count.

    // --- any() predicate ---  5 items
    group.throughput(Throughput::Elements(5));
    group.bench_function("any_in_list", |b| {
        let db = BenchDb::new();
        b.iter(|| {
            black_box(
                db.service
                    .execute(
                        "WITH [1, 2, 3, 4, 5] AS nums \
                         RETURN any(x IN nums WHERE x > 3) AS has_large",
                        opts(),
                    )
                    .unwrap(),
            );
        });
    });

    // --- all() predicate --- 4 items
    group.throughput(Throughput::Elements(4));
    group.bench_function("all_in_list", |b| {
        let db = BenchDb::new();
        b.iter(|| {
            black_box(
                db.service
                    .execute(
                        "WITH [2, 4, 6, 8] AS nums \
                         RETURN all(x IN nums WHERE x % 2 = 0) AS all_even",
                        opts(),
                    )
                    .unwrap(),
            );
        });
    });

    // --- none() predicate --- 4 items
    group.throughput(Throughput::Elements(4));
    group.bench_function("none_in_list", |b| {
        let db = BenchDb::new();
        b.iter(|| {
            black_box(
                db.service
                    .execute(
                        "WITH [1, 3, 5, 7] AS nums \
                         RETURN none(x IN nums WHERE x % 2 = 0) AS no_even",
                        opts(),
                    )
                    .unwrap(),
            );
        });
    });

    // --- single() predicate --- 5 items
    group.throughput(Throughput::Elements(5));
    group.bench_function("single_in_list", |b| {
        let db = BenchDb::new();
        b.iter(|| {
            black_box(
                db.service
                    .execute(
                        "WITH [1, 2, 3, 4, 5] AS nums \
                         RETURN single(x IN nums WHERE x = 3) AS exactly_one",
                        opts(),
                    )
                    .unwrap(),
            );
        });
    });

    // --- list predicates on graph data ---
    // Aggregation runs over 200 Person nodes.
    {
        let db = build_social_graph(200, 4);
        group.throughput(Throughput::Elements(200));
        group.bench_function("any_on_graph_200", |b| {
            b.iter(|| {
                black_box(
                    db.service
                        .execute(
                            "MATCH (p:Person)-[:KNOWS]->(f:Person) \
                             WITH p, collect(f.age) AS friend_ages \
                             WHERE any(a IN friend_ages WHERE a > 50) \
                             RETURN p.id, p.name",
                            opts(),
                        )
                        .unwrap(),
                );
            });
        });
    }

    // --- list comprehension with filter --- 200 elements iterated
    group.throughput(Throughput::Elements(200));
    group.bench_function("comprehension_filter_transform", |b| {
        let db = BenchDb::new();
        b.iter(|| {
            black_box(
                db.service
                    .execute(
                        "WITH range(1, 200) AS nums \
                         RETURN [x IN nums WHERE x % 3 = 0 | x * x] AS squares",
                        opts(),
                    )
                    .unwrap(),
            );
        });
    });

    // --- reduce over large list ---
    // 100/500 are enough to show the linear trend; 1k doubled the runtime
    // without adding a new data point.
    for &size in &[100usize, 500] {
        group.throughput(Throughput::Elements(size as u64));
        group.bench_with_input(BenchmarkId::new("reduce_sum", size), &size, |b, &size| {
            let db = BenchDb::new();
            let q = format!("RETURN reduce(acc = 0, x IN range(1, {size}) | acc + x) AS total");
            b.iter(|| {
                black_box(db.service.execute(&q, opts()).unwrap());
            });
        });
    }

    group.finish();
}

// ===================================================================
// 4. STRING FUNCTIONS — extended coverage
// ===================================================================

fn bench_string_functions(c: &mut Criterion) {
    let mut group = c.benchmark_group("string_functions");

    // Throughput unit: *one function-evaluation query per iteration*. The
    // `*_on_graph` bench at the end overrides this with the row count.
    group.throughput(Throughput::Elements(1));

    let db = BenchDb::new();

    group.bench_function("toUpper", |b| {
        b.iter(|| {
            black_box(
                db.service
                    .execute("RETURN toUpper('hello world') AS r", opts())
                    .unwrap(),
            );
        });
    });

    group.bench_function("trim", |b| {
        b.iter(|| {
            black_box(
                db.service
                    .execute("RETURN trim('  hello  ') AS r", opts())
                    .unwrap(),
            );
        });
    });

    group.bench_function("ltrim_rtrim", |b| {
        b.iter(|| {
            black_box(
                db.service
                    .execute(
                        "RETURN ltrim('  hello') AS l, rtrim('hello  ') AS r",
                        opts(),
                    )
                    .unwrap(),
            );
        });
    });

    group.bench_function("substring", |b| {
        b.iter(|| {
            black_box(
                db.service
                    .execute("RETURN substring('hello world', 6, 5) AS r", opts())
                    .unwrap(),
            );
        });
    });

    group.bench_function("split", |b| {
        b.iter(|| {
            black_box(
                db.service
                    .execute("RETURN split('a,b,c,d,e', ',') AS parts", opts())
                    .unwrap(),
            );
        });
    });

    group.bench_function("left_right", |b| {
        b.iter(|| {
            black_box(
                db.service
                    .execute(
                        "RETURN left('hello world', 5) AS l, right('hello world', 5) AS r",
                        opts(),
                    )
                    .unwrap(),
            );
        });
    });

    group.bench_function("reverse_string", |b| {
        b.iter(|| {
            black_box(
                db.service
                    .execute("RETURN reverse('abcdefghij') AS r", opts())
                    .unwrap(),
            );
        });
    });

    group.bench_function("char_length", |b| {
        b.iter(|| {
            black_box(
                db.service
                    .execute("RETURN char_length('hello world') AS len", opts())
                    .unwrap(),
            );
        });
    });

    // --- string functions on graph data --- 100 function evaluations/query
    {
        let db_graph = build_node_graph(Scale::TINY);
        group.throughput(Throughput::Elements(Scale::TINY as u64));
        group.bench_function("string_pipeline_100_nodes", |b| {
            b.iter(|| {
                black_box(
                    db_graph
                        .service
                        .execute(
                            "MATCH (n:Node) \
                             RETURN toUpper(n.name) AS upper, \
                                    substring(n.name, 0, 4) AS prefix, \
                                    size(n.name) AS len",
                            opts(),
                        )
                        .unwrap(),
                );
            });
        });
    }

    group.finish();
}

// ===================================================================
// 5. MATH FUNCTIONS — extended coverage including trig
// ===================================================================

fn bench_math_functions(c: &mut Criterion) {
    let mut group = c.benchmark_group("math_functions");

    // Throughput unit: *one function-evaluation query per iteration*. The
    // `*_on_graph` bench overrides this with the row count.
    group.throughput(Throughput::Elements(1));

    let db = BenchDb::new();

    // --- rounding functions ---
    group.bench_function("ceil_floor_round", |b| {
        b.iter(|| {
            black_box(
                db.service
                    .execute(
                        "RETURN ceil(3.14) AS c, floor(3.14) AS f, round(3.5) AS r",
                        opts(),
                    )
                    .unwrap(),
            );
        });
    });

    group.bench_function("sign", |b| {
        b.iter(|| {
            black_box(
                db.service
                    .execute(
                        "RETURN sign(-42) AS neg, sign(0) AS zero, sign(42) AS pos",
                        opts(),
                    )
                    .unwrap(),
            );
        });
    });

    group.bench_function("exp_log", |b| {
        b.iter(|| {
            black_box(
                db.service
                    .execute(
                        "RETURN exp(1) AS e, log(10) AS ln, log10(100) AS lg",
                        opts(),
                    )
                    .unwrap(),
            );
        });
    });

    // --- trig functions ---
    group.bench_function("trig_sin_cos_tan", |b| {
        b.iter(|| {
            black_box(
                db.service
                    .execute("RETURN sin(1.0) AS s, cos(1.0) AS c, tan(0.5) AS t", opts())
                    .unwrap(),
            );
        });
    });

    group.bench_function("trig_inverse", |b| {
        b.iter(|| {
            black_box(
                db.service
                    .execute(
                        "RETURN asin(0.5) AS as, acos(0.5) AS ac, atan(1.0) AS at, atan2(1.0, 1.0) AS at2",
                        opts(),
                    )
                    .unwrap(),
            );
        });
    });

    group.bench_function("degrees_radians", |b| {
        b.iter(|| {
            black_box(
                db.service
                    .execute(
                        "RETURN degrees(pi()) AS deg, radians(180) AS rad, pi() AS pi, e() AS euler",
                        opts(),
                    )
                    .unwrap(),
            );
        });
    });

    // --- math on graph data --- 100 function evaluations/query
    {
        let db_graph = build_node_graph(Scale::TINY);
        group.throughput(Throughput::Elements(Scale::TINY as u64));
        group.bench_function("math_pipeline_100_nodes", |b| {
            b.iter(|| {
                black_box(
                    db_graph
                        .service
                        .execute(
                            "MATCH (n:Node) \
                             RETURN n.id, \
                                    ceil(toFloat(n.value) / 3.0) AS bucket, \
                                    sqrt(toFloat(n.value)) AS root, \
                                    sign(n.value - 50) AS half",
                            opts(),
                        )
                        .unwrap(),
                );
            });
        });
    }

    group.finish();
}

// ===================================================================
// 6. TYPE CONVERSION — toInteger, toFloat, toBoolean, toString, valueType
// ===================================================================

fn bench_type_conversion(c: &mut Criterion) {
    let mut group = c.benchmark_group("type_conversion");

    // Throughput unit: *one conversion query per iteration* for scalar
    // benches, 100 rows for the graph bench.
    group.throughput(Throughput::Elements(1));

    let db = BenchDb::new();

    group.bench_function("toInteger_from_string", |b| {
        b.iter(|| {
            black_box(
                db.service
                    .execute("RETURN toInteger('42') AS i", opts())
                    .unwrap(),
            );
        });
    });

    group.bench_function("toFloat_from_string", |b| {
        b.iter(|| {
            black_box(
                db.service
                    .execute("RETURN toFloat('3.14') AS f", opts())
                    .unwrap(),
            );
        });
    });

    group.bench_function("toBoolean_from_string", |b| {
        b.iter(|| {
            black_box(
                db.service
                    .execute("RETURN toBoolean('true') AS b", opts())
                    .unwrap(),
            );
        });
    });

    group.bench_function("toString_from_int", |b| {
        b.iter(|| {
            black_box(
                db.service
                    .execute("RETURN toString(42) AS s", opts())
                    .unwrap(),
            );
        });
    });

    group.bench_function("valueType", |b| {
        b.iter(|| {
            black_box(
                db.service
                    .execute(
                        "RETURN valueType(42) AS t1, valueType('hello') AS t2, \
                                valueType(3.14) AS t3, valueType(true) AS t4",
                        opts(),
                    )
                    .unwrap(),
            );
        });
    });

    // --- type conversion on graph data --- 100 conversions/query
    {
        let db_graph = build_node_graph(Scale::TINY);
        group.throughput(Throughput::Elements(Scale::TINY as u64));
        group.bench_function("conversions_on_100_nodes", |b| {
            b.iter(|| {
                black_box(
                    db_graph
                        .service
                        .execute(
                            "MATCH (n:Node) \
                             RETURN toString(n.id) AS sid, \
                                    toFloat(n.value) AS fval",
                            opts(),
                        )
                        .unwrap(),
                );
            });
        });
    }

    group.finish();
}

// ===================================================================
// 7. LIST FUNCTIONS — head, tail, last, reverse, range, size
// ===================================================================

fn bench_list_functions(c: &mut Criterion) {
    let mut group = c.benchmark_group("list_functions");

    // Throughput unit: *list elements touched per iteration*.

    let db = BenchDb::new();

    // head/tail/last over 5 elements.
    group.throughput(Throughput::Elements(5));
    group.bench_function("head_tail_last", |b| {
        b.iter(|| {
            black_box(
                db.service
                    .execute(
                        "WITH [1, 2, 3, 4, 5] AS lst \
                         RETURN head(lst) AS h, tail(lst) AS t, last(lst) AS l",
                        opts(),
                    )
                    .unwrap(),
            );
        });
    });

    // reverse of 100-element list.
    group.throughput(Throughput::Elements(100));
    group.bench_function("reverse_list", |b| {
        b.iter(|| {
            black_box(
                db.service
                    .execute("RETURN reverse(range(1, 100)) AS rev", opts())
                    .unwrap(),
            );
        });
    });

    // range generates 1000 elements (twice, size() is O(1) on Vec).
    group.throughput(Throughput::Elements(1000));
    group.bench_function("range_generation", |b| {
        b.iter(|| {
            black_box(
                db.service
                    .execute(
                        "RETURN range(1, 1000) AS nums, size(range(1, 1000)) AS len",
                        opts(),
                    )
                    .unwrap(),
            );
        });
    });

    // 0..=100 step 3 → 34 elements.
    group.throughput(Throughput::Elements(34));
    group.bench_function("range_with_step", |b| {
        b.iter(|| {
            black_box(
                db.service
                    .execute("RETURN range(0, 100, 3) AS nums", opts())
                    .unwrap(),
            );
        });
    });

    // size() of a 500-element list (generation dominates cost).
    group.throughput(Throughput::Elements(500));
    group.bench_function("size_of_list", |b| {
        b.iter(|| {
            black_box(
                db.service
                    .execute("WITH range(1, 500) AS lst RETURN size(lst) AS s", opts())
                    .unwrap(),
            );
        });
    });

    // --- nested list operations --- 50 elements built then reversed.
    group.throughput(Throughput::Elements(50));
    group.bench_function("nested_list_ops", |b| {
        b.iter(|| {
            black_box(
                db.service
                    .execute(
                        "WITH range(1, 50) AS nums \
                         RETURN head(tail(reverse(nums))) AS second_to_last, \
                                size(nums) AS len",
                        opts(),
                    )
                    .unwrap(),
            );
        });
    });

    group.finish();
}

// ===================================================================
// 8. PATH FUNCTIONS — nodes(), relationships(), length() on paths
// ===================================================================

fn bench_path_functions(c: &mut Criterion) {
    let mut group = c.benchmark_group("path_functions");

    // Throughput unit: *paths produced per iteration*.

    // --- nodes() on path --- bounded *1..5 → 5 paths from idx=0
    {
        let db = build_chain(100);
        group.throughput(Throughput::Elements(5));
        group.bench_function("nodes_on_path_chain_100", |b| {
            b.iter(|| {
                black_box(
                    db.service
                        .execute(
                            "MATCH p = (a:Chain {idx:0})-[:NEXT*1..5]->(b) \
                             RETURN nodes(p) AS path_nodes, length(p) AS len",
                            opts(),
                        )
                        .unwrap(),
                );
            });
        });
    }

    // --- relationships() on path ---
    {
        let db = build_chain(100);
        group.throughput(Throughput::Elements(5));
        group.bench_function("relationships_on_path_chain_100", |b| {
            b.iter(|| {
                black_box(
                    db.service
                        .execute(
                            "MATCH p = (a:Chain {idx:0})-[:NEXT*1..5]->(b) \
                             RETURN relationships(p) AS rels, length(p) AS len",
                            opts(),
                        )
                        .unwrap(),
                );
            });
        });
    }

    // --- path extraction on social graph --- LIMIT 50 caps result paths.
    {
        let db = build_social_graph(200, 4);
        group.throughput(Throughput::Elements(50));
        group.bench_function("path_extract_social_200", |b| {
            b.iter(|| {
                black_box(
                    db.service
                        .execute(
                            "MATCH p = (a:Person {id:0})-[:KNOWS*1..3]->(b) \
                             RETURN length(p) AS len, size(nodes(p)) AS node_count \
                             LIMIT 50",
                            opts(),
                        )
                        .unwrap(),
                );
            });
        });
    }

    group.finish();
}

// ===================================================================
// 9. REGEX MATCHING — =~ operator
// ===================================================================

fn bench_regex_matching(c: &mut Criterion) {
    let mut group = c.benchmark_group("regex");

    // Throughput unit: *regex matches evaluated per iteration*. Literal bench
    // evaluates exactly one; graph benches evaluate one per scanned node.
    group.throughput(Throughput::Elements(1));

    let db_empty = BenchDb::new();
    let db_small = build_node_graph(Scale::SMALL);

    group.bench_function("regex_simple_literal", |b| {
        b.iter(|| {
            black_box(
                db_empty
                    .service
                    .execute(
                        "WITH 'Hello World 123' AS s \
                         RETURN s =~ '.*World.*' AS matches",
                        opts(),
                    )
                    .unwrap(),
            );
        });
    });

    group.throughput(Throughput::Elements(Scale::SMALL as u64));
    group.bench_function("regex_filter_1k", |b| {
        b.iter(|| {
            black_box(
                db_small
                    .service
                    .execute(
                        "MATCH (n:Node) WHERE n.name =~ 'node_[5-9].*' RETURN n.id",
                        opts(),
                    )
                    .unwrap(),
            );
        });
    });

    group.throughput(Throughput::Elements(Scale::SMALL as u64));
    group.bench_function("regex_complex_pattern_1k", |b| {
        b.iter(|| {
            black_box(
                db_small
                    .service
                    .execute(
                        "MATCH (n:Node) WHERE n.name =~ 'node_[0-9]{2,3}' RETURN n.id, n.name",
                        opts(),
                    )
                    .unwrap(),
            );
        });
    });

    group.finish();
}

// ===================================================================
// 10. WITH PIPING — multi-part queries with WITH chaining
// ===================================================================

fn bench_with_piping(c: &mut Criterion) {
    let mut group = c.benchmark_group("with_piping");

    // Throughput unit: *rows flowing through the WITH pipeline per query*.

    let db_small = build_node_graph(Scale::SMALL);
    let db_social = build_social_graph(200, 4);

    group.throughput(Throughput::Elements(Scale::SMALL as u64));
    group.bench_function("with_passthrough_1k", |b| {
        b.iter(|| {
            black_box(
                db_small
                    .service
                    .execute(
                        "MATCH (n:Node) \
                         WITH n.id AS id, n.value AS val \
                         RETURN id, val",
                        opts(),
                    )
                    .unwrap(),
            );
        });
    });

    // 200 Person nodes × ~4 KNOWS = 800 edges feeding the aggregator.
    group.throughput(Throughput::Elements(800));
    group.bench_function("with_agg_then_match_200", |b| {
        b.iter(|| {
            black_box(
                db_social
                    .service
                    .execute(
                        "MATCH (p:Person)-[:KNOWS]->(f:Person) \
                         WITH p, count(f) AS friend_count \
                         WHERE friend_count > 2 \
                         RETURN p.name, friend_count \
                         ORDER BY friend_count DESC LIMIT 10",
                        opts(),
                    )
                    .unwrap(),
            );
        });
    });

    group.throughput(Throughput::Elements(800));
    group.bench_function("with_triple_chain_200", |b| {
        b.iter(|| {
            black_box(
                db_social
                    .service
                    .execute(
                        "MATCH (p:Person)-[:KNOWS]->(f:Person) \
                         WITH p, count(f) AS cnt \
                         WITH p.city AS city, sum(cnt) AS total_friends \
                         RETURN city, total_friends \
                         ORDER BY total_friends DESC",
                        opts(),
                    )
                    .unwrap(),
            );
        });
    });

    group.throughput(Throughput::Elements(Scale::SMALL as u64));
    group.bench_function("with_top_n_pattern_1k", |b| {
        b.iter(|| {
            black_box(
                db_small
                    .service
                    .execute(
                        "MATCH (n:Node) \
                         WITH n ORDER BY n.value DESC LIMIT 50 \
                         RETURN n.id, n.name, n.value",
                        opts(),
                    )
                    .unwrap(),
            );
        });
    });

    group.finish();
}

// ===================================================================
// 11. RECOMMENDATION — realistic e-commerce workloads
// ===================================================================

fn bench_recommendation(c: &mut Criterion) {
    let mut group = c.benchmark_group("recommendation");
    group.sample_size(40);

    // Throughput unit: *one realistic recommendation query per iteration*.
    // These combine joins/aggregations/sorts over the 200×100 fixture, so
    // "queries per second" is the most honest figure.
    group.throughput(Throughput::Elements(1));

    // Previously this group built `build_recommendation_graph(200, 100)` six
    // separate times — each one a multi-pass UNWIND sequence that dominated
    // the group's setup cost. Build once and reuse.
    let db = build_recommendation_graph(200, 100);

    group.bench_function("user_purchases_200u_100p", |b| {
        b.iter(|| {
            black_box(
                db.service
                    .execute(
                        "MATCH (u:User {id: 0})-[:BOUGHT]->(p:Product) \
                         RETURN p.name, p.price, p.category",
                        opts(),
                    )
                    .unwrap(),
            );
        });
    });

    group.bench_function("common_buyers_200u_100p", |b| {
        b.iter(|| {
            black_box(
                db.service
                    .execute(
                        "MATCH (u1:User {id: 0})-[:BOUGHT]->(p:Product)<-[:BOUGHT]-(u2:User) \
                         WHERE u2.id <> 0 \
                         RETURN DISTINCT u2.id, u2.name",
                        opts(),
                    )
                    .unwrap(),
            );
        });
    });

    group.bench_function("collab_filter_200u_100p", |b| {
        b.iter(|| {
            black_box(
                db.service
                    .execute(
                        "MATCH (u1:User {id: 0})-[:BOUGHT]->(p:Product)<-[:BOUGHT]-(u2:User)-[:BOUGHT]->(rec:Product) \
                         WHERE u2.id <> 0 \
                         RETURN rec.name, rec.category, count(DISTINCT u2) AS score \
                         ORDER BY score DESC LIMIT 10",
                        opts(),
                    )
                    .unwrap(),
            );
        });
    });

    group.bench_function("avg_rating_200u_100p", |b| {
        b.iter(|| {
            black_box(
                db.service
                    .execute(
                        "MATCH (u:User)-[r:REVIEWED]->(p:Product) \
                         RETURN p.name, p.category, avg(r.rating) AS avg_rating, count(r) AS reviews \
                         ORDER BY avg_rating DESC LIMIT 20",
                        opts(),
                    )
                    .unwrap(),
            );
        });
    });

    group.bench_function("spending_by_tier_200u_100p", |b| {
        b.iter(|| {
            black_box(
                db.service
                    .execute(
                        "MATCH (u:User)-[b:BOUGHT]->(p:Product) \
                         RETURN u.tier, count(DISTINCT u) AS users, \
                                sum(p.price * b.quantity) AS total_spend \
                         ORDER BY total_spend DESC",
                        opts(),
                    )
                    .unwrap(),
            );
        });
    });

    group.bench_function("similar_products_200u_100p", |b| {
        b.iter(|| {
            black_box(
                db.service
                    .execute(
                        "MATCH (p:Product {id: 0})-[:SIMILAR_TO]->(similar:Product) \
                         RETURN similar.name, similar.price, similar.category",
                        opts(),
                    )
                    .unwrap(),
            );
        });
    });

    // Larger dataset retained for top_categories — this is the one bench that
    // benefits from the extra breadth. Kept at a single build.
    let db_large = build_recommendation_graph(400, 150);
    group.bench_function("top_categories_400u_150p", |b| {
        b.iter(|| {
            black_box(
                db_large
                    .service
                    .execute(
                        "MATCH (u:User)-[b:BOUGHT]->(p:Product) \
                         RETURN p.category, \
                                count(b) AS purchases, \
                                sum(p.price * b.quantity) AS revenue \
                         ORDER BY revenue DESC",
                        opts(),
                    )
                    .unwrap(),
            );
        });
    });

    group.finish();
}

// ===================================================================
// Criterion harness
// ===================================================================

criterion_group! {
    name = benches;
    config = bench_config();
    targets =
        bench_union,
        bench_optional_match,
        bench_list_predicates,
        bench_string_functions,
        bench_math_functions,
        bench_type_conversion,
        bench_list_functions,
        bench_path_functions,
        bench_regex_matching,
        bench_with_piping,
        bench_recommendation,
}
criterion_main!(benches);