rocksgraph 0.1.0

A Gremlin-inspired property graph query engine written in Rust, backed by RocksDB
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
// Copyright (c) 2026 Austin Han <austinhan1024@gmail.com>
//
// This file is part of RocksGraph.
//
// RocksGraph is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// RocksGraph is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with RocksGraph.  If not, see <https://www.gnu.org/licenses/>.

//! Engine-agnostic logical IR — the intermediate representation shared by the
//! optimizer and all execution engines.
//!
//! A [`LogicalPlan`] is an ordered list of [`LogicalStep`]s. It carries only
//! *what* to compute, with no reference to any physical operator or execution
//! strategy. The volcano builder ([`engine::volcano::builder`]) is responsible
//! for compiling a `LogicalPlan` into a chain of physical steps.
//!
//! [`engine::volcano::builder`]: crate::engine::volcano::builder

use crate::types::{
    gvalue::{Primitive, PrimitivePredicate},
    keys::{DegreeDirection, Rank, VertexKey},
    prop_key::PropKey,
    StoreError, ORDER_KEY_INLINE, SMALL_VECTOR_LENGTH, STEP_LABEL_INLINE, VERTEX_PROPS_LENGTH,
};
use smallvec::SmallVec;
use smol_str::SmolStr;

// Reuse the same rewrite/optimize rule for both LogicalPlan and LogicalStep.
pub type OptimizerRule = fn(&mut LogicalPlan) -> Result<bool, StoreError>;

pub trait Optimizer {
    /// Applies an optimization rule to the implementor.
    fn optimize(&mut self, _: &OptimizerRule) -> Result<bool, StoreError> {
        Ok(false)
    }
}

/// Represents a sequence of logical steps that form a query plan.
#[derive(Clone)]
pub struct LogicalPlan {
    pub steps: Vec<LogicalStep>,
}

impl Optimizer for LogicalPlan {
    fn optimize(&mut self, rule: &OptimizerRule) -> Result<bool, StoreError> {
        let mut changed = false;
        for step in self.steps.iter_mut() {
            changed |= step.optimize(rule)?;
        }
        changed |= rule(self)?;
        Ok(changed)
    }
}

impl LogicalPlan {
    /// Returns `true` if any step in this plan depends on path tracking
    /// (`as()`, `select()`, `path()`). When false, the builder can skip
    /// parent-chain construction, eliminating Rc::clone overhead.
    pub fn has_path_consumer(&self) -> bool {
        fn scan(steps: &[LogicalStep]) -> bool {
            use LogicalStep::*;
            for s in steps {
                match s {
                    As(_) | Select(_) | Path(_) | SimplePath(_) | CyclicPath(_) => return true,
                    Not(NotStep { plan }) if scan(&plan.steps) => {
                        return true;
                    }
                    And(AndStep { plans }) | Or(OrStep { plans }) => {
                        for p in plans {
                            if scan(&p.steps) {
                                return true;
                            }
                        }
                    }
                    Union(UnionStep { plans }) => {
                        for p in plans {
                            if scan(&p.steps) {
                                return true;
                            }
                        }
                    }
                    Coalesce(CoalesceStep { plans }) => {
                        for p in plans {
                            if scan(&p.steps) {
                                return true;
                            }
                        }
                    }
                    Where(WhereStep { plan }) if scan(&plan.steps) => {
                        return true;
                    }
                    Repeat(RepeatStep { body, until, emit, .. }) => {
                        if scan(&body.steps) {
                            return true;
                        }
                        if let Some(p) = until {
                            if scan(&p.steps) {
                                return true;
                            }
                        }
                        if let EmitSpec::If(p) = emit {
                            if scan(&p.steps) {
                                return true;
                            }
                        }
                    }
                    Choose(ChooseStep { predicate, true_choice, false_choice, .. }) => {
                        if scan(&predicate.steps) {
                            return true;
                        }
                        if scan(&true_choice.steps) {
                            return true;
                        }
                        if let Some(fc) = false_choice {
                            if scan(&fc.steps) {
                                return true;
                            }
                        }
                    }
                    Local(LocalStep { plan }) if scan(&plan.steps) => {
                        return true;
                    }
                    _ => {}
                }
            }
            false
        }
        scan(&self.steps)
    }
}

