polyglot-sql 0.3.3

SQL parsing, validating, formatting, and dialect translation library
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
//! Identity Roundtrip Tests
//!
//! Ported from sqlglot's tests/fixtures/identity.sql
//! These tests verify that SQL can be parsed and regenerated identically.

use polyglot_sql::generator::Generator;
use polyglot_sql::parser::Parser;

/// Helper function to test roundtrip: parse SQL, generate, and verify
fn roundtrip(sql: &str) -> String {
    let ast = Parser::parse_sql(sql).expect(&format!("Failed to parse: {}", sql));
    Generator::sql(&ast[0]).expect("Failed to generate SQL")
}

/// Assert that SQL roundtrips with potential normalization
fn assert_roundtrip(sql: &str) {
    let result = roundtrip(sql);
    // Re-parse the generated SQL to verify it's valid
    let ast2 = Parser::parse_sql(&result).expect(&format!("Failed to re-parse: {}", result));
    // Generate again and compare
    let result2 = Generator::sql(&ast2[0]).expect("Failed to generate SQL again");
    assert_eq!(
        result, result2,
        "Roundtrip not stable for: {}\nFirst: {}\nSecond: {}",
        sql, result, result2
    );
}

// ============================================================================
// LITERALS - Lines 1-37 from identity.sql
// ============================================================================

#[cfg(test)]
mod literal_tests {
    use super::*;

    #[test]
    fn test_numeric_literals() {
        // Integer literals
        assert_roundtrip("SELECT 1");
        assert_roundtrip("SELECT (1)");

        // Decimal literals
        assert_roundtrip("SELECT 1.0");
        assert_roundtrip("SELECT (1.0)");
        assert_roundtrip("SELECT 0.2");
    }

    #[test]
    fn test_scientific_notation() {
        assert_roundtrip("SELECT 1E2");
        assert_roundtrip("SELECT 1E+2");
        assert_roundtrip("SELECT 1E-2");
        assert_roundtrip("SELECT 1.1E10");
        assert_roundtrip("SELECT 1.12e-10");
    }

    #[test]
    fn test_negative_numbers() {
        assert_roundtrip("SELECT 1 - -1");
    }

    #[test]
    fn test_negative_scientific_notation() {
        assert_roundtrip("SELECT -11.023E7 * 3");
        assert_roundtrip("SELECT - -5");
    }

    #[test]
    fn test_string_literals() {
        assert_roundtrip("SELECT ''");
        assert_roundtrip("SELECT 'x'");
        assert_roundtrip("SELECT ''''");
    }

    #[test]
    fn test_national_string_literals() {
        assert_roundtrip("SELECT N'abc'");
    }

    #[test]
    fn test_hex_string_literals() {
        assert_roundtrip("SELECT X'ABCD'");
    }

    #[test]
    fn test_bit_string_literals() {
        assert_roundtrip("SELECT B'01010'");
    }

    #[test]
    fn test_boolean_literals() {
        assert_roundtrip("SELECT TRUE");
        assert_roundtrip("SELECT FALSE");
        assert_roundtrip("SELECT ((TRUE))");
    }

    #[test]
    fn test_null_literal() {
        assert_roundtrip("SELECT NULL");
    }
}

// ============================================================================
// ARITHMETIC & OPERATORS - Lines 38-60 from identity.sql
// ============================================================================

#[cfg(test)]
mod operator_tests {
    use super::*;

    #[test]
    fn test_comparison_operators() {
        assert_roundtrip("SELECT x < 1");
        assert_roundtrip("SELECT x <= 1");
        assert_roundtrip("SELECT x > 1");
        assert_roundtrip("SELECT x >= 1");
        assert_roundtrip("SELECT x <> 1");
        assert_roundtrip("SELECT x = 1");
    }

    #[test]
    fn test_arithmetic_operators() {
        assert_roundtrip("SELECT x % 1");
        assert_roundtrip("SELECT 1 + 2");
        assert_roundtrip("SELECT 1 - 2");
        assert_roundtrip("SELECT 1 * 2");
        assert_roundtrip("SELECT 1 / 2");
        assert_roundtrip("SELECT (1 * 2) / (3 - 5)");
    }

    #[test]
    fn test_logical_operators() {
        assert_roundtrip("SELECT x = y OR x > 1");
        assert_roundtrip("SELECT x = 1 AND y = 2");
        assert_roundtrip("SELECT NOT x");
        assert_roundtrip("SELECT NOT 1");
        assert_roundtrip("SELECT NOT NOT 1");
    }

