MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / try_document_index_lookup

Function try_document_index_lookup

nodedb-sql/src/engine_rules/mod.rs:193–287  ·  view source on GitHub ↗

Attempt to rewrite `ScanParams` into a [`SqlPlan::DocumentIndexLookup`] when exactly one of the filters is an equality predicate on a `Ready` indexed field. Returns `None` to fall through to a generic `Scan`. Shared by the schemaless and strict document engines so the index-rewrite rule has one source of truth. Normalizes strict column names to `$.column` before matching against index fields beca

(
    params: &ScanParams,
    engine: EngineType,
)

Source from the content-addressed store, hash-verified

191/// names to `$.column` before matching against index fields because the
192/// catalog stores every document index in JSON-path canonical form.
193pub(crate) fn try_document_index_lookup(
194 params: &ScanParams,
195 engine: EngineType,
196) -> Option<SqlPlan> {
197 // Sort / window functions still force a full scan because the
198 // IndexedFetch handler does not yet order rows or evaluate window
199 // aggregates. DISTINCT is safe on the index path: the handler
200 // emits each matched doc exactly once and any further dedup is a
201 // cheap Control-Plane pass on the scan-shaped response.
202 if !params.sort_keys.is_empty() || !params.window_functions.is_empty() {
203 return None;
204 }
205
206 // Iterate filters to find the first equality candidate that lines up
207 // with a Ready index. Keep the remaining filters as post-filters.
208 // Predicates appear in three shapes:
209 // - `FilterExpr::Comparison { field, Eq, value }` — resolver path.
210 // - `FilterExpr::Expr(BinaryOp { Column, Eq, Literal })` — generic.
211 // - `FilterExpr::Expr(BinaryOp { left AND right })` — the
212 // top-level AND the `convert_where_to_filters` path produces
213 // for compound WHERE clauses like `a = 1 AND b > 2`. Descend
214 // into AND conjuncts so an equality nested in a conjunction
215 // still picks up the index, and the non-equality siblings
216 // survive as a residual conjunction carried on the
217 // IndexedFetch node (not as a wrapping Filter plan node —
218 // that wrapping is the Control-Plane post-filter anti-pattern
219 // the handler's `filters` payload exists to eliminate).
220 // Pre-collect the query's top-level conjuncts once. Used for
221 // partial-index entailment: every conjunct of the index predicate
222 // must appear as a conjunct of the query WHERE for the rewrite to
223 // be sound, otherwise the index would silently omit rows the
224 // query expects to see.
225 let query_conjuncts: Vec<SqlExpr> = params
226 .filters
227 .iter()
228 .flat_map(|f| match &f.expr {
229 FilterExpr::Expr(e) => {
230 let mut v = Vec::new();
231 flatten_and(e, &mut v);
232 v
233 }
234 _ => Vec::new(),
235 })
236 .collect();
237
238 for (i, f) in params.filters.iter().enumerate() {
239 let Some((field, value, residual)) = extract_equality_with_residual(&f.expr) else {
240 continue;
241 };
242 let canonical = canonical_index_field(&field);
243 let Some(idx) = params.indexes.iter().find(|i| {
244 i.state == IndexState::Ready
245 && i.field == canonical
246 && partial_index_entailed(i.predicate.as_deref(), &query_conjuncts)
247 }) else {
248 continue;
249 };
250

Callers 2

plan_scanMethod · 0.85
plan_scanMethod · 0.85

Calls 12

flatten_andFunction · 0.85
canonical_index_fieldFunction · 0.85
partial_index_entailedFunction · 0.85
lowercase_string_valueFunction · 0.85
collectMethod · 0.80
ExprEnum · 0.50
is_emptyMethod · 0.45
iterMethod · 0.45
findMethod · 0.45
cloneMethod · 0.45
removeMethod · 0.45

Tested by

no test coverage detected