nedb-engine 4.3.0

NEDB v2 — content-addressed DAG storage engine with NQL and HTTP server (nedbd binary)
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
// SPDX-License-Identifier: BUSL-1.1
// SPDX-FileCopyrightText: © 2026 INTERCHAINED LLC × Claude Sonnet 4.6

//! Differential tests: the hash join against the nested loop.
//!
//! # The invariant
//!
//! For every join query both strategies can execute, they must return
//! IDENTICAL results — the same rows, with the same duplicates, in the same
//! ORDER. Not the same set. Order equality is deliberate and achievable: the
//! hash table holds right-hand row indices in ascending order and probing
//! walks left rows in order, so a correct hash join emits pairs in exactly the
//! sequence a nested loop would.
//!
//! Comparing sorted sets would hide two real bug classes — a duplicate emitted
//! once instead of twice, and outer rows placed in the wrong position — so it
//! is not done here.
//!
//! # Why a generator and not only hand-written cases
//!
//! Hand-written cases test what the author thought of. The failure mode this
//! engine has actually suffered is the case nobody imagined: a NULL key in a
//! `FULL JOIN` whose partner row also has a NULL key, a numeric string
//! matching a number on one side of the join but not the other, a duplicate
//! key on BOTH sides at once. The generator below enumerates and randomises
//! those combinations across a deliberately hostile value pool.
//!
//! The seed is fixed, so a failure is reproducible — a differential test that
//! cannot be replayed is a rumour, not a bug report.

use nedb_engine::sqljoin::{JoinExec, Strategy};
use nedb_engine::sqlselect::{execute_explain, parse};
use serde_json::{json, Value};

/// Values chosen to make dynamic-typed equality as awkward as possible.
///
/// `1` / `"1"` / `"1.0"` are the non-transitive triple: the first matches both
/// others, which do not match each other. `true` / `"t"` compare equal.
/// `0` / `-0.0` are numerically equal with different bit patterns. NULL
/// matches nothing, including itself.
fn pool() -> Vec<Value> {
    vec![
        Value::Null,
        json!(1),
        json!("1"),
        json!("1.0"),
        json!(2),
        json!(0),
        json!(-0.0),
        json!(true),
        json!("t"),
        json!("abc"),
    ]
}

/// A deterministic PRNG. Fixed constants, fixed seed, so every failure
/// replays exactly.
struct Lcg(u64);

impl Lcg {
    fn next(&mut self) -> u64 {
        self.0 = self
            .0
            .wrapping_mul(6_364_136_223_846_793_005)
            .wrapping_add(1_442_695_040_888_963_407);
        self.0 >> 11
    }
    fn below(&mut self, n: usize) -> usize {
        (self.next() % n as u64) as usize
    }
}

struct Fixture {
    left: Vec<Value>,
    right: Vec<Value>,
}

impl Fixture {
    fn resolve(&self) -> impl Fn(&str) -> anyhow::Result<Option<Box<dyn nedb_engine::sqlselect::Relation>>> + '_ {
        move |t: &str| {
            Ok(match t {
                "l" => Some(nedb_engine::sqlselect::from_vec(self.left.clone())),
                "r" => Some(nedb_engine::sqlselect::from_vec(self.right.clone())),
                _ => None,
            })
        }
    }
}

