datafusion-expr-common 53.1.0

Logical plan and expression representation for DataFusion query engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

//! Function signatures: [`Volatility`], [`Signature`] and [`TypeSignature`]

use std::fmt::Display;
use std::hash::Hash;
use std::sync::Arc;

use crate::type_coercion::aggregates::NUMERICS;
use arrow::datatypes::{
    DECIMAL32_MAX_PRECISION, DECIMAL64_MAX_PRECISION, DECIMAL128_MAX_PRECISION, DataType,
    Decimal128Type, DecimalType, Field, IntervalUnit, TimeUnit,
};
use datafusion_common::types::{LogicalType, LogicalTypeRef, NativeType};
use datafusion_common::utils::ListCoercion;
use datafusion_common::{Result, internal_err, plan_err};
use indexmap::IndexSet;
use itertools::Itertools;

/// Constant that is used as a placeholder for any valid timezone.
/// This is used where a function can accept a timestamp type with any
/// valid timezone, it exists to avoid the need to enumerate all possible
/// timezones. See [`TypeSignature`] for more details.
///
/// Type coercion always ensures that functions will be executed using
/// timestamp arrays that have a valid time zone. Functions must never
/// return results with this timezone.
pub const TIMEZONE_WILDCARD: &str = "+TZ";

/// Constant that is used as a placeholder for any valid fixed size list.
/// This is used where a function can accept a fixed size list type with any
/// valid length. It exists to avoid the need to enumerate all possible fixed size list lengths.
pub const FIXED_SIZE_LIST_WILDCARD: i32 = i32::MIN;

/// How a function's output changes with respect to a fixed input
///
/// The volatility of a function determines eligibility for certain
/// optimizations. You should always define your function to have the strictest
/// possible volatility to maximize performance and avoid unexpected
/// results.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
pub enum Volatility {
    /// Always returns the same output when given the same input.
    ///
    /// DataFusion will inline immutable functions during planning.
    ///
    /// For example, the `abs` function is immutable, so `abs(-1)` will be
    /// evaluated and replaced  with `1` during planning rather than invoking
    /// the function at runtime.
    Immutable,
    /// May return different values given the same input across different
    /// queries but must return the same value for a given input within a query.
    ///
    /// For example, the `now()` function is stable, because the query `select
    /// col1, now() from t1`, will return different results each time it is run,
    /// but within the same query, the output of the `now()` function has the
    /// same value for each output row.
    ///
    /// DataFusion will inline `Stable` functions when possible. For example,
    /// `Stable` functions are inlined when planning a query for execution, but
    /// not in View definitions or prepared statements.
    Stable,
    /// May change the return value from evaluation to evaluation.
    ///
    /// Multiple invocations of a volatile function may return different results
    /// when used in the same query on different rows. An example of this is the
    /// `random()` function.
    ///
    /// DataFusion can not evaluate such functions during planning or push these
    /// predicates into scans. In the query `select col1, random() from t1`,
    /// `random()` function will be evaluated for each output row, resulting in
    /// a unique random value for each row.
    Volatile,
}

/// Represents the arity (number of arguments) of a function signature
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Arity {
    /// Fixed number of arguments
    Fixed(usize),
    /// Variable number of arguments (e.g., Variadic, VariadicAny, UserDefined)
    Variable,
}

/// The types of arguments for which a function has implementations.
///
/// [`TypeSignature`] **DOES NOT** define the types that a user query could call the
/// function with. DataFusion will automatically coerce (cast) argument types to
/// one of the supported function signatures, if possible.
///
/// # Overview
/// Functions typically provide implementations for a small number of different
/// argument [`DataType`]s, rather than all possible combinations. If a user
/// calls a function with arguments that do not match any of the declared types,
/// DataFusion will attempt to automatically coerce (add casts to) function
/// arguments so they match the [`TypeSignature`]. See the [`type_coercion`] module
/// for more details
///
/// # Example: Numeric Functions
/// For example, a function like `cos` may only provide an implementation for
/// [`DataType::Float64`]. When users call `cos` with a different argument type,
/// such as `cos(int_column)`, and type coercion automatically adds a cast such
/// as `cos(CAST int_column AS DOUBLE)` during planning.
///
/// [`type_coercion`]: crate::type_coercion
///
/// ## Example: Strings
///
/// There are several different string types in Arrow, such as
/// [`DataType::Utf8`], [`DataType::LargeUtf8`], and [`DataType::Utf8View`].
///
/// Some functions may have specialized implementations for these types, while others
/// may be able to handle only one of them. For example, a function that
/// only works with [`DataType::Utf8View`] would have the following signature:
///
/// ```
/// # use arrow::datatypes::DataType;
/// # use datafusion_expr_common::signature::{TypeSignature};
/// // Declares the function must be invoked with a single argument of type `Utf8View`.
/// // if a user calls the function with `Utf8` or `LargeUtf8`, DataFusion will
/// // automatically add a cast to `Utf8View` during planning.
/// let type_signature = TypeSignature::Exact(vec![DataType::Utf8View]);
/// ```
///
/// # Example: Timestamps
///
/// Types to match are represented using Arrow's [`DataType`].  [`DataType::Timestamp`] has an optional variable
/// timezone specification. To specify a function can handle a timestamp with *ANY* timezone, use
/// the [`TIMEZONE_WILDCARD`]. For example:
///
/// ```
/// # use arrow::datatypes::{DataType, TimeUnit};
/// # use datafusion_expr_common::signature::{TIMEZONE_WILDCARD, TypeSignature};
/// let type_signature = TypeSignature::Exact(vec![
///     // A nanosecond precision timestamp with ANY timezone
///     // matches  Timestamp(Nanosecond, Some("+0:00"))
///     // matches  Timestamp(Nanosecond, Some("+5:00"))
///     // does not match  Timestamp(Nanosecond, None)
///     DataType::Timestamp(TimeUnit::Nanosecond, Some(TIMEZONE_WILDCARD.into())),
/// ]);
/// ```
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash)]
pub enum TypeSignature {
    /// One or more arguments of a common type out of a list of valid types.
    ///
    /// For functions that take no arguments (e.g. `random()` see [`TypeSignature::Nullary`]).
    ///
    /// # Examples
    ///
    /// A function such as `concat` is `Variadic(vec![DataType::Utf8,
    /// DataType::LargeUtf8])`
    Variadic(Vec<DataType>),
    /// The acceptable signature and coercions rules are special for this
    /// function.
    ///
    /// If this signature is specified,
    /// DataFusion will call [`ScalarUDFImpl::coerce_types`] to prepare argument types.
    ///
    /// [`ScalarUDFImpl::coerce_types`]: https://docs.rs/datafusion/latest/datafusion/logical_expr/trait.ScalarUDFImpl.html#method.coerce_types
    UserDefined,
    /// One or more arguments with arbitrary types
    VariadicAny,
    /// One or more arguments of an arbitrary but equal type out of a list of valid types.
    ///
    /// # Examples
    ///
    /// 1. A function of one argument of f64 is `Uniform(1, vec![DataType::Float64])`
    /// 2. A function of one argument of f64 or f32 is `Uniform(1, vec![DataType::Float32, DataType::Float64])`
    Uniform(usize, Vec<DataType>),
    /// One or more arguments with exactly the specified types in order.
    ///
    /// For functions that take no arguments (e.g. `random()`) use [`TypeSignature::Nullary`].
    Exact(Vec<DataType>),
    /// One or more arguments belonging to the [`TypeSignatureClass`], in order.
    ///
    /// [`Coercion`] contains not only the desired type but also the allowed
    /// casts. For example, if you expect a function has string type, but you
    /// also allow it to be casted from binary type.
    ///
    /// For functions that take no arguments (e.g. `random()`) see [`TypeSignature::Nullary`].
    Coercible(Vec<Coercion>),
    /// One or more arguments coercible to a single, comparable type.
    ///
    /// Each argument will be coerced to a single type using the
    /// coercion rules described in [`comparison_coercion_numeric`].
    ///
    /// # Examples
    ///
    /// If the `nullif(1, 2)` function is called with `i32` and `i64` arguments
    /// the types will both be coerced to `i64` before the function is invoked.
    ///
    /// If the `nullif('1', 2)` function is called with `Utf8` and `i64` arguments
    /// the types will both be coerced to `Utf8` before the function is invoked.
    ///
    /// Note:
    /// - For functions that take no arguments (e.g. `random()` see [`TypeSignature::Nullary`]).
    /// - If all arguments have type [`DataType::Null`], they are coerced to `Utf8`
    ///
    /// [`comparison_coercion_numeric`]: crate::type_coercion::binary::comparison_coercion_numeric
    Comparable(usize),
    /// One or more arguments of arbitrary types.
    ///
    /// For functions that take no arguments (e.g. `random()`) use [`TypeSignature::Nullary`].
    Any(usize),
    /// Matches exactly one of a list of [`TypeSignature`]s.
    ///
    /// Coercion is attempted to match the signatures in order, and stops after
    /// the first success, if any.
    ///
    /// # Examples
    ///
    /// Since `make_array` takes 0 or more arguments with arbitrary types, its `TypeSignature`
    /// is `OneOf(vec![Any(0), VariadicAny])`.
    OneOf(Vec<TypeSignature>),
    /// A function that has an [`ArrayFunctionSignature`]
    ArraySignature(ArrayFunctionSignature),
    /// One or more arguments of numeric types, coerced to a common numeric type.
    ///
    /// See [`NativeType::is_numeric`] to know which type is considered numeric
    ///
    /// For functions that take no arguments (e.g. `random()`) use [`TypeSignature::Nullary`].
    ///
    /// [`NativeType::is_numeric`]: datafusion_common::types::NativeType::is_numeric
    Numeric(usize),
    /// One or arguments of all the same string types.
    ///
    /// The precedence of type from high to low is Utf8View, LargeUtf8 and Utf8.
    /// Null is considered as `Utf8` by default
    /// Dictionary with string value type is also handled.
    ///
    /// For example, if a function is called with (utf8, large_utf8), all
    /// arguments will be coerced to  `LargeUtf8`
    ///
    /// For functions that take no arguments (e.g. `random()` use [`TypeSignature::Nullary`]).
    String(usize),
    /// No arguments
    Nullary,
}

