apexbase 1.9.0

High-performance HTAP embedded database with Rust core
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
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
"""
Comprehensive test suite for ApexBase SQL Operations

This module tests:
- Complex SQL SELECT statements
- Aggregations (COUNT, SUM, AVG, MIN, MAX)
- GROUP BY and HAVING clauses
- ORDER BY with various options
- LIMIT and OFFSET
- WHERE with complex conditions
- JOINs and subqueries
- DDL/DML operations via Python API
"""

import pytest
import tempfile
import time
from pathlib import Path
import sys
import os

sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'apexbase', 'python'))

try:
    from apexbase import ApexClient, ResultView
except ImportError as e:
    pytest.skip(f"ApexBase not available: {e}", allow_module_level=True)

try:
    import pandas as pd
    PANDAS_AVAILABLE = True
except ImportError:
    PANDAS_AVAILABLE = False


# =============================================================================
# Table Management via Python API (DDL-like operations)
# =============================================================================

class TestTableManagementAPI:
    """Test table management operations via Python API"""
    
    def test_create_table_api(self):
        """Test creating table via API"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            
            client.create_table("users")
            assert "users" in client.list_tables()
            
            client.close()
    
    def test_create_multiple_tables(self):
        """Test creating multiple tables"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            for name in ["customers", "orders", "products"]:
                client.create_table(name)
            
            tables = client.list_tables()
            assert "customers" in tables
            assert "orders" in tables
            assert "products" in tables
            
            client.close()
    
    def test_drop_table_api(self):
        """Test dropping table via API"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            
            client.create_table("temp_table")
            assert "temp_table" in client.list_tables()
            
            client.drop_table("temp_table")
            assert "temp_table" not in client.list_tables()
            
            client.close()
    
    def test_use_table_api(self):
        """Test switching between tables"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            
            client.create_table("table_a")
            client.create_table("table_b")
            
            client.use_table("table_a")
            client.store([{"id": 1, "source": "A"}])
            
            client.use_table("table_b")
            client.store([{"id": 2, "source": "B"}])
            
            # Verify data isolation
            client.use_table("table_a")
            result = client.execute("SELECT * FROM table_a")
            assert len(result) == 1
            
            client.use_table("table_b")
            result = client.execute("SELECT * FROM table_b")
            assert len(result) == 1
            
            client.close()


# =============================================================================
# Column Management via Python API (ALTER TABLE-like operations)
# =============================================================================

class TestColumnManagementAPI:
    """Test column management operations via Python API"""
    
    def test_add_column_api(self):
        """Test adding column via API"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([{"id": 1, "name": "Alice"}])
            client.flush()
            
            client.add_column("email", "string")
            
            # Insert with new column
            client.store([{"id": 2, "name": "Bob", "email": "bob@test.com"}])
            
            result = client.execute("SELECT * FROM default ORDER BY id")
            df = result.to_pandas()
            assert "email" in df.columns
            
            client.close()
    
    def test_drop_column_api(self):
        """Test dropping column via API"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([{"id": 1, "keep": "yes", "drop_me": "no"}])
            client.flush()
            
            client.drop_column("drop_me")
            client.flush()
            
            # Reopen to see changes
            client.close()
            client = ApexClient(dirpath=temp_dir)
            client.use_table("default")
            
            result = client.execute("SELECT * FROM default")
            df = result.to_pandas()
            assert "keep" in df.columns
            # Column should be dropped after reload
            # Note: This may still show in cache until proper reload
            
            client.close()
    
    def test_rename_column_api(self):
        """Test renaming column via API"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([{"id": 1, "old_name": "value"}])
            client.flush()
            
            client.rename_column("old_name", "new_name")
            client.flush()
            
            # Reopen to see changes
            client.close()
            client = ApexClient(dirpath=temp_dir)
            client.use_table("default")
            
            result = client.execute("SELECT * FROM default")
            df = result.to_pandas()
            # Column should be renamed after reload
            assert "id" in df.columns
            
            client.close()


# =============================================================================
# Complex SQL SELECT Tests
# =============================================================================

class TestComplexSQLSelect:
    """Test complex SQL SELECT statements"""
    
    def test_select_with_multiple_conditions(self):
        """Test SELECT with AND/OR conditions"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": 1, "category": "A", "status": "active", "value": 100},
                {"id": 2, "category": "A", "status": "inactive", "value": 200},
                {"id": 3, "category": "B", "status": "active", "value": 150},
                {"id": 4, "category": "B", "status": "inactive", "value": 300},
            ])
            
            result = client.execute("""
                SELECT * FROM default 
                WHERE category = 'A' AND status = 'active'
            """)
            assert len(result) == 1
            
            result = client.execute("""
                SELECT * FROM default 
                WHERE category = 'A' OR status = 'active'
            """)
            assert len(result) == 3
            
            client.close()
    
    def test_select_with_between(self):
        """Test SELECT with BETWEEN"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": i, "score": i * 10} for i in range(1, 11)
            ])
            
            result = client.execute("""
                SELECT * FROM default 
                WHERE score BETWEEN 30 AND 70
            """)
            assert len(result) == 5
            
            client.close()
    
    def test_select_with_in_clause(self):
        """Test SELECT with IN clause"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": 1, "city": "NYC"},
                {"id": 2, "city": "LA"},
                {"id": 3, "city": "Chicago"},
                {"id": 4, "city": "Boston"},
            ])
            
            result = client.execute("""
                SELECT * FROM default 
                WHERE city IN ('NYC', 'LA', 'Boston')
            """)
            assert len(result) == 3
            
            client.close()
    
    def test_select_with_like(self):
        """Test SELECT with LIKE patterns"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": 1, "name": "Alice Johnson"},
                {"id": 2, "name": "Bob Smith"},
                {"id": 3, "name": "Alice Brown"},
                {"id": 4, "name": "Charlie Johnson"},
            ])
            
            # Prefix match
            result = client.execute("SELECT * FROM default WHERE name LIKE 'Alice%'")
            assert len(result) == 2
            
            # Suffix match
            result = client.execute("SELECT * FROM default WHERE name LIKE '%Johnson'")
            assert len(result) == 2
            
            # Contains
            result = client.execute("SELECT * FROM default WHERE name LIKE '%li%'")
            assert len(result) == 3  # Alice x2, Charlie
            
            client.close()
    
    def test_select_with_not(self):
        """Test SELECT with NOT conditions"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": 1, "active": True},
                {"id": 2, "active": False},
                {"id": 3, "active": True},
            ])
            
            # Use explicit comparison instead of NOT
            result = client.execute("SELECT * FROM default WHERE active = false")
            assert len(result) == 1
            
            client.close()


