relate-executor 1.3.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
//! A 128-bit structural hash of a [`Value`], and the memo table keyed by it
//! (LABS-1478, memo tier 3: a wide-value key).
//!
//! **Why no verification.** A memo hit is a wrong query result if two
//! different values share a key, so the key must be collision-free in
//! practice. A full-content 128-bit hash is: random collisions are about
//! 2^-128 per pair, far below hardware error rates, so a hit is one probe
//! with no equality walk. (A *sampled* hash cannot make that claim, its
//! collisions are structured, and is deliberately not offered.) The cost is
//! one pass over the value, O(size), paid once per distinct value the memo
//! sees; a key that repeats is paid once per lookup only by this hash, so a
//! caller holding a value across many lookups should keep its [`Hash128`]
//! (the companion hash). Caching the hash inside the shared container
//! itself needs a change to `Value`'s representation in `relate-core` and
//! is a separate decision.
//!
//! **Not cryptographic.** The value is flattened into one canonical byte
//! stream (a type tag per node, lengths before runs, so no two shapes share
//! a stream) fed to a streaming XXH3-128 (`xxhash-rust`, no dependencies)
//! seeded per process, so a client that controls the values cannot craft a
//! collision without the seed; it is not designed against a determined
//! adversary. Scalars append to a small stack buffer that is flushed to the
//! hasher when full, long strings and byte runs are hashed straight from
//! their slice, and a non-empty list of only integers (or only floats) is
//! one tagged run of eight-byte words, so a 1000-element numeric list is
//! a handful of bulk updates, not 1000.
//!
//! **What hashes.** The representation, tagged by variant: a value equal
//! under the engine's `=` but spelled differently (`1` and `1.0`) gets two
//! keys, which costs a missed hit and never a wrong one. Entities (node,
//! relationship, path), temporals, points and vectors return `None` (the
//! memo bypasses): an entity is keyed by its id in the id tables, not by
//! content. Nesting deeper than [`MAX_DEPTH`] returns `None`.

use std::hash::BuildHasher;
use std::sync::OnceLock;

use xxhash_rust::xxh3::Xxh3;

use relate_core::{NodeId, Value};

use crate::hash_seed::shared_random_state;

use super::memo::MemoTable;
use super::node_map::NodeMap;

/// Nesting deeper than this is not hashed (the memo bypasses).
const MAX_DEPTH: u32 = 24;

/// Bytes of scalar stream held before a flush into the hasher.
const BUF: usize = 512;

/// Stream tags. A non-empty all-Int / all-Float list has its own tag, so it
/// can never share a stream with the generic list holding the same items.
mod tag {
    pub(super) const NULL: u8 = 0;
    pub(super) const BOOL: u8 = 1;
    pub(super) const INT: u8 = 2;
    pub(super) const FLOAT: u8 = 3;
    pub(super) const STRING: u8 = 4;
    pub(super) const LIST: u8 = 5;
    pub(super) const MAP: u8 = 6;
    pub(super) const UUID: u8 = 7;
    pub(super) const BYTES: u8 = 8;
    pub(super) const INT_LIST: u8 = 9;
    pub(super) const FLOAT_LIST: u8 = 10;
}

/// A value's 128-bit content key.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) struct Hash128(u128);

impl Hash128 {
    /// The key split for the table: the high word verified on lookup, the
    /// low word as the map key (a `NodeId`-keyed map). The one place the
    /// halves are decoded.
    fn halves(self) -> (u64, NodeId) {
        ((self.0 >> 64) as u64, self.0 as u64 as NodeId)
    }
}

/// The stream's XXH3 seed: two words of the executor's one process-wide
/// secret ([`shared_random_state`]), folded once and cached.
fn seed() -> u64 {
    static SEED: OnceLock<u64> = OnceLock::new();
    *SEED.get_or_init(|| {
        let state = shared_random_state();
        state.hash_one(0x5eed_u64) ^ state.hash_one(0xfeed_u64).rotate_left(32)
    })
}

