relate-executor 1.0.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
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
//! Canonical equivalence key for `DISTINCT`, `GROUP BY`, `UNION`
//! deduplication, aggregate `DISTINCT` (`count(DISTINCT x)`, ...),
//! `coll.distinct`, and (in its equality reading) the property index, the
//! uniqueness probe and `MERGE`/`MATCH` equality buckets.
//!
//! Cypher's *equivalence* relation is deliberately **not** the same relation
//! as `=` equality (see `Value::eq_non_null` / `Value::cypher_eq`): `null` is
//! reflexive here (`null ≡ null`) while `null = null` is `null`, and every
//! NaN is its own equivalence class while `NaN = NaN` is false. This module is
//! the executor's single source of truth for both relations: one typed
//! [`GroupKey`] keys the five dedup/grouping sites, and its equality reading
//! keys every equality bucket, so `RETURN DISTINCT`, `GROUP BY`, `UNION`,
//! `count(DISTINCT ...)`, `coll.distinct(...)`, an indexed `{prop: value}`
//! seek, a uniqueness constraint and `MERGE` derive their verdicts from one
//! normalization -- the divergence the old `Value::to_cypher_literal()` string
//! keys had (they rendered `Int(1)` as `"1"` and `Float(1.0)` as `"1.0"`,
//! splitting groups and missing index seeks, while rendering every NaN as
//! `"NaN"`, merging groups).
//!
//! The Int/Float half of the numeric normalization is the one exact rule
//! owned by [`relate_model::canonical_key`] (ground-truthed against live
//! Neo4j) — `norm_f64` normalizes through its [`f64_exact_i64`], the same
//! primitive `=`/`<` evaluation (`Value::eq_non_null`/`compare`) routes
//! through, so an indexed `{prop: value}` seek, a `WHERE` residual, and a
//! `DISTINCT` bucket can never disagree:
//!
//! * numbers compare by **mathematical value**, ignoring the Int/Float
//!   distinction, recursively inside lists, maps, and vectors:
//!   `1 ≡ 1.0`, `0.0 ≡ -0.0`, `{a:[1]} ≡ {a:[1.0]}`.
//! * **every NaN is its own distinct key** -- never equivalent, even to
//!   itself (`UNWIND [0.0/0.0, 0.0/0.0] AS x RETURN DISTINCT x` yields two
//!   rows). This holds for a NaN nested arbitrarily deep inside a
//!   list/map/point too.
//! * strings are byte-identical only; a string never collides with a list
//!   (`'a,b' ≢ ['a','b']`).
//! * nodes/relationships compare by element id; two structurally identical
//!   nodes with different ids stay distinct. Paths key on their node/rel ids.
//! * temporals key on their value identity, not on a bare instant:
//!   `time('12:00+01:00') ≢ time('13:00+02:00')` (same instant, different
//!   local time/offset) while `time('12:00Z') ≡ time('12:00+00:00')`, and a
//!   zoned DateTime keys on its UTC instant **plus** its [`ZoneId`], so the
//!   two halves of a DST fall-back overlap stay distinct even though `=`
//!   holds between them.
//! * the **first-seen** occurrence's representation is what a dedup caller
//!   emits -- the key only decides set membership (`[1, 1.0]` → `1`;
//!   `[1.0, 1]` → `1.0`; `RETURN 1 UNION RETURN 1.0` → a single row `1`).
//!
//! The same tree keys both grouping and the property index. An index bucket
//! is the value's identity, not its `=` class: Neo4j's range index and
//! uniqueness constraint keep the two halves of a zoned-DateTime DST
//! fall-back overlap apart even though `=` holds between them (and an
//! indexed equality seek therefore does not find the `=`-equal partner,
//! exactly like a scan-free Neo4j plan). ADR-0446 originally coarsened the
//! index key to `=`, which made an indexed seek disagree with Neo4j's; the
//! measured suite corrected it.
//!
//! The key is a fully-owned, normalized tree (`Norm`) whose `Hash`/`Eq` are
//! derived, so the two can never fall out of sync -- the classic hazard of a
//! hand-written `Hash` paired with a hand-written `Eq`.

