grafeo-engine 0.5.41

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
//! Integration tests for temporal types: Date, Time, Duration.
//!
//! Tests temporal constructors, arithmetic, comparisons, and typed literals
//! across GQL, Cypher, and SPARQL query languages.
//!
//! ```bash
//! cargo test -p grafeo-engine --features full --test temporal_types
//! ```

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

/// Creates 2 Person nodes (Alix birthday 1990-06-15, Gus birthday 2000-01-01).
fn create_test_db() -> GrafeoDB {
    let db = GrafeoDB::new_in_memory();
    let mut session = db.session();

    session
        .create_node_with_props(
            &["Person"],
            [
                ("name", Value::String("Alix".into())),
                (
                    "birthday",
                    Value::Date(grafeo_common::types::Date::from_ymd(1990, 6, 15).unwrap()),
                ),
            ],
        )
        .unwrap();
    session
        .create_node_with_props(
            &["Person"],
            [
                ("name", Value::String("Gus".into())),
                (
                    "birthday",
                    Value::Date(grafeo_common::types::Date::from_ymd(2000, 1, 1).unwrap()),
                ),
            ],
        )
        .unwrap();
    let _ = session.commit();
    db
}

// ============================================================================
// Cypher temporal constructors (standalone RETURN)
// ============================================================================

#[test]
#[cfg(feature = "cypher")]
fn cypher_date_function() {
    let db = GrafeoDB::new_in_memory();
    let result = db.execute_cypher("RETURN date('2024-01-15') AS d").unwrap();
    let row = &result.rows()[0];
    let d = row[0].as_date().expect("expected Date value");
    assert_eq!(d.year(), 2024);
    assert_eq!(d.month(), 1);
    assert_eq!(d.day(), 15);
}

#[test]
#[cfg(feature = "cypher")]
fn cypher_duration_function() {
    let db = GrafeoDB::new_in_memory();
    let result = db.execute_cypher("RETURN duration('P1Y') AS d").unwrap();
    let row = &result.rows()[0];
    let d = row[0].as_duration().expect("expected Duration value");
    assert_eq!(d.months(), 12);
}

#[test]
#[cfg(feature = "cypher")]
fn cypher_time_function() {
    let db = GrafeoDB::new_in_memory();
    let result = db.execute_cypher("RETURN time('23:59:59') AS t").unwrap();
    let row = &result.rows()[0];
    let t = row[0].as_time().expect("expected Time value");
    assert_eq!(t.hour(), 23);
    assert_eq!(t.minute(), 59);
    assert_eq!(t.second(), 59);
}

#[test]
#[cfg(feature = "cypher")]
fn cypher_datetime_function() {
    let db = GrafeoDB::new_in_memory();
    let result = db
        .execute_cypher("RETURN datetime('2024-03-15T14:30:00Z') AS dt")
        .unwrap();
    let row = &result.rows()[0];
    let ts = row[0].as_timestamp().expect("expected Timestamp value");
    let date = ts.to_date();
    assert_eq!(date.year(), 2024);
    assert_eq!(date.month(), 3);
    assert_eq!(date.day(), 15);
}

#[test]
#[cfg(feature = "cypher")]
fn cypher_duration_with_components() {
    let db = GrafeoDB::new_in_memory();
    let result = db
        .execute_cypher("RETURN duration('P1Y2M3D') AS d")
        .unwrap();
    let row = &result.rows()[0];
    let d = row[0].as_duration().expect("expected Duration value");
    assert_eq!(d.months(), 14); // 1Y2M = 14 months
    assert_eq!(d.days(), 3);
}

// ============================================================================
// GQL typed literal syntax (needs MATCH context)
// ============================================================================

#[test]
#[cfg(feature = "gql")]
fn gql_date_typed_literal() {
    let db = create_test_db();
    let result = db
        .execute("MATCH (p:Person) RETURN DATE '2024-03-15' AS d LIMIT 1")
        .unwrap();
    let row = &result.rows()[0];
    let d = row[0].as_date().expect("expected Date value");
    assert_eq!(d.year(), 2024);
    assert_eq!(d.month(), 3);
    assert_eq!(d.day(), 15);
}

