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
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
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
// 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.

//! Abstract Syntax Tree (AST) types for SQL parser
//!
//! This module defines the AST node types that represent parsed SQL statements
//! and expressions.

use super::token::{Position, Token, TokenType};
use crate::common::SmartString;
use rustc_hash::FxHashMap;
use std::fmt;

use crate::common::CompactArc;
use crate::core::{ForeignKeyAction, ValueSet};

// ============================================================================
// Core Traits
// ============================================================================

/// Node trait - base for all AST nodes
pub trait Node: fmt::Display + fmt::Debug {
    /// Returns the literal string of the first token
    fn token_literal(&self) -> &str;
    /// Returns the position of the node in source code
    fn position(&self) -> Position;
}

// ============================================================================
// Expressions
// ============================================================================

/// Expression enum representing all expression types
#[derive(Debug, Clone, PartialEq)]
pub enum Expression {
    /// Identifier (column name, table name)
    Identifier(Identifier),
    /// Qualified identifier (table.column)
    QualifiedIdentifier(QualifiedIdentifier),
    /// Integer literal
    IntegerLiteral(IntegerLiteral),
    /// Float literal
    FloatLiteral(FloatLiteral),
    /// String literal
    StringLiteral(StringLiteral),
    /// Boolean literal (TRUE/FALSE)
    BooleanLiteral(BooleanLiteral),
    /// NULL literal
    NullLiteral(NullLiteral),
    /// INTERVAL literal
    IntervalLiteral(IntervalLiteral),
    /// Parameter ($1, ?)
    Parameter(Parameter),
    /// Prefix expression (-x, NOT x)
    Prefix(PrefixExpression),
    /// Infix expression (a + b, a = b)
    Infix(InfixExpression),
    /// List of expressions (for IN clause) - Boxed to reduce enum size
    List(Box<ListExpression>),
    /// DISTINCT expression
    Distinct(DistinctExpression),
    /// EXISTS subquery
    Exists(ExistsExpression),
    /// ALL/ANY/SOME subquery comparison (e.g., x > ALL (SELECT ...))
    AllAny(AllAnyExpression),
    /// IN expression
    In(InExpression),
    /// Pre-computed IN expression with HashSet (for semi-join optimization)
    /// Uses Arc for cheap cloning in parallel execution
    InHashSet(InHashSetExpression),
    /// BETWEEN expression
    Between(BetweenExpression),
    /// LIKE expression (with optional ESCAPE clause)
    Like(LikeExpression),
    /// Scalar subquery
    ScalarSubquery(ScalarSubquery),
    /// Expression list (for IN values) - Boxed to reduce enum size
    ExpressionList(Box<ExpressionList>),
    /// CASE expression - Boxed to reduce enum size
    Case(Box<CaseExpression>),
    /// CAST expression
    Cast(CastExpression),
    /// Function call - Boxed to reduce enum size (has 2 Vecs)
    FunctionCall(Box<FunctionCall>),
    /// Aliased expression (expr AS alias)
    Aliased(AliasedExpression),
    /// Window expression - Boxed to reduce enum size (has 2 Vecs)
    Window(Box<WindowExpression>),
    /// Simple table source - Boxed to reduce enum size
    TableSource(Box<SimpleTableSource>),
    /// Join table source
    JoinSource(Box<JoinTableSource>),
    /// Subquery table source - Boxed to reduce enum size (216 bytes unboxed)
    SubquerySource(Box<SubqueryTableSource>),
    /// VALUES table source - Boxed to reduce enum size (256 bytes unboxed)
    ValuesSource(Box<ValuesTableSource>),
    /// CTE reference - Boxed to reduce enum size (336 bytes unboxed)
    CteReference(Box<CteReference>),
    /// Function table source (table-valued function in FROM clause) - Boxed to reduce enum size
    FunctionTableSource(Box<FunctionTableSource>),
    /// Star (*) for SELECT *
    Star(StarExpression),
    /// Qualified star (table.*) for SELECT table.*
    QualifiedStar(QualifiedStarExpression),
    /// DEFAULT keyword (for INSERT VALUES)
    Default(DefaultExpression),
}

impl fmt::Display for Expression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Expression::Identifier(e) => write!(f, "{}", e),
            Expression::QualifiedIdentifier(e) => write!(f, "{}", e),
            Expression::IntegerLiteral(e) => write!(f, "{}", e),
            Expression::FloatLiteral(e) => write!(f, "{}", e),
            Expression::StringLiteral(e) => write!(f, "{}", e),
            Expression::BooleanLiteral(e) => write!(f, "{}", e),
            Expression::NullLiteral(e) => write!(f, "{}", e),
            Expression::IntervalLiteral(e) => write!(f, "{}", e),
            Expression::Parameter(e) => write!(f, "{}", e),
            Expression::Prefix(e) => write!(f, "{}", e),
            Expression::Infix(e) => write!(f, "{}", e),
            Expression::List(e) => write!(f, "{}", e),
            Expression::Distinct(e) => write!(f, "{}", e),
            Expression::Exists(e) => write!(f, "{}", e),
            Expression::AllAny(e) => write!(f, "{}", e),
            Expression::In(e) => write!(f, "{}", e),
            Expression::InHashSet(e) => write!(f, "{}", e),
            Expression::Between(e) => write!(f, "{}", e),
            Expression::Like(e) => write!(f, "{}", e),
            Expression::ScalarSubquery(e) => write!(f, "{}", e),
            Expression::ExpressionList(e) => write!(f, "{}", e),
            Expression::Case(e) => write!(f, "{}", e),
            Expression::Cast(e) => write!(f, "{}", e),
            Expression::FunctionCall(e) => write!(f, "{}", e),
            Expression::Aliased(e) => write!(f, "{}", e),
            Expression::Window(e) => write!(f, "{}", e),
            Expression::TableSource(e) => write!(f, "{}", e),
            Expression::JoinSource(e) => write!(f, "{}", e),
            Expression::SubquerySource(e) => write!(f, "{}", e),
            Expression::ValuesSource(e) => write!(f, "{}", e),
            Expression::CteReference(e) => write!(f, "{}", e),
            Expression::FunctionTableSource(e) => write!(f, "{}", e),
            Expression::Star(e) => write!(f, "{}", e),
            Expression::QualifiedStar(e) => write!(f, "{}", e),
            Expression::Default(e) => write!(f, "{}", e),
        }
    }
}

impl Expression {
    /// Get the position of this expression
    pub fn position(&self) -> Position {
        match self {
            Expression::Identifier(e) => e.token.position,
            Expression::QualifiedIdentifier(e) => e.token.position,
            Expression::IntegerLiteral(e) => e.token.position,
            Expression::FloatLiteral(e) => e.token.position,
            Expression::StringLiteral(e) => e.token.position,
            Expression::BooleanLiteral(e) => e.token.position,
            Expression::NullLiteral(e) => e.token.position,
            Expression::IntervalLiteral(e) => e.token.position,
            Expression::Parameter(e) => e.token.position,
            Expression::Prefix(e) => e.token.position,
            Expression::Infix(e) => e.token.position,
            Expression::List(e) => e.token.position,
            Expression::Distinct(e) => e.token.position,
            Expression::Exists(e) => e.token.position,
            Expression::AllAny(e) => e.token.position,
            Expression::In(e) => e.token.position,
            Expression::InHashSet(e) => e.token.position,
            Expression::Between(e) => e.token.position,
            Expression::Like(e) => e.token.position,
            Expression::ScalarSubquery(e) => e.token.position,
            Expression::ExpressionList(e) => e.token.position,
            Expression::Case(e) => e.token.position,
            Expression::Cast(e) => e.token.position,
            Expression::FunctionCall(e) => e.token.position,
            Expression::Aliased(e) => e.token.position,
            Expression::Window(e) => e.token.position,
            Expression::TableSource(e) => e.token.position,
            Expression::JoinSource(e) => e.token.position,
            Expression::SubquerySource(e) => e.token.position,
            Expression::ValuesSource(e) => e.token.position,
            Expression::CteReference(e) => e.token.position,
            Expression::FunctionTableSource(e) => e.token.position,
            Expression::Star(e) => e.token.position,
            Expression::QualifiedStar(e) => e.token.position,
            Expression::Default(e) => e.token.position,
        }
    }
}

// ============================================================================
// Expression Types
// ============================================================================

/// Identifier (column name, table name, etc.)
#[derive(Debug, Clone, PartialEq)]
pub struct Identifier {
    pub token: Token,
    pub value: SmartString,
    /// Pre-computed lowercase value for fast case-insensitive lookups
    pub value_lower: SmartString,
}

impl Identifier {
    /// Create a new identifier with pre-computed lowercase value.
    /// Keywords are uppercased by the lexer for parsing; when used as identifiers
    /// (column names, aliases, table names), fold to lowercase like PostgreSQL.
    #[inline]
    pub fn new(token: Token, value: impl Into<SmartString>) -> Self {
        let value = value.into();
        if token.token_type == TokenType::Keyword {
            // Keywords are uppercased by the lexer; fold to lowercase like PostgreSQL.
            // value and value_lower are identical, so avoid double lowercasing.
            let lowered = value.to_lowercase();
            Self {
                token,
                value_lower: lowered.clone(),
                value: lowered,
            }
        } else {
            let value_lower = value.to_lowercase();
            Self {
                token,
                value,
                value_lower,
            }
        }
    }
}

impl fmt::Display for Identifier {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.value)
    }
}