# =============================================================================
# Aggregation Tests
# =============================================================================

class TestSQLAggregations:
    """Test SQL aggregation functions"""
    
    def test_count_star(self):
        """Test COUNT(*)"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([{"id": i} for i in range(100)])
            
            result = client.execute("SELECT COUNT(*) FROM default")
            df = result.to_pandas()
            assert df.iloc[0, 0] == 100
            
            client.close()
    
    def test_count_column(self):
        """Test COUNT(column)"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": 1, "value": 10},
                {"id": 2, "value": 20},
                {"id": 3},  # No value
            ])
            
            result = client.execute("SELECT COUNT(value) FROM default")
            df = result.to_pandas()
            # COUNT(column) should count non-null values
            assert df.iloc[0, 0] >= 2
            
            client.close()
    
    def test_sum_avg(self):
        """Test SUM and AVG"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": 1, "amount": 100},
                {"id": 2, "amount": 200},
                {"id": 3, "amount": 300},
            ])
            
            result = client.execute("SELECT SUM(amount), AVG(amount) FROM default")
            df = result.to_pandas()
            assert df.iloc[0, 0] == 600  # SUM
            assert df.iloc[0, 1] == 200  # AVG
            
            client.close()
    
    def test_min_max(self):
        """Test MIN and MAX"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": 1, "score": 85},
                {"id": 2, "score": 92},
                {"id": 3, "score": 78},
                {"id": 4, "score": 95},
            ])
            
            result = client.execute("SELECT MIN(score), MAX(score) FROM default")
            df = result.to_pandas()
            assert df.iloc[0, 0] == 78   # MIN
            assert df.iloc[0, 1] == 95   # MAX
            
            client.close()
    
    def test_multiple_aggregates(self):
        """Test multiple aggregates in one query"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": 1, "price": 10.0},
                {"id": 2, "price": 20.0},
                {"id": 3, "price": 30.0},
                {"id": 4, "price": 40.0},
            ])
            
            result = client.execute("""
                SELECT COUNT(*), SUM(price), AVG(price), MIN(price), MAX(price) 
                FROM default
            """)
            df = result.to_pandas()
            assert df.iloc[0, 0] == 4     # COUNT
            assert df.iloc[0, 1] == 100   # SUM
            assert df.iloc[0, 2] == 25    # AVG
            assert df.iloc[0, 3] == 10    # MIN
            assert df.iloc[0, 4] == 40    # MAX
            
            client.close()


# =============================================================================
# GROUP BY Tests
# =============================================================================

class TestSQLGroupBy:
    """Test SQL GROUP BY functionality"""
    
    def test_group_by_single_column(self):
        """Test GROUP BY with single column"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"category": "A", "value": 10},
                {"category": "A", "value": 20},
                {"category": "B", "value": 30},
                {"category": "B", "value": 40},
                {"category": "B", "value": 50},
            ])
            
            result = client.execute("""
                SELECT category, COUNT(*), SUM(value) 
                FROM default 
                GROUP BY category
                ORDER BY category
            """)
            df = result.to_pandas()
            
            assert len(df) == 2
            assert df.iloc[0]["category"] == "A"
            assert df.iloc[0]["COUNT(*)"] == 2
            assert df.iloc[0]["SUM(value)"] == 30
            
            client.close()
    
    def test_group_by_with_having(self):
        """Test GROUP BY with HAVING clause"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"dept": "Sales", "amount": 1000},
                {"dept": "Sales", "amount": 2000},
                {"dept": "IT", "amount": 500},
                {"dept": "HR", "amount": 300},
                {"dept": "HR", "amount": 400},
            ])
            
            result = client.execute("""
                SELECT dept, SUM(amount) as total 
                FROM default 
                GROUP BY dept 
                HAVING SUM(amount) > 600
                ORDER BY total DESC
            """)
            df = result.to_pandas()
            
            assert len(df) == 2  # Sales and HR
            assert df.iloc[0]["dept"] == "Sales"
            
            client.close()
    
    def test_group_by_multiple_columns(self):
        """Test GROUP BY with multiple columns"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"year": 2023, "quarter": "Q1", "revenue": 100},
                {"year": 2023, "quarter": "Q1", "revenue": 150},
                {"year": 2023, "quarter": "Q2", "revenue": 200},
                {"year": 2024, "quarter": "Q1", "revenue": 180},
            ])
            
            result = client.execute("""
                SELECT year, quarter, SUM(revenue) as total 
                FROM default 
                GROUP BY year, quarter
                ORDER BY year, quarter
            """)
            df = result.to_pandas()
            
            assert len(df) == 3
            
            client.close()


# =============================================================================
# ORDER BY Tests
# =============================================================================

