velesdb-core 1.6.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
//! Integration tests for similarity() function execution.

#[cfg(test)]
mod tests {
    use crate::collection::types::Collection;
    use crate::distance::DistanceMetric;
    use crate::velesql::Parser;
    use crate::Point;
    use std::collections::HashMap;
    use std::path::PathBuf;

    fn create_test_collection_with_data() -> (Collection, tempfile::TempDir) {
        let temp_dir = tempfile::tempdir().unwrap();
        let path = PathBuf::from(temp_dir.path());
        let collection = Collection::create(path, 4, DistanceMetric::Cosine).unwrap();

        // Insert points with different vectors for similarity testing
        let points = vec![
            Point {
                id: 1,
                vector: vec![1.0, 0.0, 0.0, 0.0],
                payload: Some(serde_json::json!({"name": "point_1"})),
                sparse_vectors: None,
            },
            Point {
                id: 2,
                vector: vec![0.0, 1.0, 0.0, 0.0],
                payload: Some(serde_json::json!({"name": "point_2"})),
                sparse_vectors: None,
            },
            Point {
                id: 3,
                vector: vec![0.7, 0.7, 0.0, 0.0],
                payload: Some(serde_json::json!({"name": "point_3"})),
                sparse_vectors: None,
            },
            Point {
                id: 4,
                vector: vec![0.5, 0.5, 0.5, 0.5],
                payload: Some(serde_json::json!({"name": "point_4"})),
                sparse_vectors: None,
            },
            Point {
                id: 5,
                vector: vec![0.9, 0.1, 0.0, 0.0],
                payload: Some(serde_json::json!({"name": "point_5"})),
                sparse_vectors: None,
            },
        ];

        collection.upsert(points).unwrap();
        (collection, temp_dir)
    }

    #[test]
    fn test_similarity_greater_than_threshold() {
        let (collection, _temp) = create_test_collection_with_data();

        // Query: find points with similarity > 0.8 to [1, 0, 0, 0]
        let query = "SELECT * FROM test_similarity WHERE similarity(vector, $v) > 0.8 LIMIT 10";
        let parsed = Parser::parse(query).unwrap();

        let mut params = HashMap::new();
        params.insert("v".to_string(), serde_json::json!([1.0, 0.0, 0.0, 0.0]));

        let results = collection.execute_query(&parsed, &params).unwrap();

        // Points 1 (identical), 3, 5 should have high similarity
        assert!(
            !results.is_empty(),
            "Should return results with high similarity"
        );

        // Verify all returned results have score > 0.8
        for r in &results {
            assert!(r.score > 0.8, "Score {} should be > 0.8", r.score);
        }
    }

    #[test]
    fn test_similarity_greater_than_or_equal() {
        let (collection, _temp) = create_test_collection_with_data();

        let query = "SELECT * FROM test_similarity WHERE similarity(vector, $v) >= 0.99 LIMIT 10";
        let parsed = Parser::parse(query).unwrap();

        let mut params = HashMap::new();
        params.insert("v".to_string(), serde_json::json!([1.0, 0.0, 0.0, 0.0]));

        let results = collection.execute_query(&parsed, &params).unwrap();

        // Only point 1 (identical) should have similarity >= 0.99
        for r in &results {
            assert!(r.score >= 0.99, "Score {} should be >= 0.99", r.score);
        }
    }

    #[test]
    fn test_similarity_less_than_threshold() {
        let (collection, _temp) = create_test_collection_with_data();

        let query = "SELECT * FROM test_similarity WHERE similarity(vector, $v) < 0.5 LIMIT 10";
        let parsed = Parser::parse(query).unwrap();

        let mut params = HashMap::new();
        params.insert("v".to_string(), serde_json::json!([1.0, 0.0, 0.0, 0.0]));

        let results = collection.execute_query(&parsed, &params).unwrap();

        // All returned results should have score < 0.5
        for r in &results {
            assert!(r.score < 0.5, "Score {} should be < 0.5", r.score);
        }
    }