/// Qualified identifier (table.column)
#[derive(Debug, Clone, PartialEq)]
pub struct QualifiedIdentifier {
    pub token: Token,
    pub qualifier: Box<Identifier>,
    pub name: Box<Identifier>,
}

impl fmt::Display for QualifiedIdentifier {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}.{}", self.qualifier, self.name)
    }
}

/// Integer literal
#[derive(Debug, Clone, PartialEq)]
pub struct IntegerLiteral {
    pub token: Token,
    pub value: i64,
}

impl fmt::Display for IntegerLiteral {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.value)
    }
}

/// Float literal
#[derive(Debug, Clone, PartialEq)]
pub struct FloatLiteral {
    pub token: Token,
    pub value: f64,
}

impl fmt::Display for FloatLiteral {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.token.literal)
    }
}

/// String literal
#[derive(Debug, Clone, PartialEq)]
pub struct StringLiteral {
    pub token: Token,
    pub value: SmartString,
    /// Optional type hint (DATE, TIME, JSON, etc.)
    pub type_hint: Option<SmartString>,
}

impl fmt::Display for StringLiteral {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "'{}'", self.value)
    }
}

/// Boolean literal
#[derive(Debug, Clone, PartialEq)]
pub struct BooleanLiteral {
    pub token: Token,
    pub value: bool,
}

impl fmt::Display for BooleanLiteral {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", if self.value { "TRUE" } else { "FALSE" })
    }
}

/// NULL literal
#[derive(Debug, Clone, PartialEq)]
pub struct NullLiteral {
    pub token: Token,
}

impl fmt::Display for NullLiteral {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "NULL")
    }
}

/// INTERVAL literal
#[derive(Debug, Clone, PartialEq)]
pub struct IntervalLiteral {
    pub token: Token,
    pub value: SmartString,
    pub quantity: i64,
    pub unit: SmartString,
}

impl fmt::Display for IntervalLiteral {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "INTERVAL '{}'", self.value)
    }
}

/// Parameter ($1, ?)
#[derive(Debug, Clone, PartialEq)]
pub struct Parameter {
    pub token: Token,
    pub name: SmartString,
    pub index: usize,
}

impl fmt::Display for Parameter {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.name.is_empty() {
            write!(f, "?")
        } else {
            write!(f, "{}", self.name)
        }
    }
}

/// Infix operator type (pre-computed at parse time for zero-allocation evaluation)
/// This is a key optimization: instead of string comparison for every row,
/// we match on a small enum which is faster and allocation-free.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum InfixOperator {
    // Comparison operators
    Equal,        // =
    NotEqual,     // <> or !=
    LessThan,     // <
    LessEqual,    // <=
    GreaterThan,  // >
    GreaterEqual, // >=

    // Logical operators
    And,
    Or,
    Xor,

    // Arithmetic operators
    Add,      // +
    Subtract, // -
    Multiply, // *
    Divide,   // /
    Modulo,   // % or MOD

    // String operators
    Concat, // ||

    // Pattern matching
    Like,
    ILike,
    NotLike,
    NotILike,
    Glob,
    NotGlob,
    Regexp,
    NotRegexp,

    // Null check
    Is,                // IS (NULL)
    IsNot,             // IS NOT (NULL)
    IsDistinctFrom,    // IS DISTINCT FROM (NULL-safe not equal)
    IsNotDistinctFrom, // IS NOT DISTINCT FROM (NULL-safe equal)

    // Array index
    Index, // []

    // JSON operators
    JsonAccess,     // -> (returns JSON)
    JsonAccessText, // ->> (returns TEXT)

    // Vector distance operator
    VectorDistance, // <=>

    // Bitwise operators
    BitwiseAnd, // &
    BitwiseOr,  // |
    BitwiseXor, // ^
    LeftShift,  // <<
    RightShift, // >>

    // Unknown/other (fallback for rare operators)
    Other,
}

impl InfixOperator {
    /// Parse operator string to enum (called once at parse time)
    #[inline]
    #[allow(clippy::should_implement_trait)]
    pub fn from_str(s: &str) -> Self {
        match s.to_uppercase().as_str() {
            "=" => InfixOperator::Equal,
            "<>" | "!=" => InfixOperator::NotEqual,
            "<" => InfixOperator::LessThan,
            "<=" => InfixOperator::LessEqual,
            ">" => InfixOperator::GreaterThan,
            ">=" => InfixOperator::GreaterEqual,
            "AND" => InfixOperator::And,
            "OR" => InfixOperator::Or,
            "XOR" => InfixOperator::Xor,
            "+" => InfixOperator::Add,
            "-" => InfixOperator::Subtract,
            "*" => InfixOperator::Multiply,
            "/" => InfixOperator::Divide,
            "%" | "MOD" => InfixOperator::Modulo,
            "||" => InfixOperator::Concat,
            "LIKE" => InfixOperator::Like,
            "ILIKE" => InfixOperator::ILike,
            "NOT LIKE" => InfixOperator::NotLike,
            "NOT ILIKE" => InfixOperator::NotILike,
            "GLOB" => InfixOperator::Glob,
            "NOT GLOB" => InfixOperator::NotGlob,
            "REGEXP" | "RLIKE" => InfixOperator::Regexp,
            "NOT REGEXP" | "NOT RLIKE" => InfixOperator::NotRegexp,
            "IS" => InfixOperator::Is,
            "IS NOT" => InfixOperator::IsNot,
            "IS DISTINCT FROM" => InfixOperator::IsDistinctFrom,
            "IS NOT DISTINCT FROM" => InfixOperator::IsNotDistinctFrom,
            "[]" => InfixOperator::Index,
            "->" => InfixOperator::JsonAccess,
            "->>" => InfixOperator::JsonAccessText,
            "<=>" => InfixOperator::VectorDistance,
            "&" => InfixOperator::BitwiseAnd,
            "|" => InfixOperator::BitwiseOr,
            "^" => InfixOperator::BitwiseXor,
            "<<" => InfixOperator::LeftShift,
            ">>" => InfixOperator::RightShift,
            _ => InfixOperator::Other,
        }
    }
}

/// Prefix operator type (pre-computed at parse time)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PrefixOperator {
    Negate,     // -
    Not,        // NOT
    Plus,       // + (unary plus, no-op)
    BitwiseNot, // ~ (bitwise NOT)
    Other,
}

impl PrefixOperator {
    /// Parse operator string to enum (called once at parse time)
    #[inline]
    #[allow(clippy::should_implement_trait)]
    pub fn from_str(s: &str) -> Self {
        match s.to_uppercase().as_str() {
            "-" => PrefixOperator::Negate,
            "NOT" => PrefixOperator::Not,
            "+" => PrefixOperator::Plus,
            "~" => PrefixOperator::BitwiseNot,
            _ => PrefixOperator::Other,
        }
    }
}

/// Star expression (*)
#[derive(Debug, Clone, PartialEq)]
pub struct StarExpression {
    pub token: Token,
}

impl fmt::Display for StarExpression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "*")
    }
}

/// Qualified star expression (table.*)
#[derive(Debug, Clone, PartialEq)]
pub struct QualifiedStarExpression {
    pub token: Token,
    pub qualifier: SmartString,
}

impl fmt::Display for QualifiedStarExpression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}.*", self.qualifier)
    }
}

/// DEFAULT keyword expression (for INSERT VALUES)
#[derive(Debug, Clone, PartialEq)]
pub struct DefaultExpression {
    pub token: Token,
}

impl fmt::Display for DefaultExpression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "DEFAULT")
    }
}

/// Prefix expression (-x, NOT x)
#[derive(Debug, Clone, PartialEq)]
pub struct PrefixExpression {
    pub token: Token,
    pub operator: SmartString,
    /// Pre-computed operator type for fast evaluation (no string comparison)
    pub op_type: PrefixOperator,
    pub right: Box<Expression>,
}

impl PrefixExpression {
    /// Create a new prefix expression with auto-computed op_type
    #[inline]
    pub fn new(token: Token, operator: impl Into<SmartString>, right: Box<Expression>) -> Self {
        let operator = operator.into();
        let op_type = PrefixOperator::from_str(&operator);
        Self {
            token,
            operator,
            op_type,
            right,
        }
    }
}

impl fmt::Display for PrefixExpression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.operator == "-" || self.operator == "+" {
            write!(f, "({}{})", self.operator, self.right)
        } else {
            write!(f, "({} {})", self.operator, self.right)
        }
    }
}

/// Infix expression (a + b, a = b)
#[derive(Debug, Clone, PartialEq)]
pub struct InfixExpression {
    pub token: Token,
    pub left: Box<Expression>,
    pub operator: SmartString,
    /// Pre-computed operator type for fast evaluation (no string comparison)
    pub op_type: InfixOperator,
    pub right: Box<Expression>,
}

impl InfixExpression {
    /// Create a new infix expression with auto-computed op_type
    #[inline]
    pub fn new(
        token: Token,
        left: Box<Expression>,
        operator: impl Into<SmartString>,
        right: Box<Expression>,
    ) -> Self {
        let operator = operator.into();
        let op_type = InfixOperator::from_str(&operator);
        Self {
            token,
            left,
            operator,
            op_type,
            right,
        }
    }
}

impl fmt::Display for InfixExpression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "({} {} {})", self.left, self.operator, self.right)
    }
}

/// List expression (for IN clause values)
#[derive(Debug, Clone, PartialEq)]
pub struct ListExpression {
    pub token: Token,
    pub elements: Vec<Expression>,
}

impl fmt::Display for ListExpression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let elements: Vec<String> = self.elements.iter().map(|e| e.to_string()).collect();
        write!(f, "({})", elements.join(", "))
    }
}