use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;

use chrono::{NaiveDate, NaiveDateTime, NaiveTime, Timelike};

use relate_core::temporal::ZoneId;
use relate_core::{Temporal, Value, VectorCoordKind};
use relate_model::canonical_key::f64_exact_i64;

/// Process-global monotonically increasing nonce source for NaN keys. Every
/// NaN encountered while a key is built claims a fresh nonce, so two NaNs
/// never collide -- including NaNs nested inside lists/maps/points. The
/// counter's actual values are irrelevant (equal values never reach this
/// path); only the guaranteed uniqueness matters, so `Relaxed` ordering is
/// sufficient.
static NAN_NONCE: AtomicU64 = AtomicU64::new(0);

fn next_nan_nonce() -> u64 {
    NAN_NONCE.fetch_add(1, Ordering::Relaxed)
}

/// A number normalized so that Int/Float and `+0.0`/`-0.0` collapse while
/// every NaN stays unique. The Int/Float collapse is the one exact rule from
/// [`relate_model::canonical_key`]; the NaN nonce is GroupKey-specific.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum NormNum {
    /// An exact integer value -- integral floats within `i64` range land here
    /// too, so `Int(1)` and `Float(1.0)` share one key.
    Int(i64),
    /// A non-integral (or out-of-`i64`-range) finite float, or an infinity,
    /// keyed by its IEEE-754 bit pattern. `-0.0`/`+0.0` never reach this arm
    /// (they normalize to `Int(0)`), so distinct bit patterns here always mean
    /// distinct mathematical values.
    Bits(u64),
    /// A NaN, tagged with a per-occurrence nonce so it is equivalent to
    /// nothing, not even another NaN.
    Nan(u64),
}

fn norm_f64(f: f64) -> NormNum {
    if f.is_nan() {
        return NormNum::Nan(next_nan_nonce());
    }
    // The exact Int/Float rule, shared with `=`/`<` evaluation: `-0.0` and
    // `+0.0` are both integral and in range, so both land in `Int(0)` --
    // giving `0.0 ≡ -0.0` for free. Anything without an exact `i64`
    // (non-integral, or at/beyond ±2^63) keys by bit pattern and therefore
    // never spuriously collides with the genuine integer
    // `i64::MAX`/`i64::MIN`.
    match f64_exact_i64(f) {
        Some(i) => NormNum::Int(i),
        None => NormNum::Bits(f.to_bits()),
    }
}

/// Normalized temporal. `Time` keys on its (UTC nanos-of-day, offset) pair —
/// `Temporal::equals`'s local-time-and-offset identity — and a zoned
/// `DateTime` on its UTC instant plus [`ZoneId`], so a named zone stays
/// apart from the offset it denotes while `Z` shares `+00:00`'s key, and
/// the two halves of a DST fall-back overlap (same wall clock and zone,
/// different instants) stay apart.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum NormTemporal {
    Date(NaiveDate),
    CivilDate {
        year: i64,
        month: u32,
        day: u32,
    },
    LocalTime(NaiveTime),
    Time {
        /// Nanoseconds since midnight UTC (`local time - offset`).
        utc_nanos: i64,
        offset: i32,
    },
    LocalDateTime(NaiveDateTime),
    CivilDateTime {
        year: i64,
        month: u32,
        day: u32,
        time: NaiveTime,
    },
    /// A zoned datetime: UTC instant plus zone.
    DateTime {
        utc: NaiveDateTime,
        zone: ZoneId,
    },
    Duration {
        months: i64,
        days: i64,
        nanos: i128,
    },
}

/// Nanoseconds since midnight, mirroring `Temporal::compare`'s private
/// `time_to_nanos`.
fn time_to_nanos(t: NaiveTime) -> i64 {
    t.num_seconds_from_midnight() as i64 * 1_000_000_000 + t.nanosecond() as i64
}

