velesdb-core 5.0.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
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
//! BDD-style end-to-end tests for `VelesQL` operators and statements that are
//! **not** covered by the `where_filters` or `vector_search` suites.
//!
//! Coverage: UPDATE, comparison operators (`<`, `<=`, `>=`, `!=`, `<>`),
//! NOT operator, ILIKE, LIKE edge cases, string escaping, and SQL comments.
//!
//! Each scenario follows GIVEN (setup data) -> WHEN (execute SQL) -> THEN
//! (verify results). Tests exercise the **full pipeline**: SQL string ->
//! `Parser::parse()` -> `Database::execute_query()` -> verify state.

use std::collections::HashSet;

use serde_json::json;
use velesdb_core::{Database, Point};

use super::helpers::{create_test_db, execute_sql, result_ids};

// =========================================================================
// Module-specific setup
// =========================================================================

/// Populate an `items` collection with diverse test data.
///
/// | id | name      | price | status   | category |
/// |----|-----------|-------|----------|----------|
/// | 1  | O'Brien   | 10    | active   | A        |
/// | 2  | Alice     | 20    | active   | B        |
/// | 3  | Bob       | 30    | deleted  | A        |
/// | 4  | CHARLIE   | 40    | pending  | B        |
/// | 5  | alice     | 50    | active   | C        |
fn setup_items(db: &Database) {
    execute_sql(
        db,
        "CREATE COLLECTION items (dimension = 4, metric = 'cosine');",
    )
    .expect("test: CREATE items");

    let vc = db
        .get_vector_collection("items")
        .expect("test: get items collection");

    vc.upsert(vec![
        Point::new(
            1,
            vec![1.0, 0.0, 0.0, 0.0],
            Some(json!({"name": "O'Brien", "price": 10, "status": "active", "category": "A"})),
        ),
        Point::new(
            2,
            vec![0.0, 1.0, 0.0, 0.0],
            Some(json!({"name": "Alice", "price": 20, "status": "active", "category": "B"})),
        ),
        Point::new(
            3,
            vec![0.0, 0.0, 1.0, 0.0],
            Some(json!({"name": "Bob", "price": 30, "status": "deleted", "category": "A"})),
        ),
        Point::new(
            4,
            vec![0.0, 0.0, 0.0, 1.0],
            Some(json!({"name": "CHARLIE", "price": 40, "status": "pending", "category": "B"})),
        ),
        Point::new(
            5,
            vec![0.5, 0.5, 0.0, 0.0],
            Some(json!({"name": "alice", "price": 50, "status": "active", "category": "C"})),
        ),
    ])
    .expect("test: upsert items");
}

// =========================================================================
// UPDATE: single field
// =========================================================================

/// GIVEN items, WHEN UPDATE sets status to 'archived' WHERE id = 1,
/// THEN direct get confirms the field changed.
#[test]
fn test_update_changes_field_value() {
    let (_dir, db) = create_test_db();
    setup_items(&db);

    execute_sql(&db, "UPDATE items SET status = 'archived' WHERE id = 1;")
        .expect("test: UPDATE single field");

    let vc = db.get_vector_collection("items").expect("test: get items");
    let points = vc.get(&[1]);
    let point = points[0].as_ref().expect("test: point 1 should exist");
    let status = point
        .payload
        .as_ref()
        .and_then(|p| p.get("status"))
        .and_then(serde_json::Value::as_str);
    assert_eq!(
        status,
        Some("archived"),
        "status should be 'archived' after UPDATE"
    );
}

// =========================================================================
// UPDATE: multiple fields
// =========================================================================

/// GIVEN items, WHEN UPDATE sets status='sold' and price=0 WHERE id = 3,
/// THEN both fields reflect the new values.
#[test]
fn test_update_multiple_fields() {
    let (_dir, db) = create_test_db();
    setup_items(&db);

    execute_sql(
        &db,
        "UPDATE items SET status = 'sold', price = 0 WHERE id = 3;",
    )
    .expect("test: UPDATE multiple fields");

    let vc = db.get_vector_collection("items").expect("test: get items");
    let points = vc.get(&[3]);
    let point = points[0].as_ref().expect("test: point 3 should exist");
    let payload = point.payload.as_ref().expect("test: payload should exist");

    assert_eq!(
        payload.get("status").and_then(serde_json::Value::as_str),
        Some("sold"),
        "status should be 'sold'"
    );
    assert_eq!(
        payload.get("price").and_then(serde_json::Value::as_i64),
        Some(0),
        "price should be 0"
    );
}

