relate-executor 1.2.0

In-memory graph and Cypher physical executor
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
//! The key-construction unit tests — the normalized tree's equivalence
//! classes, the property-index key's identity reading, and the ADR-0614
//! decline predicate. A child module of `group_key` (the
//! `constraint_keys` precedent): the white-box pins construct the
//! private tree directly, so the tests live where the representation is
//! visible and the normalization types stay private to their module.

use super::*;
use relate_core::{Node, PointValue};
use relate_test_support::temporal::dst_overlap_half;
use std::collections::{BTreeMap, HashSet};
use std::sync::Arc;

fn key(v: Value) -> GroupKey {
    GroupKey::new(&v)
}

fn nan() -> Value {
    Value::Float(f64::NAN)
}

#[test]
fn int_and_integral_float_share_a_key() {
    assert_eq!(key(Value::Int(1)), key(Value::Float(1.0)));
    assert_eq!(key(Value::Int(0)), key(Value::Float(0.0)));
}

#[test]
fn keys_agree_with_eq_non_null_past_2_pow_53() {
    // The equivalence key and `=` must give the same verdict on the
    // boundary where a lossy `as f64` cast goes wrong.
    let big = Value::Int(9_007_199_254_740_993);
    let near = Value::Float(9_007_199_254_740_992.0);
    assert!(!big.eq_non_null(&near));
    assert_ne!(key(big.clone()), key(near.clone()));
    // ... and the float DOES share a key with the integer it denotes.
    let exact = Value::Int(9_007_199_254_740_992);
    assert!(exact.eq_non_null(&near));
    assert_eq!(key(exact), key(near));
}

#[test]
fn vector_keys_agree_with_vector_eq_both_directions() {
    // `=` and the equivalence key must give the same verdict on vectors:
    // numerically equal elements collapse (equal), different values or
    // coordinate kinds do not (unequal).
    let int_backed = Value::Vector {
        coord: VectorCoordKind::Float64,
        values: vec![Value::Int(1)].into(),
    };
    let float_backed = Value::Vector {
        coord: VectorCoordKind::Float64,
        values: vec![Value::Float(1.0)].into(),
    };
    assert!(int_backed.eq_non_null(&float_backed));
    assert_eq!(key(int_backed.clone()), key(float_backed));
    let two = Value::Vector {
        coord: VectorCoordKind::Float64,
        values: vec![Value::Float(2.0)].into(),
    };
    assert!(!int_backed.eq_non_null(&two));
    assert_ne!(key(int_backed.clone()), key(two));
    let other_kind = Value::Vector {
        coord: VectorCoordKind::Int32,
        values: vec![Value::Int(1)].into(),
    };
    assert!(!int_backed.eq_non_null(&other_kind));
    assert_ne!(key(int_backed), key(other_kind));
}

#[test]
fn positive_and_negative_zero_share_a_key() {
    assert_eq!(key(Value::Float(0.0)), key(Value::Float(-0.0)));
    assert_eq!(key(Value::Int(0)), key(Value::Float(-0.0)));
}

#[test]
fn non_integral_floats_key_by_value() {
    assert_eq!(key(Value::Float(1.5)), key(Value::Float(1.5)));
    assert_ne!(key(Value::Float(1.5)), key(Value::Float(2.5)));
    assert_ne!(key(Value::Int(1)), key(Value::Float(1.5)));
}

#[test]
fn every_nan_is_its_own_key() {
    assert_ne!(key(nan()), key(nan()));
    // Two lists each holding a NaN are also never equivalent.
    assert_ne!(
        key(Value::List(vec![nan()].into())),
        key(Value::List(vec![nan()].into()))
    );
}

#[test]
fn float_beyond_i64_range_does_not_collide_with_i64_extremes() {
    // 2^63 is i64::MAX + 1; it must not collapse onto Int(i64::MAX).
    let two_pow_63 = Value::Float(9_223_372_036_854_775_808.0);
    assert_ne!(key(two_pow_63.clone()), key(Value::Int(i64::MAX)));
    // But an in-range integral float (2^62, exactly representable) still
    // collapses onto its integer.
    let two_pow_62: i64 = 4_611_686_018_427_387_904;
    assert_eq!(
        key(Value::Float(two_pow_62 as f64)),
        key(Value::Int(two_pow_62))
    );
}

