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
//! BDD tests for cross-collection JOIN optimization (Issue #513).
//!
//! Validates filter pushdown, lookup join, and combined optimization paths.
//! Reuses `setup_cross_type_collections` from `cross_collection.rs`.

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

use super::helpers::{
    create_test_db, execute_sql, execute_sql_with_params, payload_f64, payload_str, vector_param,
};

// =========================================================================
// Helpers
// =========================================================================

/// Creates `products` (VectorCollection) and `inventory` (MetadataCollection).
/// Same data as `cross_collection::setup_cross_type_collections`.
fn setup_collections(db: &Database) {
    execute_sql(
        db,
        "CREATE COLLECTION products (dimension = 4, metric = 'cosine');",
    )
    .expect("CREATE products");

    let products = db.get_vector_collection("products").expect("get products");
    products
        .upsert(vec![
            Point::new(
                1,
                vec![1.0, 0.0, 0.0, 0.0],
                Some(json!({"name": "Headphones", "category": "audio"})),
            ),
            Point::new(
                2,
                vec![0.0, 1.0, 0.0, 0.0],
                Some(json!({"name": "Keyboard", "category": "input"})),
            ),
            Point::new(
                3,
                vec![0.0, 0.0, 1.0, 0.0],
                Some(json!({"name": "Monitor", "category": "display"})),
            ),
            Point::new(
                4,
                vec![0.0, 0.0, 0.0, 1.0],
                Some(json!({"name": "Mouse", "category": "input"})),
            ),
            Point::new(
                5,
                vec![0.7, 0.7, 0.0, 0.0],
                Some(json!({"name": "Speakers", "category": "audio"})),
            ),
        ])
        .expect("upsert products");

    execute_sql(db, "CREATE METADATA COLLECTION inventory;").expect("CREATE inventory");

    let inventory = db
        .get_metadata_collection("inventory")
        .expect("get inventory");
    inventory
        .upsert(vec![
            Point::metadata_only(1, json!({"product_id": 1, "price": 99.99, "stock": 50})),
            Point::metadata_only(2, json!({"product_id": 2, "price": 149.99, "stock": 0})),
            Point::metadata_only(3, json!({"product_id": 3, "price": 399.99, "stock": 12})),
            Point::metadata_only(4, json!({"product_id": 4, "price": 29.99, "stock": 200})),
            Point::metadata_only(5, json!({"product_id": 5, "price": 79.99, "stock": 30})),
        ])
        .expect("upsert inventory");
}

// =========================================================================
// Nominal: Filter pushdown
// =========================================================================

/// GIVEN products JOIN inventory with WHERE filter on joined table
/// WHEN the query filters `inventory.price > 100`
/// THEN only rows matching the filter are returned.
#[test]
fn test_join_with_where_filter_on_joined_table_returns_matching_rows() {
    let (_dir, db) = create_test_db();
    setup_collections(&db);

    let sql = "SELECT * FROM products \
               JOIN inventory ON products.id = inventory.id \
               WHERE inventory.price > 100 \
               LIMIT 10";

    let results = execute_sql(&db, sql).expect("filter pushdown JOIN");

    // Only Keyboard (149.99) and Monitor (399.99) have price > 100
    assert_eq!(results.len(), 2, "should return 2 rows with price > 100");
    for r in &results {
        let price = payload_f64(r, "price").expect("price field");
        assert!(price > 100.0, "price {price} should be > 100");
    }
}

// =========================================================================
// Nominal: Lookup join on primary key
// =========================================================================

/// GIVEN products JOIN inventory ON primary key
/// WHEN the query joins on `products.id = inventory.id`
/// THEN correct results are returned via lookup path.
#[test]
fn test_join_on_primary_key_returns_correct_results() {
    let (_dir, db) = create_test_db();
    setup_collections(&db);

    let sql = "SELECT * FROM products \
               JOIN inventory ON products.id = inventory.id \
               LIMIT 10";

    let results = execute_sql(&db, sql).expect("lookup join");

    assert_eq!(
        results.len(),
        5,
        "all 5 products should join with inventory"
    );
    // Verify merged payload contains fields from both collections
    let first = results.iter().find(|r| r.point.id == 1).expect("id=1");
    assert_eq!(payload_str(first, "name"), Some("Headphones"));
    assert!(
        payload_f64(first, "price").is_some(),
        "should have price from inventory"
    );
}

// =========================================================================
// Nominal: Pushdown + vector search
// =========================================================================