/// DISTINCT expression
#[derive(Debug, Clone, PartialEq)]
pub struct DistinctExpression {
    pub token: Token,
    pub expr: Box<Expression>,
}

impl fmt::Display for DistinctExpression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "DISTINCT {}", self.expr)
    }
}

/// EXISTS expression
#[derive(Debug, Clone, PartialEq)]
pub struct ExistsExpression {
    pub token: Token,
    pub subquery: Box<SelectStatement>,
}

impl fmt::Display for ExistsExpression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "EXISTS ({})", self.subquery)
    }
}

/// ALL/ANY comparison type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AllAnyType {
    All,
    Any,
}

impl fmt::Display for AllAnyType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            AllAnyType::All => write!(f, "ALL"),
            AllAnyType::Any => write!(f, "ANY"),
        }
    }
}

/// ALL/ANY/SOME subquery expression (e.g., x > ALL (SELECT ...))
#[derive(Debug, Clone, PartialEq)]
pub struct AllAnyExpression {
    pub token: Token,
    pub left: Box<Expression>,
    pub operator: SmartString,
    pub all_any_type: AllAnyType,
    pub subquery: Box<SelectStatement>,
}

impl fmt::Display for AllAnyExpression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{} {} {} ({})",
            self.left, self.operator, self.all_any_type, self.subquery
        )
    }
}

/// IN expression
#[derive(Debug, Clone, PartialEq)]
pub struct InExpression {
    pub token: Token,
    pub left: Box<Expression>,
    pub right: Box<Expression>,
    pub not: bool,
}

impl fmt::Display for InExpression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.not {
            write!(f, "{} NOT IN {}", self.left, self.right)
        } else {
            write!(f, "{} IN {}", self.left, self.right)
        }
    }
}

/// Pre-computed IN expression with HashSet for O(1) lookup
///
/// This is used by the semi-join optimization to avoid rebuilding
/// the HashSet on every row during parallel filtering.
/// Arc enables cheap cloning when the expression is cloned for parallel execution.
#[derive(Debug, Clone)]
pub struct InHashSetExpression {
    pub token: Token,
    /// The column/expression to check
    pub column: Box<Expression>,
    /// Pre-computed ValueSet for O(1) lookup - Arc for cheap parallel cloning
    pub values: CompactArc<ValueSet>,
    /// Whether this is NOT IN
    pub not: bool,
}

impl PartialEq for InHashSetExpression {
    fn eq(&self, other: &Self) -> bool {
        // Compare by CompactArc pointer for efficiency (same HashSet = same CompactArc)
        self.not == other.not
            && CompactArc::ptr_eq(&self.values, &other.values)
            && self.column == other.column
    }
}

impl fmt::Display for InHashSetExpression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.not {
            write!(f, "{} NOT IN (<{} values>)", self.column, self.values.len())
        } else {
            write!(f, "{} IN (<{} values>)", self.column, self.values.len())
        }
    }
}

/// BETWEEN expression
#[derive(Debug, Clone, PartialEq)]
pub struct BetweenExpression {
    pub token: Token,
    pub expr: Box<Expression>,
    pub lower: Box<Expression>,
    pub upper: Box<Expression>,
    pub not: bool,
}

impl fmt::Display for BetweenExpression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.not {
            write!(
                f,
                "{} NOT BETWEEN {} AND {}",
                self.expr, self.lower, self.upper
            )
        } else {
            write!(f, "{} BETWEEN {} AND {}", self.expr, self.lower, self.upper)
        }
    }
}

/// LIKE expression with optional ESCAPE clause
#[derive(Debug, Clone, PartialEq)]
pub struct LikeExpression {
    pub token: Token,
    pub left: Box<Expression>,
    pub pattern: Box<Expression>,
    /// The operator: LIKE, ILIKE, NOT LIKE, NOT ILIKE, GLOB, NOT GLOB, REGEXP, RLIKE, NOT REGEXP, NOT RLIKE
    pub operator: SmartString,
    /// Optional escape character
    pub escape: Option<Box<Expression>>,
}

impl fmt::Display for LikeExpression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} {} {}", self.left, self.operator, self.pattern)?;
        if let Some(ref escape) = self.escape {
            write!(f, " ESCAPE {}", escape)?;
        }
        Ok(())
    }
}

/// Scalar subquery
#[derive(Debug, Clone, PartialEq)]
pub struct ScalarSubquery {
    pub token: Token,
    pub subquery: Box<SelectStatement>,
}

impl fmt::Display for ScalarSubquery {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "({})", self.subquery)
    }
}

/// Expression list (for IN values)
#[derive(Debug, Clone, PartialEq)]
pub struct ExpressionList {
    pub token: Token,
    pub expressions: Vec<Expression>,
}

impl fmt::Display for ExpressionList {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let exprs: Vec<String> = self.expressions.iter().map(|e| e.to_string()).collect();
        write!(f, "({})", exprs.join(", "))
    }
}

/// CASE expression
#[derive(Debug, Clone, PartialEq)]
pub struct CaseExpression {
    pub token: Token,
    pub value: Option<Box<Expression>>,
    pub when_clauses: Vec<WhenClause>,
    pub else_value: Option<Box<Expression>>,
}

impl fmt::Display for CaseExpression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut result = String::from("CASE");
        if let Some(ref val) = self.value {
            result.push_str(&format!(" {}", val));
        }
        for when in &self.when_clauses {
            result.push_str(&format!(" {}", when));
        }
        if let Some(ref else_val) = self.else_value {
            result.push_str(&format!(" ELSE {}", else_val));
        }
        result.push_str(" END");
        write!(f, "{}", result)
    }
}

/// WHEN clause in CASE expression
#[derive(Debug, Clone, PartialEq)]
pub struct WhenClause {
    pub token: Token,
    pub condition: Expression,
    pub then_result: Expression,
}

impl fmt::Display for WhenClause {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "WHEN {} THEN {}", self.condition, self.then_result)
    }
}

/// CAST expression
#[derive(Debug, Clone, PartialEq)]
pub struct CastExpression {
    pub token: Token,
    pub expr: Box<Expression>,
    pub type_name: SmartString,
}

impl fmt::Display for CastExpression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "CAST({} AS {})", self.expr, self.type_name)
    }
}

/// Function call
#[derive(Debug, Clone, PartialEq)]
pub struct FunctionCall {
    pub token: Token,
    pub function: SmartString,
    pub arguments: Vec<Expression>,
    pub is_distinct: bool,
    pub order_by: Vec<OrderByExpression>,
    /// FILTER clause for aggregate functions (e.g., COUNT(*) FILTER (WHERE condition))
    pub filter: Option<Box<Expression>>,
}

impl fmt::Display for FunctionCall {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut args = String::new();
        if self.is_distinct && !self.arguments.is_empty() {
            args.push_str("DISTINCT ");
            args.push_str(&self.arguments[0].to_string());
            for arg in &self.arguments[1..] {
                args.push_str(", ");
                args.push_str(&arg.to_string());
            }
        } else {
            let arg_strs: Vec<String> = self
                .arguments
                .iter()
                .map(|a| {
                    if matches!(a, Expression::Star(_)) {
                        "*".to_string()
                    } else {
                        a.to_string()
                    }
                })
                .collect();
            args = arg_strs.join(", ");
        }
        if !self.order_by.is_empty() {
            args.push_str(" ORDER BY ");
            let order_strs: Vec<String> = self.order_by.iter().map(|o| o.to_string()).collect();
            args.push_str(&order_strs.join(", "));
        }
        write!(f, "{}({})", self.function, args)?;
        if let Some(filter) = &self.filter {
            write!(f, " FILTER (WHERE {})", filter)?;
        }
        Ok(())
    }
}

/// Aliased expression (expr AS alias)
#[derive(Debug, Clone, PartialEq)]
pub struct AliasedExpression {
    pub token: Token,
    pub expression: Box<Expression>,
    pub alias: Identifier,
}

impl fmt::Display for AliasedExpression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} AS {}", self.expression, self.alias)
    }
}

/// Window expression
#[derive(Debug, Clone, PartialEq)]
pub struct WindowExpression {
    pub token: Token,
    pub function: Box<FunctionCall>,
    /// Named window reference (e.g., OVER w)
    pub window_ref: Option<SmartString>,
    pub partition_by: Vec<Expression>,
    pub order_by: Vec<OrderByExpression>,
    pub frame: Option<WindowFrame>,
}

impl fmt::Display for WindowExpression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut result = self.function.to_string();
        if let Some(ref win_ref) = self.window_ref {
            result.push_str(" OVER ");
            result.push_str(win_ref);
        } else {
            result.push_str(" OVER (");
            if !self.partition_by.is_empty() {
                result.push_str("PARTITION BY ");
                let parts: Vec<String> = self.partition_by.iter().map(|e| e.to_string()).collect();
                result.push_str(&parts.join(", "));
            }
            if !self.order_by.is_empty() {
                if !self.partition_by.is_empty() {
                    result.push(' ');
                }
                result.push_str("ORDER BY ");
                let orders: Vec<String> = self.order_by.iter().map(|o| o.to_string()).collect();
                result.push_str(&orders.join(", "));
            }
            if let Some(ref frame) = self.frame {
                result.push(' ');
                result.push_str(&frame.to_string());
            }
            result.push(')');
        }
        write!(f, "{}", result)
    }
}

/// Window frame specification
#[derive(Debug, Clone, PartialEq)]
pub struct WindowFrame {
    pub unit: WindowFrameUnit,
    pub start: WindowFrameBound,
    pub end: Option<WindowFrameBound>,
}

