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
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
// 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.

//! Integration tests for INSERT INTO ... SELECT support

use stoolap::Database;

fn create_test_db(name: &str) -> Database {
    Database::open(&format!("memory://{}", name)).expect("Failed to create in-memory database")
}

// ============================================================================
// Basic INSERT ... SELECT Tests
// ============================================================================

#[test]
fn test_insert_select_from_same_table() {
    let db = create_test_db("insert_select_same");

    db.execute(
        "CREATE TABLE numbers (id INTEGER PRIMARY KEY, value INTEGER)",
        (),
    )
    .unwrap();
    db.execute(
        "INSERT INTO numbers VALUES (1, 100), (2, 200), (3, 300)",
        (),
    )
    .unwrap();

    // Insert copy of existing rows with new IDs
    db.execute(
        "INSERT INTO numbers (id, value) SELECT id + 10, value * 2 FROM numbers",
        (),
    )
    .unwrap();

    let result = db.query("SELECT COUNT(*) FROM numbers", ()).unwrap();
    let mut count: Option<i64> = None;
    for row in result {
        let row = row.unwrap();
        count = Some(row.get(0).unwrap());
    }
    assert_eq!(count, Some(6));

    // Check the new rows
    let result = db
        .query(
            "SELECT id, value FROM numbers WHERE id > 10 ORDER BY id",
            (),
        )
        .unwrap();
    let mut rows: Vec<(i64, i64)> = Vec::new();
    for row in result {
        let row = row.unwrap();
        rows.push((row.get(0).unwrap(), row.get(1).unwrap()));
    }
    assert_eq!(rows.len(), 3);
    assert_eq!(rows[0], (11, 200));
    assert_eq!(rows[1], (12, 400));
    assert_eq!(rows[2], (13, 600));
}

#[test]
fn test_insert_select_from_another_table() {
    let db = create_test_db("insert_select_other");

    db.execute(
        "CREATE TABLE source (id INTEGER PRIMARY KEY, name TEXT)",
        (),
    )
    .unwrap();
    db.execute(
        "CREATE TABLE target (id INTEGER PRIMARY KEY, name TEXT)",
        (),
    )
    .unwrap();

    db.execute(
        "INSERT INTO source VALUES (1, 'Alice'), (2, 'Bob'), (3, 'Charlie')",
        (),
    )
    .unwrap();

    db.execute(
        "INSERT INTO target (id, name) SELECT id, name FROM source",
        (),
    )
    .unwrap();

    let result = db.query("SELECT COUNT(*) FROM target", ()).unwrap();
    let mut count: Option<i64> = None;
    for row in result {
        let row = row.unwrap();
        count = Some(row.get(0).unwrap());
    }
    assert_eq!(count, Some(3));

    let result = db.query("SELECT name FROM target ORDER BY id", ()).unwrap();
    let mut names: Vec<String> = Vec::new();
    for row in result {
        let row = row.unwrap();
        names.push(row.get(0).unwrap());
    }
    assert_eq!(names, vec!["Alice", "Bob", "Charlie"]);
}

#[test]
fn test_insert_select_with_where_clause() {
    let db = create_test_db("insert_select_where");

    db.execute(
        "CREATE TABLE source (id INTEGER PRIMARY KEY, value INTEGER)",
        (),
    )
    .unwrap();
    db.execute(
        "CREATE TABLE target (id INTEGER PRIMARY KEY, value INTEGER)",
        (),
    )
    .unwrap();

    db.execute(
        "INSERT INTO source VALUES (1, 10), (2, 20), (3, 30), (4, 40), (5, 50)",
        (),
    )
    .unwrap();

    // Only insert rows where value > 25
    db.execute(
        "INSERT INTO target (id, value) SELECT id, value FROM source WHERE value > 25",
        (),
    )
    .unwrap();

    let result = db.query("SELECT COUNT(*) FROM target", ()).unwrap();
    let mut count: Option<i64> = None;
    for row in result {
        let row = row.unwrap();
        count = Some(row.get(0).unwrap());
    }
    assert_eq!(count, Some(3));

    let result = db.query("SELECT id FROM target ORDER BY id", ()).unwrap();
    let mut ids: Vec<i64> = Vec::new();
    for row in result {
        let row = row.unwrap();
        ids.push(row.get(0).unwrap());
    }
    assert_eq!(ids, vec![3, 4, 5]);
}