#[test]
#[cfg(feature = "gql")]
fn gql_time_typed_literal() {
    let db = create_test_db();
    let result = db
        .execute("MATCH (p:Person) RETURN TIME '14:30:00' AS t LIMIT 1")
        .unwrap();
    let row = &result.rows()[0];
    let t = row[0].as_time().expect("expected Time value");
    assert_eq!(t.hour(), 14);
    assert_eq!(t.minute(), 30);
    assert_eq!(t.second(), 0);
}

#[test]
#[cfg(feature = "gql")]
fn gql_duration_typed_literal() {
    let db = create_test_db();
    let result = db
        .execute("MATCH (p:Person) RETURN DURATION 'P1Y2M3D' AS d LIMIT 1")
        .unwrap();
    let row = &result.rows()[0];
    let d = row[0].as_duration().expect("expected Duration value");
    assert_eq!(d.months(), 14);
    assert_eq!(d.days(), 3);
}

#[test]
#[cfg(feature = "gql")]
fn gql_datetime_typed_literal() {
    let db = create_test_db();
    let result = db
        .execute("MATCH (p:Person) RETURN DATETIME '2024-03-15T14:30:00Z' AS dt LIMIT 1")
        .unwrap();
    let row = &result.rows()[0];
    let ts = row[0].as_timestamp().expect("expected Timestamp value");
    let date = ts.to_date();
    assert_eq!(date.year(), 2024);
    assert_eq!(date.month(), 3);
    assert_eq!(date.day(), 15);
}

// ============================================================================
// Component extraction
// ============================================================================

#[test]
#[cfg(feature = "cypher")]
fn cypher_year_month_day_extraction() {
    let db = GrafeoDB::new_in_memory();
    let result = db
        .execute_cypher(
            "RETURN year(date('2024-03-15')) AS y, month(date('2024-03-15')) AS m, day(date('2024-03-15')) AS d",
        )
        .unwrap();
    let row = &result.rows()[0];
    assert_eq!(row[0].as_int64(), Some(2024));
    assert_eq!(row[1].as_int64(), Some(3));
    assert_eq!(row[2].as_int64(), Some(15));
}

#[test]
#[cfg(feature = "cypher")]
fn cypher_hour_minute_second_extraction() {
    let db = GrafeoDB::new_in_memory();
    let result = db
        .execute_cypher(
            "RETURN hour(time('14:30:45')) AS h, minute(time('14:30:45')) AS m, second(time('14:30:45')) AS s",
        )
        .unwrap();
    let row = &result.rows()[0];
    assert_eq!(row[0].as_int64(), Some(14));
    assert_eq!(row[1].as_int64(), Some(30));
    assert_eq!(row[2].as_int64(), Some(45));
}

// ============================================================================
// Temporal arithmetic
// ============================================================================

#[test]
#[cfg(feature = "cypher")]
fn cypher_date_plus_duration() {
    let db = GrafeoDB::new_in_memory();
    let result = db
        .execute_cypher("RETURN date('2024-01-15') + duration('P1M') AS next_month")
        .unwrap();
    let row = &result.rows()[0];
    let d = row[0].as_date().expect("expected Date value");
    assert_eq!(d.year(), 2024);
    assert_eq!(d.month(), 2);
    assert_eq!(d.day(), 15);
}

#[test]
#[cfg(feature = "cypher")]
fn cypher_date_minus_date() {
    let db = GrafeoDB::new_in_memory();
    let result = db
        .execute_cypher("RETURN date('2024-03-15') - date('2024-01-01') AS diff")
        .unwrap();
    let row = &result.rows()[0];
    let d = row[0].as_duration().expect("expected Duration value");
    // 74 days between Jan 1 and Mar 15
    assert_eq!(d.days(), 74);
}

