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
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
// 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 SQL Tests
//!
//! Comprehensive integration tests for SQL functionality

use stoolap::Database;

/// Test basic aggregations (COUNT, SUM, AVG, MIN, MAX)
#[test]
fn test_basic_aggregations() {
    let db = Database::open("memory://int_agg").expect("Failed to create database");

    db.execute(
        "CREATE TABLE sales_data (
            id INT PRIMARY KEY,
            product TEXT,
            category TEXT,
            amount FLOAT,
            quantity INT,
            sale_date TIMESTAMP
        )",
        (),
    )
    .expect("Failed to create table");

    db.execute(
        "INSERT INTO sales_data VALUES
            (1, 'Laptop', 'Electronics', 1200.50, 2, TIMESTAMP '2024-01-15 10:00:00'),
            (2, 'Mouse', 'Electronics', 25.99, 5, TIMESTAMP '2024-01-15 11:00:00'),
            (3, 'Desk', 'Furniture', 350.00, 1, TIMESTAMP '2024-01-16 09:00:00'),
            (4, 'Chair', 'Furniture', 150.00, 4, TIMESTAMP '2024-01-16 10:00:00'),
            (5, 'Keyboard', 'Electronics', 79.99, 3, TIMESTAMP '2024-01-17 14:00:00')",
        (),
    )
    .expect("Failed to insert data");

    // Test COUNT
    let count: i64 = db
        .query_one("SELECT COUNT(*) FROM sales_data", ())
        .expect("Failed to execute COUNT");
    assert_eq!(count, 5, "Expected COUNT(*) = 5");

    // Test SUM
    let total: f64 = db
        .query_one("SELECT SUM(amount) FROM sales_data", ())
        .expect("Failed to execute SUM");
    assert!(
        (total - 1806.48).abs() < 0.01,
        "Expected SUM(amount) = 1806.48, got {}",
        total
    );

    // Test AVG
    let avg: f64 = db
        .query_one("SELECT AVG(amount) FROM sales_data", ())
        .expect("Failed to execute AVG");
    assert!(
        (avg - 361.296).abs() < 0.01,
        "Expected AVG(amount) = 361.296, got {}",
        avg
    );

    // Test MIN/MAX
    let min: f64 = db
        .query_one("SELECT MIN(amount) FROM sales_data", ())
        .expect("Failed to execute MIN");
    let max: f64 = db
        .query_one("SELECT MAX(amount) FROM sales_data", ())
        .expect("Failed to execute MAX");
    assert!(
        (min - 25.99).abs() < 0.01,
        "Expected MIN=25.99, got {}",
        min
    );
    assert!(
        (max - 1200.50).abs() < 0.01,
        "Expected MAX=1200.50, got {}",
        max
    );
}

/// Test GROUP BY operations
#[test]
fn test_group_by_operations() {
    let db = Database::open("memory://int_group").expect("Failed to create database");

    db.execute(
        "CREATE TABLE product_sales (
            id INT PRIMARY KEY,
            category TEXT,
            product TEXT,
            amount FLOAT,
            region TEXT
        )",
        (),
    )
    .expect("Failed to create table");

    db.execute(
        "INSERT INTO product_sales VALUES
            (1, 'Electronics', 'Laptop', 1200.0, 'North'),
            (2, 'Electronics', 'Phone', 800.0, 'North'),
            (3, 'Electronics', 'Tablet', 600.0, 'South'),
            (4, 'Furniture', 'Desk', 400.0, 'North'),
            (5, 'Furniture', 'Chair', 200.0, 'South'),
            (6, 'Furniture', 'Shelf', 150.0, 'South')",
        (),
    )
    .expect("Failed to insert data");

    // Test GROUP BY with aggregation
    let result = db
        .query(
            "SELECT category, COUNT(*) as cnt, SUM(amount) as total
             FROM product_sales
             GROUP BY category
             ORDER BY category",
            (),
        )
        .expect("Failed to execute GROUP BY query");

    let mut results: Vec<(String, i64, f64)> = Vec::new();
    for row in result {
        let row = row.expect("Failed to get row");
        let category: String = row.get(0).unwrap();
        let count: i64 = row.get(1).unwrap();
        let total: f64 = row.get(2).unwrap();
        results.push((category, count, total));
    }

    assert_eq!(results.len(), 2, "Expected 2 groups");
    assert_eq!(results[0].0, "Electronics");
    assert_eq!(results[0].1, 3);
    assert!((results[0].2 - 2600.0).abs() < 0.01);
    assert_eq!(results[1].0, "Furniture");
    assert_eq!(results[1].1, 3);
    assert!((results[1].2 - 750.0).abs() < 0.01);

    // Test HAVING clause - using alias reference
    let result = db
        .query(
            "SELECT category, SUM(amount) as total
             FROM product_sales
             GROUP BY category
             HAVING total > 1000",
            (),
        )
        .expect("Failed to execute HAVING query");

    let mut having_results: Vec<(String, f64)> = Vec::new();
    for row in result {
        let row = row.expect("Failed to get row");
        let category: String = row.get(0).unwrap();
        let total: f64 = row.get(1).unwrap();
        having_results.push((category, total));
    }

    assert_eq!(having_results.len(), 1);
    assert_eq!(having_results[0].0, "Electronics");
    assert!((having_results[0].1 - 2600.0).abs() < 0.01);
}

