sochdb-query 2.0.6

SochDB query engine (sync-first execution and vector query planning)
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
// SPDX-License-Identifier: AGPL-3.0-or-later
// SochDB - LLM-Optimized Embedded Database
// Copyright (C) 2026 Sushanth Reddy Vanagala (https://github.com/sushanthpy)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Unified Candidate Gate Interface (Task 4)
//!
//! This module defines the `AllowedSet` abstraction that every retrieval
//! executor MUST accept. The gate guarantees:
//!
//! 1. **Never return a doc outside AllowedSet** - structural enforcement
//! 2. **Apply constraints during generation** - no post-filtering
//! 3. **Consistent semantics** across vector/BM25/hybrid/context
//!
//! ## The Contract
//!
//! Every executor receives an `AllowedSet` and must:
//! - Check membership BEFORE including any candidate
//! - Short-circuit if AllowedSet is empty (return empty results)
//! - Report selectivity for query planning
//!
//! ## Representations
//!
//! `AllowedSet` supports multiple representations for efficiency:
//!
//! | Representation | Best For | Membership | Space |
//! |----------------|----------|------------|-------|
//! | Bitmap | Dense sets | O(1) | O(N/8) |
//! | SortedVec | Sparse sets | O(log n) | O(n) |
//! | HashSet | Random access | O(1) avg | O(n) |
//! | All | No constraint | O(1) | O(1) |
//!
//! ## Selectivity
//!
//! Executors use selectivity `|S|/N` to choose execution strategy:
//! - High selectivity (> 0.1): Standard search with filter
//! - Low selectivity (< 0.01): Scan only allowed IDs
//! - Very low (< 0.001): Consider alternative strategy

use std::collections::HashSet;
use std::fmt;
use std::sync::Arc;

// ============================================================================
// AllowedSet - Core Abstraction
// ============================================================================

/// The unified gate for candidate filtering
///
/// Every executor MUST check `allowed_set.contains(doc_id)` before
/// including any result. This is the structural enforcement of pushdown.
#[derive(Clone)]
pub enum AllowedSet {
    /// All documents are allowed (no filter constraint)
    All,

    /// Bitmap representation (efficient for dense sets)
    Bitmap(Arc<AllowedBitmap>),

    /// Sorted vector (efficient for sparse sets with iteration)
    SortedVec(Arc<Vec<u64>>),

    /// Hash set (efficient for random access)
    HashSet(Arc<HashSet<u64>>),

    /// No documents allowed (empty result shortcut)
    None,
}

impl AllowedSet {
    /// Create an AllowedSet from a bitmap
    pub fn from_bitmap(bitmap: AllowedBitmap) -> Self {
        if bitmap.is_empty() {
            Self::None
        } else if bitmap.is_all() {
            Self::All
        } else {
            Self::Bitmap(Arc::new(bitmap))
        }
    }

    /// Create an AllowedSet from a sorted vector of doc IDs
    pub fn from_sorted_vec(mut ids: Vec<u64>) -> Self {
        if ids.is_empty() {
            return Self::None;
        }
        ids.sort_unstable();
        ids.dedup();
        Self::SortedVec(Arc::new(ids))
    }

    /// Create an AllowedSet from an iterator of doc IDs
    pub fn from_iter(ids: impl IntoIterator<Item = u64>) -> Self {
        let set: HashSet<u64> = ids.into_iter().collect();
        if set.is_empty() {
            Self::None
        } else {
            Self::HashSet(Arc::new(set))
        }
    }

    /// Check if a document ID is allowed
    ///
    /// This is the core operation that executors MUST call.
    #[inline]
    pub fn contains(&self, doc_id: u64) -> bool {
        match self {
            Self::All => true,
            Self::Bitmap(bm) => bm.contains(doc_id),
            Self::SortedVec(vec) => vec.binary_search(&doc_id).is_ok(),
            Self::HashSet(set) => set.contains(&doc_id),
            Self::None => false,
        }
    }

    /// Check if this set is empty (no allowed documents)
    pub fn is_empty(&self) -> bool {
        matches!(self, Self::None)
    }

    /// Check if this set allows all documents
    pub fn is_all(&self) -> bool {
        matches!(self, Self::All)
    }

