velesdb-core 1.14.4

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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
//! Execution tests for LET clause (VelesQL v1.10 Phase 3).
//!
//! Validates that LET bindings are correctly evaluated and used by
//! ORDER BY during query execution. Tests cover:
//! - LET + ORDER BY interaction
//! - Weighted hybrid ordering
//! - Chained bindings
//! - Backward compatibility (no LET)
//! - Edge cases (unused bindings, WITH + LET, OFFSET + LET)

#![allow(
    clippy::cast_precision_loss,
    clippy::cast_possible_truncation,
    clippy::float_cmp
)]

use crate::collection::types::Collection;
use crate::distance::DistanceMetric;
use crate::point::Point;
use std::collections::HashMap;
use tempfile::TempDir;

// ============================================================================
// Helper: create a 4-dim cosine collection with 20 points
// ============================================================================

fn setup_let_collection() -> (TempDir, Collection) {
    let dir = TempDir::new().unwrap();
    let path = dir.path().join("let_col");
    let col = Collection::create(path, 4, DistanceMetric::Cosine).expect("create collection");

    let mut points = Vec::new();
    for i in 0u64..20 {
        #[allow(clippy::cast_precision_loss)]
        let fi = i as f32;
        let v = vec![fi / 20.0, 1.0 - fi / 20.0, 0.5, 0.3];
        points.push(Point {
            id: i,
            vector: v,
            payload: Some(serde_json::json!({ "idx": i, "priority": 20 - i })),
            sparse_vectors: None,
        });
    }
    col.upsert(points).expect("upsert");
    (dir, col)
}

// ============================================================================
// A. LET binding used in ORDER BY
// ============================================================================

/// `LET s = similarity() ... ORDER BY s DESC` produces same ordering as
/// `ORDER BY similarity() DESC`.
#[test]
fn test_let_binding_in_order_by() {
    let (_dir, col) = setup_let_collection();
    let mut params = HashMap::new();
    params.insert("v".to_string(), serde_json::json!([0.5, 0.5, 0.5, 0.3]));

    // Baseline: ORDER BY similarity() DESC
    let baseline = col
        .execute_query_str(
            "SELECT * FROM docs WHERE vector NEAR $v ORDER BY similarity() DESC LIMIT 5",
            &params,
        )
        .expect("baseline query");

    // LET version: ORDER BY s DESC
    let let_results = col
        .execute_query_str(
            "LET s = similarity() SELECT * FROM docs WHERE vector NEAR $v ORDER BY s DESC LIMIT 5",
            &params,
        )
        .expect("LET query");

    assert_eq!(baseline.len(), let_results.len());
    // Same ordering: same IDs in the same positions.
    let baseline_ids: Vec<u64> = baseline.iter().map(|r| r.point.id).collect();
    let let_ids: Vec<u64> = let_results.iter().map(|r| r.point.id).collect();
    assert_eq!(
        baseline_ids, let_ids,
        "LET s = similarity() ORDER BY s should match ORDER BY similarity()"
    );
}

// ============================================================================
// B. Weighted hybrid scoring (LET changes ordering)
// ============================================================================