/// Test complex expressions
#[test]
fn test_complex_expressions() {
    let db = Database::open("memory://int_expr").expect("Failed to create database");

    db.execute(
        "CREATE TABLE expr_test (
            id INT PRIMARY KEY,
            a INT,
            b INT,
            c FLOAT,
            d TEXT
        )",
        (),
    )
    .expect("Failed to create table");

    db.execute(
        "INSERT INTO expr_test VALUES
            (1, 10, 5, 2.5, 'hello'),
            (2, 20, 8, 3.5, 'world'),
            (3, 15, 3, 4.5, 'test')",
        (),
    )
    .expect("Failed to insert data");

    // Test arithmetic expressions
    let result: f64 = db
        .query_one("SELECT (a + b) FROM expr_test WHERE id = 1", ())
        .expect("Failed to execute arithmetic expression");
    assert!((result - 15.0).abs() < 0.01, "Expected 15, got {}", result);

    // Test complex WHERE with AND/OR
    let count: i64 = db
        .query_one(
            "SELECT COUNT(*) FROM expr_test WHERE (a > 10 AND b < 10) OR c > 4.0",
            (),
        )
        .expect("Failed to execute complex WHERE");
    assert_eq!(count, 2, "Expected 2 rows");
}

/// Test string functions
#[test]
fn test_string_functions() {
    let db = Database::open("memory://int_string").expect("Failed to create database");

    db.execute(
        "CREATE TABLE string_test (
            id INT PRIMARY KEY,
            text1 TEXT,
            text2 TEXT
        )",
        (),
    )
    .expect("Failed to create table");

    db.execute(
        "INSERT INTO string_test VALUES
            (1, 'Hello', 'World'),
            (2, 'STOOLAP', 'Database'),
            (3, 'Test', 'String')",
        (),
    )
    .expect("Failed to insert data");

    // Test UPPER/LOWER
    let result = db
        .query(
            "SELECT UPPER(text1), LOWER(text2) FROM string_test WHERE id = 1",
            (),
        )
        .expect("Failed to execute UPPER/LOWER");
    let mut row_data: Option<(String, String)> = None;
    for row in result {
        let row = row.expect("Failed to get row");
        let upper: String = row.get(0).unwrap();
        let lower: String = row.get(1).unwrap();
        row_data = Some((upper, lower));
    }
    let (upper, lower) = row_data.expect("No rows returned");
    assert_eq!(upper, "HELLO");
    assert_eq!(lower, "world");

    // Test LENGTH
    let length: i64 = db
        .query_one("SELECT LENGTH(text1) FROM string_test WHERE id = 2", ())
        .expect("Failed to execute LENGTH");
    assert_eq!(length, 7, "Expected length 7");

    // Test CONCAT
    let concat: String = db
        .query_one(
            "SELECT CONCAT(text1, ' ', text2) FROM string_test WHERE id = 1",
            (),
        )
        .expect("Failed to execute CONCAT");
    assert_eq!(concat, "Hello World");

    // Test SUBSTRING
    let substr: String = db
        .query_one(
            "SELECT SUBSTRING(text1, 1, 3) FROM string_test WHERE id = 2",
            (),
        )
        .expect("Failed to execute SUBSTRING");
    assert_eq!(substr, "STO");
}

