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
//! [`FilterStrategy`] — how an index applies a [`Filter`], and the selector
//! that chooses it.
//!
//! [`FilterStrategy`] names the four shapes; [`StrategySelector`] and the
//! Tier-1 [`choose_strategy`] resolve a concrete `PreFilter` / `PostFilter`
//! from a selectivity estimate. `PreFilter` and `PostFilter` are realised by
//! [`crate::FilterEvaluator::prefilter`] / [`crate::FilterEvaluator::postfilter`];
//! `InFilter` (graph-traversal pushdown) remains reserved for a future
//! approximate-index consumer (see `dev/ROADMAP.md`).
//!
//! [`Filter`]: iqdb_types::Filter
/// How an index plans to apply a metadata [`Filter`](iqdb_types::Filter)
/// relative to its distance scan.
///
/// `#[non_exhaustive]` because selection logic will add variants (and likely
/// adapter parameters) as approximate indexes start honouring filters.
/// Callers should not exhaustively match on this enum and should not rely on
/// the variant set being closed.
///
/// # Variants in plain terms
///
/// - [`FilterStrategy::PreFilter`] — apply the predicate **before** the
/// distance computation; only matching candidates enter the scan. Cheap
/// when the predicate is selective, wasteful when it is broad.
/// - [`FilterStrategy::PostFilter`] — run the distance scan over every
/// candidate, then drop hits that fail the predicate. Cheap when the
/// predicate is broad, defeats top-`k` truncation when the predicate is
/// selective (you may have to scan far past `k` to refill the result set).
/// - [`FilterStrategy::InFilter`] — interleave predicate evaluation with the
/// distance walk so a graph index can prune branches it knows can't
/// produce surviving candidates. Requires `MetadataIndex` co-design.
/// - [`FilterStrategy::Auto`] — let the index pick from the above based on
/// estimated selectivity. Requires the selectivity machinery that doesn't
/// exist yet; documented here so future configs can name it.
///
/// # Examples
///
/// ```
/// use iqdb_filter::FilterStrategy;
///
/// let chosen = FilterStrategy::PreFilter;
/// // Callers do not branch on this yet; the enum is vocabulary for later.
/// assert_ne!(chosen, FilterStrategy::PostFilter);
/// ```
/// Default cutoff [`StrategySelector`] uses to split `PreFilter` from
/// `PostFilter`: a filter whose estimated selectivity is at or below this value
/// is treated as narrow enough to pre-filter.
///
/// `0.5` means "pre-filter when the predicate is expected to drop at least half
/// the corpus". It is a deliberately neutral default; tune it for a specific
/// index with [`StrategySelector::with_prefilter_threshold`].
pub const DEFAULT_PREFILTER_THRESHOLD: f64 = 0.5;
/// Picks a concrete [`FilterStrategy`] for a validated filter from its
/// estimated selectivity — the Tier-2, tunable counterpart to
/// [`choose_strategy`].
///
/// The rule is simple and monotone: a **narrow** predicate (low
/// [`estimate_selectivity`](crate::estimate_selectivity), at or below the
/// threshold) resolves to [`FilterStrategy::PreFilter`], because evaluating it
/// up front skips the distance computation for the rows it rejects; a **broad**
/// predicate resolves to [`FilterStrategy::PostFilter`], because pre-filtering
/// would materialise nearly the whole corpus for little gain. The selector
/// never returns [`FilterStrategy::Auto`] (it is the thing that resolves it) or
/// [`FilterStrategy::InFilter`] (which needs graph-traversal co-design).
///
/// The type is immutable: [`with_prefilter_threshold`](Self::with_prefilter_threshold)
/// returns a new selector rather than mutating in place.
///
/// # Examples
///
/// ```
/// use iqdb_filter::{FilterEvaluator, FilterStrategy, StrategySelector};
/// use iqdb_types::{Filter, Value};
///
/// # fn main() -> iqdb_types::Result<()> {
/// let selector = StrategySelector::new().with_prefilter_threshold(0.3);
///
/// let narrow = FilterEvaluator::new(Filter::eq("id", Value::Int(7)))?;
/// let broad = FilterEvaluator::new(Filter::neq("id", Value::Int(7)))?;
///
/// assert_eq!(selector.choose(&narrow), FilterStrategy::PreFilter);
/// assert_eq!(selector.choose(&broad), FilterStrategy::PostFilter);
/// # Ok(())
/// # }
/// ```
/// Picks a concrete [`FilterStrategy`] for a validated filter using the
/// [`DEFAULT_PREFILTER_THRESHOLD`] — the Tier-1 shortcut for
/// [`StrategySelector::new().choose(..)`](StrategySelector::choose).
///
/// Returns [`FilterStrategy::PreFilter`] for narrow predicates and
/// [`FilterStrategy::PostFilter`] for broad ones; never `Auto` or `InFilter`.
///
/// # Examples
///
/// ```
/// use iqdb_filter::{FilterEvaluator, FilterStrategy, choose_strategy};
/// use iqdb_types::{Filter, Value};
///
/// # fn main() -> iqdb_types::Result<()> {
/// let evaluator = FilterEvaluator::new(Filter::eq("k", Value::Int(1)))?;
/// assert_eq!(choose_strategy(&evaluator), FilterStrategy::PreFilter);
/// # Ok(())
/// # }
/// ```