nescio 0.6.0

nescioDB (Aporia): a database whose primary object is ignorance — evidence with decay, regions with entropy, and queries that plan their own data procurement.
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
//! JOIN: relational predicates between entities, under uncertainty.
//!
//! A join compares a slot of one entity with a slot of another. Because
//! both sides are regions, the truth of the join is itself uncertain —
//! every match carries a graded `probability` AND a three-valued
//! `certainty` (the region-containment answer, consistent with the
//! `certainly` verb).
//!
//! Distinct entities carry independent posteriors, so the join
//! probability is an exact integral over their product measure — no
//! possible-worlds sampling, no lineage bookkeeping. That independence is
//! what makes `P(A.price > B.price)` a closed-form sum here, where
//! value-uncertain databases need Monte-Carlo or lineage tracking to join
//! over uncertainty.
//!
//! Pruning uses the same support hulls as FIND: a pair is examined only
//! when the predicate is regionally *possible*. An unselective join
//! (every region overlaps every other) is quadratic — fundamental to
//! joins, SQL included — so evaluation is capped at [`MAX_PAIRS`] and the
//! cap is reported, never silently applied.

use std::collections::{BTreeMap, HashMap, HashSet};

use crate::error::{Error, Result};
use crate::model::domain::Domain;

use super::inference::support_indices;
use super::types::{JoinMatch, JoinOptions, JoinPredicate, JoinResult, Tri};
use super::Query;

/// Ceiling on regionally-possible pairs evaluated in one join. A selective
/// join stays far below it; an unselective one hits it and is flagged
/// `truncated`.
const MAX_PAIRS: usize = 3_000_000;

struct Prep {
    id: String,
    post: Vec<f64>,
    support: Vec<usize>,
}

