relate-model 1.1.0

Data model types (CypherValue) for the Relate graph database
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
use super::*;
use crate::value::{GeometryValue, IsoDuration, PointValue, VectorCoordKind};
use chrono::{FixedOffset, NaiveDate, NaiveTime, TimeZone};

/// The fixed `elapsed_ms` the envelope fixtures pin (ADR-0221): real
/// surfaces pass the engine-reported value; fixtures use a fixed value so
/// the expected envelope is deterministic. `0.5` so the phase fixtures can
/// partition it exactly (see [`FIXTURE_TIMING_STAMP`]) — binary fractions,
/// no f64 rounding in the fixture's own arithmetic.
pub const FIXTURE_ELAPSED_MS: f64 = 0.5;

/// The fixed timing stamp the ADR-0491 envelope fixtures pin: real
/// surfaces pass the engine-reported timings; fixtures use fixed values so
/// the expected envelope is deterministic. The total is the shared
/// [`FIXTURE_ELAPSED_MS`] (re-sourced, never re-typed); the phases are
/// binary fractions that sum to exactly it — the fixture's own example of
/// the ADR-0491 rule that the split partitions the total's window — and
/// the LABS-1120 fine sub-windows partition their phases the same way
/// (`lex_parse` + `rewrite` + `lower` = `parse`;
/// `result_generation` < `execute`).
pub const FIXTURE_TIMING_STAMP: TimingStampMs = TimingStampMs {
    elapsed_ms: FIXTURE_ELAPSED_MS,
    parse_ms: 0.25,
    plan_ms: 0.1875,
    execute_ms: 0.0625,
    lex_parse_ms: 0.125,
    rewrite_ms: 0.0625,
    lower_ms: 0.0625,
    result_generation_ms: 0.03125,
    plan_cache: "miss",
};

/// A single named value fixture: a `CypherValue` and its expected canonical
/// wire JSON.
pub struct ValueFixture {
    pub name: &'static str,
    pub value: CypherValue,
    pub expected: Value,
}

fn node_alice() -> CypherValue {
    let mut props = IndexMap::new();
    props.insert("name".to_string(), CypherValue::Str("Alice".into()));
    props.insert("age".to_string(), CypherValue::Int(30));
    CypherValue::Node {
        id: 7,
        labels: vec!["Person".into(), "User".into()],
        properties: props,
    }
}

fn node_bob() -> CypherValue {
    let mut props = IndexMap::new();
    props.insert("name".to_string(), CypherValue::Str("Bob".into()));
    CypherValue::Node {
        id: 8,
        labels: vec!["Person".into()],
        properties: props,
    }
}

fn rel_knows() -> CypherValue {
    let mut props = IndexMap::new();
    props.insert("since".to_string(), CypherValue::Int(2020));
    CypherValue::Relationship {
        id: 11,
        start_id: 7,
        end_id: 8,
        rel_type: "KNOWS".into(),
        properties: props,
    }
}

