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

select polyf(point(3,4));  -- fail for lack of + operator
ERROR:  operator does not exist: point + integer
LINE 2:   select x + 1
                   ^
HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.
QUERY:  
  select x + 1

CONTEXT:  SQL function "polyf" during inlining
drop function polyf(x anyelement);
create function polyf(x anyelement) returns anyarray as $$
  select array[x + 1, x + 2]
$$ language sql;
select polyf(42) as int, polyf(4.5) as num;
   int   |    num    
---------+-----------
 {43,44} | {5.5,6.5}
(1 row)

drop function polyf(x anyelement);
create function polyf(x anyarray) returns anyelement as $$
  select x[1]
$$ language sql;
select polyf(array[2,4]) as int, polyf(array[4.5, 7.7]) as num;
 int | num 
-----+-----
   2 | 4.5
(1 row)

select polyf(stavalues1) from pg_statistic;  -- fail, can't infer element type
ERROR:  cannot determine element type of "anyarray" argument
drop function polyf(x anyarray);
create function polyf(x anyarray) returns anyarray as $$
  select x
$$ language sql;
select polyf(array[2,4]) as int, polyf(array[4.5, 7.7]) as num;
  int  |    num    
-------+-----------
 {2,4} | {4.5,7.7}
(1 row)

select polyf(stavalues1) from pg_statistic;  -- fail, can't infer element type
ERROR:  return type anyarray is not supported for SQL functions
CONTEXT:  SQL function "polyf" during inlining
drop function polyf(x anyarray);
-- fail, can't infer type:
create function polyf(x anyelement) returns anyrange as $$
  select array[x + 1, x + 2]
$$ language sql;
ERROR:  cannot determine result data type
DETAIL:  A result of type anyrange requires at least one input of type anyrange or anymultirange.
create function polyf(x anyrange) returns anyarray as $$
  select array[lower(x), upper(x)]
$$ language sql;
select polyf(int4range(42, 49)) as int, polyf(float8range(4.5, 7.8)) as num;
   int   |    num    
---------+-----------
 {42,49} | {4.5,7.8}
(1 row)

drop function polyf(x anyrange);
create function polyf(x anycompatible, y anycompatible) returns anycompatiblearray as $$
  select array[x, y]
$$ language sql;
select polyf(2, 4) as int, polyf(2, 4.5) as num;
  int  |   num   
-------+---------
 {2,4} | {2,4.5}
(1 row)

drop function polyf(x anycompatible, y anycompatible);
create function polyf(x anycompatiblerange, y anycompatible, z anycompatible) returns anycompatiblearray as $$
  select array[lower(x), upper(x), y, z]
$$ language sql;
select polyf(int4range(42, 49), 11, 2::smallint) as int, polyf(float8range(4.5, 7.8), 7.8, 11::real) as num;
     int      |       num        
--------------+------------------
 {42,49,11,2} | {4.5,7.8,7.8,11}
(1 row)

select polyf(int4range(42, 49), 11, 4.5) as fail;  -- range type doesn't fit
ERROR:  function polyf(int4range, integer, numeric) does not exist
LINE 1: select polyf(int4range(42, 49), 11, 4.5) as fail;
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
drop function polyf(x anycompatiblerange, y anycompatible, z anycompatible);
create function polyf(x anycompatiblemultirange, y anycompatible, z anycompatible) returns anycompatiblearray as $$
  select array[lower(x), upper(x), y, z]
$$ language sql;
select polyf(multirange(int4range(42, 49)), 11, 2::smallint) as int, polyf(multirange(float8range(4.5, 7.8)), 7.8, 11::real) as num;
     int      |       num        
--------------+------------------
 {42,49,11,2} | {4.5,7.8,7.8,11}
(1 row)

select polyf(multirange(int4range(42, 49)), 11, 4.5) as fail;  -- range type doesn't fit
ERROR:  function polyf(int4multirange, integer, numeric) does not exist
LINE 1: select polyf(multirange(int4range(42, 49)), 11, 4.5) as fail...
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
drop function polyf(x anycompatiblemultirange, y anycompatible, z anycompatible);
-- fail, can't infer type:
create function polyf(x anycompatible) returns anycompatiblerange as $$
  select array[x + 1, x + 2]
$$ language sql;
ERROR:  cannot determine result data type
DETAIL:  A result of type anycompatiblerange requires at least one input of type anycompatiblerange or anycompatiblemultirange.
create function polyf(x anycompatiblerange, y anycompatiblearray) returns anycompatiblerange as $$
  select x
$$ language sql;
select polyf(int4range(42, 49), array[11]) as int, polyf(float8range(4.5, 7.8), array[7]) as num;
   int   |    num    
---------+-----------
 [42,49) | [4.5,7.8)
(1 row)

drop function polyf(x anycompatiblerange, y anycompatiblearray);
-- fail, can't infer type:
create function polyf(x anycompatible) returns anycompatiblemultirange as $$
  select array[x + 1, x + 2]
$$ language sql;
ERROR:  cannot determine result data type
DETAIL:  A result of type anycompatiblemultirange requires at least one input of type anycompatiblerange or anycompatiblemultirange.
create function polyf(x anycompatiblemultirange, y anycompatiblearray) returns anycompatiblemultirange as $$
  select x
$$ language sql;
select polyf(multirange(int4range(42, 49)), array[11]) as int, polyf(multirange(float8range(4.5, 7.8)), array[7]) as num;
    int    |     num     
-----------+-------------
 {[42,49)} | {[4.5,7.8)}
(1 row)

drop function polyf(x anycompatiblemultirange, y anycompatiblearray);
create function polyf(a anyelement, b anyarray,
                      c anycompatible, d anycompatible,
                      OUT x anyarray, OUT y anycompatiblearray)
as $$
  select a || b, array[c, d]
$$ language sql;
select x, pg_typeof(x), y, pg_typeof(y)
  from polyf(11, array[1, 2], 42, 34.5);
    x     | pg_typeof |     y     | pg_typeof 
----------+-----------+-----------+-----------
 {11,1,2} | integer[] | {42,34.5} | numeric[]
(1 row)

select x, pg_typeof(x), y, pg_typeof(y)
  from polyf(11, array[1, 2], point(1,2), point(3,4));
    x     | pg_typeof |         y         | pg_typeof 
----------+-----------+-------------------+-----------
 {11,1,2} | integer[] | {"(1,2)","(3,4)"} | point[]
(1 row)

select x, pg_typeof(x), y, pg_typeof(y)
  from polyf(11, '{1,2}', point(1,2), '(3,4)');
    x     | pg_typeof |         y         | pg_typeof 
----------+-----------+-------------------+-----------
 {11,1,2} | integer[] | {"(1,2)","(3,4)"} | point[]
(1 row)

select x, pg_typeof(x), y, pg_typeof(y)
  from polyf(11, array[1, 2.2], 42, 34.5);  -- fail
ERROR:  function polyf(integer, numeric[], integer, numeric) does not exist
LINE 2:   from polyf(11, array[1, 2.2], 42, 34.5);
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
drop function polyf(a anyelement, b anyarray,
                    c anycompatible, d anycompatible);
create function polyf(anyrange) returns anymultirange
as 'select multirange($1);' language sql;
select polyf(int4range(1,10));
  polyf   
----------
 {[1,10)}
(1 row)

select polyf(null);
ERROR:  could not determine polymorphic type because input has type unknown
drop function polyf(anyrange);
create function polyf(anymultirange) returns anyelement
as 'select lower($1);' language sql;
select polyf(int4multirange(int4range(1,10), int4range(20,30)));
 polyf 
-------
     1
(1 row)

select polyf(null);
ERROR:  could not determine polymorphic type because input has type unknown
drop function polyf(anymultirange);
create function polyf(anycompatiblerange) returns anycompatiblemultirange
as 'select multirange($1);' language sql;
select polyf(int4range(1,10));
  polyf   
----------
 {[1,10)}
(1 row)

select polyf(null);
ERROR:  could not determine polymorphic type anycompatiblerange because input has type unknown
drop function polyf(anycompatiblerange);
create function polyf(anymultirange) returns anyrange
as 'select range_merge($1);' language sql;
select polyf(int4multirange(int4range(1,10), int4range(20,30)));
 polyf  
--------
 [1,30)
(1 row)

select polyf(null);
ERROR:  could not determine polymorphic type because input has type unknown
drop function polyf(anymultirange);
create function polyf(anycompatiblemultirange) returns anycompatiblerange
as 'select range_merge($1);' language sql;
select polyf(int4multirange(int4range(1,10), int4range(20,30)));
 polyf  
--------
 [1,30)
(1 row)

select polyf(null);
ERROR:  could not determine polymorphic type anycompatiblerange because input has type unknown
drop function polyf(anycompatiblemultirange);
create function polyf(anycompatiblemultirange) returns anycompatible
as 'select lower($1);' language sql;
select polyf(int4multirange(int4range(1,10), int4range(20,30)));
 polyf 
-------
     1
(1 row)