    #[test]
    fn test_similarity_with_literal_vector() {
        let (collection, _temp) = create_test_collection_with_data();

        // Query with literal vector instead of parameter
        let query =
            "SELECT * FROM test_similarity WHERE similarity(vector, [1.0, 0.0, 0.0, 0.0]) > 0.9 LIMIT 10";
        let parsed = Parser::parse(query).unwrap();

        let params = HashMap::new();
        let results = collection.execute_query(&parsed, &params).unwrap();

        // Should find points with high similarity to [1, 0, 0, 0]
        for r in &results {
            assert!(r.score > 0.9, "Score {} should be > 0.9", r.score);
        }
    }

    #[test]
    fn test_similarity_no_results_when_threshold_too_high() {
        let (collection, _temp) = create_test_collection_with_data();

        // No point has similarity > 1.5 (impossible for normalized vectors)
        let query = "SELECT * FROM test_similarity WHERE similarity(vector, $v) > 1.5 LIMIT 10";
        let parsed = Parser::parse(query).unwrap();

        let mut params = HashMap::new();
        params.insert("v".to_string(), serde_json::json!([1.0, 0.0, 0.0, 0.0]));

        let results = collection.execute_query(&parsed, &params).unwrap();
        assert!(
            results.is_empty(),
            "Should return no results for impossible threshold"
        );
    }

    #[test]
    fn test_similarity_missing_parameter_error() {
        let (collection, _temp) = create_test_collection_with_data();

        let query =
            "SELECT * FROM test_similarity WHERE similarity(vector, $missing) > 0.8 LIMIT 10";
        let parsed = Parser::parse(query).unwrap();

        let params = HashMap::new(); // Empty params - $missing not provided
        let result = collection.execute_query(&parsed, &params);

        assert!(result.is_err(), "Should error on missing parameter");
    }

    /// Regression test: similarity() with additional filter conditions.
    ///
    /// Bug: similarity() queries were ignoring additional filter conditions
    /// in the WHERE clause (e.g., `AND category = 'tech'`).
    #[test]
    fn test_similarity_with_metadata_filter_applied() {
        let temp_dir = tempfile::tempdir().unwrap();
        let path = PathBuf::from(temp_dir.path());
        let collection = Collection::create(path, 4, DistanceMetric::Cosine).unwrap();

        // Insert points with category metadata
        let points = vec![
            Point {
                id: 1,
                vector: vec![1.0, 0.0, 0.0, 0.0],
                payload: Some(serde_json::json!({"category": "tech", "name": "tech_1"})),
                sparse_vectors: None,
            },
            Point {
                id: 2,
                vector: vec![0.95, 0.05, 0.0, 0.0], // Very similar to query vector
                payload: Some(serde_json::json!({"category": "sports", "name": "sports_1"})),
                sparse_vectors: None,
            },
            Point {
                id: 3,
                vector: vec![0.9, 0.1, 0.0, 0.0], // Also similar
                payload: Some(serde_json::json!({"category": "tech", "name": "tech_2"})),
                sparse_vectors: None,
            },
            Point {
                id: 4,
                vector: vec![0.5, 0.5, 0.5, 0.5], // Less similar
                payload: Some(serde_json::json!({"category": "tech", "name": "tech_3"})),
                sparse_vectors: None,
            },
        ];

        collection.upsert(points).unwrap();

        // Query: similarity > 0.8 AND category = 'tech'
        // Should only return tech items with high similarity (id=1, id=3)
        // Should NOT return id=2 (sports) even though it has high similarity
        let query =
            "SELECT * FROM test WHERE similarity(vector, $v) > 0.8 AND category = 'tech' LIMIT 10";
        let parsed = Parser::parse(query).unwrap();

        let mut params = HashMap::new();
        params.insert("v".to_string(), serde_json::json!([1.0, 0.0, 0.0, 0.0]));

        let results = collection.execute_query(&parsed, &params).unwrap();

        // All results should be category = 'tech'
        for result in &results {
            let payload = result.point.payload.as_ref().expect("Should have payload");
            let category = payload.get("category").and_then(|v| v.as_str());
            assert_eq!(
                category,
                Some("tech"),
                "All results should have category='tech', but got {:?} for id={}",
                category,
                result.point.id
            );
        }

        // id=2 (sports) should NOT be in results even though it has high similarity
        let ids: Vec<u64> = results.iter().map(|r| r.point.id).collect();
        assert!(
            !ids.contains(&2),
            "id=2 (sports) should be filtered out, but got ids: {:?}",
            ids
        );
    }

