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
465
466
467
468
469
470
471
472
473
474
475
// 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.

// Tests for the Compiled Expression VM

use std::sync::Arc;

use crate::common::SmartString;

use crate::common::CompactArc;

use super::compiler::{CompileContext, ExprCompiler};
use super::ops::{CompiledPattern, Op};
use super::program::Program;
use super::vm::{ExecuteContext, ExprVM};
use crate::core::{Value, ValueSet};
use crate::Row;

#[test]
fn test_simple_load_and_compare() {
    let mut vm = ExprVM::new();

    // col[0] > 5
    let program = Program::new(vec![
        Op::LoadColumn(0),
        Op::LoadConst(Value::Integer(5)),
        Op::Gt,
        Op::Return,
    ]);

    // True case
    let row = Row::from_values(vec![Value::Integer(10)]);
    let ctx = ExecuteContext::new(&row);
    let result = vm.execute(&program, &ctx).unwrap();
    assert_eq!(result, Value::Boolean(true));

    // False case
    let row = Row::from_values(vec![Value::Integer(3)]);
    let ctx = ExecuteContext::new(&row);
    let result = vm.execute(&program, &ctx).unwrap();
    assert_eq!(result, Value::Boolean(false));
}

#[test]
fn test_null_comparison() {
    let mut vm = ExprVM::new();

    // col[0] > 5 with NULL col
    let program = Program::new(vec![
        Op::LoadColumn(0),
        Op::LoadConst(Value::Integer(5)),
        Op::Gt,
        Op::Return,
    ]);

    let row = Row::from_values(vec![Value::Null(crate::core::DataType::Integer)]);
    let ctx = ExecuteContext::new(&row);
    let result = vm.execute(&program, &ctx).unwrap();
    assert!(result.is_null());
}

#[test]
fn test_and_short_circuit() {
    let mut vm = ExprVM::new();

    // col[0] > 5 AND col[1] < 10
    let program = Program::new(vec![
        Op::LoadColumn(0),
        Op::LoadConst(Value::Integer(5)),
        Op::Gt,
        Op::And(10), // Jump to position 10 if false
        Op::LoadColumn(1),
        Op::LoadConst(Value::Integer(10)),
        Op::Lt,
        Op::AndFinalize,
        Op::Return,
        Op::Nop,                              // Position 9
        Op::LoadConst(Value::Boolean(false)), // Position 10
        Op::Return,
    ]);

    // Both true
    let row = Row::from_values(vec![Value::Integer(10), Value::Integer(5)]);
    let ctx = ExecuteContext::new(&row);
    let result = vm.execute(&program, &ctx).unwrap();
    assert_eq!(result, Value::Boolean(true));

    // First false (short circuit)
    let row = Row::from_values(vec![Value::Integer(3), Value::Integer(5)]);
    let ctx = ExecuteContext::new(&row);
    let result = vm.execute(&program, &ctx).unwrap();
    assert_eq!(result, Value::Boolean(false));

    // First true, second false
    let row = Row::from_values(vec![Value::Integer(10), Value::Integer(15)]);
    let ctx = ExecuteContext::new(&row);
    let result = vm.execute(&program, &ctx).unwrap();
    assert_eq!(result, Value::Boolean(false));
}

#[test]
fn test_or_short_circuit() {
    let mut vm = ExprVM::new();

    // col[0] < 5 OR col[1] > 10
    let program = Program::new(vec![
        Op::LoadColumn(0),
        Op::LoadConst(Value::Integer(5)),
        Op::Lt,
        Op::Or(10), // Jump to position 10 if true
        Op::LoadColumn(1),
        Op::LoadConst(Value::Integer(10)),
        Op::Gt,
        Op::OrFinalize,
        Op::Return,
        Op::Nop,                             // Position 9
        Op::LoadConst(Value::Boolean(true)), // Position 10
        Op::Return,
    ]);

    // First true (short circuit)
    let row = Row::from_values(vec![Value::Integer(3), Value::Integer(5)]);
    let ctx = ExecuteContext::new(&row);
    let result = vm.execute(&program, &ctx).unwrap();
    assert_eq!(result, Value::Boolean(true));

    // First false, second true
    let row = Row::from_values(vec![Value::Integer(10), Value::Integer(15)]);
    let ctx = ExecuteContext::new(&row);
    let result = vm.execute(&program, &ctx).unwrap();
    assert_eq!(result, Value::Boolean(true));

    // Both false
    let row = Row::from_values(vec![Value::Integer(10), Value::Integer(5)]);
    let ctx = ExecuteContext::new(&row);
    let result = vm.execute(&program, &ctx).unwrap();
    assert_eq!(result, Value::Boolean(false));
}

#[test]
fn test_arithmetic() {
    let mut vm = ExprVM::new();

    // col[0] + col[1] * 2
    let program = Program::new(vec![
        Op::LoadColumn(0),
        Op::LoadColumn(1),
        Op::LoadConst(Value::Integer(2)),
        Op::Mul,
        Op::Add,
        Op::Return,
    ]);

    let row = Row::from_values(vec![Value::Integer(5), Value::Integer(3)]);
    let ctx = ExecuteContext::new(&row);
    let result = vm.execute(&program, &ctx).unwrap();
    assert_eq!(result, Value::Integer(11)); // 5 + 3*2 = 11
}

