nodedb-query 0.6.0

Shared query evaluation engine for NodeDB — expressions, filters, functions, aggregations, window functions
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
// SPDX-License-Identifier: Apache-2.0

//! Byte-level comparison and hashing for MessagePack field values.
//!
//! Operates on raw byte ranges returned by `extract_field`. Used for
//! join key matching, GROUP BY key deduplication, ORDER BY, and DISTINCT.

use std::cmp::Ordering;
use std::hash::{BuildHasher, Hasher};

use nodedb_types::read_instant;

use crate::msgpack_scan::reader::{read_integer, read_null, read_numeric, str_bounds};
use crate::numeric_cmp::cmp_numeric;

/// Hash the raw bytes of a MessagePack value at `range` within `buf`.
/// Uses a fast non-cryptographic hash suitable for hash joins and GROUP BY.
///
/// For canonical-encoded documents (integers in smallest form, sorted keys),
/// semantically equal values produce identical byte sequences and thus
/// identical hashes.
pub fn hash_field_bytes(buf: &[u8], range: (usize, usize)) -> u64 {
    let slice = match buf.get(range.0..range.1) {
        Some(s) => s,
        None => return 0,
    };
    let hasher_builder = std::collections::hash_map::RandomState::new();
    let mut hasher = hasher_builder.build_hasher();
    hasher.write(slice);
    hasher.finish()
}

/// Hash the raw bytes using a provided `RandomState` for consistent hashing
/// within a single query (all docs hashed with the same seed).
pub fn hash_field_bytes_with(
    buf: &[u8],
    range: (usize, usize),
    state: &std::collections::hash_map::RandomState,
) -> u64 {
    let slice = match buf.get(range.0..range.1) {
        Some(s) => s,
        None => return 0,
    };
    let mut hasher = state.build_hasher();
    hasher.write(slice);
    hasher.finish()
}

/// Compare two MessagePack values by their decoded content.
///
/// Comparison order:
/// 1. Null < Bool < Number < Instant < String < Binary < Array < Map < Ext
/// 2. Within numbers: exact numeric order; integers (`uint64` included)
///    never round through f64, an integer against a float compares
///    exactly, and NaN sorts above every number and equals NaN
/// 3. Within instants: by kind (UTC before naive), then signed epoch micros
/// 4. Within strings: lexicographic on raw bytes (valid UTF-8 guarantees
///    byte order = Unicode code-point order for ASCII/Latin-1)
/// 5. Fallback: raw byte comparison
pub fn compare_field_bytes(
    a_buf: &[u8],
    a_range: (usize, usize),
    b_buf: &[u8],
    b_range: (usize, usize),
) -> Ordering {
    let a_off = a_range.0;
    let b_off = b_range.0;

    let a_tag = match a_buf.get(a_off) {
        Some(&t) => t,
        None => return Ordering::Less,
    };
    let b_tag = match b_buf.get(b_off) {
        Some(&t) => t,
        None => return Ordering::Greater,
    };

    let a_type = type_rank(a_buf, a_off, a_tag);
    let b_type = type_rank(b_buf, b_off, b_tag);

    if a_type != b_type {
        return a_type.cmp(&b_type);
    }

    match a_type {
        RANK_NULL => Ordering::Equal,
        RANK_BOOL => {
            let a_val = a_tag == 0xc3; // true
            let b_val = b_tag == 0xc3;
            a_val.cmp(&b_val)
        }
        RANK_NUMBER => {
            // Exact: integers never round through f64. NaN sorts above
            // every number and equals NaN, so the order is total.
            match (read_numeric(a_buf, a_off), read_numeric(b_buf, b_off)) {
                (Some(a), Some(b)) => cmp_numeric(a, b),
                (Some(_), None) => Ordering::Greater,
                (None, Some(_)) => Ordering::Less,
                (None, None) => Ordering::Equal,
            }
        }
        RANK_INSTANT => {
            // Both sides decoded as instants by `type_rank`. Kind first, then
            // signed micros: the raw payload is not byte-comparable below 0.
            match (read_instant(a_buf, a_off), read_instant(b_buf, b_off)) {
                (Some((a_kind, a_us)), Some((b_kind, b_us))) => a_kind
                    .ext_type()
                    .cmp(&b_kind.ext_type())
                    .then(a_us.cmp(&b_us)),
                (Some(_), None) => Ordering::Greater,
                (None, Some(_)) => Ordering::Less,
                (None, None) => Ordering::Equal,
            }
        }
        RANK_STRING => {
            // string — compare raw bytes
            match (str_bounds(a_buf, a_off), str_bounds(b_buf, b_off)) {
                (Some((a_s, a_l)), Some((b_s, b_l))) => {
                    let a_bytes = &a_buf[a_s..a_s + a_l];
                    let b_bytes = &b_buf[b_s..b_s + b_l];
                    a_bytes.cmp(b_bytes)
                }
                _ => Ordering::Equal,
            }
        }
        _ => {
            // binary, array, map, ext — fallback to raw byte comparison
            let a_slice = &a_buf[a_range.0..a_range.1];
            let b_slice = &b_buf[b_range.0..b_range.1];
            a_slice.cmp(b_slice)
        }
    }
}