impl TypeSignature {
    #[inline]
    pub fn is_one_of(&self) -> bool {
        matches!(self, TypeSignature::OneOf(_))
    }

    /// Returns the arity (expected number of arguments) for this type signature.
    ///
    /// Returns `Arity::Fixed(n)` for signatures with a specific argument count,
    /// or `Arity::Variable` for variable-arity signatures like `Variadic`, `VariadicAny`, `UserDefined`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use datafusion_expr_common::signature::{TypeSignature, Arity};
    /// # use arrow::datatypes::DataType;
    /// // Exact signature has fixed arity
    /// let sig = TypeSignature::Exact(vec![DataType::Int32, DataType::Utf8]);
    /// assert_eq!(sig.arity(), Arity::Fixed(2));
    ///
    /// // Variadic signature has variable arity
    /// let sig = TypeSignature::VariadicAny;
    /// assert_eq!(sig.arity(), Arity::Variable);
    /// ```
    pub fn arity(&self) -> Arity {
        match self {
            TypeSignature::Exact(types) => Arity::Fixed(types.len()),
            TypeSignature::Uniform(count, _) => Arity::Fixed(*count),
            TypeSignature::Numeric(count) => Arity::Fixed(*count),
            TypeSignature::String(count) => Arity::Fixed(*count),
            TypeSignature::Comparable(count) => Arity::Fixed(*count),
            TypeSignature::Any(count) => Arity::Fixed(*count),
            TypeSignature::Coercible(types) => Arity::Fixed(types.len()),
            TypeSignature::Nullary => Arity::Fixed(0),
            TypeSignature::ArraySignature(ArrayFunctionSignature::Array {
                arguments,
                ..
            }) => Arity::Fixed(arguments.len()),
            TypeSignature::ArraySignature(ArrayFunctionSignature::RecursiveArray) => {
                Arity::Fixed(1)
            }
            TypeSignature::ArraySignature(ArrayFunctionSignature::MapArray) => {
                Arity::Fixed(1)
            }
            TypeSignature::OneOf(variants) => {
                // If any variant is Variable, the whole OneOf is Variable
                let has_variable = variants.iter().any(|v| v.arity() == Arity::Variable);
                if has_variable {
                    return Arity::Variable;
                }
                // Otherwise, get max arity from all fixed arity variants
                let max_arity = variants
                    .iter()
                    .filter_map(|v| match v.arity() {
                        Arity::Fixed(n) => Some(n),
                        Arity::Variable => None,
                    })
                    .max();
                match max_arity {
                    Some(n) => Arity::Fixed(n),
                    None => Arity::Variable,
                }
            }
            TypeSignature::Variadic(_)
            | TypeSignature::VariadicAny
            | TypeSignature::UserDefined => Arity::Variable,
        }
    }
}

/// Represents the class of types that can be used in a function signature.
///
/// This is used to specify what types are valid for function arguments in a more flexible way than
/// just listing specific DataTypes. For example, TypeSignatureClass::Timestamp matches any timestamp
/// type regardless of timezone or precision.
///
/// Used primarily with [`TypeSignature::Coercible`] to define function signatures that can accept
/// arguments that can be coerced to a particular class of types.
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Hash)]
pub enum TypeSignatureClass {
    /// Allows an arbitrary type argument without coercing the argument.
    Any,
    /// Timestamps, allowing arbitrary (or no) timezones
    Timestamp,
    /// All time types
    Time,
    /// All interval types
    Interval,
    /// All duration types
    Duration,
    /// A specific native type
    Native(LogicalTypeRef),
    /// Signed and unsigned integers
    Integer,
    /// All float types
    Float,
    /// All decimal types, allowing arbitrary precision & scale
    Decimal,
    /// Integers, floats and decimals
    Numeric,
    /// Encompasses both the native Binary/LargeBinary types as well as arbitrarily sized FixedSizeBinary types
    Binary,
}

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

impl TypeSignatureClass {
    /// Get example acceptable types for this `TypeSignatureClass`
    ///
    /// This is used for `information_schema` and can be used to generate
    /// documentation or error messages.
    fn get_example_types(&self) -> Vec<DataType> {
        match self {
            // TODO: might be too much info to return every single type here
            //       maybe https://github.com/apache/datafusion/issues/14761 will help here?
            TypeSignatureClass::Any => vec![],
            TypeSignatureClass::Native(l) => get_data_types(l.native()),
            TypeSignatureClass::Timestamp => {
                vec![
                    DataType::Timestamp(TimeUnit::Nanosecond, None),
                    DataType::Timestamp(
                        TimeUnit::Nanosecond,
                        Some(TIMEZONE_WILDCARD.into()),
                    ),
                ]
            }
            TypeSignatureClass::Time => {
                vec![DataType::Time64(TimeUnit::Nanosecond)]
            }
            TypeSignatureClass::Interval => {
                vec![DataType::Interval(IntervalUnit::DayTime)]
            }
            TypeSignatureClass::Duration => {
                vec![DataType::Duration(TimeUnit::Nanosecond)]
            }
            TypeSignatureClass::Integer => {
                vec![DataType::Int64]
            }
            TypeSignatureClass::Binary => {
                vec![DataType::Binary]
            }
            TypeSignatureClass::Decimal => vec![Decimal128Type::DEFAULT_TYPE],
            TypeSignatureClass::Float => vec![DataType::Float64],
            TypeSignatureClass::Numeric => vec![
                DataType::Float64,
                DataType::Int64,
                Decimal128Type::DEFAULT_TYPE,
            ],
        }
    }

    /// Does the specified `NativeType` match this type signature class?
    pub fn matches_native_type(&self, logical_type: &NativeType) -> bool {
        if logical_type == &NativeType::Null {
            return true;
        }

        match self {
            TypeSignatureClass::Any => true,
            TypeSignatureClass::Native(t) if t.native() == logical_type => true,
            TypeSignatureClass::Timestamp if logical_type.is_timestamp() => true,
            TypeSignatureClass::Time if logical_type.is_time() => true,
            TypeSignatureClass::Interval if logical_type.is_interval() => true,
            TypeSignatureClass::Duration if logical_type.is_duration() => true,
            TypeSignatureClass::Integer if logical_type.is_integer() => true,
            TypeSignatureClass::Binary if logical_type.is_binary() => true,
            TypeSignatureClass::Decimal if logical_type.is_decimal() => true,
            TypeSignatureClass::Float if logical_type.is_float() => true,
            TypeSignatureClass::Numeric if logical_type.is_numeric() => true,
            _ => false,
        }
    }