/// Run one query both ways and assert the two agree exactly.
///
/// Returns whether the hash path was genuinely exercised, so a caller can
/// prove its battery was not silently running the nested loop twice.
#[track_caller]
fn differ(sql: &str, fx: &Fixture) -> bool {
    let sel = parse(sql).unwrap_or_else(|e| panic!("{sql}\n  failed to parse: {e:#}"));
    let resolve = fx.resolve();

    let (nl_names, nl_rows, nl_choices) =
        execute_explain(&sel, &resolve, JoinExec::NestedLoop)
            .unwrap_or_else(|e| panic!("{sql}\n  nested loop failed: {e:#}"));
    let (h_names, h_rows, h_choices) = execute_explain(&sel, &resolve, JoinExec::Hash)
        .unwrap_or_else(|e| panic!("{sql}\n  hash join failed: {e:#}"));

    assert!(
        nl_choices.join_strategies().iter().all(|s| *s == Strategy::NestedLoop),
        "{sql}\n  forcing NestedLoop did not take effect"
    );

    assert_eq!(nl_names, h_names, "{sql}\n  column names differ");
    assert_eq!(
        nl_rows, h_rows,
        "{sql}\n  RESULTS DIFFER\n  left  = {}\n  right = {}\n  nested loop {:?}\n  hash {:?}",
        json!(nl_rows),
        json!(h_rows),
        nl_choices.render(),
        h_choices.render()
    );

    h_choices.join_strategies().contains(&Strategy::Hash)
}

// ─────────────────────────────────────────────────────────────────────────────
// Enumerated: every join kind × key shape × residual, over the hostile pool
// ─────────────────────────────────────────────────────────────────────────────

/// Relations covering every awkward pairing: each pool value against each
/// other, plus deliberate duplicates on both sides.
fn cross_fixture() -> Fixture {
    let p = pool();
    let mut left = vec![];
    let mut right = vec![];
    for (i, a) in p.iter().enumerate() {
        for (j, b) in p.iter().enumerate() {
            left.push(json!({"k1": a, "k2": b, "v": i * 10 + j}));
        }
    }
    for (i, a) in p.iter().enumerate() {
        for b in p.iter() {
            right.push(json!({"k1": a, "k2": b, "w": i}));
        }
        // A duplicate of every row, so "multiple matching rows" is real on the
        // BUILD side and not merely on the probe side.
        right.push(json!({"k1": a, "k2": a, "w": 999}));
    }
    Fixture { left, right }
}

const KINDS: &[&str] = &["JOIN", "LEFT JOIN", "RIGHT JOIN", "FULL JOIN"];

const RESIDUALS: &[&str] = &[
    "",
    " AND l.v > 30",
    " AND r.w IS NOT NULL",
    " AND l.k2 = r.k2",
    " AND (l.v > 30 OR r.w = 1)",
    " AND l.v IS NOT NULL AND r.w BETWEEN 0 AND 4",
];

#[test]
fn every_join_kind_agrees_on_a_single_key() {
    let fx = cross_fixture();
    let mut hashed = 0;
    for kind in KINDS {
        for res in RESIDUALS {
            let sql = format!(
                "SELECT l.v, r.w FROM l {kind} r ON l.k1 = r.k1{res} \
                 ORDER BY 1, 2"
            );
            if differ(&sql, &fx) {
                hashed += 1;
            }
        }
    }
    assert_eq!(
        hashed,
        KINDS.len() * RESIDUALS.len(),
        "every case must actually have taken the hash path"
    );
}

#[test]
fn every_join_kind_agrees_on_a_compound_key() {
    let fx = cross_fixture();
    let mut hashed = 0;
    for kind in KINDS {
        let sql = format!(
            "SELECT l.v, r.w FROM l {kind} r ON l.k1 = r.k1 AND l.k2 = r.k2 \
             ORDER BY 1, 2"
        );
        if differ(&sql, &fx) {
            hashed += 1;
        }
    }
    assert_eq!(hashed, KINDS.len());
}

#[test]
fn unordered_results_agree_row_for_row_including_order() {
    // No ORDER BY: the two strategies must emit rows in the SAME sequence,
    // which is a far sharper claim than agreeing as multisets.
    let fx = cross_fixture();
    for kind in KINDS {
        let sql = format!("SELECT l.v, r.w FROM l {kind} r ON l.k1 = r.k1");
        assert!(differ(&sql, &fx));
    }
}

