venndb 0.7.0

an in-memory database in Rust for rows queried using bit (flag) columns
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
//! Combining row sets into the rows that match a query.

use crate::rowset::{Ones, Repr, RowSet, WORD, select};
use crate::runtime::RuntimeColumn;

/// Start from the smallest set and test its rows one by one
/// as long as it holds less than 1 in `DRIVE_RATIO` rows,
/// combine whole bitmasks otherwise.
const DRIVE_RATIO: usize = 256;

/// The number of random rows tried before filtering all of them.
const RANDOM_TRIES: usize = 8;

/// Trying a random row costs about as much as working out the result for this many rows:
/// as many rows are tried as it takes to stay below the cost of working out all of them.
const GUESS_COST: usize = 1024;

/// The most random rows tried for a query, however large the database.
const GUESSES: usize = 1024;

/// Random rows are only tried if one is expected to meet the query well within
/// the number of tries: after 1 in `GUESS_MARGIN` of them.
const GUESS_MARGIN: f64 = 4.0;

/// A few row sets, of which the first ones are kept without allocating.
#[derive(Debug, Clone, Default)]
pub struct RowSets<'a> {
    first: [Option<&'a RowSet>; 3],
    more: Vec<&'a RowSet>,
}

impl<'a> RowSets<'a> {
    /// Create an empty list of sets.
    #[must_use]
    pub const fn new() -> Self {
        Self {
            first: [None; 3],
            more: Vec::new(),
        }
    }

    /// Add a set, unless it is in the list already.
    pub fn push(&mut self, set: &'a RowSet) {
        if self.iter().any(|known| std::ptr::eq(known, set)) {
            return;
        }
        match self.first.iter_mut().find(|slot| slot.is_none()) {
            Some(slot) => *slot = Some(set),
            None => self.more.push(set),
        }
    }

    /// A list of the given sets, which the caller knows to be all different.
    pub(crate) fn distinct(sets: impl IntoIterator<Item = &'a RowSet>) -> Self {
        let mut sets = sets.into_iter();
        let mut first = [None; 3];
        first.iter_mut().for_each(|slot| *slot = sets.next());
        Self {
            first,
            more: sets.collect(),
        }
    }

    /// Iterate over the sets, in the order they were added.
    pub fn iter(&self) -> impl Iterator<Item = &'a RowSet> + Clone + '_ {
        self.first.iter().flatten().chain(&self.more).copied()
    }
}

impl<'a> FromIterator<&'a RowSet> for RowSets<'a> {
    fn from_iter<I: IntoIterator<Item = &'a RowSet>>(sets: I) -> Self {
        let mut this = Self::new();
        sets.into_iter().for_each(|set| this.push(set));
        this
    }
}