    /// What type would `origin_type` be casted to when casting to the specified native type?
    pub fn default_casted_type(
        &self,
        native_type: &NativeType,
        origin_type: &DataType,
    ) -> Result<DataType> {
        match self {
            TypeSignatureClass::Any => Ok(origin_type.to_owned()),
            TypeSignatureClass::Native(logical_type) => {
                logical_type.native().default_cast_for(origin_type)
            }
            // If the given type is already a timestamp, we don't change the unit and timezone
            TypeSignatureClass::Timestamp if native_type.is_timestamp() => {
                Ok(origin_type.to_owned())
            }
            TypeSignatureClass::Time if native_type.is_time() => {
                Ok(origin_type.to_owned())
            }
            TypeSignatureClass::Interval if native_type.is_interval() => {
                Ok(origin_type.to_owned())
            }
            TypeSignatureClass::Duration if native_type.is_duration() => {
                Ok(origin_type.to_owned())
            }
            TypeSignatureClass::Integer if native_type.is_integer() => {
                Ok(origin_type.to_owned())
            }
            TypeSignatureClass::Binary if native_type.is_binary() => {
                Ok(origin_type.to_owned())
            }
            TypeSignatureClass::Decimal if native_type.is_decimal() => {
                Ok(origin_type.to_owned())
            }
            TypeSignatureClass::Float if native_type.is_float() => {
                Ok(origin_type.to_owned())
            }
            TypeSignatureClass::Numeric if native_type.is_numeric() => {
                Ok(origin_type.to_owned())
            }
            _ if native_type.is_null() => Ok(origin_type.to_owned()),
            _ => internal_err!("May miss the matching logic in `matches_native_type`"),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash)]
pub enum ArrayFunctionSignature {
    /// A function takes at least one List/LargeList/FixedSizeList argument.
    Array {
        /// A full list of the arguments accepted by this function.
        arguments: Vec<ArrayFunctionArgument>,
        /// Additional information about how array arguments should be coerced.
        array_coercion: Option<ListCoercion>,
    },
    /// A function takes a single argument that must be a List/LargeList/FixedSizeList
    /// which gets coerced to List, with element type recursively coerced to List too if it is list-like.
    RecursiveArray,
    /// Specialized Signature for MapArray
    /// The function takes a single argument that must be a MapArray
    MapArray,
}

impl Display for ArrayFunctionSignature {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ArrayFunctionSignature::Array { arguments, .. } => {
                for (idx, argument) in arguments.iter().enumerate() {
                    write!(f, "{argument}")?;
                    if idx != arguments.len() - 1 {
                        write!(f, ", ")?;
                    }
                }
                Ok(())
            }
            ArrayFunctionSignature::RecursiveArray => {
                write!(f, "recursive_array")
            }
            ArrayFunctionSignature::MapArray => {
                write!(f, "map_array")
            }
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash)]
pub enum ArrayFunctionArgument {
    /// A non-list or list argument. The list dimensions should be one less than the Array's list
    /// dimensions.
    Element,
    /// An Int64 index argument.
    Index,
    /// An argument of type List/LargeList/FixedSizeList. All Array arguments must be coercible
    /// to the same type.
    Array,
    // A Utf8 argument.
    String,
}

impl Display for ArrayFunctionArgument {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ArrayFunctionArgument::Element => {
                write!(f, "element")
            }
            ArrayFunctionArgument::Index => {
                write!(f, "index")
            }
            ArrayFunctionArgument::Array => {
                write!(f, "array")
            }
            ArrayFunctionArgument::String => {
                write!(f, "string")
            }
        }
    }
}

impl TypeSignature {
    pub fn to_string_repr(&self) -> Vec<String> {
        match self {
            TypeSignature::Nullary => {
                vec!["NullAry()".to_string()]
            }
            TypeSignature::Variadic(types) => {
                vec![format!("{}, ..", Self::join_types(types, "/"))]
            }
            TypeSignature::Uniform(arg_count, valid_types) => {
                vec![
                    std::iter::repeat_n(Self::join_types(valid_types, "/"), *arg_count)
                        .collect::<Vec<String>>()
                        .join(", "),
                ]
            }
            TypeSignature::String(num) => {
                vec![format!("String({num})")]
            }
            TypeSignature::Numeric(num) => {
                vec![format!("Numeric({num})")]
            }
            TypeSignature::Comparable(num) => {
                vec![format!("Comparable({num})")]
            }
            TypeSignature::Coercible(coercions) => {
                vec![Self::join_types(coercions, ", ")]
            }
            TypeSignature::Exact(types) => {
                vec![Self::join_types(types, ", ")]
            }
            TypeSignature::Any(arg_count) => {
                vec![
                    std::iter::repeat_n("Any", *arg_count)
                        .collect::<Vec<&str>>()
                        .join(", "),
                ]
            }
            TypeSignature::UserDefined => {
                vec!["UserDefined".to_string()]
            }
            TypeSignature::VariadicAny => vec!["Any, .., Any".to_string()],
            TypeSignature::OneOf(sigs) => {
                sigs.iter().flat_map(|s| s.to_string_repr()).collect()
            }
            TypeSignature::ArraySignature(array_signature) => {
                vec![array_signature.to_string()]
            }
        }
    }

    /// Return string representation of the function signature with parameter names.
    ///
    /// This method is similar to [`Self::to_string_repr`] but uses parameter names
    /// instead of types when available. This is useful for generating more helpful
    /// error messages.
    ///
    /// # Arguments
    /// * `parameter_names` - Optional slice of parameter names. When provided, these
    ///   names will be used instead of type names in the output.
    ///
    /// # Examples
    /// ```
    /// # use datafusion_expr_common::signature::TypeSignature;
    /// # use arrow::datatypes::DataType;
    /// let sig = TypeSignature::Exact(vec![DataType::Int32, DataType::Utf8]);
    ///
    /// // Without names: shows types only
    /// assert_eq!(sig.to_string_repr_with_names(None), vec!["Int32, Utf8"]);
    ///
    /// // With names: shows parameter names with types
    /// assert_eq!(
    ///     sig.to_string_repr_with_names(Some(&["id".to_string(), "name".to_string()])),
    ///     vec!["id: Int32, name: Utf8"]
    /// );
    /// ```
    pub fn to_string_repr_with_names(
        &self,
        parameter_names: Option<&[String]>,
    ) -> Vec<String> {
        match self {
            TypeSignature::Exact(types) => {
                if let Some(names) = parameter_names {
                    vec![
                        names
                            .iter()
                            .zip(types.iter())
                            .map(|(name, typ)| format!("{name}: {typ}"))
                            .collect::<Vec<_>>()
                            .join(", "),
                    ]
                } else {
                    vec![Self::join_types(types, ", ")]
                }
            }
            TypeSignature::Any(count) => {
                if let Some(names) = parameter_names {
                    vec![
                        names
                            .iter()
                            .take(*count)
                            .map(|name| format!("{name}: Any"))
                            .collect::<Vec<_>>()
                            .join(", "),
                    ]
                } else {
                    vec![
                        std::iter::repeat_n("Any", *count)
                            .collect::<Vec<&str>>()
                            .join(", "),
                    ]
                }
            }
            TypeSignature::Uniform(count, types) => {
                if let Some(names) = parameter_names {
                    let type_str = Self::join_types(types, "/");
                    vec![
                        names
                            .iter()
                            .take(*count)
                            .map(|name| format!("{name}: {type_str}"))
                            .collect::<Vec<_>>()
                            .join(", "),
                    ]
                } else {
                    self.to_string_repr()
                }
            }
            TypeSignature::Coercible(coercions) => {
                if let Some(names) = parameter_names {
                    vec![
                        names
                            .iter()
                            .zip(coercions.iter())
                            .map(|(name, coercion)| format!("{name}: {coercion}"))
                            .collect::<Vec<_>>()
                            .join(", "),
                    ]
                } else {
                    vec![Self::join_types(coercions, ", ")]
                }
            }
            TypeSignature::Comparable(count) => {
                if let Some(names) = parameter_names {
                    vec![
                        names
                            .iter()
                            .take(*count)
                            .map(|name| format!("{name}: Comparable"))
                            .collect::<Vec<_>>()
                            .join(", "),
                    ]
                } else {
                    self.to_string_repr()
                }
            }
            TypeSignature::Numeric(count) => {
                if let Some(names) = parameter_names {
                    vec![
                        names
                            .iter()
                            .take(*count)
                            .map(|name| format!("{name}: Numeric"))
                            .collect::<Vec<_>>()
                            .join(", "),
                    ]
                } else {
                    self.to_string_repr()
                }
            }
            TypeSignature::String(count) => {
                if let Some(names) = parameter_names {
                    vec![
                        names
                            .iter()
                            .take(*count)
                            .map(|name| format!("{name}: String"))
                            .collect::<Vec<_>>()
                            .join(", "),
                    ]
                } else {
                    self.to_string_repr()
                }
            }
            TypeSignature::Nullary => self.to_string_repr(),
            TypeSignature::ArraySignature(array_sig) => {
                if let Some(names) = parameter_names {
                    match array_sig {
                        ArrayFunctionSignature::Array { arguments, .. } => {
                            vec![
                                names
                                    .iter()
                                    .zip(arguments.iter())
                                    .map(|(name, arg_type)| format!("{name}: {arg_type}"))
                                    .collect::<Vec<_>>()
                                    .join(", "),
                            ]
                        }
                        ArrayFunctionSignature::RecursiveArray => {
                            vec![
                                names
                                    .iter()
                                    .take(1)
                                    .map(|name| format!("{name}: recursive_array"))
                                    .collect::<Vec<_>>()
                                    .join(", "),
                            ]
                        }
                        ArrayFunctionSignature::MapArray => {
                            vec![
                                names
                                    .iter()
                                    .take(1)
                                    .map(|name| format!("{name}: map_array"))
                                    .collect::<Vec<_>>()
                                    .join(", "),
                            ]
                        }
                    }
                } else {
                    self.to_string_repr()
                }
            }
            TypeSignature::OneOf(sigs) => sigs
                .iter()
                .flat_map(|s| s.to_string_repr_with_names(parameter_names))
                .collect(),
            TypeSignature::UserDefined => {
                if let Some(names) = parameter_names {
                    vec![names.join(", ")]
                } else {
                    self.to_string_repr()
                }
            }
            // Variable arity signatures cannot use parameter names
            TypeSignature::Variadic(_) | TypeSignature::VariadicAny => {
                self.to_string_repr()
            }
        }
    }

