tempest-engine 0.0.2

Relational database engine for TempestDB
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
use std::borrow::Cow;

use tempest_core::test_utils::setup_tracing;
use tempest_io::VirtualIo;
use tempest_rt::block_on;

use crate::{query::QueryResult, types::TempestValue};

use super::{get_columns, get_rows, open_engine};

// -- global Option[T] without prefix --

async fn setup_global_option_schema(engine: &mut crate::Engine<VirtualIo>) {
    engine.execute("create database main;").await.unwrap();
    engine
        .execute("create type main.User struct { id: Int64, name: Option[String] };")
        .await
        .unwrap();
    engine
        .execute("create table main.users : main.User { primary key (id) };")
        .await
        .unwrap();
}

async fn insert_global_option_users(engine: &mut crate::Engine<VirtualIo>) {
    engine
        .execute("insert into main.users { id: 1, name: Option.None };")
        .await
        .unwrap();
    engine
        .execute(r#"insert into main.users { id: 2, name: Option.Some("Alice") };"#)
        .await
        .unwrap();
    engine
        .execute(r#"insert into main.users { id: 3, name: Option.Some("John") };"#)
        .await
        .unwrap();
}

#[test]
fn test_global_option_in_struct_field() {
    setup_tracing();
    block_on(VirtualIo::default(), async {
        let mut engine = open_engine().await;
        setup_global_option_schema(&mut engine).await;
        engine.shutdown().await.unwrap();
    });
}

#[test]
fn test_insert_option_without_prefix() {
    setup_tracing();
    block_on(VirtualIo::default(), async {
        let mut engine = open_engine().await;
        setup_global_option_schema(&mut engine).await;
        insert_global_option_users(&mut engine).await;

        let results = engine.execute("select * from main.users;").await.unwrap();
        let rows = get_rows(&results[0]);
        assert_eq!(rows.len(), 3);

        assert_eq!(rows[0][0], TempestValue::Int64(1));
        assert!(matches!(rows[0][1], TempestValue::Enum { variant_id: 0, .. })); // None

        assert_eq!(rows[1][0], TempestValue::Int64(2));
        assert!(matches!(&rows[1][1], TempestValue::Enum { variant_id: 1, fields, .. }
            if fields[0] == TempestValue::String(Cow::Borrowed("Alice"))));

        assert_eq!(rows[2][0], TempestValue::Int64(3));
        assert!(matches!(&rows[2][1], TempestValue::Enum { variant_id: 1, fields, .. }
            if fields[0] == TempestValue::String(Cow::Borrowed("John"))));

        engine.shutdown().await.unwrap();
    });
}

#[test]
fn test_is_option_none_global() {
    setup_tracing();
    block_on(VirtualIo::default(), async {
        let mut engine = open_engine().await;
        setup_global_option_schema(&mut engine).await;
        insert_global_option_users(&mut engine).await;

        let results = engine
            .execute("select * from main.users where name is Option.None;")
            .await
            .unwrap();
        let rows = get_rows(&results[0]);
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0][0], TempestValue::Int64(1));

        engine.shutdown().await.unwrap();
    });
}

#[test]
fn test_is_option_some_concrete_global() {
    setup_tracing();
    block_on(VirtualIo::default(), async {
        let mut engine = open_engine().await;
        setup_global_option_schema(&mut engine).await;
        insert_global_option_users(&mut engine).await;

        let results = engine
            .execute(r#"select * from main.users where name is Option.Some("Alice");"#)
            .await
            .unwrap();
        let rows = get_rows(&results[0]);
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0][0], TempestValue::Int64(2));

        engine.shutdown().await.unwrap();
    });
}

#[test]
fn test_is_option_some_wildcard_global() {
    setup_tracing();
    block_on(VirtualIo::default(), async {
        let mut engine = open_engine().await;
        setup_global_option_schema(&mut engine).await;
        insert_global_option_users(&mut engine).await;

        let results = engine
            .execute(r#"select * from main.users where name is Option.Some(n) and n = "John";"#)
            .await
            .unwrap();
        let rows = get_rows(&results[0]);
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0][0], TempestValue::Int64(3));

        engine.shutdown().await.unwrap();
    });
}

// -- helpers --

/// Sets up:
///   main.Status enum { Draft, Published, Archived }
///   main.Post struct { id: Int64, status: main.Status }
///   main.posts : main.Post { primary key (id) }
async fn setup_status_schema(engine: &mut crate::Engine<VirtualIo>) {
    engine.execute("create database main;").await.unwrap();
    engine
        .execute("create type main.Status enum { Draft, Published, Archived };")
        .await
        .unwrap();
    engine
        .execute("create type main.Post struct { id: Int64, status: main.Status };")
        .await
        .unwrap();
    engine
        .execute("create table main.posts : main.Post { primary key (id) };")
        .await
        .unwrap();
}