/// Test date/time functions
#[test]
fn test_datetime_functions() {
    let db = Database::open("memory://int_dt").expect("Failed to create database");

    db.execute(
        "CREATE TABLE datetime_test (
            id INT PRIMARY KEY,
            event_time TIMESTAMP
        )",
        (),
    )
    .expect("Failed to create table");

    db.execute(
        "INSERT INTO datetime_test VALUES
            (1, TIMESTAMP '2024-01-15 14:30:45'),
            (2, TIMESTAMP '2024-02-20 09:15:30'),
            (3, TIMESTAMP '2024-03-25 18:45:00')",
        (),
    )
    .expect("Failed to insert data");

    // Test NOW() function
    let now: String = db
        .query_one("SELECT NOW()", ())
        .expect("Failed to execute NOW()");
    assert!(!now.is_empty(), "NOW() returned empty string");

    // Test DATE_TRUNC
    let truncated: String = db
        .query_one(
            "SELECT DATE_TRUNC('day', event_time) FROM datetime_test WHERE id = 1",
            (),
        )
        .expect("Failed to execute DATE_TRUNC");
    assert!(!truncated.is_empty(), "DATE_TRUNC returned empty string");
}

/// Test CAST operations
#[test]
fn test_cast_operations() {
    let db = Database::open("memory://int_cast").expect("Failed to create database");

    db.execute(
        "CREATE TABLE cast_test (
            id INT PRIMARY KEY,
            int_val INT,
            float_val FLOAT,
            text_val TEXT,
            bool_val BOOLEAN
        )",
        (),
    )
    .expect("Failed to create table");

    db.execute(
        "INSERT INTO cast_test VALUES
            (1, 42, 3.14, '123', true),
            (2, 100, 2.718, '456.78', false),
            (3, 0, 0.0, 'abc', true)",
        (),
    )
    .expect("Failed to insert data");

    // Test INT to FLOAT cast
    let float_result: f64 = db
        .query_one(
            "SELECT CAST(int_val AS FLOAT) FROM cast_test WHERE id = 1",
            (),
        )
        .expect("Failed to execute INT to FLOAT cast");
    assert!((float_result - 42.0).abs() < 0.01, "Expected 42.0");

    // Test TEXT to INT cast
    let int_result: i64 = db
        .query_one(
            "SELECT CAST(text_val AS INTEGER) FROM cast_test WHERE id = 1",
            (),
        )
        .expect("Failed to execute TEXT to INT cast");
    assert_eq!(int_result, 123, "Expected 123");

    // Test FLOAT to TEXT cast
    let text_result: String = db
        .query_one(
            "SELECT CAST(float_val AS TEXT) FROM cast_test WHERE id = 1",
            (),
        )
        .expect("Failed to execute FLOAT to TEXT cast");
    assert!(
        text_result == "3.14" || text_result.starts_with("3.14"),
        "Expected '3.14', got '{}'",
        text_result
    );
}

/// Test NULL handling
#[test]
fn test_null_handling() {
    let db = Database::open("memory://int_null").expect("Failed to create database");

    db.execute(
        "CREATE TABLE null_test (
            id INT PRIMARY KEY,
            value INT,
            text_val TEXT,
            float_val FLOAT
        )",
        (),
    )
    .expect("Failed to create table");

    db.execute(
        "INSERT INTO null_test VALUES
            (1, 100, 'test', 3.5),
            (2, NULL, NULL, NULL),
            (3, 200, 'value', NULL)",
        (),
    )
    .expect("Failed to insert data");

    // Test IS NULL
    let count: i64 = db
        .query_one("SELECT COUNT(*) FROM null_test WHERE value IS NULL", ())
        .expect("Failed to execute IS NULL");
    assert_eq!(count, 1, "Expected 1 NULL value");

    // Test IS NOT NULL
    let count: i64 = db
        .query_one(
            "SELECT COUNT(*) FROM null_test WHERE text_val IS NOT NULL",
            (),
        )
        .expect("Failed to execute IS NOT NULL");
    assert_eq!(count, 2, "Expected 2 non-NULL text values");

    // Test COALESCE
    let result: i64 = db
        .query_one(
            "SELECT COALESCE(value, 999) FROM null_test WHERE id = 2",
            (),
        )
        .expect("Failed to execute COALESCE");
    assert_eq!(result, 999, "Expected 999 from COALESCE");

    // Test aggregates with NULL - AVG should only consider non-NULL values
    let avg: f64 = db
        .query_one("SELECT AVG(float_val) FROM null_test", ())
        .expect("Failed to execute AVG with NULL");
    assert!((avg - 3.5).abs() < 0.01, "Expected AVG=3.5, got {}", avg);
}

