grafeo-engine 0.5.33

Query engine and database management for Grafeo
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
//! Tests for aggregate translator and execution coverage gaps.
//!
//! Targets: aggregate.rs (45.45%), common.rs (64.48%), expression.rs (82.32%)
//!
//! ```bash
//! cargo test -p grafeo-engine --test coverage_aggregates
//! ```

use grafeo_common::types::Value;
use grafeo_engine::GrafeoDB;

/// Creates 5 Data nodes (Alix, Gus, Vincent, Jules, Mia) with x/y/score properties.
fn stats_graph() -> GrafeoDB {
    let db = GrafeoDB::new_in_memory();
    let session = db.session();
    for (name, x, y) in [
        ("Alix", 1.0, 2.0),
        ("Gus", 2.0, 4.0),
        ("Vincent", 3.0, 6.0),
        ("Jules", 4.0, 8.0),
        ("Mia", 5.0, 10.0),
    ] {
        session.create_node_with_props(
            &["Data"],
            [
                ("name", Value::String(name.into())),
                ("x", Value::Float64(x)),
                ("y", Value::Float64(y)),
                ("score", Value::Int64(x as i64 * 10)),
            ],
        );
    }
    db
}

// ---------------------------------------------------------------------------
// Wrapped aggregates: exercises extract_wrapped_aggregate (Binary branch)
// ---------------------------------------------------------------------------

#[test]
fn test_wrapped_aggregate_count_gt_zero() {
    let db = stats_graph();
    let s = db.session();
    let r = s
        .execute("MATCH (d:Data) RETURN count(d) > 0 AS has_data")
        .unwrap();
    assert_eq!(r.rows.len(), 1);
    assert_eq!(r.rows[0][0], Value::Bool(true));
}

#[test]
fn test_wrapped_aggregate_sum_minus_literal() {
    let db = stats_graph();
    let s = db.session();
    let r = s
        .execute("MATCH (d:Data) RETURN sum(d.score) - 10 AS adjusted")
        .unwrap();
    assert_eq!(r.rows.len(), 1);
    // sum(10+20+30+40+50) - 10 = 140
    assert_eq!(r.rows[0][0], Value::Int64(140));
}

#[test]
fn test_wrapped_aggregate_not_count() {
    let db = stats_graph();
    let s = db.session();
    let r = s
        .execute("MATCH (d:Data) RETURN NOT (count(d) > 100) AS few")
        .unwrap();
    assert_eq!(r.rows.len(), 1);
    assert_eq!(r.rows[0][0], Value::Bool(true));
}

// ---------------------------------------------------------------------------
// GROUP_CONCAT / LISTAGG with separator
// ---------------------------------------------------------------------------

#[test]
fn test_group_concat_default_separator() {
    let db = stats_graph();
    let s = db.session();
    let r = s
        .execute("MATCH (d:Data) RETURN group_concat(d.name) AS names")
        .unwrap();
    assert_eq!(r.rows.len(), 1);
    if let Value::String(names) = &r.rows[0][0] {
        assert_eq!(names.split(' ').count(), 5);
    } else {
        panic!("expected string, got {:?}", r.rows[0][0]);
    }
}

#[test]
fn test_group_concat_custom_separator() {
    let db = stats_graph();
    let s = db.session();
    let r = s
        .execute("MATCH (d:Data) RETURN group_concat(d.name, ';') AS names")
        .unwrap();
    assert_eq!(r.rows.len(), 1);
    if let Value::String(names) = &r.rows[0][0] {
        assert!(names.contains(';'), "expected semicolons: {names}");
        assert_eq!(names.split(';').count(), 5);
    } else {
        panic!("expected string, got {:?}", r.rows[0][0]);
    }
}

#[test]
fn test_listagg_default_comma() {
    let db = stats_graph();
    let s = db.session();
    let r = s
        .execute("MATCH (d:Data) RETURN listagg(d.name) AS names")
        .unwrap();
    assert_eq!(r.rows.len(), 1);
    if let Value::String(names) = &r.rows[0][0] {
        assert!(names.contains(','), "expected commas: {names}");
    } else {
        panic!("expected string, got {:?}", r.rows[0][0]);
    }
}