#[test]
#[cfg(feature = "cypher")]
fn cypher_duration_arithmetic() {
    let db = GrafeoDB::new_in_memory();
    let result = db
        .execute_cypher("RETURN duration('P1Y') + duration('P6M') AS combined")
        .unwrap();
    let row = &result.rows()[0];
    let d = row[0].as_duration().expect("expected Duration value");
    assert_eq!(d.months(), 18); // 12 + 6
}

// ============================================================================
// GQL temporal arithmetic with typed literals (MATCH context)
// ============================================================================

#[test]
#[cfg(feature = "gql")]
fn gql_date_plus_duration() {
    let db = create_test_db();
    let result = db
        .execute("MATCH (p:Person) RETURN DATE '2024-01-15' + DURATION 'P1M' AS next_month LIMIT 1")
        .unwrap();
    let row = &result.rows()[0];
    let d = row[0].as_date().expect("expected Date value");
    assert_eq!(d.year(), 2024);
    assert_eq!(d.month(), 2);
    assert_eq!(d.day(), 15);
}

#[test]
#[cfg(feature = "gql")]
fn gql_date_minus_date() {
    let db = create_test_db();
    let result = db
        .execute("MATCH (p:Person) RETURN DATE '2024-03-15' - DATE '2024-01-01' AS diff LIMIT 1")
        .unwrap();
    let row = &result.rows()[0];
    let d = row[0].as_duration().expect("expected Duration value");
    assert_eq!(d.days(), 74);
}

// ============================================================================
// Temporal comparison in WHERE
// ============================================================================

#[test]
#[cfg(feature = "gql")]
fn gql_date_comparison_where() {
    let db = create_test_db();
    let result = db
        .execute("MATCH (p:Person) WHERE p.birthday > DATE '1995-01-01' RETURN p.name AS name")
        .unwrap();
    assert_eq!(result.rows().len(), 1);
    assert_eq!(result.rows()[0][0].as_str(), Some("Gus"));
}

#[test]
#[cfg(feature = "cypher")]
fn cypher_date_comparison_where() {
    let db = create_test_db();
    let result = db
        .execute_cypher(
            "MATCH (p:Person) WHERE p.birthday > date('1995-01-01') RETURN p.name AS name",
        )
        .unwrap();
    assert_eq!(result.rows().len(), 1);
    assert_eq!(result.rows()[0][0].as_str(), Some("Gus"));
}

// ============================================================================
// JSON parameter roundtrip
// ============================================================================

#[test]
#[cfg(feature = "gql")]
fn gql_date_json_param_roundtrip() {
    let db = create_test_db();
    let mut params = std::collections::HashMap::new();
    params.insert(
        "d".to_string(),
        Value::Date(grafeo_common::types::Date::from_ymd(2024, 6, 15).unwrap()),
    );
    let result = db
        .execute_with_params("MATCH (p:Person) RETURN $d AS val LIMIT 1", params)
        .unwrap();
    let d = result.rows()[0][0]
        .as_date()
        .expect("expected Date value from param");
    assert_eq!(d.year(), 2024);
    assert_eq!(d.month(), 6);
    assert_eq!(d.day(), 15);
}

// ============================================================================
// SPARQL xsd: typed literals
// ============================================================================

#[test]
#[cfg(all(feature = "sparql", feature = "triple-store"))]
fn sparql_xsd_date_literal() {
    let db = GrafeoDB::new_in_memory();
    let result = db
        .execute_sparql(
            r#"SELECT ?d WHERE { BIND("2024-03-15"^^<http://www.w3.org/2001/XMLSchema#date> AS ?d) }"#,
        )
        .unwrap();
    assert!(!result.rows().is_empty(), "expected at least one row");
    let d = result.rows()[0][0].as_date().expect("expected Date value");
    assert_eq!(d.year(), 2024);
    assert_eq!(d.month(), 3);
    assert_eq!(d.day(), 15);
}