// =========================================================================
// UPDATE: no matching rows (no-op)
// =========================================================================

/// GIVEN items, WHEN UPDATE targets a nonexistent id,
/// THEN no error occurs and other items are unchanged.
#[test]
fn test_update_with_no_matching_rows() {
    let (_dir, db) = create_test_db();
    setup_items(&db);

    let result = execute_sql(&db, "UPDATE items SET status = 'x' WHERE id = 999;");
    assert!(result.is_ok(), "UPDATE with no matches should not error");

    // Verify no item was touched
    let all = execute_sql(&db, "SELECT * FROM items LIMIT 10;")
        .expect("test: SELECT all after no-op UPDATE");
    assert_eq!(all.len(), 5, "All 5 items should still exist");

    for r in &all {
        let status = r
            .point
            .payload
            .as_ref()
            .and_then(|p| p.get("status"))
            .and_then(serde_json::Value::as_str)
            .unwrap_or("");
        assert_ne!(
            status, "x",
            "No item should have status='x', but id={} does",
            r.point.id
        );
    }
}

// =========================================================================
// UPDATE: without WHERE updates all rows
// =========================================================================

/// GIVEN items, WHEN UPDATE omits WHERE, THEN all 5 items are updated.
#[test]
fn test_update_without_where_updates_all() {
    let (_dir, db) = create_test_db();
    setup_items(&db);

    execute_sql(&db, "UPDATE items SET category = 'X';").expect("test: UPDATE all rows");

    let all = execute_sql(&db, "SELECT * FROM items LIMIT 10;")
        .expect("test: SELECT all after blanket UPDATE");
    assert_eq!(all.len(), 5, "All 5 items should exist");

    for r in &all {
        let cat = r
            .point
            .payload
            .as_ref()
            .and_then(|p| p.get("category"))
            .and_then(serde_json::Value::as_str);
        assert_eq!(
            cat,
            Some("X"),
            "Every item should have category='X', but id={} has {:?}",
            r.point.id,
            cat
        );
    }
}

// =========================================================================
// Comparison: less than (<)
// =========================================================================

/// GIVEN items, WHEN WHERE price < 25, THEN ids {1,2} match.
#[test]
fn test_where_less_than() {
    let (_dir, db) = create_test_db();
    setup_items(&db);

    let results = execute_sql(&db, "SELECT * FROM items WHERE price < 25 LIMIT 10;")
        .expect("test: less-than filter");

    let ids = result_ids(&results);
    let expected: HashSet<u64> = [1, 2].into_iter().collect();
    assert_eq!(ids, expected, "price < 25 should match ids {{1, 2}}");
}

// =========================================================================
// Comparison: less than or equal (<=)
// =========================================================================

/// GIVEN items, WHEN WHERE price <= 20, THEN ids {1,2} match.
#[test]
fn test_where_less_than_or_equal() {
    let (_dir, db) = create_test_db();
    setup_items(&db);

    let results = execute_sql(&db, "SELECT * FROM items WHERE price <= 20 LIMIT 10;")
        .expect("test: less-than-or-equal filter");

    let ids = result_ids(&results);
    let expected: HashSet<u64> = [1, 2].into_iter().collect();
    assert_eq!(ids, expected, "price <= 20 should match ids {{1, 2}}");
}

// =========================================================================
// Comparison: greater than or equal (>=)
// =========================================================================