/// Test transaction support
#[test]
fn test_transaction_support() {
    let db = Database::open("memory://int_tx").expect("Failed to create database");

    db.execute(
        "CREATE TABLE tx_test (
            id INT PRIMARY KEY,
            value INT
        )",
        (),
    )
    .expect("Failed to create table");

    // Test successful transaction
    db.execute("BEGIN", ())
        .expect("Failed to begin transaction");
    db.execute("INSERT INTO tx_test VALUES (1, 100)", ())
        .expect("Failed to insert");
    db.execute("INSERT INTO tx_test VALUES (2, 200)", ())
        .expect("Failed to insert");
    db.execute("COMMIT", ()).expect("Failed to commit");

    let count: i64 = db
        .query_one("SELECT COUNT(*) FROM tx_test", ())
        .expect("Failed to count");
    assert_eq!(count, 2, "Expected 2 rows after commit");

    // Test rollback
    db.execute("BEGIN", ())
        .expect("Failed to begin transaction");
    db.execute("INSERT INTO tx_test VALUES (3, 300)", ())
        .expect("Failed to insert");
    db.execute("ROLLBACK", ()).expect("Failed to rollback");

    let count: i64 = db
        .query_one("SELECT COUNT(*) FROM tx_test", ())
        .expect("Failed to count");
    assert_eq!(count, 2, "Expected 2 rows after rollback");
}

/// Test simple JOINs
#[test]
fn test_simple_joins() {
    let db = Database::open("memory://int_join").expect("Failed to create database");

    db.execute(
        "CREATE TABLE departments (
            id INT PRIMARY KEY,
            name TEXT
        )",
        (),
    )
    .expect("Failed to create departments table");

    db.execute(
        "CREATE TABLE employees (
            id INT PRIMARY KEY,
            name TEXT,
            dept_id INT
        )",
        (),
    )
    .expect("Failed to create employees table");

    db.execute(
        "INSERT INTO departments VALUES
            (1, 'Engineering'),
            (2, 'Sales'),
            (3, 'Marketing')",
        (),
    )
    .expect("Failed to insert departments");

    db.execute(
        "INSERT INTO employees VALUES
            (1, 'Alice', 1),
            (2, 'Bob', 1),
            (3, 'Charlie', 2),
            (4, 'David', NULL)",
        (),
    )
    .expect("Failed to insert employees");

    // Test INNER JOIN
    let result = db
        .query(
            "SELECT e.name, d.name
             FROM employees e
             INNER JOIN departments d ON e.dept_id = d.id
             ORDER BY e.name",
            (),
        )
        .expect("Failed to execute INNER JOIN");

    let mut inner_join_count = 0;
    for row in result {
        let _row = row.expect("Failed to get row");
        inner_join_count += 1;
    }
    assert_eq!(
        inner_join_count, 3,
        "Expected 3 INNER JOIN results, got {}",
        inner_join_count
    );

    // Test LEFT JOIN
    let result = db
        .query(
            "SELECT e.name, d.name
             FROM employees e
             LEFT JOIN departments d ON e.dept_id = d.id
             ORDER BY e.name",
            (),
        )
        .expect("Failed to execute LEFT JOIN");

    let mut left_join_count = 0;
    for row in result {
        let _row = row.expect("Failed to get row");
        left_join_count += 1;
    }
    assert_eq!(
        left_join_count, 4,
        "Expected 4 LEFT JOIN results, got {}",
        left_join_count
    );
}

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

    db.execute(
        "CREATE TABLE distinct_test (
            id INT PRIMARY KEY,
            category TEXT,
            value INT
        )",
        (),
    )
    .expect("Failed to create table");

    db.execute(
        "INSERT INTO distinct_test VALUES
            (1, 'A', 10),
            (2, 'B', 20),
            (3, 'A', 30),
            (4, 'C', 40),
            (5, 'B', 50)",
        (),
    )
    .expect("Failed to insert data");

    let count: i64 = db
        .query_one("SELECT COUNT(DISTINCT category) FROM distinct_test", ())
        .expect("Failed to execute DISTINCT");
    assert_eq!(count, 3, "Expected 3 distinct categories");
}