// ---------------------------------------------------------------------------
// SAMPLE aggregate
// ---------------------------------------------------------------------------

#[test]
fn test_sample_returns_one_value() {
    let db = stats_graph();
    let s = db.session();
    let r = s
        .execute("MATCH (d:Data) RETURN sample(d.name) AS picked")
        .unwrap();
    assert_eq!(r.rows.len(), 1);
    if let Value::String(name) = &r.rows[0][0] {
        assert!(
            ["Alix", "Gus", "Vincent", "Jules", "Mia"].contains(&name.as_str()),
            "unexpected: {name}"
        );
    } else {
        panic!("expected string, got {:?}", r.rows[0][0]);
    }
}

// ---------------------------------------------------------------------------
// COLLECT aggregate
// ---------------------------------------------------------------------------

#[test]
fn test_collect_to_list() {
    let db = stats_graph();
    let s = db.session();
    let r = s
        .execute("MATCH (d:Data) RETURN collect(d.score) AS scores")
        .unwrap();
    assert_eq!(r.rows.len(), 1);
    if let Value::List(items) = &r.rows[0][0] {
        assert_eq!(items.len(), 5);
    } else {
        panic!("expected list, got {:?}", r.rows[0][0]);
    }
}

// ---------------------------------------------------------------------------
// PERCENTILE with integer parameter (exercises int-to-float branch)
// ---------------------------------------------------------------------------

#[test]
fn test_percentile_disc_with_integer_param() {
    let db = stats_graph();
    let s = db.session();
    // percentile_disc(score, 1) should return max value
    let r = s
        .execute("MATCH (d:Data) RETURN percentile_disc(d.score, 1) AS p100")
        .unwrap();
    assert_eq!(r.rows.len(), 1);
    // May return Float64 or Int64 depending on implementation
    match &r.rows[0][0] {
        Value::Int64(v) => assert_eq!(*v, 50),
        Value::Float64(v) => assert!((*v - 50.0).abs() < 0.01),
        other => panic!("expected numeric, got {other:?}"),
    }
}

#[test]
fn test_percentile_cont_zero() {
    let db = stats_graph();
    let s = db.session();
    let r = s
        .execute("MATCH (d:Data) RETURN percentile_cont(d.score, 0) AS p0")
        .unwrap();
    assert_eq!(r.rows.len(), 1);
    match &r.rows[0][0] {
        Value::Float64(v) => assert!((*v - 10.0).abs() < 0.01),
        Value::Int64(v) => assert_eq!(*v, 10),
        other => panic!("expected numeric, got {other:?}"),
    }
}

// ---------------------------------------------------------------------------
// STDEV / VARIANCE (sample and population)
// ---------------------------------------------------------------------------

#[test]
fn test_stdev_and_stdevp() {
    let db = stats_graph();
    let s = db.session();
    let r = s
        .execute("MATCH (d:Data) RETURN stdev(d.score) AS s, stdevp(d.score) AS sp")
        .unwrap();
    assert_eq!(r.rows.len(), 1);
    if let Value::Float64(s) = r.rows[0][0] {
        assert!(s > 0.0);
    }
    if let Value::Float64(sp) = r.rows[0][1] {
        assert!(sp > 0.0);
    }
}

#[test]
fn test_variance_and_variance_pop() {
    let db = stats_graph();
    let s = db.session();
    let r = s
        .execute("MATCH (d:Data) RETURN variance(d.score) AS v, var_pop(d.score) AS vp")
        .unwrap();
    assert_eq!(r.rows.len(), 1);
    if let Value::Float64(v) = r.rows[0][0] {
        assert!(v > 0.0);
    }
}

// ---------------------------------------------------------------------------
// Multiple aggregates + GROUP BY in one query (uses WITH for grouping)
// ---------------------------------------------------------------------------

