reddb-io-server 1.23.1

RedDB server-side engine: storage, runtime, replication, MCP, AI, and the gRPC/HTTP/RedWire/PG-wire dispatchers. Re-exported by the umbrella `reddb` crate.
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
//! Vector similarity search executor.
//!
//! Handles `Vector` query expressions — ANN search over a collection's
//! registered vector index with optional metadata pre/post-filters.
//! Split out of `query_exec.rs` to keep the main executor focused on
//! table-scan paths.
//!
//! Uses `use super::*;` to inherit the parent executor's imports.

use super::*;
use crate::runtime::vector_index::{BruteForceVectorIndex, VectorIndexEntry};
use crate::storage::engine::distance::DistanceMetric;
use crate::storage::query::sql_lowering::effective_vector_filter;

pub(crate) fn execute_runtime_vector_query(
    db: &RedDB,
    query: &VectorQuery,
) -> RedDBResult<UnifiedResult> {
    let plan = CanonicalPlanner::new(db).build(&QueryExpr::Vector(query.clone()));
    let records = execute_runtime_canonical_vector_node(db, &plan.root, query)?;

    Ok(UnifiedResult {
        columns: collect_visible_columns(&records),
        records,
        stats: Default::default(),
        pre_serialized_json: None,
    })
}

pub(crate) fn execute_runtime_canonical_vector_node(
    db: &RedDB,
    node: &crate::storage::query::planner::CanonicalLogicalNode,
    query: &VectorQuery,
) -> RedDBResult<Vec<UnifiedRecord>> {
    match node.operator.as_str() {
        "vector_ann_hnsw" | "vector_ann_ivf" | "vector_exact_scan" => {
            let vector = resolve_runtime_vector_source(db, &query.query_vector)?;
            let matches = runtime_vector_matches(db, query, &vector)?;
            Ok(matches
                .into_iter()
                .map(runtime_vector_record_from_match)
                .collect())
        }
        "metadata_filter" => {
            let mut records = execute_runtime_canonical_vector_child(db, node, query)?;
            if let Some(filter) = effective_vector_filter(query).as_ref() {
                records.retain(|record| {
                    runtime_vector_record_matches_filter(db, &query.collection, record, filter)
                });
            }
            Ok(records)
        }
        "similarity_threshold" => {
            let mut records = execute_runtime_canonical_vector_child(db, node, query)?;
            if let Some(threshold) = query.threshold {
                let metric = runtime_vector_metric(db, query);
                records.retain(|record| {
                    runtime_vector_record_within_threshold(record, metric, threshold)
                });
            }
            Ok(records)
        }
        "topk" => {
            let mut records = execute_runtime_canonical_vector_child(db, node, query)?;
            records.sort_by(compare_runtime_ranked_records);
            Ok(records.into_iter().take(query.k.max(1)).collect())
        }
        "projection" => execute_runtime_canonical_vector_child(db, node, query),
        other => Err(RedDBError::Query(format!(
            "unsupported canonical vector operator {other}"
        ))),
    }
}

pub(crate) fn execute_runtime_canonical_vector_child(
    db: &RedDB,
    node: &crate::storage::query::planner::CanonicalLogicalNode,
    query: &VectorQuery,
) -> RedDBResult<Vec<UnifiedRecord>> {
    let child = node.children.first().ok_or_else(|| {
        RedDBError::Query(format!(
            "canonical vector operator {} is missing its child plan",
            node.operator
        ))
    })?;
    execute_runtime_canonical_vector_node(db, child, query)
}