/// `LET hybrid = 0.01 * similarity() + 0.99 * priority ...`
/// This heavily weights `priority` (which decreases as id increases),
/// producing a DIFFERENT ordering than pure similarity-based ordering.
/// Similarity peaks around id=10 (middle), but priority peaks at id=0.
#[test]
fn test_let_weighted_hybrid_ordering() {
    let (_dir, col) = setup_let_collection();
    let mut params = HashMap::new();
    params.insert("v".to_string(), serde_json::json!([0.5, 0.5, 0.5, 0.3]));

    // Baseline: pure similarity ordering — top results cluster around id=10
    let baseline = col
        .execute_query_str(
            "SELECT * FROM docs WHERE vector NEAR $v ORDER BY similarity() DESC LIMIT 10",
            &params,
        )
        .expect("baseline");

    // Hybrid: priority dominates — top results should be low-id (high priority)
    let hybrid = col
        .execute_query_str(
            "LET hybrid = 0.01 * similarity() + 0.99 * priority \
             SELECT * FROM docs WHERE vector NEAR $v ORDER BY hybrid DESC LIMIT 10",
            &params,
        )
        .expect("hybrid query");

    assert_eq!(hybrid.len(), 10);

    // Priority-dominated ordering should put low-id (high-priority) results first,
    // unlike similarity-dominated ordering which clusters around id~10.
    // Note: candidates come from HNSW search which limits to similar vectors, so
    // not all 20 points are available — the reorder happens within that candidate set.
    let baseline_ids: Vec<u64> = baseline.iter().map(|r| r.point.id).collect();
    let hybrid_ids: Vec<u64> = hybrid.iter().map(|r| r.point.id).collect();
    assert_ne!(
        baseline_ids, hybrid_ids,
        "Priority-dominated ordering should differ from similarity ordering"
    );
    // Verify that the highest-priority (lowest-id) candidate from the HNSW
    // result set is now first. With 0.99 weight on priority, the smallest id
    // in the candidate set should be first.
    let min_id_in_candidates = *hybrid_ids.iter().min().expect("non-empty results");
    assert_eq!(
        hybrid_ids[0], min_id_in_candidates,
        "First result should be the lowest-id (highest priority) candidate"
    );
}

// ============================================================================
// C. Chained bindings
// ============================================================================

/// `LET a = similarity() LET b = a * 2.0 ORDER BY b DESC` should produce
/// the same ranking as `ORDER BY similarity() DESC` (monotonic transform).
#[test]
fn test_let_chained_bindings() {
    let (_dir, col) = setup_let_collection();
    let mut params = HashMap::new();
    params.insert("v".to_string(), serde_json::json!([0.5, 0.5, 0.5, 0.3]));

    let baseline = col
        .execute_query_str(
            "SELECT * FROM docs WHERE vector NEAR $v ORDER BY similarity() DESC LIMIT 5",
            &params,
        )
        .expect("baseline");

    let chained = col
        .execute_query_str(
            "LET a = similarity() LET b = a * 2.0 \
             SELECT * FROM docs WHERE vector NEAR $v ORDER BY b DESC LIMIT 5",
            &params,
        )
        .expect("chained query");

    let baseline_ids: Vec<u64> = baseline.iter().map(|r| r.point.id).collect();
    let chained_ids: Vec<u64> = chained.iter().map(|r| r.point.id).collect();
    assert_eq!(
        baseline_ids, chained_ids,
        "Monotonic transform should preserve ordering"
    );
}

// ============================================================================
// D. Literal binding (constant ordering)
// ============================================================================

/// `LET threshold = 0.8 ORDER BY threshold DESC` — constant value produces
/// stable but arbitrary ordering (all scores equal).
#[test]
fn test_let_literal_binding() {
    let (_dir, col) = setup_let_collection();
    let mut params = HashMap::new();
    params.insert("v".to_string(), serde_json::json!([0.5, 0.5, 0.5, 0.3]));

    let results = col
        .execute_query_str(
            "LET threshold = 0.8 \
             SELECT * FROM docs WHERE vector NEAR $v ORDER BY threshold DESC LIMIT 5",
            &params,
        )
        .expect("literal query");

    assert_eq!(results.len(), 5);
}

// ============================================================================
// E. Unused binding — no error
// ============================================================================

/// Binding defined but not referenced in ORDER BY should not cause errors.
#[test]
fn test_let_unused_binding_no_error() {
    let (_dir, col) = setup_let_collection();
    let mut params = HashMap::new();
    params.insert("v".to_string(), serde_json::json!([0.5, 0.5, 0.5, 0.3]));

    let results = col
        .execute_query_str(
            "LET unused = 0.5 \
             SELECT * FROM docs WHERE vector NEAR $v ORDER BY similarity() DESC LIMIT 5",
            &params,
        )
        .expect("unused binding should not error");

    assert_eq!(results.len(), 5);
}

// ============================================================================
// F. Backward compatibility — no LET
// ============================================================================

