graphersal 0.1.1

In-memory graph traversal engine modelled on Apache TinkerPop Gremlin, with a Rhai DSL, optimizer and GraphSON/GraphML I/O
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
use crate::graph::GraphStorage;
use crate::*;
use fnv::FnvBuildHasher;
use indexmap::IndexMap;
use indexmap::map::Entry;

/// `InvalidModulator` reasons specific to `group()`/`group_count()`; `help()` keys on them.
pub(crate) mod reason {
    /// A sort direction, not a projection: `group()`'s `by()` picks a key or a value, it does not
    /// order one.
    pub(crate) const ORDER: &str = "an order is not a projection this step can use";
    /// A second `by()` on `group_count()`: it counts per key, so it has no value projection.
    pub(crate) const TOO_MANY: &str = "group_count() takes a single by()";
}

/// Resolves the grouping key of one traverser.
///
/// A lazy property handle (`VertexProperty`/`EdgeProperty`, e.g. after `values("lang")`) is keyed
/// by the value it points at, not by the handle: the handle carries the element it was read from,
/// so two elements holding the same value would otherwise form two groups that then collide when
/// the result is rendered as a map (`java: 1` instead of `java: 2`). Group is a materializing
/// barrier, and a property value is cloned once per traverser only here.
pub(crate) fn group_key<'g, S: GraphStorage + 'g>(
    context: &mut TraverserContext<'_, S>,
    traverser: &Traverser<S>,
    traverser_value: &TraverserValue<S>,
    by: &By<'g, S>,
) -> TraverserResult<Option<TraverserValue<S>>> {
    let key = TraverserValue::fetch_traverser_value_by(context, traverser, traverser_value, by)?;
    Ok(match key {
        Some(
            handle @ (TraverserValue::VertexProperty(_, _, _)
            | TraverserValue::EdgeProperty(_, _, _)),
        ) => Some(handle.into_node_property(context.storage)?.into()),
        other => other,
    })
}

pub(crate) fn bulk_overflow(step: &str) -> Box<TraverserError> {
    Box::new(TraverserError::BulkOverflow {
        step: step.to_string(),
    })
}

/// Appends `value` to `bucket` `n` times (one per unit of the traverser's bulk). The caller has
/// already checked the step's total against `bulk.max_expansion`.
pub(crate) fn push_n<S: GraphStorage>(
    context: &mut TraverserContext<'_, S>,
    bucket: &mut Vec<TraverserValue<S>>,
    value: TraverserValue<S>,
    n: u64,
    step: &str,
) -> TraverserResult<()> {
    if n > 1 {
        bulk::ExpansionLimits::of(&context.options).check_copies(&value, n, step)?;
    }
    bulk::push_copies(bucket, value, n, &mut context.deadline)
}

/// `group()` / `group_count()`. `group_count()` takes a single `by()`; a second one is
/// `InvalidModulator` (user's guide: "The by() Modulator: Single-by Steps"). `group()` itself takes
/// the key `by()` and the value `by()`.
struct StepGroup<'g, S: GraphStorage + 'g> {
    by: Option<Vec<By<'g, S>>>,
    group_count: bool,
    by_check: ByModulatorCheck,
}

impl<'g, S: GraphStorage + 'g> Default for StepGroup<'g, S> {
    fn default() -> Self {
        Self {
            by: None,
            group_count: false,
            by_check: ByModulatorCheck::default(),
        }
    }
}

impl<'g, S: GraphStorage + 'g> Clone for StepGroup<'g, S> {
    fn clone(&self) -> Self {
        Self {
            by: self.by.clone(),
            group_count: self.group_count,
            by_check: self.by_check.clone(),
        }
    }
}

impl<'g, S: GraphStorage + 'g> fmt::Display for StepGroup<'g, S> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut by = "".to_string();
        if let Some(by_list) = self.by.as_ref() {
            by_list
                .iter()
                .for_each(|b| by.push_str(&crate::traversal::render_by(b)));
        }
        let name = if self.group_count {
            "group_count"
        } else {
            "group"
        };
        write!(f, "{}(){}", name, by)
    }
}

impl<'g, S: GraphStorage + 'g> GraphTraversalSource<'g, S> {
    /// Groups the stream into a map: key by the first by(), values by the second by() (default: the
    /// elements).
    pub fn group(self) -> Self {
        self.push_step(GraphStep::Barrier("group", Box::new(StepGroup::default())))
    }

    /// Counts the stream per key (the first by(), default the element) into a map.
    pub fn group_count(self) -> Self {
        self.push_step(GraphStep::Barrier(
            "group_count",
            Box::new(StepGroup {
                group_count: true,
                ..Default::default()
            }),
        ))
    }
}