/// Every variant (plus a heterogeneous list) paired with its expected wire
/// JSON. The expected values are hand-authored (not derived from
/// [`value_to_wire_json`]) so they genuinely pin the contract.
pub fn value_fixtures() -> Vec<ValueFixture> {
    let offset = FixedOffset::east_opt(2 * 3600).unwrap();
    let ndt = NaiveDate::from_ymd_opt(2024, 3, 14)
        .unwrap()
        .and_hms_opt(12, 30, 45)
        .unwrap();
    let zoned = offset.from_local_datetime(&ndt).unwrap();

    vec![
        ValueFixture {
            name: "null",
            value: CypherValue::Null,
            expected: Value::Null,
        },
        ValueFixture {
            name: "bool",
            value: CypherValue::Bool(true),
            expected: serde_json::json!(true),
        },
        ValueFixture {
            name: "int",
            value: CypherValue::Int(42),
            expected: serde_json::json!(42),
        },
        ValueFixture {
            name: "float",
            value: CypherValue::Float(3.5),
            expected: serde_json::json!(3.5),
        },
        // ADR-0280: non-finite floats encode as their `Display` spellings,
        // never `null`.
        ValueFixture {
            name: "float_nan",
            value: CypherValue::Float(f64::NAN),
            expected: serde_json::json!("NaN"),
        },
        ValueFixture {
            name: "float_positive_infinity",
            value: CypherValue::Float(f64::INFINITY),
            expected: serde_json::json!("Inf"),
        },
        ValueFixture {
            name: "float_negative_infinity",
            value: CypherValue::Float(f64::NEG_INFINITY),
            expected: serde_json::json!("-Inf"),
        },
        ValueFixture {
            name: "string",
            value: CypherValue::Str("hello".into()),
            expected: serde_json::json!("hello"),
        },
        ValueFixture {
            name: "list_heterogeneous",
            value: CypherValue::List(vec![
                CypherValue::Int(1),
                CypherValue::Str("two".into()),
                CypherValue::Bool(true),
                CypherValue::Null,
            ]),
            expected: serde_json::json!([1, "two", true, null]),
        },
        ValueFixture {
            name: "map",
            value: {
                let mut m = IndexMap::new();
                m.insert("a".to_string(), CypherValue::Int(1));
                m.insert("b".to_string(), CypherValue::Str("x".into()));
                CypherValue::Map(m)
            },
            expected: serde_json::json!({"a": 1, "b": "x"}),
        },
        ValueFixture {
            name: "node",
            value: node_alice(),
            expected: serde_json::json!({
                "kind": "node",
                "id": 7,
                "labels": ["Person", "User"],
                "properties": {"name": "Alice", "age": 30}
            }),
        },
        ValueFixture {
            name: "relationship",
            value: rel_knows(),
            expected: serde_json::json!({
                "kind": "relationship",
                "id": 11,
                "type": "KNOWS",
                "start_node": 7,
                "end_node": 8,
                "properties": {"since": 2020}
            }),
        },
        ValueFixture {
            name: "path",
            value: CypherValue::Path {
                nodes: vec![node_alice(), node_bob()],
                relationships: vec![rel_knows()],
            },
            expected: serde_json::json!([
                {
                    "kind": "node",
                    "id": 7,
                    "labels": ["Person", "User"],
                    "properties": {"name": "Alice", "age": 30}
                },
                {
                    "kind": "relationship",
                    "id": 11,
                    "type": "KNOWS",
                    "start_node": 7,
                    "end_node": 8,
                    "properties": {"since": 2020}
                },
                {
                    "kind": "node",
                    "id": 8,
                    "labels": ["Person"],
                    "properties": {"name": "Bob"}
                }
            ]),
        },
        ValueFixture {
            name: "date",
            value: CypherValue::Date(NaiveDate::from_ymd_opt(2024, 3, 14).unwrap()),
            expected: serde_json::json!("2024-03-14"),
        },
        ValueFixture {
            name: "time",
            value: CypherValue::Time(NaiveTime::from_hms_opt(12, 30, 45).unwrap(), Some(offset)),
            expected: serde_json::json!("12:30:45+02:00"),
        },
        ValueFixture {
            name: "datetime",
            value: CypherValue::DateTime(zoned, Some("Europe/Stockholm".into())),
            expected: serde_json::json!("2024-03-14T12:30:45+02:00[Europe/Stockholm]"),
        },
        ValueFixture {
            name: "localdatetime",
            value: CypherValue::LocalDateTime(ndt),
            expected: serde_json::json!("2024-03-14T12:30:45"),
        },
        ValueFixture {
            name: "duration",
            value: CypherValue::Duration(IsoDuration::new(0, 0, 3661, 0)),
            expected: serde_json::json!("PT1H1M1S"),
        },
        ValueFixture {
            name: "point_cartesian",
            value: CypherValue::Point(PointValue::cartesian_2d(1.0, 2.0)),
            expected: serde_json::json!({
                "srid": 7203,
                "crs": "cartesian",
                "x": 1.0,
                "y": 2.0
            }),
        },
        ValueFixture {
            name: "point_geographic",
            value: CypherValue::Point(PointValue::wgs84_2d(-73.98, 40.75)),
            expected: serde_json::json!({
                "srid": 4326,
                "crs": "wgs-84",
                "longitude": -73.98,
                "latitude": 40.75
            }),
        },
        ValueFixture {
            name: "geometry",
            value: CypherValue::Geometry(GeometryValue::LineString {
                coordinates: vec![[0.0, 0.0], [1.0, 1.0]],
                srid: 7203,
            }),
            expected: serde_json::json!({
                "type": "LineString",
                "coordinates": [[0.0, 0.0], [1.0, 1.0]]
            }),
        },
        ValueFixture {
            name: "vector",
            value: CypherValue::Vector {
                coord: VectorCoordKind::Float32,
                dim: 3,
                values: vec![
                    CypherValue::Float(1.0),
                    CypherValue::Float(2.0),
                    CypherValue::Float(3.0),
                ],
            },
            expected: serde_json::json!({
                "type": "FLOAT32",
                "dim": 3,
                "values": [1.0, 2.0, 3.0]
            }),
        },
    ]
}