/// An enumeration of all possible logical steps in a query plan.
#[derive(Clone)]
pub enum LogicalStep {
    Both(BothStep),
    BothE(BothEStep),
    Count(CountStep),
    Degree(DegreeStep),
    HasLabel(HasLabelStep),
    HasProperty(HasPropertyStep),
    In(InStep),
    InE(InEStep),
    Out(OutStep),
    OutE(OutEStep),
    InV(InVStep),
    OtherV(OtherVStep),
    OutV(OutVStep),
    ScalarFilter(ScalarFilterStep),
    Values(ValuesStep),
    Properties(PropertiesStep),
    Where(WhereStep),
    Union(UnionStep),
    AddV(AddVStep),
    AddE(AddEStep),
    From(FromStep),
    To(ToStep),
    Property(PropertyStep),
    V(VStep),
    E(EStep),
    Limit(LimitStep),
    HasId(HasIdStep),
    Coalesce(CoalesceStep),
    EndVertexFilter(EndVertexFilter),
    Drop(DropStep),
    Path(PathStep),
    Dedup(DedupStep),
    Fold(FoldStep),
    Repeat(RepeatStep),
    Not(NotStep),
    And(AndStep),
    Or(OrStep),
    Sum(SumStep),
    Mean(MeanStep),
    Max(MaxStep),
    Min(MinStep),
    Unfold(UnfoldStep),
    As(AsStep),
    Select(SelectStep),
    Range(RangeStep),
    Skip(SkipStep),
    Tail(TailStep),
    Order(OrderStep),
    SimplePath(SimplePathStep),
    CyclicPath(CyclicPathStep),
    Choose(ChooseStep),
    Group(GroupStep),
    GroupCount(GroupCountStep),
    Id(IdStep),
    Label(LabelStep),
    Rank(RankStep),
    HasRank(HasRankStep),
    Constant(ConstantStep),
    Identity(IdentityStep),
    Local(LocalStep),
}

/// Specifies when a repeat step should emit intermediate results.
#[derive(Clone)]
pub enum EmitSpec {
    Never,
    Always,
    If(LogicalPlan),
}

/// Represents a logical `repeat` step — a variable-length looping construct.
#[derive(Clone)]
pub struct RepeatStep {
    pub body: LogicalPlan,
    pub until: Option<LogicalPlan>,
    pub times: Option<i64>,
    pub emit: EmitSpec,
}

impl Optimizer for RepeatStep {
    fn optimize(&mut self, optimizer_rule: &OptimizerRule) -> Result<bool, StoreError> {
        let mut changed = false;
        changed |= optimizer_rule(&mut self.body)?;
        if let Some(ref mut until) = self.until {
            changed |= optimizer_rule(until)?;
        }
        if let EmitSpec::If(ref mut plan) = self.emit {
            changed |= optimizer_rule(plan)?;
        }
        Ok(changed)
    }
}

/// Represents a logical `drop` step in a query plan.
#[derive(Clone)]
pub struct DropStep {}

impl Optimizer for DropStep {}

/// Represents a logical `path` step in a query plan.
#[derive(Clone, Debug)]
pub struct PathStep {}

impl Optimizer for PathStep {}

/// Represents a logical `dedup` step in a query plan.
#[derive(Clone, Debug)]
pub struct DedupStep {}

impl Optimizer for DedupStep {}

/// Collects all traversers into a single `GValue::List` (Gremlin `fold()` step).
#[derive(Clone, Debug)]
pub struct FoldStep {}

impl Optimizer for FoldStep {}

/// Negates a sub-traversal filter: passes the traverser if the sub-plan yields nothing.
#[derive(Clone)]
pub struct NotStep {
    pub plan: LogicalPlan,
}

impl Optimizer for NotStep {
    fn optimize(&mut self, optimizer_rule: &OptimizerRule) -> Result<bool, StoreError> {
        optimizer_rule(&mut self.plan)
    }
}

/// Passes the traverser if all sub-plans yield results (short-circuit on first failure).
#[derive(Clone)]
pub struct AndStep {
    pub plans: Vec<LogicalPlan>,
}

impl Optimizer for AndStep {
    fn optimize(&mut self, optimizer_rule: &OptimizerRule) -> Result<bool, StoreError> {
        let mut changed = false;
        for plan in self.plans.iter_mut() {
            changed |= optimizer_rule(plan)?;
        }
        Ok(changed)
    }
}

/// Passes the traverser if any sub-plan yields results (short-circuit on first success).
#[derive(Clone)]
pub struct OrStep {
    pub plans: Vec<LogicalPlan>,
}