select polyf(null);
ERROR:  could not determine polymorphic type anycompatiblemultirange because input has type unknown
drop function polyf(anycompatiblemultirange);
--
-- Polymorphic aggregate tests
--
-- Legend:
-----------
-- A = type is ANY
-- P = type is polymorphic
-- N = type is non-polymorphic
-- B = aggregate base type
-- S = aggregate state type
-- R = aggregate return type
-- 1 = arg1 of a function
-- 2 = arg2 of a function
-- ag = aggregate
-- tf = trans (state) function
-- ff = final function
-- rt = return type of a function
-- -> = implies
-- => = allowed
-- !> = not allowed
-- E  = exists
-- NE = not-exists
--
-- Possible states:
-- ----------------
-- B = (A || P || N)
--   when (B = A) -> (tf2 = NE)
-- S = (P || N)
-- ff = (E || NE)
-- tf1 = (P || N)
-- tf2 = (NE || P || N)
-- R = (P || N)
-- create functions for use as tf and ff with the needed combinations of
-- argument polymorphism, but within the constraints of valid aggregate
-- functions, i.e. tf arg1 and tf return type must match
-- polymorphic single arg transfn
CREATE FUNCTION stfp(anyarray) RETURNS anyarray AS
'select $1' LANGUAGE SQL;
-- non-polymorphic single arg transfn
CREATE FUNCTION stfnp(int[]) RETURNS int[] AS
'select $1' LANGUAGE SQL;
-- dual polymorphic transfn
CREATE FUNCTION tfp(anyarray,anyelement) RETURNS anyarray AS
'select $1 || $2' LANGUAGE SQL;
-- dual non-polymorphic transfn
CREATE FUNCTION tfnp(int[],int) RETURNS int[] AS
'select $1 || $2' LANGUAGE SQL;
-- arg1 only polymorphic transfn
CREATE FUNCTION tf1p(anyarray,int) RETURNS anyarray AS
'select $1' LANGUAGE SQL;
-- arg2 only polymorphic transfn
CREATE FUNCTION tf2p(int[],anyelement) RETURNS int[] AS
'select $1' LANGUAGE SQL;
-- multi-arg polymorphic
CREATE FUNCTION sum3(anyelement,anyelement,anyelement) returns anyelement AS
'select $1+$2+$3' language sql strict;
-- finalfn polymorphic
CREATE FUNCTION ffp(anyarray) RETURNS anyarray AS
'select $1' LANGUAGE SQL;
-- finalfn non-polymorphic
CREATE FUNCTION ffnp(int[]) returns int[] as
'select $1' LANGUAGE SQL;
-- Try to cover all the possible states:
--
-- Note: in Cases 1 & 2, we are trying to return P. Therefore, if the transfn
-- is stfnp, tfnp, or tf2p, we must use ffp as finalfn, because stfnp, tfnp,
-- and tf2p do not return P. Conversely, in Cases 3 & 4, we are trying to
-- return N. Therefore, if the transfn is stfp, tfp, or tf1p, we must use ffnp
-- as finalfn, because stfp, tfp, and tf1p do not return N.
--
--     Case1 (R = P) && (B = A)
--     ------------------------
--     S    tf1
--     -------
--     N    N
-- should CREATE
CREATE AGGREGATE myaggp01a(*) (SFUNC = stfnp, STYPE = int4[],
  FINALFUNC = ffp, INITCOND = '{}');
--     P    N
-- should ERROR: stfnp(anyarray) not matched by stfnp(int[])
CREATE AGGREGATE myaggp02a(*) (SFUNC = stfnp, STYPE = anyarray,
  FINALFUNC = ffp, INITCOND = '{}');
ERROR:  cannot determine transition data type
DETAIL:  A result of type anyarray requires at least one input of type anyelement, anyarray, anynonarray, anyenum, anyrange, or anymultirange.
--     N    P
-- should CREATE
CREATE AGGREGATE myaggp03a(*) (SFUNC = stfp, STYPE = int4[],
  FINALFUNC = ffp, INITCOND = '{}');
CREATE AGGREGATE myaggp03b(*) (SFUNC = stfp, STYPE = int4[],
  INITCOND = '{}');
--     P    P
-- should ERROR: we have no way to resolve S
CREATE AGGREGATE myaggp04a(*) (SFUNC = stfp, STYPE = anyarray,
  FINALFUNC = ffp, INITCOND = '{}');
ERROR:  cannot determine transition data type
DETAIL:  A result of type anyarray requires at least one input of type anyelement, anyarray, anynonarray, anyenum, anyrange, or anymultirange.
CREATE AGGREGATE myaggp04b(*) (SFUNC = stfp, STYPE = anyarray,
  INITCOND = '{}');
ERROR:  cannot determine transition data type
DETAIL:  A result of type anyarray requires at least one input of type anyelement, anyarray, anynonarray, anyenum, anyrange, or anymultirange.
--    Case2 (R = P) && ((B = P) || (B = N))
--    -------------------------------------
--    S    tf1      B    tf2
--    -----------------------
--    N    N        N    N
-- should CREATE
CREATE AGGREGATE myaggp05a(BASETYPE = int, SFUNC = tfnp, STYPE = int[],
  FINALFUNC = ffp, INITCOND = '{}');
--    N    N        N    P
-- should CREATE
CREATE AGGREGATE myaggp06a(BASETYPE = int, SFUNC = tf2p, STYPE = int[],
  FINALFUNC = ffp, INITCOND = '{}');
--    N    N        P    N
-- should ERROR: tfnp(int[], anyelement) not matched by tfnp(int[], int)
CREATE AGGREGATE myaggp07a(BASETYPE = anyelement, SFUNC = tfnp, STYPE = int[],
  FINALFUNC = ffp, INITCOND = '{}');
ERROR:  function tfnp(integer[], anyelement) does not exist
--    N    N        P    P
-- should CREATE
CREATE AGGREGATE myaggp08a(BASETYPE = anyelement, SFUNC = tf2p, STYPE = int[],
  FINALFUNC = ffp, INITCOND = '{}');
--    N    P        N    N
-- should CREATE
CREATE AGGREGATE myaggp09a(BASETYPE = int, SFUNC = tf1p, STYPE = int[],
  FINALFUNC = ffp, INITCOND = '{}');
CREATE AGGREGATE myaggp09b(BASETYPE = int, SFUNC = tf1p, STYPE = int[],
  INITCOND = '{}');
--    N    P        N    P
-- should CREATE
CREATE AGGREGATE myaggp10a(BASETYPE = int, SFUNC = tfp, STYPE = int[],
  FINALFUNC = ffp, INITCOND = '{}');
CREATE AGGREGATE myaggp10b(BASETYPE = int, SFUNC = tfp, STYPE = int[],
  INITCOND = '{}');
--    N    P        P    N
-- should ERROR: tf1p(int[],anyelement) not matched by tf1p(anyarray,int)
CREATE AGGREGATE myaggp11a(BASETYPE = anyelement, SFUNC = tf1p, STYPE = int[],
  FINALFUNC = ffp, INITCOND = '{}');
ERROR:  function tf1p(integer[], anyelement) does not exist
CREATE AGGREGATE myaggp11b(BASETYPE = anyelement, SFUNC = tf1p, STYPE = int[],
  INITCOND = '{}');
ERROR:  function tf1p(integer[], anyelement) does not exist
--    N    P        P    P
-- should ERROR: tfp(int[],anyelement) not matched by tfp(anyarray,anyelement)
CREATE AGGREGATE myaggp12a(BASETYPE = anyelement, SFUNC = tfp, STYPE = int[],
  FINALFUNC = ffp, INITCOND = '{}');
ERROR:  function tfp(integer[], anyelement) does not exist
CREATE AGGREGATE myaggp12b(BASETYPE = anyelement, SFUNC = tfp, STYPE = int[],
  INITCOND = '{}');
ERROR:  function tfp(integer[], anyelement) does not exist
--    P    N        N    N
-- should ERROR: tfnp(anyarray, int) not matched by tfnp(int[],int)
CREATE AGGREGATE myaggp13a(BASETYPE = int, SFUNC = tfnp, STYPE = anyarray,
  FINALFUNC = ffp, INITCOND = '{}');
ERROR:  cannot determine transition data type
DETAIL:  A result of type anyarray requires at least one input of type anyelement, anyarray, anynonarray, anyenum, anyrange, or anymultirange.
--    P    N        N    P
-- should ERROR: tf2p(anyarray, int) not matched by tf2p(int[],anyelement)
CREATE AGGREGATE myaggp14a(BASETYPE = int, SFUNC = tf2p, STYPE = anyarray,
  FINALFUNC = ffp, INITCOND = '{}');
ERROR:  cannot determine transition data type
DETAIL:  A result of type anyarray requires at least one input of type anyelement, anyarray, anynonarray, anyenum, anyrange, or anymultirange.
--    P    N        P    N
-- should ERROR: tfnp(anyarray, anyelement) not matched by tfnp(int[],int)
CREATE AGGREGATE myaggp15a(BASETYPE = anyelement, SFUNC = tfnp,
  STYPE = anyarray, FINALFUNC = ffp, INITCOND = '{}');
ERROR:  function tfnp(anyarray, anyelement) does not exist
--    P    N        P    P
-- should ERROR: tf2p(anyarray, anyelement) not matched by tf2p(int[],anyelement)
CREATE AGGREGATE myaggp16a(BASETYPE = anyelement, SFUNC = tf2p,
  STYPE = anyarray, FINALFUNC = ffp, INITCOND = '{}');