async fn insert_three_posts(engine: &mut crate::Engine<VirtualIo>) {
    engine
        .execute("insert into main.posts { id: 1, status: main.Status.Draft };")
        .await
        .unwrap();
    engine
        .execute("insert into main.posts { id: 2, status: main.Status.Published };")
        .await
        .unwrap();
    engine
        .execute("insert into main.posts { id: 3, status: main.Status.Archived };")
        .await
        .unwrap();
}

// -- DDL --

#[test]
fn test_create_unit_enum() {
    setup_tracing();
    block_on(VirtualIo::default(), async {
        let mut engine = open_engine().await;
        engine.execute("create database main;").await.unwrap();
        let results = engine
            .execute("create type main.Status enum { Draft, Published, Archived };")
            .await
            .unwrap();
        assert!(matches!(results[0], QueryResult::Empty));
        engine.shutdown().await.unwrap();
    });
}

#[test]
fn test_create_table_with_enum_field() {
    setup_tracing();
    block_on(VirtualIo::default(), async {
        let mut engine = open_engine().await;
        setup_status_schema(&mut engine).await;
        engine.shutdown().await.unwrap();
    });
}

// -- Insert --

#[test]
fn test_insert_unit_variant() {
    setup_tracing();
    block_on(VirtualIo::default(), async {
        let mut engine = open_engine().await;
        setup_status_schema(&mut engine).await;
        let results = engine
            .execute("insert into main.posts { id: 1, status: main.Status.Draft };")
            .await
            .unwrap();
        assert!(matches!(results[0], QueryResult::Empty));
        engine.shutdown().await.unwrap();
    });
}

// -- Select --

#[test]
fn test_select_all_with_enum_field() {
    setup_tracing();
    block_on(VirtualIo::default(), async {
        let mut engine = open_engine().await;
        setup_status_schema(&mut engine).await;
        insert_three_posts(&mut engine).await;
        let results = engine
            .execute("select * from main.posts;")
            .await
            .unwrap();
        let cols = get_columns(&results[0]);
        let rows = get_rows(&results[0]);
        assert_eq!(cols[0], "id".into());
        assert_eq!(cols[1], "status".into());
        assert_eq!(rows.len(), 3);
        assert_eq!(rows[0][0], TempestValue::Int64(1));
        assert!(matches!(rows[0][1], TempestValue::Enum { variant_id: 0, .. })); // Draft
        assert_eq!(rows[1][0], TempestValue::Int64(2));
        assert!(matches!(rows[1][1], TempestValue::Enum { variant_id: 1, .. })); // Published
        assert_eq!(rows[2][0], TempestValue::Int64(3));
        assert!(matches!(rows[2][1], TempestValue::Enum { variant_id: 2, .. })); // Archived
        engine.shutdown().await.unwrap();
    });
}

#[test]
fn test_select_where_enum_is() {
    setup_tracing();
    block_on(VirtualIo::default(), async {
        let mut engine = open_engine().await;
        setup_status_schema(&mut engine).await;
        insert_three_posts(&mut engine).await;
        let results = engine
            .execute("select * from main.posts where status is main.Status.Published;")
            .await
            .unwrap();
        let rows = get_rows(&results[0]);
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0][0], TempestValue::Int64(2));
        engine.shutdown().await.unwrap();
    });
}

// -- Delete --

#[test]
fn test_delete_where_enum_is() {
    setup_tracing();
    block_on(VirtualIo::default(), async {
        let mut engine = open_engine().await;
        setup_status_schema(&mut engine).await;
        insert_three_posts(&mut engine).await;
        let del_results = engine
            .execute("delete from main.posts where status is main.Status.Archived;")
            .await
            .unwrap();
        assert!(matches!(del_results[0], QueryResult::RowsChanged(1)));
        let remaining = engine
            .execute("select * from main.posts;")
            .await
            .unwrap();
        let rows = get_rows(&remaining[0]);
        assert_eq!(rows.len(), 2);
        assert_eq!(rows[0][0], TempestValue::Int64(1));
        assert_eq!(rows[1][0], TempestValue::Int64(2));
        engine.shutdown().await.unwrap();
    });
}

// -- Option[T] generic enum tests --

/// Sets up:
///   main.Option[T] enum { None, Some(T) }
///   main.User struct { id: Int64, display_name: main.Option[String] }
///   main.users : main.User { primary key (id) }
async fn setup_option_schema(engine: &mut crate::Engine<VirtualIo>) {
    engine.execute("create database main;").await.unwrap();
    engine
        .execute("create type main.Option[T] enum { None, Some(T) };")
        .await
        .unwrap();
    engine
        .execute("create type main.User struct { id: Int64, display_name: main.Option[String] };")
        .await
        .unwrap();
    engine
        .execute("create table main.users : main.User { primary key (id) };")
        .await
        .unwrap();
}