/// GIVEN items, WHEN WHERE price >= 40, THEN ids {4,5} match.
#[test]
fn test_where_greater_than_or_equal() {
    let (_dir, db) = create_test_db();
    setup_items(&db);

    let results = execute_sql(&db, "SELECT * FROM items WHERE price >= 40 LIMIT 10;")
        .expect("test: greater-than-or-equal filter");

    let ids = result_ids(&results);
    let expected: HashSet<u64> = [4, 5].into_iter().collect();
    assert_eq!(ids, expected, "price >= 40 should match ids {{4, 5}}");
}

// =========================================================================
// Comparison: not equal (!=)
// =========================================================================

/// GIVEN items, WHEN WHERE status != 'active', THEN ids {3,4} match.
#[test]
fn test_where_not_equal() {
    let (_dir, db) = create_test_db();
    setup_items(&db);

    let results = execute_sql(
        &db,
        "SELECT * FROM items WHERE status != 'active' LIMIT 10;",
    )
    .expect("test: not-equal filter");

    let ids = result_ids(&results);
    let expected: HashSet<u64> = [3, 4].into_iter().collect();
    assert_eq!(
        ids, expected,
        "status != 'active' should match ids {{3, 4}} (deleted, pending)"
    );
}

// =========================================================================
// NOT operator: negates condition
// =========================================================================

/// GIVEN items, WHEN WHERE NOT (status = 'active'), THEN ids {3,4} match.
#[test]
fn test_where_not_negates_condition() {
    let (_dir, db) = create_test_db();
    setup_items(&db);

    let results = execute_sql(
        &db,
        "SELECT * FROM items WHERE NOT (status = 'active') LIMIT 10;",
    )
    .expect("test: NOT condition filter");

    let ids = result_ids(&results);
    let expected: HashSet<u64> = [3, 4].into_iter().collect();
    assert_eq!(
        ids, expected,
        "NOT (status = 'active') should match ids {{3, 4}}"
    );
}

// =========================================================================
// NOT operator: with IN
// =========================================================================

/// GIVEN items, WHEN WHERE NOT (category IN ('A', 'B')), THEN id {5} matches.
#[test]
fn test_where_not_with_in() {
    let (_dir, db) = create_test_db();
    setup_items(&db);

    let results = execute_sql(
        &db,
        "SELECT * FROM items WHERE NOT (category IN ('A', 'B')) LIMIT 10;",
    )
    .expect("test: NOT IN filter");

    let ids = result_ids(&results);
    let expected: HashSet<u64> = [5].into_iter().collect();
    assert_eq!(
        ids, expected,
        "NOT (category IN ('A', 'B')) should match only id {{5}} (category C)"
    );
}

// =========================================================================
// ILIKE: case-insensitive exact match
// =========================================================================

/// GIVEN items, WHEN WHERE name ILIKE 'alice', THEN ids {2,5} match.
#[test]
fn test_ilike_case_insensitive_match() {
    let (_dir, db) = create_test_db();
    setup_items(&db);

    let results = execute_sql(
        &db,
        "SELECT * FROM items WHERE name ILIKE 'alice' LIMIT 10;",
    )
    .expect("test: ILIKE exact case-insensitive");

    let ids = result_ids(&results);
    let expected: HashSet<u64> = [2, 5].into_iter().collect();
    assert_eq!(
        ids, expected,
        "ILIKE 'alice' should match both Alice (id=2) and alice (id=5)"
    );
}

// =========================================================================
// ILIKE: pattern match with wildcards
// =========================================================================

/// GIVEN items, WHEN WHERE name ILIKE '%LI%', THEN ids {2,4,5} match.
#[test]
fn test_ilike_pattern_match() {
    let (_dir, db) = create_test_db();
    setup_items(&db);

    let results = execute_sql(&db, "SELECT * FROM items WHERE name ILIKE '%LI%' LIMIT 10;")
        .expect("test: ILIKE pattern case-insensitive");

    let ids = result_ids(&results);
    let expected: HashSet<u64> = [2, 4, 5].into_iter().collect();
    assert_eq!(
        ids, expected,
        "ILIKE '%LI%' should match Alice (id=2), CHARLIE (id=4), alice (id=5)"
    );
}

// =========================================================================
// LIKE: case-sensitive exact match
// =========================================================================