// ============================================================================
// INSERT ... SELECT with VALUES Tests
// ============================================================================

#[test]
fn test_insert_select_from_values() {
    let db = create_test_db("insert_select_values");

    db.execute(
        "CREATE TABLE numbers (id INTEGER PRIMARY KEY, name TEXT)",
        (),
    )
    .unwrap();

    db.execute("INSERT INTO numbers (id, name) SELECT * FROM (VALUES (1, 'one'), (2, 'two'), (3, 'three')) AS t(id, name)", ())
        .unwrap();

    let result = db.query("SELECT COUNT(*) FROM numbers", ()).unwrap();
    let mut count: Option<i64> = None;
    for row in result {
        let row = row.unwrap();
        count = Some(row.get(0).unwrap());
    }
    assert_eq!(count, Some(3));

    let result = db
        .query("SELECT id, name FROM numbers ORDER BY id", ())
        .unwrap();
    let mut rows: Vec<(i64, String)> = Vec::new();
    for row in result {
        let row = row.unwrap();
        rows.push((row.get(0).unwrap(), row.get(1).unwrap()));
    }
    assert_eq!(rows[0], (1, "one".to_string()));
    assert_eq!(rows[1], (2, "two".to_string()));
    assert_eq!(rows[2], (3, "three".to_string()));
}

#[test]
fn test_insert_select_from_values_with_expressions() {
    let db = create_test_db("insert_select_values_expr");

    db.execute(
        "CREATE TABLE data (id INTEGER PRIMARY KEY, value INTEGER)",
        (),
    )
    .unwrap();

    db.execute("INSERT INTO data (id, value) SELECT x, x * x FROM (VALUES (1), (2), (3), (4), (5)) AS t(x)", ())
        .unwrap();

    let result = db
        .query("SELECT id, value FROM data ORDER BY id", ())
        .unwrap();
    let mut rows: Vec<(i64, i64)> = Vec::new();
    for row in result {
        let row = row.unwrap();
        rows.push((row.get(0).unwrap(), row.get(1).unwrap()));
    }
    assert_eq!(rows.len(), 5);
    assert_eq!(rows[0], (1, 1)); // 1^2
    assert_eq!(rows[1], (2, 4)); // 2^2
    assert_eq!(rows[2], (3, 9)); // 3^2
    assert_eq!(rows[3], (4, 16)); // 4^2
    assert_eq!(rows[4], (5, 25)); // 5^2
}

#[test]
fn test_insert_select_from_values_filtered() {
    let db = create_test_db("insert_select_values_filter");

    db.execute(
        "CREATE TABLE positive (id INTEGER PRIMARY KEY, num INTEGER)",
        (),
    )
    .unwrap();

    db.execute(
        "INSERT INTO positive (id, num)
         SELECT ROW_NUMBER() OVER (), x
         FROM (VALUES (-2), (-1), (0), (1), (2), (3)) AS t(x)
         WHERE x > 0",
        (),
    )
    .unwrap();

    let result = db
        .query("SELECT num FROM positive ORDER BY num", ())
        .unwrap();
    let mut nums: Vec<i64> = Vec::new();
    for row in result {
        let row = row.unwrap();
        nums.push(row.get(0).unwrap());
    }
    assert_eq!(nums, vec![1, 2, 3]);
}

// ============================================================================
// INSERT ... SELECT with Aggregation Tests
// ============================================================================

#[test]
fn test_insert_select_with_aggregation() {
    let db = create_test_db("insert_select_agg");

    db.execute(
        "CREATE TABLE sales (id INTEGER PRIMARY KEY, category TEXT, amount INTEGER)",
        (),
    )
    .unwrap();
    // Simple table without primary key constraint on category
    db.execute("CREATE TABLE summary (category TEXT, total INTEGER)", ())
        .unwrap();

    db.execute(
        "INSERT INTO sales VALUES
         (1, 'Electronics', 100),
         (2, 'Electronics', 200),
         (3, 'Books', 50),
         (4, 'Books', 75),
         (5, 'Clothing', 150)",
        (),
    )
    .unwrap();

    db.execute(
        "INSERT INTO summary (category, total)
         SELECT category, SUM(amount) FROM sales GROUP BY category",
        (),
    )
    .unwrap();

    let result = db
        .query("SELECT category, total FROM summary ORDER BY category", ())
        .unwrap();
    let mut rows: Vec<(String, i64)> = Vec::new();
    for row in result {
        let row = row.unwrap();
        rows.push((row.get(0).unwrap(), row.get(1).unwrap()));
    }
    assert_eq!(rows.len(), 3);
    assert_eq!(rows[0], ("Books".to_string(), 125));
    assert_eq!(rows[1], ("Clothing".to_string(), 150));
    assert_eq!(rows[2], ("Electronics".to_string(), 300));
}