    #[test]
    fn test_string_concatenation() {
        assert_roundtrip("SELECT x || y");
    }

    // TODO: Bitwise operators not fully implemented
    // #[test]
    // fn test_bitwise_operators() {
    //     assert_roundtrip("SELECT x & 1");
    //     assert_roundtrip("SELECT x | 1");
    //     assert_roundtrip("SELECT x ^ 1");
    //     assert_roundtrip("SELECT ~x");
    //     assert_roundtrip("SELECT x << 1");
    //     assert_roundtrip("SELECT x >> 1");
    // }
}

// ============================================================================
// COLUMN ACCESS - Lines 61-85 from identity.sql
// ============================================================================

#[cfg(test)]
mod column_access_tests {
    use super::*;

    #[test]
    fn test_simple_column() {
        assert_roundtrip("SELECT x");
        assert_roundtrip("SELECT a.b");
    }

    #[test]
    fn test_qualified_columns() {
        assert_roundtrip("SELECT a.b");
    }

    #[test]
    fn test_multi_level_column_access() {
        assert_roundtrip("SELECT a.b.c");
        assert_roundtrip("SELECT a.b.c.d");
        assert_roundtrip("SELECT a.b.c.d.e");
    }

    #[test]
    fn test_subscript_access() {
        assert_roundtrip("SELECT a[0]");
        assert_roundtrip("SELECT x[1]");
    }

    #[test]
    fn test_reserved_word_columns() {
        assert_roundtrip("SELECT time");
        assert_roundtrip("SELECT zone");
        assert_roundtrip("SELECT time * 100");
    }
}

// ============================================================================
// PREDICATES - Lines 86-100 from identity.sql
// ============================================================================

#[cfg(test)]
mod predicate_tests {
    use super::*;

    #[test]
    fn test_in_expression() {
        assert_roundtrip("SELECT x IN (1, 2, 3)");
        assert_roundtrip("SELECT x IN (-1, 1)");
        assert_roundtrip("SELECT x IN ('a', 'b')");
        assert_roundtrip("SELECT x IN ((1))");
    }

    #[test]
    fn test_between() {
        assert_roundtrip("SELECT x BETWEEN -1 AND 1");
        assert_roundtrip("SELECT x BETWEEN 1 AND 10");
    }

    #[test]
    fn test_is_null() {
        assert_roundtrip("SELECT NOT x IS NULL");
        assert_roundtrip("SELECT x IS NULL");
    }

    #[test]
    fn test_is_true_false() {
        assert_roundtrip("SELECT x IS TRUE");
        assert_roundtrip("SELECT x IS FALSE");
    }

    #[test]
    fn test_like() {
        assert_roundtrip("SELECT x LIKE y");
        assert_roundtrip("SELECT x LIKE '%y%'");
        assert_roundtrip("SELECT x ILIKE '%y%'");
    }

    #[test]
    fn test_glob() {
        assert_roundtrip("SELECT x GLOB '??-*'");
        assert_roundtrip("SELECT x GLOB y");
    }
}

// ============================================================================
// FUNCTIONS - Lines 97-200 from identity.sql
// ============================================================================

#[cfg(test)]
mod function_tests {
    use super::*;

    #[test]
    fn test_aggregate_functions() {
        assert_roundtrip("SELECT SUM(1)");
        assert_roundtrip("SELECT COUNT(a)");
        assert_roundtrip("SELECT COUNT(*)");
        assert_roundtrip("SELECT COUNT(DISTINCT a)");
        assert_roundtrip("SELECT AVG(a)");
        assert_roundtrip("SELECT MIN(a)");
        assert_roundtrip("SELECT MAX(a)");
    }

    #[test]
    fn test_math_functions() {
        assert_roundtrip("SELECT ABS(a)");
        assert_roundtrip("SELECT CEIL(a)");
        assert_roundtrip("SELECT FLOOR(a)");
        assert_roundtrip("SELECT ROUND(a)");
        assert_roundtrip("SELECT ROUND(a, 2)");
        assert_roundtrip("SELECT SQRT(a)");
        assert_roundtrip("SELECT POWER(a, 2)");
        assert_roundtrip("SELECT EXP(a)");
        assert_roundtrip("SELECT LN(a)");
    }

    #[test]
    fn test_string_functions() {
        assert_roundtrip("SELECT TRIM('a')");
    }