impl Optimizer for OrStep {
    fn optimize(&mut self, optimizer_rule: &OptimizerRule) -> Result<bool, StoreError> {
        let mut changed = false;
        for plan in self.plans.iter_mut() {
            changed |= optimizer_rule(plan)?;
        }
        Ok(changed)
    }
}

/// Sums all numeric traverser values into a single scalar (Gremlin `sum()` step).
#[derive(Clone, Debug)]
pub struct SumStep {}

impl Optimizer for SumStep {}

/// Averages all numeric traverser values, always returning `Float64`.
#[derive(Clone, Debug)]
pub struct MeanStep {}

impl Optimizer for MeanStep {}

/// Finds the maximum numeric traverser value.
#[derive(Clone, Debug)]
pub struct MaxStep {}

impl Optimizer for MaxStep {}

/// Finds the minimum numeric traverser value.
#[derive(Clone, Debug)]
pub struct MinStep {}

impl Optimizer for MinStep {}

/// Unfolds a `GValue::List` into individual traversers (inverse of `fold()`).
#[derive(Clone, Debug)]
pub struct UnfoldStep {}

impl Optimizer for UnfoldStep {}

/// Labels the current traverser for later retrieval via `select()`.
#[derive(Clone, Debug)]
pub struct AsStep {
    pub labels: SmallVec<[SmolStr; STEP_LABEL_INLINE]>,
}

impl Optimizer for AsStep {}

/// Retrieves traversers previously labeled with `as()`.
#[derive(Clone, Debug)]
pub struct SelectStep {
    pub labels: SmallVec<[SmolStr; STEP_LABEL_INLINE]>,
}

impl Optimizer for SelectStep {}

/// Keeps traversers in the half-open range `[lo, hi)`.
#[derive(Clone, Debug)]
pub struct RangeStep {
    pub lo: i64,
    pub hi: i64,
}
impl Optimizer for RangeStep {}

/// Skips the first `n` traversers, emitting the rest.
#[derive(Clone, Debug)]
pub struct SkipStep {
    pub n: i64,
}
impl Optimizer for SkipStep {}

/// Collects all traversers and emits only the last `n`.
#[derive(Clone, Debug)]
pub struct TailStep {
    pub n: i64,
}
impl Optimizer for TailStep {}

/// Sorting direction.
#[derive(Clone, Debug, PartialEq, Eq, Copy)]
pub enum Order {
    Asc,
    Desc,
}

/// Specifies what to compare when sorting.
#[derive(Clone, Debug)]
pub enum OrderKeySpec {
    /// Compare by the traverser value itself.
    Value,
    /// Compare by a property value (resolved at build time).
    Property(SmolStr),
}

/// A single sort key with direction.
#[derive(Clone, Debug)]
pub struct OrderKey {
    pub spec: OrderKeySpec,
    pub order: Order,
}

/// Sorts traversers using the given key specifications.
#[derive(Clone, Debug)]
pub struct OrderStep {
    pub keys: SmallVec<[OrderKey; ORDER_KEY_INLINE]>,
}
impl Optimizer for OrderStep {}

/// Filters out traversers whose path contains duplicate vertices (keeps simple paths).
#[derive(Clone, Debug)]
pub struct SimplePathStep {}
impl Optimizer for SimplePathStep {}

/// Filters out traversers whose path does NOT contain duplicates (keeps cyclic paths).
#[derive(Clone, Debug)]
pub struct CyclicPathStep {}
impl Optimizer for CyclicPathStep {}

/// Conditional branching: if predicate matches, take true_choice; else take false_choice (or pass-through).
#[derive(Clone)]
pub struct ChooseStep {
    pub predicate: LogicalPlan,
    pub true_choice: LogicalPlan,
    pub false_choice: Option<LogicalPlan>,
}
impl Optimizer for ChooseStep {
    fn optimize(&mut self, optimizer_rule: &OptimizerRule) -> Result<bool, StoreError> {
        let mut changed = optimizer_rule(&mut self.predicate)?;
        changed |= optimizer_rule(&mut self.true_choice)?;
        if let Some(ref mut fc) = self.false_choice {
            changed |= optimizer_rule(fc)?;
        }
        Ok(changed)
    }
}

/// Collects traversers into a map, grouped by key. If no key is specified, groups by value.
#[derive(Clone, Debug)]
pub struct GroupStep {
    pub key: Option<SmolStr>,
}
impl Optimizer for GroupStep {}

