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
use crate::document::value::Value;
use std::cmp::Ordering;
use std::collections::HashSet; // Added for cleaner FTS logic
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum Operator {
Eq,
Ne,
Gt,
Gte,
Lt,
Lte,
Match, // Full word matching
MatchPrefix, // Word-prefix matching (autocomplete; works on the FTS index)
Contains, // Substring matching
StartsWith, // Prefix matching
In,
ArrayContains,
ArrayContainsAny,
NotIn,
/// KNN marker (appended last: `as u8` discriminants of older variants
/// must stay stable for the plan-cache key). The query vector rides
/// `Filter.value` as `Value::Binary` (LE f32s); k rides the query
/// limit. Never a boolean predicate — only the VectorKnn scan
/// evaluates it; every other path treats it as unsatisfiable
/// (fail-closed: KNN without its scan returns nothing, not everything).
Near,
/// Graph-traversal marker (appended last: same discriminant rule).
/// `Filter.field` is the anchor collection; `Filter.value` is a
/// `Value::Map` (`id` anchor id, `dir` 0/1/2, `edge` optional field,
/// `depth` bound). Same contract as Near: only the Traverse scan
/// evaluates it, every other path answers false (fail-closed).
Traverse,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum FilterLogic {
And,
Or,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct FilterGroup {
pub logic: FilterLogic,
pub filters: Vec<Filter>,
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Filter {
pub field: String,
pub op: Operator,
pub value: Value,
}
pub fn compare_values(a: &Value, op: &Operator, b: &Value) -> bool {
// Handle the 'In' operator first because it breaks the standard (a, b) pairing logic
// (a is the field value, b is the array of allowed values)
match op {
Operator::In => {
return if let Value::Array(allowed) = b {
// allowed.contains(a)
allowed.iter().any(|item| compare_values(a, &Operator::Eq, item))
} else {
false
};
}
Operator::NotIn => {
return if let Value::Array(allowed) = b {
// !allowed.contains(a)
!allowed.iter().any(|item| compare_values(a, &Operator::Eq, item))
} else {
true
};
}
Operator::ArrayContains => {
return if let Value::Array(items) = a {
items.contains(b)
} else {
false
};
}
Operator::ArrayContainsAny => {
return if let (Value::Array(items), Value::Array(query_items)) = (a, b) {
query_items.iter().any(|qi| items.contains(qi))
} else {
false
};
}
_ => {} // Fall through to standard comparisons
}
// 2. Strict type equality (Fast Path)
if matches!(op, Operator::Eq) && a == b { return true; }
match (a, b) {
// String-specific logic for FTS and standard comparisons
// (Value::String(s), Value::Int(i)) => {
// if let Ok(parsed) = s.parse::<i64>() {
// return eval_ordering(parsed.cmp(i), op);
// }
// eval_ordering(a.cmp(b), op)
// },
// (Value::Int(i), Value::String(s)) => {
// if let Ok(parsed) = s.parse::<i64>() {
// return eval_ordering(i.cmp(&parsed), op);
// }
// eval_ordering(a.cmp(b), op)
// },
// (Value::String(s), Value::Float(f)) => {
// if let Ok(parsed) = s.parse::<f64>() {
// return eval_ordering(parsed.total_cmp(f), op);
// }
// eval_ordering(a.cmp(b), op)
// },
(Value::String(s), Value::Int(i)) | (Value::Int(i), Value::String(s)) => {
if let Ok(parsed) = s.parse::<i64>() {
return eval_ordering(parsed.cmp(i), op);
}
// If not a valid number string, return false for Eq, true for Ne
if matches!(op, Operator::Eq) { return false; }
if matches!(op, Operator::Ne) { return true; }
eval_ordering(a.cmp(b), op)
},
(Value::String(s), Value::Float(f)) | (Value::Float(f), Value::String(s)) => {
if let Ok(parsed) = s.parse::<f64>() {
return eval_ordering(parsed.total_cmp(f), op);
}
if matches!(op, Operator::Eq) { return false; }
if matches!(op, Operator::Ne) { return true; }
eval_ordering(a.cmp(b), op)
},
(Value::String(d), Value::String(f)) => match op {
Operator::StartsWith => d.starts_with(f),
Operator::Contains => d.contains(f),
Operator::Match => {
// We create owned lowercase strings to ensure references created
// by split_whitespace() remain valid while checking the HashSet.
let d_lower = d.to_lowercase();
let f_lower = f.to_lowercase();
let doc_words: HashSet<&str> = d_lower.split_whitespace().collect();
// Return true if every word in the filter exists in the document
f_lower.split_whitespace().all(|w| doc_words.contains(w))
}
Operator::MatchPrefix => {
// Word-prefix matching: every query word must be a prefix of some
// word in the document (mirrors the FTS-index prefix scan).
let d_lower = d.to_lowercase();
let f_lower = f.to_lowercase();
let doc_words: HashSet<&str> = d_lower.split_whitespace().collect();
f_lower
.split_whitespace()
.all(|w| doc_words.iter().any(|doc_word| doc_word.starts_with(w)))
}
// Fallback for standard string sorting (Eq, Gt, etc.)
_ => eval_ordering(d.cmp(f), op),
},
// Standard Order-based comparisons
(Value::Int(a), Value::Int(b)) => eval_ordering(a.cmp(b), op),
(Value::Timestamp(a), Value::Timestamp(b)) => eval_ordering(a.cmp(b), op),
(Value::Binary(a), Value::Binary(b)) => eval_ordering(a.cmp(b), op),
(Value::Bool(a), Value::Bool(b)) => eval_ordering(a.cmp(b), op),
// Compatibility bridge: some older gateway payloads encoded booleans as 0/1 integers.
// Keep equality/ordering behavior stable across mixed bool/int datasets.
(Value::Bool(a), Value::Int(b)) => eval_ordering((*a as i64).cmp(b), op),
(Value::Int(a), Value::Bool(b)) => eval_ordering(a.cmp(&(*b as i64)), op),
// Compatibility bridge: bool string literals ("true"/"false") from legacy payloads.
(Value::Bool(a), Value::String(s)) => parse_bool_like(s)
.map(|b| eval_ordering(a.cmp(&b), op))
.unwrap_or(false),
(Value::String(s), Value::Bool(b)) => parse_bool_like(s)
.map(|a| eval_ordering(a.cmp(b), op))
.unwrap_or(false),
(Value::Float(a), Value::Float(b)) => {
if let Some(ord) = a.partial_cmp(b) {
eval_ordering(ord, op)
} else {
false
}
}
(Value::Null, Value::Null) => eval_ordering(Ordering::Equal, op),
(Value::Int(a_val), Value::Float(b_val)) => {
eval_ordering((*a_val as f64).total_cmp(b_val), op)
}
(Value::Float(a_val), Value::Int(b_val)) => {
eval_ordering(a_val.total_cmp(&(*b_val as f64)), op)
}
// Reference comparison
(
Value::Reference {
collection: c1,
doc_id: i1,
},
Value::Reference {
collection: c2,
doc_id: i2,
},
) => eval_ordering(c1.cmp(c2).then(i1.cmp(i2)), op),
// Cross-type comparisons or comparisons involving ServerTimestamp placeholders
// In Firestore-style engines, comparing different types usually returns false.
// _ => eval_ordering(a.cmp(b), op),
_ => {
if a.type_weight() != b.type_weight() {
// Special case: Allow cross-comparison of Int and Float as "Numbers"
let is_numeric = (matches!(a, Value::Int(_)) || matches!(a, Value::Float(_))) &&
(matches!(b, Value::Int(_)) || matches!(b, Value::Float(_)));
if !is_numeric { return eval_ordering(a.type_weight().cmp(&b.type_weight()), op); }
}
eval_ordering(a.cmp(b), op)
}
}
}
#[inline]
fn parse_bool_like(input: &str) -> Option<bool> {
match input.trim().to_ascii_lowercase().as_str() {
"true" => Some(true),
"false" => Some(false),
_ => None,
}
}
/// Internal helper to map a Sort Ordering to an Operator boolean
fn eval_ordering(ord: Ordering, op: &Operator) -> bool {
match op {
Operator::Eq => ord == Ordering::Equal,
Operator::Ne => ord != Ordering::Equal,
Operator::Gt => ord == Ordering::Greater,
Operator::Gte => ord == Ordering::Greater || ord == Ordering::Equal,
Operator::Lt => ord == Ordering::Less,
Operator::Lte => ord == Ordering::Less || ord == Ordering::Equal,
// String-only operators return false if used on non-string types
Operator::Match
| Operator::MatchPrefix
| Operator::Contains
| Operator::StartsWith
| Operator::In
| Operator::NotIn
| Operator::ArrayContains
| Operator::ArrayContainsAny
// Near is a scan, not a predicate: only the VectorKnn arm (which
// verifies everything itself and marks the plan satisfied) may
// produce rows for it. Any re-verification path answers false.
// Traverse identical: only the Traverse scan evaluates it.
| Operator::Near | Operator::Traverse => false,
}
}