/// Query without LET works identically to before.
#[test]
fn test_let_backward_compat_no_let() {
    let (_dir, col) = setup_let_collection();
    let mut params = HashMap::new();
    params.insert("v".to_string(), serde_json::json!([0.5, 0.5, 0.5, 0.3]));

    let results = col
        .execute_query_str(
            "SELECT * FROM docs WHERE vector NEAR $v ORDER BY similarity() DESC LIMIT 5",
            &params,
        )
        .expect("no-LET query");

    assert_eq!(results.len(), 5);
}

// ============================================================================
// G. Edge cases — OFFSET + LET, WITH + LET
// ============================================================================

/// LET + OFFSET interaction works.
#[test]
fn test_let_with_offset_and_limit() {
    let (_dir, col) = setup_let_collection();
    let mut params = HashMap::new();
    params.insert("v".to_string(), serde_json::json!([0.5, 0.5, 0.5, 0.3]));

    let results = col
        .execute_query_str(
            "LET s = similarity() \
             SELECT * FROM docs WHERE vector NEAR $v ORDER BY s DESC LIMIT 3 OFFSET 2",
            &params,
        )
        .expect("LET + OFFSET");

    assert_eq!(results.len(), 3);
}

/// LET + NOT similarity() returns explicit error (not silent wrong results).
#[test]
fn test_let_with_not_similarity_returns_error() {
    let (_dir, col) = setup_let_collection();
    let mut params = HashMap::new();
    params.insert("v".to_string(), serde_json::json!([0.5, 0.5, 0.5, 0.3]));

    let result = col.execute_query_str(
        "LET s = 1.0 SELECT * FROM docs WHERE NOT similarity(vector, $v) > 0.8 LIMIT 5",
        &params,
    );

    assert!(result.is_err());
    let msg = result.unwrap_err().to_string();
    assert!(
        msg.contains("NOT similarity()"),
        "expected NOT similarity() error, got: {msg}"
    );
}

/// LET + OR/union query returns explicit error.
#[test]
fn test_let_with_union_query_returns_error() {
    let (_dir, col) = setup_let_collection();
    let mut params = HashMap::new();
    params.insert("v".to_string(), serde_json::json!([0.5, 0.5, 0.5, 0.3]));

    let result = col.execute_query_str(
        "LET s = 1.0 SELECT * FROM docs \
         WHERE similarity(vector, $v) > 0.5 OR category = 'tech' LIMIT 5",
        &params,
    );

    assert!(result.is_err());
    let msg = result.unwrap_err().to_string();
    assert!(
        msg.contains("OR/union"),
        "expected OR/union error, got: {msg}"
    );
}

/// LET + WITH (mode='fast') combined.
#[test]
fn test_let_with_with_clause() {
    let (_dir, col) = setup_let_collection();
    let mut params = HashMap::new();
    params.insert("v".to_string(), serde_json::json!([0.5, 0.5, 0.5, 0.3]));

    let results = col
        .execute_query_str(
            "LET s = similarity() \
             SELECT * FROM docs WHERE vector NEAR $v ORDER BY s DESC LIMIT 5 WITH (mode='fast')",
            &params,
        )
        .expect("LET + WITH");

    assert_eq!(results.len(), 5);
}

// ============================================================================
// H. Unit tests for evaluate_let_bindings
// ============================================================================

/// Evaluate a single literal binding.
#[test]
fn test_evaluate_let_bindings_single_literal() {
    use crate::collection::search::query::ordering::evaluate_let_bindings;
    use crate::velesql::{ArithmeticExpr, LetBinding};

    let bindings = vec![LetBinding {
        name: "x".to_string(),
        expr: ArithmeticExpr::Literal(0.42),
    }];

    let result = evaluate_let_bindings(&bindings, 0.5, None, None);
    assert_eq!(result.len(), 1);
    assert_eq!(result[0].0, "x");
    assert!((result[0].1 - 0.42).abs() < 1e-5);
}