#[test]
fn strings_never_collide_with_lists() {
    let s = Value::String("a,b".into());
    let l = Value::List(vec![Value::String("a".into()), Value::String("b".into())].into());
    assert_ne!(key(s), key(l));
}

#[test]
fn strings_with_quotes_and_escapes_stay_distinct() {
    // The old to_cypher_literal key rendered these ambiguously; the
    // canonical key keeps the byte content verbatim.
    assert_ne!(
        key(Value::String("a\"b".into())),
        key(Value::String("a'b".into()))
    );
    assert_ne!(
        key(Value::String("a\\b".into())),
        key(Value::String("ab".into()))
    );
}

#[test]
fn bools_never_collide_with_ints() {
    assert_ne!(key(Value::Bool(true)), key(Value::Int(1)));
    assert_ne!(key(Value::Bool(false)), key(Value::Int(0)));
}

#[test]
fn nested_numeric_equivalence_in_lists_and_maps() {
    let a = Value::List(vec![Value::Int(1)].into());
    let b = Value::List(vec![Value::Float(1.0)].into());
    assert_eq!(key(a), key(b));

    let mut m1 = BTreeMap::new();
    m1.insert("a".to_string(), Value::List(vec![Value::Int(1)].into()));
    let mut m2 = BTreeMap::new();
    m2.insert("a".to_string(), Value::List(vec![Value::Float(1.0)].into()));
    assert_eq!(key(Value::Map(m1.into())), key(Value::Map(m2.into())));
}

#[test]
fn map_key_order_does_not_matter() {
    let mut m1 = BTreeMap::new();
    m1.insert("a".to_string(), Value::Int(1));
    m1.insert("b".to_string(), Value::Int(2));
    let mut m2 = BTreeMap::new();
    m2.insert("b".to_string(), Value::Int(2));
    m2.insert("a".to_string(), Value::Int(1));
    assert_eq!(key(Value::Map(m1.into())), key(Value::Map(m2.into())));
}

#[test]
fn nodes_key_by_id() {
    let n1 = Node {
        id: 1,
        labels: Arc::new(vec!["P".to_string()]),
        properties: relate_core::PropertyMap::share(BTreeMap::new()),
    };
    let n2 = Node {
        id: 2,
        labels: Arc::new(vec!["P".to_string()]),
        properties: relate_core::PropertyMap::share(BTreeMap::new()),
    };
    // Structurally identical but different ids -> distinct.
    assert_ne!(key(Value::from_node(n1.clone())), key(Value::from_node(n2)));
    assert_eq!(key(Value::from_node(n1.clone())), key(Value::from_node(n1)));
}

#[test]
fn time_keys_by_local_time_and_offset() {
    // `Temporal::equals`: 12:00+01:00 and 13:00+02:00 share an instant
    // but are different times, so they key apart.
    let a = Temporal::Time {
        time: NaiveTime::from_hms_opt(12, 0, 0).unwrap(),
        offset: 3600,
    };
    let b = Temporal::Time {
        time: NaiveTime::from_hms_opt(13, 0, 0).unwrap(),
        offset: 7200,
    };
    assert_ne!(
        key(Value::Temporal(a.clone())),
        key(Value::Temporal(b.clone()))
    );
    assert_ne!(a.compare(&b), Some(std::cmp::Ordering::Equal));
}

#[test]
fn datetime_keys_by_instant_and_zone_identity() {
    let utc = NaiveDate::from_ymd_opt(2026, 1, 1)
        .unwrap()
        .and_hms_opt(12, 0, 0)
        .unwrap();
    let a = Temporal::DateTime {
        utc,
        offset: 0,
        zone: Some("UTC".to_string()),
    };
    let b = Temporal::DateTime {
        utc,
        offset: 0,
        zone: Some("Europe/Stockholm".to_string()),
    };
    // A named zone is never the offset it denotes.
    assert_ne!(key(Value::Temporal(a)), key(Value::Temporal(b)));
}