#[test]
fn test_mixed_aggregates_with_group_by() {
    let db = GrafeoDB::new_in_memory();
    let session = db.session();
    for (name, city, score) in [
        ("Alix", "Amsterdam", 80),
        ("Gus", "Amsterdam", 90),
        ("Vincent", "Berlin", 70),
        ("Jules", "Berlin", 85),
    ] {
        session.create_node_with_props(
            &["Person"],
            [
                ("name", Value::String(name.into())),
                ("city", Value::String(city.into())),
                ("score", Value::Int64(score)),
            ],
        );
    }
    // Use RETURN with aggregates directly: non-aggregated p.city acts as grouping key
    let r = session
        .execute(
            "MATCH (p:Person) \
             RETURN p.city AS city, count(p) AS cnt, min(p.score) AS lo, max(p.score) AS hi",
        )
        .unwrap();
    assert_eq!(r.rows.len(), 2);
    // Both groups should have count=2
    assert_eq!(r.rows[0][1], Value::Int64(2));
    assert_eq!(r.rows[1][1], Value::Int64(2));
}

// ---------------------------------------------------------------------------
// ORDER BY alias after aggregation (was: "Undefined variable 'city'")
// ---------------------------------------------------------------------------

#[test]
fn test_order_by_alias_after_aggregation() {
    let db = GrafeoDB::new_in_memory();
    let session = db.session();
    for (name, city) in [
        ("Alix", "Berlin"),
        ("Gus", "Amsterdam"),
        ("Vincent", "Berlin"),
        ("Jules", "Amsterdam"),
        ("Mia", "Paris"),
    ] {
        session.create_node_with_props(
            &["Person"],
            [
                ("name", Value::String(name.into())),
                ("city", Value::String(city.into())),
            ],
        );
    }
    let r = session
        .execute("MATCH (p:Person) RETURN p.city AS city, count(p) AS cnt ORDER BY city")
        .unwrap();
    assert_eq!(r.rows.len(), 3);
    // Sorted ascending by city: Amsterdam, Berlin, Paris
    assert_eq!(r.rows[0][0], Value::String("Amsterdam".into()));
    assert_eq!(r.rows[1][0], Value::String("Berlin".into()));
    assert_eq!(r.rows[2][0], Value::String("Paris".into()));
}

#[test]
fn test_order_by_aggregate_alias_desc() {
    let db = GrafeoDB::new_in_memory();
    let session = db.session();
    for (name, city) in [
        ("Alix", "Berlin"),
        ("Gus", "Amsterdam"),
        ("Vincent", "Berlin"),
        ("Jules", "Amsterdam"),
        ("Mia", "Paris"),
    ] {
        session.create_node_with_props(
            &["Person"],
            [
                ("name", Value::String(name.into())),
                ("city", Value::String(city.into())),
            ],
        );
    }
    let r = session
        .execute("MATCH (p:Person) RETURN p.city AS city, count(p) AS cnt ORDER BY cnt DESC")
        .unwrap();
    assert_eq!(r.rows.len(), 3);
    // DESC by cnt: Amsterdam(2) and Berlin(2) first, Paris(1) last
    assert_eq!(r.rows[0][1], Value::Int64(2));
    assert_eq!(r.rows[1][1], Value::Int64(2));
    assert_eq!(r.rows[2][1], Value::Int64(1));
    // Paris must be last
    assert_eq!(r.rows[2][0], Value::String("Paris".into()));
}

// ---------------------------------------------------------------------------
// Expression coverage: NULLIF
// ---------------------------------------------------------------------------

#[test]
fn test_nullif_expression() {
    let db = stats_graph();
    let s = db.session();
    let r = s
        .execute("MATCH (d:Data) RETURN nullif(d.score, 10) AS v ORDER BY d.x")
        .unwrap();
    // First person (score=10) should get NULL
    assert_eq!(r.rows[0][0], Value::Null);
    // Second person (score=20) should keep 20
    assert_eq!(r.rows[1][0], Value::Int64(20));
}

