nodedb-types 0.6.2

Portable type definitions shared between NodeDB Origin and NodeDB-Lite
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
// SPDX-License-Identifier: Apache-2.0

//! Type-coerced equality and ordering for `Value`.
//!
//! Single source of truth for type coercion in filter/sort evaluation.
//! Numbers compare through the shared order in [`crate::numeric_cmp`].

use std::cmp::Ordering;

use super::core::Value;
use crate::numeric_cmp::{Numeric, cmp_numeric, decimal_reading, parse_numeric_str};

impl Value {
    /// Coerced equality: `Value` vs `Value` with numeric/string coercion.
    ///
    /// Single source of truth for type coercion in filter evaluation.
    /// Used by `matches_binary` (msgpack path) and `matches_value` (Value path).
    ///
    /// Two strings are equal by their text or by the instant they denote.
    /// Any other pair where both sides read as numbers is equal when
    /// [`cmp_numeric`] orders it `Equal`: exact for integers and decimals,
    /// and NaN equals NaN.
    pub fn eq_coerced(&self, other: &Value) -> bool {
        match (self, other) {
            (Value::Null, Value::Null) => true,
            (Value::Bool(a), Value::Bool(b)) => a == b,
            (Value::String(a), Value::String(b)) => {
                a == b
                    || matches!(
                        (crate::NdbDateTime::parse(a), crate::NdbDateTime::parse(b)),
                        (Some(x), Some(y)) if x.micros == y.micros
                    )
            }
            // Structural equality on ND cells: same coords and same attrs.
            (Value::ArrayCell(a), Value::ArrayCell(b)) => a == b,
            (a, b) => {
                if let (Some(x), Some(y)) = (numeric_reading(a), numeric_reading(b)) {
                    return cmp_numeric(x, y) == Ordering::Equal;
                }
                match (datetime_micros(a), datetime_micros(b)) {
                    (Some(x), Some(y)) => x == y,
                    _ => false,
                }
            }
        }
    }

    /// The hash bucket of this value under [`Value::eq_coerced`], for an
    /// equi-join or an index probe that must find every pair a WHERE `=`
    /// matches.
    ///
    /// Two values `eq_coerced` reports equal share a bucket. Two values in
    /// one bucket need not be equal (`'1.0'` and `'1'` as text, or two
    /// integers that round to one `f64`), so a join confirms each candidate
    /// with `eq_coerced`. `None` for a value that equals nothing, NULL
    /// included: it joins no row.
    ///
    /// A string reads first as a number, then as an instant, then as text.
    /// A string with both a numeric and an instant reading buckets as a
    /// number.
    pub fn eq_coerced_bucket(&self) -> Option<EqCoercedBucket> {
        match self {
            Value::Null => None,
            Value::Bool(b) => Some(EqCoercedBucket::Bool(*b)),
            Value::ArrayCell(_) => Some(EqCoercedBucket::Cell),
            other => {
                if let Some(n) = numeric_reading(other) {
                    return Some(EqCoercedBucket::Number(
                        crate::numeric_cmp::numeric_bucket_bits(n),
                    ));
                }
                if let Some(micros) = datetime_micros(other) {
                    return Some(EqCoercedBucket::Instant(micros));
                }
                match other {
                    Value::String(s) => Some(EqCoercedBucket::Text(s.clone())),
                    _ => None,
                }
            }
        }
    }

    /// Coerced partial ordering for predicate evaluation.
    ///
    /// Two numbers (or numeric strings) order by [`cmp_numeric`]: exact for
    /// integers and decimals, NaN above every number and equal to NaN, as
    /// in PostgreSQL. Two instants (or ISO-8601 strings) order by epoch
    /// microseconds, two other strings lexicographically, and two ND cells
    /// coordinate-major. A pair with no defined order — an integer against
    /// an instant, text against a number, a bool against a number — is
    /// `None`, so a range predicate over it matches nothing rather than
    /// every row: the row-level counterpart of PostgreSQL refusing to
    /// compare the two types.
    pub fn partial_cmp_coerced(&self, other: &Value) -> Option<Ordering> {
        if let (Value::ArrayCell(a), Value::ArrayCell(b)) = (self, other) {
            for (x, y) in a.coords.iter().zip(b.coords.iter()) {
                match x.partial_cmp_coerced(y)? {
                    Ordering::Equal => continue,
                    non_eq => return Some(non_eq),
                }
            }
            match a.coords.len().cmp(&b.coords.len()) {
                Ordering::Equal => {}
                non_eq => return Some(non_eq),
            }
            for (x, y) in a.attrs.iter().zip(b.attrs.iter()) {
                match x.partial_cmp_coerced(y)? {
                    Ordering::Equal => continue,
                    non_eq => return Some(non_eq),
                }
            }
            return Some(a.attrs.len().cmp(&b.attrs.len()));
        }
        if let (Some(a), Some(b)) = (numeric_reading(self), numeric_reading(other)) {
            return Some(cmp_numeric(a, b));
        }
        if let (Some(a), Some(b)) = (datetime_micros(self), datetime_micros(other)) {
            return Some(a.cmp(&b));
        }
        match (self, other) {
            (Value::String(a), Value::String(b)) => Some(a.cmp(b)),
            _ => None,
        }
    }