#[test]
fn expression_keys_agree() {
    let fx = cross_fixture();
    for kind in KINDS {
        for on in [
            "lower(l.k1) = lower(r.k1)",
            "l.k1 = r.k1::text",
            "coalesce(l.k1, 'x') = coalesce(r.k1, 'x')",
        ] {
            let sql = format!("SELECT l.v, r.w FROM l {kind} r ON {on} ORDER BY 1, 2");
            assert!(differ(&sql, &fx), "{sql}");
        }
    }
}

#[test]
fn a_key_that_is_null_on_both_sides_still_does_not_join() {
    // `coalesce(k1,'x')` above makes NULLs match; plain `k1 = k1` must not.
    let fx = Fixture {
        left: vec![json!({"k1": null, "v": 1})],
        right: vec![json!({"k1": null, "w": 2})],
    };
    for (kind, want) in [
        ("JOIN", 0),
        ("LEFT JOIN", 1),
        ("RIGHT JOIN", 1),
        ("FULL JOIN", 2),
    ] {
        let sql = format!("SELECT l.v, r.w FROM l {kind} r ON l.k1 = r.k1");
        differ(&sql, &fx);
        let sel = parse(&sql).unwrap();
        let (_, rows, _) = execute_explain(&sel, &fx.resolve(), JoinExec::Hash).unwrap();
        assert_eq!(rows.len(), want, "{sql}");
    }
}

#[test]
fn an_empty_relation_on_either_side_agrees() {
    let full = vec![json!({"k1": 1, "v": 1}), json!({"k1": null, "v": 2})];
    for kind in KINDS {
        for fx in [
            Fixture { left: vec![], right: full.clone() },
            Fixture { left: full.clone(), right: vec![] },
            Fixture { left: vec![], right: vec![] },
        ] {
            let sql = format!("SELECT l.v, r.w FROM l {kind} r ON l.k1 = r.k1");
            differ(&sql, &fx);
        }
    }
}

#[test]
fn a_three_relation_chain_agrees() {
    let sql = "SELECT l.v, r.w FROM l \
               JOIN r ON l.k1 = r.k1 \
               LEFT JOIN l AS l2 ON r.k1 = l2.k1 \
               ORDER BY 1, 2";
    let p = pool();
    let mut left = vec![];
    for (i, a) in p.iter().enumerate() {
        left.push(json!({"k1": a, "k2": a, "v": i}));
    }
    let right = left
        .iter()
        .enumerate()
        .map(|(i, r)| json!({"k1": r["k1"], "k2": r["k2"], "w": i}))
        .collect();
    let fx = Fixture { left: left.clone(), right };

    let sel = parse(sql).unwrap();
    // `l AS l2` resolves through the same relation, so the resolver serves it.
    let resolve = |t: &str| -> anyhow::Result<Option<Box<dyn nedb_engine::sqlselect::Relation>>> {
        Ok(match t {
            "l" => Some(nedb_engine::sqlselect::from_vec(fx.left.clone())),
            "r" => Some(nedb_engine::sqlselect::from_vec(fx.right.clone())),
            _ => None,
        })
    };
    let (n1, r1, c1) = execute_explain(&sel, &resolve, JoinExec::NestedLoop).unwrap();
    let (n2, r2, c2) = execute_explain(&sel, &resolve, JoinExec::Hash).unwrap();
    assert_eq!(n1, n2);
    assert_eq!(r1, r2, "chained joins disagree\n{c1:?}\n{c2:?}");
    assert_eq!(c2.joins().len(), 2, "both joins accounted for");
    assert!(c2.join_strategies().iter().all(|s| *s == Strategy::Hash));
}