// ---------------------------------------------------------------------------
// List predicates via Cypher-compatible syntax
// ---------------------------------------------------------------------------

// Tests all()/any() in WHERE clause (RETURN variant in gql_spec_compliance.rs)

#[test]
fn test_list_predicate_all() {
    let db = GrafeoDB::new_in_memory();
    let s = db.session();
    s.create_node_with_props(&["Flag"], [("v", Value::Int64(1))]);
    let r = s
        .execute("MATCH (f:Flag) WHERE all(x IN [2, 4, 6] WHERE x % 2 = 0) RETURN f.v AS v")
        .unwrap();
    assert_eq!(r.rows.len(), 1);
}

#[test]
fn test_list_predicate_any() {
    let db = GrafeoDB::new_in_memory();
    let s = db.session();
    s.create_node_with_props(&["Flag"], [("v", Value::Int64(1))]);
    let r = s
        .execute("MATCH (f:Flag) WHERE any(x IN [1, 2, 3] WHERE x > 2) RETURN f.v AS v")
        .unwrap();
    assert_eq!(r.rows.len(), 1);
}

// ---------------------------------------------------------------------------
// Wrapped aggregate: sum(x) * 2 (right-side aggregate in binary)
// ---------------------------------------------------------------------------

#[test]
fn test_wrapped_aggregate_literal_plus_count() {
    let db = stats_graph();
    let s = db.session();
    // 100 + count(d): aggregate on the right side of binary
    let r = s
        .execute("MATCH (d:Data) RETURN 100 + count(d) AS total")
        .unwrap();
    assert_eq!(r.rows.len(), 1);
    assert_eq!(r.rows[0][0], Value::Int64(105));
}

// ---------------------------------------------------------------------------
// COUNT(DISTINCT x) and COUNT(x) non-null
// ---------------------------------------------------------------------------

#[test]
fn test_count_distinct() {
    let db = GrafeoDB::new_in_memory();
    let session = db.session();
    for city in ["Amsterdam", "Amsterdam", "Berlin", "Berlin", "Paris"] {
        session.create_node_with_props(&["City"], [("name", Value::String(city.into()))]);
    }
    let r = session
        .execute("MATCH (c:City) RETURN count(DISTINCT c.name) AS unique_cities")
        .unwrap();
    assert_eq!(r.rows[0][0], Value::Int64(3));
}

#[test]
fn test_count_expression_non_null() {
    let db = GrafeoDB::new_in_memory();
    let session = db.session();
    session.create_node_with_props(&["Item"], [("val", Value::Int64(1))]);
    session.create_node_with_props(&["Item"], [("val", Value::Null)]);
    session.create_node_with_props(&["Item"], [("val", Value::Int64(3))]);

    let r = session
        .execute("MATCH (i:Item) RETURN count(i.val) AS cnt")
        .unwrap();
    assert_eq!(r.rows[0][0], Value::Int64(2));
}

// ===========================================================================
// T2-01: HAVING clause tests
// ===========================================================================

#[test]
fn test_having_filters_groups() {
    let db = GrafeoDB::new_in_memory();
    let session = db.session();
    for (name, city) in [
        ("Alix", "Amsterdam"),
        ("Gus", "Amsterdam"),
        ("Vincent", "Berlin"),
        ("Jules", "Berlin"),
        ("Mia", "Paris"),
    ] {
        session.create_node_with_props(
            &["Person"],
            [
                ("name", Value::String(name.into())),
                ("city", Value::String(city.into())),
            ],
        );
    }
    let r = session
        .execute(
            "MATCH (p:Person) \
             RETURN p.city AS city, count(p) AS cnt \
             ORDER BY city \
             HAVING cnt > 1",
        )
        .unwrap();
    // Only Amsterdam (2) and Berlin (2) qualify; Paris (1) is filtered out
    assert_eq!(r.rows.len(), 2, "HAVING should filter groups with cnt <= 1");
    assert_eq!(r.rows[0][0], Value::String("Amsterdam".into()));
    assert_eq!(r.rows[0][1], Value::Int64(2));
    assert_eq!(r.rows[1][0], Value::String("Berlin".into()));
    assert_eq!(r.rows[1][1], Value::Int64(2));
}