/// Compare two integer MessagePack values exactly, `uint64` included.
/// Useful when the caller knows both values are integers.
pub fn compare_field_i64(a_buf: &[u8], a_off: usize, b_buf: &[u8], b_off: usize) -> Ordering {
    match (read_integer(a_buf, a_off), read_integer(b_buf, b_off)) {
        (Some(a), Some(b)) => a.cmp(&b),
        (Some(_), None) => Ordering::Greater,
        (None, Some(_)) => Ordering::Less,
        (None, None) => Ordering::Equal,
    }
}

/// Check if two MessagePack values are byte-identical.
/// For canonical-encoded documents, byte equality implies semantic equality.
pub fn field_bytes_eq(
    a_buf: &[u8],
    a_range: (usize, usize),
    b_buf: &[u8],
    b_range: (usize, usize),
) -> bool {
    let a_slice = match a_buf.get(a_range.0..a_range.1) {
        Some(s) => s,
        None => return false,
    };
    let b_slice = match b_buf.get(b_range.0..b_range.1) {
        Some(s) => s,
        None => return false,
    };
    a_slice == b_slice
}

/// Check if a field value is null without extracting it.
pub fn is_field_null(buf: &[u8], range: (usize, usize)) -> bool {
    read_null(buf, range.0)
}

const RANK_NULL: u8 = 0;
const RANK_BOOL: u8 = 1;
const RANK_NUMBER: u8 = 2;
const RANK_INSTANT: u8 = 3;
const RANK_STRING: u8 = 4;
const RANK_BINARY: u8 = 5;
const RANK_ARRAY: u8 = 6;
const RANK_MAP: u8 = 7;
const RANK_EXT: u8 = 8;
const RANK_UNKNOWN: u8 = 9;