/// A stats snapshot exercising all 11 counters — including a deliberate
/// zero (`labels_removed`) to pin "zeros are included" — paired with its
/// expected wire JSON.
pub fn stats_fixture() -> (WireStatsSnapshot, Value) {
    let snap = WireStatsSnapshot {
        nodes_created: 3,
        nodes_deleted: 1,
        relationships_created: 2,
        relationships_deleted: 1,
        properties_set: 5,
        labels_added: 4,
        labels_removed: 0,
        indexes_added: 1,
        indexes_removed: 1,
        constraints_added: 2,
        constraints_removed: 1,
    };
    let expected = serde_json::json!({
        "nodes_created": 3,
        "nodes_deleted": 1,
        "relationships_created": 2,
        "relationships_deleted": 1,
        "properties_set": 5,
        "labels_added": 4,
        "labels_removed": 0,
        "indexes_added": 1,
        "indexes_removed": 1,
        "constraints_added": 2,
        "constraints_removed": 1
    });
    (snap, expected)
}

/// The ADR-0224 `columnTypes` of the golden row: one
/// [`crate::wire::column_type_name`] per column, in column order. The
/// golden row IS the first row, so this derivation is the fixture's
/// `columnTypes` array — surfaces reproducing the envelope call this
/// instead of re-deriving it.
pub fn column_types(row: &IndexMap<String, CypherValue>) -> Vec<String> {
    row.values()
        .map(|v| crate::wire::column_type_name(v).to_string())
        .collect()
}

/// One result row exercising every variant, plus the expected full success
/// envelope for it — the canonical ADR-0491 shape, timing split included.
/// Columns are the fixture names, in order. Any surface that assembles a
/// response from [`value_to_wire_json`], [`stats_to_wire_json`], and
/// [`response_envelope`] must reproduce this envelope exactly — including
/// the ADR-0224 `columnTypes` array, one [`crate::wire::column_type_name`]
/// per column, in column order ([`column_types`]), and the ADR-0491 timing
/// block, by passing [`FIXTURE_TIMING_STAMP`] to [`response_envelope`].
pub fn golden_row() -> (Vec<String>, IndexMap<String, CypherValue>, Value) {
    let fixtures = value_fixtures();
    let columns: Vec<String> = fixtures.iter().map(|f| f.name.to_string()).collect();
    let mut row = IndexMap::new();
    let mut row_json = Map::new();
    for f in &fixtures {
        row.insert(f.name.to_string(), f.value.clone());
        row_json.insert(f.name.to_string(), f.expected.clone());
    }
    let (_snap, stats_expected) = stats_fixture();

    let mut envelope = Map::new();
    envelope.insert(
        "columns".into(),
        Value::Array(columns.iter().map(|c| Value::String(c.clone())).collect()),
    );
    // ADR-0224: the engine stamps each column from the first row; the golden
    // row IS the first row, so callers reproduce the fixture by passing
    // `column_types(&row)` to `response_envelope`.
    let column_type_names: Vec<Value> = column_types(&row).into_iter().map(Value::String).collect();
    envelope.insert("columnTypes".into(), Value::Array(column_type_names));
    envelope.insert("rows".into(), Value::Array(vec![Value::Object(row_json)]));
    envelope.insert("stats".into(), stats_expected);
    // ADR-0363 decision 9: the success envelope always carries the list;
    // this fixture is a plain run, so it is empty.
    envelope.insert(
        notification_keys::NOTIFICATIONS.into(),
        Value::Array(Vec::new()),
    );
    // ADR-0221 + ADR-0491: the engine-reported elapsed together with its
    // PARSE / PLAN / EXECUTE split; the fixture pins the shape with the
    // fixed stamp, so callers reproduce the fixture by passing
    // `FIXTURE_TIMING_STAMP` to `response_envelope`.
    envelope.insert(
        "elapsed_ms".into(),
        Value::from(FIXTURE_TIMING_STAMP.elapsed_ms),
    );
    envelope.insert(
        "parse_ms".into(),
        Value::from(FIXTURE_TIMING_STAMP.parse_ms),
    );
    envelope.insert("plan_ms".into(), Value::from(FIXTURE_TIMING_STAMP.plan_ms));
    envelope.insert(
        "execute_ms".into(),
        Value::from(FIXTURE_TIMING_STAMP.execute_ms),
    );
    // LABS-1120: the fine sub-split and the plan-cache outcome, from the
    // same fixed stamp.
    envelope.insert(
        "lex_parse_ms".into(),
        Value::from(FIXTURE_TIMING_STAMP.lex_parse_ms),
    );
    envelope.insert(
        "rewrite_ms".into(),
        Value::from(FIXTURE_TIMING_STAMP.rewrite_ms),
    );
    envelope.insert(
        "lower_ms".into(),
        Value::from(FIXTURE_TIMING_STAMP.lower_ms),
    );
    envelope.insert(
        "result_generation_ms".into(),
        Value::from(FIXTURE_TIMING_STAMP.result_generation_ms),
    );
    envelope.insert(
        "plan_cache".into(),
        Value::from(FIXTURE_TIMING_STAMP.plan_cache),
    );
    // LABS-418: the structured plan document is always present, `null` on a
    // plain run.
    envelope.insert("plan".into(), Value::Null);
    envelope.insert("error".into(), Value::Null);

    (columns, row, Value::Object(envelope))
}

