ruvector-graph 2.0.6

Distributed Neo4j-compatible hypergraph database with SIMD optimization
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
//! Cypher query execution correctness tests
//!
//! Tests to verify that Cypher queries execute correctly and return expected results.

use ruvector_graph::{Edge, GraphDB, Label, Node, Properties, PropertyValue};

fn setup_test_graph() -> GraphDB {
    let db = GraphDB::new();

    // Create people
    let mut alice_props = Properties::new();
    alice_props.insert(
        "name".to_string(),
        PropertyValue::String("Alice".to_string()),
    );
    alice_props.insert("age".to_string(), PropertyValue::Integer(30));

    let mut bob_props = Properties::new();
    bob_props.insert("name".to_string(), PropertyValue::String("Bob".to_string()));
    bob_props.insert("age".to_string(), PropertyValue::Integer(35));

    let mut charlie_props = Properties::new();
    charlie_props.insert(
        "name".to_string(),
        PropertyValue::String("Charlie".to_string()),
    );
    charlie_props.insert("age".to_string(), PropertyValue::Integer(28));

    db.create_node(Node::new(
        "alice".to_string(),
        vec![Label {
            name: "Person".to_string(),
        }],
        alice_props,
    ))
    .unwrap();

    db.create_node(Node::new(
        "bob".to_string(),
        vec![Label {
            name: "Person".to_string(),
        }],
        bob_props,
    ))
    .unwrap();

    db.create_node(Node::new(
        "charlie".to_string(),
        vec![Label {
            name: "Person".to_string(),
        }],
        charlie_props,
    ))
    .unwrap();

    // Create relationships
    db.create_edge(Edge::new(
        "e1".to_string(),
        "alice".to_string(),
        "bob".to_string(),
        "KNOWS".to_string(),
        Properties::new(),
    ))
    .unwrap();

    db.create_edge(Edge::new(
        "e2".to_string(),
        "bob".to_string(),
        "charlie".to_string(),
        "KNOWS".to_string(),
        Properties::new(),
    ))
    .unwrap();

    db
}

#[test]
fn test_execute_simple_match_all_nodes() {
    let db = setup_test_graph();

    // TODO: Implement query execution
    // let results = db.execute("MATCH (n) RETURN n").unwrap();
    // assert_eq!(results.len(), 3);

    // For now, just verify the graph was set up correctly
    assert!(db.get_node("alice").is_some());
    assert!(db.get_node("bob").is_some());
    assert!(db.get_node("charlie").is_some());
}

#[test]
fn test_execute_match_with_label_filter() {
    let db = setup_test_graph();

    // TODO: Implement
    // let results = db.execute("MATCH (n:Person) RETURN n").unwrap();
    // assert_eq!(results.len(), 3);

    assert!(db.get_node("alice").is_some());
}

#[test]
fn test_execute_match_with_property_filter() {
    let db = setup_test_graph();

    // TODO: Implement
    // let results = db.execute("MATCH (n:Person {name: 'Alice'}) RETURN n").unwrap();
    // assert_eq!(results.len(), 1);

    let alice = db.get_node("alice").unwrap();
    assert_eq!(
        alice.properties.get("name"),
        Some(&PropertyValue::String("Alice".to_string()))
    );
}

#[test]
fn test_execute_match_with_where_clause() {
    let db = setup_test_graph();

    // TODO: Implement
    // let results = db.execute("MATCH (n:Person) WHERE n.age > 30 RETURN n").unwrap();
    // Should return Bob (35)
    // assert_eq!(results.len(), 1);

    let bob = db.get_node("bob").unwrap();
    if let Some(PropertyValue::Integer(age)) = bob.properties.get("age") {
        assert!(*age > 30);
    }
}

#[test]
fn test_execute_match_relationship() {
    let db = setup_test_graph();

    // TODO: Implement
    // let results = db.execute("MATCH (a)-[r:KNOWS]->(b) RETURN a, r, b").unwrap();
    // Should return 2 relationships

    assert!(db.get_edge("e1").is_some());
    assert!(db.get_edge("e2").is_some());
}

#[test]
fn test_execute_create_node() {
    let db = GraphDB::new();

    // TODO: Implement
    // db.execute("CREATE (n:Person {name: 'David', age: 40})").unwrap();

    // For now, create manually
    let mut props = Properties::new();
    props.insert(
        "name".to_string(),
        PropertyValue::String("David".to_string()),
    );
    props.insert("age".to_string(), PropertyValue::Integer(40));

    db.create_node(Node::new(
        "david".to_string(),
        vec![Label {
            name: "Person".to_string(),
        }],
        props,
    ))
    .unwrap();

    let david = db.get_node("david").unwrap();
    assert_eq!(
        david.properties.get("name"),
        Some(&PropertyValue::String("David".to_string()))
    );
}

#[test]
fn test_execute_count_aggregation() {
    let db = setup_test_graph();

    // TODO: Implement
    // let results = db.execute("MATCH (n:Person) RETURN COUNT(n) AS count").unwrap();
    // assert_eq!(results[0]["count"], 3);

    // Manual verification
    assert!(db.get_node("alice").is_some());
    assert!(db.get_node("bob").is_some());
    assert!(db.get_node("charlie").is_some());
}

#[test]
fn test_execute_sum_aggregation() {
    let db = setup_test_graph();

    // TODO: Implement
    // let results = db.execute("MATCH (n:Person) RETURN SUM(n.age) AS total_age").unwrap();
    // assert_eq!(results[0]["total_age"], 93); // 30 + 35 + 28

    // Manual verification
    let ages: Vec<i64> = ["alice", "bob", "charlie"]
        .iter()
        .filter_map(|id| {
            db.get_node(*id).and_then(|n| {
                if let Some(PropertyValue::Integer(age)) = n.properties.get("age") {
                    Some(*age)
                } else {
                    None
                }
            })
        })
        .collect();

    assert_eq!(ages.iter().sum::<i64>(), 93);
}