    #[test]
    fn test_replace_function() {
        assert_roundtrip("SELECT REPLACE('new york', ' ', '_')");
    }

    #[test]
    fn test_concat_ws() {
        assert_roundtrip("SELECT CONCAT_WS('-', 'a', 'b')");
    }

    #[test]
    fn test_null_handling_functions() {
        assert_roundtrip("SELECT COALESCE(a, b, c)");
        assert_roundtrip("SELECT GREATEST(a, b, c)");
        assert_roundtrip("SELECT LEAST(a, b, c)");
    }

    #[test]
    fn test_date_functions() {
        assert_roundtrip("SELECT CURRENT_DATE");
        assert_roundtrip("SELECT CURRENT_TIMESTAMP");
    }

    #[test]
    fn test_extract_function() {
        assert_roundtrip("SELECT EXTRACT(YEAR FROM y)");
        assert_roundtrip("SELECT EXTRACT(MONTH FROM y)");
        assert_roundtrip("SELECT EXTRACT(DAY FROM d)");
        assert_roundtrip("SELECT EXTRACT(HOUR FROM ts)");
    }

    #[test]
    fn test_cast() {
        assert_roundtrip("SELECT CAST(a AS INT)");
        assert_roundtrip("SELECT CAST(a AS VARCHAR)");
    }

    #[test]
    fn test_try_cast() {
        assert_roundtrip("SELECT TRY_CAST(a AS INT)");
    }

    #[test]
    fn test_cast_decimal_precision() {
        assert_roundtrip("SELECT CAST(a AS DECIMAL(5, 3))");
    }

    #[test]
    fn test_array_functions() {
        assert_roundtrip("SELECT ARRAY(1, 2)");
        assert_roundtrip("SELECT ARRAY_CONTAINS(x, 1)");
    }

    #[test]
    fn test_json_functions() {
        assert_roundtrip("SELECT JSON_EXTRACT(x, '$.name')");
        assert_roundtrip("SELECT JSON_EXTRACT_SCALAR(x, '$.name')");
    }

    #[test]
    fn test_window_value_functions() {
        assert_roundtrip("SELECT FIRST_VALUE(a)");
        assert_roundtrip("SELECT LAST_VALUE(a)");
        assert_roundtrip("SELECT LAG(x) OVER (ORDER BY y)");
        assert_roundtrip("SELECT LEAD(a) OVER (ORDER BY b)");
    }
}

// ============================================================================
// CASE EXPRESSIONS
// ============================================================================

#[cfg(test)]
mod case_tests {
    use super::*;

    #[test]
    fn test_simple_case() {
        assert_roundtrip("SELECT CASE WHEN TRUE THEN 1 ELSE 0 END");
        assert_roundtrip("SELECT CASE WHEN a < b THEN 1 WHEN a < c THEN 2 ELSE 3 END");
    }

    #[test]
    fn test_searched_case() {
        assert_roundtrip("SELECT CASE 1 WHEN 1 THEN 1 ELSE 2 END");
        assert_roundtrip("SELECT CASE x WHEN 1 THEN 'one' WHEN 2 THEN 'two' ELSE 'other' END");
    }

    #[test]
    fn test_case_with_expressions() {
        assert_roundtrip("SELECT CASE WHEN (x > 1) THEN 1 ELSE 0 END");
        assert_roundtrip("SELECT CASE (1) WHEN 1 THEN 1 ELSE 0 END");
    }
}

// ============================================================================
// SELECT STATEMENTS - Lines 214-400 from identity.sql
// ============================================================================

#[cfg(test)]
mod select_tests {
    use super::*;

    #[test]
    fn test_basic_select() {
        assert_roundtrip("SELECT 1");
        assert_roundtrip("SELECT 1 FROM test");
        assert_roundtrip("SELECT * FROM test");
        assert_roundtrip("SELECT a FROM test");
        assert_roundtrip("SELECT a, b FROM test");
    }

    #[test]
    fn test_select_alias() {
        assert_roundtrip("SELECT a AS b FROM test");
        assert_roundtrip("SELECT 1 AS x FROM test");
        assert_roundtrip("SELECT 1 + 2 AS x FROM test");
    }

    #[test]
    fn test_select_distinct() {
        assert_roundtrip("SELECT DISTINCT x FROM test");
        assert_roundtrip("SELECT DISTINCT x, y FROM test");
    }