ERROR:  function tf2p(anyarray, anyelement) does not exist
--    P    P        N    N
-- should ERROR: we have no way to resolve S
CREATE AGGREGATE myaggp17a(BASETYPE = int, SFUNC = tf1p, STYPE = anyarray,
  FINALFUNC = ffp, INITCOND = '{}');
ERROR:  cannot determine transition data type
DETAIL:  A result of type anyarray requires at least one input of type anyelement, anyarray, anynonarray, anyenum, anyrange, or anymultirange.
CREATE AGGREGATE myaggp17b(BASETYPE = int, SFUNC = tf1p, STYPE = anyarray,
  INITCOND = '{}');
ERROR:  cannot determine transition data type
DETAIL:  A result of type anyarray requires at least one input of type anyelement, anyarray, anynonarray, anyenum, anyrange, or anymultirange.
--    P    P        N    P
-- should ERROR: tfp(anyarray, int) not matched by tfp(anyarray, anyelement)
CREATE AGGREGATE myaggp18a(BASETYPE = int, SFUNC = tfp, STYPE = anyarray,
  FINALFUNC = ffp, INITCOND = '{}');
ERROR:  cannot determine transition data type
DETAIL:  A result of type anyarray requires at least one input of type anyelement, anyarray, anynonarray, anyenum, anyrange, or anymultirange.
CREATE AGGREGATE myaggp18b(BASETYPE = int, SFUNC = tfp, STYPE = anyarray,
  INITCOND = '{}');
ERROR:  cannot determine transition data type
DETAIL:  A result of type anyarray requires at least one input of type anyelement, anyarray, anynonarray, anyenum, anyrange, or anymultirange.
--    P    P        P    N
-- should ERROR: tf1p(anyarray, anyelement) not matched by tf1p(anyarray, int)
CREATE AGGREGATE myaggp19a(BASETYPE = anyelement, SFUNC = tf1p,
  STYPE = anyarray, FINALFUNC = ffp, INITCOND = '{}');
ERROR:  function tf1p(anyarray, anyelement) does not exist
CREATE AGGREGATE myaggp19b(BASETYPE = anyelement, SFUNC = tf1p,
  STYPE = anyarray, INITCOND = '{}');
ERROR:  function tf1p(anyarray, anyelement) does not exist
--    P    P        P    P
-- should CREATE
CREATE AGGREGATE myaggp20a(BASETYPE = anyelement, SFUNC = tfp,
  STYPE = anyarray, FINALFUNC = ffp, INITCOND = '{}');
CREATE AGGREGATE myaggp20b(BASETYPE = anyelement, SFUNC = tfp,
  STYPE = anyarray, INITCOND = '{}');
--     Case3 (R = N) && (B = A)
--     ------------------------
--     S    tf1
--     -------
--     N    N
-- should CREATE
CREATE AGGREGATE myaggn01a(*) (SFUNC = stfnp, STYPE = int4[],
  FINALFUNC = ffnp, INITCOND = '{}');
CREATE AGGREGATE myaggn01b(*) (SFUNC = stfnp, STYPE = int4[],
  INITCOND = '{}');
--     P    N
-- should ERROR: stfnp(anyarray) not matched by stfnp(int[])
CREATE AGGREGATE myaggn02a(*) (SFUNC = stfnp, STYPE = anyarray,
  FINALFUNC = ffnp, INITCOND = '{}');
ERROR:  cannot determine transition data type
DETAIL:  A result of type anyarray requires at least one input of type anyelement, anyarray, anynonarray, anyenum, anyrange, or anymultirange.
CREATE AGGREGATE myaggn02b(*) (SFUNC = stfnp, STYPE = anyarray,
  INITCOND = '{}');
ERROR:  cannot determine transition data type
DETAIL:  A result of type anyarray requires at least one input of type anyelement, anyarray, anynonarray, anyenum, anyrange, or anymultirange.
--     N    P
-- should CREATE
CREATE AGGREGATE myaggn03a(*) (SFUNC = stfp, STYPE = int4[],
  FINALFUNC = ffnp, INITCOND = '{}');
--     P    P
-- should ERROR: ffnp(anyarray) not matched by ffnp(int[])
CREATE AGGREGATE myaggn04a(*) (SFUNC = stfp, STYPE = anyarray,
  FINALFUNC = ffnp, INITCOND = '{}');
ERROR:  cannot determine transition data type
DETAIL:  A result of type anyarray requires at least one input of type anyelement, anyarray, anynonarray, anyenum, anyrange, or anymultirange.
--    Case4 (R = N) && ((B = P) || (B = N))
--    -------------------------------------
--    S    tf1      B    tf2
--    -----------------------
--    N    N        N    N
-- should CREATE
CREATE AGGREGATE myaggn05a(BASETYPE = int, SFUNC = tfnp, STYPE = int[],
  FINALFUNC = ffnp, INITCOND = '{}');
CREATE AGGREGATE myaggn05b(BASETYPE = int, SFUNC = tfnp, STYPE = int[],
  INITCOND = '{}');
--    N    N        N    P
-- should CREATE
CREATE AGGREGATE myaggn06a(BASETYPE = int, SFUNC = tf2p, STYPE = int[],
  FINALFUNC = ffnp, INITCOND = '{}');
CREATE AGGREGATE myaggn06b(BASETYPE = int, SFUNC = tf2p, STYPE = int[],
  INITCOND = '{}');
--    N    N        P    N
-- should ERROR: tfnp(int[], anyelement) not matched by tfnp(int[], int)
CREATE AGGREGATE myaggn07a(BASETYPE = anyelement, SFUNC = tfnp, STYPE = int[],
  FINALFUNC = ffnp, INITCOND = '{}');
ERROR:  function tfnp(integer[], anyelement) does not exist
CREATE AGGREGATE myaggn07b(BASETYPE = anyelement, SFUNC = tfnp, STYPE = int[],
  INITCOND = '{}');
ERROR:  function tfnp(integer[], anyelement) does not exist
--    N    N        P    P
-- should CREATE
CREATE AGGREGATE myaggn08a(BASETYPE = anyelement, SFUNC = tf2p, STYPE = int[],
  FINALFUNC = ffnp, INITCOND = '{}');
CREATE AGGREGATE myaggn08b(BASETYPE = anyelement, SFUNC = tf2p, STYPE = int[],
  INITCOND = '{}');
--    N    P        N    N
-- should CREATE
CREATE AGGREGATE myaggn09a(BASETYPE = int, SFUNC = tf1p, STYPE = int[],
  FINALFUNC = ffnp, INITCOND = '{}');
--    N    P        N    P
-- should CREATE
CREATE AGGREGATE myaggn10a(BASETYPE = int, SFUNC = tfp, STYPE = int[],
  FINALFUNC = ffnp, INITCOND = '{}');
--    N    P        P    N
-- should ERROR: tf1p(int[],anyelement) not matched by tf1p(anyarray,int)
CREATE AGGREGATE myaggn11a(BASETYPE = anyelement, SFUNC = tf1p, STYPE = int[],
  FINALFUNC = ffnp, INITCOND = '{}');
ERROR:  function tf1p(integer[], anyelement) does not exist
--    N    P        P    P
-- should ERROR: tfp(int[],anyelement) not matched by tfp(anyarray,anyelement)
CREATE AGGREGATE myaggn12a(BASETYPE = anyelement, SFUNC = tfp, STYPE = int[],
  FINALFUNC = ffnp, INITCOND = '{}');
ERROR:  function tfp(integer[], anyelement) does not exist
--    P    N        N    N
-- should ERROR: tfnp(anyarray, int) not matched by tfnp(int[],int)
CREATE AGGREGATE myaggn13a(BASETYPE = int, SFUNC = tfnp, STYPE = anyarray,
  FINALFUNC = ffnp, INITCOND = '{}');
ERROR:  cannot determine transition data type
DETAIL:  A result of type anyarray requires at least one input of type anyelement, anyarray, anynonarray, anyenum, anyrange, or anymultirange.
CREATE AGGREGATE myaggn13b(BASETYPE = int, SFUNC = tfnp, STYPE = anyarray,
  INITCOND = '{}');
ERROR:  cannot determine transition data type
DETAIL:  A result of type anyarray requires at least one input of type anyelement, anyarray, anynonarray, anyenum, anyrange, or anymultirange.
--    P    N        N    P
-- should ERROR: tf2p(anyarray, int) not matched by tf2p(int[],anyelement)
CREATE AGGREGATE myaggn14a(BASETYPE = int, SFUNC = tf2p, STYPE = anyarray,
  FINALFUNC = ffnp, INITCOND = '{}');
ERROR:  cannot determine transition data type
DETAIL:  A result of type anyarray requires at least one input of type anyelement, anyarray, anynonarray, anyenum, anyrange, or anymultirange.
CREATE AGGREGATE myaggn14b(BASETYPE = int, SFUNC = tf2p, STYPE = anyarray,
  INITCOND = '{}');