    /// Helper function to join types with specified delimiter.
    pub fn join_types<T: Display>(types: &[T], delimiter: &str) -> String {
        types
            .iter()
            .map(|t| t.to_string())
            .collect::<Vec<String>>()
            .join(delimiter)
    }

    /// Check whether 0 input argument is valid for given `TypeSignature`
    pub fn supports_zero_argument(&self) -> bool {
        match &self {
            TypeSignature::Exact(vec) => vec.is_empty(),
            TypeSignature::Nullary => true,
            TypeSignature::OneOf(types) => types
                .iter()
                .any(|type_sig| type_sig.supports_zero_argument()),
            _ => false,
        }
    }

    /// Returns true if the signature currently supports or used to supported 0
    /// input arguments in a previous version of DataFusion.
    pub fn used_to_support_zero_arguments(&self) -> bool {
        match &self {
            TypeSignature::Any(num) => *num == 0,
            _ => self.supports_zero_argument(),
        }
    }

    #[deprecated(since = "46.0.0", note = "See get_example_types instead")]
    pub fn get_possible_types(&self) -> Vec<Vec<DataType>> {
        self.get_example_types()
    }

    /// Return example acceptable types for this `TypeSignature`'
    ///
    /// Returns a `Vec<DataType>` for each argument to the function
    ///
    /// This is used for `information_schema` and can be used to generate
    /// documentation or error messages.
    pub fn get_example_types(&self) -> Vec<Vec<DataType>> {
        match self {
            TypeSignature::Exact(types) => vec![types.clone()],
            TypeSignature::OneOf(types) => types
                .iter()
                .flat_map(|type_sig| type_sig.get_example_types())
                .collect(),
            TypeSignature::Uniform(arg_count, types) => types
                .iter()
                .cloned()
                .map(|data_type| vec![data_type; *arg_count])
                .collect(),
            TypeSignature::Coercible(coercions) => coercions
                .iter()
                .map(|c| {
                    let mut all_types: IndexSet<DataType> =
                        c.desired_type().get_example_types().into_iter().collect();

                    if let Some(implicit_coercion) = c.implicit_coercion() {
                        let allowed_casts: Vec<DataType> = implicit_coercion
                            .allowed_source_types
                            .iter()
                            .flat_map(|t| t.get_example_types())
                            .collect();
                        all_types.extend(allowed_casts);
                    }

                    all_types.into_iter().collect::<Vec<_>>()
                })
                .multi_cartesian_product()
                .collect(),
            TypeSignature::Variadic(types) => types
                .iter()
                .cloned()
                .map(|data_type| vec![data_type])
                .collect(),
            TypeSignature::Numeric(arg_count) => NUMERICS
                .iter()
                .cloned()
                .map(|numeric_type| vec![numeric_type; *arg_count])
                .collect(),
            TypeSignature::String(arg_count) => get_data_types(&NativeType::String)
                .into_iter()
                .map(|dt| vec![dt; *arg_count])
                .collect::<Vec<_>>(),
            // TODO: Implement for other types
            TypeSignature::Any(_)
            | TypeSignature::Comparable(_)
            | TypeSignature::Nullary
            | TypeSignature::VariadicAny
            | TypeSignature::ArraySignature(_)
            | TypeSignature::UserDefined => vec![],
        }
    }
}

fn get_data_types(native_type: &NativeType) -> Vec<DataType> {
    match native_type {
        NativeType::Null => vec![DataType::Null],
        NativeType::Boolean => vec![DataType::Boolean],
        NativeType::Int8 => vec![DataType::Int8],
        NativeType::Int16 => vec![DataType::Int16],
        NativeType::Int32 => vec![DataType::Int32],
        NativeType::Int64 => vec![DataType::Int64],
        NativeType::UInt8 => vec![DataType::UInt8],
        NativeType::UInt16 => vec![DataType::UInt16],
        NativeType::UInt32 => vec![DataType::UInt32],
        NativeType::UInt64 => vec![DataType::UInt64],
        NativeType::Float16 => vec![DataType::Float16],
        NativeType::Float32 => vec![DataType::Float32],
        NativeType::Float64 => vec![DataType::Float64],
        NativeType::Date => vec![DataType::Date32, DataType::Date64],
        NativeType::Binary => vec![
            DataType::Binary,
            DataType::LargeBinary,
            DataType::BinaryView,
        ],
        NativeType::String => {
            vec![DataType::Utf8, DataType::LargeUtf8, DataType::Utf8View]
        }
        NativeType::Decimal(precision, scale) => {
            // We assume incoming NativeType is valid already, in terms of precision & scale
            let mut types = vec![DataType::Decimal256(*precision, *scale)];
            if *precision <= DECIMAL32_MAX_PRECISION {
                types.push(DataType::Decimal32(*precision, *scale));
            }
            if *precision <= DECIMAL64_MAX_PRECISION {
                types.push(DataType::Decimal64(*precision, *scale));
            }
            if *precision <= DECIMAL128_MAX_PRECISION {
                types.push(DataType::Decimal128(*precision, *scale));
            }
            types
        }
        NativeType::Timestamp(time_unit, timezone) => {
            vec![DataType::Timestamp(*time_unit, timezone.to_owned())]
        }
        NativeType::Time(TimeUnit::Second) => vec![DataType::Time32(TimeUnit::Second)],
        NativeType::Time(TimeUnit::Millisecond) => {
            vec![DataType::Time32(TimeUnit::Millisecond)]
        }
        NativeType::Time(TimeUnit::Microsecond) => {
            vec![DataType::Time64(TimeUnit::Microsecond)]
        }
        NativeType::Time(TimeUnit::Nanosecond) => {
            vec![DataType::Time64(TimeUnit::Nanosecond)]
        }
        NativeType::Duration(time_unit) => vec![DataType::Duration(*time_unit)],
        NativeType::Interval(interval_unit) => vec![DataType::Interval(*interval_unit)],
        NativeType::FixedSizeBinary(size) => vec![DataType::FixedSizeBinary(*size)],
        NativeType::FixedSizeList(logical_field, size) => {
            get_data_types(logical_field.logical_type.native())
                .iter()
                .map(|child_dt| {
                    let field = Field::new(
                        logical_field.name.clone(),
                        child_dt.clone(),
                        logical_field.nullable,
                    );
                    DataType::FixedSizeList(Arc::new(field), *size)
                })
                .collect()
        }
        // TODO: implement for nested types
        NativeType::List(_)
        | NativeType::Struct(_)
        | NativeType::Union(_)
        | NativeType::Map(_) => {
            vec![]
        }
    }
}