    #[test]
    fn test_select_where() {
        assert_roundtrip("SELECT a FROM test WHERE a = 1");
        assert_roundtrip("SELECT a FROM test WHERE a = 1 AND b = 2");
        assert_roundtrip("SELECT a FROM test WHERE NOT FALSE");
        assert_roundtrip("SELECT a FROM test WHERE (a > 1)");
    }

    #[test]
    fn test_select_order_by() {
        assert_roundtrip("SELECT a FROM test ORDER BY a");
        assert_roundtrip("SELECT a FROM test ORDER BY a, b");
        assert_roundtrip("SELECT x FROM tests ORDER BY a DESC, b DESC, c");
        assert_roundtrip("SELECT a FROM test ORDER BY a ASC");
    }

    #[test]
    fn test_select_group_by() {
        assert_roundtrip("SELECT a, b FROM test GROUP BY 1");
        assert_roundtrip("SELECT a, b FROM test GROUP BY a");
        assert_roundtrip("SELECT a, b FROM test WHERE a = 1 GROUP BY a HAVING a = 2");
    }

    #[test]
    fn test_select_limit() {
        assert_roundtrip("SELECT * FROM test LIMIT 100");
        assert_roundtrip("SELECT * FROM test LIMIT 100 OFFSET 200");
    }

    #[test]
    fn test_subquery_in_from() {
        assert_roundtrip("SELECT a FROM (SELECT a FROM test) AS x");
        assert_roundtrip("SELECT * FROM (SELECT 1) AS x");
    }

    #[test]
    fn test_subquery_in_where() {
        assert_roundtrip("SELECT a FROM test WHERE EXISTS(SELECT 1)");
    }

    #[test]
    fn test_in_subquery() {
        assert_roundtrip("SELECT a FROM test WHERE a IN (SELECT b FROM z)");
    }

    #[test]
    fn test_all_subquery() {
        assert_roundtrip("SELECT a FROM test WHERE a > ALL (SELECT 1)");
    }

    #[test]
    fn test_qualified_star() {
        assert_roundtrip("SELECT test.* FROM test");
        assert_roundtrip("SELECT a.*, b.* FROM a, b");
    }
}

// ============================================================================
// JOINS - Lines 404-430 from identity.sql
// ============================================================================

#[cfg(test)]
mod join_tests {
    use super::*;

    #[test]
    fn test_inner_join() {
        assert_roundtrip("SELECT 1 FROM a JOIN b ON a.x = b.x");
        assert_roundtrip("SELECT 1 FROM a INNER JOIN b ON a.x = b.x");
    }

    #[test]
    fn test_outer_joins() {
        assert_roundtrip("SELECT 1 FROM a LEFT JOIN b ON a.x = b.x");
        assert_roundtrip("SELECT 1 FROM a RIGHT JOIN b ON a.x = b.x");
        assert_roundtrip("SELECT 1 FROM a FULL JOIN b ON a.x = b.x");
    }

    #[test]
    fn test_cross_join() {
        assert_roundtrip("SELECT 1 FROM a CROSS JOIN b");
    }

    #[test]
    fn test_natural_join() {
        assert_roundtrip("SELECT 1 FROM a NATURAL JOIN b");
        assert_roundtrip("SELECT 1 FROM a NATURAL LEFT JOIN b");
    }

    #[test]
    fn test_join_using() {
        assert_roundtrip("SELECT 1 FROM a JOIN b USING (x)");
        assert_roundtrip("SELECT 1 FROM a JOIN b USING (x, y, z)");
    }

    #[test]
    fn test_semi_anti_joins() {
        assert_roundtrip("SELECT 1 FROM a SEMI JOIN b ON a.x = b.x");
        assert_roundtrip("SELECT 1 FROM a LEFT SEMI JOIN b ON a.x = b.x");
        assert_roundtrip("SELECT 1 FROM a LEFT ANTI JOIN b ON a.x = b.x");
    }

    #[test]
    fn test_multiple_joins() {
        assert_roundtrip("SELECT 1 FROM a JOIN b ON a.foo = b.bar JOIN c ON a.foo = c.bar");
    }
}

// ============================================================================
// SET OPERATIONS - Lines 418-436 from identity.sql
// ============================================================================

#[cfg(test)]
mod set_operation_tests {
    use super::*;

