velesdb-core 1.15.0

High-performance vector database engine written in Rust
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
//! SQL projection engine for VelesQL SELECT expressions.
//!
//! Applies `SelectColumns` to `SearchResult` rows, producing JSON objects
//! with only the requested fields. Used by the query pipeline after
//! post-processing (DISTINCT, ORDER BY, LIMIT).

use crate::point::SearchResult;
use crate::velesql::{SelectColumns, SimilarityScoreExpr};

/// Projects a list of `SearchResult` according to the parsed SELECT expressions.
///
/// Returns `serde_json::Value::Object` rows with only the requested fields.
/// The `id` field is always the system point ID (takes precedence over payload).
#[must_use]
pub fn project_results(
    results: &[SearchResult],
    select_exprs: &SelectColumns,
) -> Vec<serde_json::Value> {
    results
        .iter()
        .map(|r| project_single(r, select_exprs))
        .collect()
}

/// Projects a single `SearchResult` into a JSON row.
fn project_single(result: &SearchResult, select_exprs: &SelectColumns) -> serde_json::Value {
    match select_exprs {
        SelectColumns::All | SelectColumns::QualifiedWildcard(_) => project_wildcard(result),
        SelectColumns::Columns(cols) => project_columns(result, cols),
        SelectColumns::SimilarityScore(expr) => project_similarity_only(result, expr),
        SelectColumns::Aggregations(_) => {
            // Aggregations are handled by a separate code path; return empty row.
            serde_json::Value::Object(serde_json::Map::new())
        }
        SelectColumns::Mixed {
            columns,
            aggregations: _,
            similarity_scores,
            qualified_wildcards,
            window_functions,
        } => project_mixed(
            result,
            columns,
            similarity_scores,
            qualified_wildcards,
            window_functions,
        ),
    }
}

/// `SELECT *` or `SELECT alias.*`: returns `{id, ...payload_fields}`.
///
/// Excludes vectors and similarity score. Use `SELECT similarity() AS score, *`
/// to include the score explicitly.
fn project_wildcard(result: &SearchResult) -> serde_json::Value {
    let mut map = serde_json::Map::new();
    map.insert("id".to_string(), serde_json::Value::from(result.point.id));

    if let Some(serde_json::Value::Object(payload_map)) = result.point.payload.as_ref() {
        for (k, v) in payload_map {
            if k != "id" {
                map.insert(k.clone(), v.clone());
            }
        }
    }

    serde_json::Value::Object(map)
}

/// `SELECT col1, col2 [AS alias]`: extracts only named fields.
fn project_columns(result: &SearchResult, columns: &[crate::velesql::Column]) -> serde_json::Value {
    let mut map = serde_json::Map::new();

    for col in columns {
        let output_key = col.alias.as_deref().unwrap_or(&col.name);
        let value = extract_field_value(result, &col.name);
        map.insert(output_key.to_string(), value);
    }

    serde_json::Value::Object(map)
}

/// `SELECT similarity() [AS alias]`: materializes the score only.
fn project_similarity_only(result: &SearchResult, expr: &SimilarityScoreExpr) -> serde_json::Value {
    let mut map = serde_json::Map::new();
    let key = expr.alias.as_deref().unwrap_or("similarity");
    map.insert(
        key.to_string(),
        serde_json::Value::from(f64::from(result.score)),
    );
    serde_json::Value::Object(map)
}