#[test]
fn test_insert_select_with_count() {
    let db = create_test_db("insert_select_count");

    db.execute(
        "CREATE TABLE items (id INTEGER PRIMARY KEY, category TEXT)",
        (),
    )
    .unwrap();
    // Simple table without primary key constraint
    db.execute(
        "CREATE TABLE category_counts (category TEXT, item_count INTEGER)",
        (),
    )
    .unwrap();

    db.execute(
        "INSERT INTO items VALUES (1, 'A'), (2, 'A'), (3, 'B'), (4, 'A'), (5, 'C')",
        (),
    )
    .unwrap();

    db.execute(
        "INSERT INTO category_counts (category, item_count)
         SELECT category, COUNT(*) FROM items GROUP BY category",
        (),
    )
    .unwrap();

    let result = db
        .query(
            "SELECT category, item_count FROM category_counts ORDER BY category",
            (),
        )
        .unwrap();
    let mut rows: Vec<(String, i64)> = Vec::new();
    for row in result {
        let row = row.unwrap();
        rows.push((row.get(0).unwrap(), row.get(1).unwrap()));
    }
    assert_eq!(rows.len(), 3);
    assert_eq!(rows[0], ("A".to_string(), 3));
    assert_eq!(rows[1], ("B".to_string(), 1));
    assert_eq!(rows[2], ("C".to_string(), 1));
}

// ============================================================================
// INSERT ... SELECT with JOINs Tests
// ============================================================================

#[test]
fn test_insert_select_with_join() {
    let db = create_test_db("insert_select_join");

    db.execute("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)", ())
        .unwrap();
    db.execute(
        "CREATE TABLE orders (id INTEGER PRIMARY KEY, user_id INTEGER, amount INTEGER)",
        (),
    )
    .unwrap();
    // Simple table without primary key constraint
    db.execute(
        "CREATE TABLE user_totals (user_name TEXT, total_amount INTEGER)",
        (),
    )
    .unwrap();

    db.execute(
        "INSERT INTO users VALUES (1, 'Alice'), (2, 'Bob'), (3, 'Charlie')",
        (),
    )
    .unwrap();
    db.execute(
        "INSERT INTO orders VALUES (1, 1, 100), (2, 1, 50), (3, 2, 200), (4, 3, 75)",
        (),
    )
    .unwrap();

    db.execute(
        "INSERT INTO user_totals (user_name, total_amount)
         SELECT u.name, SUM(o.amount)
         FROM users u
         JOIN orders o ON u.id = o.user_id
         GROUP BY u.name",
        (),
    )
    .unwrap();

    let result = db
        .query(
            "SELECT user_name, total_amount FROM user_totals ORDER BY user_name",
            (),
        )
        .unwrap();
    let mut rows: Vec<(String, i64)> = Vec::new();
    for row in result {
        let row = row.unwrap();
        rows.push((row.get(0).unwrap(), row.get(1).unwrap()));
    }
    assert_eq!(rows.len(), 3);
    assert_eq!(rows[0], ("Alice".to_string(), 150));
    assert_eq!(rows[1], ("Bob".to_string(), 200));
    assert_eq!(rows[2], ("Charlie".to_string(), 75));
}

// ============================================================================
// INSERT ... SELECT with UNION Tests
// ============================================================================