/// Represents type coercion rules for function arguments, specifying both the desired type
/// and optional implicit coercion rules for source types.
///
/// # Examples
///
/// ```
/// use datafusion_common::types::{logical_binary, logical_string, NativeType};
/// use datafusion_expr_common::signature::{Coercion, TypeSignatureClass};
///
/// // Exact coercion that only accepts timestamp types
/// let exact = Coercion::new_exact(TypeSignatureClass::Timestamp);
///
/// // Implicit coercion that accepts string types but can coerce from binary types
/// let implicit = Coercion::new_implicit(
///     TypeSignatureClass::Native(logical_string()),
///     vec![TypeSignatureClass::Native(logical_binary())],
///     NativeType::String,
/// );
/// ```
///
/// There are two variants:
///
/// * `Exact` - Only accepts arguments that exactly match the desired type
/// * `Implicit` - Accepts the desired type and can coerce from specified source types
#[derive(Debug, Clone, Eq, PartialOrd)]
pub enum Coercion {
    /// Coercion that only accepts arguments exactly matching the desired type.
    Exact {
        /// The required type for the argument
        desired_type: TypeSignatureClass,
    },

    /// Coercion that accepts the desired type and can implicitly coerce from other types.
    Implicit {
        /// The primary desired type for the argument
        desired_type: TypeSignatureClass,
        /// Rules for implicit coercion from other types
        implicit_coercion: ImplicitCoercion,
    },
}

impl Coercion {
    pub fn new_exact(desired_type: TypeSignatureClass) -> Self {
        Self::Exact { desired_type }
    }

    /// Create a new coercion with implicit coercion rules.
    ///
    /// `allowed_source_types` defines the possible types that can be coerced to `desired_type`.
    /// `default_casted_type` is the default type to be used for coercion if we cast from other types via `allowed_source_types`.
    pub fn new_implicit(
        desired_type: TypeSignatureClass,
        allowed_source_types: Vec<TypeSignatureClass>,
        default_casted_type: NativeType,
    ) -> Self {
        Self::Implicit {
            desired_type,
            implicit_coercion: ImplicitCoercion {
                allowed_source_types,
                default_casted_type,
            },
        }
    }

    pub fn allowed_source_types(&self) -> &[TypeSignatureClass] {
        match self {
            Coercion::Exact { .. } => &[],
            Coercion::Implicit {
                implicit_coercion, ..
            } => implicit_coercion.allowed_source_types.as_slice(),
        }
    }

    pub fn default_casted_type(&self) -> Option<&NativeType> {
        match self {
            Coercion::Exact { .. } => None,
            Coercion::Implicit {
                implicit_coercion, ..
            } => Some(&implicit_coercion.default_casted_type),
        }
    }

    pub fn desired_type(&self) -> &TypeSignatureClass {
        match self {
            Coercion::Exact { desired_type } => desired_type,
            Coercion::Implicit { desired_type, .. } => desired_type,
        }
    }

    pub fn implicit_coercion(&self) -> Option<&ImplicitCoercion> {
        match self {
            Coercion::Exact { .. } => None,
            Coercion::Implicit {
                implicit_coercion, ..
            } => Some(implicit_coercion),
        }
    }
}

impl Display for Coercion {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Coercion({}", self.desired_type())?;
        if let Some(implicit_coercion) = self.implicit_coercion() {
            write!(f, ", implicit_coercion={implicit_coercion}",)
        } else {
            write!(f, ")")
        }
    }
}

impl PartialEq for Coercion {
    fn eq(&self, other: &Self) -> bool {
        self.desired_type() == other.desired_type()
            && self.implicit_coercion() == other.implicit_coercion()
    }
}

impl Hash for Coercion {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.desired_type().hash(state);
        self.implicit_coercion().hash(state);
    }
}

/// Defines rules for implicit type coercion, specifying which source types can be
/// coerced and the default type to use when coercing.
///
/// This is used by functions to specify which types they can accept via implicit
/// coercion in addition to their primary desired type.
///
/// # Examples
///
/// ```
/// use arrow::datatypes::TimeUnit;
///
/// use datafusion_expr_common::signature::{Coercion, ImplicitCoercion, TypeSignatureClass};
/// use datafusion_common::types::{NativeType, logical_binary};
///
/// // Allow coercing from binary types to timestamp, coerce to specific timestamp unit and timezone
/// let implicit = Coercion::new_implicit(
///     TypeSignatureClass::Timestamp,
///     vec![TypeSignatureClass::Native(logical_binary())],
///     NativeType::Timestamp(TimeUnit::Second, None),
/// );
/// ```
#[derive(Debug, Clone, Eq, PartialOrd)]
pub struct ImplicitCoercion {
    /// The types that can be coerced from via implicit casting
    allowed_source_types: Vec<TypeSignatureClass>,

    /// The default type to use when coercing from allowed source types.
    /// This is particularly important for types like Timestamp that have multiple
    /// possible configurations (different time units and timezones).
    default_casted_type: NativeType,
}

impl Display for ImplicitCoercion {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "ImplicitCoercion({:?}, default_type={:?})",
            self.allowed_source_types, self.default_casted_type
        )
    }
}

impl PartialEq for ImplicitCoercion {
    fn eq(&self, other: &Self) -> bool {
        self.allowed_source_types == other.allowed_source_types
            && self.default_casted_type == other.default_casted_type
    }
}

impl Hash for ImplicitCoercion {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.allowed_source_types.hash(state);
        self.default_casted_type.hash(state);
    }
}

/// Provides  information necessary for calling a function.
///
/// - [`TypeSignature`] defines the argument types that a function has implementations
///   for.
///
/// - [`Volatility`] defines how the output of the function changes with the input.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash)]
pub struct Signature {
    /// The data types that the function accepts. See [TypeSignature] for more information.
    pub type_signature: TypeSignature,
    /// The volatility of the function. See [Volatility] for more information.
    pub volatility: Volatility,
    /// Optional parameter names for the function arguments.
    ///
    /// If provided, enables named argument notation for function calls (e.g., `func(a => 1, b => 2)`).
    /// The length must match the number of arguments defined by `type_signature`.
    ///
    /// Defaults to `None`, meaning only positional arguments are supported.
    pub parameter_names: Option<Vec<String>>,
}

impl Signature {
    /// Creates a new Signature from a given type signature and volatility.
    pub fn new(type_signature: TypeSignature, volatility: Volatility) -> Self {
        Signature {
            type_signature,
            volatility,
            parameter_names: None,
        }
    }
    /// An arbitrary number of arguments with the same type, from those listed in `common_types`.
    pub fn variadic(common_types: Vec<DataType>, volatility: Volatility) -> Self {
        Self {
            type_signature: TypeSignature::Variadic(common_types),
            volatility,
            parameter_names: None,
        }
    }
    /// User-defined coercion rules for the function.
    pub fn user_defined(volatility: Volatility) -> Self {
        Self {
            type_signature: TypeSignature::UserDefined,
            volatility,
            parameter_names: None,
        }
    }

    /// A specified number of numeric arguments
    pub fn numeric(arg_count: usize, volatility: Volatility) -> Self {
        Self {
            type_signature: TypeSignature::Numeric(arg_count),
            volatility,
            parameter_names: None,
        }
    }

    /// A specified number of string arguments
    pub fn string(arg_count: usize, volatility: Volatility) -> Self {
        Self {
            type_signature: TypeSignature::String(arg_count),
            volatility,
            parameter_names: None,
        }
    }

    /// An arbitrary number of arguments of any type.
    pub fn variadic_any(volatility: Volatility) -> Self {
        Self {
            type_signature: TypeSignature::VariadicAny,
            volatility,
            parameter_names: None,
        }
    }
    /// A fixed number of arguments of the same type, from those listed in `valid_types`.
    pub fn uniform(
        arg_count: usize,
        valid_types: Vec<DataType>,
        volatility: Volatility,
    ) -> Self {
        Self {
            type_signature: TypeSignature::Uniform(arg_count, valid_types),
            volatility,
            parameter_names: None,
        }
    }
    /// Exactly matches the types in `exact_types`, in order.
    pub fn exact(exact_types: Vec<DataType>, volatility: Volatility) -> Self {
        Signature {
            type_signature: TypeSignature::Exact(exact_types),
            volatility,
            parameter_names: None,
        }
    }

    /// Target coerce types in order
    pub fn coercible(target_types: Vec<Coercion>, volatility: Volatility) -> Self {
        Self {
            type_signature: TypeSignature::Coercible(target_types),
            volatility,
            parameter_names: None,
        }
    }

