stoolap 0.4.0

High-performance embedded SQL database with MVCC, time-travel queries, and full ACID compliance
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
// Copyright 2025 Stoolap Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! WHERE Clause Tests
//!
//! Tests various WHERE clause scenarios: comparisons, logical operators, LIKE, IN, etc.

use stoolap::Database;

fn setup_products_table(db: &Database) {
    // Create a test table
    db.execute(
        "CREATE TABLE products (
            id INTEGER,
            name TEXT,
            category TEXT,
            price FLOAT,
            in_stock BOOLEAN,
            tags TEXT
        )",
        (),
    )
    .expect("Failed to create table");

    // Insert test data
    let inserts = [
        "INSERT INTO products VALUES (1, 'Laptop', 'Electronics', 1200.00, true, 'portable,computer')",
        "INSERT INTO products VALUES (2, 'Smartphone', 'Electronics', 800.00, true, 'mobile,phone')",
        "INSERT INTO products VALUES (3, 'Headphones', 'Electronics', 150.00, true, 'audio,accessory')",
        "INSERT INTO products VALUES (4, 'T-shirt', 'Clothing', 25.00, true, 'apparel,cotton')",
        "INSERT INTO products VALUES (5, 'Jeans', 'Clothing', 50.00, false, 'apparel,denim')",
        "INSERT INTO products VALUES (6, 'Sneakers', 'Footwear', 80.00, true, 'shoes,casual')",
        "INSERT INTO products VALUES (7, 'Boots', 'Footwear', 120.00, false, 'shoes,winter')",
        "INSERT INTO products VALUES (8, 'Desk', 'Furniture', 250.00, true, 'home,office')",
        "INSERT INTO products VALUES (9, 'Chair', 'Furniture', 150.00, true, 'home,office')",
        "INSERT INTO products VALUES (10, 'Bookshelf', 'Furniture', 180.00, false, 'home,storage')",
    ];

    for insert in &inserts {
        db.execute(insert, ()).expect("Failed to insert data");
    }
}

fn count_rows(db: &Database, query: &str) -> i64 {
    let result = db.query(query, ()).expect("Failed to execute query");
    let mut count = 0;
    for row in result {
        let _ = row.expect("Failed to get row");
        count += 1;
    }
    count
}

// Basic comparisons

#[test]
fn test_equality_operator() {
    let db = Database::open("memory://where_eq").expect("Failed to create database");
    setup_products_table(&db);

    let count = count_rows(&db, "SELECT * FROM products WHERE category = 'Electronics'");
    assert_eq!(count, 3, "Expected 3 Electronics products");
}

#[test]
fn test_inequality_operator() {
    let db = Database::open("memory://where_neq").expect("Failed to create database");
    setup_products_table(&db);

    let count = count_rows(
        &db,
        "SELECT * FROM products WHERE category != 'Electronics'",
    );
    assert_eq!(count, 7, "Expected 7 non-Electronics products");
}

#[test]
fn test_greater_than_operator() {
    let db = Database::open("memory://where_gt").expect("Failed to create database");
    setup_products_table(&db);

    let count = count_rows(&db, "SELECT * FROM products WHERE price > 200");
    assert_eq!(count, 3, "Expected 3 products with price > 200");
}

#[test]
fn test_greater_than_or_equal_operator() {
    let db = Database::open("memory://where_gte").expect("Failed to create database");
    setup_products_table(&db);

    let count = count_rows(&db, "SELECT * FROM products WHERE price >= 150");
    assert_eq!(count, 6, "Expected 6 products with price >= 150");
}

#[test]
fn test_less_than_operator() {
    let db = Database::open("memory://where_lt").expect("Failed to create database");
    setup_products_table(&db);

    // T-shirt (25), Jeans (50), Sneakers (80)
    let count = count_rows(&db, "SELECT * FROM products WHERE price < 100");
    assert_eq!(count, 3, "Expected 3 products with price < 100");
}

#[test]
fn test_less_than_or_equal_operator() {
    let db = Database::open("memory://where_lte").expect("Failed to create database");
    setup_products_table(&db);

    // T-shirt (25), Jeans (50), Sneakers (80), Headphones (150), Boots (120), Chair (150)
    let count = count_rows(&db, "SELECT * FROM products WHERE price <= 150");
    assert_eq!(count, 6, "Expected 6 products with price <= 150");
}

// Boolean operators

#[test]
fn test_boolean_true_comparison() {
    let db = Database::open("memory://where_bool_true").expect("Failed to create database");
    setup_products_table(&db);

    let count = count_rows(&db, "SELECT * FROM products WHERE in_stock = true");
    assert_eq!(count, 7, "Expected 7 in-stock products");
}

#[test]
fn test_boolean_false_comparison() {
    let db = Database::open("memory://where_bool_false").expect("Failed to create database");
    setup_products_table(&db);

    let count = count_rows(&db, "SELECT * FROM products WHERE in_stock = false");
    assert_eq!(count, 3, "Expected 3 out-of-stock products");
}

// Logical operators