/// Collects traversers and counts occurrences per key. If no key is specified, counts by value.
#[derive(Clone, Debug)]
pub struct GroupCountStep {
    pub key: Option<SmolStr>,
}
impl Optimizer for GroupCountStep {}

/// Passes each traverser through unchanged (Gremlin `identity()` step).
#[derive(Clone, Debug)]
pub struct IdentityStep {}
impl Optimizer for IdentityStep {}

/// Replaces each traverser with the id of its element (Gremlin `id()` step).
#[derive(Clone, Debug)]
pub struct IdStep {}
impl Optimizer for IdStep {}

/// Replaces each traverser with the label of its element (Gremlin `label()` step).
#[derive(Clone, Debug)]
pub struct LabelStep {}
impl Optimizer for LabelStep {}

/// Replaces each traverser with the rank of its element. Edge-only — rank is the
/// structural multi-edge discriminator, vertices have no rank.
#[derive(Clone, Debug)]
pub struct RankStep {}
impl Optimizer for RankStep {}

/// Filters traversers by the rank of the edge they carry. Edge-only — a vertex
/// traverser never matches.
#[derive(Clone, Debug)]
pub struct HasRankStep {
    pub pred: PrimitivePredicate,
}
impl Optimizer for HasRankStep {}

/// Replaces each traverser with a fixed constant value (Gremlin `constant()` step).
#[derive(Clone, Debug)]
pub struct ConstantStep {
    pub value: Primitive,
}
impl Optimizer for ConstantStep {}

/// Executes a sub-traversal locally on each traverser and emits every result
/// (Gremlin `local()` step).
#[derive(Clone)]
pub struct LocalStep {
    pub plan: LogicalPlan,
}

impl Optimizer for LocalStep {
    fn optimize(&mut self, optimizer_rule: &OptimizerRule) -> Result<bool, StoreError> {
        optimizer_rule(&mut self.plan)
    }
}

/// Implements the `Optimizer` trait for `LogicalStep`, allowing optimization rules to be applied to individual steps.
impl Optimizer for LogicalStep {
    fn optimize(&mut self, optimizer_rule: &OptimizerRule) -> Result<bool, StoreError> {
        let mut changed = false;
        match self {
            LogicalStep::Where(wh) => changed |= wh.optimize(optimizer_rule)?,
            LogicalStep::Union(u) => changed |= u.optimize(optimizer_rule)?,
            LogicalStep::Coalesce(c) => changed |= c.optimize(optimizer_rule)?,
            LogicalStep::Repeat(r) => changed |= r.optimize(optimizer_rule)?,
            LogicalStep::Not(n) => changed |= n.optimize(optimizer_rule)?,
            LogicalStep::And(a) => changed |= a.optimize(optimizer_rule)?,
            LogicalStep::Or(o) => changed |= o.optimize(optimizer_rule)?,
            LogicalStep::Choose(c) => changed |= c.optimize(optimizer_rule)?,
            LogicalStep::Local(l) => changed |= l.optimize(optimizer_rule)?,
            _ => {}
        }
        Ok(changed)
    }
}

/// Generalized filter on the other vertex in a `where(otherV()…)` clause.
///
/// Holds id, label, and property predicates extracted from a `where()` sub-plan.
/// `ids: None` = unconstrained; `ids: Some(empty)` = matches nothing (empty intersection).
#[derive(Clone)]
pub struct EndVertexFilter {
    pub ids: Option<SmallVec<[VertexKey; SMALL_VECTOR_LENGTH]>>,
    /// The other vertex's label predicates, ANDed — same accumulation shape as
    /// `property_preds` (label has no structural lookup-key role to constrain it to a single
    /// value, unlike `ids`/edge `rank`, so there's no reason it can't just be a list).
    pub label_preds: Vec<PrimitivePredicate>,
    /// The other vertex's property predicates, ANDed.
    pub property_preds: Vec<(SmolStr, PrimitivePredicate)>,
}

/// Implements the `Optimizer` trait for `EndVertexFilter`.
impl Optimizer for EndVertexFilter {}

#[derive(Clone)]
pub struct CoalesceStep {
    pub plans: Vec<LogicalPlan>,
}

impl Optimizer for CoalesceStep {
    fn optimize(&mut self, optimizer_rule: &OptimizerRule) -> Result<bool, StoreError> {
        let mut changed = false;
        for plan in self.plans.iter_mut() {
            changed |= optimizer_rule(plan)?;
        }
        Ok(changed)
    }
}