/// A single condition a row has to meet.
#[derive(Debug, Clone)]
pub enum Filter<'a> {
    /// The row is in the set.
    Is(&'a RowSet),
    /// The row is not in the set.
    Not(&'a RowSet),
    /// The row is in at least one of the sets.
    AnyOf(RowSets<'a>),
    /// The row is set, or not set, in the runtime column.
    Runtime(&'a RuntimeColumn, bool),
}

impl Filter<'_> {
    fn contains(&self, row: usize) -> bool {
        match self {
            Self::Is(set) => set.contains(row),
            Self::Not(set) => !set.contains(row),
            Self::AnyOf(sets) => sets.iter().any(|set| set.contains(row)),
            Self::Runtime(column, value) => column.get(row) == *value,
        }
    }

    /// The share of the rows that meet the filter at most, as far as it is known.
    fn share(&self, rows: usize) -> f64 {
        let rows_in = match self {
            Self::Is(set) => set.len(),
            Self::Not(set) => rows.saturating_sub(set.len()),
            Self::AnyOf(sets) => sets.iter().map(RowSet::len).sum(),
            Self::Runtime(..) => rows,
        };
        rows_in.min(rows) as f64 / rows as f64
    }

    /// The work it takes to walk over all rows of the filter, if that is possible.
    fn walk_cost(&self) -> Option<usize> {
        let cost = |set: &RowSet| match set.repr() {
            Repr::Sparse(rows) => rows.len(),
            Repr::Dense(words, ones) => words.len() + ones,
        };
        match self {
            Self::Is(set) => Some(cost(set)),
            Self::AnyOf(sets) => Some(sets.iter().map(cost).sum()),
            Self::Not(_) | Self::Runtime(..) => None,
        }
    }
}

/// The filters of a query: a row matches if it meets all of them.
///
/// A plan has a fixed number of slots, each holding at most one filter,
/// for the filters that can be set more than once. Other filters are added
/// with [`Plan::and`].
#[derive(Debug, Clone)]
pub struct Plan<'a> {
    rows: usize,
    slots: Vec<Option<Filter<'a>>>,
    extra: Vec<Filter<'a>>,
}

impl<'a> Plan<'a> {
    /// Create a plan for a database with the given number of rows.
    ///
    /// # Panics
    ///
    /// Panics if there are more rows than [`RowSet::MAX_ROWS`].
    #[must_use]
    pub fn new(rows: usize, slots: usize) -> Self {
        assert!(
            rows <= RowSet::MAX_ROWS,
            "venndb: a database holds at most u32::MAX rows"
        );
        Self {
            rows,
            slots: vec![None; slots],
            extra: Vec::new(),
        }
    }

    /// Remove all filters.
    pub fn reset(&mut self) {
        self.slots.fill(None);
        self.extra.clear();
    }

    /// Set the filter of a slot, replacing the one it had.
    ///
    /// # Panics
    ///
    /// Panics if the plan has no such slot.
    pub fn set(&mut self, slot: usize, filter: Filter<'a>) {
        self.slots[slot] = Some(filter);
    }

    /// Add a set to the [`Filter::AnyOf`] of a slot.
    ///
    /// Adding `None` adds no rows, but the slot does become a filter.
    /// A filter of another kind in the slot is replaced.
    ///
    /// # Panics
    ///
    /// Panics if the plan has no such slot.
    pub fn or(&mut self, slot: usize, set: Option<&'a RowSet>) {
        if !matches!(self.slots[slot], Some(Filter::AnyOf(_))) {
            self.slots[slot] = Some(Filter::AnyOf(RowSets::new()));
        }
        if let (Some(Filter::AnyOf(sets)), Some(set)) = (&mut self.slots[slot], set) {
            sets.push(set);
        }
    }

    /// Add a filter next to the ones in the slots.
    pub fn and(&mut self, filter: Filter<'a>) {
        self.extra.push(filter);
    }

    fn filters(&self) -> impl Iterator<Item = &Filter<'a>> + Clone {
        self.slots.iter().flatten().chain(&self.extra)
    }

    /// The rows that meet all filters, `None` if there are no such rows.
    #[must_use]
    pub fn execute(&self) -> Option<Matches<'a>> {
        if self.rows == 0 {
            return None;
        }
        let mut driver: Option<(usize, usize)> = None;
        let mut only = None;
        let mut count = 0;
        for (at, filter) in self.filters().enumerate() {
            count += 1;
            only = Some(filter);
            if let Some(cost) = filter.walk_cost() {
                if cost == 0 {
                    return None;
                }
                if driver.is_none_or(|(best, _)| cost < best) {
                    driver = Some((cost, at));
                }
            }
        }
        match (count, only, driver) {
            (0, ..) => Some(Matches(Repr2::All(self.rows))),
            (1, Some(Filter::Is(set)), _) if set.span() <= self.rows => {
                Some(Matches(Repr2::Set(set)))
            }
            (_, _, Some((cost, at))) if cost.saturating_mul(DRIVE_RATIO) <= self.rows => {
                self.walk(at)
            }
            _ => self.combine(),
        }
    }

    /// A random row that meets all filters, `None` if there are no such rows.
    ///
    /// Each of those rows is as likely as any other. A query that many rows meet
    /// is answered by trying random rows, without working out all rows that meet it.
    #[must_use]
    pub fn random(&self) -> Option<usize> {
        match self.guess(|_| true) {
            Guess::Found(row) => Some(row),
            Guess::Nothing => None,
            Guess::Unknown => self.execute().map(|matches| matches.random()),
        }
    }

    /// A random row that meets all filters and for which the predicate returns `true`.
    #[must_use]
    pub fn random_where(&self, mut predicate: impl FnMut(usize) -> bool) -> Option<usize> {
        match self.guess(&mut predicate) {
            Guess::Found(row) => Some(row),
            Guess::Nothing => None,
            Guess::Unknown => self.execute()?.random_where(predicate),
        }
    }

    /// Try random rows of the database, if enough of them are expected to meet the filters.
    fn guess(&self, mut accept: impl FnMut(usize) -> bool) -> Guess {
        if self.rows == 0 || self.filters().any(|filter| filter.walk_cost() == Some(0)) {
            return Guess::Nothing;
        }
        // as if the filters have nothing to do with each other, which is good enough to decide
        let share: f64 = self
            .filters()
            .map(|filter| filter.share(self.rows))
            .product();
        // a small database is worked out in less time than it takes to try a few rows
        let guesses = (self.rows / GUESS_COST).min(GUESSES);
        if share * (guesses as f64) < GUESS_MARGIN {
            return Guess::Unknown;
        }
        for _ in 0..guesses {
            let row = crate::rng::below(self.rows);
            if self.filters().all(|filter| filter.contains(row)) && accept(row) {
                return Guess::Found(row);
            }
        }
        Guess::Unknown
    }

    /// Walk over the rows of one filter, keeping those that meet the others.
    fn walk(&self, driver: usize) -> Option<Matches<'a>> {
        let keep = |row: &usize| {
            *row < self.rows
                && self
                    .filters()
                    .enumerate()
                    .all(|(at, filter)| at == driver || filter.contains(*row))
        };
        let mut found: Vec<u32> = Vec::new();
        match self.filters().nth(driver) {
            Some(Filter::Is(set)) => found.extend(set.iter().filter(keep).map(|row| row as u32)),
            Some(Filter::AnyOf(sets)) => {
                for set in sets.iter() {
                    found.extend(set.iter().filter(keep).map(|row| row as u32));
                }
                if sets.iter().nth(1).is_some() {
                    found.sort_unstable();
                    found.dedup();
                }
            }
            _ => unreachable!("not a filter to walk over"),
        }
        (!found.is_empty()).then_some(Matches(Repr2::Rows(found)))
    }

    /// Combine the filters as bitmasks of all rows.
    fn combine(&self) -> Option<Matches<'a>> {
        let len = self.rows.div_ceil(WORD);
        let start = self
            .filters()
            .enumerate()
            .find_map(|(at, filter)| match filter {
                Filter::Is(set) => match set.repr() {
                    Repr::Dense(words, _) => Some((at, words)),
                    Repr::Sparse(_) => None,
                },
                _ => None,
            });
        let mut mask = match start {
            Some((_, words)) => {
                let mut mask: Vec<u64> = Vec::with_capacity(len);
                mask.extend(words.iter().take(len));
                mask.resize(len, 0);
                mask
            }
            None => vec![u64::MAX; len],
        };
        let mut scratch = Vec::new();
        for (at, filter) in self.filters().enumerate() {
            if start.is_some_and(|(start, _)| start == at) {
                continue;
            }
            match filter {
                Filter::Is(set) => match set.repr() {
                    Repr::Dense(words, _) => and(&mut mask, words.iter().copied()),
                    Repr::Sparse(found) => {
                        scratch.clear();
                        scratch.resize(len, 0);
                        for row in found.iter().map(|row| *row as usize) {
                            if let Some(word) = mask.get(row / WORD) {
                                scratch[row / WORD] |= word & (1 << (row % WORD));
                            }
                        }
                        std::mem::swap(&mut mask, &mut scratch);
                    }
                },
                Filter::Not(set) => match set.repr() {
                    Repr::Dense(words, _) => {
                        for (word, other) in mask.iter_mut().zip(words) {
                            *word &= !other;
                        }
                    }
                    Repr::Sparse(found) => {
                        for row in found.iter().map(|row| *row as usize) {
                            if let Some(word) = mask.get_mut(row / WORD) {
                                *word &= !(1 << (row % WORD));
                            }
                        }
                    }
                },
                Filter::AnyOf(sets) => {
                    scratch.clear();
                    scratch.resize(len, 0);
                    for set in sets.iter() {
                        match set.repr() {
                            Repr::Dense(words, _) => {
                                for (word, other) in scratch.iter_mut().zip(words) {
                                    *word |= other;
                                }
                            }
                            Repr::Sparse(found) => {
                                for row in found.iter().map(|row| *row as usize) {
                                    if let Some(word) = scratch.get_mut(row / WORD) {
                                        *word |= 1 << (row % WORD);
                                    }
                                }
                            }
                        }
                    }
                    and(&mut mask, scratch.iter().copied());
                }
                Filter::Runtime(column, value) => column.filter(&mut mask, *value),
            }
        }
        if !self.rows.is_multiple_of(WORD) {
            mask[len - 1] &= (1 << (self.rows % WORD)) - 1;
        }
        let ones: usize = mask.iter().map(|word| word.count_ones() as usize).sum();
        (ones > 0).then_some(Matches(Repr2::Mask(mask, ones)))
    }
}