#[test]
fn test_insert_select_with_union() {
    let db = create_test_db("insert_select_union");

    db.execute("CREATE TABLE t1 (id INTEGER PRIMARY KEY, val INTEGER)", ())
        .unwrap();
    db.execute("CREATE TABLE t2 (id INTEGER PRIMARY KEY, val INTEGER)", ())
        .unwrap();
    db.execute("CREATE TABLE combined (val INTEGER)", ())
        .unwrap();

    db.execute("INSERT INTO t1 VALUES (1, 10), (2, 20)", ())
        .unwrap();
    db.execute("INSERT INTO t2 VALUES (1, 20), (2, 30)", ())
        .unwrap();

    // UNION removes duplicate 20
    db.execute(
        "INSERT INTO combined (val) SELECT val FROM t1 UNION SELECT val FROM t2",
        (),
    )
    .unwrap();

    let result = db
        .query("SELECT val FROM combined ORDER BY val", ())
        .unwrap();
    let mut values: Vec<i64> = Vec::new();
    for row in result {
        let row = row.unwrap();
        values.push(row.get(0).unwrap());
    }
    assert_eq!(values, vec![10, 20, 30]);
}

#[test]
fn test_insert_select_with_union_all() {
    let db = create_test_db("insert_select_union_all");

    db.execute("CREATE TABLE t1 (id INTEGER PRIMARY KEY, val INTEGER)", ())
        .unwrap();
    db.execute("CREATE TABLE t2 (id INTEGER PRIMARY KEY, val INTEGER)", ())
        .unwrap();
    db.execute("CREATE TABLE combined (val INTEGER)", ())
        .unwrap();

    db.execute("INSERT INTO t1 VALUES (1, 10), (2, 20)", ())
        .unwrap();
    db.execute("INSERT INTO t2 VALUES (1, 20), (2, 30)", ())
        .unwrap();

    // UNION ALL keeps duplicate 20
    db.execute(
        "INSERT INTO combined (val) SELECT val FROM t1 UNION ALL SELECT val FROM t2",
        (),
    )
    .unwrap();

    let result = db
        .query("SELECT val FROM combined ORDER BY val", ())
        .unwrap();
    let mut values: Vec<i64> = Vec::new();
    for row in result {
        let row = row.unwrap();
        values.push(row.get(0).unwrap());
    }
    assert_eq!(values, vec![10, 20, 20, 30]);
}

// ============================================================================
// INSERT ... SELECT with CTE Tests
// ============================================================================

#[test]
fn test_insert_select_with_cte() {
    let db = create_test_db("insert_select_cte");

    db.execute(
        "CREATE TABLE results (id INTEGER PRIMARY KEY, value INTEGER)",
        (),
    )
    .unwrap();

    db.execute(
        "INSERT INTO results (id, value)
         WITH doubled AS (
             SELECT * FROM (VALUES (1, 10), (2, 20), (3, 30)) AS t(id, val)
         )
         SELECT id, val * 2 FROM doubled",
        (),
    )
    .unwrap();

    let result = db
        .query("SELECT id, value FROM results ORDER BY id", ())
        .unwrap();
    let mut rows: Vec<(i64, i64)> = Vec::new();
    for row in result {
        let row = row.unwrap();
        rows.push((row.get(0).unwrap(), row.get(1).unwrap()));
    }
    assert_eq!(rows.len(), 3);
    assert_eq!(rows[0], (1, 20));
    assert_eq!(rows[1], (2, 40));
    assert_eq!(rows[2], (3, 60));
}

// ============================================================================
// INSERT ... SELECT with Different Column Orders
// ============================================================================

#[test]
fn test_insert_select_column_reorder() {
    let db = create_test_db("insert_select_reorder");

    db.execute(
        "CREATE TABLE source (a INTEGER PRIMARY KEY, b TEXT, c INTEGER)",
        (),
    )
    .unwrap();
    db.execute(
        "CREATE TABLE target (x INTEGER, y INTEGER PRIMARY KEY, z TEXT)",
        (),
    )
    .unwrap();

    db.execute("INSERT INTO source VALUES (1, 'hello', 100)", ())
        .unwrap();

    // Insert with reordered columns
    db.execute(
        "INSERT INTO target (z, y, x) SELECT b, a, c FROM source",
        (),
    )
    .unwrap();

    let result = db.query("SELECT x, y, z FROM target", ()).unwrap();
    for row in result {
        let row = row.unwrap();
        let x: i64 = row.get(0).unwrap();
        let y: i64 = row.get(1).unwrap();
        let z: String = row.get(2).unwrap();
        assert_eq!(x, 100);
        assert_eq!(y, 1);
        assert_eq!(z, "hello");
    }
}

