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
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
//! Additional coverage tests for traversal/transform/order.rs
//!
//! This module covers edge cases and branches not covered by inline tests,
//! focusing on:
//! - Mixed type comparisons in sorting
//! - Bool sorting
//! - Edge property sorting
//! - None/missing value handling
//! - Sub-traversal sorting

#![allow(unused_variables)]
use interstellar::storage::Graph;
use interstellar::traversal::SnapshotLike;
use interstellar::value::{Value, VertexId};
use std::collections::HashMap;

// =============================================================================
// Helper Functions
// =============================================================================

fn create_mixed_type_graph() -> Graph {
    let graph = Graph::new();

    // Vertex 0: int value
    let mut props = HashMap::new();
    props.insert("value".to_string(), Value::Int(10));
    graph.add_vertex("item", props);

    // Vertex 1: float value
    let mut props = HashMap::new();
    props.insert("value".to_string(), Value::Float(5.5));
    graph.add_vertex("item", props);

    // Vertex 2: string value
    let mut props = HashMap::new();
    props.insert("value".to_string(), Value::String("abc".to_string()));
    graph.add_vertex("item", props);

    // Vertex 3: bool value
    let mut props = HashMap::new();
    props.insert("value".to_string(), Value::Bool(true));
    graph.add_vertex("item", props);

    graph
}

fn create_bool_graph() -> Graph {
    let graph = Graph::new();

    let mut props = HashMap::new();
    props.insert("active".to_string(), Value::Bool(false));
    graph.add_vertex("flag", props);

    let mut props = HashMap::new();
    props.insert("active".to_string(), Value::Bool(true));
    graph.add_vertex("flag", props);

    let mut props = HashMap::new();
    props.insert("active".to_string(), Value::Bool(false));
    graph.add_vertex("flag", props);

    graph
}

fn create_edge_graph() -> Graph {
    let graph = Graph::new();

    graph.add_vertex("person", HashMap::new());
    graph.add_vertex("person", HashMap::new());
    graph.add_vertex("person", HashMap::new());

    // Edge with weight 0.5
    let mut props = HashMap::new();
    props.insert("weight".to_string(), Value::Float(0.5));
    graph
        .add_edge(VertexId(0), VertexId(1), "knows", props)
        .unwrap();

    // Edge with weight 0.8
    let mut props = HashMap::new();
    props.insert("weight".to_string(), Value::Float(0.8));
    graph
        .add_edge(VertexId(1), VertexId(2), "knows", props)
        .unwrap();

    // Edge with weight 0.2
    let mut props = HashMap::new();
    props.insert("weight".to_string(), Value::Float(0.2));
    graph
        .add_edge(VertexId(0), VertexId(2), "knows", props)
        .unwrap();

    graph
}

fn create_missing_prop_graph() -> Graph {
    let graph = Graph::new();

    // Vertex with age
    let mut props = HashMap::new();
    props.insert("name".to_string(), Value::String("Alice".to_string()));
    props.insert("age".to_string(), Value::Int(30));
    graph.add_vertex("person", props);

    // Vertex without age
    let mut props = HashMap::new();
    props.insert("name".to_string(), Value::String("Bob".to_string()));
    graph.add_vertex("person", props);

    // Vertex with age
    let mut props = HashMap::new();
    props.insert("name".to_string(), Value::String("Charlie".to_string()));
    props.insert("age".to_string(), Value::Int(25));
    graph.add_vertex("person", props);

    graph
}

// =============================================================================
// Mixed Type Sorting Tests
// =============================================================================

mod mixed_type_sorting {
    use super::*;

    #[test]
    fn order_by_mixed_type_property_asc() {
        let graph = create_mixed_type_graph();
        let snapshot = graph.snapshot();
        let g = snapshot.gremlin();

        // Sort by value property which has different types
        let results = g
            .v()
            .has_label("item")
            .order()
            .by_key_asc("value")
            .build()
            .to_list();

        // Should sort by type discriminant when types differ
        // Int < Float < String < Bool (based on compare_values impl)
        assert_eq!(results.len(), 4);
    }