#[test]
fn test_and_operator() {
    let db = Database::open("memory://where_and").expect("Failed to create database");
    setup_products_table(&db);

    let count = count_rows(
        &db,
        "SELECT * FROM products WHERE category = 'Electronics' AND price > 500",
    );
    assert_eq!(count, 2, "Expected 2 Electronics products with price > 500");
}

#[test]
fn test_or_operator() {
    let db = Database::open("memory://where_or").expect("Failed to create database");
    setup_products_table(&db);

    let count = count_rows(
        &db,
        "SELECT * FROM products WHERE category = 'Furniture' OR category = 'Footwear'",
    );
    assert_eq!(count, 5, "Expected 5 Furniture or Footwear products");
}

#[test]
fn test_complex_and_or_combination() {
    let db = Database::open("memory://where_complex").expect("Failed to create database");
    setup_products_table(&db);

    // Laptop (Electronics, 1200), Chair (Furniture, 150), Bookshelf (Furniture, 180)
    let count = count_rows(
        &db,
        "SELECT * FROM products WHERE (category = 'Electronics' AND price > 1000) OR (category = 'Furniture' AND price < 200)",
    );
    assert_eq!(count, 3, "Expected 3 products matching complex condition");
}

// LIKE operator

#[test]
fn test_like_starts_with() {
    let db = Database::open("memory://where_like_start").expect("Failed to create database");
    setup_products_table(&db);

    // Boots, Bookshelf
    let count = count_rows(&db, "SELECT * FROM products WHERE name LIKE 'B%'");
    assert_eq!(count, 2, "Expected 2 products starting with 'B'");
}

#[test]
fn test_like_contains() {
    let db = Database::open("memory://where_like_contains").expect("Failed to create database");
    setup_products_table(&db);

    // Laptop, Smartphone, Headphones, Boots, Bookshelf
    let count = count_rows(&db, "SELECT * FROM products WHERE name LIKE '%o%'");
    assert_eq!(count, 5, "Expected 5 products containing 'o'");
}

#[test]
fn test_like_ends_with() {
    let db = Database::open("memory://where_like_end").expect("Failed to create database");
    setup_products_table(&db);

    // Headphones, Jeans, Boots, Sneakers
    let count = count_rows(&db, "SELECT * FROM products WHERE name LIKE '%s'");
    assert_eq!(count, 4, "Expected 4 products ending with 's'");
}

#[test]
fn test_like_single_char() {
    let db = Database::open("memory://where_like_char").expect("Failed to create database");
    setup_products_table(&db);

    // Desk
    let count = count_rows(&db, "SELECT * FROM products WHERE name LIKE 'Des_'");
    assert_eq!(count, 1, "Expected 1 product matching 'Des_'");
}

// NOT operator

#[test]
fn test_not_operator() {
    let db = Database::open("memory://where_not").expect("Failed to create database");
    setup_products_table(&db);

    let count = count_rows(
        &db,
        "SELECT * FROM products WHERE NOT category = 'Electronics'",
    );
    assert_eq!(count, 7, "Expected 7 non-Electronics products");
}

#[test]
fn test_not_with_other_conditions() {
    let db = Database::open("memory://where_not_and").expect("Failed to create database");
    setup_products_table(&db);

    // Boots (120), Bookshelf (180)
    let count = count_rows(
        &db,
        "SELECT * FROM products WHERE NOT in_stock AND price > 100",
    );
    assert_eq!(
        count, 2,
        "Expected 2 out-of-stock products with price > 100"
    );
}

// IN operator

#[test]
fn test_in_operator() {
    let db = Database::open("memory://where_in").expect("Failed to create database");
    setup_products_table(&db);

    let count = count_rows(
        &db,
        "SELECT * FROM products WHERE category IN ('Electronics', 'Furniture')",
    );
    assert_eq!(count, 6, "Expected 6 Electronics or Furniture products");
}

#[test]
fn test_not_in_operator() {
    let db = Database::open("memory://where_not_in").expect("Failed to create database");
    setup_products_table(&db);

    let count = count_rows(
        &db,
        "SELECT * FROM products WHERE category NOT IN ('Electronics', 'Furniture')",
    );
    assert_eq!(count, 4, "Expected 4 non-Electronics/Furniture products");
}

// ORDER BY and LIMIT

#[test]
fn test_order_by_asc_with_limit() {
    let db = Database::open("memory://where_order_asc").expect("Failed to create database");
    setup_products_table(&db);

    let count = count_rows(&db, "SELECT * FROM products ORDER BY price ASC LIMIT 3");
    assert_eq!(count, 3, "Expected 3 products with LIMIT");
}

#[test]
fn test_order_by_desc_with_limit() {
    let db = Database::open("memory://where_order_desc").expect("Failed to create database");
    setup_products_table(&db);

    let count = count_rows(&db, "SELECT * FROM products ORDER BY price DESC LIMIT 2");
    assert_eq!(count, 2, "Expected 2 products with LIMIT");
}