/// Chained: second binding references first.
#[test]
fn test_evaluate_let_bindings_chained() {
    use crate::collection::search::query::ordering::evaluate_let_bindings;
    use crate::velesql::{ArithmeticExpr, ArithmeticOp, LetBinding};

    let bindings = vec![
        LetBinding {
            name: "a".to_string(),
            expr: ArithmeticExpr::Literal(2.0),
        },
        LetBinding {
            name: "b".to_string(),
            expr: ArithmeticExpr::BinaryOp {
                left: Box::new(ArithmeticExpr::Variable("a".to_string())),
                op: ArithmeticOp::Mul,
                right: Box::new(ArithmeticExpr::Literal(3.0)),
            },
        },
    ];

    let result = evaluate_let_bindings(&bindings, 0.0, None, None);
    assert_eq!(result.len(), 2);
    assert!((result[0].1 - 2.0).abs() < 1e-5, "a = 2.0");
    assert!(
        (result[1].1 - 6.0).abs() < 1e-5,
        "b = a * 3.0 = 6.0, got {}",
        result[1].1
    );
}

/// LET binding referencing similarity() uses search_score.
#[test]
fn test_evaluate_let_bindings_similarity() {
    use crate::collection::search::query::ordering::evaluate_let_bindings;
    use crate::velesql::{ArithmeticExpr, LetBinding, OrderByExpr};

    let bindings = vec![LetBinding {
        name: "s".to_string(),
        expr: ArithmeticExpr::Similarity(Box::new(OrderByExpr::SimilarityBare)),
    }];

    let result = evaluate_let_bindings(&bindings, 0.77, None, None);
    assert!((result[0].1 - 0.77).abs() < 1e-5);
}

// ============================================================================
// I. FIX 2: LET with MATCH returns clear error
// ============================================================================

/// `LET x = 0.5 MATCH (a)-[r]->(b) RETURN a LIMIT 5` must return an error
/// explaining that LET bindings are not supported with MATCH queries.
/// Before the fix, MATCH dispatch happened before LET evaluation, silently
/// discarding the bindings.
#[test]
fn test_let_with_match_returns_clear_error() {
    use crate::collection::graph::GraphEdge;

    let (_dir, col) = setup_let_collection();

    // Add graph edges so MATCH has something to traverse.
    let edge1 = GraphEdge::new(1, 0, 1, "RELATED").expect("edge");
    let edge2 = GraphEdge::new(2, 1, 2, "RELATED").expect("edge");
    col.add_edge(edge1).expect("add edge");
    col.add_edge(edge2).expect("add edge");

    let params = HashMap::new();
    let result = col.execute_query_str(
        "LET x = 0.5 MATCH (a)-[r:RELATED]->(b) RETURN a LIMIT 5",
        &params,
    );

    // Must be an error, not silently ignored.
    assert!(result.is_err(), "LET + MATCH should return an error");
    let err_msg = result.unwrap_err().to_string();
    assert!(
        err_msg.contains("LET") && err_msg.contains("MATCH"),
        "Error should mention LET and MATCH, got: {err_msg}"
    );
}

/// MATCH without LET should still work fine (backward compat).
#[test]
fn test_match_without_let_still_works() {
    use crate::collection::graph::GraphEdge;

    let (_dir, col) = setup_let_collection();
    let edge = GraphEdge::new(1, 0, 1, "RELATED").expect("edge");
    col.add_edge(edge).expect("add edge");

    let params = HashMap::new();
    let result = col.execute_query_str("MATCH (a)-[r:RELATED]->(b) RETURN a LIMIT 5", &params);
    // Should succeed (no LET bindings).
    assert!(result.is_ok(), "MATCH without LET should succeed");
}

/// Empty bindings produce empty result.
#[test]
fn test_evaluate_let_bindings_empty() {
    use crate::collection::search::query::ordering::evaluate_let_bindings;

    let result = evaluate_let_bindings(&[], 0.5, None, None);
    assert!(result.is_empty());
}

// ============================================================================
// J. Issue #473: LET bindings appear in SELECT projection
// ============================================================================