    // =========================================================================
    // ORDER BY similarity() Tests (EPIC-008 US-008)
    // =========================================================================

    #[test]
    fn test_order_by_similarity_desc() {
        let (collection, _temp_dir) = create_test_collection_with_data();

        // Query with ORDER BY similarity DESC (highest first)
        let query = "SELECT * FROM test ORDER BY similarity(vector, $v) DESC LIMIT 5";
        let parsed = Parser::parse(query).unwrap();

        let mut params = HashMap::new();
        params.insert("v".to_string(), serde_json::json!([1.0, 0.0, 0.0, 0.0]));

        let results = collection.execute_query(&parsed, &params).unwrap();

        // Verify results are sorted by similarity DESC
        assert!(!results.is_empty(), "Should return results");

        for i in 1..results.len() {
            assert!(
                results[i - 1].score >= results[i].score,
                "Results should be sorted DESC: {} >= {} at position {}",
                results[i - 1].score,
                results[i].score,
                i
            );
        }

        // First result should be the most similar (point 1 with [1,0,0,0])
        assert_eq!(results[0].point.id, 1, "Most similar point should be first");
    }

    #[test]
    fn test_order_by_similarity_asc() {
        let (collection, _temp_dir) = create_test_collection_with_data();

        // Query with ORDER BY similarity ASC (lowest first)
        let query = "SELECT * FROM test ORDER BY similarity(vector, $v) ASC LIMIT 5";
        let parsed = Parser::parse(query).unwrap();

        let mut params = HashMap::new();
        params.insert("v".to_string(), serde_json::json!([1.0, 0.0, 0.0, 0.0]));

        let results = collection.execute_query(&parsed, &params).unwrap();

        // Verify results are sorted by similarity ASC
        assert!(!results.is_empty(), "Should return results");

        for i in 1..results.len() {
            assert!(
                results[i - 1].score <= results[i].score,
                "Results should be sorted ASC: {} <= {} at position {}",
                results[i - 1].score,
                results[i].score,
                i
            );
        }
    }

    #[test]
    fn test_order_by_similarity_with_filter() {
        let temp_dir = tempfile::tempdir().unwrap();
        let path = PathBuf::from(temp_dir.path());
        let collection = Collection::create(path, 4, DistanceMetric::Cosine).unwrap();

        // Insert points with category metadata
        let points = vec![
            Point {
                id: 1,
                vector: vec![1.0, 0.0, 0.0, 0.0],
                payload: Some(serde_json::json!({"category": "tech", "name": "tech_1"})),
                sparse_vectors: None,
            },
            Point {
                id: 2,
                vector: vec![0.9, 0.1, 0.0, 0.0],
                payload: Some(serde_json::json!({"category": "tech", "name": "tech_2"})),
                sparse_vectors: None,
            },
            Point {
                id: 3,
                vector: vec![0.8, 0.2, 0.0, 0.0],
                payload: Some(serde_json::json!({"category": "sports", "name": "sports_1"})),
                sparse_vectors: None,
            },
        ];
        collection.upsert(points).unwrap();

        // Query with WHERE filter AND ORDER BY similarity
        let query =
            "SELECT * FROM test WHERE category = 'tech' ORDER BY similarity(vector, $v) DESC LIMIT 10";
        let parsed = Parser::parse(query).unwrap();

        let mut params = HashMap::new();
        params.insert("v".to_string(), serde_json::json!([1.0, 0.0, 0.0, 0.0]));

        let results = collection.execute_query(&parsed, &params).unwrap();

        // All results should be category = 'tech' AND sorted by similarity
        for result in &results {
            let payload = result.point.payload.as_ref().expect("Should have payload");
            let category = payload
                .get("category")
                .and_then(|v: &serde_json::Value| v.as_str());
            assert_eq!(
                category,
                Some("tech"),
                "All results should have category='tech'"
            );
        }

        // Verify sorted DESC
        for i in 1..results.len() {
            assert!(
                results[i - 1].score >= results[i].score,
                "Results should be sorted DESC"
            );
        }
    }