impl Query<'_> {
    /// Join entities on a relational predicate. See the module docs for the
    /// probability/certainty semantics.
    pub fn join(&self, pred: &JoinPredicate, opts: &JoinOptions) -> Result<JoinResult> {
        let (ls, rs) = pred.slots();
        let ld = self.db.domain(ls)?.clone();
        let rd = self.db.domain(rs)?.clone();
        if pred.is_numeric() {
            if !matches!(ld, Domain::Continuous { .. }) || !matches!(rd, Domain::Continuous { .. })
            {
                return Err(Error::Invalid(
                    "gt/lt/approx joins require continuous slots on both sides".into(),
                ));
            }
        } else if !matches!(ld, Domain::Categorical { .. })
            || !matches!(rd, Domain::Categorical { .. })
        {
            return Err(Error::Invalid(
                "same joins require categorical slots on both sides".into(),
            ));
        }

        let left = self.prepare(ls, opts.left_prefix.as_deref(), opts.require_evidence)?;
        let symmetric = ls == rs && opts.left_prefix == opts.right_prefix;
        let right_owned = if symmetric {
            Vec::new()
        } else {
            self.prepare(rs, opts.right_prefix.as_deref(), opts.require_evidence)?
        };
        let right: &[Prep] = if symmetric { &left } else { &right_owned };

        let mut matches = Vec::new();
        let mut examined = 0usize;
        let capped = if pred.is_numeric() {
            self.join_numeric(
                pred,
                opts,
                &ld,
                &rd,
                &left,
                right,
                symmetric,
                &mut matches,
                &mut examined,
            )
        } else {
            self.join_same(
                pred,
                opts,
                &ld,
                &rd,
                &left,
                right,
                symmetric,
                &mut matches,
                &mut examined,
            )
        };

        matches.sort_by(|a, b| {
            b.probability
                .total_cmp(&a.probability)
                .then(a.left.cmp(&b.left))
                .then(a.right.cmp(&b.right))
        });
        let truncated = capped || matches.len() > opts.limit;
        matches.truncate(opts.limit);
        Ok(JoinResult {
            matches,
            pairs_examined: examined,
            truncated,
        })
    }

    fn prepare(
        &self,
        slot: &str,
        prefix: Option<&str>,
        require_evidence: bool,
    ) -> Result<Vec<Prep>> {
        let mut out = Vec::new();
        for e in self.db.entities() {
            if let Some(p) = prefix {
                if !e.starts_with(p) {
                    continue;
                }
            }
            if require_evidence && self.db.evidence_for(e, slot).is_empty() {
                continue;
            }
            let post = self.marginal(e, slot, &BTreeMap::new(), &[])?;
            let support = support_indices(&post);
            out.push(Prep {
                id: e.to_string(),
                post,
                support,
            });
        }
        Ok(out)
    }

    #[allow(clippy::too_many_arguments)]
    fn join_numeric(
        &self,
        pred: &JoinPredicate,
        opts: &JoinOptions,
        ld: &Domain,
        rd: &Domain,
        left: &[Prep],
        right: &[Prep],
        symmetric: bool,
        matches: &mut Vec<JoinMatch>,
        examined: &mut usize,
    ) -> bool {
        let lmid: Vec<f64> = (0..ld.n()).map(|i| ld.midpoint(i)).collect();
        let rmid: Vec<f64> = (0..rd.n()).map(|i| rd.midpoint(i)).collect();
        let rmin: Vec<f64> = right.iter().map(|p| rmid[p.support[0]]).collect();
        let rmax: Vec<f64> = right
            .iter()
            .map(|p| rmid[*p.support.last().unwrap()])
            .collect();
        let tol = match pred {
            JoinPredicate::Approx { tol, .. } => *tol,
            _ => 0.0,
        };
        // Order the right side so the pruning break is monotone.
        let mut order: Vec<usize> = (0..right.len()).collect();
        match pred {
            JoinPredicate::Lt { .. } => order.sort_by(|&a, &b| rmax[b].total_cmp(&rmax[a])),
            _ => order.sort_by(|&a, &b| rmin[a].total_cmp(&rmin[b])),
        }
        let dedup = symmetric && pred.is_symmetric();

        for a in left {
            let a_min = lmid[a.support[0]];
            let a_max = lmid[*a.support.last().unwrap()];
            for &ri in &order {
                let (b_min, b_max) = (rmin[ri], rmax[ri]);
                match pred {
                    JoinPredicate::Gt { .. } => {
                        if b_min >= a_max {
                            break; // possible iff a_max > b_min
                        }
                    }
                    JoinPredicate::Lt { .. } => {
                        if b_max <= a_min {
                            break; // possible iff a_min < b_max
                        }
                    }
                    JoinPredicate::Approx { .. } => {
                        if b_min > a_max + tol {
                            break;
                        }
                        if b_max < a_min - tol {
                            continue;
                        }
                    }
                    JoinPredicate::Same { .. } => unreachable!(),
                }
                let b = &right[ri];
                if a.id == b.id {
                    continue;
                }
                if dedup && a.id >= b.id {
                    continue;
                }
                *examined += 1;
                if *examined > MAX_PAIRS {
                    return true;
                }
                let tri = match pred {
                    JoinPredicate::Gt { .. } => tri_gt(a_min, a_max, b_min, b_max),
                    JoinPredicate::Lt { .. } => tri_gt(b_min, b_max, a_min, a_max),
                    JoinPredicate::Approx { .. } => tri_approx(a_min, a_max, b_min, b_max, tol),
                    JoinPredicate::Same { .. } => unreachable!(),
                };
                if tri == Tri::False {
                    continue;
                }
                if opts.certain_only && tri != Tri::True {
                    continue;
                }
                let p = match pred {
                    JoinPredicate::Gt { .. } => p_gt(&a.post, &lmid, &b.post, &rmid),
                    JoinPredicate::Lt { .. } => p_gt(&b.post, &rmid, &a.post, &lmid),
                    JoinPredicate::Approx { .. } => p_approx(&a.post, &lmid, &b.post, &rmid, tol),
                    JoinPredicate::Same { .. } => unreachable!(),
                };
                if p + 1e-12 < opts.min_probability {
                    continue;
                }
                matches.push(JoinMatch {
                    left: a.id.clone(),
                    right: b.id.clone(),
                    probability: p,
                    certainty: tri,
                });
            }
        }
        false
    }

    #[allow(clippy::too_many_arguments)]
    fn join_same(
        &self,
        pred: &JoinPredicate,
        opts: &JoinOptions,
        ld: &Domain,
        rd: &Domain,
        left: &[Prep],
        right: &[Prep],
        symmetric: bool,
        matches: &mut Vec<JoinMatch>,
        examined: &mut usize,
    ) -> bool {
        let lvals = match ld {
            Domain::Categorical { values } => values,
            _ => unreachable!(),
        };
        let rvals = match rd {
            Domain::Categorical { values } => values,
            _ => unreachable!(),
        };
        // Each right entity's support as a set of value labels, plus buckets
        // label -> right positions. A "same" pair is only possible when the
        // supports intersect, so we only ever visit compatible candidates.
        let r_labels: Vec<HashSet<&str>> = right
            .iter()
            .map(|p| p.support.iter().map(|&i| rvals[i].as_str()).collect())
            .collect();
        let mut buckets: HashMap<&str, Vec<usize>> = HashMap::new();
        for (ri, labs) in r_labels.iter().enumerate() {
            for l in labs {
                buckets.entry(l).or_default().push(ri);
            }
        }
        let dedup = symmetric && pred.is_symmetric();

        for a in left {
            let a_labels: HashSet<&str> = a.support.iter().map(|&i| lvals[i].as_str()).collect();
            let mut seen: HashSet<usize> = HashSet::new();
            for l in &a_labels {
                let Some(cands) = buckets.get(l) else {
                    continue;
                };
                for &ri in cands {
                    if !seen.insert(ri) {
                        continue;
                    }
                    let b = &right[ri];
                    if a.id == b.id {
                        continue;
                    }
                    if dedup && a.id >= b.id {
                        continue;
                    }
                    *examined += 1;
                    if *examined > MAX_PAIRS {
                        return true;
                    }
                    let tri = tri_same(&a_labels, &r_labels[ri]);
                    if opts.certain_only && tri != Tri::True {
                        continue;
                    }
                    let p = p_same(&a.post, lvals, &b.post, rvals);
                    if p + 1e-12 < opts.min_probability {
                        continue;
                    }
                    matches.push(JoinMatch {
                        left: a.id.clone(),
                        right: b.id.clone(),
                        probability: p,
                        certainty: tri,
                    });
                }
            }
        }
        false
    }
}