ERROR:  cannot determine transition data type
DETAIL:  A result of type anyarray requires at least one input of type anyelement, anyarray, anynonarray, anyenum, anyrange, or anymultirange.
--    P    N        P    N
-- should ERROR: tfnp(anyarray, anyelement) not matched by tfnp(int[],int)
CREATE AGGREGATE myaggn15a(BASETYPE = anyelement, SFUNC = tfnp,
  STYPE = anyarray, FINALFUNC = ffnp, INITCOND = '{}');
ERROR:  function tfnp(anyarray, anyelement) does not exist
CREATE AGGREGATE myaggn15b(BASETYPE = anyelement, SFUNC = tfnp,
  STYPE = anyarray, INITCOND = '{}');
ERROR:  function tfnp(anyarray, anyelement) does not exist
--    P    N        P    P
-- should ERROR: tf2p(anyarray, anyelement) not matched by tf2p(int[],anyelement)
CREATE AGGREGATE myaggn16a(BASETYPE = anyelement, SFUNC = tf2p,
  STYPE = anyarray, FINALFUNC = ffnp, INITCOND = '{}');
ERROR:  function tf2p(anyarray, anyelement) does not exist
CREATE AGGREGATE myaggn16b(BASETYPE = anyelement, SFUNC = tf2p,
  STYPE = anyarray, INITCOND = '{}');
ERROR:  function tf2p(anyarray, anyelement) does not exist
--    P    P        N    N
-- should ERROR: ffnp(anyarray) not matched by ffnp(int[])
CREATE AGGREGATE myaggn17a(BASETYPE = int, SFUNC = tf1p, STYPE = anyarray,
  FINALFUNC = ffnp, INITCOND = '{}');
ERROR:  cannot determine transition data type
DETAIL:  A result of type anyarray requires at least one input of type anyelement, anyarray, anynonarray, anyenum, anyrange, or anymultirange.
--    P    P        N    P
-- should ERROR: tfp(anyarray, int) not matched by tfp(anyarray, anyelement)
CREATE AGGREGATE myaggn18a(BASETYPE = int, SFUNC = tfp, STYPE = anyarray,
  FINALFUNC = ffnp, INITCOND = '{}');
ERROR:  cannot determine transition data type
DETAIL:  A result of type anyarray requires at least one input of type anyelement, anyarray, anynonarray, anyenum, anyrange, or anymultirange.
--    P    P        P    N
-- should ERROR: tf1p(anyarray, anyelement) not matched by tf1p(anyarray, int)
CREATE AGGREGATE myaggn19a(BASETYPE = anyelement, SFUNC = tf1p,
  STYPE = anyarray, FINALFUNC = ffnp, INITCOND = '{}');
ERROR:  function tf1p(anyarray, anyelement) does not exist
--    P    P        P    P
-- should ERROR: ffnp(anyarray) not matched by ffnp(int[])
CREATE AGGREGATE myaggn20a(BASETYPE = anyelement, SFUNC = tfp,
  STYPE = anyarray, FINALFUNC = ffnp, INITCOND = '{}');
ERROR:  function ffnp(anyarray) does not exist
-- multi-arg polymorphic
CREATE AGGREGATE mysum2(anyelement,anyelement) (SFUNC = sum3,
  STYPE = anyelement, INITCOND = '0');
-- create test data for polymorphic aggregates
create temp table t(f1 int, f2 int[], f3 text);
insert into t values(1,array[1],'a');
insert into t values(1,array[11],'b');
insert into t values(1,array[111],'c');
insert into t values(2,array[2],'a');
insert into t values(2,array[22],'b');
insert into t values(2,array[222],'c');
insert into t values(3,array[3],'a');
insert into t values(3,array[3],'b');
-- test the successfully created polymorphic aggregates
select f3, myaggp01a(*) from t group by f3 order by f3;
 f3 | myaggp01a 
----+-----------
 a  | {}
 b  | {}
 c  | {}
(3 rows)

select f3, myaggp03a(*) from t group by f3 order by f3;
 f3 | myaggp03a 
----+-----------
 a  | {}
 b  | {}
 c  | {}
(3 rows)

select f3, myaggp03b(*) from t group by f3 order by f3;
 f3 | myaggp03b 
----+-----------
 a  | {}
 b  | {}
 c  | {}
(3 rows)

select f3, myaggp05a(f1) from t group by f3 order by f3;
 f3 | myaggp05a 
----+-----------
 a  | {1,2,3}
 b  | {1,2,3}
 c  | {1,2}
(3 rows)

select f3, myaggp06a(f1) from t group by f3 order by f3;
 f3 | myaggp06a 
----+-----------
 a  | {}
 b  | {}
 c  | {}
(3 rows)

select f3, myaggp08a(f1) from t group by f3 order by f3;
 f3 | myaggp08a 
----+-----------
 a  | {}
 b  | {}
 c  | {}
(3 rows)

select f3, myaggp09a(f1) from t group by f3 order by f3;
 f3 | myaggp09a 
----+-----------
 a  | {}
 b  | {}
 c  | {}
(3 rows)

select f3, myaggp09b(f1) from t group by f3 order by f3;
 f3 | myaggp09b 
----+-----------
 a  | {}
 b  | {}
 c  | {}
(3 rows)

select f3, myaggp10a(f1) from t group by f3 order by f3;
 f3 | myaggp10a 
----+-----------
 a  | {1,2,3}
 b  | {1,2,3}
 c  | {1,2}
(3 rows)

select f3, myaggp10b(f1) from t group by f3 order by f3;
 f3 | myaggp10b 
----+-----------
 a  | {1,2,3}
 b  | {1,2,3}
 c  | {1,2}
(3 rows)

select f3, myaggp20a(f1) from t group by f3 order by f3;
 f3 | myaggp20a 
----+-----------
 a  | {1,2,3}
 b  | {1,2,3}
 c  | {1,2}
(3 rows)

select f3, myaggp20b(f1) from t group by f3 order by f3;
 f3 | myaggp20b 
----+-----------
 a  | {1,2,3}
 b  | {1,2,3}
 c  | {1,2}
(3 rows)

select f3, myaggn01a(*) from t group by f3 order by f3;
 f3 | myaggn01a 
----+-----------
 a  | {}
 b  | {}
 c  | {}
(3 rows)

select f3, myaggn01b(*) from t group by f3 order by f3;
 f3 | myaggn01b 
----+-----------
 a  | {}
 b  | {}
 c  | {}
(3 rows)

select f3, myaggn03a(*) from t group by f3 order by f3;
 f3 | myaggn03a 
----+-----------
 a  | {}
 b  | {}
 c  | {}
(3 rows)

select f3, myaggn05a(f1) from t group by f3 order by f3;
 f3 | myaggn05a 
----+-----------
 a  | {1,2,3}
 b  | {1,2,3}
 c  | {1,2}
(3 rows)

select f3, myaggn05b(f1) from t group by f3 order by f3;
 f3 | myaggn05b 
----+-----------
 a  | {1,2,3}
 b  | {1,2,3}
 c  | {1,2}
(3 rows)

select f3, myaggn06a(f1) from t group by f3 order by f3;
 f3 | myaggn06a 
----+-----------
 a  | {}
 b  | {}
 c  | {}
(3 rows)

select f3, myaggn06b(f1) from t group by f3 order by f3;
 f3 | myaggn06b 
----+-----------
 a  | {}
 b  | {}
 c  | {}
(3 rows)

select f3, myaggn08a(f1) from t group by f3 order by f3;
 f3 | myaggn08a 
----+-----------
 a  | {}
 b  | {}
 c  | {}
(3 rows)

select f3, myaggn08b(f1) from t group by f3 order by f3;
 f3 | myaggn08b 
----+-----------
 a  | {}
 b  | {}
 c  | {}
(3 rows)

select f3, myaggn09a(f1) from t group by f3 order by f3;
 f3 | myaggn09a 
----+-----------
 a  | {}
 b  | {}
 c  | {}
(3 rows)

select f3, myaggn10a(f1) from t group by f3 order by f3;
 f3 | myaggn10a 
----+-----------
 a  | {1,2,3}
 b  | {1,2,3}
 c  | {1,2}
(3 rows)

select mysum2(f1, f1 + 1) from t;
 mysum2 
--------
     38
(1 row)

-- test inlining of polymorphic SQL functions
create function bleat(int) returns int as $$
begin
  raise notice 'bleat %', $1;
  return $1;
end$$ language plpgsql;
create function sql_if(bool, anyelement, anyelement) returns anyelement as $$
select case when $1 then $2 else $3 end $$ language sql;
-- Note this would fail with integer overflow, never mind wrong bleat() output,
-- if the CASE expression were not successfully inlined
select f1, sql_if(f1 > 0, bleat(f1), bleat(f1 + 1)) from int4_tbl;
NOTICE:  bleat 1
NOTICE:  bleat 123456
NOTICE:  bleat -123455
NOTICE:  bleat 2147483647
NOTICE:  bleat -2147483646
     f1      |   sql_if    
-------------+-------------
           0 |           1
      123456 |      123456
     -123456 |     -123455
  2147483647 |  2147483647
 -2147483647 | -2147483646
(5 rows)

select q2, sql_if(q2 > 0, q2, q2 + 1) from int8_tbl;
        q2         |      sql_if       
-------------------+-------------------
               456 |               456
  4567890123456789 |  4567890123456789
               123 |               123
  4567890123456789 |  4567890123456789
 -4567890123456789 | -4567890123456788