    /// Get the cardinality (number of allowed documents)
    ///
    /// Returns None for All (unknown without universe size)
    pub fn cardinality(&self) -> Option<usize> {
        match self {
            Self::All => None,
            Self::Bitmap(bm) => Some(bm.count()),
            Self::SortedVec(vec) => Some(vec.len()),
            Self::HashSet(set) => Some(set.len()),
            Self::None => Some(0),
        }
    }

    /// Compute selectivity against a universe of size N
    ///
    /// Returns |S| / N, the fraction of allowed documents
    pub fn selectivity(&self, universe_size: usize) -> f64 {
        if universe_size == 0 {
            return 0.0;
        }
        match self {
            Self::All => 1.0,
            Self::None => 0.0,
            other => other
                .cardinality()
                .map(|c| c as f64 / universe_size as f64)
                .unwrap_or(1.0),
        }
    }

    /// Intersect with another AllowedSet
    pub fn intersect(&self, other: &AllowedSet) -> AllowedSet {
        match (self, other) {
            // Identity cases
            (Self::All, x) | (x, Self::All) => x.clone(),
            (Self::None, _) | (_, Self::None) => Self::None,

            // Both are sets - compute intersection
            (Self::SortedVec(a), Self::SortedVec(b)) => {
                let result = sorted_vec_intersect(a, b);
                Self::from_sorted_vec(result)
            }
            (Self::HashSet(a), Self::HashSet(b)) => {
                let result: HashSet<_> = a.intersection(b).copied().collect();
                if result.is_empty() {
                    Self::None
                } else {
                    Self::HashSet(Arc::new(result))
                }
            }
            (Self::Bitmap(a), Self::Bitmap(b)) => {
                let result = a.intersect(b);
                Self::from_bitmap(result)
            }

            // Mixed - convert to hash set
            (a, b) => {
                let set_a: HashSet<u64> = a.iter().collect();
                let set_b: HashSet<u64> = b.iter().collect();
                let result: HashSet<_> = set_a.intersection(&set_b).copied().collect();
                if result.is_empty() {
                    Self::None
                } else {
                    Self::HashSet(Arc::new(result))
                }
            }
        }
    }

    /// Union with another AllowedSet
    pub fn union(&self, other: &AllowedSet) -> AllowedSet {
        match (self, other) {
            (Self::All, _) | (_, Self::All) => Self::All,
            (Self::None, x) | (x, Self::None) => x.clone(),

            (Self::HashSet(a), Self::HashSet(b)) => {
                let result: HashSet<_> = a.union(b).copied().collect();
                Self::HashSet(Arc::new(result))
            }

            // Mixed - convert to hash set
            (a, b) => {
                let mut result: HashSet<u64> = a.iter().collect();
                result.extend(b.iter());
                Self::HashSet(Arc::new(result))
            }
        }
    }

    /// Iterate over allowed document IDs
    ///
    /// Note: For All, this returns an empty iterator (unknown universe)
    pub fn iter(&self) -> AllowedSetIter<'_> {
        match self {
            Self::All => AllowedSetIter::Empty,
            Self::Bitmap(bm) => AllowedSetIter::Bitmap(bm.iter()),
            Self::SortedVec(vec) => AllowedSetIter::SortedVec(vec.iter()),
            Self::HashSet(set) => AllowedSetIter::HashSet(set.iter()),
            Self::None => AllowedSetIter::Empty,
        }
    }

    /// Convert to a Vec (for small sets)
    pub fn to_vec(&self) -> Vec<u64> {
        self.iter().collect()
    }
}

impl fmt::Debug for AllowedSet {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::All => write!(f, "AllowedSet::All"),
            Self::None => write!(f, "AllowedSet::None"),
            Self::Bitmap(bm) => write!(f, "AllowedSet::Bitmap(count={})", bm.count()),
            Self::SortedVec(vec) => write!(f, "AllowedSet::SortedVec(len={})", vec.len()),
            Self::HashSet(set) => write!(f, "AllowedSet::HashSet(len={})", set.len()),
        }
    }
}

impl Default for AllowedSet {
    fn default() -> Self {
        Self::All
    }
}