/// GIVEN items, WHEN WHERE name LIKE 'alice', THEN only id 5 matches.
#[test]
fn test_like_is_case_sensitive() {
    let (_dir, db) = create_test_db();
    setup_items(&db);

    let results = execute_sql(&db, "SELECT * FROM items WHERE name LIKE 'alice' LIMIT 10;")
        .expect("test: LIKE case-sensitive exact");

    let ids = result_ids(&results);
    let expected: HashSet<u64> = [5].into_iter().collect();
    assert_eq!(
        ids, expected,
        "LIKE 'alice' (case-sensitive) should match only lowercase alice (id=5)"
    );
}

// =========================================================================
// LIKE: underscore wildcard
// =========================================================================

/// GIVEN items, WHEN WHERE name LIKE 'Bo_', THEN id 3 (Bob) matches.
#[test]
fn test_like_wildcard_underscore() {
    let (_dir, db) = create_test_db();
    setup_items(&db);

    let results = execute_sql(&db, "SELECT * FROM items WHERE name LIKE 'Bo_' LIMIT 10;")
        .expect("test: LIKE underscore wildcard");

    let ids = result_ids(&results);
    let expected: HashSet<u64> = [3].into_iter().collect();
    assert_eq!(ids, expected, "LIKE 'Bo_' should match only Bob (id=3)");
}

// =========================================================================
// String escaping: single-quote in value
// =========================================================================

/// GIVEN items with name "O'Brien" (id=1),
/// WHEN WHERE name = 'O''Brien', THEN id 1 matches.
#[test]
fn test_string_escaping_obrien() {
    let (_dir, db) = create_test_db();
    setup_items(&db);

    let results = execute_sql(&db, "SELECT * FROM items WHERE name = 'O''Brien' LIMIT 10;")
        .expect("test: string escaping with doubled single quote");

    let ids = result_ids(&results);
    let expected: HashSet<u64> = [1].into_iter().collect();
    assert_eq!(ids, expected, "Escaped O''Brien should match id 1");
}

// =========================================================================
// Comments: line comment is ignored
// =========================================================================

/// GIVEN items, WHEN query contains a line comment (--),
/// THEN the comment is ignored and all 5 items are returned.
#[test]
fn test_comment_ignored_in_query() {
    let (_dir, db) = create_test_db();
    setup_items(&db);

    let results = execute_sql(&db, "SELECT * FROM items -- get all\nLIMIT 10;")
        .expect("test: line comment should be ignored by parser");

    assert_eq!(
        results.len(),
        5,
        "Comment should be stripped; all 5 items should be returned"
    );
}

// =========================================================================
// Negative: UPDATE nonexistent collection
// =========================================================================

/// GIVEN no collection named 'ghost',
/// WHEN UPDATE ghost SET x = 1 WHERE id = 1,
/// THEN an error mentioning the collection is returned.
#[test]
fn test_update_nonexistent_collection_fails() {
    let (_dir, db) = create_test_db();

    let err = execute_sql(&db, "UPDATE ghost SET x = 1 WHERE id = 1;")
        .expect_err("UPDATE on nonexistent collection should fail");
    let msg = err.to_string();
    assert!(
        msg.contains("ghost") || msg.contains("not found") || msg.contains("NotFound"),
        "Error should mention the missing collection, got: {msg}"
    );
}

// =========================================================================
// Comparison: diamond not-equal (<>)
// =========================================================================

/// GIVEN items, WHEN WHERE status <> 'active',
/// THEN same result as != (ids {3,4}).
#[test]
fn test_where_not_equal_diamond_syntax() {
    let (_dir, db) = create_test_db();
    setup_items(&db);

    let results = execute_sql(
        &db,
        "SELECT * FROM items WHERE status <> 'active' LIMIT 10;",
    )
    .expect("test: diamond not-equal filter");

    let ids = result_ids(&results);
    let expected: HashSet<u64> = [3, 4].into_iter().collect();
    assert_eq!(
        ids, expected,
        "status <> 'active' should match ids {{3, 4}}, same as !="
    );
}