#[test]
fn test_insert_select_partial_columns() {
    let db = create_test_db("insert_select_partial");

    db.execute(
        "CREATE TABLE source (id INTEGER PRIMARY KEY, name TEXT, value INTEGER)",
        (),
    )
    .unwrap();
    db.execute(
        "CREATE TABLE target (id INTEGER PRIMARY KEY, name TEXT)",
        (),
    )
    .unwrap();

    db.execute(
        "INSERT INTO source VALUES (1, 'Alice', 100), (2, 'Bob', 200)",
        (),
    )
    .unwrap();

    // Only insert id and name, not value
    db.execute(
        "INSERT INTO target (id, name) SELECT id, name FROM source",
        (),
    )
    .unwrap();

    let result = db.query("SELECT COUNT(*) FROM target", ()).unwrap();
    let mut count: Option<i64> = None;
    for row in result {
        let row = row.unwrap();
        count = Some(row.get(0).unwrap());
    }
    assert_eq!(count, Some(2));
}

// ============================================================================
// INSERT ... SELECT with ORDER BY and LIMIT
// ============================================================================

#[test]
fn test_insert_select_with_limit_only() {
    let db = create_test_db("insert_select_limit_only");

    db.execute(
        "CREATE TABLE source (id INTEGER PRIMARY KEY, value INTEGER)",
        (),
    )
    .unwrap();
    db.execute("CREATE TABLE limited (value INTEGER)", ())
        .unwrap();

    db.execute(
        "INSERT INTO source VALUES (1, 50), (2, 30), (3, 80), (4, 10), (5, 90)",
        (),
    )
    .unwrap();

    // Insert first 3 rows (without ORDER BY)
    db.execute(
        "INSERT INTO limited (value) SELECT value FROM source LIMIT 3",
        (),
    )
    .unwrap();

    let result = db.query("SELECT COUNT(*) FROM limited", ()).unwrap();
    let mut count: Option<i64> = None;
    for row in result {
        let row = row.unwrap();
        count = Some(row.get(0).unwrap());
    }
    assert_eq!(count, Some(3));
}

// ============================================================================
// Large Dataset Tests
// ============================================================================

#[test]
fn test_insert_select_large_dataset() {
    let db = create_test_db("insert_select_large");

    db.execute("CREATE TABLE source (id INTEGER PRIMARY KEY)", ())
        .unwrap();
    db.execute("CREATE TABLE target (id INTEGER PRIMARY KEY)", ())
        .unwrap();

    // Insert 1000 rows using recursive CTE
    db.execute(
        "INSERT INTO source
         WITH RECURSIVE cnt(x) AS (
             SELECT 1
             UNION ALL
             SELECT x + 1 FROM cnt WHERE x < 1000
         )
         SELECT x FROM cnt",
        (),
    )
    .unwrap();

    // Copy all rows to target
    db.execute("INSERT INTO target (id) SELECT id FROM source", ())
        .unwrap();

    let result = db.query("SELECT COUNT(*) FROM target", ()).unwrap();
    let mut count: Option<i64> = None;
    for row in result {
        let row = row.unwrap();
        count = Some(row.get(0).unwrap());
    }
    assert_eq!(count, Some(1000));

    // Verify min and max
    let result = db.query("SELECT MIN(id), MAX(id) FROM target", ()).unwrap();
    for row in result {
        let row = row.unwrap();
        let min: i64 = row.get(0).unwrap();
        let max: i64 = row.get(1).unwrap();
        assert_eq!(min, 1);
        assert_eq!(max, 1000);
    }
}

// ============================================================================
// Edge Cases
// ============================================================================