fn norm_temporal(t: &Temporal) -> NormTemporal {
    match t {
        Temporal::Date(d) => NormTemporal::Date(*d),
        Temporal::CivilDate { year, month, day } => NormTemporal::CivilDate {
            year: *year,
            month: *month,
            day: *day,
        },
        Temporal::LocalTime(tm) => NormTemporal::LocalTime(*tm),
        Temporal::Time { time, offset } => NormTemporal::Time {
            utc_nanos: time_to_nanos(*time) - i64::from(*offset) * 1_000_000_000,
            offset: *offset,
        },
        Temporal::LocalDateTime(dt) => NormTemporal::LocalDateTime(*dt),
        Temporal::CivilDateTime {
            year,
            month,
            day,
            time,
        } => NormTemporal::CivilDateTime {
            year: *year,
            month: *month,
            day: *day,
            time: *time,
        },
        Temporal::DateTime { utc, offset, zone } => NormTemporal::DateTime {
            utc: *utc,
            zone: ZoneId::of(zone.as_deref(), *offset),
        },
        Temporal::Duration {
            months,
            days,
            nanos,
        } => NormTemporal::Duration {
            months: *months,
            days: *days,
            nanos: *nanos,
        },
    }
}

/// The normalized equivalence tree. Distinct enum variants carry an implicit
/// type tag, so cross-type collisions (a string vs a list, a bool vs an int)
/// are impossible.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum Norm {
    /// All nulls share this one key (reflexive), the sole divergence from `=`.
    Null,
    Bool(bool),
    Num(NormNum),
    Str(Arc<str>),
    /// A byte array keys byte-wise (ADR-0327): its own variant, so bytes
    /// never collide with a list of the same integers.
    Bytes(Arc<[u8]>),
    List(Vec<Norm>),
    /// Map entries sorted by key so insertion order does not affect the key;
    /// values keyed recursively, so `{a:1} ≡ {a:1.0}`.
    Map(Vec<(String, Norm)>),
    Node(i64),
    Rel(i64),
    Path(Vec<Norm>, Vec<Norm>),
    Temporal(NormTemporal),
    Point {
        srid: i32,
        coords: Vec<NormNum>,
    },
    Uuid(u128),
    Vector {
        coord: VectorCoordKind,
        values: Vec<Norm>,
    },
}

fn norm(v: &Value) -> Norm {
    match v {
        Value::Null => Norm::Null,
        Value::Bool(b) => Norm::Bool(*b),
        Value::Int(i) => Norm::Num(NormNum::Int(*i)),
        Value::Float(f) => Norm::Num(norm_f64(*f)),
        Value::String(s) => Norm::Str(s.clone()),
        Value::Bytes(b) => Norm::Bytes(b.clone()),
        Value::Uuid(u) => Norm::Uuid(*u),
        Value::List(items) => Norm::List(items.iter().map(norm).collect()),
        Value::Map(m) => {
            let mut entries: Vec<(String, Norm)> =
                m.iter().map(|(k, val)| (k.clone(), norm(val))).collect();
            entries.sort_by(|a, b| a.0.cmp(&b.0));
            Norm::Map(entries)
        }
        Value::Node(n) => Norm::Node(n.id),
        Value::Relationship(r) => Norm::Rel(r.id),
        Value::Path(p) => Norm::Path(
            p.nodes.iter().map(|n| Norm::Node(n.id)).collect(),
            p.rels.iter().map(|r| Norm::Rel(r.id)).collect(),
        ),
        Value::Temporal(t) => Norm::Temporal(norm_temporal(t)),
        Value::Point(p) => {
            let mut coords = vec![norm_f64(p.x), norm_f64(p.y)];
            if let Some(z) = p.z {
                coords.push(norm_f64(z));
            }
            Norm::Point {
                srid: p.srid,
                coords,
            }
        }
        Value::Vector { coord, values } => Norm::Vector {
            // The coordinate kind tag keeps an INT8 vector distinct from a
            // FLOAT32 vector even when their element values coincide
            // numerically (matching `Value::eq_non_null`'s vector arm, which
            // compares `coord`). Elements are normalized numerically, so
            // `vector([1], ...)` ≡ `vector([1.0], ...)` — the same
            // elementwise-exact rule `eq_non_null` applies.
            coord: *coord,
            values: values.iter().map(norm).collect(),
        },
    }
}