/// What trying random rows came to.
#[derive(Debug, PartialEq, Eq)]
enum Guess {
    /// A row that meets the query.
    Found(usize),
    /// No row can meet the query.
    Nothing,
    /// None of the rows tried meets the query, or none was tried.
    Unknown,
}

/// Keep the bits that are also set in the other words, which may be fewer.
fn and(mask: &mut [u64], other: impl ExactSizeIterator<Item = u64>) {
    let known = other.len().min(mask.len());
    for (word, other) in mask.iter_mut().zip(other) {
        *word &= other;
    }
    mask[known..].fill(0);
}

/// The rows that match a query, there is always at least one.
#[derive(Debug, Clone)]
pub struct Matches<'a>(Repr2<'a>);

#[derive(Debug, Clone)]
enum Repr2<'a> {
    /// All rows of a database with this many rows.
    All(usize),
    /// The rows of a set of the database itself.
    Set(&'a RowSet),
    /// The rows, sorted and unique.
    Rows(Vec<u32>),
    /// One bit per row, and the number of ones.
    Mask(Vec<u64>, usize),
}

impl Matches<'_> {
    /// The number of rows, never zero.
    #[expect(clippy::len_without_is_empty, reason = "matches are never empty")]
    #[must_use]
    pub fn len(&self) -> usize {
        match &self.0 {
            Repr2::All(rows) => *rows,
            Repr2::Set(set) => set.len(),
            Repr2::Rows(rows) => rows.len(),
            Repr2::Mask(_, ones) => *ones,
        }
    }

    /// The `n`th row in increasing order, counting from zero.
    #[must_use]
    pub fn nth(&self, n: usize) -> Option<usize> {
        match &self.0 {
            Repr2::All(rows) => (n < *rows).then_some(n),
            Repr2::Set(set) => set.nth(n),
            Repr2::Rows(rows) => rows.get(n).map(|row| *row as usize),
            Repr2::Mask(words, _) => select(words, n),
        }
    }

    /// The first row.
    #[must_use]
    pub fn first(&self) -> usize {
        self.nth(0).expect("matches are never empty")
    }

    /// A random row.
    #[must_use]
    pub fn random(&self) -> usize {
        self.nth(crate::rng::below(self.len()))
            .expect("matches are never empty")
    }

    /// A random row for which the predicate returns `true`.
    ///
    /// Tries a few random rows first, to only test all rows if none of those do.
    #[must_use]
    pub fn random_where(&self, mut predicate: impl FnMut(usize) -> bool) -> Option<usize> {
        for _ in 0..RANDOM_TRIES.min(self.len()) {
            let row = self.random();
            if predicate(row) {
                return Some(row);
            }
        }
        self.filter(predicate).map(|matches| matches.random())
    }

    /// The rows for which the predicate returns `true`, `None` if there are none.
    #[must_use]
    pub fn filter(&self, mut predicate: impl FnMut(usize) -> bool) -> Option<Matches<'static>> {
        let rows: Vec<u32> = self
            .iter()
            .filter(|row| predicate(*row))
            .map(|row| row as u32)
            .collect();
        (!rows.is_empty()).then_some(Matches(Repr2::Rows(rows)))
    }

    /// Iterate over the rows, in increasing order.
    #[must_use]
    pub fn iter(&self) -> MatchesIter<'_> {
        MatchesIter(match &self.0 {
            Repr2::All(rows) => IterRepr::All(0..*rows),
            Repr2::Set(set) => IterRepr::Set(set.iter()),
            Repr2::Rows(rows) => IterRepr::Rows(rows.iter()),
            Repr2::Mask(words, _) => IterRepr::Mask(Ones::new(words)),
        })
    }
}