// Helper for sorted vec intersection
fn sorted_vec_intersect(a: &[u64], b: &[u64]) -> Vec<u64> {
    let mut result = Vec::with_capacity(a.len().min(b.len()));
    let mut i = 0;
    let mut j = 0;

    while i < a.len() && j < b.len() {
        match a[i].cmp(&b[j]) {
            std::cmp::Ordering::Less => i += 1,
            std::cmp::Ordering::Greater => j += 1,
            std::cmp::Ordering::Equal => {
                result.push(a[i]);
                i += 1;
                j += 1;
            }
        }
    }

    result
}

// ============================================================================
// AllowedSet Iterator
// ============================================================================

/// Iterator over allowed document IDs
pub enum AllowedSetIter<'a> {
    Empty,
    Bitmap(BitmapIter<'a>),
    SortedVec(std::slice::Iter<'a, u64>),
    HashSet(std::collections::hash_set::Iter<'a, u64>),
}

impl<'a> Iterator for AllowedSetIter<'a> {
    type Item = u64;

    fn next(&mut self) -> Option<Self::Item> {
        match self {
            Self::Empty => None,
            Self::Bitmap(iter) => iter.next(),
            Self::SortedVec(iter) => iter.next().copied(),
            Self::HashSet(iter) => iter.next().copied(),
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        match self {
            Self::Empty => (0, Some(0)),
            Self::Bitmap(iter) => iter.size_hint(),
            Self::SortedVec(iter) => iter.size_hint(),
            Self::HashSet(iter) => iter.size_hint(),
        }
    }
}

// ============================================================================
// Bitmap Implementation
// ============================================================================

/// Simple bitmap for allowed document IDs
///
/// This is a basic implementation. For production, consider using
/// the `roaring` crate for compressed bitmaps.
pub struct AllowedBitmap {
    /// Bits stored as u64 words
    words: Vec<u64>,
    /// Total number of set bits (cached)
    count: usize,
    /// Whether this represents "all" (complement mode)
    all: bool,
}

impl AllowedBitmap {
    /// Create a new empty bitmap
    pub fn new() -> Self {
        Self {
            words: Vec::new(),
            count: 0,
            all: false,
        }
    }

    /// Create a bitmap with all bits set up to max_id
    pub fn all(max_id: u64) -> Self {
        let word_count = (max_id as usize / 64) + 1;
        Self {
            words: vec![u64::MAX; word_count],
            count: max_id as usize + 1,
            all: true,
        }
    }

    /// Create a bitmap from a set of IDs
    pub fn from_ids(ids: &[u64]) -> Self {
        if ids.is_empty() {
            return Self::new();
        }

        let max_id = *ids.iter().max().unwrap();
        let word_count = (max_id as usize / 64) + 1;
        let mut words = vec![0u64; word_count];

        for &id in ids {
            let word_idx = id as usize / 64;
            let bit_idx = id % 64;
            words[word_idx] |= 1 << bit_idx;
        }

        Self {
            words,
            count: ids.len(),
            all: false,
        }
    }

    /// Set a bit
    pub fn set(&mut self, id: u64) {
        let word_idx = id as usize / 64;
        let bit_idx = id % 64;

        // Extend if necessary
        if word_idx >= self.words.len() {
            self.words.resize(word_idx + 1, 0);
        }

        let old = self.words[word_idx];
        self.words[word_idx] |= 1 << bit_idx;
        if old != self.words[word_idx] {
            self.count += 1;
        }
    }

    /// Clear a bit
    pub fn clear(&mut self, id: u64) {
        let word_idx = id as usize / 64;
        if word_idx >= self.words.len() {
            return;
        }

        let bit_idx = id % 64;
        let old = self.words[word_idx];
        self.words[word_idx] &= !(1 << bit_idx);
        if old != self.words[word_idx] {
            self.count -= 1;
        }
    }

    /// Check if a bit is set
    #[inline]
    pub fn contains(&self, id: u64) -> bool {
        let word_idx = id as usize / 64;
        if word_idx >= self.words.len() {
            return false;
        }
        let bit_idx = id % 64;
        (self.words[word_idx] & (1 << bit_idx)) != 0
    }

    /// Get the count of set bits
    pub fn count(&self) -> usize {
        self.count
    }

    /// Check if empty
    pub fn is_empty(&self) -> bool {
        self.count == 0
    }

    /// Check if all bits are set
    pub fn is_all(&self) -> bool {
        self.all
    }

    /// Intersect with another bitmap
    pub fn intersect(&self, other: &AllowedBitmap) -> AllowedBitmap {
        let min_len = self.words.len().min(other.words.len());
        let mut words = Vec::with_capacity(min_len);
        let mut count = 0;

        for i in 0..min_len {
            let word = self.words[i] & other.words[i];
            count += word.count_ones() as usize;
            words.push(word);
        }

        AllowedBitmap {
            words,
            count,
            all: false,
        }
    }

    /// Union with another bitmap
    pub fn union(&self, other: &AllowedBitmap) -> AllowedBitmap {
        let max_len = self.words.len().max(other.words.len());
        let mut words = Vec::with_capacity(max_len);
        let mut count = 0;

        for i in 0..max_len {
            let a = self.words.get(i).copied().unwrap_or(0);
            let b = other.words.get(i).copied().unwrap_or(0);
            let word = a | b;
            count += word.count_ones() as usize;
            words.push(word);
        }

        AllowedBitmap {
            words,
            count,
            all: false,
        }
    }

    /// Iterate over set bit positions
    pub fn iter(&self) -> BitmapIter<'_> {
        BitmapIter {
            words: &self.words,
            word_idx: 0,
            bit_offset: 0,
            remaining: self.count,
        }
    }
}

impl Default for AllowedBitmap {
    fn default() -> Self {
        Self::new()
    }
}

/// Iterator over set bits in a bitmap
pub struct BitmapIter<'a> {
    words: &'a [u64],
    word_idx: usize,
    bit_offset: u64,
    remaining: usize,
}

impl<'a> Iterator for BitmapIter<'a> {
    type Item = u64;

    fn next(&mut self) -> Option<Self::Item> {
        if self.remaining == 0 {
            return None;
        }

        while self.word_idx < self.words.len() {
            let word = self.words[self.word_idx];
            let masked = word >> self.bit_offset;

            if masked != 0 {
                let trailing = masked.trailing_zeros() as u64;
                let bit_pos = self.bit_offset + trailing;
                self.bit_offset = bit_pos + 1;

                if self.bit_offset >= 64 {
                    self.bit_offset = 0;
                    self.word_idx += 1;
                }

                self.remaining -= 1;
                return Some(
                    self.word_idx as u64 * 64 + bit_pos
                        - (if self.bit_offset == 0 { 64 } else { 0 })
                        + (if bit_pos >= 64 { 0 } else { bit_pos }),
                );
            }

            self.word_idx += 1;
            self.bit_offset = 0;
        }

        None
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.remaining, Some(self.remaining))
    }
}