    /// Coerced total ordering for sorting.
    ///
    /// [`Value::partial_cmp_coerced`] with an unordered pair placed as
    /// `Equal`, so a stable sort keeps such rows in their input order. Only
    /// for ORDER BY / MIN / MAX style paths that need an `Ordering` for every
    /// pair; a predicate uses `partial_cmp_coerced` so an unordered pair
    /// matches nothing.
    pub fn cmp_coerced(&self, other: &Value) -> Ordering {
        self.partial_cmp_coerced(other).unwrap_or(Ordering::Equal)
    }
}

/// A hash bucket of [`Value::eq_coerced`]. See [`Value::eq_coerced_bucket`].
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum EqCoercedBucket {
    Bool(bool),
    /// The `f64` bits a number rounds to (see
    /// [`crate::numeric_cmp::numeric_bucket_bits`]).
    Number(u64),
    /// Epoch microseconds of an instant.
    Instant(i64),
    /// Text that reads as neither a number nor an instant.
    Text(String),
    /// Every ND cell: cells compare structurally, so they share one bucket.
    Cell,
}

/// The number a value denotes for coerced comparison: an integer, a float,
/// a decimal, or a string that parses as a number. A bool has no numeric
/// reading here. `None` for anything else.
fn numeric_reading(v: &Value) -> Option<Numeric> {
    match v {
        Value::Integer(i) => Some(Numeric::Int(i128::from(*i))),
        Value::Float(f) => Some(Numeric::Float(*f)),
        Value::Decimal(d) => Some(decimal_reading(d)),
        Value::String(s) => parse_numeric_str(s),
        _ => None,
    }
}