#[test]
fn datetime_dst_overlap_halves_key_apart_but_stay_equal() {
    // The centralized fixture: 02:30+02:00 and 02:30+01:00 on the
    // fall-back day — equal local date-time and zone, different
    // offsets/instants. `=` holds between the halves, yet they are two
    // grouping keys (TCK TemporalCompare).
    let first = dst_overlap_half(false);
    let second = dst_overlap_half(true);
    assert!(first.eq_non_null(&second), "the halves are `=`-equal");
    assert_ne!(key(first.clone()), key(second.clone()));
    // The property index keys the same identity, not `=`: the pair stays
    // apart for an indexed seek and a uniqueness probe, exactly as
    // Neo4j's range index and constraints keep it apart.
    assert_ne!(
        GroupKey::for_property_index(&first),
        GroupKey::for_property_index(&second)
    );
}

#[test]
fn property_index_key_shares_the_grouping_tree() {
    // Times the two relations agree on: equal times share the bucket,
    // the same instant in a different offset does not.
    let time = |h, m, offset| {
        Value::Temporal(Temporal::Time {
            time: NaiveTime::from_hms_opt(h, m, 0).unwrap(),
            offset,
        })
    };
    // The concrete bucket: UTC nanos of day plus the offset.
    assert_eq!(
        GroupKey::for_property_index(&time(12, 0, 0)),
        Some(GroupKey(Norm::Temporal(NormTemporal::Time {
            utc_nanos: 12 * 3_600 * 1_000_000_000,
            offset: 0,
        })))
    );
    assert_ne!(
        GroupKey::for_property_index(&time(12, 0, 0)),
        GroupKey::for_property_index(&time(12, 0, 3600))
    );
    // A local date-time is the same value in both readings.
    let local = NaiveDate::from_ymd_opt(2026, 1, 1)
        .unwrap()
        .and_hms_opt(12, 0, 0)
        .unwrap();
    let value = Value::Temporal(Temporal::LocalDateTime(local));
    assert_eq!(
        key(value.clone()),
        GroupKey::for_property_index(&value).unwrap()
    );
}

#[test]
fn vector_elements_normalize_numerically() {
    let a = Value::Vector {
        coord: VectorCoordKind::Float64,
        values: vec![Value::Int(1)].into(),
    };
    let b = Value::Vector {
        coord: VectorCoordKind::Float64,
        values: vec![Value::Float(1.0)].into(),
    };
    assert_eq!(key(a), key(b.clone()));
    // The coordinate kind stays part of the key.
    let c = Value::Vector {
        coord: VectorCoordKind::Int32,
        values: vec![Value::Int(1)].into(),
    };
    assert_ne!(key(b), key(c));
}

#[test]
fn points_equivalent_by_coordinate_value() {
    let p1 = Value::Point(PointValue {
        srid: 7203,
        x: 1.0,
        y: 2.0,
        z: None,
    });
    let p2 = Value::Point(PointValue {
        srid: 7203,
        x: 1.0,
        y: 2.0,
        z: None,
    });
    assert_eq!(key(p1), key(p2.clone()));
    let p3 = Value::Point(PointValue {
        srid: 7203,
        x: 1.0,
        y: 2.5,
        z: None,
    });
    assert_ne!(key(p2), key(p3));
}

#[test]
fn null_is_reflexive() {
    assert_eq!(key(Value::Null), key(Value::Null));
}

#[test]
fn hashset_dedups_by_equivalence() {
    let mut set: HashSet<GroupKey> = HashSet::new();
    set.insert(key(Value::Int(1)));
    set.insert(key(Value::Float(1.0)));
    set.insert(key(Value::Int(2)));
    // 1 and 1.0 collapse; 2 stands alone.
    assert_eq!(set.len(), 2);
}

#[test]
fn row_key_compares_columnwise_by_equivalence() {
    let r1 = vec![Value::Int(1), Value::Float(2.0)];
    let r2 = vec![Value::Float(1.0), Value::Int(2)];
    assert_eq!(row_key(&r1), row_key(&r2));
    let r3 = vec![Value::Int(1), Value::Float(3.0)];
    assert_ne!(row_key(&r1), row_key(&r3));
}

#[test]
fn property_index_key_shares_the_numeric_normalization() {
    // The one canonical numeric key: Int(1) and Float(1.0) collide.
    assert_eq!(
        GroupKey::for_property_index(&Value::Int(1)),
        GroupKey::for_property_index(&Value::Float(1.0))
    );
    assert_eq!(
        GroupKey::for_property_index(&Value::Float(-0.0)),
        GroupKey::for_property_index(&Value::Int(0))
    );
    // ... and it is the grouping key's tree, for every value.
    assert_eq!(
        GroupKey::for_property_index(&Value::Float(1.0)),
        Some(GroupKey::new(&Value::Int(1)))
    );
}

