interstellar 0.2.0

A high-performance graph database with Gremlin-style traversals and GQL query language
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
//! Conditional and branching pattern tests.
//!
//! Tests for branching traversal patterns including:
//! - choose() for conditional branching
//! - coalesce() for fallback patterns
//! - union() for merging traversals
//! - optional() for optional steps
//! - and_()/or_() for logical combinations

#![allow(unused_variables)]

use std::collections::HashMap;

use interstellar::p;
use interstellar::storage::Graph;
use interstellar::traversal::__;
use interstellar::value::Value;

use crate::common::graphs::{create_medium_graph, create_small_graph};

// =============================================================================
// Choose (If-Then-Else)
// =============================================================================

#[test]
fn choose_with_predicate_true_branch() {
    let tg = create_medium_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // If person has status "active", go to knows, else go to created
    let results = g
        .v()
        .has_label("person")
        .choose(
            __.has_value("status", "active"),
            __.out_labels(&["knows"]),
            __.out_labels(&["created"]),
        )
        .to_list();

    // Alice and Charlie are active, Bob is inactive
    assert!(!results.is_empty());
}

#[test]
fn choose_with_predicate_false_branch() {
    let tg = create_medium_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // Start from Bob (inactive) specifically
    let results = g
        .v_ids([tg.bob])
        .choose(
            __.has_value("status", "active"),
            __.out_labels(&["knows"]),
            __.out_labels(&["created"]),
        )
        .to_list();

    // Bob is inactive, so should follow "created" branch
    // Bob created Redis
    assert!(!results.is_empty());
}

#[test]
fn choose_with_constant_branches() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // Return different constants based on label
    let results = g
        .v()
        .choose(
            __.has_label("person"),
            __.constant(Value::String("is_person".to_string())),
            __.constant(Value::String("is_other".to_string())),
        )
        .to_list();

    assert_eq!(results.len(), 4);
    let person_count = results
        .iter()
        .filter(|v| *v == &Value::String("is_person".to_string()))
        .count();
    assert_eq!(person_count, 3);
}

#[test]
fn choose_with_value_extraction() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // Extract different properties based on label
    let results = g
        .v()
        .choose(
            __.has_label("person"),
            __.values("age"),
            __.values("version"),
        )
        .to_list();

    // 3 ages + 1 version
    assert_eq!(results.len(), 4);
}

// =============================================================================
// Coalesce (First Non-Empty)
// =============================================================================

#[test]
fn coalesce_returns_first_match() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // Try to get nickname (doesn't exist), fall back to name
    let results = g
        .v()
        .has_label("person")
        .coalesce(vec![__.values("nickname"), __.values("name")])
        .to_list();

    // No nickname exists, so all should be names
    assert_eq!(results.len(), 3);
    assert!(results.contains(&Value::String("Alice".to_string())));
}

#[test]
fn coalesce_with_constant_fallback() {
    let graph = Graph::new();
    graph.add_vertex("test", {
        let mut props = HashMap::new();
        props.insert("name".to_string(), Value::String("HasName".to_string()));
        props
    });
    graph.add_vertex("test", HashMap::new()); // No name

    let snapshot = graph.snapshot();
    let g = snapshot.gremlin();

    let results = g
        .v()
        .coalesce(vec![
            __.values("name"),
            __.constant(Value::String("Unknown".to_string())),
        ])
        .to_list();

    assert_eq!(results.len(), 2);
    assert!(results.contains(&Value::String("HasName".to_string())));
    assert!(results.contains(&Value::String("Unknown".to_string())));
}

#[test]
fn coalesce_with_navigation() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // Try "manages" edges first, fall back to "knows"
    let results = g
        .v_ids([tg.alice])
        .coalesce(vec![__.out_labels(&["manages"]), __.out_labels(&["knows"])])
        .to_list();

    // No "manages" edges, so should return knows (Bob)
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].as_vertex_id(), Some(tg.bob));
}

#[test]
fn coalesce_all_empty_returns_empty() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // All traversals produce empty
    let results = g
        .v_ids([tg.graphdb])
        .coalesce(vec![__.out_labels(&["manages"]), __.out_labels(&["owns"])])
        .to_list();

    assert!(results.is_empty());
}

// =============================================================================
// Union (Merge Multiple Traversals)
// =============================================================================

#[test]
fn union_combines_traversals() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // Get both knows and uses edges from Alice
    let results = g
        .v_ids([tg.alice])
        .union(vec![__.out_labels(&["knows"]), __.out_labels(&["uses"])])
        .to_list();

    // Bob (knows) + GraphDB (uses)
    assert_eq!(results.len(), 2);
}

#[test]
fn union_with_different_depths() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // Combine 1-hop and 2-hop traversals
    let results = g
        .v_ids([tg.alice])
        .union(vec![
            __.out_labels(&["knows"]),
            __.out_labels(&["knows"]).out_labels(&["knows"]),
        ])
        .dedup()
        .to_list();

    // Bob (1-hop) and Charlie (2-hop)
    assert_eq!(results.len(), 2);
}

#[test]
fn union_with_values() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // Get both name and age from person
    let results = g
        .v_ids([tg.alice])
        .union(vec![__.values("name"), __.values("age")])
        .to_list();

    assert_eq!(results.len(), 2);
    assert!(results.contains(&Value::String("Alice".to_string())));
    assert!(results.contains(&Value::Int(30)));
}