impl fmt::Display for WindowFrame {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let unit = match self.unit {
            WindowFrameUnit::Rows => "ROWS",
            WindowFrameUnit::Range => "RANGE",
        };
        if let Some(ref end) = self.end {
            write!(f, "{} BETWEEN {} AND {}", unit, self.start, end)
        } else {
            write!(f, "{} {}", unit, self.start)
        }
    }
}

/// Window frame unit
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WindowFrameUnit {
    Rows,
    Range,
}

/// Window frame bound
#[derive(Debug, Clone, PartialEq)]
pub enum WindowFrameBound {
    CurrentRow,
    UnboundedPreceding,
    UnboundedFollowing,
    Preceding(Box<Expression>),
    Following(Box<Expression>),
}

impl fmt::Display for WindowFrameBound {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            WindowFrameBound::CurrentRow => write!(f, "CURRENT ROW"),
            WindowFrameBound::UnboundedPreceding => write!(f, "UNBOUNDED PRECEDING"),
            WindowFrameBound::UnboundedFollowing => write!(f, "UNBOUNDED FOLLOWING"),
            WindowFrameBound::Preceding(e) => write!(f, "{} PRECEDING", e),
            WindowFrameBound::Following(e) => write!(f, "{} FOLLOWING", e),
        }
    }
}

/// Named window definition (WINDOW w AS (...))
#[derive(Debug, Clone, PartialEq)]
pub struct WindowDefinition {
    pub name: SmartString,
    pub partition_by: Vec<Expression>,
    pub order_by: Vec<OrderByExpression>,
    pub frame: Option<WindowFrame>,
}

impl fmt::Display for WindowDefinition {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut result = format!("{} AS (", self.name);
        if !self.partition_by.is_empty() {
            result.push_str("PARTITION BY ");
            let parts: Vec<String> = self.partition_by.iter().map(|e| e.to_string()).collect();
            result.push_str(&parts.join(", "));
        }
        if !self.order_by.is_empty() {
            if !self.partition_by.is_empty() {
                result.push(' ');
            }
            result.push_str("ORDER BY ");
            let orders: Vec<String> = self.order_by.iter().map(|o| o.to_string()).collect();
            result.push_str(&orders.join(", "));
        }
        if let Some(ref frame) = self.frame {
            result.push(' ');
            result.push_str(&frame.to_string());
        }
        result.push(')');
        write!(f, "{}", result)
    }
}

// ============================================================================
// Table Sources
// ============================================================================

/// Simple table source
#[derive(Debug, Clone, PartialEq)]
pub struct SimpleTableSource {
    pub token: Token,
    pub name: Identifier,
    pub alias: Option<Identifier>,
    pub as_of: Option<AsOfClause>,
}

impl fmt::Display for SimpleTableSource {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut result = self.name.to_string();
        if let Some(ref as_of) = self.as_of {
            result.push_str(&format!(" {}", as_of));
        }
        if let Some(ref alias) = self.alias {
            result.push_str(&format!(" AS {}", alias));
        }
        write!(f, "{}", result)
    }
}

/// AS OF clause for temporal queries
#[derive(Debug, Clone, PartialEq)]
pub struct AsOfClause {
    pub token: Token,
    pub as_of_type: SmartString, // "TRANSACTION" or "TIMESTAMP"
    pub value: Box<Expression>,
}

impl fmt::Display for AsOfClause {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "AS OF {} {}", self.as_of_type, self.value)
    }
}

/// Join table source
#[derive(Debug, Clone, PartialEq)]
pub struct JoinTableSource {
    pub token: Token,
    pub left: Box<Expression>,
    pub join_type: SmartString,
    pub right: Box<Expression>,
    pub condition: Option<Box<Expression>>,
    /// USING clause columns (e.g., USING(id, name))
    pub using_columns: Vec<Identifier>,
}

impl fmt::Display for JoinTableSource {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut result = self.left.to_string();
        result.push_str(&format!(" {} JOIN {}", self.join_type, self.right));
        if let Some(ref cond) = self.condition {
            result.push_str(&format!(" ON {}", cond));
        } else if !self.using_columns.is_empty() {
            let cols: Vec<String> = self.using_columns.iter().map(|c| c.to_string()).collect();
            result.push_str(&format!(" USING ({})", cols.join(", ")));
        }
        write!(f, "{}", result)
    }
}

/// Subquery table source
#[derive(Debug, Clone, PartialEq)]
pub struct SubqueryTableSource {
    pub token: Token,
    pub subquery: Box<SelectStatement>,
    pub alias: Option<Identifier>,
}

impl fmt::Display for SubqueryTableSource {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut result = format!("({})", self.subquery);
        if let Some(ref alias) = self.alias {
            result.push_str(&format!(" AS {}", alias));
        }
        write!(f, "{}", result)
    }
}

/// VALUES table source (e.g., VALUES (1, 'a'), (2, 'b') AS t(col1, col2))
#[derive(Debug, Clone, PartialEq)]
pub struct ValuesTableSource {
    pub token: Token,
    /// Each row is a list of expressions
    pub rows: Vec<Vec<Expression>>,
    /// Optional alias for the derived table
    pub alias: Option<Identifier>,
    /// Optional column aliases (e.g., t(col1, col2))
    pub column_aliases: Vec<Identifier>,
}

impl fmt::Display for ValuesTableSource {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut result = String::from("(VALUES ");
        let rows_str: Vec<String> = self
            .rows
            .iter()
            .map(|row| {
                let values: Vec<String> = row.iter().map(|e| e.to_string()).collect();
                format!("({})", values.join(", "))
            })
            .collect();
        result.push_str(&rows_str.join(", "));
        result.push(')');

        if let Some(ref alias) = self.alias {
            result.push_str(&format!(" AS {}", alias));
            if !self.column_aliases.is_empty() {
                let cols: Vec<String> = self.column_aliases.iter().map(|c| c.to_string()).collect();
                result.push_str(&format!("({})", cols.join(", ")));
            }
        }
        write!(f, "{}", result)
    }
}

/// Function table source (table-valued function in FROM clause)
/// e.g., SELECT * FROM generate_series(1, 10) AS gs(value)
#[derive(Debug, Clone, PartialEq)]
pub struct FunctionTableSource {
    pub token: Token,
    /// Function name
    pub function: Identifier,
    /// Function arguments
    pub arguments: Vec<Expression>,
    /// Optional table alias
    pub alias: Option<Identifier>,
    /// Optional column aliases (e.g., AS gs(value))
    pub column_aliases: Vec<Identifier>,
}

impl fmt::Display for FunctionTableSource {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}(", self.function)?;
        for (i, arg) in self.arguments.iter().enumerate() {
            if i > 0 {
                write!(f, ", ")?;
            }
            write!(f, "{}", arg)?;
        }
        write!(f, ")")?;
        if let Some(ref alias) = self.alias {
            write!(f, " AS {}", alias)?;
            if !self.column_aliases.is_empty() {
                let cols: Vec<String> = self.column_aliases.iter().map(|c| c.to_string()).collect();
                write!(f, "({})", cols.join(", "))?;
            }
        }
        Ok(())
    }
}

/// CTE reference
#[derive(Debug, Clone, PartialEq)]
pub struct CteReference {
    pub token: Token,
    pub name: Identifier,
    pub alias: Option<Identifier>,
}

impl fmt::Display for CteReference {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut result = self.name.to_string();
        if let Some(ref alias) = self.alias {
            result.push_str(&format!(" AS {}", alias));
        }
        write!(f, "{}", result)
    }
}

// ============================================================================
// ORDER BY
// ============================================================================

/// ORDER BY expression
#[derive(Debug, Clone, PartialEq)]
pub struct OrderByExpression {
    pub expression: Expression,
    pub ascending: bool,
    /// None = default (NULLS LAST for ASC, NULLS FIRST for DESC in SQL standard)
    /// Some(true) = NULLS FIRST
    /// Some(false) = NULLS LAST
    pub nulls_first: Option<bool>,
}

impl fmt::Display for OrderByExpression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.ascending {
            write!(f, "{} ASC", self.expression)?;
        } else {
            write!(f, "{} DESC", self.expression)?;
        }
        if let Some(nulls_first) = self.nulls_first {
            if nulls_first {
                write!(f, " NULLS FIRST")?;
            } else {
                write!(f, " NULLS LAST")?;
            }
        }
        Ok(())
    }
}

// ============================================================================
// Statements
// ============================================================================

/// Statement enum representing all statement types
#[derive(Debug, Clone, PartialEq)]
pub enum Statement {
    Select(SelectStatement),
    Insert(InsertStatement),
    Update(UpdateStatement),
    Delete(DeleteStatement),
    Truncate(TruncateStatement),
    CreateTable(CreateTableStatement),
    DropTable(DropTableStatement),
    /// Boxed to reduce enum size (776 bytes unboxed)
    AlterTable(Box<AlterTableStatement>),
    CreateIndex(CreateIndexStatement),
    DropIndex(DropIndexStatement),
    CreateView(CreateViewStatement),
    DropView(DropViewStatement),
    Begin(BeginStatement),
    Commit(CommitStatement),
    Rollback(RollbackStatement),
    Savepoint(SavepointStatement),
    ReleaseSavepoint(ReleaseSavepointStatement),
    /// Boxed to reduce enum size (424 bytes unboxed)
    Set(Box<SetStatement>),
    Pragma(PragmaStatement),
    ShowTables(ShowTablesStatement),
    ShowViews(ShowViewsStatement),
    ShowCreateTable(ShowCreateTableStatement),
    ShowCreateView(ShowCreateViewStatement),
    ShowIndexes(ShowIndexesStatement),
    Describe(DescribeStatement),
    Expression(ExpressionStatement),
    Explain(ExplainStatement),
    Analyze(AnalyzeStatement),
    Vacuum(VacuumStatement),
}