/// Test LIMIT and OFFSET
#[test]
fn test_limit_offset() {
    let db = Database::open("memory://int_limit").expect("Failed to create database");

    db.execute(
        "CREATE TABLE limit_test (
            id INT PRIMARY KEY,
            value INT
        )",
        (),
    )
    .expect("Failed to create table");

    db.execute(
        "INSERT INTO limit_test VALUES
            (1, 10), (2, 20), (3, 30), (4, 40), (5, 50)",
        (),
    )
    .expect("Failed to insert data");

    // Test LIMIT
    let result = db
        .query("SELECT id FROM limit_test ORDER BY id LIMIT 3", ())
        .expect("Failed to execute LIMIT");
    let mut count = 0;
    for row in result {
        let _row = row.expect("Failed to get row");
        count += 1;
    }
    assert_eq!(count, 3, "Expected 3 rows with LIMIT 3");

    // Test LIMIT with OFFSET
    let result = db
        .query("SELECT id FROM limit_test ORDER BY id LIMIT 2 OFFSET 2", ())
        .expect("Failed to execute LIMIT OFFSET");
    let mut ids: Vec<i64> = Vec::new();
    for row in result {
        let row = row.expect("Failed to get row");
        let id: i64 = row.get(0).unwrap();
        ids.push(id);
    }
    assert_eq!(ids, vec![3, 4], "Expected ids 3, 4 with LIMIT 2 OFFSET 2");
}

/// Test IN clause
#[test]
fn test_in_clause() {
    let db = Database::open("memory://int_in").expect("Failed to create database");

    db.execute(
        "CREATE TABLE in_test (
            id INT PRIMARY KEY,
            name TEXT
        )",
        (),
    )
    .expect("Failed to create table");

    db.execute(
        "INSERT INTO in_test VALUES
            (1, 'Alice'), (2, 'Bob'), (3, 'Charlie'), (4, 'David')",
        (),
    )
    .expect("Failed to insert data");

    let count: i64 = db
        .query_one(
            "SELECT COUNT(*) FROM in_test WHERE name IN ('Alice', 'Charlie')",
            (),
        )
        .expect("Failed to execute IN");
    assert_eq!(count, 2, "Expected 2 rows with IN clause");

    let count: i64 = db
        .query_one("SELECT COUNT(*) FROM in_test WHERE id NOT IN (1, 2)", ())
        .expect("Failed to execute NOT IN");
    assert_eq!(count, 2, "Expected 2 rows with NOT IN clause");
}

/// Test LIKE clause
#[test]
fn test_like_clause() {
    let db = Database::open("memory://int_like").expect("Failed to create database");

    db.execute(
        "CREATE TABLE like_test (
            id INT PRIMARY KEY,
            name TEXT
        )",
        (),
    )
    .expect("Failed to create table");

    db.execute(
        "INSERT INTO like_test VALUES
            (1, 'Apple'), (2, 'Banana'), (3, 'Apricot'), (4, 'Avocado')",
        (),
    )
    .expect("Failed to insert data");

    // Test starts with
    let count: i64 = db
        .query_one("SELECT COUNT(*) FROM like_test WHERE name LIKE 'A%'", ())
        .expect("Failed to execute LIKE");
    assert_eq!(count, 3, "Expected 3 rows starting with A");

    // Test ends with
    let count: i64 = db
        .query_one("SELECT COUNT(*) FROM like_test WHERE name LIKE '%a'", ())
        .expect("Failed to execute LIKE");
    assert_eq!(count, 1, "Expected 1 row ending with a");

    // Test contains
    let count: i64 = db
        .query_one("SELECT COUNT(*) FROM like_test WHERE name LIKE '%an%'", ())
        .expect("Failed to execute LIKE");
    assert_eq!(count, 1, "Expected 1 row containing 'an'");
}

/// Test BETWEEN clause
#[test]
fn test_between_clause() {
    let db = Database::open("memory://int_between").expect("Failed to create database");

    db.execute(
        "CREATE TABLE between_test (
            id INT PRIMARY KEY,
            value INT
        )",
        (),
    )
    .expect("Failed to create table");

    db.execute(
        "INSERT INTO between_test VALUES
            (1, 10), (2, 20), (3, 30), (4, 40), (5, 50)",
        (),
    )
    .expect("Failed to insert data");

    let count: i64 = db
        .query_one(
            "SELECT COUNT(*) FROM between_test WHERE value BETWEEN 20 AND 40",
            (),
        )
        .expect("Failed to execute BETWEEN");
    assert_eq!(count, 3, "Expected 3 rows between 20 and 40");

    let count: i64 = db
        .query_one(
            "SELECT COUNT(*) FROM between_test WHERE value NOT BETWEEN 20 AND 40",
            (),
        )
        .expect("Failed to execute NOT BETWEEN");
    assert_eq!(count, 2, "Expected 2 rows not between 20 and 40");
}