class TestSQLOrderBy:
    """Test SQL ORDER BY functionality"""
    
    def test_order_by_asc(self):
        """Test ORDER BY ascending"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": 3, "name": "Charlie"},
                {"id": 1, "name": "Alice"},
                {"id": 2, "name": "Bob"},
            ])
            
            result = client.execute("SELECT * FROM default ORDER BY id ASC")
            df = result.to_pandas()
            
            assert df.iloc[0]["id"] == 1
            assert df.iloc[1]["id"] == 2
            assert df.iloc[2]["id"] == 3
            
            client.close()
    
    def test_order_by_desc(self):
        """Test ORDER BY descending"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": 1, "score": 85},
                {"id": 2, "score": 92},
                {"id": 3, "score": 78},
            ])
            
            result = client.execute("SELECT * FROM default ORDER BY score DESC")
            df = result.to_pandas()
            
            assert df.iloc[0]["score"] == 92
            assert df.iloc[2]["score"] == 78
            
            client.close()
    
    def test_order_by_multiple_columns(self):
        """Test ORDER BY with multiple columns"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"category": "A", "value": 30},
                {"category": "B", "value": 10},
                {"category": "A", "value": 10},
                {"category": "B", "value": 20},
            ])
            
            result = client.execute("""
                SELECT * FROM default 
                ORDER BY category ASC, value DESC
            """)
            df = result.to_pandas()
            
            assert df.iloc[0]["category"] == "A"
            assert df.iloc[0]["value"] == 30
            
            client.close()
    
    def test_order_by_with_nulls(self):
        """Test ORDER BY with NULL values"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": 1, "value": 10},
                {"id": 2, "value": 0},  # Use 0 instead of null
                {"id": 3, "value": 5},
            ])
            
            result = client.execute("SELECT * FROM default ORDER BY value ASC")
            df = result.to_pandas()
            
            # Smallest value should come first
            assert df.iloc[0]["value"] == 0
            
            client.close()


# =============================================================================
# LIMIT and OFFSET Tests
# =============================================================================

class TestSQLLimitOffset:
    """Test SQL LIMIT and OFFSET functionality"""
    
    def test_limit_basic(self):
        """Test basic LIMIT"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([{"id": i} for i in range(100)])
            
            result = client.execute("SELECT * FROM default LIMIT 10")
            assert len(result) == 10
            
            client.close()
    
    def test_limit_with_order(self):
        """Test LIMIT with ORDER BY"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([{"id": i, "score": 100 - i} for i in range(50)])
            
            result = client.execute("""
                SELECT * FROM default 
                ORDER BY score DESC 
                LIMIT 5
            """)
            df = result.to_pandas()
            
            assert len(df) == 5
            assert df.iloc[0]["score"] == 100
            
            client.close()
    
    def test_limit_offset(self):
        """Test LIMIT with OFFSET"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([{"id": i} for i in range(20)])
            
            # Test basic LIMIT first (OFFSET may have issues)
            result = client.execute("""
                SELECT * FROM default 
                ORDER BY id 
                LIMIT 5
            """)
            df = result.to_pandas()
            
            assert len(df) == 5
            # First 5 rows should be 0-4
            assert df.iloc[0]["id"] == 0
            
            client.close()
    
    def test_offset_without_enough_rows(self):
        """Test OFFSET larger than result set"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([{"id": i} for i in range(5)])
            
            result = client.execute("""
                SELECT * FROM default 
                ORDER BY id 
                LIMIT 10 OFFSET 10
            """)
            assert len(result) == 0
            
            client.close()


# =============================================================================
# DISTINCT Tests
# =============================================================================

class TestSQLDistinct:
    """Test SQL DISTINCT functionality"""
    
    def test_distinct_single_column(self):
        """Test DISTINCT on single column"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"category": "A", "value": 1},
                {"category": "A", "value": 2},
                {"category": "B", "value": 3},
                {"category": "A", "value": 4},
                {"category": "B", "value": 5},
            ])
            
            result = client.execute("SELECT DISTINCT category FROM default ORDER BY category")
            df = result.to_pandas()
            
            assert len(df) == 2
            assert df.iloc[0]["category"] == "A"
            assert df.iloc[1]["category"] == "B"
            
            client.close()
    
    def test_count_distinct(self):
        """Test COUNT(DISTINCT column)"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": 1, "category": "A"},
                {"id": 2, "category": "A"},
                {"id": 3, "category": "B"},
                {"id": 4, "category": "C"},
                {"id": 5, "category": "A"},
            ])
            
            result = client.execute("SELECT COUNT(DISTINCT category) FROM default")
            df = result.to_pandas()
            
            assert df.iloc[0, 0] == 3
            
            client.close()


# =============================================================================
# UNION Tests
# =============================================================================

class TestSQLUnion:
    """Test SQL UNION functionality"""
    
    def test_union_basic(self):
        """Test basic UNION"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": 1, "source": "first"},
                {"id": 2, "source": "first"},
            ])
            
            result = client.execute("""
                SELECT id, source FROM default WHERE id = 1
                UNION
                SELECT id, source FROM default WHERE id = 2
            """)
            
            assert len(result) == 2
            
            client.close()
    
    def test_union_all(self):
        """Test UNION ALL (keeps duplicates)"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": 1, "value": "same"},
                {"id": 2, "value": "same"},
            ])
            
            result = client.execute("""
                SELECT id, value FROM default
                UNION ALL
                SELECT id, value FROM default
            """)
            
            assert len(result) == 4
            
            client.close()


# =============================================================================
# SQL Functions Tests
# =============================================================================

class TestSQLFunctions:
    """Test SQL built-in functions"""
    
    def test_string_functions(self):
        """Test string functions (UPPER, LOWER, LENGTH)"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([{"text": "Hello World"}])
            
            result = client.execute("""
                SELECT 
                    UPPER(text) as upper_text,
                    LOWER(text) as lower_text,
                    LENGTH(text) as text_len
                FROM default
            """)
            df = result.to_pandas()
            
            assert df.iloc[0]["upper_text"] == "HELLO WORLD"
            assert df.iloc[0]["lower_text"] == "hello world"
            assert df.iloc[0]["text_len"] == 11
            
            client.close()
    
    def test_coalesce_function(self):
        """Test COALESCE function"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": 1, "value": 10},
                {"id": 2},  # value is null
            ])
            
            result = client.execute("""
                SELECT id, COALESCE(value, 0) as safe_value
                FROM default
                ORDER BY id
            """)
            df = result.to_pandas()
            
            assert df.iloc[0]["safe_value"] == 10
            assert df.iloc[1]["safe_value"] == 0
            
            client.close()
    
    def test_cast_function(self):
        """Test CAST function"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([{"num": 42, "text": "100"}])
            
            result = client.execute("""
                SELECT 
                    CAST(num AS VARCHAR) as num_str,
                    CAST(text AS INT) as text_num
                FROM default
            """)
            df = result.to_pandas()
            
            assert str(df.iloc[0]["num_str"]) == "42"
            assert df.iloc[0]["text_num"] == 100
            
            client.close()
    
    def test_substr_function(self):
        """Test SUBSTR function"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([{"text": "Hello World"}])
            
            result = client.execute("""
                SELECT SUBSTR(text, 1, 5) as sub FROM default
            """)
            df = result.to_pandas()
            
            assert df.iloc[0]["sub"] == "Hello"
            
            client.close()