impl fmt::Display for Statement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Statement::Select(s) => write!(f, "{}", s),
            Statement::Insert(s) => write!(f, "{}", s),
            Statement::Update(s) => write!(f, "{}", s),
            Statement::Delete(s) => write!(f, "{}", s),
            Statement::Truncate(s) => write!(f, "{}", s),
            Statement::CreateTable(s) => write!(f, "{}", s),
            Statement::DropTable(s) => write!(f, "{}", s),
            Statement::AlterTable(s) => write!(f, "{}", s),
            Statement::CreateIndex(s) => write!(f, "{}", s),
            Statement::DropIndex(s) => write!(f, "{}", s),
            Statement::CreateView(s) => write!(f, "{}", s),
            Statement::DropView(s) => write!(f, "{}", s),
            Statement::Begin(s) => write!(f, "{}", s),
            Statement::Commit(s) => write!(f, "{}", s),
            Statement::Rollback(s) => write!(f, "{}", s),
            Statement::Savepoint(s) => write!(f, "{}", s),
            Statement::ReleaseSavepoint(s) => write!(f, "{}", s),
            Statement::Set(s) => write!(f, "{}", s),
            Statement::Pragma(s) => write!(f, "{}", s),
            Statement::ShowTables(s) => write!(f, "{}", s),
            Statement::ShowViews(s) => write!(f, "{}", s),
            Statement::ShowCreateTable(s) => write!(f, "{}", s),
            Statement::ShowCreateView(s) => write!(f, "{}", s),
            Statement::ShowIndexes(s) => write!(f, "{}", s),
            Statement::Describe(s) => write!(f, "{}", s),
            Statement::Expression(s) => write!(f, "{}", s),
            Statement::Explain(s) => write!(f, "{}", s),
            Statement::Analyze(s) => write!(f, "{}", s),
            Statement::Vacuum(s) => write!(f, "{}", s),
        }
    }
}

/// Program (collection of statements)
#[derive(Debug, Clone, PartialEq)]
pub struct Program {
    pub statements: Vec<Statement>,
}

impl fmt::Display for Program {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for stmt in &self.statements {
            write!(f, "{};", stmt)?;
        }
        Ok(())
    }
}

// ============================================================================
// WITH Clause (CTEs)
// ============================================================================

/// Common Table Expression
#[derive(Debug, Clone, PartialEq)]
pub struct CommonTableExpression {
    pub token: Token,
    pub name: Identifier,
    pub column_names: Vec<Identifier>,
    pub query: Box<SelectStatement>,
    pub is_recursive: bool,
}

impl fmt::Display for CommonTableExpression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut result = self.name.to_string();
        if !self.column_names.is_empty() {
            let cols: Vec<String> = self.column_names.iter().map(|c| c.to_string()).collect();
            result.push_str(&format!("({})", cols.join(", ")));
        }
        result.push_str(&format!(" AS ({})", self.query));
        write!(f, "{}", result)
    }
}

/// WITH clause
#[derive(Debug, Clone, PartialEq)]
pub struct WithClause {
    pub token: Token,
    pub ctes: Vec<CommonTableExpression>,
    pub is_recursive: bool,
}

impl fmt::Display for WithClause {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut result = String::from("WITH ");
        if self.is_recursive {
            result.push_str("RECURSIVE ");
        }
        let cte_strs: Vec<String> = self.ctes.iter().map(|c| c.to_string()).collect();
        result.push_str(&cte_strs.join(", "));
        write!(f, "{}", result)
    }
}

// ============================================================================
// Statement Types
// ============================================================================

/// Set operation type for compound queries
#[derive(Debug, Clone, PartialEq)]
pub enum SetOperationType {
    Union,
    UnionAll,
    Intersect,
    IntersectAll,
    Except,
    ExceptAll,
}

impl fmt::Display for SetOperationType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            SetOperationType::Union => write!(f, "UNION"),
            SetOperationType::UnionAll => write!(f, "UNION ALL"),
            SetOperationType::Intersect => write!(f, "INTERSECT"),
            SetOperationType::IntersectAll => write!(f, "INTERSECT ALL"),
            SetOperationType::Except => write!(f, "EXCEPT"),
            SetOperationType::ExceptAll => write!(f, "EXCEPT ALL"),
        }
    }
}

/// Set operation combining two SELECT statements
#[derive(Debug, Clone, PartialEq)]
pub struct SetOperation {
    pub operation: SetOperationType,
    pub right: Box<SelectStatement>,
}

/// Group by modifier (ROLLUP, CUBE, GROUPING SETS, or none)
#[derive(Debug, Clone, PartialEq, Default)]
pub enum GroupByModifier {
    #[default]
    None,
    Rollup,
    Cube,
    /// GROUPING SETS - each inner Vec is one grouping set
    /// e.g., GROUPING SETS ((a, b), (a), ()) has 3 sets
    GroupingSets(Vec<Vec<Expression>>),
}

/// GROUP BY clause with optional ROLLUP/CUBE modifier
#[derive(Debug, Clone, PartialEq, Default)]
pub struct GroupByClause {
    pub columns: Vec<Expression>,
    pub modifier: GroupByModifier,
}

impl fmt::Display for GroupByClause {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // GROUPING SETS uses its own column lists, not self.columns
        if let GroupByModifier::GroupingSets(sets) = &self.modifier {
            let sets_str: Vec<String> = sets
                .iter()
                .map(|set| {
                    let cols: Vec<String> = set.iter().map(|c| c.to_string()).collect();
                    format!("({})", cols.join(", "))
                })
                .collect();
            return write!(f, "GROUPING SETS ({})", sets_str.join(", "));
        }

        // None, Rollup, Cube all use self.columns
        if self.columns.is_empty() {
            return Ok(());
        }
        let cols: Vec<String> = self.columns.iter().map(|c| c.to_string()).collect();
        match &self.modifier {
            GroupByModifier::None => write!(f, "{}", cols.join(", ")),
            GroupByModifier::Rollup => write!(f, "ROLLUP({})", cols.join(", ")),
            GroupByModifier::Cube => write!(f, "CUBE({})", cols.join(", ")),
            GroupByModifier::GroupingSets(_) => Ok(()), // Already handled above
        }
    }
}

/// SELECT statement
#[derive(Debug, Clone, PartialEq)]
pub struct SelectStatement {
    pub token: Token,
    pub distinct: bool,
    /// DISTINCT ON (expr1, expr2, ...) expressions. Empty for regular DISTINCT or no DISTINCT.
    pub distinct_on: Vec<Expression>,
    pub columns: Vec<Expression>,
    pub with: Option<WithClause>,
    pub table_expr: Option<Box<Expression>>,
    pub where_clause: Option<Box<Expression>>,
    pub group_by: GroupByClause,
    pub having: Option<Box<Expression>>,
    /// Named window definitions (WINDOW w AS (...))
    pub window_defs: Vec<WindowDefinition>,
    pub order_by: Vec<OrderByExpression>,
    pub limit: Option<Box<Expression>>,
    pub offset: Option<Box<Expression>>,
    /// Set operations (UNION, INTERSECT, EXCEPT)
    pub set_operations: Vec<SetOperation>,
}

impl fmt::Display for SelectStatement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut result = String::new();
        if let Some(ref with) = self.with {
            result.push_str(&format!("{} ", with));
        }
        result.push_str("SELECT ");
        if !self.distinct_on.is_empty() {
            let on_cols: Vec<String> = self.distinct_on.iter().map(|e| e.to_string()).collect();
            result.push_str(&format!("DISTINCT ON ({}) ", on_cols.join(", ")));
        } else if self.distinct {
            result.push_str("DISTINCT ");
        }
        let cols: Vec<String> = self.columns.iter().map(|c| c.to_string()).collect();
        result.push_str(&cols.join(", "));
        if let Some(ref table) = self.table_expr {
            result.push_str(&format!(" FROM {}", table));
        }
        if let Some(ref where_clause) = self.where_clause {
            result.push_str(&format!(" WHERE {}", where_clause));
        }
        if !self.group_by.columns.is_empty() {
            result.push_str(&format!(" GROUP BY {}", self.group_by));
        }
        if let Some(ref having) = self.having {
            result.push_str(&format!(" HAVING {}", having));
        }
        if !self.window_defs.is_empty() {
            let wins: Vec<String> = self.window_defs.iter().map(|w| w.to_string()).collect();
            result.push_str(&format!(" WINDOW {}", wins.join(", ")));
        }
        if !self.order_by.is_empty() {
            let orders: Vec<String> = self.order_by.iter().map(|o| o.to_string()).collect();
            result.push_str(&format!(" ORDER BY {}", orders.join(", ")));
        }
        if let Some(ref limit) = self.limit {
            result.push_str(&format!(" LIMIT {}", limit));
        }
        if let Some(ref offset) = self.offset {
            result.push_str(&format!(" OFFSET {}", offset));
        }
        // Add set operations
        for set_op in &self.set_operations {
            result.push_str(&format!(" {} {}", set_op.operation, set_op.right));
        }
        write!(f, "{}", result)
    }
}