    /// Used for function that expects comparable data types, it will try to coerced all the types into single final one.
    pub fn comparable(arg_count: usize, volatility: Volatility) -> Self {
        Self {
            type_signature: TypeSignature::Comparable(arg_count),
            volatility,
            parameter_names: None,
        }
    }

    pub fn nullary(volatility: Volatility) -> Self {
        Signature {
            type_signature: TypeSignature::Nullary,
            volatility,
            parameter_names: None,
        }
    }

    /// A specified number of arguments of any type
    pub fn any(arg_count: usize, volatility: Volatility) -> Self {
        Signature {
            type_signature: TypeSignature::Any(arg_count),
            volatility,
            parameter_names: None,
        }
    }

    /// Any one of a list of [TypeSignature]s.
    pub fn one_of(type_signatures: Vec<TypeSignature>, volatility: Volatility) -> Self {
        Signature {
            type_signature: TypeSignature::OneOf(type_signatures),
            volatility,
            parameter_names: None,
        }
    }

    /// Specialized [Signature] for ArrayAppend and similar functions.
    pub fn array_and_element(volatility: Volatility) -> Self {
        Signature {
            type_signature: TypeSignature::ArraySignature(
                ArrayFunctionSignature::Array {
                    arguments: vec![
                        ArrayFunctionArgument::Array,
                        ArrayFunctionArgument::Element,
                    ],
                    array_coercion: Some(ListCoercion::FixedSizedListToList),
                },
            ),
            volatility,
            parameter_names: None,
        }
    }

    /// Specialized [Signature] for ArrayPrepend and similar functions.
    pub fn element_and_array(volatility: Volatility) -> Self {
        Signature {
            type_signature: TypeSignature::ArraySignature(
                ArrayFunctionSignature::Array {
                    arguments: vec![
                        ArrayFunctionArgument::Element,
                        ArrayFunctionArgument::Array,
                    ],
                    array_coercion: Some(ListCoercion::FixedSizedListToList),
                },
            ),
            volatility,
            parameter_names: None,
        }
    }

    /// Specialized [Signature] for functions that take a fixed number of arrays.
    pub fn arrays(
        n: usize,
        coercion: Option<ListCoercion>,
        volatility: Volatility,
    ) -> Self {
        Signature {
            type_signature: TypeSignature::ArraySignature(
                ArrayFunctionSignature::Array {
                    arguments: vec![ArrayFunctionArgument::Array; n],
                    array_coercion: coercion,
                },
            ),
            volatility,
            parameter_names: None,
        }
    }

    /// Specialized [Signature] for Array functions with an optional index.
    pub fn array_and_element_and_optional_index(volatility: Volatility) -> Self {
        Signature {
            type_signature: TypeSignature::OneOf(vec![
                TypeSignature::ArraySignature(ArrayFunctionSignature::Array {
                    arguments: vec![
                        ArrayFunctionArgument::Array,
                        ArrayFunctionArgument::Element,
                    ],
                    array_coercion: Some(ListCoercion::FixedSizedListToList),
                }),
                TypeSignature::ArraySignature(ArrayFunctionSignature::Array {
                    arguments: vec![
                        ArrayFunctionArgument::Array,
                        ArrayFunctionArgument::Element,
                        ArrayFunctionArgument::Index,
                    ],
                    array_coercion: Some(ListCoercion::FixedSizedListToList),
                }),
            ]),
            volatility,
            parameter_names: None,
        }
    }

    /// Specialized [Signature] for ArrayElement and similar functions.
    pub fn array_and_index(volatility: Volatility) -> Self {
        Signature {
            type_signature: TypeSignature::ArraySignature(
                ArrayFunctionSignature::Array {
                    arguments: vec![
                        ArrayFunctionArgument::Array,
                        ArrayFunctionArgument::Index,
                    ],
                    array_coercion: Some(ListCoercion::FixedSizedListToList),
                },
            ),
            volatility,
            parameter_names: None,
        }
    }

    /// Specialized [Signature] for ArrayEmpty and similar functions.
    pub fn array(volatility: Volatility) -> Self {
        Signature::arrays(1, Some(ListCoercion::FixedSizedListToList), volatility)
    }

    /// Add parameter names to this signature, enabling named argument notation.
    ///
    /// # Example
    /// ```
    /// # use datafusion_expr_common::signature::{Signature, Volatility};
    /// # use arrow::datatypes::DataType;
    /// let sig =
    ///     Signature::exact(vec![DataType::Int32, DataType::Utf8], Volatility::Immutable)
    ///         .with_parameter_names(vec!["count".to_string(), "name".to_string()]);
    /// ```
    ///
    /// # Errors
    /// Returns an error if the number of parameter names doesn't match the signature's arity.
    /// For signatures with variable arity (e.g., `Variadic`, `VariadicAny`), parameter names
    /// cannot be specified.
    pub fn with_parameter_names(mut self, names: Vec<impl Into<String>>) -> Result<Self> {
        let names = names.into_iter().map(Into::into).collect::<Vec<String>>();
        // Validate that the number of names matches the signature
        self.validate_parameter_names(&names)?;
        self.parameter_names = Some(names);
        Ok(self)
    }