    #[test]
    fn order_natural_with_mixed_value_types() {
        let graph = Graph::new();
        let snapshot = graph.snapshot();
        let g = snapshot.gremlin();

        // Inject mixed types and sort naturally
        let results = g
            .inject([
                Value::Bool(true),
                Value::String("hello".to_string()),
                Value::Int(42),
                Value::Float(3.14),
            ])
            .order()
            .by_asc()
            .build()
            .to_list();

        assert_eq!(results.len(), 4);
        // First should be Int (lowest discriminant for non-null)
        assert!(matches!(results[0], Value::Int(_)));
    }
}

// =============================================================================
// Bool Sorting Tests
// =============================================================================

mod bool_sorting {
    use super::*;

    #[test]
    fn order_by_bool_property_ascending() {
        let graph = create_bool_graph();
        let snapshot = graph.snapshot();
        let g = snapshot.gremlin();

        let results = g
            .v()
            .has_label("flag")
            .order()
            .by_key_asc("active")
            .build()
            .to_list();

        assert_eq!(results.len(), 3);
        // false < true, so false values should come first
        // We can verify by checking the first vertex
        if let Value::Vertex(id) = &results[0] {
            let vertex = snapshot.storage().get_vertex(*id).unwrap();
            assert_eq!(vertex.properties.get("active"), Some(&Value::Bool(false)));
        }
    }

    #[test]
    fn order_by_bool_property_descending() {
        let graph = create_bool_graph();
        let snapshot = graph.snapshot();
        let g = snapshot.gremlin();

        let results = g
            .v()
            .has_label("flag")
            .order()
            .by_key_desc("active")
            .build()
            .to_list();

        assert_eq!(results.len(), 3);
        // true > false, so true values should come first
        if let Value::Vertex(id) = &results[0] {
            let vertex = snapshot.storage().get_vertex(*id).unwrap();
            assert_eq!(vertex.properties.get("active"), Some(&Value::Bool(true)));
        }
    }

    #[test]
    fn order_natural_bools_ascending() {
        let graph = Graph::new();
        let snapshot = graph.snapshot();
        let g = snapshot.gremlin();

        let results = g
            .inject([Value::Bool(true), Value::Bool(false), Value::Bool(true)])
            .order()
            .by_asc()
            .build()
            .to_list();

        assert_eq!(results.len(), 3);
        assert_eq!(results[0], Value::Bool(false));
        assert_eq!(results[1], Value::Bool(true));
        assert_eq!(results[2], Value::Bool(true));
    }
}

// =============================================================================
// Edge Sorting Tests
// =============================================================================

mod edge_sorting {
    use super::*;

    #[test]
    fn order_edges_by_property_ascending() {
        let graph = create_edge_graph();
        let snapshot = graph.snapshot();
        let g = snapshot.gremlin();

        let results = g.e().order().by_key_asc("weight").build().to_list();

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

        // Extract weights in order
        let weights: Vec<f64> = results
            .iter()
            .filter_map(|v| {
                if let Value::Edge(id) = v {
                    snapshot
                        .storage()
                        .get_edge(*id)
                        .and_then(|e| e.properties.get("weight").cloned())
                        .and_then(|w| {
                            if let Value::Float(f) = w {
                                Some(f)
                            } else {
                                None
                            }
                        })
                } else {
                    None
                }
            })
            .collect();

        // Should be sorted: 0.2, 0.5, 0.8
        assert!((weights[0] - 0.2).abs() < 0.001);
        assert!((weights[1] - 0.5).abs() < 0.001);
        assert!((weights[2] - 0.8).abs() < 0.001);
    }