/// GIVEN products JOIN inventory with NEAR search and filter on joined table
/// WHEN the query combines vector search with inventory filter
/// THEN results are filtered and ordered by similarity.
#[test]
fn test_join_with_near_and_filter_on_joined_table() {
    let (_dir, db) = create_test_db();
    setup_collections(&db);

    let sql = "SELECT * FROM products \
               JOIN inventory ON products.id = inventory.id \
               WHERE vector NEAR $v AND inventory.stock > 0 \
               LIMIT 5";

    let params = vector_param(&[1.0, 0.0, 0.0, 0.0]);
    let results = execute_sql_with_params(&db, sql, &params).expect("NEAR + pushdown");

    assert!(!results.is_empty(), "should return results");
    // Keyboard (stock=0) should be excluded
    for r in &results {
        let stock = payload_f64(r, "stock").expect("stock field");
        assert!(stock > 0.0, "stock {stock} should be > 0");
    }
}

// =========================================================================
// Edge: Filter eliminates all rows — INNER JOIN
// =========================================================================

/// GIVEN products JOIN inventory with filter that matches no rows
/// WHEN the join type is INNER
/// THEN the result set is empty.
#[test]
fn test_join_filter_eliminates_all_rows_inner_returns_empty() {
    let (_dir, db) = create_test_db();
    setup_collections(&db);

    let sql = "SELECT * FROM products \
               JOIN inventory ON products.id = inventory.id \
               WHERE inventory.price > 99999 \
               LIMIT 10";

    let results = execute_sql(&db, sql).expect("filter eliminates all");
    assert!(
        results.is_empty(),
        "INNER JOIN with no matches should be empty"
    );
}

// =========================================================================
// Edge: Filter eliminates all rows — LEFT JOIN
// =========================================================================

/// GIVEN products LEFT JOIN inventory with filter that matches no rows
/// WHEN the join type is LEFT
/// THEN left-side results are returned with null joined columns.
#[test]
fn test_join_filter_eliminates_all_rows_left_returns_nulls() {
    let (_dir, db) = create_test_db();
    setup_collections(&db);

    let sql = "SELECT * FROM products \
               LEFT JOIN inventory ON products.id = inventory.id \
               WHERE inventory.price > 99999 \
               LIMIT 10";

    let results = execute_sql(&db, sql).expect("LEFT JOIN filter eliminates all");

    // With filter pushdown, the ColumnStore is empty. LEFT JOIN must still
    // return all left-side rows with null joined columns. Pinning the exact
    // cardinality (5 products) catches a silent degradation to INNER JOIN.
    assert_eq!(
        results.len(),
        5,
        "LEFT JOIN must keep all 5 left-side products even when the filter eliminates every right row"
    );
    assert!(
        results.iter().all(|r| payload_f64(r, "price").is_none()),
        "unmatched LEFT JOIN rows must carry a null/absent joined price"
    );
}

// =========================================================================
// Edge: Lookup join with no matching keys
// =========================================================================

/// GIVEN products with IDs not present in inventory
/// WHEN joining on primary key
/// THEN INNER JOIN returns empty.
#[test]
fn test_join_on_pk_no_matching_keys_returns_empty() {
    let (_dir, db) = create_test_db();

    execute_sql(
        &db,
        "CREATE COLLECTION items (dimension = 4, metric = 'cosine');",
    )
    .expect("CREATE items");

    let items = db.get_vector_collection("items").expect("get items");
    items
        .upsert(vec![
            Point::new(100, vec![1.0, 0.0, 0.0, 0.0], Some(json!({"name": "X"}))),
            Point::new(200, vec![0.0, 1.0, 0.0, 0.0], Some(json!({"name": "Y"}))),
        ])
        .expect("upsert items");

    execute_sql(&db, "CREATE METADATA COLLECTION stock;").expect("CREATE stock");
    let stock = db.get_metadata_collection("stock").expect("get stock");
    stock
        .upsert(vec![
            Point::metadata_only(1, json!({"qty": 10})),
            Point::metadata_only(2, json!({"qty": 20})),
        ])
        .expect("upsert stock");

    let sql = "SELECT * FROM items \
               JOIN stock ON items.id = stock.id \
               LIMIT 10";

    let results = execute_sql(&db, sql).expect("no matching keys");
    assert!(
        results.is_empty(),
        "no matching IDs should yield empty result"
    );
}

// =========================================================================
// Edge: Empty joined collection
// =========================================================================