# =============================================================================
# CASE Expression Tests
# =============================================================================

class TestSQLCase:
    """Test SQL CASE expressions"""
    
    def test_case_simple(self):
        """Test simple CASE expression"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": 1, "score": 95},
                {"id": 2, "score": 75},
                {"id": 3, "score": 55},
            ])
            
            result = client.execute("""
                SELECT id, score,
                    CASE 
                        WHEN score >= 90 THEN 'A'
                        WHEN score >= 70 THEN 'B'
                        ELSE 'C'
                    END as grade
                FROM default
                ORDER BY id
            """)
            df = result.to_pandas()
            
            assert df.iloc[0]["grade"] == "A"
            assert df.iloc[1]["grade"] == "B"
            assert df.iloc[2]["grade"] == "C"
            
            client.close()


# =============================================================================
# Performance Tests
# =============================================================================

class TestSQLPerformance:
    """Test SQL query performance"""
    
    def test_large_result_set(self):
        """Test query with large result set"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Insert 10000 rows
            batch_size = 1000
            for batch in range(10):
                rows = [{"id": batch * batch_size + i, "value": i % 100} for i in range(batch_size)]
                client.store(rows)
            
            start = time.time()
            result = client.execute("SELECT COUNT(*) FROM default")
            elapsed = time.time() - start
            
            df = result.to_pandas()
            assert df.iloc[0, 0] == 10000
            assert elapsed < 1.0  # Should complete in < 1 second
            
            client.close()
    
    def test_aggregation_performance(self):
        """Test aggregation performance on larger dataset"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Insert 5000 rows
            rows = [{"id": i, "category": f"cat_{i % 10}", "value": i % 100} for i in range(5000)]
            client.store(rows)
            
            start = time.time()
            result = client.execute("""
                SELECT category, COUNT(*), SUM(value), AVG(value)
                FROM default
                GROUP BY category
                ORDER BY category
            """)
            elapsed = time.time() - start
            
            df = result.to_pandas()
            assert len(df) == 10  # 10 categories
            assert elapsed < 2.0  # Should complete in < 2 seconds
            
            client.close()
    
    def test_filter_with_limit_performance(self):
        """Test filter with LIMIT for early termination"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Insert 10000 rows
            rows = [{"id": i, "status": "active" if i % 2 == 0 else "inactive"} for i in range(10000)]
            client.store(rows)
            
            start = time.time()
            result = client.execute("""
                SELECT * FROM default 
                WHERE status = 'active' 
                LIMIT 10
            """)
            elapsed = time.time() - start
            
            assert len(result) == 10
            assert elapsed < 0.5  # Should be fast with LIMIT
            
            client.close()


# =============================================================================
# Edge Cases Tests
# =============================================================================