/// Test ORDER BY with multiple columns
#[test]
fn test_order_by_multiple() {
    let db = Database::open("memory://int_order").expect("Failed to create database");

    db.execute(
        "CREATE TABLE order_test (
            id INT PRIMARY KEY,
            category TEXT,
            value INT
        )",
        (),
    )
    .expect("Failed to create table");

    db.execute(
        "INSERT INTO order_test VALUES
            (1, 'A', 30),
            (2, 'B', 20),
            (3, 'A', 10),
            (4, 'B', 40),
            (5, 'A', 20)",
        (),
    )
    .expect("Failed to insert data");

    let result = db
        .query(
            "SELECT id, category, value FROM order_test ORDER BY category, value DESC",
            (),
        )
        .expect("Failed to execute ORDER BY");

    let mut ids: Vec<i64> = Vec::new();
    for row in result {
        let row = row.expect("Failed to get row");
        let id: i64 = row.get(0).unwrap();
        ids.push(id);
    }
    // A with values 30, 20, 10 should be 1, 5, 3 (descending)
    // B with values 40, 20 should be 4, 2 (descending)
    assert_eq!(ids, vec![1, 5, 3, 4, 2], "Unexpected order");
}

/// Test DEFAULT values in CREATE TABLE
#[test]
fn test_default_values() {
    let db = Database::open("memory://int_default").expect("Failed to create database");

    db.execute(
        "CREATE TABLE products (
            id INTEGER PRIMARY KEY,
            name TEXT NOT NULL,
            description TEXT DEFAULT 'No description',
            price FLOAT DEFAULT 0.0,
            quantity INTEGER DEFAULT 1,
            active BOOLEAN DEFAULT true
        )",
        (),
    )
    .expect("Failed to create table with defaults");

    // Insert with only required columns
    db.execute("INSERT INTO products (id, name) VALUES (1, 'Widget')", ())
        .expect("Failed to insert with defaults");

    // Insert with some defaults overridden
    db.execute(
        "INSERT INTO products (id, name, price, quantity) VALUES (2, 'Gadget', 29.99, 5)",
        (),
    )
    .expect("Failed to insert with partial defaults");

    // Insert with all columns specified
    db.execute(
        "INSERT INTO products VALUES (3, 'Thing', 'A useful thing', 19.99, 10, false)",
        (),
    )
    .expect("Failed to insert with all values");

    // Verify defaults were applied correctly
    let result = db
        .query("SELECT * FROM products ORDER BY id", ())
        .expect("Failed to query");

    let mut rows_data: Vec<(i64, String, String, f64, i64, bool)> = Vec::new();
    for row in result {
        let row = row.expect("Failed to get row");
        rows_data.push((
            row.get(0).unwrap(),
            row.get(1).unwrap(),
            row.get(2).unwrap(),
            row.get(3).unwrap(),
            row.get(4).unwrap(),
            row.get(5).unwrap(),
        ));
    }

    assert_eq!(rows_data.len(), 3);

    // Row 1: all defaults except id and name
    assert_eq!(rows_data[0].0, 1);
    assert_eq!(rows_data[0].1, "Widget");
    assert_eq!(rows_data[0].2, "No description");
    assert_eq!(rows_data[0].3, 0.0);
    assert_eq!(rows_data[0].4, 1);
    assert!(rows_data[0].5);

    // Row 2: some defaults
    assert_eq!(rows_data[1].0, 2);
    assert_eq!(rows_data[1].1, "Gadget");
    assert_eq!(rows_data[1].2, "No description");
    assert_eq!(rows_data[1].3, 29.99);
    assert_eq!(rows_data[1].4, 5);
    assert!(rows_data[1].5);

    // Row 3: no defaults
    assert_eq!(rows_data[2].0, 3);
    assert_eq!(rows_data[2].1, "Thing");
    assert_eq!(rows_data[2].2, "A useful thing");
    assert_eq!(rows_data[2].3, 19.99);
    assert_eq!(rows_data[2].4, 10);
    assert!(!rows_data[2].5);
}