    #[test]
    fn test_union() {
        assert_roundtrip("SELECT 1 FROM a UNION SELECT 2 FROM b");
        assert_roundtrip("SELECT 1 FROM a UNION ALL SELECT 2 FROM b");
        assert_roundtrip("SELECT 1 UNION ALL SELECT 2");
    }

    #[test]
    fn test_except() {
        assert_roundtrip("SELECT 1 EXCEPT SELECT 2");
    }

    #[test]
    fn test_intersect() {
        assert_roundtrip("SELECT 1 INTERSECT SELECT 2");
    }

    #[test]
    fn test_parenthesized_set_ops() {
        assert_roundtrip("(SELECT 1) UNION (SELECT 2)");
        assert_roundtrip("(SELECT 1) UNION SELECT 2");
    }
}

// ============================================================================
// CTEs (Common Table Expressions) - Lines 469-493 from identity.sql
// ============================================================================

#[cfg(test)]
mod cte_tests {
    use super::*;

    #[test]
    fn test_basic_cte() {
        assert_roundtrip("WITH a AS (SELECT 1) SELECT * FROM a");
        assert_roundtrip(
            "WITH a AS (SELECT 1), b AS (SELECT 2) SELECT a.*, b.* FROM a CROSS JOIN b",
        );
    }

    #[test]
    fn test_cte_with_set_ops() {
        assert_roundtrip("WITH a AS (SELECT 1) SELECT 1 UNION ALL SELECT 2");
        assert_roundtrip("WITH a AS (SELECT 1) SELECT 1 UNION SELECT 2");
    }

    #[test]
    fn test_recursive_cte() {
        assert_roundtrip("WITH RECURSIVE T(n) AS (VALUES (1) UNION ALL SELECT n + 1 FROM t WHERE n < 100) SELECT SUM(n) FROM t");
    }

    #[test]
    fn test_nested_cte() {
        assert_roundtrip(
            "WITH a AS (WITH b AS (SELECT 1 AS x) SELECT b.x FROM b) SELECT a.x FROM a",
        );
    }
}

// ============================================================================
// WINDOW FUNCTIONS - Lines 494-524 from identity.sql
// ============================================================================

#[cfg(test)]
mod window_tests {
    use super::*;

    #[test]
    fn test_basic_window() {
        assert_roundtrip("SELECT RANK() OVER () FROM x");
        assert_roundtrip("SELECT RANK() OVER () AS y FROM x");
    }

    #[test]
    fn test_window_partition() {
        assert_roundtrip("SELECT RANK() OVER (PARTITION BY a) FROM x");
        assert_roundtrip("SELECT RANK() OVER (PARTITION BY a, b) FROM x");
    }

    #[test]
    fn test_window_order() {
        assert_roundtrip("SELECT RANK() OVER (ORDER BY a) FROM x");
        assert_roundtrip("SELECT RANK() OVER (ORDER BY a, b) FROM x");
        assert_roundtrip("SELECT RANK() OVER (PARTITION BY a ORDER BY a) FROM x");
    }

    #[test]
    fn test_window_frame() {
        assert_roundtrip(
            "SELECT SUM(x) OVER (PARTITION BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)",
        );
        assert_roundtrip("SELECT SUM(x) OVER (PARTITION BY a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)");
        assert_roundtrip(
            "SELECT SUM(x) OVER (PARTITION BY a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)",
        );
    }

    #[test]
    fn test_window_range_frame() {
        assert_roundtrip(
            "SELECT SUM(x) OVER (PARTITION BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)",
        );
    }

    #[test]
    fn test_aggregate_with_window() {
        assert_roundtrip("SELECT SUM(x) OVER (PARTITION BY a) AS y FROM x");
        assert_roundtrip("SELECT COUNT(DISTINCT a) OVER (PARTITION BY c ORDER BY d ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)");
    }

    #[test]
    fn test_window_filter() {
        assert_roundtrip("SELECT SUM(x) FILTER(WHERE x > 1)");
        assert_roundtrip("SELECT SUM(x) FILTER(WHERE x > 1) OVER (ORDER BY y)");
    }
}

// ============================================================================
// DDL - CREATE TABLE - Lines 545-605 from identity.sql
// ============================================================================

#[cfg(test)]
mod ddl_create_table_tests {
    use super::*;

    #[test]
    fn test_create_table_as_select() {
        assert_roundtrip("CREATE TABLE foo AS SELECT 1");
        assert_roundtrip("CREATE TABLE a.b AS SELECT 1");
        assert_roundtrip("CREATE TABLE IF NOT EXISTS x AS SELECT a FROM d");
    }