impl<'g, S: GraphStorage + 'g> AnonymousTraversal<'g, S> {
    /// Groups the stream into a map: key by the first by(), values by the second by() (default: the
    /// elements).
    pub fn group(self) -> Self {
        let step = GraphStep::Barrier("group", Box::new(StepGroup::default()));
        self.push_step(step)
    }

    /// Counts the stream per key (the first by(), default the element) into a map.
    pub fn group_count(self) -> Self {
        let step = GraphStep::Barrier(
            "group_count",
            Box::new(StepGroup {
                group_count: true,
                ..Default::default()
            }),
        );
        self.push_step(step)
    }
}

/// Whether a value traversal holds a barrier at its top level, TinkerPop's
/// `Grouping.determineBarrierStep()`: an aggregating step (`fold()`, `count()`, `sum()`,
/// `group()`, `cap()`, ...) or a collecting/filtering barrier (`order()`, `dedup()`,
/// `limit()`/`range()`/`skip()`/`tail()`). `repeat()` and the list filters `all()`/`any()`/`none()`
/// are `FlatBarrier`s here but not barriers in TinkerPop.
pub(crate) fn has_barrier<'g, S: GraphStorage + 'g>(traversal: &AnonymousTraversal<'g, S>) -> bool {
    traversal.steps.iter().any(|step| match step {
        GraphStep::Barrier(_, _) | GraphStep::Count(_, _) => true,
        GraphStep::FlatBarrier(name, _) => {
            matches!(
                *name,
                "dedup" | "order" | "range" | "limit" | "skip" | "tail"
            )
        }
        GraphStep::Init(_, _)
        | GraphStep::Append(_, _)
        | GraphStep::Map(_, _)
        | GraphStep::FlatMap(_, _)
        | GraphStep::Filter(_, _)
        | GraphStep::SideEffect(_, _)
        | GraphStep::SideEffectMut(_, _)
        | GraphStep::Modulator(_, _)
        | GraphStep::As(_, _) => false,
    })
}

fn is_count_traversal<'g, S: GraphStorage + 'g>(traversal: &AnonymousTraversal<'g, S>) -> bool {
    traversal.steps.len() == 1 && matches!(traversal.steps.first(), Some(GraphStep::Count(_, _)))
}

/// Whether a value traversal is the bare identity, the lowering of a bare `by()`: TinkerPop's
/// `GroupStep.convertValueTraversal` turns it into `map(identity).fold()`, so the key collects
/// its members as a list instead of keeping the last one.
fn is_identity_traversal<'g, S: GraphStorage + 'g>(traversal: &AnonymousTraversal<'g, S>) -> bool {
    traversal.steps.len() == 1
        && matches!(traversal.steps.first(), Some(GraphStep::Map("identity", _)))
}

/// How a group step reduces the members of one key, decided by its value `by()`.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub(crate) enum GroupValueKind {
    /// `group_count()`, `by(Count)`, `by(__.count())`: the bulk-weighted member count.
    Count,
    /// A projection (no `by()`, a property key, `T.id`, `T.label`, a bare `by()`, ...): a list of
    /// one projected value per unit of bulk.
    List,
    /// A value traversal without a barrier: the first result of the last productive member.
    Assign,
    /// A value traversal with a barrier: run once over all members of a key.
    Barrier,
}

/// The [`GroupValueKind`] of a value `by()`.
pub(crate) fn group_value_kind<'g, S: GraphStorage + 'g>(
    group_count: bool,
    value_by: &By<'g, S>,
) -> GroupValueKind {
    if group_count {
        return GroupValueKind::Count;
    }
    match value_by {
        By::Count => GroupValueKind::Count,
        By::Traversal(t) if is_count_traversal(t) => GroupValueKind::Count,
        By::Traversal(t) if is_identity_traversal(t) => GroupValueKind::List,
        By::Traversal(t) if !has_barrier(t) => GroupValueKind::Assign,
        By::Traversal(_) => GroupValueKind::Barrier,
        By::Property(_)
        | By::Order(_)
        | By::Label
        | By::Id
        | By::Value
        | By::Identity
        | By::Key
        | By::JsonPath(_)
        | By::Labels(_)
        | By::Column(_) => GroupValueKind::List,
    }
}