pub(crate) fn runtime_vector_matches(
    db: &RedDB,
    query: &VectorQuery,
    vector: &[f32],
) -> RedDBResult<Vec<SimilarResult>> {
    validate_vector_query_shape(db, query, vector)?;
    let metric = runtime_vector_metric(db, query);
    let manager = db
        .store()
        .get_collection(&query.collection)
        .ok_or_else(|| RedDBError::NotFound(query.collection.clone()))?;

    // Issue #693 — `vector.turbo` SEARCH goes through the
    // TurboQuantIndex, which dispatches scoring through
    // `select_scorer()` (scalar / AVX2 / AVX-512BW / NEON, runtime
    // selected). Legacy `vector` collections continue on the
    // brute-force path below.
    if let Some(state) = db.turbo_state(&query.collection) {
        // Issue #673 — wait briefly for the background rebuild to
        // finish. If the timeout fires, return a structured NOT_READY
        // signal instead of silently blocking or returning empty.
        // The bounded wait lets fast rebuilds satisfy the caller
        // transparently while slow ones surface as actionable errors.
        let wait_ms = std::env::var("REDDB_TURBO_SEARCH_READY_TIMEOUT_MS")
            .ok()
            .and_then(|s| s.parse::<u64>().ok())
            .unwrap_or(500);
        if !state.wait_until_ready(std::time::Duration::from_millis(wait_ms)) {
            // Fall back to a synchronous populate from the calling
            // thread. The rebuild may have raced with the wait
            // (cheap collections finish before the worker is even
            // scheduled); the explicit populate doubles as a
            // last-chance unblock so a SEARCH after restart on a
            // single-vector collection never spuriously 503s.
            state.ensure_populated(&db.store(), &query.collection);
            if !state.is_ready() {
                return Err(RedDBError::InvalidOperation(format!(
                    "NOT_READY: vector.turbo collection '{}' is rebuilding (turbo index recovery); retry shortly",
                    query.collection
                )));
            }
        }
        // TurboQuant returns *approximate* (quantised) scores; on small or
        // low-dimensional collections the quantisation collapses the scores
        // and the approximate order is wrong (#1372). Over-fetch a generous
        // candidate set from the index, then re-rank with full-precision
        // exact distances so metric ordering is correct. The index still
        // prunes the candidate set on large collections.
        const RERANK_OVERFETCH: usize = 32;
        let k = query.k.max(1);
        let collection_count = manager.count().max(1);
        let search_k = if effective_vector_filter(query).is_some() {
            collection_count
        } else {
            k.saturating_mul(RERANK_OVERFETCH).min(collection_count)
        };
        let raw = {
            let index = state.index.lock();
            index.search(vector, search_k, metric)
        };
        let mut results = Vec::with_capacity(raw.len());
        let filter = effective_vector_filter(query);
        for hit in raw {
            let Some(entity) = db.store().get(&query.collection, hit.entity_id) else {
                continue;
            };
            // The TurboQuant index is append-only and never prunes
            // deleted/superseded vectors (#673/#688 own removal). Post-
            // filter every candidate through the current-snapshot
            // visibility gate — which hides any tombstoned/superseded
            // physical version (`xmax != 0`) even in autocommit (where
            // there is no captured snapshot context) — so a deleted or
            // version-superseded vector never reaches the results.
            if !crate::runtime::impl_core::entity_visible_under_current_snapshot(&entity) {
                continue;
            }
            if let Some(filter) = filter.as_ref() {
                if !runtime_vector_entity_matches_filter(db, &query.collection, entity.id, filter) {
                    continue;
                }
            }
            // Exact re-rank against the stored full-precision vector rather
            // than trusting the quantised approximate score.
            let (score, distance) = match &entity.data {
                EntityData::Vector(data) => {
                    let raw_distance =
                        crate::storage::engine::distance::distance(vector, &data.dense, metric);
                    let score = match metric {
                        DistanceMetric::Cosine => 1.0 - raw_distance,
                        DistanceMetric::InnerProduct | DistanceMetric::L2 => -raw_distance,
                    };
                    (score, raw_distance)
                }
                _ => {
                    let distance = match metric {
                        DistanceMetric::Cosine => 1.0 - hit.score,
                        DistanceMetric::InnerProduct | DistanceMetric::L2 => -hit.score,
                    };
                    (hit.score, distance)
                }
            };
            if let Some(threshold) = query.threshold {
                let pass = match metric {
                    DistanceMetric::L2 => distance <= threshold,
                    DistanceMetric::Cosine | DistanceMetric::InnerProduct => score >= threshold,
                };
                if !pass {
                    continue;
                }
            }
            results.push(SimilarResult {
                entity_id: hit.entity_id,
                score,
                distance,
                entity,
            });
        }
        results.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
                .then_with(|| a.entity_id.raw().cmp(&b.entity_id.raw()))
        });
        results.truncate(k);
        return Ok(results);
    }

    let snap_ctx = crate::runtime::impl_core::capture_current_snapshot();
    let mut index = BruteForceVectorIndex::default();
    let filter = effective_vector_filter(query);
    let search_k = if effective_vector_filter(query).is_some() {
        manager.count().max(1)
    } else {
        query.k.max(1)
    };

    for entity in manager.query_all(|entity| {
        // `query_all` may fan out across worker threads that do not
        // inherit the dispatch thread's snapshot thread-local, so we
        // pass the captured snapshot context explicitly. In autocommit
        // there is no captured context (`None`); the table-scan paths
        // would treat that as "always visible", but a deleted/superseded
        // vector carries `xmax != 0` and must be hidden — mirror the
        // autocommit rule of `entity_visible_under_current_snapshot`.
        if snap_ctx.is_none() {
            return entity.xmax == 0
                && !crate::runtime::ai::moderation::entity_moderation_hidden(entity);
        }
        crate::runtime::impl_core::entity_visible_with_context(snap_ctx.as_ref(), entity)
    }) {
        if let Some(filter) = filter.as_ref() {
            if !runtime_vector_entity_matches_filter(db, &query.collection, entity.id, filter) {
                continue;
            }
        }
        if let EntityData::Vector(data) = &entity.data {
            index.upsert(VectorIndexEntry {
                entity_id: entity.id,
                vector: data.dense.clone(),
                entity,
            });
        }
    }

    let out = index.search(vector, search_k, metric, query.threshold);
    eprintln!(
        "VECDBG matches metric={metric:?} k={search_k}: {:?}",
        out.iter()
            .map(|m| (m.entity_id.raw(), m.score))
            .collect::<Vec<_>>()
    );
    Ok(out)
}