    /// Validate that parameter names are compatible with this signature
    fn validate_parameter_names(&self, names: &[String]) -> Result<()> {
        match self.type_signature.arity() {
            Arity::Fixed(expected) => {
                if names.len() != expected {
                    return plan_err!(
                        "Parameter names count ({}) does not match signature arity ({})",
                        names.len(),
                        expected
                    );
                }
            }
            Arity::Variable => {
                // For UserDefined signatures, allow parameter names
                // The function implementer is responsible for validating the names match the actual arguments
                if self.type_signature != TypeSignature::UserDefined {
                    return plan_err!(
                        "Cannot specify parameter names for variable arity signature: {:?}",
                        self.type_signature
                    );
                }
            }
        }

        let mut seen = std::collections::HashSet::new();
        for name in names {
            if !seen.insert(name) {
                return plan_err!("Duplicate parameter name: '{}'", name);
            }
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use datafusion_common::types::{logical_int32, logical_int64, logical_string};

    use super::*;
    use crate::signature::{
        ArrayFunctionArgument, ArrayFunctionSignature, Coercion, TypeSignatureClass,
    };

    #[test]
    fn supports_zero_argument_tests() {
        // Testing `TypeSignature`s which supports 0 arg
        let positive_cases = vec![
            TypeSignature::Exact(vec![]),
            TypeSignature::OneOf(vec![
                TypeSignature::Exact(vec![DataType::Int8]),
                TypeSignature::Nullary,
                TypeSignature::Uniform(1, vec![DataType::Int8]),
            ]),
            TypeSignature::Nullary,
        ];

        for case in positive_cases {
            assert!(
                case.supports_zero_argument(),
                "Expected {case:?} to support zero arguments"
            );
        }

        // Testing `TypeSignature`s which doesn't support 0 arg
        let negative_cases = vec![
            TypeSignature::Exact(vec![DataType::Utf8]),
            TypeSignature::Uniform(1, vec![DataType::Float64]),
            TypeSignature::Any(1),
            TypeSignature::VariadicAny,
            TypeSignature::OneOf(vec![
                TypeSignature::Exact(vec![DataType::Int8]),
                TypeSignature::Uniform(1, vec![DataType::Int8]),
            ]),
        ];

        for case in negative_cases {
            assert!(
                !case.supports_zero_argument(),
                "Expected {case:?} not to support zero arguments"
            );
        }
    }

    #[test]
    fn type_signature_partial_ord() {
        // Test validates that partial ord is defined for TypeSignature and Signature.
        assert!(TypeSignature::UserDefined < TypeSignature::VariadicAny);
        assert!(TypeSignature::UserDefined < TypeSignature::Any(1));

        assert!(
            TypeSignature::Uniform(1, vec![DataType::Null])
                < TypeSignature::Uniform(1, vec![DataType::Boolean])
        );
        assert!(
            TypeSignature::Uniform(1, vec![DataType::Null])
                < TypeSignature::Uniform(2, vec![DataType::Null])
        );
        assert!(
            TypeSignature::Uniform(usize::MAX, vec![DataType::Null])
                < TypeSignature::Exact(vec![DataType::Null])
        );
    }

    #[test]
    fn test_get_possible_types() {
        let type_signature = TypeSignature::Exact(vec![DataType::Int32, DataType::Int64]);
        let possible_types = type_signature.get_example_types();
        assert_eq!(possible_types, vec![vec![DataType::Int32, DataType::Int64]]);

        let type_signature = TypeSignature::OneOf(vec![
            TypeSignature::Exact(vec![DataType::Int32, DataType::Int64]),
            TypeSignature::Exact(vec![DataType::Float32, DataType::Float64]),
        ]);
        let possible_types = type_signature.get_example_types();
        assert_eq!(
            possible_types,
            vec![
                vec![DataType::Int32, DataType::Int64],
                vec![DataType::Float32, DataType::Float64]
            ]
        );

        let type_signature = TypeSignature::OneOf(vec![
            TypeSignature::Exact(vec![DataType::Int32, DataType::Int64]),
            TypeSignature::Exact(vec![DataType::Float32, DataType::Float64]),
            TypeSignature::Exact(vec![DataType::Utf8]),
        ]);
        let possible_types = type_signature.get_example_types();
        assert_eq!(
            possible_types,
            vec![
                vec![DataType::Int32, DataType::Int64],
                vec![DataType::Float32, DataType::Float64],
                vec![DataType::Utf8]
            ]
        );

        let type_signature =
            TypeSignature::Uniform(2, vec![DataType::Float32, DataType::Int64]);
        let possible_types = type_signature.get_example_types();
        assert_eq!(
            possible_types,
            vec![
                vec![DataType::Float32, DataType::Float32],
                vec![DataType::Int64, DataType::Int64]
            ]
        );

        let type_signature = TypeSignature::Coercible(vec![
            Coercion::new_exact(TypeSignatureClass::Native(logical_string())),
            Coercion::new_exact(TypeSignatureClass::Native(logical_int64())),
        ]);
        let possible_types = type_signature.get_example_types();
        assert_eq!(
            possible_types,
            vec![
                vec![DataType::Utf8, DataType::Int64],
                vec![DataType::LargeUtf8, DataType::Int64],
                vec![DataType::Utf8View, DataType::Int64]
            ]
        );

        let type_signature =
            TypeSignature::Variadic(vec![DataType::Int32, DataType::Int64]);
        let possible_types = type_signature.get_example_types();
        assert_eq!(
            possible_types,
            vec![vec![DataType::Int32], vec![DataType::Int64]]
        );

        let type_signature = TypeSignature::Numeric(2);
        let possible_types = type_signature.get_example_types();
        assert_eq!(
            possible_types,
            vec![
                vec![DataType::Int8, DataType::Int8],
                vec![DataType::Int16, DataType::Int16],
                vec![DataType::Int32, DataType::Int32],
                vec![DataType::Int64, DataType::Int64],
                vec![DataType::UInt8, DataType::UInt8],
                vec![DataType::UInt16, DataType::UInt16],
                vec![DataType::UInt32, DataType::UInt32],
                vec![DataType::UInt64, DataType::UInt64],
                vec![DataType::Float16, DataType::Float16],
                vec![DataType::Float32, DataType::Float32],
                vec![DataType::Float64, DataType::Float64]
            ]
        );

        let type_signature = TypeSignature::String(2);
        let possible_types = type_signature.get_example_types();
        assert_eq!(
            possible_types,
            vec![
                vec![DataType::Utf8, DataType::Utf8],
                vec![DataType::LargeUtf8, DataType::LargeUtf8],
                vec![DataType::Utf8View, DataType::Utf8View]
            ]
        );
    }

    #[test]
    fn test_signature_with_parameter_names() {
        let sig = Signature::exact(
            vec![DataType::Int32, DataType::Utf8],
            Volatility::Immutable,
        )
        .with_parameter_names(vec!["count".to_string(), "name".to_string()])
        .unwrap();

        assert_eq!(
            sig.parameter_names,
            Some(vec!["count".to_string(), "name".to_string()])
        );
        assert_eq!(
            sig.type_signature,
            TypeSignature::Exact(vec![DataType::Int32, DataType::Utf8])
        );
    }

    #[test]
    fn test_signature_parameter_names_wrong_count() {
        let result = Signature::exact(
            vec![DataType::Int32, DataType::Utf8],
            Volatility::Immutable,
        )
        .with_parameter_names(vec!["count".to_string()]); // Only 1 name for 2 args

        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("does not match signature arity")
        );
    }

    #[test]
    fn test_signature_parameter_names_duplicate() {
        let result = Signature::exact(
            vec![DataType::Int32, DataType::Int32],
            Volatility::Immutable,
        )
        .with_parameter_names(vec!["count".to_string(), "count".to_string()]);

        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Duplicate parameter name")
        );
    }