    #[test]
    fn test_create_table_columns() {
        assert_roundtrip("CREATE TABLE z (a INT, b VARCHAR, c VARCHAR(100), d DECIMAL(5, 3))");
        assert_roundtrip("CREATE TABLE z (a INT, PRIMARY KEY (a))");
    }

    #[test]
    fn test_create_table_constraints() {
        assert_roundtrip("CREATE TABLE z (a INT UNIQUE)");
        assert_roundtrip("CREATE TABLE z (a INT NOT NULL)");
        assert_roundtrip("CREATE TABLE z (a INT PRIMARY KEY)");
    }

    #[test]
    fn test_create_table_default() {
        assert_roundtrip("CREATE TABLE z (n INT DEFAULT 0 NOT NULL)");
    }

    #[test]
    fn test_create_table_references() {
        assert_roundtrip("CREATE TABLE z (a INT REFERENCES parent (b, c))");
        assert_roundtrip("CREATE TABLE z (a INT, FOREIGN KEY (a) REFERENCES parent (b, c))");
    }

    #[test]
    fn test_create_temporary_table() {
        assert_roundtrip("CREATE TEMPORARY TABLE x AS SELECT a FROM d");
        assert_roundtrip("CREATE TEMPORARY TABLE IF NOT EXISTS x AS SELECT a FROM d");
    }
}

// ============================================================================
// DDL - CREATE VIEW - Lines 611-623 from identity.sql
// ============================================================================

#[cfg(test)]
mod ddl_create_view_tests {
    use super::*;

    #[test]
    fn test_create_view() {
        assert_roundtrip("CREATE VIEW x AS SELECT a FROM b");
        assert_roundtrip("CREATE VIEW IF NOT EXISTS x AS SELECT a FROM b");
    }

    #[test]
    fn test_create_or_replace_view() {
        assert_roundtrip("CREATE OR REPLACE VIEW x AS SELECT *");
    }

    #[test]
    fn test_create_materialized_view() {
        assert_roundtrip("CREATE MATERIALIZED VIEW x.y.z AS SELECT a FROM b");
    }

    #[test]
    fn test_create_temporary_view() {
        assert_roundtrip("CREATE TEMPORARY VIEW x AS SELECT a FROM d");
    }
}

// ============================================================================
// DDL - CREATE INDEX, SCHEMA, FUNCTION - Lines 636-647 from identity.sql
// ============================================================================

#[cfg(test)]
mod ddl_other_tests {
    use super::*;

    #[test]
    fn test_create_index() {
        assert_roundtrip("CREATE INDEX abc ON t(a)");
        assert_roundtrip("CREATE INDEX abc ON t(a, b, b)");
        assert_roundtrip("CREATE UNIQUE INDEX abc ON t(a, b, b)");
    }

    #[test]
    fn test_create_schema() {
        assert_roundtrip("CREATE SCHEMA x");
        assert_roundtrip("CREATE SCHEMA IF NOT EXISTS y");
    }

    #[test]
    fn test_create_database() {
        assert_roundtrip("CREATE DATABASE x");
        assert_roundtrip("CREATE DATABASE IF NOT EXISTS y");
    }

    #[test]
    fn test_create_function() {
        assert_roundtrip("CREATE FUNCTION f");
        assert_roundtrip("CREATE FUNCTION f AS 'g'");
        assert_roundtrip("CREATE TEMPORARY FUNCTION f");
    }
}

// ============================================================================
// DDL - DROP STATEMENTS - Lines 653-687 from identity.sql
// ============================================================================

#[cfg(test)]
mod ddl_drop_tests {
    use super::*;

    #[test]
    fn test_drop_table() {
        assert_roundtrip("DROP TABLE a");
        assert_roundtrip("DROP TABLE a.b");
        assert_roundtrip("DROP TABLE IF EXISTS a");
        assert_roundtrip("DROP TABLE IF EXISTS a.b");
        assert_roundtrip("DROP TABLE a CASCADE");
    }

    #[test]
    fn test_drop_view() {
        assert_roundtrip("DROP VIEW a");
        assert_roundtrip("DROP VIEW a.b");
        assert_roundtrip("DROP VIEW IF EXISTS a");
        assert_roundtrip("DROP MATERIALIZED VIEW x.y.z");
    }

    #[test]
    fn test_drop_index() {
        assert_roundtrip("DROP INDEX a.b.c");
    }