(5 rows)

-- another sort of polymorphic aggregate
CREATE AGGREGATE array_larger_accum (anyarray)
(
    sfunc = array_larger,
    stype = anyarray,
    initcond = '{}'
);
SELECT array_larger_accum(i)
FROM (VALUES (ARRAY[1,2]), (ARRAY[3,4])) as t(i);
 array_larger_accum 
--------------------
 {3,4}
(1 row)

SELECT array_larger_accum(i)
FROM (VALUES (ARRAY[row(1,2),row(3,4)]), (ARRAY[row(5,6),row(7,8)])) as t(i);
 array_larger_accum 
--------------------
 {"(5,6)","(7,8)"}
(1 row)

-- another kind of polymorphic aggregate
create function add_group(grp anyarray, ad anyelement, size integer)
  returns anyarray
  as $$
begin
  if grp is null then
    return array[ad];
  end if;
  if array_upper(grp, 1) < size then
    return grp || ad;
  end if;
  return grp;
end;
$$
  language plpgsql immutable;
create aggregate build_group(anyelement, integer) (
  SFUNC = add_group,
  STYPE = anyarray
);
select build_group(q1,3) from int8_tbl;
        build_group         
----------------------------
 {123,123,4567890123456789}
(1 row)

-- this should fail because stype isn't compatible with arg
create aggregate build_group(int8, integer) (
  SFUNC = add_group,
  STYPE = int2[]
);
ERROR:  function add_group(smallint[], bigint, integer) does not exist
-- but we can make a non-poly agg from a poly sfunc if types are OK
create aggregate build_group(int8, integer) (
  SFUNC = add_group,
  STYPE = int8[]
);
-- check proper resolution of data types for polymorphic transfn/finalfn
create function first_el_transfn(anyarray, anyelement) returns anyarray as
'select $1 || $2' language sql immutable;
create function first_el(anyarray) returns anyelement as
'select $1[1]' language sql strict immutable;
create aggregate first_el_agg_f8(float8) (
  SFUNC = array_append,
  STYPE = float8[],
  FINALFUNC = first_el
);
create aggregate first_el_agg_any(anyelement) (
  SFUNC = first_el_transfn,
  STYPE = anyarray,
  FINALFUNC = first_el
);
select first_el_agg_f8(x::float8) from generate_series(1,10) x;
 first_el_agg_f8 
-----------------
               1
(1 row)

select first_el_agg_any(x) from generate_series(1,10) x;
 first_el_agg_any 
------------------
                1
(1 row)

select first_el_agg_f8(x::float8) over(order by x) from generate_series(1,10) x;
 first_el_agg_f8 
-----------------
               1
               1
               1
               1
               1
               1
               1
               1
               1
               1
(10 rows)

select first_el_agg_any(x) over(order by x) from generate_series(1,10) x;
 first_el_agg_any 
------------------
                1
                1
                1
                1
                1
                1
                1
                1
                1
                1
(10 rows)

-- check that we can apply functions taking ANYARRAY to pg_stats
select distinct array_ndims(histogram_bounds) from pg_stats
where histogram_bounds is not null;
 array_ndims 
-------------
           1
(1 row)

-- such functions must protect themselves if varying element type isn't OK
-- (WHERE clause here is to avoid possibly getting a collation error instead)
select max(histogram_bounds) from pg_stats where tablename = 'pg_am';
ERROR:  cannot compare arrays of different element types
-- another corner case is the input functions for polymorphic pseudotypes
select array_in('{1,2,3}','int4'::regtype,-1);  -- this has historically worked
 array_in 
----------
 {1,2,3}
(1 row)

select * from array_in('{1,2,3}','int4'::regtype,-1);  -- this not
ERROR:  function "array_in" in FROM has unsupported return type anyarray
LINE 1: select * from array_in('{1,2,3}','int4'::regtype,-1);
                      ^
select anyrange_in('[10,20)','int4range'::regtype,-1);
ERROR:  cannot accept a value of type anyrange
-- test variadic polymorphic functions
create function myleast(variadic anyarray) returns anyelement as $$
  select min($1[i]) from generate_subscripts($1,1) g(i)
$$ language sql immutable strict;
select myleast(10, 1, 20, 33);
 myleast 
---------
       1
(1 row)

select myleast(1.1, 0.22, 0.55);
 myleast 
---------
    0.22
(1 row)

select myleast('z'::text);
 myleast 
---------
 z
(1 row)

select myleast(); -- fail
ERROR:  function myleast() does not exist
LINE 1: select myleast();
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
-- test with variadic call parameter
select myleast(variadic array[1,2,3,4,-1]);
 myleast 
---------
      -1
(1 row)

select myleast(variadic array[1.1, -5.5]);
 myleast 
---------
    -5.5
(1 row)

--test with empty variadic call parameter
select myleast(variadic array[]::int[]);
 myleast 
---------
        
(1 row)

-- an example with some ordinary arguments too
create function concat(text, variadic anyarray) returns text as $$
  select array_to_string($2, $1);
$$ language sql immutable strict;
select concat('%', 1, 2, 3, 4, 5);
  concat   
-----------
 1%2%3%4%5
(1 row)

select concat('|', 'a'::text, 'b', 'c');
 concat 
--------
 a|b|c
(1 row)

select concat('|', variadic array[1,2,33]);
 concat 
--------
 1|2|33
(1 row)

select concat('|', variadic array[]::int[]);
 concat 
--------
 
(1 row)

drop function concat(text, anyarray);
-- mix variadic with anyelement
create function formarray(anyelement, variadic anyarray) returns anyarray as $$
  select array_prepend($1, $2);
$$ language sql immutable strict;
select formarray(1,2,3,4,5);
  formarray  
-------------
 {1,2,3,4,5}
(1 row)

select formarray(1.1, variadic array[1.2,55.5]);
   formarray    
----------------
 {1.1,1.2,55.5}
(1 row)

select formarray(1.1, array[1.2,55.5]); -- fail without variadic
ERROR:  function formarray(numeric, numeric[]) does not exist
LINE 1: select formarray(1.1, array[1.2,55.5]);
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
select formarray(1, 'x'::text); -- fail, type mismatch
ERROR:  function formarray(integer, text) does not exist
LINE 1: select formarray(1, 'x'::text);
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
select formarray(1, variadic array['x'::text]); -- fail, type mismatch
ERROR:  function formarray(integer, text[]) does not exist
LINE 1: select formarray(1, variadic array['x'::text]);
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
drop function formarray(anyelement, variadic anyarray);
-- test pg_typeof() function
select pg_typeof(null);           -- unknown
 pg_typeof 
-----------
 unknown
(1 row)

select pg_typeof(0);              -- integer
 pg_typeof 
-----------
 integer
(1 row)

select pg_typeof(0.0);            -- numeric
 pg_typeof 
-----------
 numeric
(1 row)

select pg_typeof(1+1 = 2);        -- boolean
 pg_typeof 
-----------
 boolean
(1 row)

select pg_typeof('x');            -- unknown
 pg_typeof 
-----------
 unknown
(1 row)

select pg_typeof('' || '');       -- text
 pg_typeof 
-----------
 text
(1 row)

select pg_typeof(pg_typeof(0));   -- regtype
 pg_typeof 
-----------
 regtype
(1 row)

select pg_typeof(array[1.2,55.5]); -- numeric[]
 pg_typeof 
-----------
 numeric[]
(1 row)

select pg_typeof(myleast(10, 1, 20, 33));  -- polymorphic input
 pg_typeof 
-----------
 integer
(1 row)

-- test functions with default parameters
-- test basic functionality
create function dfunc(a int = 1, int = 2) returns int as $$
  select $1 + $2;
$$ language sql;
select dfunc();
 dfunc 
-------
     3
(1 row)

select dfunc(10);
 dfunc 
-------
    12
(1 row)

select dfunc(10, 20);
 dfunc 
-------
    30
(1 row)

select dfunc(10, 20, 30);  -- fail
ERROR:  function dfunc(integer, integer, integer) does not exist
LINE 1: select dfunc(10, 20, 30);
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
drop function dfunc();  -- fail
ERROR:  function dfunc() does not exist
drop function dfunc(int);  -- fail
ERROR:  function dfunc(integer) does not exist
drop function dfunc(int, int);  -- ok
-- fail: defaults must be at end of argument list
create function dfunc(a int = 1, b int) returns int as $$
  select $1 + $2;
$$ language sql;
ERROR:  input parameters after one with a default value must also have defaults
-- however, this should work:
create function dfunc(a int = 1, out sum int, b int = 2) as $$
  select $1 + $2;
$$ language sql;
select dfunc();
 dfunc 
-------
     3
(1 row)

-- verify it lists properly
\df dfunc
                                          List of functions
 Schema | Name  | Result data type |                    Argument data types                    | Type 
--------+-------+------------------+-----------------------------------------------------------+------
 public | dfunc | integer          | a integer DEFAULT 1, OUT sum integer, b integer DEFAULT 2 | func
(1 row)

drop function dfunc(int, int);
-- check implicit coercion
create function dfunc(a int DEFAULT 1.0, int DEFAULT '-1') returns int as $$
  select $1 + $2;