#[test]
fn test_in_set() {
    let mut vm = ExprVM::new();

    let set: ValueSet = [Value::Integer(1), Value::Integer(2), Value::Integer(3)]
        .into_iter()
        .collect();

    let program = Program::new(vec![
        Op::LoadColumn(0),
        Op::InSet(CompactArc::new(set), false),
        Op::Return,
    ]);

    // In set
    let row = Row::from_values(vec![Value::Integer(2)]);
    let ctx = ExecuteContext::new(&row);
    let result = vm.execute(&program, &ctx).unwrap();
    assert_eq!(result, Value::Boolean(true));

    // Not in set
    let row = Row::from_values(vec![Value::Integer(5)]);
    let ctx = ExecuteContext::new(&row);
    let result = vm.execute(&program, &ctx).unwrap();
    assert_eq!(result, Value::Boolean(false));
}

#[test]
fn test_between() {
    let mut vm = ExprVM::new();

    // col[0] BETWEEN 5 AND 10
    let program = Program::new(vec![
        Op::LoadColumn(0),
        Op::LoadConst(Value::Integer(5)),
        Op::LoadConst(Value::Integer(10)),
        Op::Between,
        Op::Return,
    ]);

    // In range
    let row = Row::from_values(vec![Value::Integer(7)]);
    let ctx = ExecuteContext::new(&row);
    let result = vm.execute(&program, &ctx).unwrap();
    assert_eq!(result, Value::Boolean(true));

    // Below range
    let row = Row::from_values(vec![Value::Integer(3)]);
    let ctx = ExecuteContext::new(&row);
    let result = vm.execute(&program, &ctx).unwrap();
    assert_eq!(result, Value::Boolean(false));

    // Above range
    let row = Row::from_values(vec![Value::Integer(15)]);
    let ctx = ExecuteContext::new(&row);
    let result = vm.execute(&program, &ctx).unwrap();
    assert_eq!(result, Value::Boolean(false));
}

#[test]
fn test_like_pattern() {
    let mut vm = ExprVM::new();

    // col[0] LIKE 'test%'
    let pattern = CompiledPattern::compile("test%", false);
    let program = Program::new(vec![
        Op::LoadColumn(0),
        Op::Like(Arc::new(pattern), false),
        Op::Return,
    ]);

    // Match
    let row = Row::from_values(vec![Value::Text(SmartString::from("testing"))]);
    let ctx = ExecuteContext::new(&row);
    let result = vm.execute(&program, &ctx).unwrap();
    assert_eq!(result, Value::Boolean(true));

    // No match
    let row = Row::from_values(vec![Value::Text(SmartString::from("other"))]);
    let ctx = ExecuteContext::new(&row);
    let result = vm.execute(&program, &ctx).unwrap();
    assert_eq!(result, Value::Boolean(false));
}

#[test]
fn test_is_null() {
    let mut vm = ExprVM::new();

    // col[0] IS NULL
    let program = Program::new(vec![Op::LoadColumn(0), Op::IsNull, Op::Return]);

    // Is null
    let row = Row::from_values(vec![Value::Null(crate::core::DataType::Integer)]);
    let ctx = ExecuteContext::new(&row);
    let result = vm.execute(&program, &ctx).unwrap();
    assert_eq!(result, Value::Boolean(true));

    // Not null
    let row = Row::from_values(vec![Value::Integer(5)]);
    let ctx = ExecuteContext::new(&row);
    let result = vm.execute(&program, &ctx).unwrap();
    assert_eq!(result, Value::Boolean(false));
}

#[test]
fn test_coalesce() {
    let mut vm = ExprVM::new();

    // COALESCE(col[0], col[1], 'default')
    let program = Program::new(vec![
        Op::LoadColumn(0),
        Op::LoadColumn(1),
        Op::LoadConst(Value::Text(SmartString::from("default"))),
        Op::Coalesce(3),
        Op::Return,
    ]);

    // First non-null
    let row = Row::from_values(vec![
        Value::Text(SmartString::from("first")),
        Value::Text(SmartString::from("second")),
    ]);
    let ctx = ExecuteContext::new(&row);
    let result = vm.execute(&program, &ctx).unwrap();
    assert_eq!(result, Value::Text(SmartString::from("first")));

    // Second non-null
    let row = Row::from_values(vec![
        Value::Null(crate::core::DataType::Text),
        Value::Text(SmartString::from("second")),
    ]);
    let ctx = ExecuteContext::new(&row);
    let result = vm.execute(&program, &ctx).unwrap();
    assert_eq!(result, Value::Text(SmartString::from("second")));

    // Default
    let row = Row::from_values(vec![
        Value::Null(crate::core::DataType::Text),
        Value::Null(crate::core::DataType::Text),
    ]);
    let ctx = ExecuteContext::new(&row);
    let result = vm.execute(&program, &ctx).unwrap();
    assert_eq!(result, Value::Text(SmartString::from("default")));
}