/// INSERT statement
#[derive(Debug, Clone, PartialEq)]
pub struct InsertStatement {
    pub token: Token,
    pub table_name: Identifier,
    pub columns: Vec<Identifier>,
    /// VALUES clause rows (None if using SELECT)
    pub values: Vec<Vec<Expression>>,
    /// SELECT statement for INSERT INTO ... SELECT (None if using VALUES)
    pub select: Option<Box<SelectStatement>>,
    /// ON DUPLICATE KEY UPDATE (MySQL-style) or ON CONFLICT DO UPDATE (PostgreSQL-style)
    pub on_duplicate: bool,
    pub update_columns: Vec<Identifier>,
    pub update_expressions: Vec<Expression>,
    /// ON CONFLICT DO NOTHING (skip duplicates silently)
    pub do_nothing: bool,
    /// Conflict target columns for ON CONFLICT (col1, col2, ...)
    pub conflict_target: Vec<Identifier>,
    /// RETURNING clause expressions
    pub returning: Vec<Expression>,
}

impl fmt::Display for InsertStatement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut result = format!("INSERT INTO {}", self.table_name);
        if !self.columns.is_empty() {
            let cols: Vec<String> = self.columns.iter().map(|c| c.to_string()).collect();
            result.push_str(&format!(" ({})", cols.join(", ")));
        }
        if let Some(ref select) = self.select {
            // INSERT INTO ... SELECT
            result.push_str(&format!(" {}", select));
        } else {
            // INSERT INTO ... VALUES
            result.push_str(" VALUES ");
            let rows: Vec<String> = self
                .values
                .iter()
                .map(|row| {
                    let vals: Vec<String> = row.iter().map(|v| v.to_string()).collect();
                    format!("({})", vals.join(", "))
                })
                .collect();
            result.push_str(&rows.join(", "));
        }
        if self.do_nothing {
            result.push_str(" ON CONFLICT");
            if !self.conflict_target.is_empty() {
                let cols: Vec<String> =
                    self.conflict_target.iter().map(|c| c.to_string()).collect();
                result.push_str(&format!(" ({})", cols.join(", ")));
            }
            result.push_str(" DO NOTHING");
        } else if self.on_duplicate {
            if !self.conflict_target.is_empty() {
                let cols: Vec<String> =
                    self.conflict_target.iter().map(|c| c.to_string()).collect();
                result.push_str(&format!(
                    " ON CONFLICT ({}) DO UPDATE SET ",
                    cols.join(", ")
                ));
            } else {
                result.push_str(" ON DUPLICATE KEY UPDATE ");
            }
            let updates: Vec<String> = self
                .update_columns
                .iter()
                .zip(&self.update_expressions)
                .map(|(col, expr)| format!("{} = {}", col, expr))
                .collect();
            result.push_str(&updates.join(", "));
        }
        if !self.returning.is_empty() {
            let returning: Vec<String> = self.returning.iter().map(|e| e.to_string()).collect();
            result.push_str(&format!(" RETURNING {}", returning.join(", ")));
        }
        write!(f, "{}", result)
    }
}

/// UPDATE statement
#[derive(Debug, Clone, PartialEq)]
pub struct UpdateStatement {
    pub token: Token,
    pub table_name: Identifier,
    pub updates: FxHashMap<SmartString, Expression>,
    pub where_clause: Option<Box<Expression>>,
    /// RETURNING clause expressions
    pub returning: Vec<Expression>,
}

impl fmt::Display for UpdateStatement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut result = format!("UPDATE {} SET ", self.table_name);
        let updates: Vec<String> = self
            .updates
            .iter()
            .map(|(col, val)| format!("{} = {}", col, val))
            .collect();
        result.push_str(&updates.join(", "));
        if let Some(ref where_clause) = self.where_clause {
            result.push_str(&format!(" WHERE {}", where_clause));
        }
        if !self.returning.is_empty() {
            let returning: Vec<String> = self.returning.iter().map(|e| e.to_string()).collect();
            result.push_str(&format!(" RETURNING {}", returning.join(", ")));
        }
        write!(f, "{}", result)
    }
}

/// DELETE statement
#[derive(Debug, Clone, PartialEq)]
pub struct DeleteStatement {
    pub token: Token,
    pub table_name: Identifier,
    /// Optional table alias (e.g., DELETE FROM users AS u)
    pub alias: Option<Identifier>,
    pub where_clause: Option<Box<Expression>>,
    /// RETURNING clause expressions
    pub returning: Vec<Expression>,
}

impl fmt::Display for DeleteStatement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut result = format!("DELETE FROM {}", self.table_name);
        if let Some(ref alias) = self.alias {
            result.push_str(&format!(" AS {}", alias));
        }
        if let Some(ref where_clause) = self.where_clause {
            result.push_str(&format!(" WHERE {}", where_clause));
        }
        if !self.returning.is_empty() {
            let returning: Vec<String> = self.returning.iter().map(|e| e.to_string()).collect();
            result.push_str(&format!(" RETURNING {}", returning.join(", ")));
        }
        write!(f, "{}", result)
    }
}

/// CREATE TABLE statement
#[derive(Debug, Clone, PartialEq)]
pub struct CreateTableStatement {
    pub token: Token,
    pub table_name: Identifier,
    pub if_not_exists: bool,
    pub columns: Vec<ColumnDefinition>,
    /// Table-level constraints (UNIQUE(cols), CHECK(expr), etc.)
    pub table_constraints: Vec<TableConstraint>,
    /// Optional SELECT statement for CREATE TABLE ... AS SELECT
    pub as_select: Option<Box<SelectStatement>>,
}

impl fmt::Display for CreateTableStatement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut result = String::from("CREATE TABLE ");
        if self.if_not_exists {
            result.push_str("IF NOT EXISTS ");
        }
        if let Some(ref select) = self.as_select {
            result.push_str(&format!("{} AS {}", self.table_name, select));
            return write!(f, "{}", result);
        }
        result.push_str(&format!("{} (", self.table_name));
        let cols: Vec<String> = self.columns.iter().map(|c| c.to_string()).collect();
        result.push_str(&cols.join(", "));
        if !self.table_constraints.is_empty() {
            let constraints: Vec<String> = self
                .table_constraints
                .iter()
                .map(|c| c.to_string())
                .collect();
            result.push_str(", ");
            result.push_str(&constraints.join(", "));
        }
        result.push(')');
        write!(f, "{}", result)
    }
}

/// Column definition
#[derive(Debug, Clone, PartialEq)]
pub struct ColumnDefinition {
    pub name: Identifier,
    pub data_type: SmartString,
    pub constraints: Vec<ColumnConstraint>,
}

impl fmt::Display for ColumnDefinition {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut result = format!("{} {}", self.name, self.data_type);
        for constraint in &self.constraints {
            result.push_str(&format!(" {}", constraint));
        }
        write!(f, "{}", result)
    }
}

/// Column constraint
#[derive(Debug, Clone, PartialEq)]
pub enum ColumnConstraint {
    NotNull,
    PrimaryKey,
    Unique,
    AutoIncrement,
    Default(Expression),
    Check(Expression),
    References {
        table: Identifier,
        column: Option<Identifier>,
        on_delete: ForeignKeyAction,
        on_update: ForeignKeyAction,
    },
}

impl fmt::Display for ColumnConstraint {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ColumnConstraint::NotNull => write!(f, "NOT NULL"),
            ColumnConstraint::PrimaryKey => write!(f, "PRIMARY KEY"),
            ColumnConstraint::Unique => write!(f, "UNIQUE"),
            ColumnConstraint::AutoIncrement => write!(f, "AUTO_INCREMENT"),
            ColumnConstraint::Default(expr) => write!(f, "DEFAULT {}", expr),
            ColumnConstraint::Check(expr) => write!(f, "CHECK ({})", expr),
            ColumnConstraint::References {
                table,
                column,
                on_delete,
                on_update,
            } => {
                write!(f, "REFERENCES {}", table)?;
                if let Some(col) = column {
                    write!(f, "({})", col)?;
                }
                if *on_delete != ForeignKeyAction::Restrict {
                    write!(f, " ON DELETE {}", on_delete)?;
                }
                if *on_update != ForeignKeyAction::Restrict {
                    write!(f, " ON UPDATE {}", on_update)?;
                }
                Ok(())
            }
        }
    }
}

/// Table-level constraint (applied to the table rather than a single column)
#[derive(Debug, Clone, PartialEq)]
pub enum TableConstraint {
    /// UNIQUE(col1, col2, ...)
    Unique(Vec<Identifier>),
    /// CHECK(expression) - boxed to reduce enum size
    Check(Box<Expression>),
    /// PRIMARY KEY(col1, col2, ...) - composite primary key (not yet fully supported)
    PrimaryKey(Vec<Identifier>),
    /// FOREIGN KEY(col) REFERENCES table(col) ON DELETE ... ON UPDATE ...
    /// Boxed to reduce enum size (Identifier is large)
    ForeignKey(Box<ForeignKeyTableConstraint>),
}

/// Fields for a table-level FOREIGN KEY constraint (boxed to reduce TableConstraint enum size)
#[derive(Debug, Clone, PartialEq)]
pub struct ForeignKeyTableConstraint {
    pub column: Identifier,
    pub ref_table: Identifier,
    pub ref_column: Option<Identifier>,
    pub on_delete: ForeignKeyAction,
    pub on_update: ForeignKeyAction,
}