/// Mixed projection: columns + similarity scores + qualified wildcards + window functions.
///
/// Window function values were injected into the row's payload by
/// [`crate::velesql::window_evaluator`]. The wildcard-expansion step below
/// therefore must skip keys that correspond to window-function aliases —
/// otherwise those values would be read from the payload twice (once by
/// wildcard expansion, once by the explicit window-function loop). The final
/// value would still be correct (the explicit loop wins), but the extra
/// copy is pointless and mis-signals in reviews as suspicious dedup.
fn project_mixed(
    result: &SearchResult,
    columns: &[crate::velesql::Column],
    similarity_scores: &[SimilarityScoreExpr],
    qualified_wildcards: &[String],
    window_functions: &[crate::velesql::WindowFunction],
) -> serde_json::Value {
    let mut map = serde_json::Map::new();

    // Pre-compute the set of window-function aliases so wildcard expansion
    // can skip them in O(1) per payload key.
    let window_aliases: rustc_hash::FxHashSet<&str> = window_functions
        .iter()
        .map(|wf| {
            wf.alias
                .as_deref()
                .unwrap_or(wf.function_type.default_alias())
        })
        .collect();

    // Qualified wildcards expand to all payload fields + id.
    if !qualified_wildcards.is_empty() {
        map.insert("id".to_string(), serde_json::Value::from(result.point.id));
        if let Some(serde_json::Value::Object(payload_map)) = result.point.payload.as_ref() {
            for (k, v) in payload_map {
                if k != "id" && !window_aliases.contains(k.as_str()) {
                    map.insert(k.clone(), v.clone());
                }
            }
        }
    }

    // Named columns.
    for col in columns {
        let output_key = col.alias.as_deref().unwrap_or(&col.name);
        let value = extract_field_value(result, &col.name);
        map.insert(output_key.to_string(), value);
    }

    // Similarity scores.
    for expr in similarity_scores {
        let key = expr.alias.as_deref().unwrap_or("similarity");
        map.insert(
            key.to_string(),
            serde_json::Value::from(f64::from(result.score)),
        );
    }

    // Window function values (injected into payload by window_evaluator).
    // This is the single source of truth for window aliases — wildcard
    // expansion above deliberately skipped them.
    for wf in window_functions {
        let alias = wf
            .alias
            .as_deref()
            .unwrap_or(wf.function_type.default_alias());
        let value = result
            .point
            .payload
            .as_ref()
            .and_then(|p| p.get(alias))
            .cloned()
            .unwrap_or(serde_json::Value::Null);
        map.insert(alias.to_string(), value);
    }

    serde_json::Value::Object(map)
}