/// The canonical byte stream, buffered in front of a seeded XXH3-128.
struct Stream {
    xxh: Xxh3,
    buf: [u8; BUF],
    len: usize,
}

impl Stream {
    fn new() -> Self {
        Self {
            xxh: Xxh3::with_seed(seed()),
            buf: [0; BUF],
            len: 0,
        }
    }

    #[inline]
    fn flush(&mut self) {
        self.xxh.update(&self.buf[..self.len]);
        self.len = 0;
    }

    #[inline]
    fn put(&mut self, bytes: &[u8]) {
        debug_assert!(bytes.len() <= BUF);
        if self.len + bytes.len() > BUF {
            self.flush();
        }
        self.buf[self.len..self.len + bytes.len()].copy_from_slice(bytes);
        self.len += bytes.len();
    }

    #[inline]
    fn tag(&mut self, tag: u8) {
        self.put(&[tag]);
    }

    #[inline]
    fn word(&mut self, w: u64) {
        self.put(&w.to_le_bytes());
    }

    /// A byte run: its length, then the bytes. The length keeps adjacent
    /// runs apart, so `("ab","c")` and `("a","bc")` differ. A run that does
    /// not fit the buffer is hashed straight from its slice.
    fn bytes(&mut self, bytes: &[u8]) {
        self.word(bytes.len() as u64);
        if bytes.len() <= BUF {
            self.put(bytes);
        } else {
            self.flush();
            self.xxh.update(bytes);
        }
    }

    fn finish(mut self) -> Hash128 {
        self.flush();
        Hash128(self.xxh.digest128())
    }
}

/// The items when every one is an `Int` (`Some(true)`) or every one a
/// `Float` (`Some(false)`); `None` for an empty or mixed list.
fn numeric_run(items: &[Value]) -> Option<bool> {
    match items.first()? {
        Value::Int(_) => items
            .iter()
            .all(|v| matches!(v, Value::Int(_)))
            .then_some(true),
        Value::Float(_) => items
            .iter()
            .all(|v| matches!(v, Value::Float(_)))
            .then_some(false),
        _ => None,
    }
}

fn walk(s: &mut Stream, value: &Value, depth: u32) -> Option<()> {
    if depth > MAX_DEPTH {
        return None;
    }
    match value {
        Value::Null => s.tag(tag::NULL),
        Value::Bool(b) => {
            s.tag(tag::BOOL);
            s.tag(u8::from(*b));
        }
        Value::Int(i) => {
            s.tag(tag::INT);
            s.word(*i as u64);
        }
        Value::Float(f) => {
            s.tag(tag::FLOAT);
            s.word(f.to_bits());
        }
        Value::String(text) => {
            s.tag(tag::STRING);
            s.bytes(text.as_bytes());
        }
        Value::List(items) => match numeric_run(items) {
            Some(ints) => {
                // The elements sit one level down, as in the generic walk.
                if depth + 1 > MAX_DEPTH {
                    return None;
                }
                s.tag(if ints { tag::INT_LIST } else { tag::FLOAT_LIST });
                s.word(items.len() as u64);
                for item in items.iter() {
                    match item {
                        Value::Int(i) => s.word(*i as u64),
                        Value::Float(f) => s.word(f.to_bits()),
                        // `numeric_run` admitted only numeric items; anything else
                        // makes the value unkeyable, which only costs a recompute.
                        _ => return None,
                    }
                }
            }
            None => {
                s.tag(tag::LIST);
                s.word(items.len() as u64);
                for item in items.iter() {
                    walk(s, item, depth + 1)?;
                }
            }
        },
        Value::Map(entries) => {
            s.tag(tag::MAP);
            s.word(entries.len() as u64);
            for (key, item) in entries.iter() {
                s.bytes(key.as_bytes());
                walk(s, item, depth + 1)?;
            }
        }
        Value::Uuid(u) => {
            s.tag(tag::UUID);
            s.put(&u.to_le_bytes());
        }
        Value::Bytes(b) => {
            s.tag(tag::BYTES);
            s.bytes(b);
        }
        Value::Node(_)
        | Value::Relationship(_)
        | Value::Path(_)
        | Value::Temporal(_)
        | Value::Point(_)
        | Value::Vector { .. } => return None,
    }
    Some(())
}