/// Represents a logical `count` step in a query plan.
#[derive(Clone)]
pub struct CountStep {}

impl Optimizer for CountStep {}

/// Internal-only step produced by the `degree_pushdown` optimizer.
/// Reads the per-vertex degree from the `vertex_degree` CF overlay — O(1), no adjacency scan.
/// Not user-visible; never produced by the traversal builder.
#[derive(Debug, Clone, PartialEq)]
pub struct DegreeStep {
    pub direction: DegreeDirection,
}

impl Optimizer for DegreeStep {}
#[derive(Clone)]
/// Represents a logical `both` step, traversing both incoming and outgoing edges.
pub struct BothStep {
    pub labels: SmallVec<[SmolStr; SMALL_VECTOR_LENGTH]>,
    pub end_vertex_ids: Option<SmallVec<[VertexKey; SMALL_VECTOR_LENGTH]>>,
}

impl Optimizer for BothStep {}

/// Represents a logical `bothE` step, traversing both incoming and outgoing edges and returning the edges themselves.
#[derive(Clone)]
pub struct BothEStep {
    pub labels: SmallVec<[SmolStr; SMALL_VECTOR_LENGTH]>,
    pub end_vertex_ids: Option<SmallVec<[VertexKey; SMALL_VECTOR_LENGTH]>>,
    /// The edge rank to filter by, folded in from a trailing `.has("rank", N)` (see
    /// `merge_end_vertex_filter`). `None` means no rank constraint is known at plan time.
    pub rank: Option<Rank>,
}

impl Optimizer for BothEStep {}

/// Represents a logical `hasLabel` step, filtering elements by their label IDs.
#[derive(Clone)]
pub struct HasLabelStep {
    pub pred: PrimitivePredicate,
}

/// Implements the `Optimizer` trait for `HasLabelStep`.
impl Optimizer for HasLabelStep {}

#[derive(Clone)]
pub struct HasPropertyStep {
    pub key: PropKey,
    pub pred: PrimitivePredicate,
}

impl Optimizer for HasPropertyStep {}

/// Represents a logical `in` step, traversing incoming edges and returning the source vertices.
#[derive(Clone)]
pub struct InStep {
    pub labels: SmallVec<[SmolStr; SMALL_VECTOR_LENGTH]>,
    pub end_vertex_ids: Option<SmallVec<[VertexKey; SMALL_VECTOR_LENGTH]>>,
}

/// Implements the `Optimizer` trait for `InStep`.
impl Optimizer for InStep {}

#[derive(Clone)]
pub struct InEStep {
    pub labels: SmallVec<[SmolStr; SMALL_VECTOR_LENGTH]>,
    pub end_vertex_ids: Option<SmallVec<[VertexKey; SMALL_VECTOR_LENGTH]>>,
    /// The edge rank to filter by, folded in from a trailing `.has("rank", N)` (see
    /// `merge_end_vertex_filter`). `None` means no rank constraint is known at plan time.
    pub rank: Option<Rank>,
}
impl Optimizer for InEStep {}

/// Represents a logical `out` step, traversing outgoing edges and returning the destination vertices.
#[derive(Clone)]
pub struct OutStep {
    pub labels: SmallVec<[SmolStr; SMALL_VECTOR_LENGTH]>,
    pub end_vertex_ids: Option<SmallVec<[VertexKey; SMALL_VECTOR_LENGTH]>>,
}

/// Implements the `Optimizer` trait for `OutStep`.
impl Optimizer for OutStep {}

#[derive(Clone)]
pub struct OutEStep {
    pub labels: SmallVec<[SmolStr; SMALL_VECTOR_LENGTH]>,
    pub end_vertex_ids: Option<SmallVec<[VertexKey; SMALL_VECTOR_LENGTH]>>,
    /// The edge rank to filter by, folded in from a trailing `.has("rank", N)` (see
    /// `merge_end_vertex_filter`). `None` means no rank constraint is known at plan time.
    pub rank: Option<Rank>,
}

/// Implements the `Optimizer` trait for `OutEStep`.
impl Optimizer for OutEStep {}

/// Represents a logical `inV` step, which extracts the incoming vertex from an edge traverser.
#[derive(Clone)]
pub struct InVStep {}

impl Optimizer for InVStep {}

#[derive(Clone)]
pub struct OtherVStep {}