    #[test]
    fn order_edges_by_property_descending() {
        let graph = create_edge_graph();
        let snapshot = graph.snapshot();
        let g = snapshot.gremlin();

        let results = g.e().order().by_key_desc("weight").build().to_list();

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

        // Extract weights in order
        let weights: Vec<f64> = results
            .iter()
            .filter_map(|v| {
                if let Value::Edge(id) = v {
                    snapshot
                        .storage()
                        .get_edge(*id)
                        .and_then(|e| e.properties.get("weight").cloned())
                        .and_then(|w| {
                            if let Value::Float(f) = w {
                                Some(f)
                            } else {
                                None
                            }
                        })
                } else {
                    None
                }
            })
            .collect();

        // Should be sorted descending: 0.8, 0.5, 0.2
        assert!((weights[0] - 0.8).abs() < 0.001);
        assert!((weights[1] - 0.5).abs() < 0.001);
        assert!((weights[2] - 0.2).abs() < 0.001);
    }
}

// =============================================================================
// Missing Property Sorting Tests
// =============================================================================

mod missing_property_sorting {
    use super::*;

    #[test]
    fn order_with_some_missing_properties() {
        let graph = create_missing_prop_graph();
        let snapshot = graph.snapshot();
        let g = snapshot.gremlin();

        // Sort by age - one vertex doesn't have age property
        let results = g
            .v()
            .has_label("person")
            .order()
            .by_key_asc("age")
            .build()
            .to_list();

        assert_eq!(results.len(), 3);
        // Vertices with values should come before None values
        // So Charlie (25) and Alice (30) should come before Bob (no age)
    }

    #[test]
    fn order_by_nonexistent_property() {
        let graph = create_missing_prop_graph();
        let snapshot = graph.snapshot();
        let g = snapshot.gremlin();

        // Sort by property that doesn't exist on any vertex
        let results = g
            .v()
            .has_label("person")
            .order()
            .by_key_asc("nonexistent")
            .build()
            .to_list();

        // Should still return all vertices (order may be arbitrary)
        assert_eq!(results.len(), 3);
    }
}

// =============================================================================
// Sub-traversal Sorting Tests
// =============================================================================

mod subtraversal_sorting {
    use super::*;
    use interstellar::traversal::__;

    #[test]
    fn order_by_subtraversal_property() {
        let graph = Graph::new();

        let mut props = HashMap::new();
        props.insert("name".to_string(), Value::String("Alice".to_string()));
        props.insert("score".to_string(), Value::Int(100));
        graph.add_vertex("person", props);

        let mut props = HashMap::new();
        props.insert("name".to_string(), Value::String("Bob".to_string()));
        props.insert("score".to_string(), Value::Int(50));
        graph.add_vertex("person", props);

        let mut props = HashMap::new();
        props.insert("name".to_string(), Value::String("Charlie".to_string()));
        props.insert("score".to_string(), Value::Int(75));
        graph.add_vertex("person", props);

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

        // Sort by score using a sub-traversal
        let sub = __.values("score");
        let results = g
            .v()
            .has_label("person")
            .order()
            .by_traversal(sub, true) // descending
            .build()
            .to_list();

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

        // Extract names in order - should be Alice, Charlie, Bob (by score desc)
        let names: Vec<String> = results
            .iter()
            .filter_map(|v| {
                if let Value::Vertex(id) = v {
                    snapshot
                        .storage()
                        .get_vertex(*id)
                        .and_then(|v| v.properties.get("name").cloned())
                        .and_then(|n| {
                            if let Value::String(s) = n {
                                Some(s)
                            } else {
                                None
                            }
                        })
                } else {
                    None
                }
            })
            .collect();

        assert_eq!(names[0], "Alice");
        assert_eq!(names[1], "Charlie");
        assert_eq!(names[2], "Bob");
    }
}

// =============================================================================
// OrderBuilder Edge Cases
// =============================================================================

mod order_builder_edge_cases {
    use super::*;