async fn insert_option_users(engine: &mut crate::Engine<VirtualIo>) {
    engine
        .execute("insert into main.users { id: 1, display_name: main.Option.None };")
        .await
        .unwrap();
    engine
        .execute(r#"insert into main.users { id: 2, display_name: main.Option.Some("Alice") };"#)
        .await
        .unwrap();
    engine
        .execute(r#"insert into main.users { id: 3, display_name: main.Option.Some("John") };"#)
        .await
        .unwrap();
}

#[test]
fn test_create_generic_option_enum() {
    setup_tracing();
    block_on(VirtualIo::default(), async {
        let mut engine = open_engine().await;
        setup_option_schema(&mut engine).await;
        engine.shutdown().await.unwrap();
    });
}

#[test]
fn test_insert_and_select_option_variants() {
    setup_tracing();
    block_on(VirtualIo::default(), async {
        let mut engine = open_engine().await;
        setup_option_schema(&mut engine).await;
        insert_option_users(&mut engine).await;

        let results = engine.execute("select * from main.users;").await.unwrap();
        let rows = get_rows(&results[0]);
        assert_eq!(rows.len(), 3);

        // Row 1: None
        assert_eq!(rows[0][0], TempestValue::Int64(1));
        assert!(matches!(rows[0][1], TempestValue::Enum { variant_id: 0, .. })); // None

        // Row 2: Some("Alice")
        assert_eq!(rows[1][0], TempestValue::Int64(2));
        assert!(matches!(&rows[1][1], TempestValue::Enum { variant_id: 1, fields, .. }
            if fields[0] == TempestValue::String(Cow::Borrowed("Alice"))));

        // Row 3: Some("John")
        assert_eq!(rows[2][0], TempestValue::Int64(3));
        assert!(matches!(&rows[2][1], TempestValue::Enum { variant_id: 1, fields, .. }
            if fields[0] == TempestValue::String(Cow::Borrowed("John"))));

        engine.shutdown().await.unwrap();
    });
}

#[test]
fn test_select_where_is_none() {
    setup_tracing();
    block_on(VirtualIo::default(), async {
        let mut engine = open_engine().await;
        setup_option_schema(&mut engine).await;
        insert_option_users(&mut engine).await;

        let results = engine
            .execute("select * from main.users where display_name is main.Option.None;")
            .await
            .unwrap();
        let rows = get_rows(&results[0]);
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0][0], TempestValue::Int64(1));

        engine.shutdown().await.unwrap();
    });
}

#[test]
fn test_select_where_is_some_concrete() {
    setup_tracing();
    block_on(VirtualIo::default(), async {
        let mut engine = open_engine().await;
        setup_option_schema(&mut engine).await;
        insert_option_users(&mut engine).await;

        let results = engine
            .execute(r#"select * from main.users where display_name is main.Option.Some("John");"#)
            .await
            .unwrap();
        let rows = get_rows(&results[0]);
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0][0], TempestValue::Int64(3));

        engine.shutdown().await.unwrap();
    });
}

#[test]
fn test_select_where_is_some_wildcard_and_eq() {
    setup_tracing();
    block_on(VirtualIo::default(), async {
        let mut engine = open_engine().await;
        setup_option_schema(&mut engine).await;
        insert_option_users(&mut engine).await;

        let results = engine
            .execute(r#"select * from main.users where display_name is main.Option.Some(d) and d = "John";"#)
            .await
            .unwrap();
        let rows = get_rows(&results[0]);
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0][0], TempestValue::Int64(3));

        engine.shutdown().await.unwrap();
    });
}

#[test]
fn test_select_where_is_some_wildcard_no_filter() {
    setup_tracing();
    block_on(VirtualIo::default(), async {
        let mut engine = open_engine().await;
        setup_option_schema(&mut engine).await;
        insert_option_users(&mut engine).await;

        // Wildcard-only: matches any Some, regardless of inner value
        let results = engine
            .execute("select * from main.users where display_name is main.Option.Some(d);")
            .await
            .unwrap();
        let rows = get_rows(&results[0]);
        assert_eq!(rows.len(), 2); // Alice and John, not None

        engine.shutdown().await.unwrap();
    });
}

#[test]
fn test_delete_where_is_some_concrete() {
    setup_tracing();
    block_on(VirtualIo::default(), async {
        let mut engine = open_engine().await;
        setup_option_schema(&mut engine).await;
        insert_option_users(&mut engine).await;

        let del = engine
            .execute(r#"delete from main.users where display_name is main.Option.Some("Alice");"#)
            .await
            .unwrap();
        assert!(matches!(del[0], QueryResult::RowsChanged(1)));

        let remaining = engine.execute("select * from main.users;").await.unwrap();
        let rows = get_rows(&remaining[0]);
        assert_eq!(rows.len(), 2);
        assert_eq!(rows[0][0], TempestValue::Int64(1)); // None user remains
        assert_eq!(rows[1][0], TempestValue::Int64(3)); // John remains

        engine.shutdown().await.unwrap();
    });
}