impl<'a> IntoIterator for &'a Matches<'_> {
    type Item = usize;
    type IntoIter = MatchesIter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

/// An iterator over the rows of [`Matches`].
#[derive(Debug, Clone)]
pub struct MatchesIter<'a>(IterRepr<'a>);

#[derive(Debug, Clone)]
enum IterRepr<'a> {
    All(std::ops::Range<usize>),
    Set(crate::rowset::Iter<'a>),
    Rows(std::slice::Iter<'a, u32>),
    Mask(Ones<'a>),
}

impl Iterator for MatchesIter<'_> {
    type Item = usize;

    fn next(&mut self) -> Option<usize> {
        match &mut self.0 {
            IterRepr::All(rows) => rows.next(),
            IterRepr::Set(rows) => rows.next(),
            IterRepr::Rows(rows) => rows.next().map(|row| *row as usize),
            IterRepr::Mask(ones) => ones.next(),
        }
    }
}

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

    struct Random(u64);

    impl Random {
        fn next(&mut self, limit: usize) -> usize {
            self.0 ^= self.0 << 13;
            self.0 ^= self.0 >> 7;
            self.0 ^= self.0 << 17;
            (self.0 % limit as u64) as usize
        }

        /// A set with about one in `one_in` of the rows.
        fn set(&mut self, rows: usize, one_in: usize) -> RowSet {
            (0..rows).filter(|_| self.next(one_in) == 0).collect()
        }
    }

    fn naive(rows: usize, filters: &[Filter<'_>]) -> Vec<usize> {
        (0..rows)
            .filter(|row| filters.iter().all(|filter| filter.contains(*row)))
            .collect()
    }

    fn found(matches: Option<Matches<'_>>) -> Vec<usize> {
        let Some(matches) = matches else {
            return Vec::new();
        };
        let rows: Vec<usize> = matches.iter().collect();
        assert!(!rows.is_empty(), "matches are never empty");
        assert_eq!(matches.len(), rows.len());
        assert_eq!(matches.first(), rows[0]);
        for (n, row) in rows.iter().enumerate() {
            assert_eq!(matches.nth(n), Some(*row));
        }
        assert_eq!(matches.nth(rows.len()), None);
        assert!(rows.contains(&matches.random()));
        assert!((&matches).into_iter().eq(rows.iter().copied()));
        rows
    }

    #[test]
    fn both_ways_agree_with_a_scan() {
        let mut random = Random(0x1234_5678_9ABC_DEF1);
        for rows in [1, 63, 64, 65, 200, 1000, 5000] {
            let column = RuntimeColumn::new();
            // also rows the database does not have
            for row in 0..rows + 70 {
                column.set(row, random.next(3) == 0);
            }
            let sets: Vec<RowSet> = [1, 2, 3, 10, 50, 100, 400, 1, 7, 300]
                .into_iter()
                .map(|one_in| {
                    let span = rows + random.next(2) * 70;
                    random.set(span, one_in)
                })
                .collect();
            for _ in 0..300 {
                let filters: Vec<Filter<'_>> = (0..1 + random.next(4))
                    .map(|_| {
                        let set = &sets[random.next(sets.len())];
                        match random.next(6) {
                            0 | 1 => Filter::Is(set),
                            2 => Filter::Not(set),
                            3 | 4 => Filter::AnyOf(
                                (0..random.next(4))
                                    .map(|_| &sets[random.next(sets.len())])
                                    .collect(),
                            ),
                            _ => Filter::Runtime(&column, random.next(2) == 0),
                        }
                    })
                    .collect();
                let expected = naive(rows, &filters);
                let mut plan = Plan::new(rows, 0);
                filters.iter().for_each(|filter| plan.and(filter.clone()));
                assert_eq!(found(plan.combine()), expected, "{filters:?}");
                for (at, filter) in filters.iter().enumerate() {
                    if filter.walk_cost().is_some() {
                        assert_eq!(found(plan.walk(at)), expected, "{filters:?}");
                    }
                }
                assert_eq!(found(plan.execute()), expected, "{filters:?}");
                // a random row is one of them, however it is found
                for _ in 0..3 {
                    let row = plan.random();
                    assert_eq!(row.is_some(), !expected.is_empty(), "{filters:?}");
                    assert!(row.is_none_or(|row| expected.contains(&row)), "{filters:?}");
                    let odd = plan.random_where(|row| row % 2 == 1);
                    assert_eq!(odd.is_some(), expected.iter().any(|row| row % 2 == 1));
                    assert!(odd.is_none_or(|row| row % 2 == 1 && expected.contains(&row)));
                }
                match plan.guess(|_| true) {
                    Guess::Found(row) => assert!(expected.contains(&row), "{filters:?}"),
                    Guess::Nothing => assert!(expected.is_empty(), "{filters:?}"),
                    Guess::Unknown => (),
                }
            }
        }
    }

    #[test]
    #[cfg(target_pointer_width = "64")]
    #[should_panic(expected = "at most u32::MAX rows")]
    fn rows_have_to_fit() {
        let _ = Plan::new(RowSet::MAX_ROWS + 1, 0);
    }

    #[test]
    fn random_reaches_every_row() {
        let odd: RowSet = (0..640).filter(|row| row % 2 == 1).collect();
        let few: RowSet = [3, 500, 77].into_iter().collect();
        let column = RuntimeColumn::new();
        let mut plans = [
            Plan::new(8, 0),
            Plan::new(640, 0),
            Plan::new(640, 0),
            Plan::new(640, 0),
        ];
        plans[1].and(Filter::Not(&odd));
        plans[1].and(Filter::Runtime(&column, false));
        plans[2].and(Filter::Is(&few));
        plans[3].and(Filter::Is(&odd));
        plans[3].and(Filter::Not(&few));
        for plan in &plans {
            let matches = plan.execute().unwrap();
            let rows: Vec<usize> = matches.iter().collect();
            let mut seen = std::collections::BTreeSet::new();
            for _ in 0..rows.len() * 40 {
                seen.insert(matches.random());
                let row = matches.random_where(|row| row % 3 != 0).unwrap();
                assert!(row % 3 != 0 && rows.contains(&row));
            }
            assert!(seen.iter().eq(rows.iter()), "{seen:?}");
        }
    }

    #[test]
    fn guesses_when_many_rows_match() {
        // 62 rows are tried for a database of this size
        let rows = 64_000;
        let half: RowSet = (0..rows).filter(|row| row % 2 == 0).collect();
        let few: RowSet = (0..rows).filter(|row| row % 100 == 0).collect();
        let none = RowSet::new();
        let column = RuntimeColumn::new();

        // nothing to filter on, or filters most rows meet: a random row will do
        let mut plan = Plan::new(rows, 0);
        assert!(matches!(plan.guess(|_| true), Guess::Found(_)));
        plan.and(Filter::Is(&half));
        plan.and(Filter::Not(&few));
        plan.and(Filter::Runtime(&column, false));
        let mut seen = std::collections::BTreeSet::new();
        for _ in 0..2000 {
            let Guess::Found(row) = plan.guess(|_| true) else {
                panic!("1 row in 2 meets the filters");
            };
            assert!(row % 2 == 0 && row % 100 != 0);
            seen.insert(row);
            seen.insert(plan.random().unwrap());
        }
        // spread over all of them
        assert!(seen.len() > 1000 && seen.first() < Some(&1000) && seen.last() > Some(&63_000));
        // a row the caller refuses is not returned
        assert_eq!(plan.guess(|_| false), Guess::Unknown);
        assert_eq!(plan.random_where(|_| false), None);
        assert_eq!(plan.random_where(|row| row == 42), Some(42));

        // too few rows meet the filters to hit one well within the tries
        let mut plan = Plan::new(rows, 0);
        plan.and(Filter::Is(&few));
        assert_eq!(plan.guess(|_| true), Guess::Unknown);
        assert!(plan.random().unwrap().is_multiple_of(100));
        // 1 row in 10 is enough for that, 1 in 20 is not
        let tenth: RowSet = (0..rows).filter(|row| row % 10 == 0).collect();
        let mut plan = Plan::new(rows, 0);
        plan.and(Filter::Is(&tenth));
        assert!((0..50).any(|_| matches!(plan.guess(|_| true), Guess::Found(_))));
        plan.and(Filter::Is(&half));
        assert_eq!(plan.guess(|_| true), Guess::Unknown);
        // the more rows, the more are tried: 1 row in 100 will do for a million of them
        let million = 1_000_000;
        let one_in = |n: usize| (0..million).filter(|row| row % n == 0).collect::<RowSet>();
        let (few_of_many, fewer) = (one_in(100), one_in(600));
        let mut plan = Plan::new(million, 0);
        plan.and(Filter::Is(&few_of_many));
        let found = |guess| matches!(guess, Guess::Found(row) if row % 100 == 0);
        assert!((0..20).all(|_| found(plan.guess(|_| true))));
        let mut plan = Plan::new(million, 0);
        plan.and(Filter::Is(&fewer));
        assert_eq!(plan.guess(|_| true), Guess::Unknown);
        // and none at all for a small one, which is worked out in no time
        let mut plan = Plan::new(4000, 0);
        assert_eq!(plan.guess(|_| true), Guess::Unknown);
        plan.and(Filter::Is(&half));
        assert_eq!(plan.guess(|_| true), Guess::Unknown);
        assert!(plan.random().unwrap().is_multiple_of(2));
        assert!(matches!(
            Plan::new(4096, 0).guess(|_| true),
            Guess::Found(_)
        ));

        // no row at all
        let mut plan = Plan::new(rows, 0);
        plan.and(Filter::Is(&none));
        assert_eq!(plan.guess(|_| true), Guess::Nothing);
        assert_eq!((plan.random(), plan.random_where(|_| true)), (None, None));
        assert_eq!(Plan::new(0, 0).guess(|_| true), Guess::Nothing);
        // rows beyond those of the database are never guessed
        let beyond: RowSet = (0..rows * 2).collect();
        let mut plan = Plan::new(rows / 2, 0);
        plan.and(Filter::Is(&beyond));
        assert!(
            (0..200).all(|_| matches!(plan.guess(|_| true), Guess::Found(row) if row < rows / 2))
        );
    }

    #[test]
    fn many_distinct_sets() {
        let sets: Vec<RowSet> = (0..10).map(|row| [row].into_iter().collect()).collect();
        let list = RowSets::distinct(&sets);
        assert!(list.iter().eq(sets.iter()));
        assert_eq!(RowSets::distinct(&sets[..2]).iter().count(), 2);
        let mut list: RowSets<'_> = sets.iter().chain(&sets).collect();
        assert!(list.iter().eq(sets.iter()));
        list.push(&sets[9]);
        assert_eq!(list.iter().count(), 10);
    }

    #[test]
    fn no_filters_match_all_rows() {
        assert!(Plan::new(0, 0).execute().is_none());
        assert_eq!(found(Plan::new(3, 2).execute()), [0, 1, 2]);
    }

    #[test]
    fn a_single_set_is_borrowed() {
        let set: RowSet = [1, 5].into_iter().collect();
        let mut plan = Plan::new(6, 0);
        plan.and(Filter::Is(&set));
        let matches = plan.execute().unwrap();
        assert!(matches!(matches.0, Repr2::Set(_)));
        assert_eq!(found(Some(matches)), [1, 5]);
        // a set with rows the database does not have is not used as is
        let mut plan = Plan::new(5, 0);
        plan.and(Filter::Is(&set));
        assert_eq!(found(plan.execute()), [1]);
    }

    #[test]
    fn slots() {
        let (a, b, empty): (RowSet, RowSet, RowSet) = (
            [0, 1].into_iter().collect(),
            [1, 2].into_iter().collect(),
            RowSet::new(),
        );
        let mut plan = Plan::new(4, 2);
        plan.set(0, Filter::Is(&a));
        plan.set(0, Filter::Not(&a));
        assert_eq!(found(plan.execute()), [2, 3]);
        plan.or(0, Some(&a));
        plan.or(0, Some(&a));
        assert!(matches!(&plan.slots[0], Some(Filter::AnyOf(sets)) if sets.iter().count() == 1));
        plan.or(0, Some(&b));
        plan.or(0, None);
        assert_eq!(found(plan.execute()), [0, 1, 2]);
        plan.or(1, Some(&b));
        assert_eq!(found(plan.execute()), [1, 2]);
        plan.and(Filter::Not(&a));
        assert_eq!(found(plan.execute()), [2]);
        plan.reset();
        assert_eq!(found(plan.execute()), [0, 1, 2, 3]);
        plan.or(1, None);
        assert_eq!(found(plan.execute()), []);
        plan.reset();
        plan.and(Filter::Is(&empty));
        plan.and(Filter::Not(&a));
        assert_eq!(found(plan.execute()), []);
    }

    #[test]
    fn small_sets_are_walked() {
        let rows = DRIVE_RATIO * 40;
        let small: RowSet = (0..40).map(|row| row * 200).collect();
        let big: RowSet = (0..rows).filter(|row| row % 2 == 0).collect();
        let mut plan = Plan::new(rows, 0);
        plan.and(Filter::Is(&big));
        plan.and(Filter::Is(&small));
        let matches = plan.execute().unwrap();
        assert!(matches!(matches.0, Repr2::Rows(_)));
        assert!(matches.iter().eq((0..40).map(|row| row * 200)));
        // one row too many to walk
        let small: RowSet = (0..41).map(|row| row * 200).collect();
        let mut plan = Plan::new(rows, 0);
        plan.and(Filter::Is(&big));
        plan.and(Filter::Is(&small));
        let matches = plan.execute().unwrap();
        assert!(matches!(matches.0, Repr2::Mask(..)));
        assert!(matches.iter().eq((0..41).map(|row| row * 200)));
    }

    #[test]
    fn filter_and_random_where() {
        let mut plan = Plan::new(100, 0);
        let odd: RowSet = (0..100).filter(|row| row % 2 == 1).collect();
        plan.and(Filter::Not(&odd));
        let matches = plan.execute().unwrap();
        assert_eq!(matches.len(), 50);
        let filtered = matches.filter(|row| row >= 90).unwrap();
        assert_eq!(found(Some(filtered)), [90, 92, 94, 96, 98]);
        assert!(matches.filter(|row| row > 98).is_none());

        for _ in 0..50 {
            assert_eq!(matches.random_where(|row| row == 42), Some(42));
            assert!(matches.random_where(|row| row < 10).unwrap() < 10);
        }
        assert_eq!(matches.random_where(|_| false), None);
        // a single row is not tested more often than needed
        let one = Plan::new(1, 0).execute().unwrap();
        let mut calls = 0;
        assert_eq!(
            one.random_where(|_| {
                calls += 1;
                false
            }),
            None
        );
        assert_eq!(calls, 2);
    }
}