#[test]
#[cfg(all(feature = "sparql", feature = "triple-store"))]
fn sparql_xsd_duration_literal() {
    let db = GrafeoDB::new_in_memory();
    let result = db
        .execute_sparql(
            r#"SELECT ?d WHERE { BIND("P1Y6M"^^<http://www.w3.org/2001/XMLSchema#duration> AS ?d) }"#,
        )
        .unwrap();
    assert!(!result.rows().is_empty(), "expected at least one row");
    let d = result.rows()[0][0]
        .as_duration()
        .expect("expected Duration value");
    assert_eq!(d.months(), 18);
}

// ============================================================================
// Temporal map constructors: date({year:...}), time({hour:...}), etc.
// ============================================================================

#[test]
#[cfg(feature = "cypher")]
fn cypher_date_map_constructor() {
    let db = GrafeoDB::new_in_memory();
    let result = db
        .execute_cypher("RETURN date({year: 2024, month: 3, day: 15}) AS d")
        .unwrap();
    let d = result.rows()[0][0].as_date().expect("expected Date value");
    assert_eq!(d.year(), 2024);
    assert_eq!(d.month(), 3);
    assert_eq!(d.day(), 15);
}

#[test]
#[cfg(feature = "cypher")]
fn cypher_date_map_defaults() {
    let db = GrafeoDB::new_in_memory();
    // month and day should default to 1 when omitted
    let result = db.execute_cypher("RETURN date({year: 2024}) AS d").unwrap();
    let d = result.rows()[0][0].as_date().expect("expected Date value");
    assert_eq!(d.year(), 2024);
    assert_eq!(d.month(), 1);
    assert_eq!(d.day(), 1);
}

#[test]
#[cfg(feature = "cypher")]
fn cypher_time_map_constructor() {
    let db = GrafeoDB::new_in_memory();
    let result = db
        .execute_cypher("RETURN time({hour: 14, minute: 30, second: 45}) AS t")
        .unwrap();
    let t = result.rows()[0][0].as_time().expect("expected Time value");
    assert_eq!(t.hour(), 14);
    assert_eq!(t.minute(), 30);
    assert_eq!(t.second(), 45);
}

#[test]
#[cfg(feature = "cypher")]
fn cypher_datetime_map_constructor() {
    let db = GrafeoDB::new_in_memory();
    let result = db
        .execute_cypher(
            "RETURN datetime({year: 2024, month: 3, day: 15, hour: 14, minute: 30}) AS dt",
        )
        .unwrap();
    let ts = result.rows()[0][0]
        .as_timestamp()
        .expect("expected Timestamp value");
    let d = ts.to_date();
    assert_eq!(d.year(), 2024);
    assert_eq!(d.month(), 3);
    assert_eq!(d.day(), 15);
    let t = ts.to_time();
    assert_eq!(t.hour(), 14);
    assert_eq!(t.minute(), 30);
}

#[test]
#[cfg(feature = "cypher")]
fn cypher_duration_map_constructor() {
    let db = GrafeoDB::new_in_memory();
    let result = db
        .execute_cypher("RETURN duration({years: 1, months: 2, days: 3, hours: 4}) AS dur")
        .unwrap();
    let d = result.rows()[0][0]
        .as_duration()
        .expect("expected Duration value");
    assert_eq!(d.months(), 14); // 1 year + 2 months
    assert_eq!(d.days(), 3);
    // 4 hours in nanoseconds = 4 * 3_600_000_000_000
    assert_eq!(d.nanos(), 4 * 3_600_000_000_000);
}

#[test]
#[cfg(feature = "cypher")]
fn cypher_duration_map_with_weeks() {
    let db = GrafeoDB::new_in_memory();
    let result = db
        .execute_cypher("RETURN duration({weeks: 2, days: 3}) AS dur")
        .unwrap();
    let d = result.rows()[0][0]
        .as_duration()
        .expect("expected Duration value");
    assert_eq!(d.days(), 17); // 2 weeks + 3 days
}