#[test]
fn test_execute_avg_aggregation() {
    let db = setup_test_graph();

    // TODO: Implement
    // let results = db.execute("MATCH (n:Person) RETURN AVG(n.age) AS avg_age").unwrap();
    // assert_eq!(results[0]["avg_age"], 31.0); // (30 + 35 + 28) / 3

    let ages: Vec<i64> = ["alice", "bob", "charlie"]
        .iter()
        .filter_map(|id| {
            db.get_node(*id).and_then(|n| {
                if let Some(PropertyValue::Integer(age)) = n.properties.get("age") {
                    Some(*age)
                } else {
                    None
                }
            })
        })
        .collect();

    let avg = ages.iter().sum::<i64>() as f64 / ages.len() as f64;
    assert!((avg - 31.0).abs() < 0.1);
}

#[test]
fn test_execute_order_by() {
    let db = setup_test_graph();

    // TODO: Implement
    // let results = db.execute("MATCH (n:Person) RETURN n ORDER BY n.age ASC").unwrap();
    // First should be Charlie (28), last should be Bob (35)

    let mut ages: Vec<i64> = ["alice", "bob", "charlie"]
        .iter()
        .filter_map(|id| {
            db.get_node(*id).and_then(|n| {
                if let Some(PropertyValue::Integer(age)) = n.properties.get("age") {
                    Some(*age)
                } else {
                    None
                }
            })
        })
        .collect();

    ages.sort();
    assert_eq!(ages, vec![28, 30, 35]);
}

#[test]
fn test_execute_limit() {
    let db = setup_test_graph();

    // TODO: Implement
    // let results = db.execute("MATCH (n:Person) RETURN n LIMIT 2").unwrap();
    // assert_eq!(results.len(), 2);

    assert!(db.get_node("alice").is_some());
}

#[test]
fn test_execute_path_query() {
    let db = setup_test_graph();

    // TODO: Implement
    // let results = db.execute("MATCH p = (a:Person)-[:KNOWS*1..2]->(b:Person) RETURN p").unwrap();
    // Should find paths: Alice->Bob, Bob->Charlie, Alice->Bob->Charlie

    let e1 = db.get_edge("e1").unwrap();
    let e2 = db.get_edge("e2").unwrap();

    assert_eq!(e1.from, "alice");
    assert_eq!(e1.to, "bob");
    assert_eq!(e2.from, "bob");
    assert_eq!(e2.to, "charlie");
}

// ============================================================================
// Complex Query Execution Tests
// ============================================================================

#[test]
fn test_execute_multi_hop_traversal() {
    let db = setup_test_graph();

    // TODO: Implement
    // Find all people connected to Alice within 2 hops
    // let results = db.execute("
    //     MATCH (alice:Person {name: 'Alice'})-[:KNOWS*1..2]->(connected)
    //     RETURN DISTINCT connected.name
    // ").unwrap();

    // Should find Bob (1 hop) and Charlie (2 hops)

    assert!(db.get_node("bob").is_some());
    assert!(db.get_node("charlie").is_some());
}

#[test]
fn test_execute_pattern_matching() {
    let db = setup_test_graph();

    // TODO: Implement
    // let results = db.execute("
    //     MATCH (a:Person)-[:KNOWS]->(b:Person)-[:KNOWS]->(c:Person)
    //     RETURN a.name, c.name
    // ").unwrap();

    // Should find Alice knows Charlie through Bob

    assert!(db.get_edge("e1").is_some());
    assert!(db.get_edge("e2").is_some());
}

#[test]
fn test_execute_collect_aggregation() {
    let db = setup_test_graph();

    // TODO: Implement
    // let results = db.execute("
    //     MATCH (p:Person)-[:KNOWS]->(friend)
    //     RETURN p.name, COLLECT(friend.name) AS friends
    // ").unwrap();

    // Alice: [Bob], Bob: [Charlie], Charlie: []

    assert!(db.get_edge("e1").is_some());
}

#[test]
fn test_execute_optional_match() {
    let db = setup_test_graph();

    // TODO: Implement
    // let results = db.execute("
    //     MATCH (p:Person)
    //     OPTIONAL MATCH (p)-[:KNOWS]->(friend)
    //     RETURN p.name, friend.name
    // ").unwrap();

    // Should return all people, some with null friends

    assert!(db.get_node("charlie").is_some());
}

// ============================================================================
// Result Verification Tests
// ============================================================================

#[test]
fn test_query_result_schema() {
    // TODO: Implement
    // Verify that query results have correct schema
    // let db = setup_test_graph();
    // let results = db.execute("MATCH (n:Person) RETURN n.name AS name, n.age AS age").unwrap();
    // assert!(results.has_column("name"));
    // assert!(results.has_column("age"));
}

#[test]
fn test_query_result_ordering() {
    // TODO: Implement
    // Verify that ORDER BY is correctly applied
}

#[test]
fn test_query_result_pagination() {
    // TODO: Implement
    // Verify SKIP and LIMIT work correctly together
}

// ============================================================================
// Error Handling Tests
// ============================================================================

#[test]
fn test_execute_invalid_property_access() {
    // TODO: Implement
    // let db = setup_test_graph();
    // let result = db.execute("MATCH (n:Person) WHERE n.nonexistent > 5 RETURN n");
    // Should handle gracefully (return no results or error depending on semantics)
}

#[test]
fn test_execute_type_mismatch() {
    // TODO: Implement
    // let db = setup_test_graph();
    // let result = db.execute("MATCH (n:Person) WHERE n.name > 5 RETURN n");
    // Should handle type mismatch error
}