    // =========================================================================
    // Issue #122: Union query with nested AND/OR
    // =========================================================================

    /// Regression test for Issue #122:
    /// Union query fails to apply outer AND filters with nested OR.
    ///
    /// Pattern: `(similarity() OR metadata1) AND metadata2`
    /// Bug: The outer AND filter (metadata2) was not applied to metadata-only results.
    #[test]
    fn test_issue_122_union_query_nested_and_or() {
        let temp_dir = tempfile::tempdir().unwrap();
        let path = PathBuf::from(temp_dir.path());
        let collection = Collection::create(path, 4, DistanceMetric::Cosine).unwrap();

        // Insert test data with category and status fields
        let points = vec![
            Point {
                id: 1,
                vector: vec![1.0, 0.0, 0.0, 0.0], // High similarity to query
                payload: Some(serde_json::json!({
                    "category": "tech",
                    "status": "active"
                })),
                sparse_vectors: None,
            },
            Point {
                id: 2,
                vector: vec![0.0, 1.0, 0.0, 0.0], // Low similarity
                payload: Some(serde_json::json!({
                    "category": "tech",
                    "status": "active"  // Matches metadata AND outer filter
                })),
                sparse_vectors: None,
            },
            Point {
                id: 3,
                vector: vec![0.0, 0.0, 1.0, 0.0], // Low similarity
                payload: Some(serde_json::json!({
                    "category": "tech",
                    "status": "inactive"  // Does NOT match outer filter
                })),
                sparse_vectors: None,
            },
            Point {
                id: 4,
                vector: vec![0.9, 0.1, 0.0, 0.0], // High similarity
                payload: Some(serde_json::json!({
                    "category": "sports",
                    "status": "inactive"  // Does NOT match outer filter
                })),
                sparse_vectors: None,
            },
        ];

        collection.upsert(points).unwrap();

        // Query: (similarity > 0.8 OR category = 'tech') AND status = 'active'
        // Expected results:
        // - id=1: High similarity AND status='active' ✓
        // - id=2: category='tech' AND status='active' ✓
        // - id=3: category='tech' BUT status='inactive' ✗ (outer AND not satisfied)
        // - id=4: High similarity BUT status='inactive' ✗ (outer AND not satisfied)
        let query = "SELECT * FROM test WHERE (similarity(vector, $v) > 0.8 OR category = 'tech') AND status = 'active' LIMIT 10";
        let parsed = Parser::parse(query).unwrap();

        let mut params = HashMap::new();
        params.insert("v".to_string(), serde_json::json!([1.0, 0.0, 0.0, 0.0]));

        let results = collection.execute_query(&parsed, &params).unwrap();

        // All results MUST have status = 'active'
        for result in &results {
            let payload = result.point.payload.as_ref().expect("Should have payload");
            let status = payload.get("status").and_then(|v| v.as_str());
            assert_eq!(
                status,
                Some("active"),
                "Issue #122: All results must satisfy outer AND filter (status='active'), but id={} has status={:?}",
                result.point.id,
                status
            );
        }

        // Specifically, id=3 and id=4 should NOT be in results
        let ids: Vec<u64> = results.iter().map(|r| r.point.id).collect();
        assert!(
            !ids.contains(&3),
            "Issue #122: id=3 has status='inactive', should be filtered by outer AND, but got ids: {:?}",
            ids
        );
        assert!(
            !ids.contains(&4),
            "Issue #122: id=4 has status='inactive', should be filtered by outer AND, but got ids: {:?}",
            ids
        );

        // id=1 and id=2 should be in results
        assert!(
            ids.contains(&1) || ids.contains(&2),
            "Should have at least one result matching the criteria, got ids: {:?}",
            ids
        );
    }
}