#[test]
fn test_join_context() {
    let mut vm = ExprVM::new();

    // row1.col[0] = row2.col[0]
    let program = Program::new(vec![
        Op::LoadColumn(0),  // From first row
        Op::LoadColumn2(0), // From second row
        Op::Eq,
        Op::Return,
    ]);

    let row1 = Row::from_values(vec![Value::Integer(5)]);
    let row2 = Row::from_values(vec![Value::Integer(5)]);
    let ctx = ExecuteContext::for_join(&row1, &row2);
    let result = vm.execute(&program, &ctx).unwrap();
    assert_eq!(result, Value::Boolean(true));

    let row1 = Row::from_values(vec![Value::Integer(5)]);
    let row2 = Row::from_values(vec![Value::Integer(10)]);
    let ctx = ExecuteContext::for_join(&row1, &row2);
    let result = vm.execute(&program, &ctx).unwrap();
    assert_eq!(result, Value::Boolean(false));
}

#[test]
fn test_parameters() {
    let mut vm = ExprVM::new();

    // col[0] = $1
    let program = Program::new(vec![
        Op::LoadColumn(0),
        Op::LoadParam(0),
        Op::Eq,
        Op::Return,
    ]);

    let row = Row::from_values(vec![Value::Integer(42)]);
    let params = vec![Value::Integer(42)];
    let ctx = ExecuteContext::new(&row).with_params(&params);
    let result = vm.execute(&program, &ctx).unwrap();
    assert_eq!(result, Value::Boolean(true));

    let params = vec![Value::Integer(100)];
    let ctx = ExecuteContext::new(&row).with_params(&params);
    let result = vm.execute(&program, &ctx).unwrap();
    assert_eq!(result, Value::Boolean(false));
}

#[test]
fn test_execute_bool() {
    let mut vm = ExprVM::new();

    let program = Program::new(vec![
        Op::LoadColumn(0),
        Op::LoadConst(Value::Integer(5)),
        Op::Gt,
        Op::Return,
    ]);

    // True
    let row = Row::from_values(vec![Value::Integer(10)]);
    let ctx = ExecuteContext::new(&row);
    assert!(vm.execute_bool(&program, &ctx));

    // False
    let row = Row::from_values(vec![Value::Integer(3)]);
    let ctx = ExecuteContext::new(&row);
    assert!(!vm.execute_bool(&program, &ctx));

    // NULL -> false
    let row = Row::from_values(vec![Value::Null(crate::core::DataType::Integer)]);
    let ctx = ExecuteContext::new(&row);
    assert!(!vm.execute_bool(&program, &ctx));
}

// ============================================================================
// Compiler Tests
// ============================================================================

#[test]
fn test_compiler_simple_expression() {
    use crate::parser::ast::*;
    use crate::parser::token::{Position, Token, TokenType};

    let columns = vec!["a".to_string(), "b".to_string()];
    let ctx = CompileContext::with_global_registry(&columns);
    let compiler = ExprCompiler::new(&ctx);

    fn make_token() -> Token {
        Token {
            token_type: TokenType::Integer,
            literal: "1".into(),
            position: Position {
                offset: 0,
                line: 1,
                column: 1,
            },
            quoted: false,
        }
    }

    // a > 5
    let expr = Expression::Infix(InfixExpression {
        token: make_token(),
        left: Box::new(Expression::Identifier(Identifier::new(
            make_token(),
            "a".to_string(),
        ))),
        operator: ">".into(),
        op_type: InfixOperator::GreaterThan,
        right: Box::new(Expression::IntegerLiteral(IntegerLiteral {
            token: make_token(),
            value: 5,
        })),
    });

    let program = compiler.compile(&expr).unwrap();

    // Execute
    let mut vm = ExprVM::new();
    let row = Row::from_values(vec![Value::Integer(10), Value::Integer(20)]);
    let ctx = ExecuteContext::new(&row);
    let result = vm.execute(&program, &ctx).unwrap();
    assert_eq!(result, Value::Boolean(true));
}

#[test]
fn test_compiled_pattern_prefix() {
    let pattern = CompiledPattern::compile("test%", false);
    assert!(pattern.matches("testing", false));
    assert!(pattern.matches("test", false));
    assert!(!pattern.matches("atest", false));
}

#[test]
fn test_compiled_pattern_suffix() {
    let pattern = CompiledPattern::compile("%test", false);
    assert!(pattern.matches("mytest", false));
    assert!(pattern.matches("test", false));
    assert!(!pattern.matches("testa", false));
}

#[test]
fn test_compiled_pattern_contains() {
    let pattern = CompiledPattern::compile("%test%", false);
    assert!(pattern.matches("mytesting", false));
    assert!(pattern.matches("test", false));
    assert!(pattern.matches("atest", false));
    assert!(!pattern.matches("other", false));
}

#[test]
fn test_compiled_pattern_case_insensitive() {
    let pattern = CompiledPattern::compile("TEST%", true);
    assert!(pattern.matches("testing", true));
    assert!(pattern.matches("TESTING", true));
    assert!(pattern.matches("TeStInG", true));
}