    #[test]
    fn order_with_no_by_clause_defaults_to_asc() {
        let graph = Graph::new();
        let snapshot = graph.snapshot();
        let g = snapshot.gremlin();

        // order() with no by_* calls should default to natural ascending
        let results = g
            .inject([Value::Int(3), Value::Int(1), Value::Int(2)])
            .order()
            .build()
            .to_list();

        assert_eq!(results.len(), 3);
        assert_eq!(results[0], Value::Int(1));
        assert_eq!(results[1], Value::Int(2));
        assert_eq!(results[2], Value::Int(3));
    }

    #[test]
    fn order_with_multiple_by_clauses() {
        let graph = Graph::new();

        // Same last name, different first names
        let mut props = HashMap::new();
        props.insert("first".to_string(), Value::String("Alice".to_string()));
        props.insert("last".to_string(), Value::String("Smith".to_string()));
        graph.add_vertex("person", props);

        let mut props = HashMap::new();
        props.insert("first".to_string(), Value::String("Bob".to_string()));
        props.insert("last".to_string(), Value::String("Jones".to_string()));
        graph.add_vertex("person", props);

        let mut props = HashMap::new();
        props.insert("first".to_string(), Value::String("Charlie".to_string()));
        props.insert("last".to_string(), Value::String("Smith".to_string()));
        graph.add_vertex("person", props);

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

        // Sort by last name asc, then first name asc
        let results = g
            .v()
            .has_label("person")
            .order()
            .by_key_asc("last")
            .by_key_asc("first")
            .build()
            .to_list();

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

        // Jones should come before Smith (alphabetically)
        // Among Smiths, Alice should come before Charlie
        let names: Vec<(String, String)> = results
            .iter()
            .filter_map(|v| {
                if let Value::Vertex(id) = v {
                    let vertex = snapshot.storage().get_vertex(*id)?;
                    let first = match vertex.properties.get("first")? {
                        Value::String(s) => s.clone(),
                        _ => return None,
                    };
                    let last = match vertex.properties.get("last")? {
                        Value::String(s) => s.clone(),
                        _ => return None,
                    };
                    Some((last, first))
                } else {
                    None
                }
            })
            .collect();

        assert_eq!(names[0], ("Jones".to_string(), "Bob".to_string()));
        assert_eq!(names[1], ("Smith".to_string(), "Alice".to_string()));
        assert_eq!(names[2], ("Smith".to_string(), "Charlie".to_string()));
    }
}

// =============================================================================
// Non-element Value Sorting
// =============================================================================

mod non_element_sorting {
    use super::*;

    #[test]
    fn order_null_values() {
        let graph = Graph::new();
        let snapshot = graph.snapshot();
        let g = snapshot.gremlin();

        let results = g
            .inject([Value::Null, Value::Int(1), Value::Null, Value::Int(2)])
            .order()
            .by_asc()
            .build()
            .to_list();

        assert_eq!(results.len(), 4);
        // Ints come before other types including Null
        assert!(matches!(results[0], Value::Int(_)));
        assert!(matches!(results[1], Value::Int(_)));
    }

    #[test]
    fn order_list_values() {
        let graph = Graph::new();
        let snapshot = graph.snapshot();
        let g = snapshot.gremlin();

        let results = g
            .inject([
                Value::List(vec![Value::Int(1), Value::Int(2)]),
                Value::Int(5),
                Value::List(vec![Value::Int(3)]),
            ])
            .order()
            .by_asc()
            .build()
            .to_list();

        assert_eq!(results.len(), 3);
        // Int comes before List
        assert!(matches!(results[0], Value::Int(_)));
    }

    #[test]
    fn order_map_values() {
        let graph = Graph::new();
        let snapshot = graph.snapshot();
        let g = snapshot.gremlin();

        let mut map1 = ::indexmap::IndexMap::<String, Value>::new();
        map1.insert("key".to_string(), Value::Int(1));

        let results = g
            .inject([Value::Map(map1), Value::Int(5)])
            .order()
            .by_asc()
            .build()
            .to_list();

        assert_eq!(results.len(), 2);
        // Int comes before Map
        assert!(matches!(results[0], Value::Int(_)));
    }
}