/// Test CHECK constraints in CREATE TABLE
#[test]
fn test_check_constraints() {
    let db = Database::open("memory://int_check").expect("Failed to create database");

    db.execute(
        "CREATE TABLE employees (
            id INTEGER PRIMARY KEY,
            name TEXT NOT NULL,
            age INTEGER CHECK (age >= 18 AND age <= 100),
            salary FLOAT CHECK (salary > 0),
            department TEXT
        )",
        (),
    )
    .expect("Failed to create table with CHECK constraints");

    // Valid insert
    db.execute(
        "INSERT INTO employees VALUES (1, 'Alice', 30, 75000.0, 'Engineering')",
        (),
    )
    .expect("Valid insert should succeed");

    // Invalid age (too young)
    let result = db.execute(
        "INSERT INTO employees VALUES (2, 'Bob', 15, 50000.0, 'Sales')",
        (),
    );
    assert!(result.is_err(), "Age < 18 should fail CHECK constraint");

    // Invalid age (too old)
    let result = db.execute(
        "INSERT INTO employees VALUES (3, 'Charlie', 150, 60000.0, 'HR')",
        (),
    );
    assert!(result.is_err(), "Age > 100 should fail CHECK constraint");

    // Invalid salary (zero)
    let result = db.execute(
        "INSERT INTO employees VALUES (4, 'David', 25, 0, 'Marketing')",
        (),
    );
    assert!(result.is_err(), "Salary = 0 should fail CHECK constraint");

    // Invalid salary (negative)
    let result = db.execute(
        "INSERT INTO employees VALUES (5, 'Eve', 28, -1000.0, 'Finance')",
        (),
    );
    assert!(
        result.is_err(),
        "Negative salary should fail CHECK constraint"
    );

    // Verify only valid row exists
    let result = db
        .query("SELECT COUNT(*) FROM employees", ())
        .expect("Failed to count");
    let mut count = 0i64;
    for row in result {
        let row = row.expect("Failed to get row");
        count = row.get(0).unwrap();
    }
    assert_eq!(count, 1, "Only one valid row should exist");
}

/// Test UNIQUE constraints in CREATE TABLE
#[test]
fn test_unique_constraints() {
    let db = Database::open("memory://int_unique").expect("Failed to create database");

    db.execute(
        "CREATE TABLE users (
            id INTEGER PRIMARY KEY,
            username TEXT UNIQUE,
            email TEXT UNIQUE,
            name TEXT
        )",
        (),
    )
    .expect("Failed to create table with UNIQUE constraints");

    // First user
    db.execute(
        "INSERT INTO users VALUES (1, 'alice', 'alice@example.com', 'Alice Smith')",
        (),
    )
    .expect("First insert should succeed");

    // Second user with different unique values
    db.execute(
        "INSERT INTO users VALUES (2, 'bob', 'bob@example.com', 'Bob Jones')",
        (),
    )
    .expect("Second insert with different values should succeed");

    // Duplicate username
    let result = db.execute(
        "INSERT INTO users VALUES (3, 'alice', 'charlie@example.com', 'Charlie Brown')",
        (),
    );
    assert!(
        result.is_err(),
        "Duplicate username should fail UNIQUE constraint"
    );

    // Duplicate email
    let result = db.execute(
        "INSERT INTO users VALUES (4, 'david', 'alice@example.com', 'David Wilson')",
        (),
    );
    assert!(
        result.is_err(),
        "Duplicate email should fail UNIQUE constraint"
    );

    // Verify only valid rows exist
    let result = db
        .query("SELECT COUNT(*) FROM users", ())
        .expect("Failed to count");
    let mut count = 0i64;
    for row in result {
        let row = row.expect("Failed to get row");
        count = row.get(0).unwrap();
    }
    assert_eq!(count, 2, "Only two valid rows should exist");
}

