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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
// 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.

//! ROLLUP, CUBE, and GROUPING SETS Tests
//!
//! Tests for GROUP BY ROLLUP, GROUP BY CUBE, and GROUPING SETS functionality

use stoolap::Database;

fn setup_sales_table(db: &Database) {
    db.execute(
        "CREATE TABLE sales (region TEXT, product TEXT, amount FLOAT)",
        (),
    )
    .expect("Failed to create table");

    let inserts = [
        "INSERT INTO sales VALUES ('East', 'A', 100.0)",
        "INSERT INTO sales VALUES ('East', 'B', 150.0)",
        "INSERT INTO sales VALUES ('West', 'A', 200.0)",
        "INSERT INTO sales VALUES ('West', 'B', 250.0)",
    ];

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

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

    let result = db
        .query(
            "SELECT region, product, SUM(amount) FROM sales GROUP BY ROLLUP(region, product)",
            (),
        )
        .expect("Failed to query");

    let mut row_count = 0;
    let mut has_grand_total = false;
    let mut has_region_subtotal = false;
    let mut has_detail = false;

    for row in result {
        let row = row.expect("Failed to get row");
        row_count += 1;

        let region: Option<String> = row.get(0).unwrap();
        let product: Option<String> = row.get(1).unwrap();
        let sum: f64 = row.get(2).unwrap();

        if region.is_none() && product.is_none() {
            // Grand total row
            has_grand_total = true;
            assert!(
                (sum - 700.0).abs() < 0.01,
                "Grand total should be 700, got {}",
                sum
            );
        } else if region.is_some() && product.is_none() {
            // Region subtotal
            has_region_subtotal = true;
            let r = region.unwrap();
            if r == "East" {
                assert!(
                    (sum - 250.0).abs() < 0.01,
                    "East subtotal should be 250, got {}",
                    sum
                );
            } else if r == "West" {
                assert!(
                    (sum - 450.0).abs() < 0.01,
                    "West subtotal should be 450, got {}",
                    sum
                );
            }
        } else {
            // Detail row
            has_detail = true;
        }
    }

    assert_eq!(
        row_count, 7,
        "ROLLUP should produce 7 rows (4 detail + 2 subtotal + 1 grand total)"
    );
    assert!(has_grand_total, "Should have a grand total row");
    assert!(has_region_subtotal, "Should have region subtotal rows");
    assert!(has_detail, "Should have detail rows");
}

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

    let result = db
        .query(
            "SELECT region, SUM(amount) FROM sales GROUP BY ROLLUP(region)",
            (),
        )
        .expect("Failed to query");

    let mut row_count = 0;
    let mut has_grand_total = false;

    for row in result {
        let row = row.expect("Failed to get row");
        row_count += 1;

        let region: Option<String> = row.get(0).unwrap();
        let sum: f64 = row.get(1).unwrap();

        if region.is_none() {
            has_grand_total = true;
            assert!(
                (sum - 700.0).abs() < 0.01,
                "Grand total should be 700, got {}",
                sum
            );
        }
    }

    assert_eq!(
        row_count, 3,
        "ROLLUP(region) should produce 3 rows (2 regions + 1 grand total)"
    );
    assert!(has_grand_total, "Should have a grand total row");
}

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

    let result = db
        .query(
            "SELECT region, product, SUM(amount) FROM sales GROUP BY CUBE(region, product)",
            (),
        )
        .expect("Failed to query");

    let mut row_count = 0;
    let mut has_grand_total = false;
    let mut has_region_subtotal = false;
    let mut has_product_subtotal = false;

    for row in result {
        let row = row.expect("Failed to get row");
        row_count += 1;

        let region: Option<String> = row.get(0).unwrap();
        let product: Option<String> = row.get(1).unwrap();
        let sum: f64 = row.get(2).unwrap();

        if region.is_none() && product.is_none() {
            has_grand_total = true;
            assert!(
                (sum - 700.0).abs() < 0.01,
                "Grand total should be 700, got {}",
                sum
            );
        } else if region.is_some() && product.is_none() {
            has_region_subtotal = true;
        } else if let (None, Some(p)) = (&region, &product) {
            has_product_subtotal = true;
            if p == "A" {
                assert!(
                    (sum - 300.0).abs() < 0.01,
                    "Product A total should be 300, got {}",
                    sum
                );
            } else if p == "B" {
                assert!(
                    (sum - 400.0).abs() < 0.01,
                    "Product B total should be 400, got {}",
                    sum
                );
            }
        }
    }

    assert_eq!(
        row_count, 9,
        "CUBE should produce 9 rows (4 detail + 2 region + 2 product + 1 grand)"
    );
    assert!(has_grand_total, "Should have a grand total row");
    assert!(has_region_subtotal, "Should have region subtotal rows");
    assert!(has_product_subtotal, "Should have product subtotal rows");
}

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

    let result = db
        .query(
            "SELECT region, product, COUNT(*) FROM sales GROUP BY ROLLUP(region, product)",
            (),
        )
        .expect("Failed to query");

    let mut grand_total_count = 0i64;

    for row in result {
        let row = row.expect("Failed to get row");
        let region: Option<String> = row.get(0).unwrap();
        let product: Option<String> = row.get(1).unwrap();
        let count: i64 = row.get(2).unwrap();

        if region.is_none() && product.is_none() {
            grand_total_count = count;
        }
    }

    assert_eq!(grand_total_count, 4, "Grand total COUNT(*) should be 4");
}

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

    let result = db
        .query(
            "SELECT region, SUM(amount), AVG(amount), COUNT(*) FROM sales GROUP BY ROLLUP(region)",
            (),
        )
        .expect("Failed to query");

    let mut has_grand_total = false;

    for row in result {
        let row = row.expect("Failed to get row");
        let region: Option<String> = row.get(0).unwrap();
        let sum: f64 = row.get(1).unwrap();
        let avg: f64 = row.get(2).unwrap();
        let count: i64 = row.get(3).unwrap();

        if region.is_none() {
            has_grand_total = true;
            assert!((sum - 700.0).abs() < 0.01, "Grand total SUM should be 700");
            assert!((avg - 175.0).abs() < 0.01, "Grand total AVG should be 175");
            assert_eq!(count, 4, "Grand total COUNT should be 4");
        }
    }

    assert!(has_grand_total, "Should have a grand total row");
}