/// `LET hybrid = 0.5 SELECT *, hybrid FROM col LIMIT 5`
/// Each result must have a "hybrid" field with value 0.5.
#[test]
fn test_let_binding_appears_in_select_projection() {
    let (_dir, col) = setup_let_collection();
    let mut params = HashMap::new();
    params.insert("v".to_string(), serde_json::json!([0.5, 0.5, 0.5, 0.3]));

    let results = col
        .execute_query_str(
            "LET hybrid = 0.5 \
             SELECT * FROM docs WHERE vector NEAR $v LIMIT 5",
            &params,
        )
        .expect("LET projection query");

    assert_eq!(results.len(), 5);
    for r in &results {
        let payload = r.point.payload.as_ref().expect("payload should exist");
        let hybrid_val = payload.get("hybrid").expect("hybrid field should exist");
        let v = hybrid_val.as_f64().expect("hybrid should be a number");
        assert!((v - 0.5).abs() < 1e-5, "hybrid should be 0.5, got {v}");
    }
}

/// LET binding overrides a payload field with the same name.
/// GIVEN: payload has field "idx" (set during setup)
/// WHEN: LET idx = 99.0 SELECT * FROM col LIMIT 5
/// THEN: "idx" in results is 99.0 (LET takes precedence)
#[test]
fn test_let_binding_overrides_payload_field() {
    let (_dir, col) = setup_let_collection();
    let mut params = HashMap::new();
    params.insert("v".to_string(), serde_json::json!([0.5, 0.5, 0.5, 0.3]));

    let results = col
        .execute_query_str(
            "LET idx = 99.0 \
             SELECT * FROM docs WHERE vector NEAR $v LIMIT 5",
            &params,
        )
        .expect("LET override query");

    assert_eq!(results.len(), 5);
    for r in &results {
        let payload = r.point.payload.as_ref().expect("payload should exist");
        let idx_val = payload.get("idx").expect("idx field should exist");
        let v = idx_val.as_f64().expect("idx should be a number");
        assert!(
            (v - 99.0).abs() < 1e-5,
            "idx should be 99.0 (LET override), got {v}"
        );
    }
}

/// Multiple LET bindings all appear in projection.
/// GIVEN: a collection with points
/// WHEN: LET a = 1.0 LET b = 2.0 SELECT * FROM col LIMIT 5
/// THEN: each result has "a" = 1.0 and "b" = 2.0
#[test]
fn test_multiple_let_bindings_in_projection() {
    let (_dir, col) = setup_let_collection();
    let mut params = HashMap::new();
    params.insert("v".to_string(), serde_json::json!([0.5, 0.5, 0.5, 0.3]));

    let results = col
        .execute_query_str(
            "LET a = 1.0 LET b = 2.0 \
             SELECT * FROM docs WHERE vector NEAR $v LIMIT 5",
            &params,
        )
        .expect("multiple LET query");

    assert_eq!(results.len(), 5);
    for r in &results {
        let payload = r.point.payload.as_ref().expect("payload should exist");
        let a_val = payload
            .get("a")
            .expect("a field should exist")
            .as_f64()
            .expect("a should be number");
        let b_val = payload
            .get("b")
            .expect("b field should exist")
            .as_f64()
            .expect("b should be number");
        assert!((a_val - 1.0).abs() < 1e-5, "a should be 1.0, got {a_val}");
        assert!((b_val - 2.0).abs() < 1e-5, "b should be 2.0, got {b_val}");
    }
}

/// LET binding with similarity() expression appears in projection with correct per-result values.
#[test]
fn test_let_similarity_binding_in_projection() {
    let (_dir, col) = setup_let_collection();
    let mut params = HashMap::new();
    params.insert("v".to_string(), serde_json::json!([0.5, 0.5, 0.5, 0.3]));

    let results = col
        .execute_query_str(
            "LET s = similarity() \
             SELECT * FROM docs WHERE vector NEAR $v ORDER BY s DESC LIMIT 5",
            &params,
        )
        .expect("LET similarity projection query");

    assert_eq!(results.len(), 5);
    for r in &results {
        let payload = r.point.payload.as_ref().expect("payload should exist");
        let s_val = payload
            .get("s")
            .expect("s field should exist")
            .as_f64()
            .expect("s should be number");
        // The similarity score should be positive and ≤ 1.0 for cosine.
        assert!(s_val > 0.0, "similarity should be positive, got {s_val}");
        assert!(
            s_val <= 1.0 + 1e-5,
            "similarity should be ≤ 1.0, got {s_val}"
        );
    }
}