impl fmt::Display for TableConstraint {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            TableConstraint::Unique(cols) => {
                let col_names: Vec<&str> = cols.iter().map(|c| c.value.as_str()).collect();
                write!(f, "UNIQUE({})", col_names.join(", "))
            }
            TableConstraint::Check(expr) => write!(f, "CHECK({})", expr),
            TableConstraint::PrimaryKey(cols) => {
                let col_names: Vec<&str> = cols.iter().map(|c| c.value.as_str()).collect();
                write!(f, "PRIMARY KEY({})", col_names.join(", "))
            }
            TableConstraint::ForeignKey(fk) => {
                write!(f, "FOREIGN KEY({}) REFERENCES {}", fk.column, fk.ref_table)?;
                if let Some(ref col) = fk.ref_column {
                    write!(f, "({})", col)?;
                }
                if fk.on_delete != ForeignKeyAction::Restrict {
                    write!(f, " ON DELETE {}", fk.on_delete)?;
                }
                if fk.on_update != ForeignKeyAction::Restrict {
                    write!(f, " ON UPDATE {}", fk.on_update)?;
                }
                Ok(())
            }
        }
    }
}

/// Helper enum for parsing - either a column definition or a table constraint
#[derive(Debug, Clone, PartialEq)]
pub enum ColumnOrConstraint {
    Column(ColumnDefinition),
    Constraint(TableConstraint),
}

/// DROP TABLE statement
#[derive(Debug, Clone, PartialEq)]
pub struct DropTableStatement {
    pub token: Token,
    pub table_name: Identifier,
    pub if_exists: bool,
}

impl fmt::Display for DropTableStatement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut result = String::from("DROP TABLE ");
        if self.if_exists {
            result.push_str("IF EXISTS ");
        }
        result.push_str(&self.table_name.to_string());
        write!(f, "{}", result)
    }
}

/// TRUNCATE TABLE statement
#[derive(Debug, Clone, PartialEq)]
pub struct TruncateStatement {
    pub token: Token,
    pub table_name: Identifier,
}

impl fmt::Display for TruncateStatement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "TRUNCATE TABLE {}", self.table_name)
    }
}

/// VACUUM statement — triggers manual cleanup of deleted rows and index compaction
#[derive(Debug, Clone, PartialEq)]
pub struct VacuumStatement {
    pub token: Token,
    pub table_name: Option<Identifier>,
}

impl fmt::Display for VacuumStatement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(ref table_name) = self.table_name {
            write!(f, "VACUUM {}", table_name)
        } else {
            write!(f, "VACUUM")
        }
    }
}

/// ALTER TABLE operation type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AlterTableOperation {
    AddColumn,
    DropColumn,
    RenameColumn,
    ModifyColumn,
    RenameTable,
}

/// ALTER TABLE statement
#[derive(Debug, Clone, PartialEq)]
pub struct AlterTableStatement {
    pub token: Token,
    pub table_name: Identifier,
    pub operation: AlterTableOperation,
    pub column_def: Option<ColumnDefinition>,
    pub column_name: Option<Identifier>,
    pub new_column_name: Option<Identifier>,
    pub new_table_name: Option<Identifier>,
}

impl fmt::Display for AlterTableStatement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut result = format!("ALTER TABLE {} ", self.table_name);
        match self.operation {
            AlterTableOperation::AddColumn => {
                if let Some(ref col) = self.column_def {
                    result.push_str(&format!("ADD COLUMN {}", col));
                }
            }
            AlterTableOperation::DropColumn => {
                if let Some(ref name) = self.column_name {
                    result.push_str(&format!("DROP COLUMN {}", name));
                }
            }
            AlterTableOperation::RenameColumn => {
                if let (Some(ref old), Some(ref new)) = (&self.column_name, &self.new_column_name) {
                    result.push_str(&format!("RENAME COLUMN {} TO {}", old, new));
                }
            }
            AlterTableOperation::ModifyColumn => {
                if let Some(ref col) = self.column_def {
                    result.push_str(&format!("MODIFY COLUMN {}", col));
                }
            }
            AlterTableOperation::RenameTable => {
                if let Some(ref name) = self.new_table_name {
                    result.push_str(&format!("RENAME TO {}", name));
                }
            }
        }
        write!(f, "{}", result)
    }
}

/// Index type for USING clause
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IndexMethod {
    /// B-tree index (default for INTEGER, FLOAT, TIMESTAMP) - good for range queries
    BTree,
    /// Hash index (default for TEXT, JSON) - good for equality lookups
    Hash,
    /// Bitmap index (default for BOOLEAN) - good for low-cardinality columns
    Bitmap,
    /// HNSW index - approximate nearest neighbor search for vector columns
    Hnsw,
}

impl fmt::Display for IndexMethod {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            IndexMethod::BTree => write!(f, "BTREE"),
            IndexMethod::Hash => write!(f, "HASH"),
            IndexMethod::Bitmap => write!(f, "BITMAP"),
            IndexMethod::Hnsw => write!(f, "HNSW"),
        }
    }
}

/// CREATE INDEX statement
#[derive(Debug, Clone, PartialEq)]
pub struct CreateIndexStatement {
    pub token: Token,
    pub index_name: Identifier,
    pub table_name: Identifier,
    pub columns: Vec<Identifier>,
    pub is_unique: bool,
    pub if_not_exists: bool,
    /// Optional index type from USING clause (None = auto-select based on column type)
    pub index_method: Option<IndexMethod>,
    /// Optional WITH clause for index parameters (e.g., HNSW m, ef_construction, ef_search, metric)
    pub options: Vec<(String, String)>,
}

impl fmt::Display for CreateIndexStatement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut result = String::from("CREATE ");
        if self.is_unique {
            result.push_str("UNIQUE ");
        }
        result.push_str("INDEX ");
        if self.if_not_exists {
            result.push_str("IF NOT EXISTS ");
        }
        result.push_str(&format!("{} ON {} (", self.index_name, self.table_name));
        let cols: Vec<String> = self.columns.iter().map(|c| c.to_string()).collect();
        result.push_str(&cols.join(", "));
        result.push(')');
        if let Some(method) = &self.index_method {
            result.push_str(&format!(" USING {}", method));
        }
        if !self.options.is_empty() {
            result.push_str(" WITH (");
            for (i, (key, val)) in self.options.iter().enumerate() {
                if i > 0 {
                    result.push_str(", ");
                }
                result.push_str(&format!("{} = {}", key, val));
            }
            result.push(')');
        }
        write!(f, "{}", result)
    }
}

/// DROP INDEX statement
#[derive(Debug, Clone, PartialEq)]
pub struct DropIndexStatement {
    pub token: Token,
    pub index_name: Identifier,
    pub table_name: Option<Identifier>,
    pub if_exists: bool,
}

impl fmt::Display for DropIndexStatement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut result = String::from("DROP INDEX ");
        if self.if_exists {
            result.push_str("IF EXISTS ");
        }
        result.push_str(&self.index_name.to_string());
        if let Some(ref table) = self.table_name {
            result.push_str(&format!(" ON {}", table));
        }
        write!(f, "{}", result)
    }
}

/// CREATE VIEW statement
#[derive(Debug, Clone, PartialEq)]
pub struct CreateViewStatement {
    pub token: Token,
    pub view_name: Identifier,
    pub query: Box<SelectStatement>,
    pub if_not_exists: bool,
}

impl fmt::Display for CreateViewStatement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut result = String::from("CREATE VIEW ");
        if self.if_not_exists {
            result.push_str("IF NOT EXISTS ");
        }
        result.push_str(&format!("{} AS {}", self.view_name, self.query));
        write!(f, "{}", result)
    }
}

/// DROP VIEW statement
#[derive(Debug, Clone, PartialEq)]
pub struct DropViewStatement {
    pub token: Token,
    pub view_name: Identifier,
    pub if_exists: bool,
}

impl fmt::Display for DropViewStatement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut result = String::from("DROP VIEW ");
        if self.if_exists {
            result.push_str("IF EXISTS ");
        }
        result.push_str(&self.view_name.to_string());
        write!(f, "{}", result)
    }
}

/// BEGIN statement
#[derive(Debug, Clone, PartialEq)]
pub struct BeginStatement {
    pub token: Token,
    pub isolation_level: Option<SmartString>,
}

impl fmt::Display for BeginStatement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut result = String::from("BEGIN TRANSACTION");
        if let Some(ref level) = self.isolation_level {
            result.push_str(&format!(" ISOLATION LEVEL {}", level));
        }
        write!(f, "{}", result)
    }
}

/// COMMIT statement
#[derive(Debug, Clone, PartialEq)]
pub struct CommitStatement {
    pub token: Token,
}

impl fmt::Display for CommitStatement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "COMMIT")
    }
}

/// ROLLBACK statement
#[derive(Debug, Clone, PartialEq)]
pub struct RollbackStatement {
    pub token: Token,
    pub savepoint_name: Option<Identifier>,
}

impl fmt::Display for RollbackStatement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(ref name) = self.savepoint_name {
            write!(f, "ROLLBACK TO SAVEPOINT {}", name)
        } else {
            write!(f, "ROLLBACK")
        }
    }
}

/// SAVEPOINT statement
#[derive(Debug, Clone, PartialEq)]
pub struct SavepointStatement {
    pub token: Token,
    pub savepoint_name: Identifier,
}

impl fmt::Display for SavepointStatement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "SAVEPOINT {}", self.savepoint_name)
    }
}

/// RELEASE SAVEPOINT statement
#[derive(Debug, Clone, PartialEq)]
pub struct ReleaseSavepointStatement {
    pub token: Token,
    pub savepoint_name: Identifier,
}

impl fmt::Display for ReleaseSavepointStatement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "RELEASE SAVEPOINT {}", self.savepoint_name)
    }
}