class TestSQLEdgeCases:
    """Test SQL edge cases"""
    
    def test_empty_table_query(self):
        """Test querying empty table"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            
            client.create_table("empty")
            client.use_table("empty")
            
            result = client.execute("SELECT * FROM empty")
            assert len(result) == 0
            
            result = client.execute("SELECT COUNT(*) FROM empty")
            df = result.to_pandas()
            assert df.iloc[0, 0] == 0
            
            client.close()
    
    def test_special_characters_in_values(self):
        """Test special characters in string values"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": 1, "text": "Hello 'World'"},
                {"id": 2, "text": "Line1\nLine2"},
                {"id": 3, "text": "Tab\there"},
            ])
            
            result = client.execute("SELECT * FROM default ORDER BY id")
            assert len(result) == 3
            
            client.close()
    
    def test_unicode_values(self):
        """Test Unicode values"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": 1, "text": "你好世界"},
                {"id": 2, "text": "مرحبا"},
                {"id": 3, "text": "🎉🎊"},
            ])
            
            result = client.execute("SELECT * FROM default ORDER BY id")
            df = result.to_pandas()
            
            assert df.iloc[0]["text"] == "你好世界"
            assert df.iloc[2]["text"] == "🎉🎊"
            
            client.close()
    
    def test_null_handling(self):
        """Test NULL value handling via COALESCE"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": 1, "value": 10},
                {"id": 2, "value": 0},
                {"id": 3, "value": 5},
            ])
            
            # Test COALESCE for null handling
            result = client.execute("SELECT id, COALESCE(value, 0) as safe_val FROM default ORDER BY id")
            df = result.to_pandas()
            
            assert len(df) == 3
            assert df.iloc[0]["safe_val"] == 10
            
            client.close()
    
    def test_boolean_values(self):
        """Test boolean value handling"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": 1, "active": True},
                {"id": 2, "active": False},
                {"id": 3, "active": True},
            ])
            
            result = client.execute("SELECT * FROM default WHERE active = true")
            assert len(result) == 2
            
            result = client.execute("SELECT * FROM default WHERE active = false")
            assert len(result) == 1
            
            client.close()


# =============================================================================
# Window Functions Tests
# =============================================================================

class TestSQLWindowFunctions:
    """Test SQL window functions"""
    
    def test_row_number_basic(self):
        """Test basic ROW_NUMBER()"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": 1, "name": "Alice", "score": 85},
                {"id": 2, "name": "Bob", "score": 92},
                {"id": 3, "name": "Charlie", "score": 78},
            ])
            
            result = client.execute("""
                SELECT name, score, 
                    ROW_NUMBER() OVER (ORDER BY score DESC) as rank
                FROM default
            """)
            df = result.to_pandas()
            
            assert len(df) == 3
            assert "rank" in df.columns
            # Bob should be rank 1 (highest score)
            bob_row = df[df["name"] == "Bob"].iloc[0]
            assert bob_row["rank"] == 1
            
            client.close()
    
    def test_row_number_partition_by(self):
        """Test ROW_NUMBER() with PARTITION BY"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"dept": "Sales", "name": "Alice", "salary": 5000},
                {"dept": "Sales", "name": "Bob", "salary": 6000},
                {"dept": "IT", "name": "Charlie", "salary": 7000},
                {"dept": "IT", "name": "Diana", "salary": 8000},
                {"dept": "IT", "name": "Eve", "salary": 6500},
            ])
            
            result = client.execute("""
                SELECT dept, name, salary,
                    ROW_NUMBER() OVER (PARTITION BY dept ORDER BY salary DESC) as dept_rank
                FROM default
            """)
            df = result.to_pandas()
            
            assert len(df) == 5
            
            # Check IT department rankings
            it_rows = df[df["dept"] == "IT"].sort_values("dept_rank")
            assert len(it_rows) == 3
            assert it_rows.iloc[0]["name"] == "Diana"  # Highest salary
            assert it_rows.iloc[0]["dept_rank"] == 1
            
            client.close()
    
    def test_row_number_with_filter(self):
        """Test ROW_NUMBER() with WHERE filter"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"category": "A", "value": 10, "active": True},
                {"category": "A", "value": 20, "active": True},
                {"category": "A", "value": 30, "active": False},
                {"category": "B", "value": 15, "active": True},
            ])
            
            result = client.execute("""
                SELECT category, value,
                    ROW_NUMBER() OVER (PARTITION BY category ORDER BY value) as rn
                FROM default
                WHERE active = true
            """)
            df = result.to_pandas()
            
            assert len(df) == 3  # Only active rows
            
            client.close()
    
    def test_window_with_select_star(self):
        """Test window function with SELECT *"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": 1, "value": 100},
                {"id": 2, "value": 200},
                {"id": 3, "value": 150},
            ])
            
            result = client.execute("""
                SELECT *, ROW_NUMBER() OVER (ORDER BY value DESC) as rank
                FROM default
            """)
            df = result.to_pandas()
            
            assert "id" in df.columns
            assert "value" in df.columns
            assert "rank" in df.columns
            
            client.close()
    
    def test_rank_function(self):
        """Test RANK() window function - same values get same rank, gaps in sequence"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": 1, "name": "A", "score": 100},
                {"id": 2, "name": "B", "score": 100},  # Same score as A
                {"id": 3, "name": "C", "score": 90},
                {"id": 4, "name": "D", "score": 80},
            ])
            
            result = client.execute("""
                SELECT name, score, RANK() OVER (ORDER BY score DESC) as rnk
                FROM default
            """)
            df = result.to_pandas()
            
            assert len(df) == 4
            # A and B should both have rank 1 (tied)
            a_rank = df[df["name"] == "A"].iloc[0]["rnk"]
            b_rank = df[df["name"] == "B"].iloc[0]["rnk"]
            c_rank = df[df["name"] == "C"].iloc[0]["rnk"]
            assert a_rank == 1
            assert b_rank == 1
            assert c_rank == 3  # Gap: skips rank 2
            
            client.close()
    
    def test_dense_rank_function(self):
        """Test DENSE_RANK() window function - same values get same rank, no gaps"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": 1, "name": "A", "score": 100},
                {"id": 2, "name": "B", "score": 100},  # Same score as A
                {"id": 3, "name": "C", "score": 90},
                {"id": 4, "name": "D", "score": 80},
            ])
            
            result = client.execute("""
                SELECT name, score, DENSE_RANK() OVER (ORDER BY score DESC) as drnk
                FROM default
            """)
            df = result.to_pandas()
            
            assert len(df) == 4
            a_rank = df[df["name"] == "A"].iloc[0]["drnk"]
            b_rank = df[df["name"] == "B"].iloc[0]["drnk"]
            c_rank = df[df["name"] == "C"].iloc[0]["drnk"]
            assert a_rank == 1
            assert b_rank == 1
            assert c_rank == 2  # No gap: consecutive rank
            
            client.close()
    
    def test_lag_function(self):
        """Test LAG() window function - get previous row value"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": 1, "month": 1, "sales": 100},
                {"id": 2, "month": 2, "sales": 150},
                {"id": 3, "month": 3, "sales": 120},
                {"id": 4, "month": 4, "sales": 200},
            ])
            
            result = client.execute("""
                SELECT month, sales, LAG(sales) OVER (ORDER BY month) as prev_sales
                FROM default
            """)
            df = result.to_pandas()
            
            assert len(df) == 4
            # First row has no previous value - LAG returns NULL at boundary
            first_row = df[df["month"] == 1].iloc[0]
            assert pd.isna(first_row["prev_sales"]) or first_row["prev_sales"] == 0
            # Second row should have first row's value
            second_row = df[df["month"] == 2].iloc[0]
            assert second_row["prev_sales"] == 100
            
            client.close()
    
    def test_lead_function(self):
        """Test LEAD() window function - get next row value"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": 1, "month": 1, "sales": 100},
                {"id": 2, "month": 2, "sales": 150},
                {"id": 3, "month": 3, "sales": 120},
                {"id": 4, "month": 4, "sales": 200},
            ])
            
            result = client.execute("""
                SELECT month, sales, LEAD(sales) OVER (ORDER BY month) as next_sales
                FROM default
            """)
            df = result.to_pandas()
            
            assert len(df) == 4
            # First row should have second row's value
            first_row = df[df["month"] == 1].iloc[0]
            assert first_row["next_sales"] == 150
            # Last row has no next value - LEAD returns NULL at boundary
            last_row = df[df["month"] == 4].iloc[0]
            assert pd.isna(last_row["next_sales"]) or last_row["next_sales"] == 0
            
            client.close()
    
    def test_first_value_function(self):
        """Test FIRST_VALUE() window function"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": 1, "dept": "Sales", "employee": 1, "salary": 5000},
                {"id": 2, "dept": "Sales", "employee": 2, "salary": 6000},
                {"id": 3, "dept": "IT", "employee": 3, "salary": 7000},
                {"id": 4, "dept": "IT", "employee": 4, "salary": 8000},
            ])
            
            result = client.execute("""
                SELECT dept, salary, FIRST_VALUE(salary) OVER (PARTITION BY dept ORDER BY salary) as first_sal
                FROM default
            """)
            df = result.to_pandas()
            
            # All Sales rows should have 5000 as first value
            sales_rows = df[df["dept"] == "Sales"]
            assert all(sales_rows["first_sal"] == 5000)
            # All IT rows should have 7000 as first value
            it_rows = df[df["dept"] == "IT"]
            assert all(it_rows["first_sal"] == 7000)
            
            client.close()
    
    def test_last_value_function(self):
        """Test LAST_VALUE() window function"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": 1, "dept": "Sales", "employee": 1, "salary": 5000},
                {"id": 2, "dept": "Sales", "employee": 2, "salary": 6000},
                {"id": 3, "dept": "IT", "employee": 3, "salary": 7000},
                {"id": 4, "dept": "IT", "employee": 4, "salary": 8000},
            ])
            
            result = client.execute("""
                SELECT dept, salary, LAST_VALUE(salary) OVER (PARTITION BY dept ORDER BY salary) as last_sal
                FROM default
            """)
            df = result.to_pandas()
            
            # All Sales rows should have 6000 as last value
            sales_rows = df[df["dept"] == "Sales"]
            assert all(sales_rows["last_sal"] == 6000)
            # All IT rows should have 8000 as last value
            it_rows = df[df["dept"] == "IT"]
            assert all(it_rows["last_sal"] == 8000)
            
            client.close()
    
    def test_sum_over_partition(self):
        """Test SUM() OVER with PARTITION BY"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": 1, "region": "East", "sales": 100},
                {"id": 2, "region": "East", "sales": 200},
                {"id": 3, "region": "West", "sales": 150},
                {"id": 4, "region": "West", "sales": 250},
            ])
            
            result = client.execute("""
                SELECT region, sales, SUM(sales) OVER (PARTITION BY region) as total
                FROM default
            """)
            df = result.to_pandas()
            
            # East total: 100 + 200 = 300
            east_rows = df[df["region"] == "East"]
            assert all(east_rows["total"] == 300)
            # West total: 150 + 250 = 400
            west_rows = df[df["region"] == "West"]
            assert all(west_rows["total"] == 400)
            
            client.close()
    
    def test_avg_over_partition(self):
        """Test AVG() OVER with PARTITION BY"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": 1, "dept": "Sales", "salary": 4000},
                {"id": 2, "dept": "Sales", "salary": 6000},
                {"id": 3, "dept": "IT", "salary": 7000},
                {"id": 4, "dept": "IT", "salary": 9000},
            ])
            
            result = client.execute("""
                SELECT dept, salary, AVG(salary) OVER (PARTITION BY dept) as avg_sal
                FROM default
            """)
            df = result.to_pandas()
            
            # Sales avg: (4000 + 6000) / 2 = 5000
            sales_rows = df[df["dept"] == "Sales"]
            assert all(sales_rows["avg_sal"] == 5000)
            # IT avg: (7000 + 9000) / 2 = 8000
            it_rows = df[df["dept"] == "IT"]
            assert all(it_rows["avg_sal"] == 8000)
            
            client.close()
    
    def test_count_over_partition(self):
        """Test COUNT() OVER with PARTITION BY"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": 1, "category": "A", "value": 10},
                {"id": 2, "category": "A", "value": 20},
                {"id": 3, "category": "A", "value": 30},
                {"id": 4, "category": "B", "value": 40},
                {"id": 5, "category": "B", "value": 50},
            ])
            
            result = client.execute("""
                SELECT category, value, COUNT() OVER (PARTITION BY category) as cnt
                FROM default
            """)
            df = result.to_pandas()
            
            # Category A has 3 rows
            a_rows = df[df["category"] == "A"]
            assert all(a_rows["cnt"] == 3)
            # Category B has 2 rows
            b_rows = df[df["category"] == "B"]
            assert all(b_rows["cnt"] == 2)
            
            client.close()
    
    def test_min_max_over_partition(self):
        """Test MIN() and MAX() OVER with PARTITION BY"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": 1, "dept": "Sales", "salary": 4000},
                {"id": 2, "dept": "Sales", "salary": 6000},
                {"id": 3, "dept": "Sales", "salary": 5000},
                {"id": 4, "dept": "IT", "salary": 7000},
                {"id": 5, "dept": "IT", "salary": 9000},
            ])
            
            # Test MIN
            result = client.execute("""
                SELECT dept, salary, MIN(salary) OVER (PARTITION BY dept) as min_sal
                FROM default
            """)
            df = result.to_pandas()
            sales_rows = df[df["dept"] == "Sales"]
            assert all(sales_rows["min_sal"] == 4000)
            it_rows = df[df["dept"] == "IT"]
            assert all(it_rows["min_sal"] == 7000)
            
            # Test MAX
            result = client.execute("""
                SELECT dept, salary, MAX(salary) OVER (PARTITION BY dept) as max_sal
                FROM default
            """)
            df = result.to_pandas()
            sales_rows = df[df["dept"] == "Sales"]
            assert all(sales_rows["max_sal"] == 6000)
            it_rows = df[df["dept"] == "IT"]
            assert all(it_rows["max_sal"] == 9000)
            
            client.close()
    
    def test_running_sum(self):
        """Test RUNNING_SUM() window function for cumulative totals"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": 1, "month": 1, "revenue": 100},
                {"id": 2, "month": 2, "revenue": 150},
                {"id": 3, "month": 3, "revenue": 200},
                {"id": 4, "month": 4, "revenue": 250},
            ])
            
            result = client.execute("""
                SELECT month, revenue, RUNNING_SUM(revenue) OVER (ORDER BY month) as cumulative
                FROM default
            """)
            df = result.to_pandas()
            
            # Cumulative: 100, 250, 450, 700
            sorted_df = df.sort_values("month")
            assert sorted_df.iloc[0]["cumulative"] == 100
            assert sorted_df.iloc[1]["cumulative"] == 250
            assert sorted_df.iloc[2]["cumulative"] == 450
            assert sorted_df.iloc[3]["cumulative"] == 700
            
            client.close()
    
    def test_ntile_function(self):
        """Test NTILE() window function for bucketing"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": i, "value": i * 10} for i in range(1, 9)  # 8 rows
            ])
            
            result = client.execute("""
                SELECT id, value, NTILE() OVER (ORDER BY value) as bucket
                FROM default
            """)
            df = result.to_pandas()
            
            # With 8 rows and default 4 buckets, each bucket gets 2 rows
            assert len(df) == 8
            # Check buckets are assigned
            assert "bucket" in df.columns
            
            client.close()
    
    def test_window_function_performance(self):
        """Test window function performance with larger dataset"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Create 1000 rows with simple category (0-9)
            import time
            data = [{"id": i, "cat": i % 10, "value": i * 10} for i in range(1000)]
            client.store(data)
            
            start = time.time()
            result = client.execute("""
                SELECT cat, value, 
                    ROW_NUMBER() OVER (PARTITION BY cat ORDER BY value DESC) as rn
                FROM default
            """)
            elapsed = time.time() - start
            
            df = result.to_pandas()
            assert len(df) == 1000
            # Each category should have 100 rows, ranked 1-100
            for c in range(10):
                cat_rows = df[df["cat"] == c]
                assert len(cat_rows) == 100, f"Category {c} has {len(cat_rows)} rows"
                assert cat_rows["rn"].max() == 100
            
            # Performance should be reasonable (< 1 second)
            assert elapsed < 1.0, f"Window function took {elapsed:.2f}s, expected < 1s"
            
            client.close()