/// Epoch micros for a datetime-typed value, or a string that parses as an
/// ISO-8601 / SQL timestamp. Returns `None` for anything not datetime-like so
/// ordinary strings keep lexicographic ordering.
fn datetime_micros(v: &Value) -> Option<i64> {
    match v {
        Value::NaiveDateTime(dt) | Value::DateTime(dt) => Some(dt.micros),
        Value::String(s) => crate::NdbDateTime::parse(s).map(|dt| dt.micros),
        _ => None,
    }
}

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

    #[test]
    fn a_decimal_literal_equals_the_float_it_denotes() {
        let d = Value::Decimal(rust_decimal::Decimal::from_str_exact("2.5").expect("decimal"));
        assert!(d.eq_coerced(&Value::Float(2.5)));
        assert!(Value::Float(2.5).eq_coerced(&d));
        assert!(!d.eq_coerced(&Value::Float(2.25)));
        assert_eq!(
            d.partial_cmp_coerced(&Value::Integer(3)),
            Some(std::cmp::Ordering::Less)
        );
    }

    #[test]
    fn eq_coerced_same_type() {
        assert!(Value::Null.eq_coerced(&Value::Null));
        assert!(Value::Bool(true).eq_coerced(&Value::Bool(true)));
        assert!(!Value::Bool(true).eq_coerced(&Value::Bool(false)));
        assert!(Value::Integer(42).eq_coerced(&Value::Integer(42)));
        assert!(Value::Float(2.78).eq_coerced(&Value::Float(2.78)));
        assert!(Value::String("hello".into()).eq_coerced(&Value::String("hello".into())));
    }

    #[test]
    fn eq_coerced_int_float() {
        assert!(Value::Integer(5).eq_coerced(&Value::Float(5.0)));
        assert!(Value::Float(5.0).eq_coerced(&Value::Integer(5)));
        assert!(!Value::Integer(5).eq_coerced(&Value::Float(5.1)));
    }

    #[test]
    fn eq_coerced_string_number() {
        assert!(Value::String("5".into()).eq_coerced(&Value::Integer(5)));
        assert!(Value::Integer(5).eq_coerced(&Value::String("5".into())));
        assert!(Value::String("2.78".into()).eq_coerced(&Value::Float(2.78)));
        assert!(Value::Float(2.78).eq_coerced(&Value::String("2.78".into())));
        assert!(!Value::String("abc".into()).eq_coerced(&Value::Integer(5)));
        assert!(!Value::Integer(5).eq_coerced(&Value::String("abc".into())));
    }

    /// Every pair `eq_coerced` reports equal shares a bucket, so a join
    /// that probes by bucket and confirms by `eq_coerced` finds every match
    /// a WHERE `=` finds.
    #[test]
    fn equal_values_share_a_bucket() {
        let dec = |s: &str| Value::Decimal(rust_decimal::Decimal::from_str_exact(s).expect("dec"));
        let equal_pairs = [
            (Value::Integer(1), Value::String("1".into())),
            (Value::Integer(1), Value::String("1.0".into())),
            (Value::Integer(1), Value::Float(1.0)),
            (Value::Integer(1), dec("1.00")),
            (dec("2.5"), Value::Float(2.5)),
            (dec("2.50"), Value::String("2.5".into())),
            (Value::Float(0.0), Value::Float(-0.0)),
            (Value::Float(f64::NAN), Value::String("NaN".into())),
            (Value::String("abc".into()), Value::String("abc".into())),
            (Value::Bool(true), Value::Bool(true)),
        ];
        for (a, b) in equal_pairs {
            assert!(a.eq_coerced(&b), "{a:?} = {b:?}");
            assert_eq!(
                a.eq_coerced_bucket(),
                b.eq_coerced_bucket(),
                "{a:?} vs {b:?}"
            );
        }
    }

    #[test]
    fn unequal_values_are_told_apart() {
        assert_ne!(
            Value::Integer(1).eq_coerced_bucket(),
            Value::Integer(2).eq_coerced_bucket()
        );
        assert_ne!(
            Value::Bool(true).eq_coerced_bucket(),
            Value::String("true".into()).eq_coerced_bucket()
        );
        // Same bucket, different text: the confirming `eq_coerced` refuses.
        let a = Value::String("1.0".into());
        let b = Value::String("1".into());
        assert_eq!(a.eq_coerced_bucket(), b.eq_coerced_bucket());
        assert!(!a.eq_coerced(&b));
    }

    #[test]
    fn null_and_containers_join_nothing() {
        assert_eq!(Value::Null.eq_coerced_bucket(), None);
        assert_eq!(
            Value::Array(vec![Value::Integer(1)]).eq_coerced_bucket(),
            None
        );
    }

    #[test]
    fn eq_coerced_cross_type_false() {
        assert!(!Value::Bool(true).eq_coerced(&Value::Integer(1)));
        assert!(!Value::Null.eq_coerced(&Value::Integer(0)));
        assert!(!Value::Null.eq_coerced(&Value::String("".into())));
    }

    #[test]
    fn cmp_coerced_numeric() {
        use std::cmp::Ordering;
        assert_eq!(
            Value::Integer(5).cmp_coerced(&Value::Integer(10)),
            Ordering::Less
        );
        assert_eq!(
            Value::Integer(10).cmp_coerced(&Value::Float(5.0)),
            Ordering::Greater
        );
        assert_eq!(
            Value::String("90".into()).cmp_coerced(&Value::Integer(80)),
            Ordering::Greater
        );
        assert_eq!(
            Value::Float(2.78).cmp_coerced(&Value::String("2.78".into())),
            Ordering::Equal
        );
    }

    #[test]
    fn cmp_coerced_string_fallback() {
        use std::cmp::Ordering;
        assert_eq!(
            Value::String("abc".into()).cmp_coerced(&Value::String("def".into())),
            Ordering::Less
        );
        assert_eq!(
            Value::String("z".into()).cmp_coerced(&Value::String("a".into())),
            Ordering::Greater
        );
    }

    #[test]
    fn cmp_coerced_timestamp_mismatched_string_formats() {
        use std::cmp::Ordering;
        let stored = Value::String("2026-07-02T13:00:00.000000Z".into());
        assert_eq!(
            stored.cmp_coerced(&Value::String("2026-07-02 14:00:00".into())),
            Ordering::Less
        );
        assert_eq!(
            stored.cmp_coerced(&Value::String("2026-07-02 12:00:00".into())),
            Ordering::Greater
        );
    }

    #[test]
    fn eq_coerced_timestamp_mismatched_string_formats() {
        let stored = Value::String("2026-07-02T13:00:00.000000Z".into());
        assert!(stored.eq_coerced(&Value::String("2026-07-02 13:00:00".into())));
    }

    #[test]
    fn cmp_coerced_ordinary_strings_stay_lexicographic() {
        use std::cmp::Ordering;
        // Non-timestamp strings must NOT be datetime-coerced.
        assert_eq!(
            Value::String("apple".into()).cmp_coerced(&Value::String("banana".into())),
            Ordering::Less
        );
        assert_eq!(
            Value::String("zed".into()).cmp_coerced(&Value::String("abc".into())),
            Ordering::Greater
        );
    }

    #[test]
    fn partial_cmp_coerced_orders_instants_against_instants_and_iso_text() {
        use std::cmp::Ordering;
        let earlier = Value::NaiveDateTime(crate::NdbDateTime::from_micros(1_583_402_400_000_000));
        let later = Value::DateTime(crate::NdbDateTime::from_micros(1_583_406_000_000_000));
        assert_eq!(earlier.partial_cmp_coerced(&later), Some(Ordering::Less));
        assert_eq!(
            later.partial_cmp_coerced(&Value::String("2020-03-05 10:00:00".into())),
            Some(Ordering::Greater)
        );
        assert_eq!(
            earlier.partial_cmp_coerced(&Value::String("2020-03-05T10:00:00Z".into())),
            Some(Ordering::Equal)
        );
    }

    /// An integer carries no unit, so it has no order against an instant:
    /// `WHERE at >= 5` over an instant column matches nothing, in both
    /// orientations. The same holds for text against a number.
    #[test]
    fn partial_cmp_coerced_is_none_for_an_unordered_pair() {
        let instant = Value::NaiveDateTime(crate::NdbDateTime::from_micros(1_583_402_400_000_000));
        assert_eq!(instant.partial_cmp_coerced(&Value::Integer(5)), None);
        assert_eq!(Value::Integer(5).partial_cmp_coerced(&instant), None);
        assert_eq!(
            Value::Integer(5).partial_cmp_coerced(&Value::String("abc".into())),
            None
        );
        assert_eq!(
            Value::Bool(true).partial_cmp_coerced(&Value::Integer(1)),
            None
        );
        assert_eq!(Value::Null.partial_cmp_coerced(&Value::Integer(0)), None);
    }

    /// NaN sorts above every number and equals NaN, as in PostgreSQL, so
    /// `WHERE f > 1` matches a NaN row and `WHERE f = 'NaN'` matches it.
    #[test]
    fn nan_orders_above_every_number_and_equals_nan() {
        let nan = Value::Float(f64::NAN);
        assert_eq!(
            nan.partial_cmp_coerced(&Value::Float(1.0)),
            Some(Ordering::Greater)
        );
        assert_eq!(
            nan.partial_cmp_coerced(&Value::Float(f64::INFINITY)),
            Some(Ordering::Greater)
        );
        assert_eq!(
            Value::Integer(i64::MAX).partial_cmp_coerced(&nan),
            Some(Ordering::Less)
        );
        assert_eq!(
            Value::Decimal(rust_decimal::Decimal::MAX).partial_cmp_coerced(&nan),
            Some(Ordering::Less)
        );
        assert_eq!(
            nan.partial_cmp_coerced(&Value::Float(f64::NAN)),
            Some(Ordering::Equal)
        );
        assert_eq!(
            nan.partial_cmp_coerced(&Value::String("NaN".into())),
            Some(Ordering::Equal)
        );
        assert!(nan.eq_coerced(&Value::Float(f64::NAN)));
        assert!(!nan.eq_coerced(&Value::Float(1.0)));
        let mut values = [
            nan.clone(),
            Value::Integer(3),
            Value::Float(f64::NEG_INFINITY),
            Value::Float(0.5),
        ];
        values.sort_by(Value::cmp_coerced);
        assert_eq!(values[0], Value::Float(f64::NEG_INFINITY));
        assert_eq!(values[1], Value::Float(0.5));
        assert_eq!(values[2], Value::Integer(3));
        assert!(matches!(values[3], Value::Float(f) if f.is_nan()));
    }

    /// Two decimals one hundredth apart past `2^53` collapse to one `f64`.
    /// They compare exactly, as decimals and as decimal text.
    #[test]
    fn decimal_pairs_compare_exactly() {
        let dec = |s: &str| rust_decimal::Decimal::from_str_exact(s).expect("decimal");
        let low = Value::Decimal(dec("12345678901234567.01"));
        let high = Value::Decimal(dec("12345678901234567.02"));
        assert_eq!(low.partial_cmp_coerced(&high), Some(Ordering::Less));
        assert_eq!(high.cmp_coerced(&low), Ordering::Greater);
        assert!(!low.eq_coerced(&high));
        let high_text = Value::String("12345678901234567.02".into());
        assert_eq!(low.partial_cmp_coerced(&high_text), Some(Ordering::Less));
        assert!(high.eq_coerced(&high_text));
        assert!(!low.eq_coerced(&high_text));
        let half = Value::Decimal(dec("9007199254740993.5"));
        assert_eq!(
            half.partial_cmp_coerced(&Value::Integer(9_007_199_254_740_993)),
            Some(Ordering::Greater)
        );
        assert_eq!(
            Value::Integer(9_007_199_254_740_994).partial_cmp_coerced(&half),
            Some(Ordering::Greater)
        );
    }

    /// An `i64` near the limit compares exactly against a float, with no
    /// rounding of the integer to `f64`.
    #[test]
    fn large_integers_compare_exactly_against_floats() {
        const TWO_POW_63: f64 = 9_223_372_036_854_775_808.0;
        let max = Value::Integer(i64::MAX);
        assert_eq!(
            max.partial_cmp_coerced(&Value::Float(TWO_POW_63)),
            Some(Ordering::Less)
        );
        assert!(!max.eq_coerced(&Value::Float(TWO_POW_63)));
        assert_eq!(
            Value::Integer(i64::MIN).partial_cmp_coerced(&Value::Float(-TWO_POW_63)),
            Some(Ordering::Equal)
        );
        assert!(Value::Integer(i64::MIN).eq_coerced(&Value::Float(-TWO_POW_63)));
        let above = Value::Integer(9_007_199_254_740_993);
        let float = Value::Float(9_007_199_254_740_992.0);
        assert_eq!(above.partial_cmp_coerced(&float), Some(Ordering::Greater));
        assert_eq!(float.partial_cmp_coerced(&above), Some(Ordering::Less));
        assert!(!above.eq_coerced(&float));
        assert!(!float.eq_coerced(&above));
        assert_eq!(
            Value::Integer(i64::MAX - 1).partial_cmp_coerced(&Value::Integer(i64::MAX)),
            Some(Ordering::Less)
        );
        assert_eq!(
            Value::Integer(i64::MIN).partial_cmp_coerced(&Value::Float(f64::NEG_INFINITY)),
            Some(Ordering::Greater)
        );
        assert!(
            !Value::String("9007199254740993".into())
                .eq_coerced(&Value::Integer(9_007_199_254_740_992))
        );
    }

    /// The sort order places an unordered pair as `Equal` so a stable sort
    /// keeps its input order; an ordered pair sorts as the predicate orders
    /// it.
    #[test]
    fn cmp_coerced_places_an_unordered_pair_as_equal_for_sorting() {
        use std::cmp::Ordering;
        let instant = Value::NaiveDateTime(crate::NdbDateTime::from_micros(1_583_402_400_000_000));
        assert_eq!(instant.cmp_coerced(&Value::Integer(5)), Ordering::Equal);
        assert_eq!(
            Value::Integer(5).cmp_coerced(&Value::Integer(7)),
            Ordering::Less
        );
    }

    #[test]
    fn eq_coerced_symmetry() {
        let cases = [
            (Value::Integer(42), Value::String("42".into())),
            (Value::Float(2.78), Value::String("2.78".into())),
            (Value::Integer(5), Value::Float(5.0)),
        ];
        for (a, b) in &cases {
            assert_eq!(
                a.eq_coerced(b),
                b.eq_coerced(a),
                "symmetry violated for {a:?} vs {b:?}"
            );
        }
    }
}