// Calls to `hash_value` on this thread, so a test can prove a path never hashes.
#[cfg(test)]
thread_local! {
    pub(crate) static HASH_CALLS: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
}

/// `value`'s content key, `None` for a value the memo bypasses.
pub(crate) fn hash_value(value: &Value) -> Option<Hash128> {
    #[cfg(test)]
    HASH_CALLS.with(|c| c.set(c.get() + 1));
    let mut stream = Stream::new();
    walk(&mut stream, value, 0)?;
    Some(stream.finish())
}

/// What a stored answer owns beyond its own inline size, so a table that
/// holds heap-bearing answers charges them (ADR-0463): [`HashedValues`] adds
/// it to [`MemoTable::heap_bytes`], which is what the byte ceiling, the
/// retained meter and the query's shared memo budget all read.
pub(crate) trait RetainedHeap {
    /// Heap bytes this value owns.
    fn retained_heap(&self) -> usize;
}

/// Any `Clone` value per [`Hash128`] key, in the sparse node map: the low 64
/// bits index it and the high 64 are stored beside the value, so a hit
/// matches all 128 bits and a low-half clash is a miss, never a wrong hit.
/// The table's bytes are the map's allocation plus every stored value's
/// [`RetainedHeap`].
pub(crate) struct HashedValues<V> {
    map: NodeMap<(u64, V)>,
    payload: usize,
}

impl<V> Default for HashedValues<V> {
    fn default() -> Self {
        Self {
            map: NodeMap::default(),
            payload: 0,
        }
    }
}

impl<V: Clone + RetainedHeap> MemoTable for HashedValues<V> {
    type Key = Hash128;
    type Value = V;

    fn get(&self, key: Hash128) -> Option<V> {
        let (hi, lo) = key.halves();
        match self.map.get(lo) {
            Some((stored, value)) if *stored == hi => Some(value.clone()),
            _ => None,
        }
    }

    fn insert(&mut self, key: Hash128, value: V) {
        let (hi, lo) = key.halves();
        // A low-half clash replaces the resident entry: its payload leaves.
        let replaced = self.map.get(lo).map_or(0, |(_, old)| old.retained_heap());
        self.payload = self.payload - replaced + value.retained_heap();
        self.map.insert(lo, (hi, value));
    }

    fn heap_bytes(&self) -> usize {
        self.map.heap_bytes() + self.payload
    }
}

#[cfg(test)]
mod tests {
    use std::collections::{BTreeMap, HashSet};
    use std::sync::Arc;

    use super::super::memo::{Memo, MemoMeter, MemoPolicy};
    use super::*;

    fn s(text: &str) -> Value {
        Value::String(Arc::from(text))
    }

    fn list(items: Vec<Value>) -> Value {
        Value::List(Arc::new(items))
    }

    fn map(entries: &[(&str, Value)]) -> Value {
        Value::Map(Arc::new(
            entries
                .iter()
                .map(|(k, v)| ((*k).to_string(), v.clone()))
                .collect::<BTreeMap<_, _>>(),
        ))
    }

    fn h(value: &Value) -> Hash128 {
        hash_value(value).expect("hashable")
    }

    impl RetainedHeap for u32 {
        fn retained_heap(&self) -> usize {
            0
        }
    }
    impl RetainedHeap for u64 {
        fn retained_heap(&self) -> usize {
            0
        }
    }

    struct Unmetered;
    impl MemoMeter for Unmetered {
        fn charge(&self, _: &mut u64, _: u64) -> relate_core::Result<()> {
            Ok(())
        }
    }