$$ language sql;
select dfunc();
 dfunc 
-------
     0
(1 row)

create function dfunc(a text DEFAULT 'Hello', b text DEFAULT 'World') returns text as $$
  select $1 || ', ' || $2;
$$ language sql;
select dfunc();  -- fail: which dfunc should be called? int or text
ERROR:  function dfunc() is not unique
LINE 1: select dfunc();
               ^
HINT:  Could not choose a best candidate function. You might need to add explicit type casts.
select dfunc('Hi');  -- ok
   dfunc   
-----------
 Hi, World
(1 row)

select dfunc('Hi', 'City');  -- ok
  dfunc   
----------
 Hi, City
(1 row)

select dfunc(0);  -- ok
 dfunc 
-------
    -1
(1 row)

select dfunc(10, 20);  -- ok
 dfunc 
-------
    30
(1 row)

drop function dfunc(int, int);
drop function dfunc(text, text);
create function dfunc(int = 1, int = 2) returns int as $$
  select 2;
$$ language sql;
create function dfunc(int = 1, int = 2, int = 3, int = 4) returns int as $$
  select 4;
$$ language sql;
-- Now, dfunc(nargs = 2) and dfunc(nargs = 4) are ambiguous when called
-- with 0 to 2 arguments.
select dfunc();  -- fail
ERROR:  function dfunc() is not unique
LINE 1: select dfunc();
               ^
HINT:  Could not choose a best candidate function. You might need to add explicit type casts.
select dfunc(1);  -- fail
ERROR:  function dfunc(integer) is not unique
LINE 1: select dfunc(1);
               ^
HINT:  Could not choose a best candidate function. You might need to add explicit type casts.
select dfunc(1, 2);  -- fail
ERROR:  function dfunc(integer, integer) is not unique
LINE 1: select dfunc(1, 2);
               ^
HINT:  Could not choose a best candidate function. You might need to add explicit type casts.
select dfunc(1, 2, 3);  -- ok
 dfunc 
-------
     4
(1 row)

select dfunc(1, 2, 3, 4);  -- ok
 dfunc 
-------
     4
(1 row)

drop function dfunc(int, int);
drop function dfunc(int, int, int, int);
-- default values are not allowed for output parameters
create function dfunc(out int = 20) returns int as $$
  select 1;
$$ language sql;
ERROR:  only input parameters can have default values
-- polymorphic parameter test
create function dfunc(anyelement = 'World'::text) returns text as $$
  select 'Hello, ' || $1::text;
$$ language sql;
select dfunc();
    dfunc     
--------------
 Hello, World
(1 row)

select dfunc(0);
  dfunc   
----------
 Hello, 0
(1 row)

select dfunc(to_date('20081215','YYYYMMDD'));
       dfunc       
-------------------
 Hello, 12-15-2008
(1 row)

select dfunc('City'::text);
    dfunc    
-------------
 Hello, City
(1 row)

drop function dfunc(anyelement);
-- check defaults for variadics
create function dfunc(a variadic int[]) returns int as
$$ select array_upper($1, 1) $$ language sql;
select dfunc();  -- fail
ERROR:  function dfunc() does not exist
LINE 1: select dfunc();
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
select dfunc(10);
 dfunc 
-------
     1
(1 row)

select dfunc(10,20);
 dfunc 
-------
     2
(1 row)

create or replace function dfunc(a variadic int[] default array[]::int[]) returns int as
$$ select array_upper($1, 1) $$ language sql;
select dfunc();  -- now ok
 dfunc 
-------
      
(1 row)

select dfunc(10);
 dfunc 
-------
     1
(1 row)

select dfunc(10,20);
 dfunc 
-------
     2
(1 row)

-- can't remove the default once it exists
create or replace function dfunc(a variadic int[]) returns int as
$$ select array_upper($1, 1) $$ language sql;
ERROR:  cannot remove parameter defaults from existing function
HINT:  Use DROP FUNCTION dfunc(integer[]) first.
\df dfunc
                                     List of functions
 Schema | Name  | Result data type |               Argument data types               | Type 
--------+-------+------------------+-------------------------------------------------+------
 public | dfunc | integer          | VARIADIC a integer[] DEFAULT ARRAY[]::integer[] | func
(1 row)

drop function dfunc(a variadic int[]);
-- Ambiguity should be reported only if there's not a better match available
create function dfunc(int = 1, int = 2, int = 3) returns int as $$
  select 3;
$$ language sql;
create function dfunc(int = 1, int = 2) returns int as $$
  select 2;
$$ language sql;
create function dfunc(text) returns text as $$
  select $1;
$$ language sql;
-- dfunc(narg=2) and dfunc(narg=3) are ambiguous
select dfunc(1);  -- fail
ERROR:  function dfunc(integer) is not unique
LINE 1: select dfunc(1);
               ^
HINT:  Could not choose a best candidate function. You might need to add explicit type casts.
-- but this works since the ambiguous functions aren't preferred anyway
select dfunc('Hi');
 dfunc 
-------
 Hi
(1 row)

drop function dfunc(int, int, int);
drop function dfunc(int, int);
drop function dfunc(text);
--
-- Tests for named- and mixed-notation function calling
--
create function dfunc(a int, b int, c int = 0, d int = 0)
  returns table (a int, b int, c int, d int) as $$
  select $1, $2, $3, $4;
$$ language sql;
select (dfunc(10,20,30)).*;
 a  | b  | c  | d 
----+----+----+---
 10 | 20 | 30 | 0
(1 row)

select (dfunc(a := 10, b := 20, c := 30)).*;
 a  | b  | c  | d 
----+----+----+---
 10 | 20 | 30 | 0
(1 row)

select * from dfunc(a := 10, b := 20);
 a  | b  | c | d 
----+----+---+---
 10 | 20 | 0 | 0
(1 row)

select * from dfunc(b := 10, a := 20);
 a  | b  | c | d 
----+----+---+---
 20 | 10 | 0 | 0
(1 row)

select * from dfunc(0);  -- fail
ERROR:  function dfunc(integer) does not exist
LINE 1: select * from dfunc(0);
                      ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
select * from dfunc(1,2);
 a | b | c | d 
---+---+---+---
 1 | 2 | 0 | 0
(1 row)

select * from dfunc(1,2,c := 3);
 a | b | c | d 
---+---+---+---
 1 | 2 | 3 | 0
(1 row)

select * from dfunc(1,2,d := 3);
 a | b | c | d 
---+---+---+---
 1 | 2 | 0 | 3
(1 row)

select * from dfunc(x := 20, b := 10, x := 30);  -- fail, duplicate name
ERROR:  argument name "x" used more than once
LINE 1: select * from dfunc(x := 20, b := 10, x := 30);
                                              ^
select * from dfunc(10, b := 20, 30);  -- fail, named args must be last
ERROR:  positional argument cannot follow named argument
LINE 1: select * from dfunc(10, b := 20, 30);
                                         ^
select * from dfunc(x := 10, b := 20, c := 30);  -- fail, unknown param
ERROR:  function dfunc(x => integer, b => integer, c => integer) does not exist
LINE 1: select * from dfunc(x := 10, b := 20, c := 30);
                      ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
select * from dfunc(10, 10, a := 20);  -- fail, a overlaps positional parameter
ERROR:  function dfunc(integer, integer, a => integer) does not exist
LINE 1: select * from dfunc(10, 10, a := 20);
                      ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
select * from dfunc(1,c := 2,d := 3); -- fail, no value for b
ERROR:  function dfunc(integer, c => integer, d => integer) does not exist
LINE 1: select * from dfunc(1,c := 2,d := 3);
                      ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
drop function dfunc(int, int, int, int);
-- test with different parameter types
create function dfunc(a varchar, b numeric, c date = current_date)
  returns table (a varchar, b numeric, c date) as $$
  select $1, $2, $3;
$$ language sql;
select (dfunc('Hello World', 20, '2009-07-25'::date)).*;
      a      | b  |     c      
-------------+----+------------
 Hello World | 20 | 07-25-2009
(1 row)

select * from dfunc('Hello World', 20, '2009-07-25'::date);
      a      | b  |     c      
-------------+----+------------
 Hello World | 20 | 07-25-2009
(1 row)

select * from dfunc(c := '2009-07-25'::date, a := 'Hello World', b := 20);
      a      | b  |     c      
-------------+----+------------
 Hello World | 20 | 07-25-2009
(1 row)

select * from dfunc('Hello World', b := 20, c := '2009-07-25'::date);
      a      | b  |     c      
-------------+----+------------
 Hello World | 20 | 07-25-2009
(1 row)

select * from dfunc('Hello World', c := '2009-07-25'::date, b := 20);
      a      | b  |     c      
-------------+----+------------
 Hello World | 20 | 07-25-2009
(1 row)