/// GIVEN an empty joined collection
/// WHEN joining
/// THEN INNER JOIN returns empty.
#[test]
fn test_join_with_empty_joined_collection() {
    let (_dir, db) = create_test_db();

    execute_sql(
        &db,
        "CREATE COLLECTION base (dimension = 4, metric = 'cosine');",
    )
    .expect("CREATE base");

    let base = db.get_vector_collection("base").expect("get base");
    base.upsert(vec![Point::new(
        1,
        vec![1.0, 0.0, 0.0, 0.0],
        Some(json!({"name": "A"})),
    )])
    .expect("upsert base");

    execute_sql(&db, "CREATE METADATA COLLECTION empty_coll;").expect("CREATE empty_coll");

    let sql = "SELECT * FROM base \
               JOIN empty_coll ON base.id = empty_coll.id \
               LIMIT 10";

    let results = execute_sql(&db, sql).expect("empty joined collection");
    assert!(
        results.is_empty(),
        "JOIN with empty collection should be empty"
    );
}

// =========================================================================
// Negative: Invalid table name
// =========================================================================

/// GIVEN a JOIN referencing a non-existent table
/// WHEN the query is executed
/// THEN a descriptive error is returned.
#[test]
fn test_join_with_invalid_table_returns_error() {
    let (_dir, db) = create_test_db();
    setup_collections(&db);

    let sql = "SELECT * FROM products \
               JOIN nonexistent ON products.id = nonexistent.id \
               LIMIT 10";

    let err = execute_sql(&db, sql).expect_err("should error on invalid table");
    let msg = err.to_string();
    assert!(
        msg.contains("nonexistent") || msg.contains("not found"),
        "error should mention the missing table, got: {msg}"
    );
}

// =========================================================================
// Combination: Pushdown + post-join filter
// =========================================================================

/// GIVEN a query with both pushdown-eligible and post-join conditions
/// WHEN executed
/// THEN both filters are applied correctly.
#[test]
fn test_join_with_pushdown_and_post_join_filter() {
    let (_dir, db) = create_test_db();
    setup_collections(&db);

    // inventory.stock > 0 is pushdown-eligible; products.category = 'audio' is base-side
    let sql = "SELECT * FROM products \
               JOIN inventory ON products.id = inventory.id \
               WHERE category = 'audio' AND inventory.stock > 0 \
               LIMIT 10";

    let results = execute_sql(&db, sql).expect("pushdown + post-join filter");

    // audio products: Headphones (stock=50), Speakers (stock=30) — both stock > 0
    assert_eq!(
        results.len(),
        2,
        "should return 2 audio products with stock > 0"
    );
    for r in &results {
        assert_eq!(payload_str(r, "category"), Some("audio"));
        let stock = payload_f64(r, "stock").expect("stock");
        assert!(stock > 0.0);
    }
}

// =========================================================================
// Combination: Multiple conditions on joined table
// =========================================================================

/// GIVEN a query with multiple conditions on the joined table
/// WHEN executed
/// THEN all conditions are applied.
#[test]
fn test_join_with_multiple_conditions_on_joined_table() {
    let (_dir, db) = create_test_db();
    setup_collections(&db);

    let sql = "SELECT * FROM products \
               JOIN inventory ON products.id = inventory.id \
               WHERE inventory.price > 50 AND inventory.stock > 10 \
               LIMIT 10";

    let results = execute_sql(&db, sql).expect("multiple conditions");

    // price > 50 AND stock > 10:
    // Headphones: 99.99, 50 ✓
    // Monitor: 399.99, 12 ✓
    // Speakers: 79.99, 30 ✓
    // Keyboard: 149.99, 0 ✗ (stock)
    // Mouse: 29.99, 200 ✗ (price)
    assert_eq!(
        results.len(),
        3,
        "should return 3 rows matching both conditions"
    );
}

// =========================================================================
// Combination: ORDER BY + LIMIT after pushdown
// =========================================================================

/// GIVEN a query with pushdown filter, ORDER BY, and LIMIT
/// WHEN executed
/// THEN results are filtered and limited correctly.
#[test]
fn test_join_with_order_by_and_limit_after_pushdown() {
    let (_dir, db) = create_test_db();
    setup_collections(&db);

    let sql = "SELECT * FROM products \
               JOIN inventory ON products.id = inventory.id \
               WHERE inventory.price > 50 \
               LIMIT 2";

    let results = execute_sql(&db, sql).expect("ORDER BY + LIMIT after pushdown");

    // price > 50: Headphones (99.99), Keyboard (149.99), Monitor (399.99), Speakers (79.99)
    assert_eq!(results.len(), 2, "LIMIT 2 should return 2 rows");
    for r in &results {
        let price = payload_f64(r, "price").expect("price");
        assert!(price > 50.0, "price {price} should be > 50");
    }
}