/// The key `by()` and the value `by()` of a group step: the first and second `by()`, defaulting
/// to the incoming value and to a list of members (a count for `group_count()`).
pub(crate) fn group_bys<'a, 'g, S: GraphStorage + 'g>(
    by: Option<&'a [By<'g, S>]>,
    group_count: bool,
) -> (&'a By<'g, S>, &'a By<'g, S>) {
    let default_value: &'a By<'g, S> = if group_count {
        &By::Count
    } else {
        &By::Identity
    };
    match by {
        Some(list) => (
            list.first().unwrap_or(&By::Identity),
            list.get(1).unwrap_or(default_value),
        ),
        None => (&By::Identity, default_value),
    }
}

/// Rejects a value `by()` that is not a projection a [`GroupValueKind::List`] key can collect
/// (an order, a vertex-label list, a column). `by_check` already fails the step for an order and
/// a column; this keeps the value path total, and fails even when no traverser arrives.
pub(crate) fn check_value_by<'g, S: GraphStorage + 'g>(
    value_by: &By<'g, S>,
    step: &str,
) -> TraverserResult<()> {
    let reason = match value_by {
        By::Order(_) => reason::ORDER,
        By::Labels(_) => "a vertex-label list is not a projection this step can use",
        By::Column(_) => crate::traversal::by_reason::COLUMN,
        By::Property(_)
        | By::Traversal(_)
        | By::Label
        | By::Id
        | By::Value
        | By::Identity
        | By::Key
        | By::Count
        | By::JsonPath(_) => return Ok(()),
    };
    Err(Box::new(TraverserError::InvalidModulator {
        step: step.to_string(),
        position: 2,
        modulator: crate::traversal::by_reason::MODULATOR,
        reason: reason.to_string(),
    }))
}

/// The value one member adds to a [`GroupValueKind::List`] key, or `None` when the projection
/// is unproductive for it (the key still exists then: TinkerPop runs the projection as
/// `map(projection).fold()`). The incoming value itself (no `by()`, `By::Identity`, a bare `by()`)
/// is the caller's business, so it can move the value instead of cloning it.
pub(crate) fn group_list_value<'g, S: GraphStorage + 'g>(
    context: &mut TraverserContext<'_, S>,
    traverser: &Traverser<S>,
    value_by: &By<'g, S>,
) -> TraverserResult<Option<TraverserValue<S>>> {
    Ok(match value_by {
        By::Label => TraverserValue::fetch_label(context, &traverser.value)?.map(|label| {
            TraverserValue::Computed(ElementProperty::String(label.as_ref().into()), None)
        }),
        By::Id => TraverserValue::fetch_id(context, &traverser.value)?
            .map(|id| TraverserValue::Computed(ElementProperty::String(id.as_ref().into()), None)),
        By::Property(property) => {
            TraverserValue::fetch_property(context, &traverser.value, property)?
                .map(|prop| TraverserValue::Computed(prop, None))
        }
        By::Identity | By::Traversal(_) => Some(traverser.value.clone()),
        By::JsonPath(_)
        | By::Value
        | By::Key
        | By::Count
        | By::Order(_)
        | By::Labels(_)
        | By::Column(_) => TraverserValue::fetch_traverser_value_by(
            context,
            traverser,
            &traverser.value,
            value_by,
        )?,
    })
}

/// Whether a [`GroupValueKind::List`] value `by()` keeps the incoming value as it is.
pub(crate) fn takes_incoming_value<'g, S: GraphStorage + 'g>(value_by: &By<'g, S>) -> bool {
    match value_by {
        By::Identity | By::Traversal(_) => true,
        By::Property(_)
        | By::Order(_)
        | By::Label
        | By::Id
        | By::Value
        | By::Key
        | By::Count
        | By::JsonPath(_)
        | By::Labels(_)
        | By::Column(_) => false,
    }
}

impl<'g, S: GraphStorage + 'g> GraphStepBarrier<'g, S> for StepGroup<'g, S> {
    /// Builds an aggregation/materialization buffer (D3 of the memory-inspection spec) — reported
    /// to `.profile()` as a non-zero `bytes_materialized`.
    fn materializes(&self) -> bool {
        true
    }

    /// TinkerPop's `group()`/`groupCount()` are seeded reducing barriers: an empty stream reduces
    /// to `{}`, not nothing (confirmed against TinkerPop 3.7's `GroupStep`/`GroupCountStep`, whose
    /// seed is an empty map).
    fn always_emits(&self) -> bool {
        true
    }