/// Type rank for cross-type ordering. Lower rank = sorts first.
/// Null < Bool < Number < Instant < String < Binary < Array < Map < Ext
///
/// An instant is a complete `fixext8` of type 1 / 2 at `off`. A `fixext8`
/// of any other type, or a truncated one, ranks as ext.
fn type_rank(buf: &[u8], off: usize, tag: u8) -> u8 {
    match tag {
        0xc0 => RANK_NULL,
        0xc2 | 0xc3 => RANK_BOOL,
        0x00..=0x7f | 0xe0..=0xff => RANK_NUMBER, // fixint
        0xca..=0xd3 => RANK_NUMBER,               // float/uint/int
        0xa0..=0xbf | 0xd9..=0xdb => RANK_STRING,
        0xc4..=0xc6 => RANK_BINARY,
        0x90..=0x9f | 0xdc | 0xdd => RANK_ARRAY,
        0x80..=0x8f | 0xde | 0xdf => RANK_MAP,
        0xd7 => {
            if read_instant(buf, off).is_some() {
                RANK_INSTANT
            } else {
                RANK_EXT
            }
        }
        0xc7..=0xc9 | 0xd4..=0xd6 | 0xd8 => RANK_EXT,
        _ => RANK_UNKNOWN,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    fn encode(v: &serde_json::Value) -> Vec<u8> {
        nodedb_types::json_msgpack::json_to_msgpack(v).expect("encode")
    }

    fn val_range(buf: &[u8]) -> (usize, usize) {
        (0, buf.len())
    }

    #[test]
    fn hash_same_bytes_same_hash() {
        let buf = encode(&json!(42));
        let state = std::collections::hash_map::RandomState::new();
        let h1 = hash_field_bytes_with(&buf, val_range(&buf), &state);
        let h2 = hash_field_bytes_with(&buf, val_range(&buf), &state);
        assert_eq!(h1, h2);
    }

    #[test]
    fn hash_different_values_likely_differ() {
        let buf1 = encode(&json!(42));
        let buf2 = encode(&json!(43));
        let state = std::collections::hash_map::RandomState::new();
        let h1 = hash_field_bytes_with(&buf1, val_range(&buf1), &state);
        let h2 = hash_field_bytes_with(&buf2, val_range(&buf2), &state);
        assert_ne!(h1, h2);
    }

    #[test]
    fn compare_integers() {
        let a = encode(&json!(10));
        let b = encode(&json!(20));
        assert_eq!(
            compare_field_bytes(&a, val_range(&a), &b, val_range(&b)),
            Ordering::Less
        );
        assert_eq!(
            compare_field_bytes(&b, val_range(&b), &a, val_range(&a)),
            Ordering::Greater
        );
        assert_eq!(
            compare_field_bytes(&a, val_range(&a), &a, val_range(&a)),
            Ordering::Equal
        );
    }

    #[test]
    fn compare_strings() {
        let a = encode(&json!("apple"));
        let b = encode(&json!("banana"));
        assert_eq!(
            compare_field_bytes(&a, val_range(&a), &b, val_range(&b)),
            Ordering::Less
        );
    }

    #[test]
    fn compare_cross_type_null_vs_number() {
        let a = encode(&json!(null));
        let b = encode(&json!(42));
        assert_eq!(
            compare_field_bytes(&a, val_range(&a), &b, val_range(&b)),
            Ordering::Less
        );
    }

    #[test]
    fn compare_cross_type_string_vs_number() {
        let a = encode(&json!(42));
        let b = encode(&json!("hello"));
        assert_eq!(
            compare_field_bytes(&a, val_range(&a), &b, val_range(&b)),
            Ordering::Less
        );
    }

    #[test]
    fn compare_booleans() {
        let a = encode(&json!(false));
        let b = encode(&json!(true));
        assert_eq!(
            compare_field_bytes(&a, val_range(&a), &b, val_range(&b)),
            Ordering::Less
        );
    }

    #[test]
    fn compare_negative_integers() {
        let a = encode(&json!(-10));
        let b = encode(&json!(-5));
        assert_eq!(
            compare_field_bytes(&a, val_range(&a), &b, val_range(&b)),
            Ordering::Less
        );
    }

    #[test]
    fn field_bytes_eq_works() {
        let a = encode(&json!("test"));
        let b = encode(&json!("test"));
        let c = encode(&json!("other"));
        assert!(field_bytes_eq(&a, val_range(&a), &b, val_range(&b)));
        assert!(!field_bytes_eq(&a, val_range(&a), &c, val_range(&c)));
    }

    #[test]
    fn is_field_null_works() {
        let null_buf = encode(&json!(null));
        let int_buf = encode(&json!(42));
        assert!(is_field_null(&null_buf, val_range(&null_buf)));
        assert!(!is_field_null(&int_buf, val_range(&int_buf)));
    }

    #[test]
    fn compare_floats() {
        let a = encode(&json!(1.5));
        let b = encode(&json!(2.5));
        assert_eq!(
            compare_field_bytes(&a, val_range(&a), &b, val_range(&b)),
            Ordering::Less
        );
    }

    fn cmp(a: &serde_json::Value, b: &serde_json::Value) -> Ordering {
        let (a, b) = (encode(a), encode(b));
        compare_field_bytes(&a, val_range(&a), &b, val_range(&b))
    }

    #[test]
    fn integers_past_two_pow_53_compare_exactly() {
        let above = json!(9_007_199_254_740_993_i64);
        let at = json!(9_007_199_254_740_992_i64);
        assert_eq!(cmp(&above, &at), Ordering::Greater);
        assert_eq!(cmp(&at, &above), Ordering::Less);
        assert_eq!(cmp(&above, &above), Ordering::Equal);
    }

    #[test]
    fn nanosecond_timestamps_compare_exactly() {
        let t1 = json!(1_700_000_000_000_000_001_i64);
        let t2 = json!(1_700_000_000_000_000_002_i64);
        assert_eq!(cmp(&t1, &t2), Ordering::Less);
        assert_eq!(cmp(&t2, &t1), Ordering::Greater);
    }

    #[test]
    fn uint64_above_i64_max_orders_above_every_i64() {
        assert_eq!(cmp(&json!(u64::MAX), &json!(i64::MAX)), Ordering::Greater);
        assert_eq!(cmp(&json!(u64::MAX - 1), &json!(u64::MAX)), Ordering::Less);
        assert_eq!(cmp(&json!(i64::MIN), &json!(u64::MAX)), Ordering::Less);
        let (a, b) = (encode(&json!(u64::MAX)), encode(&json!(-1)));
        assert_eq!(compare_field_i64(&a, 0, &b, 0), Ordering::Greater);
    }

    #[test]
    fn integer_against_float_compares_exactly() {
        let above = json!(9_007_199_254_740_993_i64);
        let float = json!(9_007_199_254_740_992.0_f64);
        assert_eq!(cmp(&above, &float), Ordering::Greater);
        assert_eq!(cmp(&float, &above), Ordering::Less);
        assert_eq!(cmp(&json!(2), &json!(2.5)), Ordering::Less);
        assert_eq!(cmp(&json!(3), &json!(3.0)), Ordering::Equal);
    }

    /// NaN sorts above every number and equals NaN, so a sort over raw
    /// cells is total.
    #[test]
    fn nan_cells_sort_above_every_number() {
        let float = |f: f64| {
            nodedb_types::value_to_msgpack(&nodedb_types::Value::Float(f)).expect("encode float")
        };
        let nan = float(f64::NAN);
        let inf = float(f64::INFINITY);
        let max = encode(&json!(u64::MAX));
        assert_eq!(
            compare_field_bytes(&nan, val_range(&nan), &inf, val_range(&inf)),
            Ordering::Greater
        );
        assert_eq!(
            compare_field_bytes(&max, val_range(&max), &nan, val_range(&nan)),
            Ordering::Less
        );
        assert_eq!(
            compare_field_bytes(&nan, val_range(&nan), &nan, val_range(&nan)),
            Ordering::Equal
        );
        let mut cells = [nan.clone(), encode(&json!(2)), float(-1.5), nan, max];
        cells.sort_by(|a, b| compare_field_bytes(a, val_range(a), b, val_range(b)));
        assert_eq!(cells[0], float(-1.5));
        assert_eq!(cells[1], encode(&json!(2)));
        assert_eq!(cells[2], encode(&json!(u64::MAX)));
    }

    #[test]
    fn numbers_keep_cross_rank_order() {
        assert_eq!(cmp(&json!(true), &json!(u64::MAX)), Ordering::Less);
        assert_eq!(cmp(&json!(u64::MAX), &json!("")), Ordering::Less);
        assert_eq!(cmp(&json!(null), &json!(-1.5)), Ordering::Less);
    }

    #[test]
    fn compare_instants_negative_micros() {
        use nodedb_types::{InstantKind, write_instant};
        let mut a = Vec::new();
        write_instant(&mut a, InstantKind::Utc, -10);
        let mut b = Vec::new();
        write_instant(&mut b, InstantKind::Utc, -5);
        let mut c = Vec::new();
        write_instant(&mut c, InstantKind::Utc, 1);
        assert_eq!(
            compare_field_bytes(&a, val_range(&a), &b, val_range(&b)),
            Ordering::Less
        );
        assert_eq!(
            compare_field_bytes(&a, val_range(&a), &c, val_range(&c)),
            Ordering::Less
        );
        assert_eq!(
            compare_field_bytes(&c, val_range(&c), &b, val_range(&b)),
            Ordering::Greater
        );
        assert_eq!(
            compare_field_bytes(&a, val_range(&a), &a, val_range(&a)),
            Ordering::Equal
        );
    }

    #[test]
    fn compare_instant_kinds_order_utc_first() {
        use nodedb_types::{InstantKind, write_instant};
        let mut utc = Vec::new();
        write_instant(&mut utc, InstantKind::Utc, 100);
        let mut naive = Vec::new();
        write_instant(&mut naive, InstantKind::Naive, 1);
        assert_eq!(
            compare_field_bytes(&utc, val_range(&utc), &naive, val_range(&naive)),
            Ordering::Less
        );
    }

    #[test]
    fn compare_instant_vs_integer_uses_rank() {
        use nodedb_types::{InstantKind, write_instant};
        // A negative instant has a payload starting 0xff, above any fixint
        // byte. Rank places it after every number and before every string.
        let mut inst = Vec::new();
        write_instant(&mut inst, InstantKind::Naive, -1);
        let int = encode(&json!(i64::MAX));
        let s = encode(&json!(""));
        assert_eq!(
            compare_field_bytes(&int, val_range(&int), &inst, val_range(&inst)),
            Ordering::Less
        );
        assert_eq!(
            compare_field_bytes(&inst, val_range(&inst), &s, val_range(&s)),
            Ordering::Less
        );
    }

    #[test]
    fn unknown_fixext8_ranks_as_ext() {
        use nodedb_types::{InstantKind, write_instant};
        let mut inst = Vec::new();
        write_instant(&mut inst, InstantKind::Utc, 0);
        let other = [0xd7, 0x09, 0, 0, 0, 0, 0, 0, 0, 0];
        assert_eq!(
            compare_field_bytes(&inst, val_range(&inst), &other, val_range(&other)),
            Ordering::Less
        );
    }

    #[test]
    fn hash_from_extracted_field() {
        let buf = encode(&json!({"id": 42}));
        let range = crate::msgpack_scan::field::extract_field(&buf, 0, "id").unwrap();
        let h = hash_field_bytes(&buf, range);
        assert_ne!(h, 0);
    }
}