pub(crate) fn runtime_vector_record_matches_filter(
    db: &RedDB,
    collection: &str,
    record: &UnifiedRecord,
    filter: &VectorMetadataFilter,
) -> bool {
    let entity_id = record
        .get("entity_id")
        .or_else(|| record.get("rid"))
        .and_then(|value| match value {
            Value::UnsignedInteger(value) => Some(EntityId::new(*value)),
            Value::Integer(value) if *value >= 0 => Some(EntityId::new(*value as u64)),
            _ => None,
        });

    let Some(entity_id) = entity_id else {
        return false;
    };

    let metadata = db
        .store()
        .get_metadata(collection, entity_id)
        .unwrap_or_default();
    runtime_metadata_matches_vector_filter(&metadata, filter)
}

fn runtime_vector_entity_matches_filter(
    db: &RedDB,
    collection: &str,
    entity_id: EntityId,
    filter: &VectorMetadataFilter,
) -> bool {
    let metadata = db
        .store()
        .get_metadata(collection, entity_id)
        .unwrap_or_default();
    runtime_metadata_matches_vector_filter(&metadata, filter)
}

fn runtime_metadata_matches_vector_filter(
    metadata: &Metadata,
    filter: &VectorMetadataFilter,
) -> bool {
    match filter {
        VectorMetadataFilter::GeoRadius {
            key,
            center_lat,
            center_lon,
            radius_km,
        } => metadata
            .get(key)
            .and_then(runtime_metadata_geo_point)
            .is_some_and(|(lat, lon)| {
                crate::geo::haversine_km(*center_lat, *center_lon, lat, lon) <= *radius_km
            }),
        VectorMetadataFilter::And(filters) => filters
            .iter()
            .all(|filter| runtime_metadata_matches_vector_filter(metadata, filter)),
        VectorMetadataFilter::Or(filters) => filters
            .iter()
            .any(|filter| runtime_metadata_matches_vector_filter(metadata, filter)),
        VectorMetadataFilter::Not(inner) => {
            !runtime_metadata_matches_vector_filter(metadata, inner)
        }
        _ => {
            let entry = runtime_metadata_entry(metadata);
            filter.matches(&entry)
        }
    }
}

fn runtime_metadata_geo_point(value: &UnifiedMetadataValue) -> Option<(f64, f64)> {
    let fields = match value {
        UnifiedMetadataValue::Geo { lat, lon } => vec![
            ("lat".to_string(), Value::Float(*lat)),
            ("lon".to_string(), Value::Float(*lon)),
        ],
        UnifiedMetadataValue::Object(object) => object
            .iter()
            .filter_map(|(key, value)| {
                runtime_metadata_geo_field_value(value).map(|value| (key.clone(), value))
            })
            .collect(),
        _ => return None,
    };
    crate::geo::recognize_geo_fields(|key| {
        fields
            .iter()
            .find_map(|(field, value)| (field == key).then_some(value))
    })
}

fn runtime_metadata_geo_field_value(value: &UnifiedMetadataValue) -> Option<Value> {
    match value {
        UnifiedMetadataValue::Int(value) => Some(Value::Integer(*value)),
        UnifiedMetadataValue::Float(value) => Some(Value::Float(*value)),
        _ => None,
    }
}

pub(crate) fn runtime_vector_metric(db: &RedDB, query: &VectorQuery) -> DistanceMetric {
    query
        .metric
        .or_else(|| {
            db.collection_contract(&query.collection)
                .and_then(|contract| contract.vector_metric)
        })
        .unwrap_or(DistanceMetric::Cosine)
}

fn validate_vector_query_shape(db: &RedDB, query: &VectorQuery, vector: &[f32]) -> RedDBResult<()> {
    if let Some(expected) = db
        .collection_contract(&query.collection)
        .and_then(|contract| contract.vector_dimension)
    {
        if expected != vector.len() {
            return Err(RedDBError::Query(format!(
                "vector dimension mismatch for collection '{}': expected {}, got {}",
                query.collection,
                expected,
                vector.len()
            )));
        }
    }
    Ok(())
}

fn runtime_vector_record_within_threshold(
    record: &UnifiedRecord,
    metric: DistanceMetric,
    threshold: f32,
) -> bool {
    match metric {
        DistanceMetric::L2 => record
            .get("distance")
            .and_then(runtime_value_number)
            .is_some_and(|distance| distance <= threshold as f64),
        DistanceMetric::Cosine | DistanceMetric::InnerProduct => {
            runtime_record_rank_score(record) >= threshold as f64
        }
    }
}