impl Optimizer for OtherVStep {}

/// Represents a logical `outV` step, which extracts the outgoing vertex from an edge traverser.
#[derive(Clone)]
pub struct OutVStep {}

impl Optimizer for OutVStep {}

/// Represents a logical `scalarFilter` step, filtering traversers based on a scalar value.
#[derive(Clone)]
pub struct ScalarFilterStep {
    pub pred: PrimitivePredicate,
}

impl Optimizer for ScalarFilterStep {}

/// Represents a logical `values` step, extracting property values from elements.
#[derive(Clone)]
pub struct ValuesStep {
    pub property_keys: SmallVec<[PropKey; SMALL_VECTOR_LENGTH]>,
}

/// Implements the `Optimizer` trait for `ValuesStep`.
impl Optimizer for ValuesStep {}

#[derive(Clone)]
pub struct PropertiesStep {
    pub property_keys: SmallVec<[PropKey; SMALL_VECTOR_LENGTH]>,
}
impl Optimizer for PropertiesStep {}

/// Represents a logical `where` step, applying a sub-plan as a filter.
#[derive(Clone)]
pub struct WhereStep {
    pub plan: LogicalPlan,
}

/// Implements the `Optimizer` trait for `WhereStep`, optimizing its sub-plan.
impl Optimizer for WhereStep {
    fn optimize(&mut self, optimizer_rule: &OptimizerRule) -> Result<bool, StoreError> {
        optimizer_rule(&mut self.plan)
    }
}

#[derive(Clone)]
/// Represents a logical `union` step, combining results from multiple sub-plans.
pub struct UnionStep {
    pub plans: SmallVec<[LogicalPlan; SMALL_VECTOR_LENGTH]>,
}

/// Implements the `Optimizer` trait for `UnionStep`, optimizing its sub-plans.
impl Optimizer for UnionStep {
    fn optimize(&mut self, optimizer_rule: &OptimizerRule) -> Result<bool, StoreError> {
        let mut changed = false;
        for plan in self.plans.iter_mut() {
            changed |= optimizer_rule(plan)?;
        }
        Ok(changed)
    }
}

#[derive(Clone)]
/// Represents a logical `addV` step, adding a new vertex to the graph.
pub struct AddVStep {
    pub label: SmolStr,
    pub vertex_id: Option<VertexKey>,
    pub properties: SmallVec<[(PropKey, Primitive); VERTEX_PROPS_LENGTH]>,
}

impl Optimizer for AddVStep {}

/// Represents a logical `addE` step, adding a new edge to the graph.
#[derive(Clone)]
pub struct AddEStep {
    pub label: SmolStr,
    pub out_v_id: Option<VertexKey>,
    pub in_v_id: Option<VertexKey>,
    pub properties: SmallVec<[(PropKey, Primitive); VERTEX_PROPS_LENGTH]>,
    pub rank: Option<Rank>,
}

impl Optimizer for AddEStep {}

/// Represents a logical `from` step, specifying the source vertex for an edge.
#[derive(Clone)]
pub struct FromStep {
    pub vertex_id: VertexKey,
}

/// Implements the `Optimizer` trait for `FromStep`.
impl Optimizer for FromStep {}

#[derive(Clone)]
pub struct ToStep {
    pub vertex_id: VertexKey,
}

impl Optimizer for ToStep {}

/// Represents a logical `property` step, setting a property on an element.
#[derive(Clone)]
pub struct PropertyStep {
    pub prop_key: PropKey,
    pub prop_value: Primitive,
}

/// Implements the `Optimizer` trait for `PropertyStep`.
impl Optimizer for PropertyStep {}

#[derive(Clone)]
pub struct VStep {
    pub ids: SmallVec<[VertexKey; SMALL_VECTOR_LENGTH]>,
}

impl Optimizer for VStep {}

#[derive(Clone)]
pub struct EStep {
    pub keys: SmallVec<[String; SMALL_VECTOR_LENGTH]>,
}

impl Optimizer for EStep {}

/// Represents a logical `limit` step, restricting the number of traversers.
#[derive(Clone)]
pub struct LimitStep {
    pub limit: i64,
}

/// Implements the `Optimizer` trait for `LimitStep`.
impl Optimizer for LimitStep {}

#[derive(Clone)]
pub struct HasIdStep {
    pub pred: PrimitivePredicate,
}

impl Optimizer for HasIdStep {}