/// Test CREATE TABLE AS SELECT
#[test]
fn test_create_table_as_select() {
    let db = Database::open("memory://int_ctas").expect("Failed to create database");

    // Create source table
    db.execute(
        "CREATE TABLE source (
            id INTEGER PRIMARY KEY,
            category TEXT,
            value FLOAT
        )",
        (),
    )
    .expect("Failed to create source table");

    db.execute(
        "INSERT INTO source VALUES
            (1, 'A', 100.0),
            (2, 'B', 200.0),
            (3, 'A', 150.0),
            (4, 'C', 300.0),
            (5, 'B', 250.0)",
        (),
    )
    .expect("Failed to insert data");

    // Create table from SELECT *
    db.execute("CREATE TABLE copy AS SELECT * FROM source", ())
        .expect("Failed to create table from SELECT *");

    let result = db
        .query("SELECT COUNT(*) FROM copy", ())
        .expect("Failed to count copy");
    let mut count = 0i64;
    for row in result {
        count = row.expect("Failed to get row").get(0).unwrap();
    }
    assert_eq!(count, 5, "Copy should have 5 rows");

    // Create table with filter
    db.execute(
        "CREATE TABLE filtered AS SELECT id, category, value FROM source WHERE value > 150.0",
        (),
    )
    .expect("Failed to create filtered table");

    let result = db
        .query("SELECT COUNT(*) FROM filtered", ())
        .expect("Failed to count filtered");
    for row in result {
        count = row.expect("Failed to get row").get(0).unwrap();
    }
    assert_eq!(count, 3, "Filtered should have 3 rows (values > 150)");

    // Create table with aggregation
    db.execute(
        "CREATE TABLE summary AS SELECT category, COUNT(*) as cnt, SUM(value) as total FROM source GROUP BY category",
        (),
    )
    .expect("Failed to create summary table");

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

/// Test JOIN USING clause
#[test]
fn test_join_using() {
    let db = Database::open("memory://int_using").expect("Failed to create database");

    db.execute(
        "CREATE TABLE departments (
            id INTEGER PRIMARY KEY,
            name TEXT
        )",
        (),
    )
    .expect("Failed to create departments table");

    db.execute(
        "CREATE TABLE employees_join (
            id INTEGER PRIMARY KEY,
            name TEXT,
            department_id INTEGER
        )",
        (),
    )
    .expect("Failed to create employees table");

    db.execute(
        "INSERT INTO departments VALUES (1, 'Engineering'), (2, 'Sales'), (3, 'HR')",
        (),
    )
    .expect("Failed to insert departments");

    db.execute(
        "INSERT INTO employees_join VALUES (1, 'Alice', 1), (2, 'Bob', 2), (3, 'Charlie', 1), (4, 'David', 4)",
        (),
    )
    .expect("Failed to insert employees");

    // Create a view-like table to test USING with same column name
    db.execute(
        "CREATE TABLE dept_emp (
            dept_id INTEGER PRIMARY KEY,
            dept_name TEXT
        )",
        (),
    )
    .expect("Failed to create dept_emp table");

    db.execute(
        "CREATE TABLE emp_dept (
            emp_id INTEGER PRIMARY KEY,
            emp_name TEXT,
            dept_id INTEGER
        )",
        (),
    )
    .expect("Failed to create emp_dept table");

    db.execute("INSERT INTO dept_emp VALUES (1, 'Eng'), (2, 'Sales')", ())
        .expect("Failed to insert dept_emp");

    db.execute(
        "INSERT INTO emp_dept VALUES (1, 'Alice', 1), (2, 'Bob', 2), (3, 'Charlie', 1)",
        (),
    )
    .expect("Failed to insert emp_dept");

    // Test INNER JOIN USING
    let result = db
        .query(
            "SELECT emp_name, dept_name FROM emp_dept JOIN dept_emp USING (dept_id) ORDER BY emp_name",
            (),
        )
        .expect("Failed to execute JOIN USING");

    let mut names: Vec<(String, String)> = Vec::new();
    for row in result {
        let row = row.expect("Failed to get row");
        names.push((row.get(0).unwrap(), row.get(1).unwrap()));
    }
    assert_eq!(names.len(), 3);
    assert_eq!(names[0], ("Alice".to_string(), "Eng".to_string()));
    assert_eq!(names[1], ("Bob".to_string(), "Sales".to_string()));
    assert_eq!(names[2], ("Charlie".to_string(), "Eng".to_string()));

    // Test LEFT JOIN USING
    db.execute("INSERT INTO emp_dept VALUES (4, 'David', 99)", ())
        .expect("Failed to insert David");

    let result = db
        .query(
            "SELECT emp_name FROM emp_dept LEFT JOIN dept_emp USING (dept_id) ORDER BY emp_name",
            (),
        )
        .expect("Failed to execute LEFT JOIN USING");

    let mut emp_names: Vec<String> = Vec::new();
    for row in result {
        let row = row.expect("Failed to get row");
        emp_names.push(row.get(0).unwrap());
    }
    assert_eq!(emp_names.len(), 4, "LEFT JOIN should include David");
    assert!(emp_names.contains(&"David".to_string()));
}