    #[test]
    fn test_drop_function() {
        assert_roundtrip("DROP FUNCTION a.b.c (INT)");
    }
}

// ============================================================================
// DML - INSERT, UPDATE, DELETE - Lines 666-722 from identity.sql
// ============================================================================

#[cfg(test)]
mod dml_tests {
    use super::*;

    #[test]
    fn test_insert_values() {
        assert_roundtrip("INSERT INTO x VALUES (1, 'a', 2.0)");
        assert_roundtrip("INSERT INTO x VALUES (1, 'a', 2.0), (1, 'a', 3.0)");
    }

    #[test]
    fn test_insert_select() {
        assert_roundtrip("INSERT INTO x SELECT * FROM y");
        assert_roundtrip("INSERT INTO y (a, b, c) SELECT a, b, c FROM x");
    }

    #[test]
    fn test_insert_with_cte() {
        assert_roundtrip("WITH a AS (SELECT 1) INSERT INTO b SELECT * FROM a");
    }

    #[test]
    fn test_update() {
        assert_roundtrip("UPDATE tbl_name SET foo = 123");
        assert_roundtrip("UPDATE tbl_name SET foo = 123, bar = 345");
    }

    #[test]
    fn test_update_qualified_where() {
        assert_roundtrip("UPDATE db.tbl_name SET foo = 123 WHERE tbl_name.bar = 234");
    }

    #[test]
    fn test_delete() {
        assert_roundtrip("DELETE FROM x WHERE y > 1");
        assert_roundtrip("DELETE FROM y");
    }
}

// ============================================================================
// ALTER TABLE - Lines 763-806 from identity.sql
// ============================================================================

#[cfg(test)]
mod alter_table_tests {
    use super::*;

    #[test]
    fn test_alter_add_column() {
        assert_roundtrip("ALTER TABLE integers ADD COLUMN k INT");
    }

    #[test]
    fn test_alter_add_column_if_exists() {
        assert_roundtrip("ALTER TABLE integers ADD COLUMN IF NOT EXISTS k INT");
        assert_roundtrip("ALTER TABLE IF EXISTS integers ADD COLUMN k INT");
    }

    #[test]
    fn test_alter_drop_column() {
        assert_roundtrip("ALTER TABLE integers DROP COLUMN k");
    }

    #[test]
    fn test_alter_drop_column_advanced() {
        assert_roundtrip("ALTER TABLE integers DROP COLUMN IF EXISTS k");
        assert_roundtrip("ALTER TABLE integers DROP COLUMN k CASCADE");
    }

    #[test]
    fn test_alter_rename() {
        assert_roundtrip("ALTER TABLE table1 RENAME COLUMN c1 TO c2");
        assert_roundtrip("ALTER TABLE table1 RENAME TO table2");
    }

    #[test]
    fn test_alter_add_constraint() {
        assert_roundtrip(
            "ALTER TABLE persons ADD CONSTRAINT persons_pk PRIMARY KEY (first_name, last_name)",
        );
    }
}

// ============================================================================
// PARAMETERS & PLACEHOLDERS - Lines 733-736 from identity.sql
// ============================================================================

#[cfg(test)]
mod parameter_tests {
    use super::*;

    #[test]
    fn test_question_mark_placeholder() {
        assert_roundtrip("SELECT ? FROM x");
        assert_roundtrip("SELECT ? AS ? FROM x WHERE b BETWEEN ? AND ?");
    }

    #[test]
    fn test_named_parameter() {
        assert_roundtrip("SELECT :hello FROM x");
        assert_roundtrip("SELECT :hello, ? FROM x LIMIT :my_limit");
    }

    #[test]
    fn test_at_parameter() {
        assert_roundtrip("SELECT * FROM x OFFSET @skip");
    }
}

// ============================================================================
// SPECIAL CONSTRUCTS
// ============================================================================

#[cfg(test)]
mod special_tests {
    use super::*;

    #[test]
    fn test_interval() {
        assert_roundtrip("SELECT INTERVAL '1' DAY");
        assert_roundtrip("SELECT INTERVAL '1' MONTH");
        assert_roundtrip("SELECT INTERVAL '1' YEAR");
    }

    #[test]
    fn test_use_statement() {
        assert_roundtrip("USE db");
    }

    #[test]
    fn test_use_schema() {
        assert_roundtrip("USE SCHEMA x.y");
    }