    #[test]
    fn test_signature_parameter_names_variadic() {
        let result = Signature::variadic(vec![DataType::Int32], Volatility::Immutable)
            .with_parameter_names(vec!["arg".to_string()]);

        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("variable arity signature")
        );
    }

    #[test]
    fn test_signature_without_parameter_names() {
        let sig = Signature::exact(
            vec![DataType::Int32, DataType::Utf8],
            Volatility::Immutable,
        );

        assert_eq!(sig.parameter_names, None);
    }

    #[test]
    fn test_signature_uniform_with_parameter_names() {
        let sig = Signature::uniform(3, vec![DataType::Float64], Volatility::Immutable)
            .with_parameter_names(vec!["x".to_string(), "y".to_string(), "z".to_string()])
            .unwrap();

        assert_eq!(
            sig.parameter_names,
            Some(vec!["x".to_string(), "y".to_string(), "z".to_string()])
        );
    }

    #[test]
    fn test_signature_numeric_with_parameter_names() {
        let sig = Signature::numeric(2, Volatility::Immutable)
            .with_parameter_names(vec!["a".to_string(), "b".to_string()])
            .unwrap();

        assert_eq!(
            sig.parameter_names,
            Some(vec!["a".to_string(), "b".to_string()])
        );
    }

    #[test]
    fn test_signature_nullary_with_empty_names() {
        let sig = Signature::nullary(Volatility::Immutable)
            .with_parameter_names(Vec::<String>::new())
            .unwrap();

        assert_eq!(sig.parameter_names, Some(vec![]));
    }

    #[test]
    fn test_to_string_repr_with_names_exact() {
        let sig = TypeSignature::Exact(vec![DataType::Int32, DataType::Utf8]);

        assert_eq!(sig.to_string_repr_with_names(None), vec!["Int32, Utf8"]);

        let names = vec!["id".to_string(), "name".to_string()];
        assert_eq!(
            sig.to_string_repr_with_names(Some(&names)),
            vec!["id: Int32, name: Utf8"]
        );
    }

    #[test]
    fn test_to_string_repr_with_names_any() {
        let sig = TypeSignature::Any(3);

        assert_eq!(sig.to_string_repr_with_names(None), vec!["Any, Any, Any"]);

        let names = vec!["x".to_string(), "y".to_string(), "z".to_string()];
        assert_eq!(
            sig.to_string_repr_with_names(Some(&names)),
            vec!["x: Any, y: Any, z: Any"]
        );
    }

    #[test]
    fn test_to_string_repr_with_names_one_of() {
        let sig =
            TypeSignature::OneOf(vec![TypeSignature::Any(2), TypeSignature::Any(3)]);

        assert_eq!(
            sig.to_string_repr_with_names(None),
            vec!["Any, Any", "Any, Any, Any"]
        );

        let names = vec![
            "str".to_string(),
            "start_pos".to_string(),
            "length".to_string(),
        ];
        assert_eq!(
            sig.to_string_repr_with_names(Some(&names)),
            vec![
                "str: Any, start_pos: Any",
                "str: Any, start_pos: Any, length: Any"
            ]
        );
    }

    #[test]
    fn test_to_string_repr_with_names_partial() {
        // This simulates providing max arity names for a OneOf signature
        let sig = TypeSignature::Exact(vec![DataType::Int32, DataType::Utf8]);

        // Provide 3 names for 2-parameter signature (extra name is ignored via zip)
        let names = vec!["a".to_string(), "b".to_string(), "c".to_string()];
        assert_eq!(
            sig.to_string_repr_with_names(Some(&names)),
            vec!["a: Int32, b: Utf8"]
        );
    }

    #[test]
    fn test_to_string_repr_with_names_uniform() {
        let sig = TypeSignature::Uniform(2, vec![DataType::Float64]);

        assert_eq!(
            sig.to_string_repr_with_names(None),
            vec!["Float64, Float64"]
        );

        let names = vec!["x".to_string(), "y".to_string()];
        assert_eq!(
            sig.to_string_repr_with_names(Some(&names)),
            vec!["x: Float64, y: Float64"]
        );
    }

    #[test]
    fn test_to_string_repr_with_names_coercible() {
        let sig = TypeSignature::Coercible(vec![
            Coercion::new_exact(TypeSignatureClass::Native(logical_int32())),
            Coercion::new_exact(TypeSignatureClass::Native(logical_int32())),
        ]);

        let names = vec!["a".to_string(), "b".to_string()];
        let result = sig.to_string_repr_with_names(Some(&names));
        // Check that it contains the parameter names with type annotations
        assert_eq!(result.len(), 1);
        assert!(result[0].starts_with("a: "));
        assert!(result[0].contains(", b: "));
    }

    #[test]
    fn test_to_string_repr_with_names_comparable_numeric_string() {
        let comparable = TypeSignature::Comparable(3);
        let numeric = TypeSignature::Numeric(2);
        let string_sig = TypeSignature::String(2);

        let names = vec!["a".to_string(), "b".to_string(), "c".to_string()];

        // All should show parameter names with type annotations
        assert_eq!(
            comparable.to_string_repr_with_names(Some(&names)),
            vec!["a: Comparable, b: Comparable, c: Comparable"]
        );
        assert_eq!(
            numeric.to_string_repr_with_names(Some(&names)),
            vec!["a: Numeric, b: Numeric"]
        );
        assert_eq!(
            string_sig.to_string_repr_with_names(Some(&names)),
            vec!["a: String, b: String"]
        );
    }

    #[test]
    fn test_to_string_repr_with_names_variadic_fallback() {
        let variadic = TypeSignature::Variadic(vec![DataType::Utf8, DataType::LargeUtf8]);
        let names = vec!["x".to_string()];
        assert_eq!(
            variadic.to_string_repr_with_names(Some(&names)),
            variadic.to_string_repr()
        );

        let variadic_any = TypeSignature::VariadicAny;
        assert_eq!(
            variadic_any.to_string_repr_with_names(Some(&names)),
            variadic_any.to_string_repr()
        );

        // UserDefined now shows parameter names when available
        let user_defined = TypeSignature::UserDefined;
        assert_eq!(
            user_defined.to_string_repr_with_names(Some(&names)),
            vec!["x"]
        );
        assert_eq!(
            user_defined.to_string_repr_with_names(None),
            user_defined.to_string_repr()
        );
    }

    #[test]
    fn test_to_string_repr_with_names_nullary() {
        let sig = TypeSignature::Nullary;
        let names = vec!["x".to_string()];

        // Should return empty representation, names don't apply
        assert_eq!(
            sig.to_string_repr_with_names(Some(&names)),
            vec!["NullAry()"]
        );
        assert_eq!(sig.to_string_repr_with_names(None), vec!["NullAry()"]);
    }

    #[test]
    fn test_to_string_repr_with_names_array_signature() {
        let sig = TypeSignature::ArraySignature(ArrayFunctionSignature::Array {
            arguments: vec![
                ArrayFunctionArgument::Array,
                ArrayFunctionArgument::Index,
                ArrayFunctionArgument::Element,
            ],
            array_coercion: None,
        });

        assert_eq!(
            sig.to_string_repr_with_names(None),
            vec!["array, index, element"]
        );

        let names = vec!["arr".to_string(), "idx".to_string(), "val".to_string()];
        assert_eq!(
            sig.to_string_repr_with_names(Some(&names)),
            vec!["arr: array, idx: index, val: element"]
        );

        let recursive =
            TypeSignature::ArraySignature(ArrayFunctionSignature::RecursiveArray);
        let names = vec!["array".to_string()];
        assert_eq!(
            recursive.to_string_repr_with_names(Some(&names)),
            vec!["array: recursive_array"]
        );

        // Test MapArray (1 argument)
        let map_array = TypeSignature::ArraySignature(ArrayFunctionSignature::MapArray);
        let names = vec!["map".to_string()];
        assert_eq!(
            map_array.to_string_repr_with_names(Some(&names)),
            vec!["map: map_array"]
        );
    }

    #[test]
    fn test_type_signature_arity_exact() {
        let sig = TypeSignature::Exact(vec![DataType::Int32, DataType::Utf8]);
        assert_eq!(sig.arity(), Arity::Fixed(2));

        let sig = TypeSignature::Exact(vec![]);
        assert_eq!(sig.arity(), Arity::Fixed(0));
    }

    #[test]
    fn test_type_signature_arity_uniform() {
        let sig = TypeSignature::Uniform(3, vec![DataType::Float64]);
        assert_eq!(sig.arity(), Arity::Fixed(3));

        let sig = TypeSignature::Uniform(1, vec![DataType::Int32]);
        assert_eq!(sig.arity(), Arity::Fixed(1));
    }

    #[test]
    fn test_type_signature_arity_numeric() {
        let sig = TypeSignature::Numeric(2);
        assert_eq!(sig.arity(), Arity::Fixed(2));
    }

    #[test]
    fn test_type_signature_arity_string() {
        let sig = TypeSignature::String(3);
        assert_eq!(sig.arity(), Arity::Fixed(3));
    }

    #[test]
    fn test_type_signature_arity_comparable() {
        let sig = TypeSignature::Comparable(2);
        assert_eq!(sig.arity(), Arity::Fixed(2));
    }

    #[test]
    fn test_type_signature_arity_any() {
        let sig = TypeSignature::Any(4);
        assert_eq!(sig.arity(), Arity::Fixed(4));
    }

    #[test]
    fn test_type_signature_arity_coercible() {
        let sig = TypeSignature::Coercible(vec![
            Coercion::new_exact(TypeSignatureClass::Native(logical_int32())),
            Coercion::new_exact(TypeSignatureClass::Native(logical_string())),
        ]);
        assert_eq!(sig.arity(), Arity::Fixed(2));
    }

    #[test]
    fn test_type_signature_arity_nullary() {
        let sig = TypeSignature::Nullary;
        assert_eq!(sig.arity(), Arity::Fixed(0));
    }

    #[test]
    fn test_type_signature_arity_array_signature() {
        // Test Array variant with 2 arguments
        let sig = TypeSignature::ArraySignature(ArrayFunctionSignature::Array {
            arguments: vec![ArrayFunctionArgument::Array, ArrayFunctionArgument::Index],
            array_coercion: None,
        });
        assert_eq!(sig.arity(), Arity::Fixed(2));

        // Test Array variant with 3 arguments
        let sig = TypeSignature::ArraySignature(ArrayFunctionSignature::Array {
            arguments: vec![
                ArrayFunctionArgument::Array,
                ArrayFunctionArgument::Element,
                ArrayFunctionArgument::Index,
            ],
            array_coercion: None,
        });
        assert_eq!(sig.arity(), Arity::Fixed(3));

        // Test RecursiveArray variant
        let sig = TypeSignature::ArraySignature(ArrayFunctionSignature::RecursiveArray);
        assert_eq!(sig.arity(), Arity::Fixed(1));

        // Test MapArray variant
        let sig = TypeSignature::ArraySignature(ArrayFunctionSignature::MapArray);
        assert_eq!(sig.arity(), Arity::Fixed(1));
    }

    #[test]
    fn test_type_signature_arity_one_of_fixed() {
        // OneOf with all fixed arity variants should return max arity
        let sig = TypeSignature::OneOf(vec![
            TypeSignature::Exact(vec![DataType::Int32]),
            TypeSignature::Exact(vec![DataType::Int32, DataType::Utf8]),
            TypeSignature::Exact(vec![
                DataType::Int32,
                DataType::Utf8,
                DataType::Float64,
            ]),
        ]);
        assert_eq!(sig.arity(), Arity::Fixed(3));
    }

    #[test]
    fn test_type_signature_arity_one_of_variable() {
        // OneOf with variable arity variant should return Variable
        let sig = TypeSignature::OneOf(vec![
            TypeSignature::Exact(vec![DataType::Int32]),
            TypeSignature::VariadicAny,
        ]);
        assert_eq!(sig.arity(), Arity::Variable);
    }

    #[test]
    fn test_type_signature_arity_variadic() {
        let sig = TypeSignature::Variadic(vec![DataType::Int32]);
        assert_eq!(sig.arity(), Arity::Variable);

        let sig = TypeSignature::VariadicAny;
        assert_eq!(sig.arity(), Arity::Variable);
    }

    #[test]
    fn test_type_signature_arity_user_defined() {
        let sig = TypeSignature::UserDefined;
        assert_eq!(sig.arity(), Arity::Variable);
    }
}