#[test]
fn test_insert_select_empty_result() {
    let db = create_test_db("insert_select_empty");

    db.execute(
        "CREATE TABLE source (id INTEGER PRIMARY KEY, value INTEGER)",
        (),
    )
    .unwrap();
    db.execute(
        "CREATE TABLE target (id INTEGER PRIMARY KEY, value INTEGER)",
        (),
    )
    .unwrap();

    db.execute("INSERT INTO source VALUES (1, 100)", ())
        .unwrap();

    // Insert with WHERE that matches nothing
    db.execute(
        "INSERT INTO target (id, value) SELECT id, value FROM source WHERE value > 1000",
        (),
    )
    .unwrap();

    let result = db.query("SELECT COUNT(*) FROM target", ()).unwrap();
    let mut count: Option<i64> = None;
    for row in result {
        let row = row.unwrap();
        count = Some(row.get(0).unwrap());
    }
    assert_eq!(count, Some(0));
}

#[test]
fn test_insert_select_single_row() {
    let db = create_test_db("insert_select_single");

    db.execute(
        "CREATE TABLE source (id INTEGER PRIMARY KEY, name TEXT)",
        (),
    )
    .unwrap();
    db.execute(
        "CREATE TABLE target (id INTEGER PRIMARY KEY, name TEXT)",
        (),
    )
    .unwrap();

    db.execute("INSERT INTO source VALUES (1, 'only')", ())
        .unwrap();

    db.execute(
        "INSERT INTO target (id, name) SELECT id, name FROM source",
        (),
    )
    .unwrap();

    let result = db.query("SELECT name FROM target", ()).unwrap();
    let mut names: Vec<String> = Vec::new();
    for row in result {
        let row = row.unwrap();
        names.push(row.get(0).unwrap());
    }
    assert_eq!(names, vec!["only"]);
}

// ============================================================================
// INSERT ... SELECT with DEFAULT Values (Regression Tests)
// ============================================================================

/// Regression test for DEFAULT values in INSERT...SELECT
/// Previously, INSERT...SELECT would not apply DEFAULT values for omitted columns,
/// instead inserting NULL. This test ensures DEFAULT values are properly applied.
#[test]
fn test_insert_select_with_default_values() {
    let db = create_test_db("insert_select_default");

    // Source table without DEFAULT
    db.execute(
        "CREATE TABLE source (id INTEGER PRIMARY KEY, value INTEGER)",
        (),
    )
    .unwrap();

    // Target table with DEFAULT values on multiple columns
    db.execute(
        "CREATE TABLE target (
            id INTEGER PRIMARY KEY,
            value INTEGER,
            status TEXT DEFAULT 'active',
            priority INTEGER DEFAULT 5,
            category TEXT DEFAULT 'general'
        )",
        (),
    )
    .unwrap();

    db.execute("INSERT INTO source VALUES (1, 100), (2, 200), (3, 300)", ())
        .unwrap();

    // INSERT...SELECT omitting columns that have DEFAULT values
    db.execute(
        "INSERT INTO target (id, value) SELECT id, value FROM source",
        (),
    )
    .unwrap();

    // Verify DEFAULT values were applied
    let result = db
        .query(
            "SELECT id, value, status, priority, category FROM target ORDER BY id",
            (),
        )
        .unwrap();
    let mut rows: Vec<(i64, i64, String, i64, String)> = Vec::new();
    for row in result {
        let row = row.unwrap();
        rows.push((
            row.get(0).unwrap(),
            row.get(1).unwrap(),
            row.get(2).unwrap(),
            row.get(3).unwrap(),
            row.get(4).unwrap(),
        ));
    }

    assert_eq!(rows.len(), 3);
    // All rows should have the DEFAULT values for status, priority, and category
    assert_eq!(
        rows[0],
        (1, 100, "active".to_string(), 5, "general".to_string())
    );
    assert_eq!(
        rows[1],
        (2, 200, "active".to_string(), 5, "general".to_string())
    );
    assert_eq!(
        rows[2],
        (3, 300, "active".to_string(), 5, "general".to_string())
    );
}