/// The canonical wire envelope for a **write-only** statement (a
/// `CREATE`/`MERGE`/`SET`/… without `RETURN`): no result columns, and the
/// single empty driving row the engine seeds must render as an **empty**
/// `rows` array — never `[{}]`. Returns the raw `(columns, rows, stats)` a
/// surface would hand to [`rows_for_result`]/[`response_envelope`] plus the
/// expected full envelope (with ADR-0224 `"columnTypes": []` — no columns,
/// so no column types — and the ADR-0491 timing block, by passing
/// [`FIXTURE_TIMING_STAMP`] to [`response_envelope`]), so every surface can
/// pin the empty-columns envelope-assembly path (which the value fixtures
/// never exercise).
pub fn envelope_write_only() -> (
    Vec<String>,
    Vec<IndexMap<String, CypherValue>>,
    WireStatsSnapshot,
    Value,
) {
    // No columns, one empty driving row — exactly what the engine yields for
    // a top-level write with no RETURN.
    let columns: Vec<String> = Vec::new();
    let rows = vec![IndexMap::new()];
    let stats = WireStatsSnapshot {
        nodes_created: 1,
        labels_added: 1,
        ..Default::default()
    };
    let expected = serde_json::json!({
        "columns": [],
        // ADR-0224: no columns, so no column types.
        "columnTypes": [],
        "rows": [],
        "stats": {
            "nodes_created": 1,
            "nodes_deleted": 0,
            "relationships_created": 0,
            "relationships_deleted": 0,
            "properties_set": 0,
            "labels_added": 1,
            "labels_removed": 0,
            "indexes_added": 0,
            "indexes_removed": 0,
            "constraints_added": 0,
            "constraints_removed": 0
        },
        // ADR-0363 decision 9: always present, empty on a plain run.
        (notification_keys::NOTIFICATIONS): [],
        // ADR-0221 + ADR-0491: the fixed stamp's total and split, together.
        "elapsed_ms": FIXTURE_TIMING_STAMP.elapsed_ms,
        "parse_ms": FIXTURE_TIMING_STAMP.parse_ms,
        "plan_ms": FIXTURE_TIMING_STAMP.plan_ms,
        "execute_ms": FIXTURE_TIMING_STAMP.execute_ms,
        "lex_parse_ms": FIXTURE_TIMING_STAMP.lex_parse_ms,
        "rewrite_ms": FIXTURE_TIMING_STAMP.rewrite_ms,
        "lower_ms": FIXTURE_TIMING_STAMP.lower_ms,
        "result_generation_ms": FIXTURE_TIMING_STAMP.result_generation_ms,
        "plan_cache": FIXTURE_TIMING_STAMP.plan_cache,
        // LABS-418: always present, null on a plain run.
        "plan": null,
        "error": null
    });
    (columns, rows, stats, expected)
}

/// Assert a candidate value serializer reproduces the canonical wire JSON
/// for every fixture. Surfaces call this with their own
/// `&CypherValue -> serde_json::Value` function so drift becomes a test
/// failure; surfaces pass [`value_to_wire_json`] itself to prove
/// the shared module is reachable and correct from each surface's build.
pub fn assert_all_match<F>(serialize: F)
where
    F: Fn(&CypherValue) -> Value,
{
    for f in value_fixtures() {
        let got = serialize(&f.value);
        assert_eq!(
            got, f.expected,
            "wire encoding drift for fixture `{}`:\n  got:      {}\n  expected: {}",
            f.name, got, f.expected
        );
    }
}