#[test]
fn test_regular_group_by_unchanged() {
    // Ensure regular GROUP BY still works as before
    let db = Database::open("memory://regular_group_by").expect("Failed to create database");
    setup_sales_table(&db);

    let result = db
        .query("SELECT region, SUM(amount) FROM sales GROUP BY region", ())
        .expect("Failed to query");

    let mut row_count = 0;
    for row in result {
        let row = row.expect("Failed to get row");
        row_count += 1;
        let region: String = row.get(0).unwrap();
        let sum: f64 = row.get(1).unwrap();

        // Regular GROUP BY should not have NULL region values
        assert!(!region.is_empty(), "Region should not be empty");
        assert!(sum > 0.0, "Sum should be positive");
    }

    assert_eq!(
        row_count, 2,
        "Regular GROUP BY should produce 2 rows (East and West)"
    );
}

// ============================================================================
// GROUPING SETS Tests
// ============================================================================

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

    // GROUPING SETS ((region, product), (region), ()) is equivalent to ROLLUP(region, product)
    let result = db
        .query(
            "SELECT region, product, SUM(amount) FROM sales GROUP BY GROUPING SETS ((region, product), (region), ())",
            (),
        )
        .expect("Failed to query");

    let mut row_count = 0;
    let mut has_grand_total = false;
    let mut has_region_subtotal = false;
    let mut has_detail = false;

    for row in result {
        let row = row.expect("Failed to get row");
        row_count += 1;

        let region: Option<String> = row.get(0).unwrap();
        let product: Option<String> = row.get(1).unwrap();
        let sum: f64 = row.get(2).unwrap();

        if region.is_none() && product.is_none() {
            has_grand_total = true;
            assert!(
                (sum - 700.0).abs() < 0.01,
                "Grand total should be 700, got {}",
                sum
            );
        } else if region.is_some() && product.is_none() {
            has_region_subtotal = true;
        } else {
            has_detail = true;
        }
    }

    assert_eq!(
        row_count, 7,
        "GROUPING SETS should produce 7 rows (4 detail + 2 region subtotal + 1 grand total)"
    );
    assert!(has_grand_total, "Should have a grand total row");
    assert!(has_region_subtotal, "Should have region subtotal rows");
    assert!(has_detail, "Should have detail rows");
}

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

    // GROUPING SETS ((region), ()) produces region groups + grand total
    let result = db
        .query(
            "SELECT region, SUM(amount) FROM sales GROUP BY GROUPING SETS ((region), ())",
            (),
        )
        .expect("Failed to query");

    let mut row_count = 0;
    let mut has_grand_total = false;

    for row in result {
        let row = row.expect("Failed to get row");
        row_count += 1;

        let region: Option<String> = row.get(0).unwrap();
        let sum: f64 = row.get(1).unwrap();

        if region.is_none() {
            has_grand_total = true;
            assert!(
                (sum - 700.0).abs() < 0.01,
                "Grand total should be 700, got {}",
                sum
            );
        }
    }

    assert_eq!(
        row_count, 3,
        "GROUPING SETS ((region), ()) should produce 3 rows"
    );
    assert!(has_grand_total, "Should have a grand total row");
}

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

    // GROUPING SETS without empty set - no grand total
    let result = db
        .query(
            "SELECT region, product, SUM(amount) FROM sales GROUP BY GROUPING SETS ((region), (product))",
            (),
        )
        .expect("Failed to query");

    let mut row_count = 0;
    let mut has_grand_total = false;

    for row in result {
        let row = row.expect("Failed to get row");
        row_count += 1;

        let region: Option<String> = row.get(0).unwrap();
        let product: Option<String> = row.get(1).unwrap();

        // Should NOT have a row where both are NULL
        if region.is_none() && product.is_none() {
            has_grand_total = true;
        }
    }

    assert_eq!(
        row_count, 4,
        "Should produce 4 rows (2 regions + 2 products)"
    );
    assert!(
        !has_grand_total,
        "Should NOT have grand total (no empty set)"
    );
}

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

    let result = db
        .query(
            "SELECT region, product, SUM(amount), GROUPING(region), GROUPING(product) FROM sales GROUP BY GROUPING SETS ((region, product), (region), ())",
            (),
        )
        .expect("Failed to query");

    let mut grand_total_groupings = None;
    let mut region_subtotal_groupings = None;
    let mut detail_groupings = None;

    for row in result {
        let row = row.expect("Failed to get row");
        let region: Option<String> = row.get(0).unwrap();
        let product: Option<String> = row.get(1).unwrap();
        let g_region: i64 = row.get(3).unwrap();
        let g_product: i64 = row.get(4).unwrap();

        if region.is_none() && product.is_none() {
            grand_total_groupings = Some((g_region, g_product));
        } else if region.is_some() && product.is_none() {
            region_subtotal_groupings = Some((g_region, g_product));
        } else if region.is_some() && product.is_some() {
            detail_groupings = Some((g_region, g_product));
        }
    }

    // Grand total: both columns are rolled up
    assert_eq!(
        grand_total_groupings,
        Some((1, 1)),
        "Grand total should have GROUPING(region)=1, GROUPING(product)=1"
    );

    // Region subtotal: product is rolled up
    assert_eq!(
        region_subtotal_groupings,
        Some((0, 1)),
        "Region subtotal should have GROUPING(region)=0, GROUPING(product)=1"
    );

    // Detail row: neither rolled up
    assert_eq!(
        detail_groupings,
        Some((0, 0)),
        "Detail row should have GROUPING(region)=0, GROUPING(product)=0"
    );
}

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

    // Compare GROUPING SETS result with equivalent ROLLUP
    let rollup_result = db
        .query(
            "SELECT region, product, SUM(amount) FROM sales GROUP BY ROLLUP(region, product) ORDER BY region, product",
            (),
        )
        .expect("Failed to query ROLLUP");

    let grouping_sets_result = db
        .query(
            "SELECT region, product, SUM(amount) FROM sales GROUP BY GROUPING SETS ((region, product), (region), ()) ORDER BY region, product",
            (),
        )
        .expect("Failed to query GROUPING SETS");

    let rollup_rows: Vec<_> = rollup_result.collect();
    let gs_rows: Vec<_> = grouping_sets_result.collect();

    assert_eq!(
        rollup_rows.len(),
        gs_rows.len(),
        "ROLLUP and equivalent GROUPING SETS should have same row count"
    );

    // Both should produce the same aggregated results
    for (r_row, gs_row) in rollup_rows.iter().zip(gs_rows.iter()) {
        let r_row = r_row.as_ref().expect("ROLLUP row error");
        let gs_row = gs_row.as_ref().expect("GROUPING SETS row error");

        let r_sum: f64 = r_row.get(2).unwrap();
        let gs_sum: f64 = gs_row.get(2).unwrap();

        assert!(
            (r_sum - gs_sum).abs() < 0.01,
            "Sums should match: ROLLUP={}, GROUPING SETS={}",
            r_sum,
            gs_sum
        );
    }
}

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

    let result = db
        .query(
            "SELECT region, SUM(amount), AVG(amount), COUNT(*) FROM sales GROUP BY GROUPING SETS ((region), ())",
            (),
        )
        .expect("Failed to query");

    let mut has_grand_total = false;

    for row in result {
        let row = row.expect("Failed to get row");
        let region: Option<String> = row.get(0).unwrap();
        let sum: f64 = row.get(1).unwrap();
        let avg: f64 = row.get(2).unwrap();
        let count: i64 = row.get(3).unwrap();

        if region.is_none() {
            has_grand_total = true;
            assert!((sum - 700.0).abs() < 0.01, "Grand total SUM should be 700");
            assert!((avg - 175.0).abs() < 0.01, "Grand total AVG should be 175");
            assert_eq!(count, 4, "Grand total COUNT should be 4");
        }
    }

    assert!(has_grand_total, "Should have a grand total row");
}

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

    // Empty set only - just grand total
    let result = db
        .query(
            "SELECT SUM(amount), COUNT(*) FROM sales GROUP BY GROUPING SETS (())",
            (),
        )
        .expect("Failed to query");

    let mut row_count = 0;
    for row in result {
        let row = row.expect("Failed to get row");
        row_count += 1;

        let sum: f64 = row.get(0).unwrap();
        let count: i64 = row.get(1).unwrap();

        assert!((sum - 700.0).abs() < 0.01, "Sum should be 700");
        assert_eq!(count, 4, "Count should be 4");
    }

    assert_eq!(row_count, 1, "Should have exactly 1 row (grand total)");
}

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

    let result = db
        .query(
            "SELECT region, SUM(amount) FROM sales GROUP BY GROUPING SETS ((region), ()) HAVING SUM(amount) > 300",
            (),
        )
        .expect("Failed to query");

    let mut row_count = 0;
    for row in result {
        let row = row.expect("Failed to get row");
        row_count += 1;

        let sum: f64 = row.get(1).unwrap();
        assert!(sum > 300.0, "All rows should have SUM > 300 due to HAVING");
    }

    // East=250 (filtered out), West=450, Grand=700
    assert_eq!(
        row_count, 2,
        "Should have 2 rows after HAVING filter (West + Grand)"
    );
}

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

    let result = db
        .query(
            "SELECT region, SUM(amount) FROM sales WHERE amount >= 150 GROUP BY GROUPING SETS ((region), ())",
            (),
        )
        .expect("Failed to query");

    let mut grand_total = 0.0;
    for row in result {
        let row = row.expect("Failed to get row");
        let region: Option<String> = row.get(0).unwrap();
        let sum: f64 = row.get(1).unwrap();

        if region.is_none() {
            grand_total = sum;
        }
    }

    // Only rows with amount >= 150: East-B(150), West-A(200), West-B(250) = 600
    assert!(
        (grand_total - 600.0).abs() < 0.01,
        "Grand total with WHERE should be 600, got {}",
        grand_total
    );
}

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

    db.execute(
        "CREATE TABLE orders (region TEXT, product TEXT, channel TEXT, amount FLOAT)",
        (),
    )
    .expect("Failed to create table");

    let inserts = [
        "INSERT INTO orders VALUES ('East', 'A', 'Online', 100.0)",
        "INSERT INTO orders VALUES ('East', 'B', 'Store', 150.0)",
        "INSERT INTO orders VALUES ('West', 'A', 'Online', 200.0)",
        "INSERT INTO orders VALUES ('West', 'B', 'Store', 250.0)",
    ];

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

    let result = db
        .query(
            "SELECT region, product, channel, SUM(amount) FROM orders GROUP BY GROUPING SETS ((region, product, channel), (region, product), (region), ())",
            (),
        )
        .expect("Failed to query");

    let mut row_count = 0;
    let mut has_grand_total = false;
    let mut has_region_only = false;
    let mut has_region_product = false;
    let mut has_all_three = false;

    for row in result {
        let row = row.expect("Failed to get row");
        row_count += 1;

        let region: Option<String> = row.get(0).unwrap();
        let product: Option<String> = row.get(1).unwrap();
        let channel: Option<String> = row.get(2).unwrap();

        match (&region, &product, &channel) {
            (None, None, None) => has_grand_total = true,
            (Some(_), None, None) => has_region_only = true,
            (Some(_), Some(_), None) => has_region_product = true,
            (Some(_), Some(_), Some(_)) => has_all_three = true,
            _ => {}
        }
    }

    // 4 detail + 4 region-product + 2 region + 1 grand = 11
    assert_eq!(
        row_count, 11,
        "Should produce 11 rows for this GROUPING SETS"
    );
    assert!(has_grand_total, "Should have grand total");
    assert!(has_region_only, "Should have region-only rows");
    assert!(has_region_product, "Should have region-product rows");
    assert!(has_all_three, "Should have all-three-columns rows");
}