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
//! T-SQL (SQL Server) Dialect
//!
//! SQL Server-specific transformations based on sqlglot patterns.
//! Key differences:
//! - TOP instead of LIMIT
//! - ISNULL instead of COALESCE (though COALESCE also works)
//! - Square brackets for identifiers
//! - + for string concatenation
//! - CONVERT vs CAST
//! - CROSS APPLY / OUTER APPLY for lateral joins
//! - Different date functions (GETDATE, DATEADD, DATEDIFF, DATENAME)
use super::{DialectImpl, DialectType};
use crate::error::Result;
use crate::expressions::{
Alias, BinaryOp, Cast, Column, Cte, DataType, Exists, Expression, Function, Identifier, In,
Join, JoinKind, LikeOp, Literal, Null, Over, QuantifiedOp, Select, StringAggFunc, Subquery,
UnaryFunc, Where,
};
#[cfg(feature = "generate")]
use crate::generator::GeneratorConfig;
use crate::tokens::TokenizerConfig;
use std::collections::HashMap;
/// T-SQL (SQL Server) dialect
pub struct TSQLDialect;
impl DialectImpl for TSQLDialect {
fn dialect_type(&self) -> DialectType {
DialectType::TSQL
}
fn tokenizer_config(&self) -> TokenizerConfig {
let mut config = TokenizerConfig::default();
// SQL Server uses square brackets for identifiers
config.identifiers.insert('[', ']');
// SQL Server also supports double quotes (when QUOTED_IDENTIFIER is ON)
config.identifiers.insert('"', '"');
config
}
#[cfg(feature = "generate")]
fn generator_config(&self) -> GeneratorConfig {
use crate::generator::IdentifierQuoteStyle;
GeneratorConfig {
// Use square brackets by default for SQL Server
identifier_quote: '[',
identifier_quote_style: IdentifierQuoteStyle::BRACKET,
dialect: Some(DialectType::TSQL),
// T-SQL specific settings from Python sqlglot
// SQL Server uses TOP/FETCH instead of LIMIT
limit_fetch_style: crate::generator::LimitFetchStyle::FetchFirst,
// NULLS FIRST/LAST not supported in SQL Server
null_ordering_supported: false,
// SQL Server does not support SQL:2003 aggregate FILTER clauses.
aggregate_filter_supported: false,
// SQL Server supports SELECT INTO
supports_select_into: true,
// ALTER TABLE doesn't require COLUMN keyword
alter_table_include_column_keyword: false,
// Computed columns don't need type declaration
computed_column_with_type: false,
// RECURSIVE keyword not required in CTEs
cte_recursive_keyword_required: false,
// Ensure boolean expressions
ensure_bools: true,
// CONCAT requires at least 2 args
supports_single_arg_concat: false,
// TABLESAMPLE REPEATABLE
tablesample_seed_keyword: "REPEATABLE",
// JSON path without brackets
json_path_bracketed_key_supported: false,
// No TO_NUMBER function
supports_to_number: false,
// SET operation modifiers not supported
set_op_modifiers: false,
// COPY params need equals sign
copy_params_eq_required: true,
// No ALL clause for EXCEPT/INTERSECT
except_intersect_support_all_clause: false,
// ALTER SET is wrapped
alter_set_wrapped: true,
// T-SQL supports TRY_CAST
try_supported: true,
// No NVL2 support
nvl2_supported: false,
// TSQL uses = instead of DEFAULT for parameter defaults
parameter_default_equals: true,
// No window EXCLUDE support
supports_window_exclude: false,
// No DISTINCT with multiple args
multi_arg_distinct: false,
// TSQL doesn't support FOR UPDATE/SHARE
locking_reads_supported: false,
..Default::default()
}
}
#[cfg(feature = "transpile")]
fn transform_expr(&self, expr: Expression) -> Result<Expression> {
// Transform column data types in DDL (transform_recursive skips them by design).
if let Expression::CreateTable(mut ct) = expr {
for col in &mut ct.columns {
if let Ok(Expression::DataType(new_dt)) =
self.transform_data_type(col.data_type.clone())
{
col.data_type = new_dt;
}
}
return Ok(Expression::CreateTable(ct));
}
match expr {
// ===== SELECT a = 1 → SELECT 1 AS a =====
// In T-SQL, `SELECT a = expr` is equivalent to `SELECT expr AS a`
// BUT: `SELECT @a = expr` is a variable assignment, not an alias!
// Python sqlglot handles this at parser level via _parse_projections()
Expression::Select(mut select) => {
select.expressions = select
.expressions
.into_iter()
.map(|e| {
match e {
Expression::Eq(op) => {
// Check if left side is an identifier (column name)
// Don't transform if it's a variable (starts with @)
match &op.left {
Expression::Column(col)
if col.table.is_none()
&& !col.name.name.starts_with('@') =>
{
Expression::Alias(Box::new(Alias {
this: op.right,
alias: col.name.clone(),
column_aliases: Vec::new(),
alias_explicit_as: false,
alias_keyword: None,
pre_alias_comments: Vec::new(),
trailing_comments: Vec::new(),
inferred_type: None,
}))
}
Expression::Identifier(ident)
if !ident.name.starts_with('@') =>
{
Expression::Alias(Box::new(Alias {
this: op.right,
alias: ident.clone(),
column_aliases: Vec::new(),
alias_explicit_as: false,
alias_keyword: None,
pre_alias_comments: Vec::new(),
trailing_comments: Vec::new(),
inferred_type: None,
}))
}
_ => Expression::Eq(op),
}
}
other => other,
}
})
.collect();
Self::normalize_frame_incompatible_window_functions(&mut select);
let outer_qualifier = Self::single_select_source_qualifier(&select);
if let Some(ref mut where_clause) = select.where_clause {
where_clause.this = Self::rewrite_tuple_in_subquery_predicates(
std::mem::replace(&mut where_clause.this, Expression::Null(Null)),
outer_qualifier.as_ref(),
false,
);
}
// Transform CTEs in the WITH clause to add auto-aliases
if let Some(ref mut with) = select.with {
with.ctes = with
.ctes
.drain(..)
.map(|cte| self.transform_cte_inner(cte))
.collect();
}
Ok(Expression::Select(select))
}
// ===== Data Type Mappings =====
Expression::DataType(dt) => self.transform_data_type(dt),
// ===== Boolean IS TRUE/FALSE -> = 1/0 for TSQL =====
// TSQL doesn't have IS TRUE/IS FALSE syntax
Expression::IsTrue(it) => {
let one = Expression::Literal(Box::new(crate::expressions::Literal::Number(
"1".to_string(),
)));
if it.not {
// a IS NOT TRUE -> NOT a = 1
Ok(Expression::Not(Box::new(crate::expressions::UnaryOp {
this: Expression::Eq(Box::new(crate::expressions::BinaryOp {
left: it.this,
right: one,
left_comments: vec![],
operator_comments: vec![],
trailing_comments: vec![],
inferred_type: None,
})),
inferred_type: None,
})))
} else {
// a IS TRUE -> a = 1
Ok(Expression::Eq(Box::new(crate::expressions::BinaryOp {
left: it.this,
right: one,
left_comments: vec![],
operator_comments: vec![],
trailing_comments: vec![],
inferred_type: None,
})))
}
}
Expression::IsFalse(it) => {
let zero = Expression::Literal(Box::new(crate::expressions::Literal::Number(
"0".to_string(),
)));
if it.not {
// a IS NOT FALSE -> NOT a = 0
Ok(Expression::Not(Box::new(crate::expressions::UnaryOp {
this: Expression::Eq(Box::new(crate::expressions::BinaryOp {
left: it.this,
right: zero,
left_comments: vec![],
operator_comments: vec![],
trailing_comments: vec![],
inferred_type: None,
})),
inferred_type: None,
})))
} else {
// a IS FALSE -> a = 0
Ok(Expression::Eq(Box::new(crate::expressions::BinaryOp {
left: it.this,
right: zero,
left_comments: vec![],
operator_comments: vec![],
trailing_comments: vec![],
inferred_type: None,
})))
}
}
// Note: CASE WHEN boolean conditions are handled in ensure_bools preprocessing
// NOT IN -> NOT ... IN for TSQL (TSQL prefers NOT prefix)
Expression::In(mut in_expr) if in_expr.not => {
in_expr.not = false;
Ok(Expression::Not(Box::new(crate::expressions::UnaryOp {
this: Expression::In(in_expr),
inferred_type: None,
})))
}
// COALESCE with 2 args -> ISNULL in SQL Server (optimization)
// Note: COALESCE works in SQL Server, ISNULL is just more idiomatic
Expression::Coalesce(f) if f.expressions.len() == 2 => Ok(Expression::Function(
Box::new(Function::new("ISNULL".to_string(), f.expressions)),
)),
// NVL -> ISNULL in SQL Server
Expression::Nvl(f) => Ok(Expression::Function(Box::new(Function::new(
"ISNULL".to_string(),
vec![f.this, f.expression],
)))),
// GROUP_CONCAT -> STRING_AGG in SQL Server (SQL Server 2017+)
Expression::GroupConcat(f) => Ok(Expression::StringAgg(Box::new(StringAggFunc {
this: f.this,
separator: f.separator,
order_by: f.order_by,
distinct: f.distinct,
filter: f.filter,
limit: None,
inferred_type: None,
}))),
// LISTAGG -> STRING_AGG in SQL Server (SQL Server 2017+)
Expression::ListAgg(f) => Ok(Expression::StringAgg(Box::new(StringAggFunc {
this: f.this,
separator: f.separator,
order_by: f.order_by,
distinct: f.distinct,
filter: f.filter,
limit: None,
inferred_type: None,
}))),
// T-SQL/Fabric do not have boolean aggregates. Preserve PostgreSQL NULL
// semantics by returning NULL for unknown input predicates.
Expression::LogicalAnd(f) => Self::transform_logical_aggregate(f.this, f.filter, "MIN"),
Expression::LogicalOr(f) => Self::transform_logical_aggregate(f.this, f.filter, "MAX"),
// TryCast -> TRY_CAST (SQL Server supports TRY_CAST starting from 2012)
Expression::TryCast(c) => Ok(Expression::TryCast(c)),
// SafeCast -> TRY_CAST
Expression::SafeCast(c) => Ok(Expression::TryCast(c)),
// ILIKE -> LOWER() LIKE LOWER() in SQL Server (no ILIKE support)
Expression::ILike(op) => {
// SQL Server is case-insensitive by default based on collation
// But for explicit case-insensitive matching, use LOWER
let lower_left = Expression::Lower(Box::new(UnaryFunc::new(op.left)));
let lower_right = Expression::Lower(Box::new(UnaryFunc::new(op.right)));
Ok(Expression::Like(Box::new(LikeOp {
left: lower_left,
right: lower_right,
escape: op.escape,
quantifier: op.quantifier,
inferred_type: None,
})))
}
// || (Concat operator) -> + in SQL Server
// SQL Server uses + for string concatenation
Expression::Concat(op) => {
// Convert || to + operator (Add)
Ok(Expression::Add(op))
}
// RANDOM -> RAND in SQL Server
Expression::Random(_) => Ok(Expression::Rand(Box::new(crate::expressions::Rand {
seed: None,
lower: None,
upper: None,
}))),
// UNNEST -> Not directly supported, use CROSS APPLY with STRING_SPLIT or OPENJSON
Expression::Unnest(f) => {
// For basic cases, we'll use a placeholder
// Full support would require context-specific transformation
Ok(Expression::Function(Box::new(Function::new(
"OPENJSON".to_string(),
vec![f.this],
))))
}
// EXPLODE -> Similar to UNNEST, use CROSS APPLY
Expression::Explode(f) => Ok(Expression::Function(Box::new(Function::new(
"OPENJSON".to_string(),
vec![f.this],
)))),
// PostgreSQL LATERAL join forms -> SQL Server APPLY.
Expression::Join(join) => Ok(Expression::Join(Box::new(
Self::transform_lateral_join_to_apply(*join),
))),
// LENGTH -> LEN in SQL Server
Expression::Length(f) => Ok(Expression::Function(Box::new(Function::new(
"LEN".to_string(),
vec![f.this],
)))),
// STDDEV -> STDEV in SQL Server
Expression::Stddev(f) => Ok(Expression::Function(Box::new(Function::new(
"STDEV".to_string(),
vec![f.this],
)))),
Expression::StddevSamp(f) => Ok(Expression::Function(Box::new(Function::new(
"STDEV".to_string(),
vec![f.this],
)))),
Expression::StddevPop(f) => Ok(Expression::Function(Box::new(Function::new(
"STDEVP".to_string(),
vec![f.this],
)))),
// Boolean literals TRUE/FALSE -> 1/0 in SQL Server
Expression::Boolean(b) => {
let value = if b.value { 1 } else { 0 };
Ok(Expression::Literal(Box::new(
crate::expressions::Literal::Number(value.to_string()),
)))
}
// LN -> LOG in SQL Server
Expression::Ln(f) => Ok(Expression::Function(Box::new(Function::new(
"LOG".to_string(),
vec![f.this],
)))),
// ===== Date/time =====
// CurrentDate -> CAST(GETDATE() AS DATE) in SQL Server
Expression::CurrentDate(_) => {
let getdate =
Expression::Function(Box::new(Function::new("GETDATE".to_string(), vec![])));
Ok(Expression::Cast(Box::new(crate::expressions::Cast {
this: getdate,
to: crate::expressions::DataType::Date,
trailing_comments: Vec::new(),
double_colon_syntax: false,
format: None,
default: None,
inferred_type: None,
})))
}
// CurrentTimestamp -> GETDATE() in SQL Server
Expression::CurrentTimestamp(_) => Ok(Expression::Function(Box::new(Function::new(
"GETDATE".to_string(),
vec![],
)))),
// DateDiff -> DATEDIFF
Expression::DateDiff(f) => {
// TSQL: DATEDIFF(unit, start, end)
let unit_str = match f.unit {
Some(crate::expressions::IntervalUnit::Year) => "YEAR",
Some(crate::expressions::IntervalUnit::Quarter) => "QUARTER",
Some(crate::expressions::IntervalUnit::Month) => "MONTH",
Some(crate::expressions::IntervalUnit::Week) => "WEEK",
Some(crate::expressions::IntervalUnit::Day) => "DAY",
Some(crate::expressions::IntervalUnit::Hour) => "HOUR",
Some(crate::expressions::IntervalUnit::Minute) => "MINUTE",
Some(crate::expressions::IntervalUnit::Second) => "SECOND",
Some(crate::expressions::IntervalUnit::Millisecond) => "MILLISECOND",
Some(crate::expressions::IntervalUnit::Microsecond) => "MICROSECOND",
Some(crate::expressions::IntervalUnit::Nanosecond) => "NANOSECOND",
None => "DAY",
};
let unit = Expression::Identifier(crate::expressions::Identifier {
name: unit_str.to_string(),
quoted: false,
trailing_comments: Vec::new(),
span: None,
});
Ok(Expression::Function(Box::new(Function::new(
"DATEDIFF".to_string(),
vec![unit, f.expression, f.this], // Note: order is different in TSQL
))))
}
// DateAdd -> DATEADD
Expression::DateAdd(f) => {
let unit_str = match f.unit {
crate::expressions::IntervalUnit::Year => "YEAR",
crate::expressions::IntervalUnit::Quarter => "QUARTER",
crate::expressions::IntervalUnit::Month => "MONTH",
crate::expressions::IntervalUnit::Week => "WEEK",
crate::expressions::IntervalUnit::Day => "DAY",
crate::expressions::IntervalUnit::Hour => "HOUR",
crate::expressions::IntervalUnit::Minute => "MINUTE",
crate::expressions::IntervalUnit::Second => "SECOND",
crate::expressions::IntervalUnit::Millisecond => "MILLISECOND",
crate::expressions::IntervalUnit::Microsecond => "MICROSECOND",
crate::expressions::IntervalUnit::Nanosecond => "NANOSECOND",
};
let unit = Expression::Identifier(crate::expressions::Identifier {
name: unit_str.to_string(),
quoted: false,
trailing_comments: Vec::new(),
span: None,
});
Ok(Expression::Function(Box::new(Function::new(
"DATEADD".to_string(),
vec![unit, f.interval, f.this],
))))
}
// ===== UUID =====
// Uuid -> NEWID in SQL Server
Expression::Uuid(_) => Ok(Expression::Function(Box::new(Function::new(
"NEWID".to_string(),
vec![],
)))),
// ===== Conditional =====
// IfFunc -> IIF in SQL Server
Expression::IfFunc(f) => {
let false_val = f
.false_value
.unwrap_or(Expression::Null(crate::expressions::Null));
Ok(Expression::Function(Box::new(Function::new(
"IIF".to_string(),
vec![f.condition, f.true_value, false_val],
))))
}
// ===== String functions =====
// StringAgg -> STRING_AGG in SQL Server 2017+ - keep as-is to preserve ORDER BY
Expression::StringAgg(f) => Ok(Expression::StringAgg(f)),
// LastDay -> EOMONTH (note: TSQL doesn't support date part argument)
Expression::LastDay(f) => Ok(Expression::Function(Box::new(Function::new(
"EOMONTH".to_string(),
vec![f.this.clone()],
)))),
// Ceil -> CEILING
Expression::Ceil(f) => Ok(Expression::Function(Box::new(Function::new(
"CEILING".to_string(),
vec![f.this],
)))),
// Repeat -> REPLICATE in SQL Server
Expression::Repeat(f) => Ok(Expression::Function(Box::new(Function::new(
"REPLICATE".to_string(),
vec![f.this, f.times],
)))),
// Chr -> CHAR in SQL Server
Expression::Chr(f) => Ok(Expression::Function(Box::new(Function::new(
"CHAR".to_string(),
vec![f.this],
)))),
// ===== Variance =====
// VarPop -> VARP
Expression::VarPop(f) => Ok(Expression::Function(Box::new(Function::new(
"VARP".to_string(),
vec![f.this],
)))),
// Variance -> VAR
Expression::Variance(f) => Ok(Expression::Function(Box::new(Function::new(
"VAR".to_string(),
vec![f.this],
)))),
Expression::VarSamp(f) => Ok(Expression::Function(Box::new(Function::new(
"VAR".to_string(),
vec![f.this],
)))),
// ===== Hash functions =====
// MD5Digest -> HASHBYTES('MD5', ...)
Expression::MD5Digest(f) => Ok(Expression::Function(Box::new(Function::new(
"HASHBYTES".to_string(),
vec![Expression::string("MD5"), *f.this],
)))),
// SHA -> HASHBYTES('SHA1', ...)
Expression::SHA(f) => Ok(Expression::Function(Box::new(Function::new(
"HASHBYTES".to_string(),
vec![Expression::string("SHA1"), f.this],
)))),
// SHA1Digest -> HASHBYTES('SHA1', ...)
Expression::SHA1Digest(f) => Ok(Expression::Function(Box::new(Function::new(
"HASHBYTES".to_string(),
vec![Expression::string("SHA1"), f.this],
)))),
// ===== Array functions =====
// ArrayToString -> STRING_AGG
Expression::ArrayToString(f) => Ok(Expression::Function(Box::new(Function::new(
"STRING_AGG".to_string(),
vec![f.this],
)))),
// ===== DDL Column Constraints =====
// AutoIncrementColumnConstraint -> IDENTITY in SQL Server
Expression::AutoIncrementColumnConstraint(_) => Ok(Expression::Function(Box::new(
Function::new("IDENTITY".to_string(), vec![]),
))),
// ===== DDL three-part name stripping =====
// TSQL strips database (catalog) prefix from 3-part names for CREATE VIEW/DROP VIEW
// Python sqlglot: expression.this.set("catalog", None)
Expression::CreateView(mut view) => {
// Strip catalog from three-part name (a.b.c -> b.c)
view.name.catalog = None;
Ok(Expression::CreateView(view))
}
Expression::DropView(mut view) => {
// Strip catalog from three-part name (a.b.c -> b.c)
view.name.catalog = None;
Ok(Expression::DropView(view))
}
// ParseJson: handled by generator (emits just the string literal for TSQL)
// JSONExtract with variant_extract (Snowflake colon syntax) -> ISNULL(JSON_QUERY, JSON_VALUE)
Expression::JSONExtract(e) if e.variant_extract.is_some() => {
let path = match *e.expression {
Expression::Literal(lit) if matches!(lit.as_ref(), Literal::String(_)) => {
let Literal::String(s) = lit.as_ref() else {
unreachable!()
};
let normalized = if s.starts_with('$') {
s.clone()
} else if s.starts_with('[') {
format!("${}", s)
} else {
format!("$.{}", s)
};
Expression::Literal(Box::new(Literal::String(normalized)))
}
other => other,
};
let json_query = Expression::Function(Box::new(Function::new(
"JSON_QUERY".to_string(),
vec![(*e.this).clone(), path.clone()],
)));
let json_value = Expression::Function(Box::new(Function::new(
"JSON_VALUE".to_string(),
vec![*e.this, path],
)));
Ok(Expression::Function(Box::new(Function::new(
"ISNULL".to_string(),
vec![json_query, json_value],
))))
}
// Generic function transformations
Expression::Function(f) => self.transform_function(*f),
// Generic aggregate function transformations
Expression::AggregateFunction(f) => self.transform_aggregate_function(f),
// ===== CTEs need auto-aliased outputs =====
// In TSQL, bare expressions in CTEs need explicit aliases
Expression::Cte(cte) => self.transform_cte(*cte),
// ===== Subqueries need auto-aliased outputs =====
// In TSQL, bare expressions in aliased subqueries need explicit aliases
Expression::Subquery(subquery) => self.transform_subquery(*subquery),
// Convert JsonQuery struct to ISNULL(JSON_QUERY(..., path), JSON_VALUE(..., path))
Expression::JsonQuery(f) => {
let json_query = Expression::Function(Box::new(Function::new(
"JSON_QUERY".to_string(),
vec![f.this.clone(), f.path.clone()],
)));
let json_value = Expression::Function(Box::new(Function::new(
"JSON_VALUE".to_string(),
vec![f.this, f.path],
)));
Ok(Expression::Function(Box::new(Function::new(
"ISNULL".to_string(),
vec![json_query, json_value],
))))
}
// Convert JsonValue struct to Function("JSON_VALUE", ...) for uniform handling
Expression::JsonValue(f) => Ok(Expression::Function(Box::new(Function::new(
"JSON_VALUE".to_string(),
vec![f.this, f.path],
)))),
// PostgreSQL pg_get_querydef can emit scalar array comparisons for
// literal arrays/tuples. T-SQL/Fabric require IN for this shape.
Expression::Any(ref q) if matches!(&q.op, Some(QuantifiedOp::Eq)) => {
let values: Option<Vec<Expression>> = match &q.subquery {
Expression::ArrayFunc(a) => Some(a.expressions.clone()),
Expression::Array(a) => Some(a.expressions.clone()),
Expression::Tuple(t) => Some(t.expressions.clone()),
_ => None,
};
match values {
Some(expressions) if expressions.is_empty() => {
Ok(Expression::Eq(Box::new(crate::expressions::BinaryOp::new(
Expression::Literal(Box::new(Literal::Number("1".to_string()))),
Expression::Literal(Box::new(Literal::Number("0".to_string()))),
))))
}
Some(expressions) => Ok(Expression::In(Box::new(In {
this: q.this.clone(),
expressions,
query: None,
not: false,
global: false,
unnest: None,
is_field: false,
}))),
None => Ok(expr.clone()),
}
}
// Pass through everything else
_ => Ok(expr),
}
}
}
#[cfg(feature = "transpile")]
impl TSQLDialect {
fn normalize_frame_incompatible_window_functions(select: &mut Select) {
let window_map: HashMap<String, Over> = select
.windows
.as_ref()
.map(|windows| {
windows
.iter()
.map(|window| (window.name.name.to_lowercase(), window.spec.clone()))
.collect()
})
.unwrap_or_default();
for expr in &mut select.expressions {
Self::normalize_frame_incompatible_window_expr(expr, &window_map);
}
if let Some(order_by) = &mut select.order_by {
for ordered in &mut order_by.expressions {
Self::normalize_frame_incompatible_window_expr(&mut ordered.this, &window_map);
}
}
if let Some(qualify) = &mut select.qualify {
Self::normalize_frame_incompatible_window_expr(&mut qualify.this, &window_map);
}
}
fn normalize_frame_incompatible_window_expr(
expr: &mut Expression,
window_map: &HashMap<String, Over>,
) {
match expr {
Expression::WindowFunction(wf) => {
Self::normalize_frame_incompatible_window_expr(&mut wf.this, window_map);
if !Self::is_tsql_frame_incompatible_window_function(&wf.this) {
return;
}
wf.over.frame = None;
let Some(window_name) = wf.over.window_name.clone() else {
return;
};
let Some(named_spec) =
Self::resolve_named_window_spec(&window_name.name, window_map, &mut Vec::new())
else {
return;
};
if named_spec.frame.is_none() {
return;
}
if wf.over.partition_by.is_empty() {
wf.over.partition_by = named_spec.partition_by;
}
if wf.over.order_by.is_empty() {
wf.over.order_by = named_spec.order_by;
}
wf.over.window_name = None;
wf.over.frame = None;
}
Expression::Alias(alias) => {
Self::normalize_frame_incompatible_window_expr(&mut alias.this, window_map);
}
Expression::Paren(paren) => {
Self::normalize_frame_incompatible_window_expr(&mut paren.this, window_map);
}
Expression::Cast(cast) | Expression::TryCast(cast) | Expression::SafeCast(cast) => {
Self::normalize_frame_incompatible_window_expr(&mut cast.this, window_map);
}
Expression::Function(function) => {
for arg in &mut function.args {
Self::normalize_frame_incompatible_window_expr(arg, window_map);
}
}
Expression::Case(case) => {
if let Some(operand) = &mut case.operand {
Self::normalize_frame_incompatible_window_expr(operand, window_map);
}
for (condition, result) in &mut case.whens {
Self::normalize_frame_incompatible_window_expr(condition, window_map);
Self::normalize_frame_incompatible_window_expr(result, window_map);
}
if let Some(else_expr) = &mut case.else_ {
Self::normalize_frame_incompatible_window_expr(else_expr, window_map);
}
}
Expression::And(op)
| Expression::Or(op)
| Expression::Add(op)
| Expression::Sub(op)
| Expression::Mul(op)
| Expression::Div(op)
| Expression::Mod(op)
| Expression::Eq(op)
| Expression::Neq(op)
| Expression::Lt(op)
| Expression::Lte(op)
| Expression::Gt(op)
| Expression::Gte(op)
| Expression::Match(op)
| Expression::BitwiseAnd(op)
| Expression::BitwiseOr(op)
| Expression::BitwiseXor(op)
| Expression::Concat(op)
| Expression::Adjacent(op)
| Expression::TsMatch(op)
| Expression::PropertyEQ(op)
| Expression::ArrayContainsAll(op)
| Expression::ArrayContainedBy(op)
| Expression::ArrayOverlaps(op)
| Expression::JSONBContainsAllTopKeys(op)
| Expression::JSONBContainsAnyTopKeys(op)
| Expression::JSONBDeleteAtPath(op)
| Expression::ExtendsLeft(op)
| Expression::ExtendsRight(op)
| Expression::Is(op)
| Expression::MemberOf(op) => {
Self::normalize_frame_incompatible_window_expr(&mut op.left, window_map);
Self::normalize_frame_incompatible_window_expr(&mut op.right, window_map);
}
Expression::Like(op) | Expression::ILike(op) => {
Self::normalize_frame_incompatible_window_expr(&mut op.left, window_map);
Self::normalize_frame_incompatible_window_expr(&mut op.right, window_map);
if let Some(escape) = &mut op.escape {
Self::normalize_frame_incompatible_window_expr(escape, window_map);
}
}
Expression::Not(op) | Expression::Neg(op) | Expression::BitwiseNot(op) => {
Self::normalize_frame_incompatible_window_expr(&mut op.this, window_map);
}
Expression::In(in_expr) => {
Self::normalize_frame_incompatible_window_expr(&mut in_expr.this, window_map);
for value in &mut in_expr.expressions {
Self::normalize_frame_incompatible_window_expr(value, window_map);
}
}
Expression::Between(between) => {
Self::normalize_frame_incompatible_window_expr(&mut between.this, window_map);
Self::normalize_frame_incompatible_window_expr(&mut between.low, window_map);
Self::normalize_frame_incompatible_window_expr(&mut between.high, window_map);
}
Expression::IsNull(is_null) => {
Self::normalize_frame_incompatible_window_expr(&mut is_null.this, window_map);
}
Expression::IsTrue(is_true) | Expression::IsFalse(is_true) => {
Self::normalize_frame_incompatible_window_expr(&mut is_true.this, window_map);
}
_ => {}
}
}
fn is_tsql_frame_incompatible_window_function(expr: &Expression) -> bool {
matches!(
expr,
Expression::RowNumber(_)
| Expression::Rank(_)
| Expression::DenseRank(_)
| Expression::NTile(_)
| Expression::Ntile(_)
| Expression::Lead(_)
| Expression::Lag(_)
| Expression::PercentRank(_)
| Expression::CumeDist(_)
)
}
fn resolve_named_window_spec(
name: &str,
window_map: &HashMap<String, Over>,
seen: &mut Vec<String>,
) -> Option<Over> {
let key = name.to_lowercase();
if seen.iter().any(|seen_name| seen_name == &key) {
return None;
}
let named_spec = window_map.get(&key)?.clone();
seen.push(key);
let mut resolved = if let Some(base_window) = &named_spec.window_name {
Self::resolve_named_window_spec(&base_window.name, window_map, seen)
.unwrap_or_else(Self::empty_over)
} else {
Self::empty_over()
};
if !named_spec.partition_by.is_empty() {
resolved.partition_by = named_spec.partition_by;
}
if !named_spec.order_by.is_empty() {
resolved.order_by = named_spec.order_by;
}
if named_spec.frame.is_some() {
resolved.frame = named_spec.frame;
}
Some(resolved)
}
fn empty_over() -> Over {
Over {
window_name: None,
partition_by: Vec::new(),
order_by: Vec::new(),
frame: None,
alias: None,
}
}
fn transform_lateral_join_to_apply(mut join: Join) -> Join {
let Some(apply_kind) = Self::lateral_apply_kind(&join) else {
return join;
};
join.this = Self::remove_lateral_marker(join.this);
join.on = None;
join.using.clear();
join.kind = apply_kind;
join.use_inner_keyword = false;
join.use_outer_keyword = false;
join.deferred_condition = false;
join.join_hint = None;
join.match_condition = None;
join.directed = false;
join
}
fn lateral_apply_kind(join: &Join) -> Option<JoinKind> {
let has_apply_condition = join.using.is_empty() && Self::has_no_or_true_on(&join.on);
match join.kind {
JoinKind::Lateral if has_apply_condition => Some(JoinKind::CrossApply),
JoinKind::LeftLateral if has_apply_condition => Some(JoinKind::OuterApply),
JoinKind::Cross | JoinKind::Inner
if has_apply_condition && Self::is_lateral_table_expression(&join.this) =>
{
Some(JoinKind::CrossApply)
}
JoinKind::Left
if has_apply_condition && Self::is_lateral_table_expression(&join.this) =>
{
Some(JoinKind::OuterApply)
}
_ => None,
}
}
fn has_no_or_true_on(on: &Option<Expression>) -> bool {
match on {
None => true,
Some(expr) => Self::is_true_condition(expr),
}
}
fn is_true_condition(expr: &Expression) -> bool {
match expr {
Expression::Boolean(boolean) => boolean.value,
Expression::Literal(lit) => {
matches!(lit.as_ref(), Literal::Number(value) if value.trim() == "1")
}
Expression::Eq(op) => {
Self::is_true_condition(&op.left) && Self::is_true_condition(&op.right)
}
Expression::Paren(paren) => Self::is_true_condition(&paren.this),
_ => false,
}
}
fn is_lateral_table_expression(expr: &Expression) -> bool {
match expr {
Expression::Subquery(subquery) => subquery.lateral,
Expression::Lateral(_) => true,
Expression::Alias(alias) => Self::is_lateral_table_expression(&alias.this),
_ => false,
}
}
fn remove_lateral_marker(expr: Expression) -> Expression {
match expr {
Expression::Subquery(mut subquery) => {
subquery.lateral = false;
Expression::Subquery(subquery)
}
Expression::Lateral(lateral) => Self::lateral_to_table_expression(*lateral),
Expression::Alias(mut alias) => {
alias.this = Self::remove_lateral_marker(alias.this);
Expression::Alias(alias)
}
other => other,
}
}
fn lateral_to_table_expression(lateral: crate::expressions::Lateral) -> Expression {
let expr = *lateral.this;
let Some(alias) = lateral.alias else {
return expr;
};
Expression::Alias(Box::new(Alias {
this: expr,
alias: if lateral.alias_quoted {
Identifier::quoted(alias)
} else {
Identifier::new(alias)
},
column_aliases: lateral
.column_aliases
.into_iter()
.map(Identifier::new)
.collect(),
alias_explicit_as: true,
alias_keyword: None,
pre_alias_comments: Vec::new(),
trailing_comments: Vec::new(),
inferred_type: None,
}))
}
fn rewrite_tuple_in_subquery_predicates(
expr: Expression,
outer_qualifier: Option<&Identifier>,
under_not: bool,
) -> Expression {
match expr {
Expression::In(in_expr) if !under_not => {
let in_expr = *in_expr;
Self::tuple_in_subquery_to_exists(&in_expr, outer_qualifier)
.unwrap_or_else(|| Expression::In(Box::new(in_expr)))
}
Expression::And(mut op) => {
op.left =
Self::rewrite_tuple_in_subquery_predicates(op.left, outer_qualifier, under_not);
op.right = Self::rewrite_tuple_in_subquery_predicates(
op.right,
outer_qualifier,
under_not,
);
Expression::And(op)
}
Expression::Or(mut op) => {
op.left =
Self::rewrite_tuple_in_subquery_predicates(op.left, outer_qualifier, under_not);
op.right = Self::rewrite_tuple_in_subquery_predicates(
op.right,
outer_qualifier,
under_not,
);
Expression::Or(op)
}
Expression::Paren(mut paren) => {
paren.this = Self::rewrite_tuple_in_subquery_predicates(
paren.this,
outer_qualifier,
under_not,
);
Expression::Paren(paren)
}
Expression::Not(mut not) => {
not.this =
Self::rewrite_tuple_in_subquery_predicates(not.this, outer_qualifier, true);
Expression::Not(not)
}
other => other,
}
}
fn tuple_in_subquery_to_exists(
in_expr: &In,
outer_qualifier: Option<&Identifier>,
) -> Option<Expression> {
if in_expr.not || !in_expr.expressions.is_empty() || in_expr.unnest.is_some() {
return None;
}
let left_expressions = Self::tuple_expressions(&in_expr.this)?;
let mut select = match in_expr.query.as_ref()? {
Expression::Select(select) => (**select).clone(),
_ => return None,
};
if left_expressions.len() != select.expressions.len() || left_expressions.is_empty() {
return None;
}
let inner_qualifier = Self::single_select_source_qualifier(&select);
let mut predicates = Vec::with_capacity(left_expressions.len() + 1);
for (projection, left) in select
.expressions
.iter()
.cloned()
.zip(left_expressions.iter().cloned())
{
let inner = Self::tuple_in_projection_expr(projection, inner_qualifier.as_ref())?;
let outer = Self::qualify_tuple_operand(left, outer_qualifier);
predicates.push(Expression::Eq(Box::new(BinaryOp::new(inner, outer))));
}
if let Some(where_clause) = select.where_clause.take() {
predicates.push(where_clause.this);
}
select.expressions = vec![Expression::number(1)];
select.where_clause = Some(Where {
this: Self::and_all(predicates)?,
});
Some(Expression::Exists(Box::new(Exists {
this: Expression::Select(Box::new(select)),
not: false,
})))
}
fn tuple_expressions(expr: &Expression) -> Option<&[Expression]> {
match expr {
Expression::Tuple(tuple) => Some(&tuple.expressions),
Expression::Paren(paren) => Self::tuple_expressions(&paren.this),
_ => None,
}
}
fn tuple_in_projection_expr(
expr: Expression,
qualifier: Option<&Identifier>,
) -> Option<Expression> {
match expr {
Expression::Alias(alias) => Self::tuple_in_projection_expr(alias.this, qualifier),
Expression::Column(mut column) => {
if column.table.is_none() {
column.table = qualifier.cloned();
}
Some(Expression::Column(column))
}
Expression::Identifier(identifier) => {
Some(Self::column_from_identifier(identifier, qualifier.cloned()))
}
Expression::Dot(_) => Some(expr),
_ => None,
}
}
fn qualify_tuple_operand(expr: Expression, qualifier: Option<&Identifier>) -> Expression {
match expr {
Expression::Column(mut column) => {
if column.table.is_none() {
column.table = qualifier.cloned();
}
Expression::Column(column)
}
Expression::Identifier(identifier) => {
Self::column_from_identifier(identifier, qualifier.cloned())
}
other => other,
}
}
fn column_from_identifier(identifier: Identifier, table: Option<Identifier>) -> Expression {
Expression::Column(Box::new(Column {
name: identifier,
table,
join_mark: false,
trailing_comments: Vec::new(),
span: None,
inferred_type: None,
}))
}
fn single_select_source_qualifier(select: &Select) -> Option<Identifier> {
if !select.joins.is_empty() {
return None;
}
let from = select.from.as_ref()?;
if from.expressions.len() != 1 {
return None;
}
Self::source_qualifier(&from.expressions[0])
}
fn source_qualifier(source: &Expression) -> Option<Identifier> {
match source {
Expression::Table(table) => table.alias.clone().or_else(|| Some(table.name.clone())),
Expression::Subquery(subquery) => subquery.alias.clone(),
_ => None,
}
}
fn and_all(mut predicates: Vec<Expression>) -> Option<Expression> {
if predicates.is_empty() {
return None;
}
let first = predicates.remove(0);
Some(predicates.into_iter().fold(first, |left, right| {
Expression::And(Box::new(BinaryOp::new(left, right)))
}))
}
/// Transform data types according to T-SQL TYPE_MAPPING
pub(super) fn transform_data_type(
&self,
dt: crate::expressions::DataType,
) -> Result<Expression> {
use crate::expressions::DataType;
let transformed = match dt {
// BOOLEAN -> BIT
DataType::Boolean => DataType::Custom {
name: "BIT".to_string(),
},
// INT stays as INT in TSQL (native type)
DataType::Int { .. } => dt,
// DOUBLE stays as Double internally (TSQL generator outputs FLOAT for it)
// DECIMAL -> NUMERIC
DataType::Decimal { precision, scale } => DataType::Custom {
name: if let (Some(p), Some(s)) = (&precision, &scale) {
format!("NUMERIC({}, {})", p, s)
} else if let Some(p) = &precision {
format!("NUMERIC({})", p)
} else {
"NUMERIC".to_string()
},
},
// TEXT -> VARCHAR(MAX)
DataType::Text => DataType::Custom {
name: "VARCHAR(MAX)".to_string(),
},
// TIMESTAMP -> DATETIME2
DataType::Timestamp { .. } => DataType::Custom {
name: "DATETIME2".to_string(),
},
// UUID -> UNIQUEIDENTIFIER
DataType::Uuid => DataType::Custom {
name: "UNIQUEIDENTIFIER".to_string(),
},
// Normalise custom type names that have PostgreSQL aliases
DataType::Custom { ref name } => {
let upper = name.trim().to_uppercase();
let (base_name, precision, _scale) = Self::parse_type_precision_and_scale(&upper);
match base_name.as_str() {
// BPCHAR is PostgreSQL's blank-padded CHAR alias — map to CHAR
"BPCHAR" => {
if let Some(len) = precision {
DataType::Char { length: Some(len) }
} else {
DataType::Char { length: None }
}
}
_ => dt,
}
}
// Keep all other types as-is
other => other,
};
Ok(Expression::DataType(transformed))
}
/// Parse a type name that may embed precision/scale: `"TYPENAME(n, m)"` → `("TYPENAME", Some(n), Some(m))`.
pub(super) fn parse_type_precision_and_scale(name: &str) -> (String, Option<u32>, Option<u32>) {
if let Some(paren_pos) = name.find('(') {
let base = name[..paren_pos].to_string();
let rest = &name[paren_pos + 1..];
if let Some(close_pos) = rest.find(')') {
let args = &rest[..close_pos];
let parts: Vec<&str> = args.split(',').map(|s| s.trim()).collect();
let precision = parts.first().and_then(|s| s.parse::<u32>().ok());
let scale = parts.get(1).and_then(|s| s.parse::<u32>().ok());
return (base, precision, scale);
}
(base, None, None)
} else {
(name.to_string(), None, None)
}
}
fn transform_logical_aggregate(
condition: Expression,
filter: Option<Expression>,
aggregate_name: &str,
) -> Result<Expression> {
let false_condition = Expression::Not(Box::new(crate::expressions::UnaryOp {
this: condition.clone(),
inferred_type: None,
}));
let true_condition = Self::apply_aggregate_filter(condition, filter.clone());
let false_condition = Self::apply_aggregate_filter(false_condition, filter);
let case_expr = Expression::Case(Box::new(crate::expressions::Case {
operand: None,
whens: vec![
(true_condition, Expression::number(1)),
(false_condition, Expression::number(0)),
],
else_: Some(Expression::null()),
comments: Vec::new(),
inferred_type: None,
}));
let case_expr = crate::transforms::ensure_bools(case_expr)?;
let aggregate = Expression::Function(Box::new(Function::new(
aggregate_name.to_string(),
vec![case_expr],
)));
Ok(Expression::Cast(Box::new(Cast {
this: aggregate,
to: DataType::Custom {
name: "BIT".to_string(),
},
trailing_comments: Vec::new(),
double_colon_syntax: false,
format: None,
default: None,
inferred_type: None,
})))
}
fn apply_aggregate_filter(condition: Expression, filter: Option<Expression>) -> Expression {
match filter {
Some(filter) => Expression::And(Box::new(crate::expressions::BinaryOp::new(
filter, condition,
))),
None => condition,
}
}
fn transform_function(&self, f: Function) -> Result<Expression> {
let name_upper = f.name.to_uppercase();
match name_upper.as_str() {
// COALESCE -> ISNULL for 2 args (optimization)
"COALESCE" if f.args.len() == 2 => Ok(Expression::Function(Box::new(Function::new(
"ISNULL".to_string(),
f.args,
)))),
// NVL -> ISNULL (SQL Server function)
"NVL" if f.args.len() == 2 => Ok(Expression::Function(Box::new(Function::new(
"ISNULL".to_string(),
f.args,
)))),
// GROUP_CONCAT -> STRING_AGG in SQL Server 2017+
"GROUP_CONCAT" if !f.args.is_empty() => Ok(Expression::Function(Box::new(
Function::new("STRING_AGG".to_string(), f.args),
))),
// STRING_AGG is native to SQL Server 2017+
"STRING_AGG" => Ok(Expression::Function(Box::new(f))),
// LISTAGG -> STRING_AGG
"LISTAGG" if !f.args.is_empty() => Ok(Expression::Function(Box::new(Function::new(
"STRING_AGG".to_string(),
f.args,
)))),
// SUBSTR -> SUBSTRING
"SUBSTR" => Ok(Expression::Function(Box::new(Function::new(
"SUBSTRING".to_string(),
f.args,
)))),
// LENGTH -> LEN in SQL Server
"LENGTH" if f.args.len() == 1 => Ok(Expression::Function(Box::new(Function::new(
"LEN".to_string(),
f.args,
)))),
// RANDOM -> RAND
"RANDOM" => Ok(Expression::Rand(Box::new(crate::expressions::Rand {
seed: None,
lower: None,
upper: None,
}))),
// NOW -> GETDATE or CURRENT_TIMESTAMP (both work)
"NOW" => Ok(Expression::Function(Box::new(Function::new(
"GETDATE".to_string(),
vec![],
)))),
// CURRENT_TIMESTAMP -> GETDATE (SQL Server prefers GETDATE)
"CURRENT_TIMESTAMP" => Ok(Expression::Function(Box::new(Function::new(
"GETDATE".to_string(),
vec![],
)))),
// CURRENT_DATE -> CAST(GETDATE() AS DATE)
"CURRENT_DATE" => {
// In SQL Server, use CAST(GETDATE() AS DATE)
Ok(Expression::Function(Box::new(Function::new(
"CAST".to_string(),
vec![
Expression::Function(Box::new(Function::new(
"GETDATE".to_string(),
vec![],
))),
Expression::Identifier(crate::expressions::Identifier::new("DATE")),
],
))))
}
// TO_DATE -> CONVERT or CAST
"TO_DATE" => Ok(Expression::Function(Box::new(Function::new(
"CONVERT".to_string(),
f.args,
)))),
// TO_TIMESTAMP -> CONVERT
"TO_TIMESTAMP" => Ok(Expression::Function(Box::new(Function::new(
"CONVERT".to_string(),
f.args,
)))),
// TO_CHAR -> FORMAT in SQL Server 2012+
"TO_CHAR" => Ok(Expression::Function(Box::new(Function::new(
"FORMAT".to_string(),
f.args,
)))),
// DATE_FORMAT -> FORMAT
"DATE_FORMAT" => Ok(Expression::Function(Box::new(Function::new(
"FORMAT".to_string(),
f.args,
)))),
// DATE_TRUNC -> DATETRUNC in SQL Server 2022+
// For older versions, use DATEADD/DATEDIFF combo
"DATE_TRUNC" | "DATETRUNC" => {
let mut args = Self::uppercase_first_arg_if_identifier(f.args);
// Cast string literal date arg to DATETIME2
if args.len() >= 2 {
if let Expression::Literal(lit) = &args[1] {
if let Literal::String(_) = lit.as_ref() {
args[1] = Expression::Cast(Box::new(Cast {
this: args[1].clone(),
to: DataType::Custom {
name: "DATETIME2".to_string(),
},
trailing_comments: Vec::new(),
double_colon_syntax: false,
format: None,
default: None,
inferred_type: None,
}));
}
}
}
Ok(Expression::Function(Box::new(Function::new(
"DATETRUNC".to_string(),
args,
))))
}
// DATEADD is native to SQL Server - uppercase the unit
"DATEADD" => {
let args = Self::uppercase_first_arg_if_identifier(f.args);
Ok(Expression::Function(Box::new(Function::new(
"DATEADD".to_string(),
args,
))))
}
// DATEDIFF is native to SQL Server - uppercase the unit
"DATEDIFF" => {
let args = Self::uppercase_first_arg_if_identifier(f.args);
Ok(Expression::Function(Box::new(Function::new(
"DATEDIFF".to_string(),
args,
))))
}
// EXTRACT -> DATEPART in SQL Server
"EXTRACT" => Ok(Expression::Function(Box::new(Function::new(
"DATEPART".to_string(),
f.args,
)))),
// STRPOS / POSITION -> CHARINDEX
"STRPOS" | "POSITION" if f.args.len() >= 2 => {
// CHARINDEX(substring, string) - same arg order as POSITION
Ok(Expression::Function(Box::new(Function::new(
"CHARINDEX".to_string(),
f.args,
))))
}
// CHARINDEX is native
"CHARINDEX" => Ok(Expression::Function(Box::new(f))),
// CEILING -> CEILING (native)
"CEILING" | "CEIL" if f.args.len() == 1 => Ok(Expression::Function(Box::new(
Function::new("CEILING".to_string(), f.args),
))),
// ARRAY functions don't exist in SQL Server
// Would need JSON or table-valued parameters
// JSON_EXTRACT -> JSON_VALUE or JSON_QUERY
"JSON_EXTRACT" => Ok(Expression::Function(Box::new(Function::new(
"JSON_VALUE".to_string(),
f.args,
)))),
// JSON_EXTRACT_SCALAR -> JSON_VALUE
"JSON_EXTRACT_SCALAR" => Ok(Expression::Function(Box::new(Function::new(
"JSON_VALUE".to_string(),
f.args,
)))),
// PARSE_JSON -> strip in TSQL (just keep the string argument)
"PARSE_JSON" if f.args.len() == 1 => Ok(f.args.into_iter().next().unwrap()),
// GET_PATH(obj, path) -> ISNULL(JSON_QUERY(obj, path), JSON_VALUE(obj, path)) in TSQL
"GET_PATH" if f.args.len() == 2 => {
let mut args = f.args;
let this = args.remove(0);
let path = args.remove(0);
let json_path = match &path {
Expression::Literal(lit) if matches!(lit.as_ref(), Literal::String(_)) => {
let Literal::String(s) = lit.as_ref() else {
unreachable!()
};
let normalized = if s.starts_with('$') {
s.clone()
} else if s.starts_with('[') {
format!("${}", s)
} else {
format!("$.{}", s)
};
Expression::Literal(Box::new(Literal::String(normalized)))
}
_ => path,
};
// ISNULL(JSON_QUERY(obj, path), JSON_VALUE(obj, path))
let json_query = Expression::Function(Box::new(Function::new(
"JSON_QUERY".to_string(),
vec![this.clone(), json_path.clone()],
)));
let json_value = Expression::Function(Box::new(Function::new(
"JSON_VALUE".to_string(),
vec![this, json_path],
)));
Ok(Expression::Function(Box::new(Function::new(
"ISNULL".to_string(),
vec![json_query, json_value],
))))
}
// JSON_QUERY with 1 arg: add '$' path and wrap in ISNULL
// JSON_QUERY with 2 args: leave as-is (already processed or inside ISNULL)
"JSON_QUERY" if f.args.len() == 1 => {
let this = f.args.into_iter().next().unwrap();
let path = Expression::Literal(Box::new(Literal::String("$".to_string())));
let json_query = Expression::Function(Box::new(Function::new(
"JSON_QUERY".to_string(),
vec![this.clone(), path.clone()],
)));
let json_value = Expression::Function(Box::new(Function::new(
"JSON_VALUE".to_string(),
vec![this, path],
)));
Ok(Expression::Function(Box::new(Function::new(
"ISNULL".to_string(),
vec![json_query, json_value],
))))
}
// SPLIT -> STRING_SPLIT (returns a table, needs CROSS APPLY)
"SPLIT" => Ok(Expression::Function(Box::new(Function::new(
"STRING_SPLIT".to_string(),
f.args,
)))),
// REGEXP_LIKE -> Not directly supported, use LIKE or PATINDEX
// SQL Server has limited regex support via PATINDEX and LIKE
"REGEXP_LIKE" => {
// Fall back to LIKE (loses regex functionality)
Ok(Expression::Function(Box::new(Function::new(
"PATINDEX".to_string(),
f.args,
))))
}
// LN -> LOG in SQL Server
"LN" if f.args.len() == 1 => Ok(Expression::Function(Box::new(Function::new(
"LOG".to_string(),
f.args,
)))),
// LOG with 2 args is LOG(base, value) in most DBs but LOG(value, base) in SQL Server
// This needs careful handling
// STDDEV -> STDEV in SQL Server
"STDDEV" | "STDDEV_SAMP" => Ok(Expression::Function(Box::new(Function::new(
"STDEV".to_string(),
f.args,
)))),
// STDDEV_POP -> STDEVP in SQL Server
"STDDEV_POP" => Ok(Expression::Function(Box::new(Function::new(
"STDEVP".to_string(),
f.args,
)))),
// VAR_SAMP -> VAR in SQL Server
"VARIANCE" | "VAR_SAMP" => Ok(Expression::Function(Box::new(Function::new(
"VAR".to_string(),
f.args,
)))),
// VAR_POP -> VARP in SQL Server
"VAR_POP" => Ok(Expression::Function(Box::new(Function::new(
"VARP".to_string(),
f.args,
)))),
// Boolean aggregates -> MIN/MAX over a null-preserving CASE, cast back to BIT.
"BOOL_AND" | "LOGICAL_AND" | "BOOLAND_AGG" | "EVERY" if f.args.len() == 1 => {
let mut args = f.args;
Self::transform_logical_aggregate(args.remove(0), None, "MIN")
}
"BOOL_OR" | "LOGICAL_OR" | "BOOLOR_AGG" if f.args.len() == 1 => {
let mut args = f.args;
Self::transform_logical_aggregate(args.remove(0), None, "MAX")
}
// DATE_ADD(date, interval) -> DATEADD(DAY, interval, date)
"DATE_ADD" => {
if f.args.len() == 2 {
let mut args = f.args;
let date = args.remove(0);
let interval = args.remove(0);
let unit = Expression::Identifier(crate::expressions::Identifier {
name: "DAY".to_string(),
quoted: false,
trailing_comments: Vec::new(),
span: None,
});
Ok(Expression::Function(Box::new(Function::new(
"DATEADD".to_string(),
vec![unit, interval, date],
))))
} else {
let args = Self::uppercase_first_arg_if_identifier(f.args);
Ok(Expression::Function(Box::new(Function::new(
"DATEADD".to_string(),
args,
))))
}
}
// INSERT → STUFF (Snowflake/MySQL string INSERT → T-SQL STUFF)
"INSERT" => Ok(Expression::Function(Box::new(Function::new(
"STUFF".to_string(),
f.args,
)))),
// SUSER_NAME(), SUSER_SNAME(), SYSTEM_USER() -> CURRENT_USER
"SUSER_NAME" | "SUSER_SNAME" | "SYSTEM_USER" => Ok(Expression::CurrentUser(Box::new(
crate::expressions::CurrentUser { this: None },
))),
// Pass through everything else
_ => Ok(Expression::Function(Box::new(f))),
}
}
fn transform_aggregate_function(
&self,
f: Box<crate::expressions::AggregateFunction>,
) -> Result<Expression> {
let name_upper = f.name.to_uppercase();
match name_upper.as_str() {
// GROUP_CONCAT -> STRING_AGG
"GROUP_CONCAT" if !f.args.is_empty() => Ok(Expression::Function(Box::new(
Function::new("STRING_AGG".to_string(), f.args),
))),
// LISTAGG -> STRING_AGG
"LISTAGG" if !f.args.is_empty() => Ok(Expression::Function(Box::new(Function::new(
"STRING_AGG".to_string(),
f.args,
)))),
// ARRAY_AGG -> Not directly supported in SQL Server
// Would need to use FOR XML PATH or STRING_AGG
"ARRAY_AGG" if !f.args.is_empty() => {
// Fall back to STRING_AGG (loses array semantics)
Ok(Expression::Function(Box::new(Function::new(
"STRING_AGG".to_string(),
f.args,
))))
}
// Boolean aggregates -> MIN/MAX over a null-preserving CASE, cast back to BIT.
"BOOL_AND" | "LOGICAL_AND" | "BOOLAND_AGG" | "EVERY" if f.args.len() == 1 => {
let mut args = f.args;
Self::transform_logical_aggregate(args.remove(0), f.filter, "MIN")
}
"BOOL_OR" | "LOGICAL_OR" | "BOOLOR_AGG" if f.args.len() == 1 => {
let mut args = f.args;
Self::transform_logical_aggregate(args.remove(0), f.filter, "MAX")
}
// Pass through everything else
_ => Ok(Expression::AggregateFunction(f)),
}
}
/// Transform CTEs to add auto-aliases to bare expressions in SELECT
/// In TSQL, when a CTE doesn't have explicit column aliases, bare expressions
/// in the SELECT need to be aliased
fn transform_cte(&self, cte: Cte) -> Result<Expression> {
Ok(Expression::Cte(Box::new(self.transform_cte_inner(cte))))
}
/// Inner method to transform a CTE, returning the modified Cte struct
fn transform_cte_inner(&self, mut cte: Cte) -> Cte {
// Only transform if the CTE doesn't have explicit column aliases
// If it has column aliases like `WITH t(a, b) AS (...)`, we don't need to auto-alias
if cte.columns.is_empty() {
cte.this = self.qualify_derived_table_outputs(cte.this);
}
cte
}
/// Transform Subqueries to add auto-aliases to bare expressions in SELECT
/// In TSQL, when a subquery has a table alias but no column aliases,
/// bare expressions need to be aliased
fn transform_subquery(&self, mut subquery: Subquery) -> Result<Expression> {
// Only transform if the subquery has a table alias but no column aliases
// e.g., `(SELECT 1) AS subq` needs aliasing, but `(SELECT 1) AS subq(a)` doesn't
if subquery.alias.is_some() && subquery.column_aliases.is_empty() {
subquery.this = self.qualify_derived_table_outputs(subquery.this);
}
Ok(Expression::Subquery(Box::new(subquery)))
}
/// Add aliases to bare (unaliased) expressions in a SELECT statement
/// This transforms expressions like `SELECT 1` into `SELECT 1 AS [1]`
/// BUT only when the SELECT has no FROM clause (i.e., it's a value expression)
fn qualify_derived_table_outputs(&self, expr: Expression) -> Expression {
match expr {
Expression::Select(mut select) => {
// Only auto-alias if the SELECT has NO from clause
// If there's a FROM clause, column references already have names from the source tables
let has_from = select.from.is_some();
if !has_from {
select.expressions = select
.expressions
.into_iter()
.map(|e| self.maybe_alias_expression(e))
.collect();
}
Expression::Select(select)
}
// For UNION/INTERSECT/EXCEPT, transform the first SELECT
Expression::Union(mut u) => {
let left = std::mem::replace(&mut u.left, Expression::Null(Null));
u.left = self.qualify_derived_table_outputs(left);
Expression::Union(u)
}
Expression::Intersect(mut i) => {
let left = std::mem::replace(&mut i.left, Expression::Null(Null));
i.left = self.qualify_derived_table_outputs(left);
Expression::Intersect(i)
}
Expression::Except(mut e) => {
let left = std::mem::replace(&mut e.left, Expression::Null(Null));
e.left = self.qualify_derived_table_outputs(left);
Expression::Except(e)
}
// Already wrapped in a Subquery (nested), transform the inner
Expression::Subquery(mut s) => {
s.this = self.qualify_derived_table_outputs(s.this);
Expression::Subquery(s)
}
// Pass through anything else
other => other,
}
}
/// Add an alias to a bare expression if needed
/// Returns the expression unchanged if it already has an alias or is a star
/// NOTE: This is only called for SELECTs without a FROM clause, so all bare
/// expressions (including identifiers and columns) need to be aliased.
fn maybe_alias_expression(&self, expr: Expression) -> Expression {
match &expr {
// Already has an alias, leave it alone
Expression::Alias(_) => expr,
// Multiple aliases, leave it alone
Expression::Aliases(_) => expr,
// Star (including qualified star like t.*) doesn't need an alias
Expression::Star(_) => expr,
// When there's no FROM clause (which is the only case when this method is called),
// we need to alias columns and identifiers too since they're standalone values
// that need explicit names for the derived table output.
// Everything else (literals, functions, columns, identifiers, etc.) needs an alias
_ => {
if let Some(output_name) = self.get_output_name(&expr) {
Expression::Alias(Box::new(Alias {
this: expr,
alias: Identifier {
name: output_name,
quoted: true, // Force quoting for TSQL bracket syntax
trailing_comments: Vec::new(),
span: None,
},
column_aliases: Vec::new(),
alias_explicit_as: false,
alias_keyword: None,
pre_alias_comments: Vec::new(),
trailing_comments: Vec::new(),
inferred_type: None,
}))
} else {
// No output name, leave as-is (shouldn't happen for valid expressions)
expr
}
}
}
}
/// Get the "output name" of an expression for auto-aliasing
/// For literals, this is the literal value
/// For columns, this is the column name
fn get_output_name(&self, expr: &Expression) -> Option<String> {
match expr {
// Literals - use the literal value as the name
Expression::Literal(lit) => match lit.as_ref() {
Literal::Number(n) => Some(n.clone()),
Literal::String(s) => Some(s.clone()),
Literal::HexString(h) => Some(format!("0x{}", h)),
Literal::HexNumber(h) => Some(format!("0x{}", h)),
Literal::BitString(b) => Some(format!("b{}", b)),
Literal::ByteString(b) => Some(format!("b'{}'", b)),
Literal::NationalString(s) => Some(format!("N'{}'", s)),
Literal::Date(d) => Some(d.clone()),
Literal::Time(t) => Some(t.clone()),
Literal::Timestamp(ts) => Some(ts.clone()),
Literal::Datetime(dt) => Some(dt.clone()),
Literal::TripleQuotedString(s, _) => Some(s.clone()),
Literal::EscapeString(s) => Some(s.clone()),
Literal::DollarString(s) => Some(s.clone()),
Literal::RawString(s) => Some(s.clone()),
},
// Columns - use the column name
Expression::Column(col) => Some(col.name.name.clone()),
// Identifiers - use the identifier name
Expression::Identifier(ident) => Some(ident.name.clone()),
// Boolean literals
Expression::Boolean(b) => Some(if b.value { "1" } else { "0" }.to_string()),
// NULL
Expression::Null(_) => Some("NULL".to_string()),
// For functions, use the function name as a fallback
Expression::Function(f) => Some(f.name.clone()),
// For aggregates, use the function name
Expression::AggregateFunction(f) => Some(f.name.clone()),
// For other expressions, generate a generic name
_ => Some(format!("_col_{}", 0)),
}
}
/// Helper to uppercase the first argument if it's an identifier or column (for DATEDIFF, DATEADD units)
fn uppercase_first_arg_if_identifier(mut args: Vec<Expression>) -> Vec<Expression> {
use crate::expressions::Identifier;
if !args.is_empty() {
match &args[0] {
Expression::Identifier(id) => {
args[0] = Expression::Identifier(Identifier {
name: id.name.to_uppercase(),
quoted: id.quoted,
trailing_comments: id.trailing_comments.clone(),
span: None,
});
}
Expression::Var(v) => {
args[0] = Expression::Identifier(Identifier {
name: v.this.to_uppercase(),
quoted: false,
trailing_comments: Vec::new(),
span: None,
});
}
Expression::Column(col) if col.table.is_none() => {
args[0] = Expression::Identifier(Identifier {
name: col.name.name.to_uppercase(),
quoted: col.name.quoted,
trailing_comments: col.name.trailing_comments.clone(),
span: None,
});
}
_ => {}
}
}
args
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::dialects::Dialect;
fn transpile_to_tsql(sql: &str) -> String {
let dialect = Dialect::get(DialectType::Generic);
let result = dialect
.transpile(sql, DialectType::TSQL)
.expect("Transpile failed");
result[0].clone()
}
#[test]
fn test_nvl_to_isnull() {
let result = transpile_to_tsql("SELECT NVL(a, b)");
assert!(
result.contains("ISNULL"),
"Expected ISNULL, got: {}",
result
);
}
#[test]
fn test_coalesce_to_isnull() {
let result = transpile_to_tsql("SELECT COALESCE(a, b)");
assert!(
result.contains("ISNULL"),
"Expected ISNULL, got: {}",
result
);
}
#[test]
fn test_basic_select() {
let result = transpile_to_tsql("SELECT a, b FROM users WHERE id = 1");
assert!(result.contains("SELECT"));
assert!(result.contains("FROM users"));
}
#[test]
fn test_length_to_len() {
let result = transpile_to_tsql("SELECT LENGTH(name)");
assert!(result.contains("LEN"), "Expected LEN, got: {}", result);
}
#[test]
fn test_now_to_getdate() {
let result = transpile_to_tsql("SELECT NOW()");
assert!(
result.contains("GETDATE"),
"Expected GETDATE, got: {}",
result
);
}
#[test]
fn test_group_concat_to_string_agg() {
let result = transpile_to_tsql("SELECT GROUP_CONCAT(name)");
assert!(
result.contains("STRING_AGG"),
"Expected STRING_AGG, got: {}",
result
);
}
#[test]
fn test_listagg_to_string_agg() {
let result = transpile_to_tsql("SELECT LISTAGG(name)");
assert!(
result.contains("STRING_AGG"),
"Expected STRING_AGG, got: {}",
result
);
}
#[test]
fn test_ln_to_log() {
let result = transpile_to_tsql("SELECT LN(x)");
assert!(result.contains("LOG"), "Expected LOG, got: {}", result);
}
#[test]
fn test_stddev_to_stdev() {
let result = transpile_to_tsql("SELECT STDDEV(x)");
assert!(result.contains("STDEV"), "Expected STDEV, got: {}", result);
}
#[test]
fn test_bracket_identifiers() {
// SQL Server uses square brackets for identifiers
let dialect = Dialect::get(DialectType::TSQL);
let config = dialect.generator_config();
assert_eq!(config.identifier_quote, '[');
}
#[test]
fn test_json_query_isnull_wrapper_simple() {
// JSON_QUERY with two args needs ISNULL wrapper when transpiling to TSQL
let dialect = Dialect::get(DialectType::TSQL);
let result = dialect
.transpile(r#"JSON_QUERY(x, '$')"#, DialectType::TSQL)
.expect("transpile failed");
assert!(
result[0].contains("ISNULL"),
"JSON_QUERY should be wrapped with ISNULL: {}",
result[0]
);
}
#[test]
fn test_json_query_isnull_wrapper_nested() {
let dialect = Dialect::get(DialectType::TSQL);
let result = dialect
.transpile(
r#"JSON_QUERY(REPLACE(REPLACE(x, '''', '"'), '""', '"'))"#,
DialectType::TSQL,
)
.expect("transpile failed");
let expected = r#"ISNULL(JSON_QUERY(REPLACE(REPLACE(x, '''', '"'), '""', '"'), '$'), JSON_VALUE(REPLACE(REPLACE(x, '''', '"'), '""', '"'), '$'))"#;
assert_eq!(
result[0], expected,
"JSON_QUERY should be wrapped with ISNULL"
);
}
}