#[test]
fn property_index_rejects_nan_anywhere() {
    assert_eq!(GroupKey::for_property_index(&nan()), None);
    assert_eq!(
        GroupKey::for_property_index(&Value::List(vec![Value::Int(1), nan()].into())),
        None
    );
    let mut m = BTreeMap::new();
    m.insert("a".to_string(), nan());
    assert_eq!(GroupKey::for_property_index(&Value::Map(m.into())), None);
    let p = Value::Point(PointValue {
        srid: 7203,
        x: f64::NAN,
        y: 2.0,
        z: None,
    });
    assert_eq!(GroupKey::for_property_index(&p), None);
    let v = Value::Vector {
        coord: VectorCoordKind::Float64,
        values: vec![nan()].into(),
    };
    assert_eq!(GroupKey::for_property_index(&v), None);
    // NaN-free values stay indexable.
    assert!(GroupKey::for_property_index(&Value::Int(1)).is_some());
}

#[test]
fn identity_narrower_than_eq_exactly_for_named_zone_datetimes() {
    let zoned = Value::Temporal(Temporal::DateTime {
        utc: NaiveDate::from_ymd_opt(2026, 1, 1)
            .unwrap()
            .and_hms_opt(12, 0, 0)
            .unwrap(),
        offset: 3600,
        zone: Some("Europe/Stockholm".to_string()),
    });
    // The decline class: a named-zone datetime, bare or nested.
    assert!(identity_narrower_than_eq(&zoned));
    assert!(identity_narrower_than_eq(&Value::List(
        vec![zoned.clone()].into()
    )));
    let mut m = BTreeMap::new();
    m.insert("at".to_string(), zoned.clone());
    assert!(identity_narrower_than_eq(&Value::Map(m.into())));
    assert!(identity_narrower_than_eq(&Value::Vector {
        coord: VectorCoordKind::Float64,
        values: vec![zoned].into(),
    }));
    // An offset-only datetime never declines: `ZoneId::Offset`'s
    // (instant, offset) identity is a bijection of its `=` class
    // (wall clock, offset) — the identity bucket answers `=` exactly.
    let offset_only = |zone: Option<&str>, offset: i32| {
        Value::Temporal(Temporal::DateTime {
            utc: NaiveDate::from_ymd_opt(2026, 1, 1)
                .unwrap()
                .and_hms_opt(12, 0, 0)
                .unwrap(),
            offset,
            zone: zone.map(str::to_string),
        })
    };
    for clean in [
        offset_only(None, 3600),
        offset_only(Some("Z"), 0),
        offset_only(Some("+00:00"), 0),
        Value::List(vec![offset_only(None, 0)].into()),
    ] {
        assert!(
            !identity_narrower_than_eq(&clean),
            "{clean:?} must not decline"
        );
    }
    // Everything else: identity is the `=` class, so an identity
    // bucket answers a `=` lookup exactly.
    let local = Value::Temporal(Temporal::LocalDateTime(
        NaiveDate::from_ymd_opt(2026, 1, 1)
            .unwrap()
            .and_hms_opt(12, 0, 0)
            .unwrap(),
    ));
    let time = Value::Temporal(Temporal::Time {
        time: NaiveTime::from_hms_opt(12, 0, 0).unwrap(),
        offset: 3600,
    });
    for clean in [
        Value::Null,
        Value::Int(1),
        Value::Float(1.5),
        Value::String("s".into()),
        local,
        time,
        Value::List(vec![Value::Int(1), Value::Float(1.0)].into()),
    ] {
        assert!(
            !identity_narrower_than_eq(&clean),
            "{clean:?} must not decline"
        );
    }
}

#[test]
fn logical_bytes_tracks_payload() {
    let small = GroupKey::new(&Value::Int(1)).logical_bytes();
    let big = GroupKey::new(&Value::String("x".repeat(100).into())).logical_bytes();
    assert!(small > 0);
    assert!(big >= 100);
}