#[test]
fn test_where_with_order_by_and_limit() {
    let db = Database::open("memory://where_order_limit").expect("Failed to create database");
    setup_products_table(&db);

    let count = count_rows(
        &db,
        "SELECT * FROM products WHERE price > 100 ORDER BY price DESC LIMIT 3",
    );
    assert_eq!(
        count, 3,
        "Expected 3 products with WHERE, ORDER BY and LIMIT"
    );
}

// Complex combined queries

#[test]
fn test_complex_query_1() {
    let db = Database::open("memory://where_complex1").expect("Failed to create database");
    setup_products_table(&db);

    // Laptop, Smartphone, Desk
    let count = count_rows(
        &db,
        "SELECT * FROM products WHERE (category = 'Electronics' OR category = 'Furniture') AND price > 200 AND in_stock = true",
    );
    assert_eq!(count, 3, "Expected 3 products matching complex query 1");
}

#[test]
fn test_complex_query_2() {
    let db = Database::open("memory://where_complex2").expect("Failed to create database");
    setup_products_table(&db);

    let count = count_rows(
        &db,
        "SELECT * FROM products WHERE name LIKE '%e%' AND price BETWEEN 100 AND 300 AND category != 'Clothing'",
    );
    assert_eq!(count, 3, "Expected 3 products matching complex query 2");
}

#[test]
fn test_complex_query_3() {
    let db = Database::open("memory://where_complex3").expect("Failed to create database");
    setup_products_table(&db);

    // Laptop (1200, true), T-shirt (25, true), Sneakers (80, true)
    let count = count_rows(
        &db,
        "SELECT * FROM products WHERE (price < 100 OR price > 1000) AND in_stock = true ORDER BY price DESC LIMIT 4",
    );
    assert_eq!(count, 3, "Expected 3 products matching complex query 3");
}

// BETWEEN operator

#[test]
fn test_between_operator() {
    let db = Database::open("memory://where_between").expect("Failed to create database");
    setup_products_table(&db);

    // Headphones (150), Boots (120), Chair (150), Bookshelf (180)
    let count = count_rows(
        &db,
        "SELECT * FROM products WHERE price BETWEEN 100 AND 200",
    );
    assert_eq!(
        count, 4,
        "Expected 4 products with price BETWEEN 100 AND 200"
    );
}

// OR with indexed columns - tests OR index union optimization
#[test]
fn test_or_with_indexed_columns() {
    let db = Database::open("memory://where_or_indexed").expect("Failed to create database");

    // Create table with indexes on category and price
    db.execute(
        "CREATE TABLE indexed_products (
            id INTEGER PRIMARY KEY,
            name TEXT,
            category TEXT,
            price REAL
        )",
        (),
    )
    .expect("Failed to create table");

    // Create indexes for OR optimization
    db.execute(
        "CREATE INDEX idx_category ON indexed_products(category)",
        (),
    )
    .expect("Failed to create category index");
    db.execute("CREATE INDEX idx_price ON indexed_products(price)", ())
        .expect("Failed to create price index");

    // Insert test data (use .0 for REAL column)
    db.execute(
        "INSERT INTO indexed_products VALUES (1, 'Laptop', 'Electronics', 1200.0)",
        (),
    )
    .unwrap();
    db.execute(
        "INSERT INTO indexed_products VALUES (2, 'Phone', 'Electronics', 800.0)",
        (),
    )
    .unwrap();
    db.execute(
        "INSERT INTO indexed_products VALUES (3, 'Chair', 'Furniture', 150.0)",
        (),
    )
    .unwrap();
    db.execute(
        "INSERT INTO indexed_products VALUES (4, 'Desk', 'Furniture', 300.0)",
        (),
    )
    .unwrap();
    db.execute(
        "INSERT INTO indexed_products VALUES (5, 'Shirt', 'Clothing', 50.0)",
        (),
    )
    .unwrap();
    db.execute(
        "INSERT INTO indexed_products VALUES (6, 'Shoes', 'Footwear', 100.0)",
        (),
    )
    .unwrap();

    // Test OR with same indexed column (should use index union)
    let count = count_rows(
        &db,
        "SELECT * FROM indexed_products WHERE category = 'Electronics' OR category = 'Furniture'",
    );
    assert_eq!(count, 4, "Expected 4 products (Electronics + Furniture)");

    // Test OR with different indexed columns (should use index union)
    let count = count_rows(
        &db,
        "SELECT * FROM indexed_products WHERE category = 'Clothing' OR price > 500.0",
    );
    assert_eq!(count, 3, "Expected 3 products (Clothing OR price > 500)");

    // Test OR with range conditions
    let count = count_rows(
        &db,
        "SELECT * FROM indexed_products WHERE price < 100.0 OR price > 1000.0",
    );
    assert_eq!(
        count, 2,
        "Expected 2 products (price < 100 OR price > 1000)"
    );

    // Test multiple OR conditions
    let count = count_rows(
        &db,
        "SELECT * FROM indexed_products WHERE category = 'Electronics' OR category = 'Furniture' OR category = 'Clothing'",
    );
    assert_eq!(count, 5, "Expected 5 products (3 categories)");
}