    #[inline]
    fn process(
        &self,
        context: &mut TraverserContext<'_, S>,
        source: Vec<Traverser<S>>,
    ) -> TraverserResult<Option<Traverser<S>>> {
        let group_name = if self.group_count {
            "group_count"
        } else {
            "group"
        };
        self.by_check.check(group_name)?;
        let (by, second_by) = group_bys(self.by.as_deref(), self.group_count);
        check_value_by(second_by, group_name)?;
        let kind = group_value_kind(self.group_count, second_by);

        let mut result: StreamMap<S> = StreamMap::default();

        match kind {
            GroupValueKind::Count => {
                // A merged traverser counts `bulk` times (checked, never wraps).
                let mut tmp: IndexMap<TraverserValue<S>, u64, FnvBuildHasher> = IndexMap::default();
                for source_item in source {
                    context.tick()?;
                    if let Some(key) = group_key(context, &source_item, &source_item.value, by)? {
                        let slot = tmp.entry(key).or_insert(0);
                        *slot = slot
                            .checked_add(source_item.bulk)
                            .ok_or_else(|| bulk_overflow(group_name))?;
                    }
                }
                for (k, count) in tmp {
                    result.insert(
                        k,
                        TraverserValue::Computed(bulk::count_property(count, group_name)?, None),
                    );
                }
            }
            GroupValueKind::List => {
                // One value per unit of bulk; the expansion of the whole step is bounded by
                // `bulk.max_expansion`.
                if context.bulk_active {
                    let total = bulk::total_bulk(context, &source, group_name)?;
                    let limit = context.options.bulk_max_expansion;
                    if total > limit {
                        return Err(Box::new(TraverserError::BulkExpansionLimit {
                            step: "group".to_string(),
                            requested: total,
                            limit,
                        }));
                    }
                }
                let incoming = takes_incoming_value(second_by);
                let mut tmp: IndexMap<TraverserValue<S>, Vec<TraverserValue<S>>, FnvBuildHasher> =
                    IndexMap::default();
                for source_item in source {
                    context.tick()?;
                    if let Some(key) = group_key(context, &source_item, &source_item.value, by)? {
                        let n = source_item.bulk;
                        let value = if incoming {
                            Some(source_item.value)
                        } else {
                            group_list_value(context, &source_item, second_by)?
                        };
                        // The key exists once its `by()` is productive, whatever the value
                        // `by()` yields: a key none of whose members has the property maps to
                        // `[]`.
                        let bucket = tmp.entry(key).or_default();
                        if let Some(value) = value {
                            push_n(context, bucket, value, n, group_name)?;
                        }
                    }
                }
                for (k, list) in tmp {
                    result.insert(k, TraverserValue::Array(list));
                }
            }
            // TinkerPop's `GroupStep`: a value traversal without a barrier runs per member and
            // keeps one value per key, the first result of the last member that produced one
            // (`Operator.assign`); a key no member produced a value for is left out.
            GroupValueKind::Assign => {
                let By::Traversal(child) = second_by else {
                    return Err(Box::new(TraverserError::wrong_query(
                        "group(): an assigning value by() must be a traversal",
                    )));
                };
                for source_item in source {
                    context.tick()?;
                    if let Some(key) = group_key(context, &source_item, &source_item.value, by)?
                        && let Some(first) = child.first(context, &source_item)?
                    {
                        result.insert(key, first.value);
                    }
                }
            }
            // With a barrier (`fold()`, `count()`, `sum()`, `order()`, ...) the value traversal
            // runs once over all members of a key, and the key maps to its first result.
            GroupValueKind::Barrier => {
                let By::Traversal(child) = second_by else {
                    return Err(Box::new(TraverserError::wrong_query(
                        "group(): a reducing value by() must be a traversal",
                    )));
                };
                let mut tmp: IndexMap<TraverserValue<S>, Vec<Traverser<S>>, FnvBuildHasher> =
                    IndexMap::default();
                for source_item in source {
                    if let Some(key) = group_key(context, &source_item, &source_item.value, by)? {
                        match tmp.entry(key) {
                            Entry::Occupied(mut e) => e.get_mut().push(source_item),
                            Entry::Vacant(e) => {
                                e.insert(vec![source_item]);
                            }
                        }
                    }
                }
                for (k, v) in tmp {
                    // batch: bulk preserved (the members' bulks reach the value traversal's
                    // `count()`/`sum()`/...).
                    let x = child.to_list(&mut *context, v)?;
                    if let Some(first) = x.into_iter().next() {
                        result.insert(k, first.value);
                    }
                }
            }
        }

        Ok(Some(Traverser::start(
            context,
            TraverserValue::Object(result),
        )))
    }
    fn is_group_by_label_count(&self) -> bool {
        if self.by_check.has_rejected() {
            return false;
        }
        let binding = &self.by;
        let first_by = match binding.as_ref().and_then(|b| b.first()) {
            Some(b) => b,
            None => return false,
        };
        if !matches!(first_by, By::Label) {
            return false;
        }

        if self.group_count {
            return true;
        }

        match binding.as_ref().and_then(|b| b.get(1)) {
            Some(By::Count) => true,
            Some(By::Traversal(t)) => is_count_traversal(t),
            _ => false,
        }
    }