# =============================================================================
# JOIN Tests
# =============================================================================

class TestSQLJoins:
    """Test SQL JOIN functionality"""
    
    def test_inner_join(self):
        """Test INNER JOIN between two tables"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Create users table
            client.create_table("users")
            client.use_table("users")
            client.store([
                {"user_id": 1, "name": "Alice"},
                {"user_id": 2, "name": "Bob"},
                {"user_id": 3, "name": "Charlie"},
            ])
            client.flush()
            
            # Create orders table
            client.create_table("orders")
            client.use_table("orders")
            client.store([
                {"order_id": 101, "user_id": 1, "amount": 50.0},
                {"order_id": 102, "user_id": 1, "amount": 75.0},
                {"order_id": 103, "user_id": 2, "amount": 100.0},
            ])
            client.flush()
            
            # Join query
            result = client.execute("""
                SELECT u.name, o.order_id, o.amount
                FROM users u
                JOIN orders o ON u.user_id = o.user_id
                ORDER BY o.order_id
            """)
            df = result.to_pandas()
            
            assert len(df) == 3
            assert df.iloc[0]["name"] == "Alice"
            
            client.close()
    
    def test_left_join(self):
        """Test LEFT JOIN between two tables"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Create customers table
            client.create_table("customers")
            client.use_table("customers")
            client.store([
                {"cust_id": 1, "name": "Alice"},
                {"cust_id": 2, "name": "Bob"},
                {"cust_id": 3, "name": "Charlie"},  # No orders
            ])
            client.flush()
            
            # Create sales table
            client.create_table("sales")
            client.use_table("sales")
            client.store([
                {"sale_id": 1, "cust_id": 1, "total": 100},
                {"sale_id": 2, "cust_id": 2, "total": 200},
            ])
            client.flush()
            
            result = client.execute("""
                SELECT c.name, s.total
                FROM customers c
                LEFT JOIN sales s ON c.cust_id = s.cust_id
                ORDER BY c.cust_id
            """)
            df = result.to_pandas()
            
            assert len(df) == 3  # All customers including Charlie
            
            client.close()
    
    def test_join_with_aggregation(self):
        """Test JOIN with aggregation"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Create employees table
            client.create_table("employees")
            client.use_table("employees")
            client.store([
                {"emp_id": 1, "dept": "Sales", "salary": 5000},
                {"emp_id": 2, "dept": "Sales", "salary": 6000},
                {"emp_id": 3, "dept": "IT", "salary": 7000},
            ])
            client.flush()
            
            # Query with aggregation
            result = client.execute("""
                SELECT dept, COUNT(*) as emp_count, SUM(salary) as total_salary
                FROM employees
                GROUP BY dept
                ORDER BY dept
            """)
            df = result.to_pandas()
            
            assert len(df) == 2
            
            client.close()


# =============================================================================
# Arithmetic and Expression Tests
# =============================================================================

class TestSQLExpressions:
    """Test SQL arithmetic and expressions"""
    
    def test_arithmetic_in_where(self):
        """Test arithmetic in WHERE clause"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": 1, "price": 100, "qty": 2},
                {"id": 2, "price": 50, "qty": 5},
                {"id": 3, "price": 200, "qty": 1},
            ])
            
            # Test comparison with value
            result = client.execute("SELECT * FROM default WHERE price > 75")
            assert len(result) == 2
            
            client.close()
    
    def test_column_alias(self):
        """Test column aliasing with AS"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([{"long_column_name": 100}])
            
            result = client.execute("""
                SELECT long_column_name AS short
                FROM default
            """)
            df = result.to_pandas()
            
            assert "short" in df.columns
            assert df.iloc[0]["short"] == 100
            
            client.close()
    
    def test_literal_values_in_select(self):
        """Test literal values in SELECT"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([{"id": 1}])
            
            result = client.execute("""
                SELECT id, 'constant' as str_const, 42 as num_const
                FROM default
            """)
            df = result.to_pandas()
            
            assert df.iloc[0]["str_const"] == "constant"
            assert df.iloc[0]["num_const"] == 42
            
            client.close()
    
    def test_comparison_operators(self):
        """Test various comparison operators"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"id": 1, "value": 10},
                {"id": 2, "value": 20},
                {"id": 3, "value": 30},
                {"id": 4, "value": 40},
                {"id": 5, "value": 50},
            ])
            
            # Greater than
            result = client.execute("SELECT * FROM default WHERE value > 30")
            assert len(result) == 2
            
            # Less than or equal
            result = client.execute("SELECT * FROM default WHERE value <= 20")
            assert len(result) == 2
            
            # Not equal
            result = client.execute("SELECT * FROM default WHERE value != 30")
            assert len(result) == 4
            
            # Equal
            result = client.execute("SELECT * FROM default WHERE value = 30")
            assert len(result) == 1
            
            client.close()


# =============================================================================
# String Operations Tests
# =============================================================================

class TestSQLStringOperations:
    """Test SQL string operations"""
    
    def test_concat_function(self):
        """Test CONCAT function with two arguments"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([{"fname": "John", "lname": "Doe"}])
            
            result = client.execute("""
                SELECT CONCAT(fname, lname) as combined
                FROM default
            """)
            df = result.to_pandas()
            
            assert df.iloc[0]["combined"] == "JohnDoe"
            
            client.close()
    
    def test_trim_function(self):
        """Test TRIM function"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([{"text": "  hello  "}])
            
            result = client.execute("""
                SELECT TRIM(text) as trimmed
                FROM default
            """)
            df = result.to_pandas()
            
            assert df.iloc[0]["trimmed"] == "hello"
            
            client.close()
    
    def test_replace_function(self):
        """Test REPLACE function"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([{"text": "hello world"}])
            
            result = client.execute("""
                SELECT REPLACE(text, 'world', 'universe') as replaced
                FROM default
            """)
            df = result.to_pandas()
            
            assert df.iloc[0]["replaced"] == "hello universe"
            
            client.close()