/// A canonical equivalence key for a single `Value`, per the module-level
/// contract. `Hash + Eq` implement Cypher's *equivalence* relation, the one
/// both [`GroupKey::new`] and [`GroupKey::for_property_index`] encode.
///
/// The encoding is **injective** over the equivalence classes: two values map
/// to equal keys **iff** they are Cypher-equivalent (with the deliberate NaN
/// exception, where every occurrence is its own class). This is precisely the
/// property the old `to_cypher_literal`-string key lacked -- there
/// `Int(1)`/`Float(1.0)` collided-wrong-way while `"1,2"`/`[1,2]` and
/// quote-bearing strings aliased.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub(crate) struct GroupKey(Norm);

impl GroupKey {
    /// Build the equivalence key for a value. A NaN anywhere inside `value`
    /// claims a fresh nonce at this call, so the resulting key is unique and
    /// two separately-built keys over NaN-containing values never compare
    /// equal.
    pub(crate) fn new(value: &Value) -> Self {
        GroupKey(norm(value))
    }

    /// Key for the property index — the buckets of an indexed seek, the
    /// uniqueness probe and the scan re-check: the value's equivalence
    /// identity, except that a value containing a NaN anywhere is unindexable
    /// (`None`). A NaN never satisfies an equality seek (`NaN = NaN` is false),
    /// so indexing it could only create a bucket no seek key can ever address
    /// -- and, because every NaN key claims a fresh nonce, one the GC drop path
    /// could never recompute to remove.
    pub(crate) fn for_property_index(value: &Value) -> Option<Self> {
        if value_has_nan(value) {
            return None;
        }
        Some(GroupKey(norm(value)))
    }

    /// Estimated heap footprint of the key, for the ADR-0042 logical index
    /// accounting in `memory.rs` (which charges per index map entry).
    pub(crate) fn logical_bytes(&self) -> u64 {
        norm_bytes(&self.0)
    }

    /// The key's cheap projection for the distinct set's tiers: the one
    /// canonical derivation, so no site re-derives it. One exhaustive
    /// `match` over `Norm` with every variant spelled — a new variant
    /// breaks the build until it is classified.
    ///
    /// Exact by construction: each word payload is a bijection of its
    /// variant's field (`i64` reinterpreted, IEEE bits as-is, `bool` as
    /// 0/1), `Norm` has already applied every equivalence rule (`1 ≡ 1.0`,
    /// `±0.0 → Int(0)`), and the tag keeps variants apart, so
    /// `a.class() == b.class()` exactly when `a == b` for word keys. A NaN
    /// stays [`KeyClass::General`]: its nonce lives only in `GroupKey`
    /// equality. There is no inverse (word → key).
    pub(crate) fn class(&self) -> KeyClass {
        match &self.0 {
            Norm::Null => KeyClass::Null,
            Norm::Bool(b) => KeyClass::Word(WordTag::Bool, u64::from(*b)),
            Norm::Num(NormNum::Int(i)) => KeyClass::Word(WordTag::Int, *i as u64),
            Norm::Num(NormNum::Bits(bits)) => KeyClass::Word(WordTag::FloatBits, *bits),
            Norm::Num(NormNum::Nan(_)) => KeyClass::General,
            Norm::Node(id) => KeyClass::Word(WordTag::Node, *id as u64),
            Norm::Rel(id) => KeyClass::Word(WordTag::Rel, *id as u64),
            Norm::Str(_)
            | Norm::Bytes(_)
            | Norm::List(_)
            | Norm::Map(_)
            | Norm::Path(_, _)
            | Norm::Temporal(_)
            | Norm::Point { .. }
            | Norm::Uuid(_)
            | Norm::Vector { .. } => KeyClass::General,
        }
    }
}