    fn push_modulator_by(&mut self, args: By<'g, S>, _modulator_name: &str) -> ByModulation {
        self.by_check.observe(&args);
        if matches!(args, By::Order(_)) {
            self.by_check.reject_last(reason::ORDER);
        }
        if self.group_count && self.by.is_some() {
            self.by_check.reject_last(reason::TOO_MANY);
        }
        if let Some(x) = self.by.as_mut() {
            x.push(args);
        } else {
            self.by = Some(vec![args]);
        }
        ByModulation::Accepted
    }

    fn child_traversals_mut(&mut self) -> Vec<&mut crate::traversal::AnonymousTraversal<'g, S>> {
        self.by
            .iter_mut()
            .flatten()
            .filter_map(By::traversal_mut)
            .collect()
    }

    fn analyze_nested_paths(
        &mut self,
        analysis: &crate::traversal::optimizer::path_analysis::PathAnalysis,
        _downstream: &crate::traversal::optimizer::path_analysis::PathRequirement,
    ) -> crate::traversal::optimizer::path_analysis::PathRequirement {
        // `by(__...)` children run on the incoming traverser, path included.
        match &mut self.by {
            Some(by_list) => By::analyze_all_paths(by_list, analysis),
            None => crate::traversal::optimizer::path_analysis::PathRequirement::None,
        }
    }

    fn clone_box(&self) -> Box<dyn GraphStepBarrier<'g, S> + 'g> {
        Box::new(self.clone())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::By::*;

    /// TinkerPop 3.7's `group()`/`groupCount()` are seeded reducing barriers (`GroupStep`/
    /// `GroupCountStep`, seed: an empty map): an empty input stream still reduces to `{}`, not
    /// nothing (confirmed via `graphersal`: `g.V().hasLabel("nope").group()` → `#{}`). A fused
    /// `count()` must then read `1`.
    #[test]
    fn empty_input_group_reduces_to_an_empty_map() {
        test_dual!(
            graph: GraphSource::tinkerpop_modern(),
            rust: |g| g.v(None).has_label("nope").group().to_list(),
            rhai: [
                r#"g.V().hasLabel("nope").group()"#,
                r#"g.v().has_label("nope").group()"#,
            ],
            assert: |res| {
                assert_eq!(res.len(), 1);
                match &res[0] {
                    ElementProperty::Object(map) => assert!(map.is_empty()),
                    other => panic!("expected an empty map, got {other:?}"),
                }
            },
        );
    }

    #[test]
    fn empty_input_group_count_is_one() {
        test_dual!(
            graph: GraphSource::tinkerpop_modern(),
            rust: |g| g.v(None).has_label("nope").group().count().to_list(),
            rhai: [
                r#"g.V().hasLabel("nope").group().count()"#,
                r#"g.v().has_label("nope").group().count()"#,
            ],
            assert: |res| assert_eq!(res[0].as_i64(), Some(1)),
        );
    }

    #[test]
    fn empty_input_group_count_step_reduces_to_an_empty_map() {
        test_dual!(
            graph: GraphSource::tinkerpop_modern(),
            rust: |g| g.v(None).has_label("nope").group_count().to_list(),
            rhai: [
                r#"g.V().hasLabel("nope").groupCount()"#,
                r#"g.v().has_label("nope").group_count()"#,
            ],
            assert: |res| {
                assert_eq!(res.len(), 1);
                match &res[0] {
                    ElementProperty::Object(map) => assert!(map.is_empty()),
                    other => panic!("expected an empty map, got {other:?}"),
                }
            },
        );
    }

    /// The known gap this task fixes: a value traversal with a barrier (`by(__.values("age")
    /// .fold())`) must keep a key whose members are all unproductive for that traversal, holding
    /// the barrier's own empty seed (`[]`) — TinkerPop keeps the key; dropping it was the bug.
    /// `software` vertices have no `age`, so `values("age")` produces nothing for them and
    /// `fold()` (fixed by this task) seeds `[]` for that key instead of the key vanishing.
    #[test]
    fn v_by_label_by_values_fold_keeps_the_unproductive_key_as_an_empty_list() {
        test_dual!(
            graph: GraphSource::tinkerpop_modern(),
            rust: |g| g.v(None).group().by(By::Label).by(__::values("age").fold()).to_list(),
            rhai: [
                r#"g.V().group().by(By::Label).by(__.values("age").fold())"#,
                r#"g.v().group().by(By::Label).by(__.values("age").fold())"#,
            ],
            assert: |res| {
                match &res[0] {
                    ElementProperty::Object(result) => {
                        let persons = result.get("person").unwrap().as_array().unwrap();
                        assert_eq!(persons.len(), 4);
                        let software = result.get("software").unwrap().as_array().unwrap();
                        assert!(software.is_empty(), "expected software: [], got {software:?}");
                    },
                    _ => panic!(),
                }
            },
        );
    }

    #[test]
    fn v_label_id() {
        test_dual!(
            graph: GraphSource::tinkerpop_modern(),
            rust: |g| g.v(None).group().by(By::Label).by(By::Id).to_list(),
            rhai: [
                r#"g.V().group().by(By::Label).by(By::Id)"#,
                r#"g.v().group().by(By::Label).by(By::Id)"#,
            ],
            assert: |res| {
                match &res[0] {
                    ElementProperty::Object(result) => {
                        let value = &result["person"];
                        match value {
                            ElementProperty::Array(value) => {
                                assert_eq!(value[0].as_string(), Some("1"));
                                assert_eq!(value[1], ElementProperty::String("2".into()));
                                assert_eq!(value[2], ElementProperty::String("4".into()));
                                assert_eq!(value[3], ElementProperty::String("6".into()));
                            },
                            _ => panic!(),
                        }
                        let value = &result["software"];
                        match value {
                            ElementProperty::Array(value) => {
                                assert_eq!(value[0], ElementProperty::String("3".into()));
                                assert_eq!(value[1], ElementProperty::String("5".into()));
                            },
                            _ => panic!(),
                        }
                    },
                    _ => panic!(),
                }
            },
        );
    }

    #[test]
    fn v_label_where_id_fold() {
        test_dual!(
            graph: GraphSource::tinkerpop_modern(),
            rust: |g| g.v(None).group().by(By::Label).by(__::id().fold()).to_list(),
            rhai: [
                r#"g.V().group().by(By::Label).by(__.id().fold())"#,
                r#"g.v().group().by(By::Label).by(__.id().fold())"#,
            ],
            assert: |res| {
                match &res[0] {
                    ElementProperty::Object(result) => {
                        let value = &result["person"];
                        match value {
                            ElementProperty::Array(value) => {
                                assert_eq!(value[0].as_string(), Some("1"));
                                assert_eq!(value[1], ElementProperty::String("2".into()));
                                assert_eq!(value[2], ElementProperty::String("4".into()));
                                assert_eq!(value[3], ElementProperty::String("6".into()));
                            },
                            _ => panic!(),
                        }
                        let value = &result["software"];
                        match value {
                            ElementProperty::Array(value) => {
                                assert_eq!(value[0], ElementProperty::String("3".into()));
                                assert_eq!(value[1], ElementProperty::String("5".into()));
                            },
                            _ => panic!(),
                        }
                    },
                    _ => panic!(),
                }
            },
        );
    }

    #[test]
    fn v_by_label_count() {
        test_dual!(
            graph: GraphSource::tinkerpop_modern(),
            rust: |g| g.v(None).group().by(Label).by(Count).to_list(),
            rhai: [
                r#"g.V().group().by(By::Label).by(By::Count)"#,
                r#"g.v().group().by(By::Label).by(By::Count)"#,
            ],
            assert: |res| {
                match &res[0] {
                    ElementProperty::Object(result) => {
                        assert_eq!(result.get("person").unwrap().as_i64(), Some(4));
                        assert_eq!(result.get("software").unwrap().as_i64(), Some(2));
                    },
                    _ => panic!(),
                }
            },
        );
    }

    #[test]
    fn v_by_age_count() {
        test_dual!(
            graph: GraphSource::tinkerpop_modern(),
            rust: |g| g.v(None).group().by("age").by(Count).to_list(),
            rhai: [
                r#"g.V().group().by("age").by(By::Count)"#,
                r#"g.v().group().by("age").by(By::Count)"#,
            ],
            assert: |res| {
                match &res[0] {
                    ElementProperty::Map(index_map) => {
                        assert_eq!(index_map.len(), 4);
                    },
                    _ => panic!(),
                }
            },
        );
    }

    #[test]
    fn v_by_label_last_count() {
        test_dual!(
            graph: GraphSource::tinkerpop_modern(),
            rust: |g| g.v(None).group().by(Label).by(Count).count().to_list(),
            rhai: [
                r#"g.V().group().by(By::Label).by(By::Count).count()"#,
                r#"g.v().group().by(By::Label).by(By::Count).count()"#,
            ],
            assert: |res| {
                assert_eq!(res.len(), 1);
                assert_eq!(res[0].as_i64(), 1.into());
            },
        );
    }

    #[test]
    fn v_by_label_where_count() {
        test_dual!(
            graph: GraphSource::tinkerpop_modern(),
            rust: |g| g.v(None).group().by(Label).by(__::count()).to_list(),
            rhai: [
                r#"g.V().group().by(By::Label).by(__.count())"#,
                r#"g.v().group().by(By::Label).by(__.count())"#,
            ],
            assert: |res| {
                match &res[0] {
                    ElementProperty::Object(result) => {
                        assert_eq!(result.get("person").unwrap().as_i64(), Some(4));
                        assert_eq!(result.get("software").unwrap().as_i64(), Some(2));
                    },
                    _ => panic!(),
                }
            },
        );
    }

    #[test]
    fn v_group_count_by_label() {
        test_dual!(
            graph: GraphSource::tinkerpop_modern(),
            rust: |g| g.v(None).group_count().by(Label).to_list(),
            rhai: [
                r#"g.V().groupCount().by(By::Label)"#,
                r#"g.v().group_count().by(By::Label)"#,
            ],
            assert: |res| {
                match &res[0] {
                    ElementProperty::Object(result) => {
                        assert_eq!(result.get("person").unwrap().as_i64(), Some(4));
                        assert_eq!(result.get("software").unwrap().as_i64(), Some(2));
                    },
                    _ => panic!(),
                }
            },
        );
    }

    #[test]
    fn v_by_label_default_value() {
        test_dual!(
            graph: GraphSource::tinkerpop_modern(),
            rust: |g| g.v(None).group().by(By::Label).to_list(),
            rhai: [
                r#"g.V().group().by(By::Label)"#,
                r#"g.v().group().by(By::Label)"#,
            ],
            assert: |res| {
                match &res[0] {
                    ElementProperty::Object(result) => {
                        let persons = result.get("person").unwrap().as_array().unwrap();
                        assert_eq!(persons.len(), 4);
                        let software = result.get("software").unwrap().as_array().unwrap();
                        assert_eq!(software.len(), 2);
                    },
                    _ => panic!(),
                }
            },
        );
    }

    #[test]
    fn v_by_label_by_property() {
        test_dual!(
            graph: GraphSource::tinkerpop_modern(),
            rust: |g| g.v(None).group().by(By::Label).by("name").to_list(),
            rhai: [
                r#"g.V().group().by(By::Label).by("name")"#,
                r#"g.v().group().by(By::Label).by("name")"#,
            ],
            assert: |res| {
                match &res[0] {
                    ElementProperty::Object(result) => {
                        let persons = result.get("person").unwrap().as_array().unwrap();
                        assert_eq!(persons.len(), 4);
                        let names: Vec<_> = persons.iter().map(|p| p.as_string().unwrap()).collect();
                        assert!(names.contains(&"marko"));
                        assert!(names.contains(&"vadas"));
                        assert!(names.contains(&"josh"));
                        assert!(names.contains(&"peter"));

                        let software = result.get("software").unwrap().as_array().unwrap();
                        assert_eq!(software.len(), 2);
                        let sw_names: Vec<_> = software.iter().map(|p| p.as_string().unwrap()).collect();
                        assert!(sw_names.contains(&"lop"));
                        assert!(sw_names.contains(&"ripple"));
                    },
                    _ => panic!(),
                }
            },
        );
    }

    #[test]
    fn v_by_name_by_id() {
        test_dual!(
            graph: GraphSource::tinkerpop_modern(),
            rust: |g| g.v(None).group().by("name").by(By::Id).to_list(),
            rhai: [
                r#"g.V().group().by("name").by(By::Id)"#,
                r#"g.v().group().by("name").by(By::Id)"#,
            ],
            assert: |res| {
                match &res[0] {
                    ElementProperty::Object(result) => {
                        assert_eq!(result.len(), 6);
                        let marko_ids = result.get("marko").unwrap().as_array().unwrap();
                        assert_eq!(marko_ids.len(), 1);
                        assert_eq!(marko_ids[0].as_string(), Some("1"));
                    },
                    _ => panic!(),
                }
            },
        );
    }

    #[test]
    fn v_group_count_default() {
        test_dual!(
            graph: GraphSource::tinkerpop_modern(),
            rust: |g| g.v(None).values("age").group_count().to_list(),
            rhai: [
                r#"g.V().values("age").groupCount()"#,
                r#"g.v().values("age").group_count()"#,
            ],
            assert: |res| {
                match &res[0] {
                    ElementProperty::Map(result) => {
                        assert_eq!(result.len(), 4);
                        for age in [29, 27, 32, 35] {
                            let key = ElementProperty::Number(crate::value::Number::from_i64(age));
                            assert_eq!(result.get(&key).unwrap().as_i64(), Some(1));
                        }
                    },
                    _ => panic!(),
                }
            },
        );
    }

    /// Equal property values read from different vertices form one group: the key is the value,
    /// not the per-vertex property handle (`lop` and `ripple` both have `lang: "java"`).
    #[test]
    fn v_group_count_merges_equal_values_from_different_vertices() {
        test_dual!(
            graph: GraphSource::tinkerpop_modern(),
            rust: |g| g.v(None).values("lang").group_count().to_list(),
            rhai: [
                r#"g.V().values("lang").groupCount()"#,
                r#"g.v().values("lang").group_count()"#,
            ],
            assert: |res| {
                match &res[0] {
                    ElementProperty::Object(result) => {
                        assert_eq!(result.len(), 1);
                        assert_eq!(result.get("java").unwrap().as_i64(), Some(2));
                    },
                    _ => panic!(),
                }
            },
        );
    }

    #[test]
    fn v_group_count_large_110k() {
        let graph = GraphSource::large_110k();
        let g_lock = graph.read();

        // 1. group_count().by("level")
        let g1 = g_lock.traversal();
        let res1 = g1.v(None).group_count().by("level").to_list().unwrap();
        match &res1[0] {
            ElementProperty::Object(result) => {
                assert_eq!(result.len(), 5);
                assert_eq!(result.get("0").unwrap().as_i64(), Some(10));
                assert_eq!(result.get("1").unwrap().as_i64(), Some(100));
                assert_eq!(result.get("2").unwrap().as_i64(), Some(1000));
                assert_eq!(result.get("3").unwrap().as_i64(), Some(10000));
                assert_eq!(result.get("4").unwrap().as_i64(), Some(100000));
            }
            _ => panic!("Expected Object property"),
        }

        // 2. group().by("level").by(Count)
        let g2 = g_lock.traversal();
        let res2 = g2.v(None).group().by("level").by(Count).to_list().unwrap();
        assert_eq!(res1, res2);

        // 3. group().by("level").by(__::count())
        let g3 = g_lock.traversal();
        let res3 = g3
            .v(None)
            .group()
            .by("level")
            .by(__::count())
            .to_list()
            .unwrap();
        assert_eq!(res1, res3);
    }

    /// `By::JsonPath` used as `group()`'s second `.by()` goes through `group()`'s own separate
    /// `match second_by` (not `fetch_traverser_value_by` alone) — this exercises that path
    /// specifically, distinct from the `select()` coverage of the shared lever.
    #[test]
    #[cfg(feature = "serde")]
    fn group_by_json_path_second_by() {
        use serde_json::json;

        let mut graph = TraversalGraph::tinkerpop_modern();
        let result = graph
            .traversal_mut()
            .v("1")
            .property_json(json!({"meta": {"k": 1}}))
            .group()
            .by(By::Label)
            .by(By::JsonPath("meta.k".parse().unwrap()))
            .next()
            .unwrap()
            .unwrap();
        match result {
            ElementProperty::Object(result) => {
                let value = &result["person"];
                match value {
                    ElementProperty::Array(value) => {
                        assert_eq!(value[0].as_i64(), Some(1));
                    }
                    _ => panic!("Expected Array property"),
                }
            }
            _ => panic!("Expected Object property"),
        }
    }

    #[test]
    fn profile() {
        let graph = GraphSource::tinkerpop_modern();
        let g_lock = graph.read();
        let g = g_lock.traversal();
        let result = g
            .v(None)
            .group()
            .by("name")
            .by(By::Id)
            .count()
            .profile()
            .unwrap();
        assert!(result.to_string().contains("group("));
    }
}