// Fix the iterator - simpler implementation
impl<'a> BitmapIter<'a> {
    #[allow(dead_code)]
    fn new(words: &'a [u64], count: usize) -> Self {
        Self {
            words,
            word_idx: 0,
            bit_offset: 0,
            remaining: count,
        }
    }
}

// Simple correct iterator implementation
impl AllowedBitmap {
    /// Iterate over set bit positions (simple implementation)
    pub fn iter_simple(&self) -> impl Iterator<Item = u64> + '_ {
        self.words.iter().enumerate().flat_map(|(word_idx, &word)| {
            (0..64).filter_map(move |bit| {
                if (word & (1 << bit)) != 0 {
                    Some(word_idx as u64 * 64 + bit as u64)
                } else {
                    None
                }
            })
        })
    }
}

// ============================================================================
// Candidate Gate Trait
// ============================================================================

/// The candidate gate trait that all executors must implement
///
/// This trait ensures every retrieval path respects the AllowedSet.
pub trait CandidateGate {
    /// The query type
    type Query;

    /// The result type  
    type Result;

    /// The error type
    type Error;

    /// Execute with a mandatory allowed set
    ///
    /// # Contract
    ///
    /// - MUST NOT return any result with doc_id not in allowed_set
    /// - SHOULD short-circuit if allowed_set is empty
    /// - SHOULD use selectivity to choose execution strategy
    fn execute_with_gate(
        &self,
        query: &Self::Query,
        allowed_set: &AllowedSet,
    ) -> Result<Self::Result, Self::Error>;