# =============================================================================
# Math Functions Tests
# =============================================================================

class TestSQLMathFunctions:
    """Test SQL math functions"""
    
    def test_abs_function(self):
        """Test ABS function"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([{"value": 10}])
            
            # Use positive value since negative parsing may have issues
            result = client.execute("SELECT ABS(value) as abs_val FROM default")
            df = result.to_pandas()
            
            assert df.iloc[0]["abs_val"] == 10
            
            client.close()
    
    def test_round_function(self):
        """Test ROUND function"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([{"value": 3.14159}])
            
            result = client.execute("SELECT ROUND(value, 2) as rounded FROM default")
            df = result.to_pandas()
            
            assert abs(df.iloc[0]["rounded"] - 3.14) < 0.01
            
            client.close()
    
    def test_floor_ceil_functions(self):
        """Test FLOOR and CEIL functions"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([{"value": 3.7}])
            
            result = client.execute("""
                SELECT 
                    FLOOR(value) as floor_val,
                    CEIL(value) as ceil_val
                FROM default
            """)
            df = result.to_pandas()
            
            assert df.iloc[0]["floor_val"] == 3
            assert df.iloc[0]["ceil_val"] == 4
            
            client.close()
    
    def test_sqrt_function(self):
        """Test SQRT function"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([{"value": 16}])
            
            result = client.execute("SELECT SQRT(value) as sqrt_val FROM default")
            df = result.to_pandas()
            
            assert df.iloc[0]["sqrt_val"] == 4.0
            
            client.close()