/// [`GroupKey::class`]'s result: the reflexive null, a 64-bit word under its
/// type tag, or a key only the full [`GroupKey`] can represent.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum KeyClass {
    Null,
    Word(WordTag, u64),
    General,
}

/// The type tag of a word key. Words of different tags never meet: each
/// tag holds a disjoint set of `Norm` variants. The tag→table map is
/// `Spill::word_table` in `distinct_set` — one exhaustive `match`, so a
/// new tag is a compile error there.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum WordTag {
    Bool,
    Int,
    FloatBits,
    Node,
    Rel,
}

/// True if a NaN occurs anywhere inside `v` (recursively, mirroring `norm`).
fn value_has_nan(v: &Value) -> bool {
    match v {
        Value::Float(f) => f.is_nan(),
        Value::List(items) => items.iter().any(value_has_nan),
        Value::Map(m) => m.values().any(value_has_nan),
        Value::Point(p) => p.x.is_nan() || p.y.is_nan() || p.z.is_some_and(|z| z.is_nan()),
        Value::Vector { values, .. } => values.iter().any(value_has_nan),
        _ => false,
    }
}

/// Rough heap size of a normalized key tree: an 8-byte enum tag plus payload.
fn norm_bytes(n: &Norm) -> u64 {
    const TAG: u64 = 8;
    const HDR: u64 = 16; // String/Vec header.
    match n {
        Norm::Null | Norm::Bool(_) => TAG + 1,
        Norm::Num(_) => TAG + 16,
        Norm::Str(s) => TAG + HDR + s.len() as u64,
        Norm::Bytes(b) => TAG + HDR + b.len() as u64,
        Norm::List(items) => TAG + HDR + items.iter().map(norm_bytes).sum::<u64>(),
        Norm::Map(entries) => {
            TAG + HDR
                + entries
                    .iter()
                    .map(|(k, v)| HDR + k.len() as u64 + norm_bytes(v))
                    .sum::<u64>()
        }
        Norm::Node(_) | Norm::Rel(_) => TAG + 8,
        Norm::Path(ns, rs) => TAG + 2 * HDR + (ns.len() + rs.len()) as u64 * (TAG + 8),
        Norm::Temporal(_) => TAG + 24,
        Norm::Uuid(_) => TAG + 16,
        Norm::Point { coords, .. } => TAG + 4 + (TAG + 8) * coords.len() as u64,
        Norm::Vector { values, .. } => TAG + 1 + HDR + values.iter().map(norm_bytes).sum::<u64>(),
    }
}

/// Build the equivalence key for a whole row: its column values in column
/// order. Used by RETURN/WITH `DISTINCT` and `UNION` deduplication, where two
/// rows are the same iff every corresponding column value is equivalent.
pub(crate) fn row_key(vals: &[Value]) -> Vec<GroupKey> {
    vals.iter().map(GroupKey::new).collect()
}

#[cfg(test)]
mod tests {
    use super::*;
    use relate_core::{Node, PointValue, VectorCoordKind};
    use std::collections::{BTreeMap, HashSet};

    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() {
        // 02:30+02:00 and 02:30+01:00 on the fall-back day: equal local
        // date-time and zone, different offsets/instants. `=` holds,
        // `count(DISTINCT ...)` keeps both (TCK TemporalCompare).
        let local = NaiveDate::from_ymd_opt(2025, 10, 26)
            .unwrap()
            .and_hms_opt(2, 30, 0)
            .unwrap();
        let first = Temporal::DateTime {
            utc: local - chrono::Duration::hours(2),
            offset: 7200,
            zone: Some("Europe/Stockholm".to_string()),
        };
        let second = Temporal::DateTime {
            utc: local - chrono::Duration::hours(1),
            offset: 3600,
            zone: Some("Europe/Stockholm".to_string()),
        };
        assert!(first.equals(&second));
        assert_eq!(first.compare(&second), Some(std::cmp::Ordering::Less));
        let first = Value::Temporal(first);
        let second = Value::Temporal(second);
        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 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);
    }
}