#[test]
fn union_preserves_duplicates() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // Both branches return the same vertex
    let results = g
        .v_ids([tg.alice])
        .union(vec![__.out_labels(&["knows"]), __.out_labels(&["knows"])])
        .to_list();

    // Should have Bob twice (no automatic dedup)
    assert_eq!(results.len(), 2);
}

// =============================================================================
// Optional (May or May Not Match)
// =============================================================================

#[test]
fn optional_with_match() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // Optional knows traversal (exists for Alice)
    let results = g
        .v_ids([tg.alice])
        .optional(__.out_labels(&["knows"]))
        .to_list();

    // Should return Bob (the match)
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].as_vertex_id(), Some(tg.bob));
}

#[test]
fn optional_without_match_returns_input() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // Optional manages traversal (doesn't exist)
    let results = g
        .v_ids([tg.alice])
        .optional(__.out_labels(&["manages"]))
        .to_list();

    // Should return Alice (the input)
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].as_vertex_id(), Some(tg.alice));
}

#[test]
fn optional_in_chain() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // Chain with optional in the middle
    let results = g
        .v_ids([tg.alice])
        .out_labels(&["knows"])
        .optional(__.out_labels(&["manages"]))
        .values("name")
        .to_list();

    // Bob has no manages, so Bob is returned, then name extracted
    assert_eq!(results.len(), 1);
    assert_eq!(results[0], Value::String("Bob".to_string()));
}

// =============================================================================
// And/Or Logical Combinations
// =============================================================================

#[test]
fn and_requires_all_conditions() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // Person with age > 25 AND outgoing knows edge
    let results = g
        .v()
        .has_label("person")
        .and_(vec![
            __.has_where("age", p::gt(25i64)),
            __.out_labels(&["knows"]),
        ])
        .to_list();

    // Alice (30, knows Bob) and Charlie (35, knows Alice) - but need to check
    // Actually: Alice (30) has knows, Charlie (35) has knows
    assert!(results.len() >= 2);
}

#[test]
fn and_with_empty_branch_filters_all() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // Condition that can't be satisfied
    let results = g
        .v()
        .has_label("person")
        .and_(vec![
            __.has_where("age", p::gt(100i64)),
            __.out_labels(&["knows"]),
        ])
        .to_list();

    // No one is over 100
    assert!(results.is_empty());
}

#[test]
fn or_matches_any_condition() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // Person with age < 26 OR age > 34
    let results = g
        .v()
        .has_label("person")
        .or_(vec![
            __.has_where("age", p::lt(26i64)),
            __.has_where("age", p::gt(34i64)),
        ])
        .to_list();

    // Bob (25) and Charlie (35)
    assert_eq!(results.len(), 2);
}

#[test]
fn or_with_navigation_conditions() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // Vertices with knows OR uses outgoing edges
    let results = g
        .v()
        .or_(vec![__.out_labels(&["knows"]), __.out_labels(&["uses"])])
        .to_list();

    // Alice, Bob, Charlie have knows; Alice, Bob have uses
    assert!(!results.is_empty());
}

// =============================================================================
// Not (Negation)
// =============================================================================

#[test]
fn not_filters_matching() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // Vertices that do NOT have outgoing "knows" edges
    let results = g.v().not(__.out_labels(&["knows"])).to_list();

    // GraphDB has no outgoing edges
    assert!(!results.is_empty());
    // Should include GraphDB
    let ids: Vec<_> = results.iter().filter_map(|v| v.as_vertex_id()).collect();
    assert!(ids.contains(&tg.graphdb));
}

#[test]
fn not_with_property_condition() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // People who do NOT have age > 30
    let results = g
        .v()
        .has_label("person")
        .not(__.has_where("age", p::gt(30i64)))
        .to_list();

    // Alice (30) and Bob (25) - not greater than 30
    assert_eq!(results.len(), 2);
}

// =============================================================================
// Where Subtraversal
// =============================================================================

#[test]
fn where_with_exists_pattern() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // People who know someone
    let results = g
        .v()
        .has_label("person")
        .where_(__.out_labels(&["knows"]))
        .to_list();

    // Alice, Bob, Charlie all have outgoing knows
    assert_eq!(results.len(), 3);
}

#[test]
fn where_with_count_predicate() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // People who know at least 1 person
    let results = g
        .v()
        .has_label("person")
        .where_(__.out_labels(&["knows"]))
        .to_list();

    assert!(!results.is_empty());
}

#[test]
fn where_with_property_match() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // People who know someone younger than 30
    let results = g
        .v()
        .has_label("person")
        .where_(__.out_labels(&["knows"]).has_where("age", p::lt(30i64)))
        .to_list();

    // Alice knows Bob (25)
    assert!(!results.is_empty());
}

// =============================================================================
// Complex Conditional Patterns
// =============================================================================

#[test]
fn nested_choose() {
    let tg = create_medium_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // Nested conditional logic
    let results = g
        .v()
        .has_label("person")
        .choose(
            __.has_value("status", "active"),
            __.choose(
                __.has_where("age", p::gt(30i64)),
                __.constant(Value::String("active_senior".to_string())),
                __.constant(Value::String("active_junior".to_string())),
            ),
            __.constant(Value::String("inactive".to_string())),
        )
        .to_list();

    assert_eq!(results.len(), 3);
}

#[test]
fn coalesce_in_union() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // Complex combination
    let results = g
        .v_ids([tg.alice])
        .union(vec![
            __.out_labels(&["knows"]),
            __.coalesce(vec![__.out_labels(&["manages"]), __.out_labels(&["uses"])]),
        ])
        .to_list();

    // knows -> Bob, coalesce -> uses -> GraphDB
    assert_eq!(results.len(), 2);
}