    #[test]
    fn equal_values_hash_equal_and_variants_stay_apart() {
        assert_eq!(h(&s("abc")), h(&s("abc")));
        assert_eq!(
            h(&list(vec![Value::Int(1), s("x")])),
            h(&list(vec![Value::Int(1), s("x")]))
        );
        let keys: HashSet<_> = [
            Value::Null,
            Value::Bool(true),
            Value::Bool(false),
            Value::Int(1),
            Value::Int(0),
            Value::Float(1.0),
            s("1"),
            s(""),
            list(vec![]),
            map(&[]),
            Value::Uuid(1),
            Value::Bytes(Arc::from(&b"1"[..])),
        ]
        .iter()
        .map(h)
        .collect();
        assert_eq!(keys.len(), 12, "each of the twelve is its own key");
    }

    #[test]
    fn structure_boundaries_matter() {
        assert_ne!(
            h(&list(vec![
                list(vec![Value::Int(1)]),
                list(vec![Value::Int(2)])
            ])),
            h(&list(vec![list(vec![Value::Int(1), Value::Int(2)])]))
        );
        assert_ne!(
            h(&list(vec![s("ab"), s("c")])),
            h(&list(vec![s("a"), s("bc")]))
        );
        assert_ne!(h(&map(&[("ab", s("c"))])), h(&map(&[("a", s("bc"))])));
        assert_ne!(
            h(&list(vec![Value::Int(1), Value::Int(2)])),
            h(&list(vec![Value::Int(2), Value::Int(1)]))
        );
    }

    #[test]
    fn long_values_differing_in_the_middle_do_not_collide() {
        let base = "x".repeat(10_000);
        let mut other = base.clone().into_bytes();
        other[5_000] = b'y';
        let other = String::from_utf8(other).unwrap();
        assert_ne!(h(&s(&base)), h(&s(&other)));
    }

    #[test]
    fn no_collisions_over_many_distinct_values() {
        let mut seen = HashSet::new();
        for i in 0..100_000i64 {
            assert!(seen.insert(h(&Value::Int(i))), "int {i}");
            assert!(seen.insert(h(&s(&format!("k{i}")))), "string {i}");
        }
    }

    #[test]
    fn entities_and_over_deep_nesting_bypass() {
        let mut deep = Value::Int(0);
        for _ in 0..(MAX_DEPTH + 2) {
            deep = list(vec![deep]);
        }
        assert!(hash_value(&deep).is_none());
        let shallow = list(vec![list(vec![Value::Int(0)])]);
        assert!(hash_value(&shallow).is_some());
    }

    #[test]
    fn the_table_matches_all_128_bits() {
        let mut table = HashedValues::<u32>::default();
        let key = h(&s("alpha"));
        table.insert(key, 7);
        assert_eq!(table.get(key), Some(7));
        assert_eq!(table.get(h(&s("beta"))), None);
        // Same low half, different high half: a miss, not a wrong hit.
        let clash = Hash128((u128::from(!(key.0 >> 64) as u64) << 64) | (key.0 & u64::MAX as u128));
        assert_eq!(table.get(clash), None);
    }

    #[test]
    fn a_memo_over_value_keys_computes_once_per_distinct_value() {
        let policy = MemoPolicy {
            warm_up: 0,
            ..MemoPolicy::MODERATE
        };
        let mut memo = Memo::<HashedValues<u64>>::new(policy, crate::overlay::MemoLive::default());
        let blob = map(&[("a", list(vec![Value::Int(1), s("two")]))]);
        let mut calls = 0;
        for _ in 0..5 {
            let got = memo
                .answer(&Unmetered, h(&blob), || {
                    calls += 1;
                    Ok(42)
                })
                .unwrap();
            assert_eq!(got, 42);
        }
        assert_eq!(calls, 1);
    }
}

#[cfg(test)]
#[path = "value_hash_diff_tests.rs"]
mod diff_tests;