// ─────────────────────────────────────────────────────────────────────────────
// Generated
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn generated_join_queries_agree() {
    let p = pool();
    let mut rng = Lcg(0x5EED_1234_ABCD_0001);
    let mut hashed = 0;
    let mut total = 0;

    for case in 0..600 {
        // Relation sizes deliberately include 0 and 1.
        let ln = rng.below(9);
        let rn = rng.below(9);
        let left: Vec<Value> = (0..ln)
            .map(|i| {
                json!({
                    "k1": p[rng.below(p.len())],
                    "k2": p[rng.below(p.len())],
                    "v": i,
                })
            })
            .collect();
        let right: Vec<Value> = (0..rn)
            .map(|i| {
                json!({
                    "k1": p[rng.below(p.len())],
                    "k2": p[rng.below(p.len())],
                    "w": i,
                })
            })
            .collect();
        let fx = Fixture { left, right };

        let kind = KINDS[rng.below(KINDS.len())];
        let keys = match rng.below(3) {
            0 => "l.k1 = r.k1",
            1 => "l.k1 = r.k1 AND l.k2 = r.k2",
            _ => "l.k2 = r.k1",
        };
        let res = RESIDUALS[rng.below(RESIDUALS.len())];
        let order = if rng.below(2) == 0 { " ORDER BY 1, 2" } else { "" };
        let sql = format!("SELECT l.v, r.w FROM l {kind} r ON {keys}{res}{order}");

        total += 1;
        if differ(&sql, &fx) {
            hashed += 1;
        }
        // A tripwire against the generator degenerating into empty relations.
        if case == 599 {
            assert!(total == 600);
        }
    }

    // Not every generated case can hash — an empty relation produces no join
    // work at all — but the great majority must, or this test is theatre.
    assert!(
        hashed > 400,
        "only {hashed}/{total} generated cases took the hash path"
    );
}

#[test]
fn generated_queries_on_larger_relations_agree() {
    // Big enough that `Auto` would pick the hash path in production, so the
    // sizes the optimiser actually optimises are the sizes under test.
    let p = pool();
    let mut rng = Lcg(0xC0FF_EE00_D15E_A5E1);
    for _ in 0..40 {
        let left: Vec<Value> = (0..120)
            .map(|i| json!({"k1": p[rng.below(p.len())], "v": i}))
            .collect();
        let right: Vec<Value> = (0..120)
            .map(|i| json!({"k1": p[rng.below(p.len())], "w": i}))
            .collect();
        let fx = Fixture { left, right };
        let kind = KINDS[rng.below(KINDS.len())];
        let sql = format!("SELECT l.v, r.w FROM l {kind} r ON l.k1 = r.k1");
        assert!(differ(&sql, &fx), "{sql}");
    }
}

#[test]
fn auto_picks_the_hash_path_only_once_the_work_justifies_it() {
    let small = Fixture {
        left: (0..4).map(|i| json!({"k1": 1, "v": i})).collect(),
        right: (0..4).map(|i| json!({"k1": 1, "w": i})).collect(),
    };
    let big = Fixture {
        left: (0..40).map(|i| json!({"k1": 1, "v": i})).collect(),
        right: (0..40).map(|i| json!({"k1": 1, "w": i})).collect(),
    };
    let sel = parse("SELECT l.v, r.w FROM l JOIN r ON l.k1 = r.k1").unwrap();

    let (_, _, c) = execute_explain(&sel, &small.resolve(), JoinExec::Auto).unwrap();
    assert_eq!(c.join_strategy(0), Some(Strategy::NestedLoop), "16 pairs is not worth a table");

    let (_, _, c) = execute_explain(&sel, &big.resolve(), JoinExec::Auto).unwrap();
    assert_eq!(c.join_strategy(0), Some(Strategy::Hash), "1600 pairs is");
}

#[test]
fn a_join_with_no_equality_key_reports_the_nested_loop_under_every_setting() {
    let fx = cross_fixture();
    let sel = parse("SELECT l.v, r.w FROM l JOIN r ON l.v > r.w").unwrap();
    for exec in [JoinExec::Auto, JoinExec::NestedLoop, JoinExec::Hash] {
        let (_, _, c) = execute_explain(&sel, &fx.resolve(), exec).unwrap();
        assert_eq!(c.join_strategy(0), Some(Strategy::NestedLoop), "{exec:?}");
        assert_eq!(c.join_keys(0), Some(0));
    }
}