/// SET statement
#[derive(Debug, Clone, PartialEq)]
pub struct SetStatement {
    pub token: Token,
    pub name: Identifier,
    pub value: Expression,
}

impl fmt::Display for SetStatement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "SET {} = {}", self.name, self.value)
    }
}

/// PRAGMA statement
#[derive(Debug, Clone, PartialEq)]
pub struct PragmaStatement {
    pub token: Token,
    pub name: Identifier,
    pub value: Option<Expression>,
}

impl fmt::Display for PragmaStatement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(ref value) = self.value {
            write!(f, "PRAGMA {} = {}", self.name, value)
        } else {
            write!(f, "PRAGMA {}", self.name)
        }
    }
}

/// SHOW TABLES statement
#[derive(Debug, Clone, PartialEq)]
pub struct ShowTablesStatement {
    pub token: Token,
}

impl fmt::Display for ShowTablesStatement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "SHOW TABLES")
    }
}

/// SHOW VIEWS statement
#[derive(Debug, Clone, PartialEq)]
pub struct ShowViewsStatement {
    pub token: Token,
}

impl fmt::Display for ShowViewsStatement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "SHOW VIEWS")
    }
}

/// SHOW CREATE TABLE statement
#[derive(Debug, Clone, PartialEq)]
pub struct ShowCreateTableStatement {
    pub token: Token,
    pub table_name: Identifier,
}

impl fmt::Display for ShowCreateTableStatement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "SHOW CREATE TABLE {}", self.table_name)
    }
}

/// SHOW CREATE VIEW statement
#[derive(Debug, Clone, PartialEq)]
pub struct ShowCreateViewStatement {
    pub token: Token,
    pub view_name: Identifier,
}

impl fmt::Display for ShowCreateViewStatement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "SHOW CREATE VIEW {}", self.view_name)
    }
}

/// SHOW INDEXES statement
#[derive(Debug, Clone, PartialEq)]
pub struct ShowIndexesStatement {
    pub token: Token,
    pub table_name: Identifier,
}

impl fmt::Display for ShowIndexesStatement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "SHOW INDEXES FROM {}", self.table_name)
    }
}

/// DESCRIBE statement - shows table structure
#[derive(Debug, Clone, PartialEq)]
pub struct DescribeStatement {
    pub token: Token,
    pub table_name: Identifier,
}

impl fmt::Display for DescribeStatement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "DESCRIBE {}", self.table_name)
    }
}

/// Expression statement (standalone expression)
#[derive(Debug, Clone, PartialEq)]
pub struct ExpressionStatement {
    pub token: Token,
    pub expression: Expression,
}

impl fmt::Display for ExpressionStatement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.expression)
    }
}

/// EXPLAIN statement
#[derive(Debug, Clone, PartialEq)]
pub struct ExplainStatement {
    pub token: Token,
    pub statement: Box<Statement>,
    pub analyze: bool,
}

impl fmt::Display for ExplainStatement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.analyze {
            write!(f, "EXPLAIN ANALYZE {}", self.statement)
        } else {
            write!(f, "EXPLAIN {}", self.statement)
        }
    }
}

/// ANALYZE statement for collecting table statistics
#[derive(Debug, Clone, PartialEq)]
pub struct AnalyzeStatement {
    pub token: Token,
    /// Table name to analyze (None = analyze all tables)
    pub table_name: Option<SmartString>,
}

impl fmt::Display for AnalyzeStatement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.table_name {
            Some(name) => write!(f, "ANALYZE {}", name),
            None => write!(f, "ANALYZE"),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parser::token::TokenType;

    fn make_token(tt: TokenType, literal: &str) -> Token {
        Token::new(tt, literal, Position::default())
    }

    #[test]
    fn test_identifier_display() {
        let id = Identifier::new(
            make_token(TokenType::Identifier, "users"),
            "users".to_string(),
        );
        assert_eq!(id.to_string(), "users");
    }

    #[test]
    fn test_qualified_identifier_display() {
        let qi = QualifiedIdentifier {
            token: make_token(TokenType::Identifier, "users"),
            qualifier: Box::new(Identifier::new(
                make_token(TokenType::Identifier, "users"),
                "users".to_string(),
            )),
            name: Box::new(Identifier::new(
                make_token(TokenType::Identifier, "id"),
                "id".to_string(),
            )),
        };
        assert_eq!(qi.to_string(), "users.id");
    }

    #[test]
    fn test_integer_literal_display() {
        let lit = IntegerLiteral {
            token: make_token(TokenType::Integer, "42"),
            value: 42,
        };
        assert_eq!(lit.to_string(), "42");
    }

    #[test]
    fn test_string_literal_display() {
        let lit = StringLiteral {
            token: make_token(TokenType::String, "'hello'"),
            value: "hello".into(),
            type_hint: None,
        };
        assert_eq!(lit.to_string(), "'hello'");
    }

    #[test]
    fn test_infix_expression_display() {
        let expr = InfixExpression::new(
            make_token(TokenType::Operator, "+"),
            Box::new(Expression::IntegerLiteral(IntegerLiteral {
                token: make_token(TokenType::Integer, "1"),
                value: 1,
            })),
            "+",
            Box::new(Expression::IntegerLiteral(IntegerLiteral {
                token: make_token(TokenType::Integer, "2"),
                value: 2,
            })),
        );
        assert_eq!(expr.to_string(), "(1 + 2)");
    }

    #[test]
    fn test_function_call_display() {
        let fc = FunctionCall {
            token: make_token(TokenType::Identifier, "COUNT"),
            function: "COUNT".into(),
            arguments: vec![Expression::Star(StarExpression {
                token: make_token(TokenType::Operator, "*"),
            })],
            is_distinct: false,
            order_by: vec![],
            filter: None,
        };
        assert_eq!(fc.to_string(), "COUNT(*)");
    }

    #[test]
    fn test_select_statement_display() {
        let stmt = SelectStatement {
            token: make_token(TokenType::Keyword, "SELECT"),
            distinct: false,
            distinct_on: vec![],
            columns: vec![Expression::Star(StarExpression {
                token: make_token(TokenType::Operator, "*"),
            })],
            with: None,
            table_expr: Some(Box::new(Expression::TableSource(Box::new(
                SimpleTableSource {
                    token: make_token(TokenType::Identifier, "users"),
                    name: Identifier::new(make_token(TokenType::Identifier, "users"), "users"),
                    alias: None,
                    as_of: None,
                },
            )))),
            where_clause: None,
            group_by: GroupByClause::default(),
            having: None,
            window_defs: vec![],
            order_by: vec![],
            limit: None,
            offset: None,
            set_operations: vec![],
        };
        assert_eq!(stmt.to_string(), "SELECT * FROM users");
    }

    #[test]
    fn test_create_table_display() {
        let stmt = CreateTableStatement {
            token: make_token(TokenType::Keyword, "CREATE"),
            table_name: Identifier::new(make_token(TokenType::Identifier, "users"), "users"),
            if_not_exists: true,
            columns: vec![
                ColumnDefinition {
                    name: Identifier::new(make_token(TokenType::Identifier, "id"), "id"),
                    data_type: "INTEGER".into(),
                    constraints: vec![ColumnConstraint::PrimaryKey],
                },
                ColumnDefinition {
                    name: Identifier::new(make_token(TokenType::Identifier, "name"), "name"),
                    data_type: "TEXT".into(),
                    constraints: vec![ColumnConstraint::NotNull],
                },
            ],
            table_constraints: vec![],
            as_select: None,
        };
        assert_eq!(
            stmt.to_string(),
            "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT NOT NULL)"
        );
    }

    #[test]
    fn test_insert_statement_display() {
        let stmt = InsertStatement {
            token: make_token(TokenType::Keyword, "INSERT"),
            table_name: Identifier::new(make_token(TokenType::Identifier, "users"), "users"),
            columns: vec![
                Identifier::new(make_token(TokenType::Identifier, "id"), "id"),
                Identifier::new(make_token(TokenType::Identifier, "name"), "name"),
            ],
            values: vec![vec![
                Expression::IntegerLiteral(IntegerLiteral {
                    token: make_token(TokenType::Integer, "1"),
                    value: 1,
                }),
                Expression::StringLiteral(StringLiteral {
                    token: make_token(TokenType::String, "'Alice'"),
                    value: "Alice".into(),
                    type_hint: None,
                }),
            ]],
            select: None,
            on_duplicate: false,
            update_columns: vec![],
            update_expressions: vec![],
            do_nothing: false,
            conflict_target: vec![],
            returning: vec![],
        };
        assert_eq!(
            stmt.to_string(),
            "INSERT INTO users (id, name) VALUES (1, 'Alice')"
        );
    }

    #[test]
    fn test_case_expression_display() {
        let case_expr = CaseExpression {
            token: make_token(TokenType::Keyword, "CASE"),
            value: None,
            when_clauses: vec![WhenClause {
                token: make_token(TokenType::Keyword, "WHEN"),
                condition: Expression::BooleanLiteral(BooleanLiteral {
                    token: make_token(TokenType::Keyword, "TRUE"),
                    value: true,
                }),
                then_result: Expression::IntegerLiteral(IntegerLiteral {
                    token: make_token(TokenType::Integer, "1"),
                    value: 1,
                }),
            }],
            else_value: Some(Box::new(Expression::IntegerLiteral(IntegerLiteral {
                token: make_token(TokenType::Integer, "0"),
                value: 0,
            }))),
        };
        assert_eq!(case_expr.to_string(), "CASE WHEN TRUE THEN 1 ELSE 0 END");
    }
}