#[test]
fn test_having_no_matching_groups() {
    let db = GrafeoDB::new_in_memory();
    let session = db.session();
    for city in ["Amsterdam", "Berlin", "Paris"] {
        session.create_node_with_props(&["City"], [("name", Value::String(city.into()))]);
    }
    let r = session
        .execute(
            "MATCH (c:City) \
             RETURN c.name AS name, count(c) AS cnt \
             HAVING cnt > 10",
        )
        .unwrap();
    // All groups have cnt=1, none pass HAVING cnt > 10
    assert_eq!(r.rows.len(), 0, "No groups should pass HAVING cnt > 10");
}

#[test]
fn test_having_with_sum_aggregate() {
    let db = GrafeoDB::new_in_memory();
    let session = db.session();
    for (name, dept, salary) in [
        ("Alix", "Engineering", 90),
        ("Gus", "Engineering", 80),
        ("Vincent", "Sales", 50),
        ("Jules", "Sales", 60),
        ("Mia", "Marketing", 70),
    ] {
        session.create_node_with_props(
            &["Employee"],
            [
                ("name", Value::String(name.into())),
                ("dept", Value::String(dept.into())),
                ("salary", Value::Int64(salary)),
            ],
        );
    }
    let r = session
        .execute(
            "MATCH (e:Employee) \
             RETURN e.dept AS dept, sum(e.salary) AS total \
             ORDER BY dept \
             HAVING total > 100",
        )
        .unwrap();
    // Engineering: 170, Sales: 110, Marketing: 70
    // Only Engineering and Sales qualify
    assert_eq!(r.rows.len(), 2, "Only depts with total > 100 should appear");
    assert_eq!(r.rows[0][0], Value::String("Engineering".into()));
    assert_eq!(r.rows[1][0], Value::String("Sales".into()));
}

// ============================================================================
// Global aggregation on empty result set (pull-based path)
// ============================================================================

#[test]
fn count_on_empty_result_returns_zero() {
    let db = GrafeoDB::new_in_memory();
    let session = db.session();
    let r = session
        .execute("MATCH (n:NonExistent) RETURN count(n) AS cnt")
        .unwrap();
    // Global COUNT on empty input should return one row with 0
    assert_eq!(r.rows.len(), 1, "Global COUNT should always return one row");
    assert_eq!(r.rows[0][0], Value::Int64(0));
}

#[test]
fn sum_on_empty_result_returns_null() {
    let db = GrafeoDB::new_in_memory();
    let session = db.session();
    let r = session
        .execute("MATCH (n:NonExistent) RETURN sum(n.x) AS total")
        .unwrap();
    assert_eq!(r.rows.len(), 1);
    assert_eq!(r.rows[0][0], Value::Null);
}

#[test]
fn avg_on_empty_result_returns_null() {
    let db = GrafeoDB::new_in_memory();
    let session = db.session();
    let r = session
        .execute("MATCH (n:NonExistent) RETURN avg(n.x) AS average")
        .unwrap();
    assert_eq!(r.rows.len(), 1);
    assert_eq!(r.rows[0][0], Value::Null);
}

#[test]
fn min_max_on_empty_result_returns_null() {
    let db = GrafeoDB::new_in_memory();
    let session = db.session();
    let r = session
        .execute("MATCH (n:NonExistent) RETURN min(n.x) AS lo, max(n.x) AS hi")
        .unwrap();
    assert_eq!(r.rows.len(), 1);
    assert_eq!(r.rows[0][0], Value::Null);
    assert_eq!(r.rows[0][1], Value::Null);
}