/// Extracts a field value from a `SearchResult`, supporting nested paths.
///
/// - `"title"` → `payload["title"]`
/// - `"meta.source"` → `payload["meta"]["source"]`
/// - `"id"` → system point ID (takes precedence over payload)
fn extract_field_value(result: &SearchResult, field_path: &str) -> serde_json::Value {
    if field_path == "id" {
        return serde_json::Value::from(result.point.id);
    }

    let Some(payload) = result.point.payload.as_ref() else {
        return serde_json::Value::Null;
    };

    if field_path.contains('.') {
        // Nested path traversal
        let mut current = payload;
        for segment in field_path.split('.') {
            match current.get(segment) {
                Some(next) => current = next,
                None => return serde_json::Value::Null,
            }
        }
        current.clone()
    } else {
        payload
            .get(field_path)
            .cloned()
            .unwrap_or(serde_json::Value::Null)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::point::Point;
    use crate::velesql::Column;

    fn make_result(id: u64, score: f32, payload: serde_json::Value) -> SearchResult {
        SearchResult::new(
            Point {
                id,
                vector: vec![0.0; 4],
                payload: Some(payload),
                sparse_vectors: None,
            },
            score,
        )
    }

    #[test]
    fn test_project_wildcard_returns_id_and_payload() {
        let result = make_result(42, 0.95, serde_json::json!({"title": "Hello", "count": 5}));
        let projected = project_single(&result, &SelectColumns::All);
        let obj = projected.as_object().expect("should be object");
        assert_eq!(obj["id"], 42);
        assert_eq!(obj["title"], "Hello");
        assert_eq!(obj["count"], 5);
        assert!(!obj.contains_key("vector"));
    }

    #[test]
    fn test_project_wildcard_system_id_prevails() {
        let result = make_result(42, 0.95, serde_json::json!({"id": 999, "title": "Hello"}));
        let projected = project_single(&result, &SelectColumns::All);
        let obj = projected.as_object().unwrap();
        // System ID (42) must prevail over payload id (999)
        assert_eq!(obj["id"], 42);
    }

    #[test]
    fn test_project_specific_columns() {
        let result = make_result(
            1,
            0.9,
            serde_json::json!({"title": "Doc", "category": "tech", "author": "Alice"}),
        );
        let columns = SelectColumns::Columns(vec![Column::new("title"), Column::new("category")]);
        let projected = project_single(&result, &columns);
        let obj = projected.as_object().unwrap();
        assert_eq!(obj.len(), 2);
        assert_eq!(obj["title"], "Doc");
        assert_eq!(obj["category"], "tech");
        assert!(!obj.contains_key("author"));
    }

    #[test]
    fn test_project_similarity_score() {
        let result = make_result(1, 0.875, serde_json::json!({"title": "Doc"}));
        let expr = SimilarityScoreExpr {
            alias: Some("relevance".to_string()),
        };
        let projected = project_single(&result, &SelectColumns::SimilarityScore(expr));
        let obj = projected.as_object().unwrap();
        assert_eq!(obj.len(), 1);
        let relevance = obj["relevance"].as_f64().unwrap();
        assert!((relevance - 0.875).abs() < 1e-3);
    }

    #[test]
    fn test_project_similarity_default_key() {
        let result = make_result(1, 0.5, serde_json::json!({}));
        let expr = SimilarityScoreExpr { alias: None };
        let projected = project_single(&result, &SelectColumns::SimilarityScore(expr));
        let obj = projected.as_object().unwrap();
        assert!(obj.contains_key("similarity"));
    }

    #[test]
    fn test_project_nested_path() {
        let result = make_result(
            1,
            0.9,
            serde_json::json!({"meta": {"source": "wiki", "lang": "en"}}),
        );
        let columns = SelectColumns::Columns(vec![Column::new("meta.source")]);
        let projected = project_single(&result, &columns);
        let obj = projected.as_object().unwrap();
        assert_eq!(obj["meta.source"], "wiki");
    }

    #[test]
    fn test_project_missing_field_returns_null() {
        let result = make_result(1, 0.9, serde_json::json!({"title": "Doc"}));
        let columns = SelectColumns::Columns(vec![Column::new("nonexistent")]);
        let projected = project_single(&result, &columns);
        let obj = projected.as_object().unwrap();
        assert!(obj["nonexistent"].is_null());
    }

    #[test]
    fn test_project_mixed_columns_and_similarity() {
        let result = make_result(
            1,
            0.85,
            serde_json::json!({"title": "Doc", "author": "Bob"}),
        );
        let columns = SelectColumns::Mixed {
            columns: vec![Column::new("title")],
            aggregations: vec![],
            similarity_scores: vec![SimilarityScoreExpr {
                alias: Some("score".to_string()),
            }],
            qualified_wildcards: vec![],
            window_functions: vec![],
        };
        let projected = project_single(&result, &columns);
        let obj = projected.as_object().unwrap();
        assert_eq!(obj["title"], "Doc");
        assert!(!obj.contains_key("author"));
        let score = obj["score"].as_f64().unwrap();
        assert!((score - 0.85).abs() < 1e-3);
    }

    #[test]
    fn test_project_qualified_wildcard_with_similarity() {
        let result = make_result(
            5,
            0.75,
            serde_json::json!({"title": "Article", "views": 100}),
        );
        let columns = SelectColumns::Mixed {
            columns: vec![],
            aggregations: vec![],
            similarity_scores: vec![SimilarityScoreExpr {
                alias: Some("relevance".to_string()),
            }],
            qualified_wildcards: vec!["ctx".to_string()],
            window_functions: vec![],
        };
        let projected = project_single(&result, &columns);
        let obj = projected.as_object().unwrap();
        assert_eq!(obj["id"], 5);
        assert_eq!(obj["title"], "Article");
        assert_eq!(obj["views"], 100);
        let rel = obj["relevance"].as_f64().unwrap();
        assert!((rel - 0.75).abs() < 1e-3);
    }

    #[test]
    fn test_project_column_with_alias() {
        let result = make_result(1, 0.9, serde_json::json!({"title": "Hello World"}));
        let columns = SelectColumns::Columns(vec![Column::with_alias("title", "name")]);
        let projected = project_single(&result, &columns);
        let obj = projected.as_object().unwrap();
        assert_eq!(obj["name"], "Hello World");
        assert!(!obj.contains_key("title"));
    }

    #[test]
    fn test_project_results_multiple() {
        let results = vec![
            make_result(1, 0.9, serde_json::json!({"title": "A"})),
            make_result(2, 0.8, serde_json::json!({"title": "B"})),
        ];
        let projected = project_results(&results, &SelectColumns::All);
        assert_eq!(projected.len(), 2);
        assert_eq!(projected[0]["id"], 1);
        assert_eq!(projected[1]["id"], 2);
    }

    #[test]
    fn test_order_by_similarity_bare_sorts_by_existing_score() {
        // This test validates the integration with ordering.rs SimilarityBare
        let results = vec![
            make_result(1, 0.5, serde_json::json!({"title": "Low"})),
            make_result(2, 0.9, serde_json::json!({"title": "High"})),
            make_result(3, 0.7, serde_json::json!({"title": "Mid"})),
        ];
        // Verify scores are preserved correctly for bare similarity ordering
        let projected = project_results(
            &results,
            &SelectColumns::SimilarityScore(SimilarityScoreExpr {
                alias: Some("score".to_string()),
            }),
        );
        let scores: Vec<f64> = projected
            .iter()
            .map(|r| r["score"].as_f64().unwrap())
            .collect();
        assert!((scores[0] - 0.5).abs() < 1e-3);
        assert!((scores[1] - 0.9).abs() < 1e-3);
        assert!((scores[2] - 0.7).abs() < 1e-3);
    }

    #[test]
    fn test_project_wildcard_no_payload() {
        let result = SearchResult::new(
            Point {
                id: 7,
                vector: vec![0.0; 4],
                payload: None,
                sparse_vectors: None,
            },
            0.5,
        );
        let projected = project_single(&result, &SelectColumns::All);
        let obj = projected.as_object().unwrap();
        assert_eq!(obj.len(), 1);
        assert_eq!(obj["id"], 7);
    }

    #[test]
    fn test_project_column_no_payload() {
        let result = SearchResult::new(
            Point {
                id: 7,
                vector: vec![0.0; 4],
                payload: None,
                sparse_vectors: None,
            },
            0.5,
        );
        let columns = SelectColumns::Columns(vec![Column::new("title")]);
        let projected = project_single(&result, &columns);
        let obj = projected.as_object().unwrap();
        assert!(obj["title"].is_null());
    }

    /// Issue #473: LET-injected values in the payload are visible through
    /// `SelectColumns::Mixed` qualified wildcard expansion.
    ///
    /// The execution pipeline injects LET binding values into `point.payload`
    /// before calling `project_results`. This test verifies that those injected
    /// keys survive the `project_mixed` path (wildcard expansion + named column).
    #[test]
    fn test_project_mixed_wildcard_exposes_let_injected_payload_field() {
        // Simulate post-LET-injection payload: original fields plus "hybrid" injected
        // by inject_let_into_payloads in select_dispatch.rs.
        let result = make_result(
            3,
            0.88,
            serde_json::json!({"title": "Doc", "idx": 7, "hybrid": 0.5}),
        );
        let columns = SelectColumns::Mixed {
            // SELECT docs.*, hybrid — qualified wildcard + explicit named column
            columns: vec![Column::new("hybrid")],
            aggregations: vec![],
            similarity_scores: vec![],
            qualified_wildcards: vec!["docs".to_string()],
            window_functions: vec![],
        };
        let projected = project_single(&result, &columns);
        let obj = projected.as_object().unwrap();

        // Wildcard expansion must include original payload fields.
        assert_eq!(obj["id"], 3);
        assert_eq!(obj["title"], "Doc");
        assert_eq!(obj["idx"], 7);
        // LET-injected value must appear: both via wildcard expansion and explicit column.
        let hybrid = obj["hybrid"].as_f64().expect("hybrid should be f64");
        assert!(
            (hybrid - 0.5).abs() < 1e-5,
            "hybrid should be 0.5, got {hybrid}"
        );
    }
}