// --------------------------------------------------- probability (graded)

/// P(A > B) under independent posteriors, midpoints ascending in both.
fn p_gt(a: &[f64], amid: &[f64], b: &[f64], bmid: &[f64]) -> f64 {
    let mut j = 0usize;
    let mut cum = 0.0;
    let mut p = 0.0;
    for i in 0..a.len() {
        while j < b.len() && bmid[j] < amid[i] {
            cum += b[j];
            j += 1;
        }
        p += a[i] * cum;
    }
    p
}

/// P(|A - B| <= tol): sliding window of B mass around each A midpoint.
fn p_approx(a: &[f64], amid: &[f64], b: &[f64], bmid: &[f64], tol: f64) -> f64 {
    let mut lo = 0usize;
    let mut hi = 0usize;
    let mut win = 0.0;
    let mut p = 0.0;
    for i in 0..a.len() {
        let ub = amid[i] + tol;
        let lb = amid[i] - tol;
        while hi < b.len() && bmid[hi] <= ub {
            win += b[hi];
            hi += 1;
        }
        while lo < hi && bmid[lo] < lb {
            win -= b[lo];
            lo += 1;
        }
        p += a[i] * win;
    }
    p
}

/// P(A == B) for categorical slots, matched by value label.
fn p_same(a: &[f64], avals: &[String], b: &[f64], bvals: &[String]) -> f64 {
    let bmap: HashMap<&str, f64> = bvals
        .iter()
        .enumerate()
        .map(|(j, l)| (l.as_str(), b[j]))
        .collect();
    let mut p = 0.0;
    for (i, l) in avals.iter().enumerate() {
        if let Some(bp) = bmap.get(l.as_str()) {
            p += a[i] * bp;
        }
    }
    p
}

// --------------------------------------------- certainty (region-valued)

fn tri_gt(a_min: f64, a_max: f64, b_min: f64, b_max: f64) -> Tri {
    if a_min > b_max {
        Tri::True
    } else if a_max > b_min {
        Tri::Possible
    } else {
        Tri::False
    }
}

fn tri_approx(a_min: f64, a_max: f64, b_min: f64, b_max: f64, tol: f64) -> Tri {
    let farthest = (a_max - b_min).max(b_max - a_min);
    let gap = 0f64.max(a_min - b_max).max(b_min - a_max);
    if farthest <= tol {
        Tri::True
    } else if gap <= tol {
        Tri::Possible
    } else {
        Tri::False
    }
}

fn tri_same(a: &HashSet<&str>, b: &HashSet<&str>) -> Tri {
    let intersect = a.iter().any(|l| b.contains(l));
    if a.len() == 1 && b.len() == 1 && intersect {
        Tri::True
    } else if intersect {
        Tri::Possible
    } else {
        Tri::False
    }
}