select * from dfunc('Hello World', c := 20, b := '2009-07-25'::date);  -- fail
ERROR:  function dfunc(unknown, c => integer, b => date) does not exist
LINE 1: select * from dfunc('Hello World', c := 20, b := '2009-07-25...
                      ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
drop function dfunc(varchar, numeric, date);
-- test out parameters with named params
create function dfunc(a varchar = 'def a', out _a varchar, c numeric = NULL, out _c numeric)
returns record as $$
  select $1, $2;
$$ language sql;
select (dfunc()).*;
  _a   | _c 
-------+----
 def a |   
(1 row)

select * from dfunc();
  _a   | _c 
-------+----
 def a |   
(1 row)

select * from dfunc('Hello', 100);
  _a   | _c  
-------+-----
 Hello | 100
(1 row)

select * from dfunc(a := 'Hello', c := 100);
  _a   | _c  
-------+-----
 Hello | 100
(1 row)

select * from dfunc(c := 100, a := 'Hello');
  _a   | _c  
-------+-----
 Hello | 100
(1 row)

select * from dfunc('Hello');
  _a   | _c 
-------+----
 Hello |   
(1 row)

select * from dfunc('Hello', c := 100);
  _a   | _c  
-------+-----
 Hello | 100
(1 row)

select * from dfunc(c := 100);
  _a   | _c  
-------+-----
 def a | 100
(1 row)

-- fail, can no longer change an input parameter's name
create or replace function dfunc(a varchar = 'def a', out _a varchar, x numeric = NULL, out _c numeric)
returns record as $$
  select $1, $2;
$$ language sql;
ERROR:  cannot change name of input parameter "c"
HINT:  Use DROP FUNCTION dfunc(character varying,numeric) first.
create or replace function dfunc(a varchar = 'def a', out _a varchar, numeric = NULL, out _c numeric)
returns record as $$
  select $1, $2;
$$ language sql;
ERROR:  cannot change name of input parameter "c"
HINT:  Use DROP FUNCTION dfunc(character varying,numeric) first.
drop function dfunc(varchar, numeric);
--fail, named parameters are not unique
create function testpolym(a int, a int) returns int as $$ select 1;$$ language sql;
ERROR:  parameter name "a" used more than once
create function testpolym(int, out a int, out a int) returns int as $$ select 1;$$ language sql;
ERROR:  parameter name "a" used more than once
create function testpolym(out a int, inout a int) returns int as $$ select 1;$$ language sql;
ERROR:  parameter name "a" used more than once
create function testpolym(a int, inout a int) returns int as $$ select 1;$$ language sql;
ERROR:  parameter name "a" used more than once
-- valid
create function testpolym(a int, out a int) returns int as $$ select $1;$$ language sql;
select testpolym(37);
 testpolym 
-----------
        37
(1 row)

drop function testpolym(int);
create function testpolym(a int) returns table(a int) as $$ select $1;$$ language sql;
select * from testpolym(37);
 a  
----
 37
(1 row)

drop function testpolym(int);
-- test polymorphic params and defaults
create function dfunc(a anyelement, b anyelement = null, flag bool = true)
returns anyelement as $$
  select case when $3 then $1 else $2 end;
$$ language sql;
select dfunc(1,2);
 dfunc 
-------
     1
(1 row)

select dfunc('a'::text, 'b'); -- positional notation with default
 dfunc 
-------
 a
(1 row)

select dfunc(a := 1, b := 2);
 dfunc 
-------
     1
(1 row)

select dfunc(a := 'a'::text, b := 'b');
 dfunc 
-------
 a
(1 row)

select dfunc(a := 'a'::text, b := 'b', flag := false); -- named notation
 dfunc 
-------
 b
(1 row)

select dfunc(b := 'b'::text, a := 'a'); -- named notation with default
 dfunc 
-------
 a
(1 row)

select dfunc(a := 'a'::text, flag := true); -- named notation with default
 dfunc 
-------
 a
(1 row)

select dfunc(a := 'a'::text, flag := false); -- named notation with default
 dfunc 
-------
 
(1 row)

select dfunc(b := 'b'::text, a := 'a', flag := true); -- named notation
 dfunc 
-------
 a
(1 row)

select dfunc('a'::text, 'b', false); -- full positional notation
 dfunc 
-------
 b
(1 row)

select dfunc('a'::text, 'b', flag := false); -- mixed notation
 dfunc 
-------
 b
(1 row)

select dfunc('a'::text, 'b', true); -- full positional notation
 dfunc 
-------
 a
(1 row)

select dfunc('a'::text, 'b', flag := true); -- mixed notation
 dfunc 
-------
 a
(1 row)

-- ansi/sql syntax
select dfunc(a => 1, b => 2);
 dfunc 
-------
     1
(1 row)

select dfunc(a => 'a'::text, b => 'b');
 dfunc 
-------
 a
(1 row)

select dfunc(a => 'a'::text, b => 'b', flag => false); -- named notation
 dfunc 
-------
 b
(1 row)

select dfunc(b => 'b'::text, a => 'a'); -- named notation with default
 dfunc 
-------
 a
(1 row)

select dfunc(a => 'a'::text, flag => true); -- named notation with default
 dfunc 
-------
 a
(1 row)

select dfunc(a => 'a'::text, flag => false); -- named notation with default
 dfunc 
-------
 
(1 row)

select dfunc(b => 'b'::text, a => 'a', flag => true); -- named notation
 dfunc 
-------
 a
(1 row)

select dfunc('a'::text, 'b', false); -- full positional notation
 dfunc 
-------
 b
(1 row)

select dfunc('a'::text, 'b', flag => false); -- mixed notation
 dfunc 
-------
 b
(1 row)

select dfunc('a'::text, 'b', true); -- full positional notation
 dfunc 
-------
 a
(1 row)

select dfunc('a'::text, 'b', flag => true); -- mixed notation
 dfunc 
-------
 a
(1 row)

-- this tests lexer edge cases around =>
select dfunc(a =>-1);
 dfunc 
-------
    -1
(1 row)

select dfunc(a =>+1);
 dfunc 
-------
     1
(1 row)

select dfunc(a =>/**/1);
 dfunc 
-------
     1
(1 row)

select dfunc(a =>--comment to be removed by psql
  1);
 dfunc 
-------
     1
(1 row)

-- need DO to protect the -- from psql
do $$
  declare r integer;
  begin
    select dfunc(a=>-- comment
      1) into r;
    raise info 'r = %', r;
  end;
$$;
INFO:  r = 1
-- check reverse-listing of named-arg calls
CREATE VIEW dfview AS
   SELECT q1, q2,
     dfunc(q1,q2, flag := q1>q2) as c3,
     dfunc(q1, flag := q1<q2, b := q2) as c4
     FROM int8_tbl;
select * from dfview;
        q1        |        q2         |        c3        |        c4         
------------------+-------------------+------------------+-------------------
              123 |               456 |              456 |               123
              123 |  4567890123456789 | 4567890123456789 |               123
 4567890123456789 |               123 | 4567890123456789 |               123
 4567890123456789 |  4567890123456789 | 4567890123456789 |  4567890123456789
 4567890123456789 | -4567890123456789 | 4567890123456789 | -4567890123456789
(5 rows)

\d+ dfview
                           View "public.dfview"
 Column |  Type  | Collation | Nullable | Default | Storage | Description 
--------+--------+-----------+----------+---------+---------+-------------
 q1     | bigint |           |          |         | plain   | 
 q2     | bigint |           |          |         | plain   | 
 c3     | bigint |           |          |         | plain   | 
 c4     | bigint |           |          |         | plain   | 
View definition:
 SELECT int8_tbl.q1,
    int8_tbl.q2,
    dfunc(int8_tbl.q1, int8_tbl.q2, flag => int8_tbl.q1 > int8_tbl.q2) AS c3,
    dfunc(int8_tbl.q1, flag => int8_tbl.q1 < int8_tbl.q2, b => int8_tbl.q2) AS c4
   FROM int8_tbl;

drop view dfview;
drop function dfunc(anyelement, anyelement, bool);
--
-- Tests for ANYCOMPATIBLE polymorphism family
--
create function anyctest(anycompatible, anycompatible)
returns anycompatible as $$
  select greatest($1, $2)
$$ language sql;
select x, pg_typeof(x) from anyctest(11, 12) x;
 x  | pg_typeof 
----+-----------
 12 | integer
(1 row)

select x, pg_typeof(x) from anyctest(11, 12.3) x;
  x   | pg_typeof 
------+-----------
 12.3 | numeric
(1 row)

select x, pg_typeof(x) from anyctest(11, point(1,2)) x;  -- fail
ERROR:  function anyctest(integer, point) does not exist
LINE 1: select x, pg_typeof(x) from anyctest(11, point(1,2)) x;
                                    ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
select x, pg_typeof(x) from anyctest('11', '12.3') x;  -- defaults to text
  x   | pg_typeof 
------+-----------
 12.3 | text
(1 row)

drop function anyctest(anycompatible, anycompatible);
create function anyctest(anycompatible, anycompatible)
returns anycompatiblearray as $$
  select array[$1, $2]
$$ language sql;
select x, pg_typeof(x) from anyctest(11, 12) x;
    x    | pg_typeof 
---------+-----------
 {11,12} | integer[]
(1 row)

select x, pg_typeof(x) from anyctest(11, 12.3) x;
     x     | pg_typeof 
-----------+-----------
 {11,12.3} | numeric[]
(1 row)

select x, pg_typeof(x) from anyctest(11, array[1,2]) x;  -- fail
ERROR:  function anyctest(integer, integer[]) does not exist
LINE 1: select x, pg_typeof(x) from anyctest(11, array[1,2]) x;
                                    ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
drop function anyctest(anycompatible, anycompatible);
create function anyctest(anycompatible, anycompatiblearray)
returns anycompatiblearray as $$
  select array[$1] || $2
$$ language sql;
select x, pg_typeof(x) from anyctest(11, array[12]) x;
    x    | pg_typeof 
---------+-----------
 {11,12} | integer[]
(1 row)

select x, pg_typeof(x) from anyctest(11, array[12.3]) x;
     x     | pg_typeof 
-----------+-----------
 {11,12.3} | numeric[]
(1 row)

select x, pg_typeof(x) from anyctest(12.3, array[13]) x;
     x     | pg_typeof 
-----------+-----------
 {12.3,13} | numeric[]
(1 row)

select x, pg_typeof(x) from anyctest(12.3, '{13,14.4}') x;
       x        | pg_typeof 
----------------+-----------
 {12.3,13,14.4} | numeric[]
(1 row)

select x, pg_typeof(x) from anyctest(11, array[point(1,2)]) x;  -- fail
ERROR:  function anyctest(integer, point[]) does not exist
LINE 1: select x, pg_typeof(x) from anyctest(11, array[point(1,2)]) ...
                                    ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
select x, pg_typeof(x) from anyctest(11, 12) x;  -- fail
ERROR:  function anyctest(integer, integer) does not exist
LINE 1: select x, pg_typeof(x) from anyctest(11, 12) x;
                                    ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
drop function anyctest(anycompatible, anycompatiblearray);
create function anyctest(anycompatible, anycompatiblerange)
returns anycompatiblerange as $$
  select $2
$$ language sql;
select x, pg_typeof(x) from anyctest(11, int4range(4,7)) x;
   x   | pg_typeof 
-------+-----------
 [4,7) | int4range
(1 row)

select x, pg_typeof(x) from anyctest(11, numrange(4,7)) x;
   x   | pg_typeof 
-------+-----------
 [4,7) | numrange
(1 row)

select x, pg_typeof(x) from anyctest(11, 12) x;  -- fail
ERROR:  function anyctest(integer, integer) does not exist
LINE 1: select x, pg_typeof(x) from anyctest(11, 12) x;
                                    ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
select x, pg_typeof(x) from anyctest(11.2, int4range(4,7)) x;  -- fail
ERROR:  function anyctest(numeric, int4range) does not exist
LINE 1: select x, pg_typeof(x) from anyctest(11.2, int4range(4,7)) x...
                                    ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
select x, pg_typeof(x) from anyctest(11.2, '[4,7)') x;  -- fail
ERROR:  could not determine polymorphic type anycompatiblerange because input has type unknown
drop function anyctest(anycompatible, anycompatiblerange);
create function anyctest(anycompatiblerange, anycompatiblerange)
returns anycompatible as $$
  select lower($1) + upper($2)
$$ language sql;
select x, pg_typeof(x) from anyctest(int4range(11,12), int4range(4,7)) x;
 x  | pg_typeof 
----+-----------
 18 | integer
(1 row)

select x, pg_typeof(x) from anyctest(int4range(11,12), numrange(4,7)) x; -- fail
ERROR:  function anyctest(int4range, numrange) does not exist
LINE 1: select x, pg_typeof(x) from anyctest(int4range(11,12), numra...
                                    ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
drop function anyctest(anycompatiblerange, anycompatiblerange);
-- fail, can't infer result type:
create function anyctest(anycompatible)
returns anycompatiblerange as $$
  select $1
$$ language sql;
ERROR:  cannot determine result data type
DETAIL:  A result of type anycompatiblerange requires at least one input of type anycompatiblerange or anycompatiblemultirange.
create function anyctest(anycompatible, anycompatiblemultirange)
returns anycompatiblemultirange as $$
  select $2
$$ language sql;
select x, pg_typeof(x) from anyctest(11, multirange(int4range(4,7))) x;
    x    |   pg_typeof    
---------+----------------
 {[4,7)} | int4multirange
(1 row)

select x, pg_typeof(x) from anyctest(11, multirange(numrange(4,7))) x;
    x    |   pg_typeof   
---------+---------------
 {[4,7)} | nummultirange
(1 row)

select x, pg_typeof(x) from anyctest(11, 12) x;  -- fail
ERROR:  function anyctest(integer, integer) does not exist
LINE 1: select x, pg_typeof(x) from anyctest(11, 12) x;
                                    ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
select x, pg_typeof(x) from anyctest(11.2, multirange(int4range(4,7))) x;  -- fail
ERROR:  function anyctest(numeric, int4multirange) does not exist
LINE 1: select x, pg_typeof(x) from anyctest(11.2, multirange(int4ra...
                                    ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
select x, pg_typeof(x) from anyctest(11.2, '{[4,7)}') x;  -- fail
ERROR:  could not determine polymorphic type anycompatiblemultirange because input has type unknown
drop function anyctest(anycompatible, anycompatiblemultirange);
create function anyctest(anycompatiblemultirange, anycompatiblemultirange)
returns anycompatible as $$
  select lower($1) + upper($2)
$$ language sql;
select x, pg_typeof(x) from anyctest(multirange(int4range(11,12)), multirange(int4range(4,7))) x;
 x  | pg_typeof 
----+-----------
 18 | integer
(1 row)

select x, pg_typeof(x) from anyctest(multirange(int4range(11,12)), multirange(numrange(4,7))) x; -- fail
ERROR:  function anyctest(int4multirange, nummultirange) does not exist
LINE 1: select x, pg_typeof(x) from anyctest(multirange(int4range(11...
                                    ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
drop function anyctest(anycompatiblemultirange, anycompatiblemultirange);
-- fail, can't infer result type:
create function anyctest(anycompatible)
returns anycompatiblemultirange as $$
  select $1
$$ language sql;
ERROR:  cannot determine result data type
DETAIL:  A result of type anycompatiblemultirange requires at least one input of type anycompatiblerange or anycompatiblemultirange.
create function anyctest(anycompatiblenonarray, anycompatiblenonarray)
returns anycompatiblearray as $$
  select array[$1, $2]
$$ language sql;
select x, pg_typeof(x) from anyctest(11, 12) x;
    x    | pg_typeof 
---------+-----------
 {11,12} | integer[]
(1 row)

select x, pg_typeof(x) from anyctest(11, 12.3) x;
     x     | pg_typeof 
-----------+-----------
 {11,12.3} | numeric[]
(1 row)

select x, pg_typeof(x) from anyctest(array[11], array[1,2]) x;  -- fail
ERROR:  function anyctest(integer[], integer[]) does not exist
LINE 1: select x, pg_typeof(x) from anyctest(array[11], array[1,2]) ...
                                    ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
drop function anyctest(anycompatiblenonarray, anycompatiblenonarray);
create function anyctest(a anyelement, b anyarray,
                         c anycompatible, d anycompatible)
returns anycompatiblearray as $$
  select array[c, d]
$$ language sql;
select x, pg_typeof(x) from anyctest(11, array[1, 2], 42, 34.5) x;
     x     | pg_typeof 
-----------+-----------
 {42,34.5} | numeric[]
(1 row)

select x, pg_typeof(x) from anyctest(11, array[1, 2], point(1,2), point(3,4)) x;
         x         | pg_typeof 
-------------------+-----------
 {"(1,2)","(3,4)"} | point[]
(1 row)

select x, pg_typeof(x) from anyctest(11, '{1,2}', point(1,2), '(3,4)') x;
         x         | pg_typeof 
-------------------+-----------
 {"(1,2)","(3,4)"} | point[]
(1 row)

select x, pg_typeof(x) from anyctest(11, array[1, 2.2], 42, 34.5) x;  -- fail
ERROR:  function anyctest(integer, numeric[], integer, numeric) does not exist
LINE 1: select x, pg_typeof(x) from anyctest(11, array[1, 2.2], 42, ...
                                    ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
drop function anyctest(a anyelement, b anyarray,
                       c anycompatible, d anycompatible);
create function anyctest(variadic anycompatiblearray)
returns anycompatiblearray as $$
  select $1
$$ language sql;
select x, pg_typeof(x) from anyctest(11, 12) x;
    x    | pg_typeof 
---------+-----------
 {11,12} | integer[]
(1 row)

select x, pg_typeof(x) from anyctest(11, 12.2) x;
     x     | pg_typeof 
-----------+-----------
 {11,12.2} | numeric[]
(1 row)

select x, pg_typeof(x) from anyctest(11, '12') x;
    x    | pg_typeof 
---------+-----------
 {11,12} | integer[]
(1 row)

select x, pg_typeof(x) from anyctest(11, '12.2') x;  -- fail
ERROR:  invalid input syntax for type integer: "12.2"
LINE 1: select x, pg_typeof(x) from anyctest(11, '12.2') x;
                                                 ^
select x, pg_typeof(x) from anyctest(variadic array[11, 12]) x;
    x    | pg_typeof 
---------+-----------
 {11,12} | integer[]
(1 row)

select x, pg_typeof(x) from anyctest(variadic array[11, 12.2]) x;
     x     | pg_typeof 
-----------+-----------
 {11,12.2} | numeric[]
(1 row)

drop function anyctest(variadic anycompatiblearray);