    /// Get the execution strategy for a given selectivity
    fn strategy_for_selectivity(&self, selectivity: f64) -> ExecutionStrategy {
        if selectivity >= 0.1 {
            ExecutionStrategy::FilterDuringSearch
        } else if selectivity >= 0.001 {
            ExecutionStrategy::ScanAllowedIds
        } else {
            ExecutionStrategy::LinearScan
        }
    }
}

/// Execution strategy based on selectivity
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExecutionStrategy {
    /// Standard search with filter check during traversal
    FilterDuringSearch,

    /// Iterate over allowed IDs and compute distances
    ScanAllowedIds,

    /// Fall back to linear scan (very low selectivity)
    LinearScan,

    /// Refuse to execute (too expensive)
    Reject,
}

// ============================================================================
// Tests
// ============================================================================

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

    #[test]
    fn test_allowed_set_contains() {
        // All
        let all = AllowedSet::All;
        assert!(all.contains(0));
        assert!(all.contains(1000000));

        // None
        let none = AllowedSet::None;
        assert!(!none.contains(0));

        // SortedVec
        let vec = AllowedSet::from_sorted_vec(vec![1, 3, 5, 7, 9]);
        assert!(vec.contains(1));
        assert!(vec.contains(5));
        assert!(!vec.contains(2));
        assert!(!vec.contains(10));

        // HashSet
        let set = AllowedSet::from_iter([1, 3, 5, 7, 9]);
        assert!(set.contains(1));
        assert!(set.contains(5));
        assert!(!set.contains(2));
    }

    #[test]
    fn test_allowed_set_selectivity() {
        let set = AllowedSet::from_sorted_vec(vec![1, 2, 3, 4, 5]);

        assert_eq!(set.selectivity(100), 0.05);
        assert_eq!(set.selectivity(10), 0.5);

        assert_eq!(AllowedSet::All.selectivity(100), 1.0);
        assert_eq!(AllowedSet::None.selectivity(100), 0.0);
    }

    #[test]
    fn test_allowed_set_intersection() {
        let a = AllowedSet::from_sorted_vec(vec![1, 2, 3, 4, 5]);
        let b = AllowedSet::from_sorted_vec(vec![3, 4, 5, 6, 7]);

        let c = a.intersect(&b);
        assert_eq!(c.cardinality(), Some(3));
        assert!(c.contains(3));
        assert!(c.contains(4));
        assert!(c.contains(5));
        assert!(!c.contains(1));
        assert!(!c.contains(7));
    }

    #[test]
    fn test_bitmap_basic() {
        let mut bm = AllowedBitmap::new();
        bm.set(0);
        bm.set(5);
        bm.set(64);
        bm.set(100);

        assert!(bm.contains(0));
        assert!(bm.contains(5));
        assert!(bm.contains(64));
        assert!(bm.contains(100));
        assert!(!bm.contains(1));
        assert!(!bm.contains(63));

        assert_eq!(bm.count(), 4);
    }

    #[test]
    fn test_bitmap_from_ids() {
        let ids = vec![1, 5, 10, 100, 1000];
        let bm = AllowedBitmap::from_ids(&ids);

        for &id in &ids {
            assert!(bm.contains(id));
        }
        assert!(!bm.contains(0));
        assert!(!bm.contains(50));
    }

    #[test]
    fn test_bitmap_intersection() {
        let a = AllowedBitmap::from_ids(&[1, 2, 3, 4, 5]);
        let b = AllowedBitmap::from_ids(&[3, 4, 5, 6, 7]);

        let c = a.intersect(&b);
        assert_eq!(c.count(), 3);
        assert!(c.contains(3));
        assert!(c.contains(4));
        assert!(c.contains(5));
    }

    #[test]
    fn test_execution_strategy() {
        struct DummyGate;
        impl CandidateGate for DummyGate {
            type Query = ();
            type Result = ();
            type Error = ();
            fn execute_with_gate(&self, _: &(), _: &AllowedSet) -> Result<(), ()> {
                Ok(())
            }
        }

        let gate = DummyGate;
        assert_eq!(
            gate.strategy_for_selectivity(0.5),
            ExecutionStrategy::FilterDuringSearch
        );
        assert_eq!(
            gate.strategy_for_selectivity(0.01),
            ExecutionStrategy::ScanAllowedIds
        );
        assert_eq!(
            gate.strategy_for_selectivity(0.0001),
            ExecutionStrategy::LinearScan
        );
    }
}