    #[test]
    fn test_commit_rollback() {
        assert_roundtrip("COMMIT");
        assert_roundtrip("ROLLBACK");
        assert_roundtrip("BEGIN");
    }

    #[test]
    fn test_describe() {
        assert_roundtrip("DESCRIBE x");
    }

    #[test]
    fn test_array_type() {
        assert_roundtrip("SELECT CAST(a AS ARRAY<INT>)");
    }

    #[test]
    fn test_map() {
        assert_roundtrip("SELECT MAP()");
    }

    #[test]
    fn test_values() {
        assert_roundtrip("VALUES (1)");
        assert_roundtrip("VALUES (1), (2), (3)");
    }
}

// ============================================================================
// ADVANCED FEATURES
// ============================================================================

#[cfg(test)]
mod advanced_tests {
    use super::*;

    #[test]
    fn test_pivot() {
        assert_roundtrip("SELECT a FROM test PIVOT(SUM(x) FOR y IN ('z', 'q'))");
    }

    #[test]
    fn test_unpivot() {
        assert_roundtrip("SELECT a FROM test UNPIVOT(x FOR y IN (z, q)) AS x");
    }

    #[test]
    fn test_tablesample() {
        assert_roundtrip("SELECT a FROM test TABLESAMPLE (0.1 PERCENT)");
        assert_roundtrip("SELECT a FROM test TABLESAMPLE (100 ROWS)");
    }

    #[test]
    fn test_qualify() {
        assert_roundtrip("SELECT id FROM b.a AS a QUALIFY ROW_NUMBER() OVER (PARTITION BY br ORDER BY sadf DESC) = 1");
    }

    #[test]
    fn test_grouping_sets() {
        assert_roundtrip("SELECT a FROM test GROUP BY GROUPING SETS (())");
        assert_roundtrip("SELECT a FROM test GROUP BY GROUPING SETS (x, ())");
        assert_roundtrip("SELECT a FROM test GROUP BY CUBE (x)");
        assert_roundtrip("SELECT a FROM test GROUP BY ROLLUP (x)");
    }

    #[test]
    fn test_lateral_view() {
        assert_roundtrip("SELECT student, score FROM tests LATERAL VIEW EXPLODE(scores)");
    }

    #[test]
    fn test_unnest() {
        assert_roundtrip("SELECT student, score FROM tests CROSS JOIN UNNEST(scores) AS t(score)");
    }

    #[test]
    fn test_except_replace() {
        assert_roundtrip("SELECT * EXCEPT (a, b)");
        assert_roundtrip("SELECT * REPLACE (a AS b, b AS C)");
    }

    #[test]
    fn test_within_group() {
        assert_roundtrip("SELECT LISTAGG(x) WITHIN GROUP (ORDER BY x) AS y");
        assert_roundtrip("SELECT PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY x)");
    }
}

// ============================================================================
// COMMENTS
// ============================================================================

#[cfg(test)]
mod comment_tests {
    use super::*;

    #[test]
    fn test_inline_comments() {
        assert_roundtrip("SELECT CAST(x AS INT) /* comment */ FROM foo");
        assert_roundtrip("SELECT c /* c1 */ AS alias /* c2 */");
    }

    #[test]
    fn test_hint_comments() {
        assert_roundtrip("SELECT /*+ SOME_HINT(foo) */ 1");
    }
}

// ============================================================================
// TRIGONOMETRIC FUNCTIONS - Lines 915-934 from identity.sql
// ============================================================================

#[cfg(test)]
mod trig_function_tests {
    use super::*;

    #[test]
    fn test_trig_functions() {
        assert_roundtrip("SELECT ACOS(x)");
        assert_roundtrip("SELECT ASIN(x)");
        assert_roundtrip("SELECT ATAN(x)");
        assert_roundtrip("SELECT ATAN2(x, y)");
        assert_roundtrip("SELECT SIN(x)");
        assert_roundtrip("SELECT COS(x)");
        assert_roundtrip("SELECT TAN(x)");
    }

    #[test]
    fn test_hyperbolic_functions() {
        assert_roundtrip("SELECT ACOSH(x)");
        assert_roundtrip("SELECT ASINH(x)");
        assert_roundtrip("SELECT ATANH(x)");
        assert_roundtrip("SELECT SINH(x)");
        assert_roundtrip("SELECT COSH(x)");
        assert_roundtrip("SELECT TANH(x)");
    }
}