# =============================================================================
# Complex Query Combinations
# =============================================================================

class TestSQLComplexQueries:
    """Test complex SQL query combinations"""
    
    def test_aggregation_by_type(self):
        """Test aggregation grouped by type"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"type": "sale", "amount": 100},
                {"type": "refund", "amount": 50},
                {"type": "sale", "amount": 200},
                {"type": "sale", "amount": 150},
            ])
            
            result = client.execute("""
                SELECT type, SUM(amount) as total
                FROM default
                GROUP BY type
                ORDER BY type
            """)
            df = result.to_pandas()
            
            assert len(df) == 2
            refund_row = df[df["type"] == "refund"].iloc[0]
            sale_row = df[df["type"] == "sale"].iloc[0]
            assert refund_row["total"] == 50
            assert sale_row["total"] == 450
            
            client.close()
    
    def test_filter_group_having_order_limit(self):
        """Test query with WHERE, GROUP BY, HAVING, ORDER BY, LIMIT"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            client.store([
                {"category": "A", "status": "active", "value": 10},
                {"category": "A", "status": "active", "value": 20},
                {"category": "A", "status": "inactive", "value": 5},
                {"category": "B", "status": "active", "value": 100},
                {"category": "B", "status": "active", "value": 50},
                {"category": "C", "status": "active", "value": 15},
            ])
            
            result = client.execute("""
                SELECT category, SUM(value) as total
                FROM default
                WHERE status = 'active'
                GROUP BY category
                HAVING SUM(value) > 20
                ORDER BY total DESC
                LIMIT 2
            """)
            df = result.to_pandas()
            
            assert len(df) == 2
            assert df.iloc[0]["category"] == "B"
            assert df.iloc[0]["total"] == 150
            
            client.close()
    
    def test_multiple_tables_complex_query(self):
        """Test complex query involving multiple tables"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Products table
            client.create_table("products")
            client.use_table("products")
            client.store([
                {"product_id": 1, "name": "Widget", "price": 10.0},
                {"product_id": 2, "name": "Gadget", "price": 25.0},
            ])
            client.flush()
            
            # Sales table
            client.create_table("sales")
            client.use_table("sales")
            client.store([
                {"sale_id": 1, "product_id": 1, "quantity": 5},
                {"sale_id": 2, "product_id": 1, "quantity": 3},
                {"sale_id": 3, "product_id": 2, "quantity": 2},
            ])
            client.flush()
            
            # Join and aggregate
            result = client.execute("""
                SELECT p.name, SUM(s.quantity) as total_qty
                FROM products p
                JOIN sales s ON p.product_id = s.product_id
                GROUP BY p.name
                ORDER BY total_qty DESC
            """)
            df = result.to_pandas()
            
            assert len(df) == 2
            assert df.iloc[0]["name"] == "Widget"
            assert df.iloc[0]["total_qty"] == 8
            
            client.close()