/// Test that DEFAULT values work correctly when INSERT...SELECT uses expressions
#[test]
fn test_insert_select_default_with_expressions() {
    let db = create_test_db("insert_select_default_expr");

    db.execute(
        "CREATE TABLE source (id INTEGER PRIMARY KEY, name TEXT)",
        (),
    )
    .unwrap();

    db.execute(
        "CREATE TABLE target (
            id INTEGER PRIMARY KEY,
            name TEXT,
            created_at TEXT DEFAULT 'now',
            is_active INTEGER DEFAULT 1
        )",
        (),
    )
    .unwrap();

    db.execute("INSERT INTO source VALUES (1, 'Alice'), (2, 'Bob')", ())
        .unwrap();

    // INSERT...SELECT with expression in SELECT list
    db.execute(
        "INSERT INTO target (id, name) SELECT id * 10, UPPER(name) FROM source",
        (),
    )
    .unwrap();

    let result = db
        .query(
            "SELECT id, name, created_at, is_active FROM target ORDER BY id",
            (),
        )
        .unwrap();
    let mut rows: Vec<(i64, String, String, i64)> = Vec::new();
    for row in result {
        let row = row.unwrap();
        rows.push((
            row.get(0).unwrap(),
            row.get(1).unwrap(),
            row.get(2).unwrap(),
            row.get(3).unwrap(),
        ));
    }

    assert_eq!(rows.len(), 2);
    assert_eq!(rows[0], (10, "ALICE".to_string(), "now".to_string(), 1));
    assert_eq!(rows[1], (20, "BOB".to_string(), "now".to_string(), 1));
}

/// Test INSERT...SELECT with DEFAULT value and explicit NULL override
#[test]
fn test_insert_select_default_vs_explicit_null() {
    let db = create_test_db("insert_select_default_null");

    db.execute(
        "CREATE TABLE source (id INTEGER PRIMARY KEY, val INTEGER)",
        (),
    )
    .unwrap();

    db.execute(
        "CREATE TABLE target (
            id INTEGER PRIMARY KEY,
            val INTEGER,
            status TEXT DEFAULT 'pending'
        )",
        (),
    )
    .unwrap();

    db.execute("INSERT INTO source VALUES (1, NULL), (2, 100)", ())
        .unwrap();

    // INSERT...SELECT - status should get DEFAULT value 'pending'
    db.execute(
        "INSERT INTO target (id, val) SELECT id, val FROM source",
        (),
    )
    .unwrap();

    let result = db
        .query("SELECT id, val, status FROM target ORDER BY id", ())
        .unwrap();

    let mut row1_status: Option<String> = None;
    let mut row2_val: Option<i64> = None;
    let mut row2_status: Option<String> = None;
    let mut count = 0;

    for row in result {
        let row = row.unwrap();
        let id: i64 = row.get(0).unwrap();
        if id == 1 {
            // val is NULL from source, status should be DEFAULT
            row1_status = Some(row.get::<String>(2).unwrap());
        } else if id == 2 {
            row2_val = Some(row.get(1).unwrap());
            row2_status = Some(row.get(2).unwrap());
        }
        count += 1;
    }

    assert_eq!(count, 2);
    // status should be 'pending' (DEFAULT value), not NULL
    assert_eq!(row1_status, Some("pending".to_string()));
    assert_eq!(row2_val, Some(100));
    assert_eq!(row2_status, Some("pending".to_string()));
}

/// Test that regular INSERT and INSERT...SELECT both apply DEFAULT values consistently
#[test]
fn test_insert_select_default_consistency() {
    let db = create_test_db("insert_select_default_consistent");

    db.execute(
        "CREATE TABLE mixed (
            id INTEGER PRIMARY KEY,
            value INTEGER,
            tag TEXT DEFAULT 'default_tag'
        )",
        (),
    )
    .unwrap();

    // Regular INSERT omitting 'tag' column
    db.execute("INSERT INTO mixed (id, value) VALUES (1, 100)", ())
        .unwrap();

    // INSERT...SELECT omitting 'tag' column
    db.execute("INSERT INTO mixed (id, value) SELECT 2, 200", ())
        .unwrap();

    let result = db
        .query("SELECT id, value, tag FROM mixed ORDER BY id", ())
        .unwrap();
    let mut rows: Vec<(i64, i64, String)> = Vec::new();
    for row in result {
        let row = row.unwrap();
        rows.push((
            row.get(0).unwrap(),
            row.get(1).unwrap(),
            row.get(2).unwrap(),
        ));
    }

    assert_eq!(rows.len(), 2);
    // Both regular INSERT and INSERT...SELECT should apply the same DEFAULT value
    assert_eq!(rows[0], (1, 100, "default_tag".to_string()));
    assert_eq!(rows[1], (2, 200, "default_tag".to_string()));
}