summaryrefslogtreecommitdiffstats
path: root/src/test/regress/expected/collate.icu.utf8.out
blob: d4c8c6de38eecc506454c96ced74ea43c048a6ba (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
/*
 * This test is for ICU collations.
 */
/* skip test if not UTF8 server encoding or no ICU collations installed */
SELECT getdatabaseencoding() <> 'UTF8' OR
       (SELECT count(*) FROM pg_collation WHERE collprovider = 'i') = 0
       AS skip_test \gset
\if :skip_test
\quit
\endif
SET client_encoding TO UTF8;
CREATE SCHEMA collate_tests;
SET search_path = collate_tests;
CREATE TABLE collate_test1 (
    a int,
    b text COLLATE "en-x-icu" NOT NULL
);
\d collate_test1
        Table "collate_tests.collate_test1"
 Column |  Type   | Collation | Nullable | Default 
--------+---------+-----------+----------+---------
 a      | integer |           |          | 
 b      | text    | en-x-icu  | not null | 

CREATE TABLE collate_test_fail (
    a int,
    b text COLLATE "ja_JP.eucjp-x-icu"
);
ERROR:  collation "ja_JP.eucjp-x-icu" for encoding "UTF8" does not exist
LINE 3:     b text COLLATE "ja_JP.eucjp-x-icu"
                   ^
CREATE TABLE collate_test_fail (
    a int,
    b text COLLATE "foo-x-icu"
);
ERROR:  collation "foo-x-icu" for encoding "UTF8" does not exist
LINE 3:     b text COLLATE "foo-x-icu"
                   ^
CREATE TABLE collate_test_fail (
    a int COLLATE "en-x-icu",
    b text
);
ERROR:  collations are not supported by type integer
LINE 2:     a int COLLATE "en-x-icu",
                  ^
CREATE TABLE collate_test_like (
    LIKE collate_test1
);
\d collate_test_like
      Table "collate_tests.collate_test_like"
 Column |  Type   | Collation | Nullable | Default 
--------+---------+-----------+----------+---------
 a      | integer |           |          | 
 b      | text    | en-x-icu  | not null | 

CREATE TABLE collate_test2 (
    a int,
    b text COLLATE "sv-x-icu"
);
CREATE TABLE collate_test3 (
    a int,
    b text COLLATE "C"
);
INSERT INTO collate_test1 VALUES (1, 'abc'), (2, 'äbc'), (3, 'bbc'), (4, 'ABC');
INSERT INTO collate_test2 SELECT * FROM collate_test1;
INSERT INTO collate_test3 SELECT * FROM collate_test1;
SELECT * FROM collate_test1 WHERE b >= 'bbc';
 a |  b  
---+-----
 3 | bbc
(1 row)

SELECT * FROM collate_test2 WHERE b >= 'bbc';
 a |  b  
---+-----
 2 | äbc
 3 | bbc
(2 rows)

SELECT * FROM collate_test3 WHERE b >= 'bbc';
 a |  b  
---+-----
 2 | äbc
 3 | bbc
(2 rows)

SELECT * FROM collate_test3 WHERE b >= 'BBC';
 a |  b  
---+-----
 1 | abc
 2 | äbc
 3 | bbc
(3 rows)

SELECT * FROM collate_test1 WHERE b COLLATE "C" >= 'bbc';
 a |  b  
---+-----
 2 | äbc
 3 | bbc
(2 rows)

SELECT * FROM collate_test1 WHERE b >= 'bbc' COLLATE "C";
 a |  b  
---+-----
 2 | äbc
 3 | bbc
(2 rows)

SELECT * FROM collate_test1 WHERE b COLLATE "C" >= 'bbc' COLLATE "C";
 a |  b  
---+-----
 2 | äbc
 3 | bbc
(2 rows)

SELECT * FROM collate_test1 WHERE b COLLATE "C" >= 'bbc' COLLATE "en-x-icu";
ERROR:  collation mismatch between explicit collations "C" and "en-x-icu"
LINE 1: ...* FROM collate_test1 WHERE b COLLATE "C" >= 'bbc' COLLATE "e...
                                                             ^
CREATE DOMAIN testdomain_sv AS text COLLATE "sv-x-icu";
CREATE DOMAIN testdomain_i AS int COLLATE "sv-x-icu"; -- fails
ERROR:  collations are not supported by type integer
CREATE TABLE collate_test4 (
    a int,
    b testdomain_sv
);
INSERT INTO collate_test4 SELECT * FROM collate_test1;
SELECT a, b FROM collate_test4 ORDER BY b;
 a |  b  
---+-----
 1 | abc
 4 | ABC
 3 | bbc
 2 | äbc
(4 rows)

CREATE TABLE collate_test5 (
    a int,
    b testdomain_sv COLLATE "en-x-icu"
);
INSERT INTO collate_test5 SELECT * FROM collate_test1;
SELECT a, b FROM collate_test5 ORDER BY b;
 a |  b  
---+-----
 1 | abc
 4 | ABC
 2 | äbc
 3 | bbc
(4 rows)

SELECT a, b FROM collate_test1 ORDER BY b;
 a |  b  
---+-----
 1 | abc
 4 | ABC
 2 | äbc
 3 | bbc
(4 rows)

SELECT a, b FROM collate_test2 ORDER BY b;
 a |  b  
---+-----
 1 | abc
 4 | ABC
 3 | bbc
 2 | äbc
(4 rows)

SELECT a, b FROM collate_test3 ORDER BY b;
 a |  b  
---+-----
 4 | ABC
 1 | abc
 3 | bbc
 2 | äbc
(4 rows)

SELECT a, b FROM collate_test1 ORDER BY b COLLATE "C";
 a |  b  
---+-----
 4 | ABC
 1 | abc
 3 | bbc
 2 | äbc
(4 rows)

-- star expansion
SELECT * FROM collate_test1 ORDER BY b;
 a |  b  
---+-----
 1 | abc
 4 | ABC
 2 | äbc
 3 | bbc
(4 rows)

SELECT * FROM collate_test2 ORDER BY b;
 a |  b  
---+-----
 1 | abc
 4 | ABC
 3 | bbc
 2 | äbc
(4 rows)

SELECT * FROM collate_test3 ORDER BY b;
 a |  b  
---+-----
 4 | ABC
 1 | abc
 3 | bbc
 2 | äbc
(4 rows)

-- constant expression folding
SELECT 'bbc' COLLATE "en-x-icu" > 'äbc' COLLATE "en-x-icu" AS "true";
 true 
------
 t
(1 row)

SELECT 'bbc' COLLATE "sv-x-icu" > 'äbc' COLLATE "sv-x-icu" AS "false";
 false 
-------
 f
(1 row)

-- upper/lower
CREATE TABLE collate_test10 (
    a int,
    x text COLLATE "en-x-icu",
    y text COLLATE "tr-x-icu"
);
INSERT INTO collate_test10 VALUES (1, 'hij', 'hij'), (2, 'HIJ', 'HIJ');
SELECT a, lower(x), lower(y), upper(x), upper(y), initcap(x), initcap(y) FROM collate_test10;
 a | lower | lower | upper | upper | initcap | initcap 
---+-------+-------+-------+-------+---------+---------
 1 | hij   | hij   | HIJ   | HİJ   | Hij     | Hij
 2 | hij   | hıj   | HIJ   | HIJ   | Hij     | Hıj
(2 rows)

SELECT a, lower(x COLLATE "C"), lower(y COLLATE "C") FROM collate_test10;
 a | lower | lower 
---+-------+-------
 1 | hij   | hij
 2 | hij   | hij
(2 rows)

SELECT a, x, y FROM collate_test10 ORDER BY lower(y), a;
 a |  x  |  y  
---+-----+-----
 2 | HIJ | HIJ
 1 | hij | hij
(2 rows)

-- LIKE/ILIKE
SELECT * FROM collate_test1 WHERE b LIKE 'abc';
 a |  b  
---+-----
 1 | abc
(1 row)

SELECT * FROM collate_test1 WHERE b LIKE 'abc%';
 a |  b  
---+-----
 1 | abc
(1 row)

SELECT * FROM collate_test1 WHERE b LIKE '%bc%';
 a |  b  
---+-----
 1 | abc
 2 | äbc
 3 | bbc
(3 rows)

SELECT * FROM collate_test1 WHERE b ILIKE 'abc';
 a |  b  
---+-----
 1 | abc
 4 | ABC
(2 rows)

SELECT * FROM collate_test1 WHERE b ILIKE 'abc%';
 a |  b  
---+-----
 1 | abc
 4 | ABC
(2 rows)

SELECT * FROM collate_test1 WHERE b ILIKE '%bc%';
 a |  b  
---+-----
 1 | abc
 2 | äbc
 3 | bbc
 4 | ABC
(4 rows)

SELECT 'Türkiye' COLLATE "en-x-icu" ILIKE '%KI%' AS "true";
 true 
------
 t
(1 row)

SELECT 'Türkiye' COLLATE "tr-x-icu" ILIKE '%KI%' AS "false";
 false 
-------
 f
(1 row)

SELECT 'bıt' ILIKE 'BIT' COLLATE "en-x-icu" AS "false";
 false 
-------
 f
(1 row)

SELECT 'bıt' ILIKE 'BIT' COLLATE "tr-x-icu" AS "true";
 true 
------
 t
(1 row)

-- The following actually exercises the selectivity estimation for ILIKE.
SELECT relname FROM pg_class WHERE relname ILIKE 'abc%';
 relname 
---------
(0 rows)

-- regular expressions
SELECT * FROM collate_test1 WHERE b ~ '^abc$';
 a |  b  
---+-----
 1 | abc
(1 row)

SELECT * FROM collate_test1 WHERE b ~ '^abc';
 a |  b  
---+-----
 1 | abc
(1 row)

SELECT * FROM collate_test1 WHERE b ~ 'bc';
 a |  b  
---+-----
 1 | abc
 2 | äbc
 3 | bbc
(3 rows)

SELECT * FROM collate_test1 WHERE b ~* '^abc$';
 a |  b  
---+-----
 1 | abc
 4 | ABC
(2 rows)

SELECT * FROM collate_test1 WHERE b ~* '^abc';
 a |  b  
---+-----
 1 | abc
 4 | ABC
(2 rows)

SELECT * FROM collate_test1 WHERE b ~* 'bc';
 a |  b  
---+-----
 1 | abc
 2 | äbc
 3 | bbc
 4 | ABC
(4 rows)

CREATE TABLE collate_test6 (
    a int,
    b text COLLATE "en-x-icu"
);
INSERT INTO collate_test6 VALUES (1, 'abc'), (2, 'ABC'), (3, '123'), (4, 'ab1'),
                                 (5, 'a1!'), (6, 'a c'), (7, '!.;'), (8, '   '),
                                 (9, 'äbç'), (10, 'ÄBÇ');
SELECT b,
       b ~ '^[[:alpha:]]+$' AS is_alpha,
       b ~ '^[[:upper:]]+$' AS is_upper,
       b ~ '^[[:lower:]]+$' AS is_lower,
       b ~ '^[[:digit:]]+$' AS is_digit,
       b ~ '^[[:alnum:]]+$' AS is_alnum,
       b ~ '^[[:graph:]]+$' AS is_graph,
       b ~ '^[[:print:]]+$' AS is_print,
       b ~ '^[[:punct:]]+$' AS is_punct,
       b ~ '^[[:space:]]+$' AS is_space
FROM collate_test6;
  b  | is_alpha | is_upper | is_lower | is_digit | is_alnum | is_graph | is_print | is_punct | is_space 
-----+----------+----------+----------+----------+----------+----------+----------+----------+----------
 abc | t        | f        | t        | f        | t        | t        | t        | f        | f
 ABC | t        | t        | f        | f        | t        | t        | t        | f        | f
 123 | f        | f        | f        | t        | t        | t        | t        | f        | f
 ab1 | f        | f        | f        | f        | t        | t        | t        | f        | f
 a1! | f        | f        | f        | f        | f        | t        | t        | f        | f
 a c | f        | f        | f        | f        | f        | f        | t        | f        | f
 !.; | f        | f        | f        | f        | f        | t        | t        | t        | f
     | f        | f        | f        | f        | f        | f        | t        | f        | t
 äbç | t        | f        | t        | f        | t        | t        | t        | f        | f
 ÄBÇ | t        | t        | f        | f        | t        | t        | t        | f        | f
(10 rows)

SELECT 'Türkiye' COLLATE "en-x-icu" ~* 'KI' AS "true";
 true 
------
 t
(1 row)

SELECT 'Türkiye' COLLATE "tr-x-icu" ~* 'KI' AS "true";  -- true with ICU
 true 
------
 t
(1 row)

SELECT 'bıt' ~* 'BIT' COLLATE "en-x-icu" AS "false";
 false 
-------
 f
(1 row)

SELECT 'bıt' ~* 'BIT' COLLATE "tr-x-icu" AS "false";  -- false with ICU
 false 
-------
 f
(1 row)

-- The following actually exercises the selectivity estimation for ~*.
SELECT relname FROM pg_class WHERE relname ~* '^abc';
 relname 
---------
(0 rows)

/* not run by default because it requires tr_TR system locale
-- to_char

SET lc_time TO 'tr_TR';
SELECT to_char(date '2010-04-01', 'DD TMMON YYYY');
SELECT to_char(date '2010-04-01', 'DD TMMON YYYY' COLLATE "tr-x-icu");
*/
-- backwards parsing
CREATE VIEW collview1 AS SELECT * FROM collate_test1 WHERE b COLLATE "C" >= 'bbc';
CREATE VIEW collview2 AS SELECT a, b FROM collate_test1 ORDER BY b COLLATE "C";
CREATE VIEW collview3 AS SELECT a, lower((x || x) COLLATE "C") FROM collate_test10;
SELECT table_name, view_definition FROM information_schema.views
  WHERE table_name LIKE 'collview%' ORDER BY 1;
 table_name |                             view_definition                              
------------+--------------------------------------------------------------------------
 collview1  |  SELECT collate_test1.a,                                                +
            |     collate_test1.b                                                     +
            |    FROM collate_test1                                                   +
            |   WHERE ((collate_test1.b COLLATE "C") >= 'bbc'::text);
 collview2  |  SELECT collate_test1.a,                                                +
            |     collate_test1.b                                                     +
            |    FROM collate_test1                                                   +
            |   ORDER BY (collate_test1.b COLLATE "C");
 collview3  |  SELECT collate_test10.a,                                               +
            |     lower(((collate_test10.x || collate_test10.x) COLLATE "C")) AS lower+
            |    FROM collate_test10;
(3 rows)

-- collation propagation in various expression types
SELECT a, coalesce(b, 'foo') FROM collate_test1 ORDER BY 2;
 a | coalesce 
---+----------
 1 | abc
 4 | ABC
 2 | äbc
 3 | bbc
(4 rows)

SELECT a, coalesce(b, 'foo') FROM collate_test2 ORDER BY 2;
 a | coalesce 
---+----------
 1 | abc
 4 | ABC
 3 | bbc
 2 | äbc
(4 rows)

SELECT a, coalesce(b, 'foo') FROM collate_test3 ORDER BY 2;
 a | coalesce 
---+----------
 4 | ABC
 1 | abc
 3 | bbc
 2 | äbc
(4 rows)

SELECT a, lower(coalesce(x, 'foo')), lower(coalesce(y, 'foo')) FROM collate_test10;
 a | lower | lower 
---+-------+-------
 1 | hij   | hij
 2 | hij   | hıj
(2 rows)

SELECT a, b, greatest(b, 'CCC') FROM collate_test1 ORDER BY 3;
 a |  b  | greatest 
---+-----+----------
 1 | abc | CCC
 2 | äbc | CCC
 3 | bbc | CCC
 4 | ABC | CCC
(4 rows)

SELECT a, b, greatest(b, 'CCC') FROM collate_test2 ORDER BY 3;
 a |  b  | greatest 
---+-----+----------
 1 | abc | CCC
 3 | bbc | CCC
 4 | ABC | CCC
 2 | äbc | äbc
(4 rows)

SELECT a, b, greatest(b, 'CCC') FROM collate_test3 ORDER BY 3;
 a |  b  | greatest 
---+-----+----------
 4 | ABC | CCC
 1 | abc | abc
 3 | bbc | bbc
 2 | äbc | äbc
(4 rows)

SELECT a, x, y, lower(greatest(x, 'foo')), lower(greatest(y, 'foo')) FROM collate_test10;
 a |  x  |  y  | lower | lower 
---+-----+-----+-------+-------
 1 | hij | hij | hij   | hij
 2 | HIJ | HIJ | hij   | hıj
(2 rows)

SELECT a, nullif(b, 'abc') FROM collate_test1 ORDER BY 2;
 a | nullif 
---+--------
 4 | ABC
 2 | äbc
 3 | bbc
 1 | 
(4 rows)

SELECT a, nullif(b, 'abc') FROM collate_test2 ORDER BY 2;
 a | nullif 
---+--------
 4 | ABC
 3 | bbc
 2 | äbc
 1 | 
(4 rows)

SELECT a, nullif(b, 'abc') FROM collate_test3 ORDER BY 2;
 a | nullif 
---+--------
 4 | ABC
 3 | bbc
 2 | äbc
 1 | 
(4 rows)

SELECT a, lower(nullif(x, 'foo')), lower(nullif(y, 'foo')) FROM collate_test10;
 a | lower | lower 
---+-------+-------
 1 | hij   | hij
 2 | hij   | hıj
(2 rows)

SELECT a, CASE b WHEN 'abc' THEN 'abcd' ELSE b END FROM collate_test1 ORDER BY 2;
 a |  b   
---+------
 4 | ABC
 2 | äbc
 1 | abcd
 3 | bbc
(4 rows)

SELECT a, CASE b WHEN 'abc' THEN 'abcd' ELSE b END FROM collate_test2 ORDER BY 2;
 a |  b   
---+------
 4 | ABC
 1 | abcd
 3 | bbc
 2 | äbc
(4 rows)

SELECT a, CASE b WHEN 'abc' THEN 'abcd' ELSE b END FROM collate_test3 ORDER BY 2;
 a |  b   
---+------
 4 | ABC
 1 | abcd
 3 | bbc
 2 | äbc
(4 rows)

CREATE DOMAIN testdomain AS text;
SELECT a, b::testdomain FROM collate_test1 ORDER BY 2;
 a |  b  
---+-----
 1 | abc
 4 | ABC
 2 | äbc
 3 | bbc
(4 rows)

SELECT a, b::testdomain FROM collate_test2 ORDER BY 2;
 a |  b  
---+-----
 1 | abc
 4 | ABC
 3 | bbc
 2 | äbc
(4 rows)

SELECT a, b::testdomain FROM collate_test3 ORDER BY 2;
 a |  b  
---+-----
 4 | ABC
 1 | abc
 3 | bbc
 2 | äbc
(4 rows)

SELECT a, b::testdomain_sv FROM collate_test3 ORDER BY 2;
 a |  b  
---+-----
 1 | abc
 4 | ABC
 3 | bbc
 2 | äbc
(4 rows)

SELECT a, lower(x::testdomain), lower(y::testdomain) FROM collate_test10;
 a | lower | lower 
---+-------+-------
 1 | hij   | hij
 2 | hij   | hıj
(2 rows)

SELECT min(b), max(b) FROM collate_test1;
 min | max 
-----+-----
 abc | bbc
(1 row)

SELECT min(b), max(b) FROM collate_test2;
 min | max 
-----+-----
 abc | äbc
(1 row)

SELECT min(b), max(b) FROM collate_test3;
 min | max 
-----+-----
 ABC | äbc
(1 row)

SELECT array_agg(b ORDER BY b) FROM collate_test1;
     array_agg     
-------------------
 {abc,ABC,äbc,bbc}
(1 row)

SELECT array_agg(b ORDER BY b) FROM collate_test2;
     array_agg     
-------------------
 {abc,ABC,bbc,äbc}
(1 row)

SELECT array_agg(b ORDER BY b) FROM collate_test3;
     array_agg     
-------------------
 {ABC,abc,bbc,äbc}
(1 row)

SELECT a, b FROM collate_test1 UNION ALL SELECT a, b FROM collate_test1 ORDER BY 2;
 a |  b  
---+-----
 1 | abc
 1 | abc
 4 | ABC
 4 | ABC
 2 | äbc
 2 | äbc
 3 | bbc
 3 | bbc
(8 rows)

SELECT a, b FROM collate_test2 UNION SELECT a, b FROM collate_test2 ORDER BY 2;
 a |  b  
---+-----
 1 | abc
 4 | ABC
 3 | bbc
 2 | äbc
(4 rows)

SELECT a, b FROM collate_test3 WHERE a < 4 INTERSECT SELECT a, b FROM collate_test3 WHERE a > 1 ORDER BY 2;
 a |  b  
---+-----
 3 | bbc
 2 | äbc
(2 rows)

SELECT a, b FROM collate_test3 EXCEPT SELECT a, b FROM collate_test3 WHERE a < 2 ORDER BY 2;
 a |  b  
---+-----
 4 | ABC
 3 | bbc
 2 | äbc
(3 rows)

SELECT a, b FROM collate_test1 UNION ALL SELECT a, b FROM collate_test3 ORDER BY 2; -- fail
ERROR:  could not determine which collation to use for string comparison
HINT:  Use the COLLATE clause to set the collation explicitly.
SELECT a, b FROM collate_test1 UNION ALL SELECT a, b FROM collate_test3; -- ok
 a |  b  
---+-----
 1 | abc
 2 | äbc
 3 | bbc
 4 | ABC
 1 | abc
 2 | äbc
 3 | bbc
 4 | ABC
(8 rows)

SELECT a, b FROM collate_test1 UNION SELECT a, b FROM collate_test3 ORDER BY 2; -- fail
ERROR:  collation mismatch between implicit collations "en-x-icu" and "C"
LINE 1: SELECT a, b FROM collate_test1 UNION SELECT a, b FROM collat...
                                                       ^
HINT:  You can choose the collation by applying the COLLATE clause to one or both expressions.
SELECT a, b COLLATE "C" FROM collate_test1 UNION SELECT a, b FROM collate_test3 ORDER BY 2; -- ok
 a |  b  
---+-----
 4 | ABC
 1 | abc
 3 | bbc
 2 | äbc
(4 rows)

SELECT a, b FROM collate_test1 INTERSECT SELECT a, b FROM collate_test3 ORDER BY 2; -- fail
ERROR:  collation mismatch between implicit collations "en-x-icu" and "C"
LINE 1: ...ELECT a, b FROM collate_test1 INTERSECT SELECT a, b FROM col...
                                                             ^
HINT:  You can choose the collation by applying the COLLATE clause to one or both expressions.
SELECT a, b FROM collate_test1 EXCEPT SELECT a, b FROM collate_test3 ORDER BY 2; -- fail
ERROR:  collation mismatch between implicit collations "en-x-icu" and "C"
LINE 1: SELECT a, b FROM collate_test1 EXCEPT SELECT a, b FROM colla...
                                                        ^
HINT:  You can choose the collation by applying the COLLATE clause to one or both expressions.
CREATE TABLE test_u AS SELECT a, b FROM collate_test1 UNION ALL SELECT a, b FROM collate_test3; -- fail
ERROR:  no collation was derived for column "b" with collatable type text
HINT:  Use the COLLATE clause to set the collation explicitly.
-- ideally this would be a parse-time error, but for now it must be run-time:
select x < y from collate_test10; -- fail
ERROR:  could not determine which collation to use for string comparison
HINT:  Use the COLLATE clause to set the collation explicitly.
select x || y from collate_test10; -- ok, because || is not collation aware
 ?column? 
----------
 hijhij
 HIJHIJ
(2 rows)

select x, y from collate_test10 order by x || y; -- not so ok
ERROR:  collation mismatch between implicit collations "en-x-icu" and "tr-x-icu"
LINE 1: select x, y from collate_test10 order by x || y;
                                                      ^
HINT:  You can choose the collation by applying the COLLATE clause to one or both expressions.
-- collation mismatch between recursive and non-recursive term
WITH RECURSIVE foo(x) AS
   (SELECT x FROM (VALUES('a' COLLATE "en-x-icu"),('b')) t(x)
   UNION ALL
   SELECT (x || 'c') COLLATE "de-x-icu" FROM foo WHERE length(x) < 10)
SELECT * FROM foo;
ERROR:  recursive query "foo" column 1 has collation "en-x-icu" in non-recursive term but collation "de-x-icu" overall
LINE 2:    (SELECT x FROM (VALUES('a' COLLATE "en-x-icu"),('b')) t(x...
                   ^
HINT:  Use the COLLATE clause to set the collation of the non-recursive term.
-- casting
SELECT CAST('42' AS text COLLATE "C");
ERROR:  syntax error at or near "COLLATE"
LINE 1: SELECT CAST('42' AS text COLLATE "C");
                                 ^
SELECT a, CAST(b AS varchar) FROM collate_test1 ORDER BY 2;
 a |  b  
---+-----
 1 | abc
 4 | ABC
 2 | äbc
 3 | bbc
(4 rows)

SELECT a, CAST(b AS varchar) FROM collate_test2 ORDER BY 2;
 a |  b  
---+-----
 1 | abc
 4 | ABC
 3 | bbc
 2 | äbc
(4 rows)

SELECT a, CAST(b AS varchar) FROM collate_test3 ORDER BY 2;
 a |  b  
---+-----
 4 | ABC
 1 | abc
 3 | bbc
 2 | äbc
(4 rows)

-- propagation of collation in SQL functions (inlined and non-inlined cases)
-- and plpgsql functions too
CREATE FUNCTION mylt (text, text) RETURNS boolean LANGUAGE sql
    AS $$ select $1 < $2 $$;
CREATE FUNCTION mylt_noninline (text, text) RETURNS boolean LANGUAGE sql
    AS $$ select $1 < $2 limit 1 $$;
CREATE FUNCTION mylt_plpgsql (text, text) RETURNS boolean LANGUAGE plpgsql
    AS $$ begin return $1 < $2; end $$;
SELECT a.b AS a, b.b AS b, a.b < b.b AS lt,
       mylt(a.b, b.b), mylt_noninline(a.b, b.b), mylt_plpgsql(a.b, b.b)
FROM collate_test1 a, collate_test1 b
ORDER BY a.b, b.b;
  a  |  b  | lt | mylt | mylt_noninline | mylt_plpgsql 
-----+-----+----+------+----------------+--------------
 abc | abc | f  | f    | f              | f
 abc | ABC | t  | t    | t              | t
 abc | äbc | t  | t    | t              | t
 abc | bbc | t  | t    | t              | t
 ABC | abc | f  | f    | f              | f
 ABC | ABC | f  | f    | f              | f
 ABC | äbc | t  | t    | t              | t
 ABC | bbc | t  | t    | t              | t
 äbc | abc | f  | f    | f              | f
 äbc | ABC | f  | f    | f              | f
 äbc | äbc | f  | f    | f              | f
 äbc | bbc | t  | t    | t              | t
 bbc | abc | f  | f    | f              | f
 bbc | ABC | f  | f    | f              | f
 bbc | äbc | f  | f    | f              | f
 bbc | bbc | f  | f    | f              | f
(16 rows)

SELECT a.b AS a, b.b AS b, a.b < b.b COLLATE "C" AS lt,
       mylt(a.b, b.b COLLATE "C"), mylt_noninline(a.b, b.b COLLATE "C"),
       mylt_plpgsql(a.b, b.b COLLATE "C")
FROM collate_test1 a, collate_test1 b
ORDER BY a.b, b.b;
  a  |  b  | lt | mylt | mylt_noninline | mylt_plpgsql 
-----+-----+----+------+----------------+--------------
 abc | abc | f  | f    | f              | f
 abc | ABC | f  | f    | f              | f
 abc | äbc | t  | t    | t              | t
 abc | bbc | t  | t    | t              | t
 ABC | abc | t  | t    | t              | t
 ABC | ABC | f  | f    | f              | f
 ABC | äbc | t  | t    | t              | t
 ABC | bbc | t  | t    | t              | t
 äbc | abc | f  | f    | f              | f
 äbc | ABC | f  | f    | f              | f
 äbc | äbc | f  | f    | f              | f
 äbc | bbc | f  | f    | f              | f
 bbc | abc | f  | f    | f              | f
 bbc | ABC | f  | f    | f              | f
 bbc | äbc | t  | t    | t              | t
 bbc | bbc | f  | f    | f              | f
(16 rows)

-- collation override in plpgsql
CREATE FUNCTION mylt2 (x text, y text) RETURNS boolean LANGUAGE plpgsql AS $$
declare
  xx text := x;
  yy text := y;
begin
  return xx < yy;
end
$$;
SELECT mylt2('a', 'B' collate "en-x-icu") as t, mylt2('a', 'B' collate "C") as f;
 t | f 
---+---
 t | f
(1 row)

CREATE OR REPLACE FUNCTION
  mylt2 (x text, y text) RETURNS boolean LANGUAGE plpgsql AS $$
declare
  xx text COLLATE "POSIX" := x;
  yy text := y;
begin
  return xx < yy;
end
$$;
SELECT mylt2('a', 'B') as f;
 f 
---
 f
(1 row)

SELECT mylt2('a', 'B' collate "C") as fail; -- conflicting collations
ERROR:  could not determine which collation to use for string comparison
HINT:  Use the COLLATE clause to set the collation explicitly.
CONTEXT:  PL/pgSQL function mylt2(text,text) line 6 at RETURN
SELECT mylt2('a', 'B' collate "POSIX") as f;
 f 
---
 f
(1 row)

-- polymorphism
SELECT * FROM unnest((SELECT array_agg(b ORDER BY b) FROM collate_test1)) ORDER BY 1;
 unnest 
--------
 abc
 ABC
 äbc
 bbc
(4 rows)

SELECT * FROM unnest((SELECT array_agg(b ORDER BY b) FROM collate_test2)) ORDER BY 1;
 unnest 
--------
 abc
 ABC
 bbc
 äbc
(4 rows)

SELECT * FROM unnest((SELECT array_agg(b ORDER BY b) FROM collate_test3)) ORDER BY 1;
 unnest 
--------
 ABC
 abc
 bbc
 äbc
(4 rows)

CREATE FUNCTION dup (anyelement) RETURNS anyelement
    AS 'select $1' LANGUAGE sql;
SELECT a, dup(b) FROM collate_test1 ORDER BY 2;
 a | dup 
---+-----
 1 | abc
 4 | ABC
 2 | äbc
 3 | bbc
(4 rows)

SELECT a, dup(b) FROM collate_test2 ORDER BY 2;
 a | dup 
---+-----
 1 | abc
 4 | ABC
 3 | bbc
 2 | äbc
(4 rows)

SELECT a, dup(b) FROM collate_test3 ORDER BY 2;
 a | dup 
---+-----
 4 | ABC
 1 | abc
 3 | bbc
 2 | äbc
(4 rows)

-- indexes
CREATE INDEX collate_test1_idx1 ON collate_test1 (b);
CREATE INDEX collate_test1_idx2 ON collate_test1 (b COLLATE "C");
CREATE INDEX collate_test1_idx3 ON collate_test1 ((b COLLATE "C")); -- this is different grammatically
CREATE INDEX collate_test1_idx4 ON collate_test1 (((b||'foo') COLLATE "POSIX"));
CREATE INDEX collate_test1_idx5 ON collate_test1 (a COLLATE "C"); -- fail
ERROR:  collations are not supported by type integer
CREATE INDEX collate_test1_idx6 ON collate_test1 ((a COLLATE "C")); -- fail
ERROR:  collations are not supported by type integer
LINE 1: ...ATE INDEX collate_test1_idx6 ON collate_test1 ((a COLLATE "C...
                                                             ^
SELECT relname, pg_get_indexdef(oid) FROM pg_class WHERE relname LIKE 'collate_test%_idx%' ORDER BY 1;
      relname       |                                                  pg_get_indexdef                                                  
--------------------+-------------------------------------------------------------------------------------------------------------------
 collate_test1_idx1 | CREATE INDEX collate_test1_idx1 ON collate_tests.collate_test1 USING btree (b)
 collate_test1_idx2 | CREATE INDEX collate_test1_idx2 ON collate_tests.collate_test1 USING btree (b COLLATE "C")
 collate_test1_idx3 | CREATE INDEX collate_test1_idx3 ON collate_tests.collate_test1 USING btree (b COLLATE "C")
 collate_test1_idx4 | CREATE INDEX collate_test1_idx4 ON collate_tests.collate_test1 USING btree (((b || 'foo'::text)) COLLATE "POSIX")
(4 rows)

set enable_seqscan = off;
explain (costs off)
select * from collate_test1 where b ilike 'abc';
          QUERY PLAN           
-------------------------------
 Seq Scan on collate_test1
   Filter: (b ~~* 'abc'::text)
(2 rows)

select * from collate_test1 where b ilike 'abc';
 a |  b  
---+-----
 1 | abc
 4 | ABC
(2 rows)

explain (costs off)
select * from collate_test1 where b ilike 'ABC';
          QUERY PLAN           
-------------------------------
 Seq Scan on collate_test1
   Filter: (b ~~* 'ABC'::text)
(2 rows)

select * from collate_test1 where b ilike 'ABC';
 a |  b  
---+-----
 1 | abc
 4 | ABC
(2 rows)

reset enable_seqscan;
-- schema manipulation commands
CREATE ROLE regress_test_role;
CREATE SCHEMA test_schema;
-- We need to do this this way to cope with varying names for encodings:
do $$
BEGIN
  EXECUTE 'CREATE COLLATION test0 (provider = icu, locale = ' ||
          quote_literal(current_setting('lc_collate')) || ');';
END
$$;
CREATE COLLATION test0 FROM "C"; -- fail, duplicate name
ERROR:  collation "test0" already exists
do $$
BEGIN
  EXECUTE 'CREATE COLLATION test1 (provider = icu, locale = ' ||
          quote_literal(current_setting('lc_collate')) || ');';
END
$$;
CREATE COLLATION test3 (provider = icu, lc_collate = 'en_US.utf8'); -- fail, needs "locale"
ERROR:  parameter "locale" must be specified
CREATE COLLATION testx (provider = icu, locale = 'nonsense'); /* never fails with ICU */  DROP COLLATION testx;
CREATE COLLATION test4 FROM nonsense;
ERROR:  collation "nonsense" for encoding "UTF8" does not exist
CREATE COLLATION test5 FROM test0;
SELECT collname FROM pg_collation WHERE collname LIKE 'test%' ORDER BY 1;
 collname 
----------
 test0
 test1
 test5
(3 rows)

ALTER COLLATION test1 RENAME TO test11;
ALTER COLLATION test0 RENAME TO test11; -- fail
ERROR:  collation "test11" already exists in schema "collate_tests"
ALTER COLLATION test1 RENAME TO test22; -- fail
ERROR:  collation "test1" for encoding "UTF8" does not exist
ALTER COLLATION test11 OWNER TO regress_test_role;
ALTER COLLATION test11 OWNER TO nonsense;
ERROR:  role "nonsense" does not exist
ALTER COLLATION test11 SET SCHEMA test_schema;
COMMENT ON COLLATION test0 IS 'US English';
SELECT collname, nspname, obj_description(pg_collation.oid, 'pg_collation')
    FROM pg_collation JOIN pg_namespace ON (collnamespace = pg_namespace.oid)
    WHERE collname LIKE 'test%'
    ORDER BY 1;
 collname |    nspname    | obj_description 
----------+---------------+-----------------
 test0    | collate_tests | US English
 test11   | test_schema   | 
 test5    | collate_tests | 
(3 rows)

DROP COLLATION test0, test_schema.test11, test5;
DROP COLLATION test0; -- fail
ERROR:  collation "test0" for encoding "UTF8" does not exist
DROP COLLATION IF EXISTS test0;
NOTICE:  collation "test0" does not exist, skipping
SELECT collname FROM pg_collation WHERE collname LIKE 'test%';
 collname 
----------
(0 rows)

DROP SCHEMA test_schema;
DROP ROLE regress_test_role;
-- ALTER
ALTER COLLATION "en-x-icu" REFRESH VERSION;
NOTICE:  version has not changed
-- also test for database while we are here
SELECT current_database() AS datname \gset
ALTER DATABASE :"datname" REFRESH COLLATION VERSION;
NOTICE:  version has not changed
-- dependencies
CREATE COLLATION test0 FROM "C";
CREATE TABLE collate_dep_test1 (a int, b text COLLATE test0);
CREATE DOMAIN collate_dep_dom1 AS text COLLATE test0;
CREATE TYPE collate_dep_test2 AS (x int, y text COLLATE test0);
CREATE VIEW collate_dep_test3 AS SELECT text 'foo' COLLATE test0 AS foo;
CREATE TABLE collate_dep_test4t (a int, b text);
CREATE INDEX collate_dep_test4i ON collate_dep_test4t (b COLLATE test0);
DROP COLLATION test0 RESTRICT; -- fail
ERROR:  cannot drop collation test0 because other objects depend on it
DETAIL:  column b of table collate_dep_test1 depends on collation test0
type collate_dep_dom1 depends on collation test0
column y of composite type collate_dep_test2 depends on collation test0
view collate_dep_test3 depends on collation test0
index collate_dep_test4i depends on collation test0
HINT:  Use DROP ... CASCADE to drop the dependent objects too.
DROP COLLATION test0 CASCADE;
NOTICE:  drop cascades to 5 other objects
DETAIL:  drop cascades to column b of table collate_dep_test1
drop cascades to type collate_dep_dom1
drop cascades to column y of composite type collate_dep_test2
drop cascades to view collate_dep_test3
drop cascades to index collate_dep_test4i
\d collate_dep_test1
      Table "collate_tests.collate_dep_test1"
 Column |  Type   | Collation | Nullable | Default 
--------+---------+-----------+----------+---------
 a      | integer |           |          | 

\d collate_dep_test2
 Composite type "collate_tests.collate_dep_test2"
 Column |  Type   | Collation | Nullable | Default 
--------+---------+-----------+----------+---------
 x      | integer |           |          | 

DROP TABLE collate_dep_test1, collate_dep_test4t;
DROP TYPE collate_dep_test2;
-- test range types and collations
create type textrange_c as range(subtype=text, collation="C");
create type textrange_en_us as range(subtype=text, collation="en-x-icu");
select textrange_c('A','Z') @> 'b'::text;
 ?column? 
----------
 f
(1 row)

select textrange_en_us('A','Z') @> 'b'::text;
 ?column? 
----------
 t
(1 row)

drop type textrange_c;
drop type textrange_en_us;
-- test ICU collation customization
-- test the attributes handled by icu_set_collation_attributes()
CREATE COLLATION testcoll_ignore_accents (provider = icu, locale = '@colStrength=primary;colCaseLevel=yes');
SELECT 'aaá' > 'AAA' COLLATE "und-x-icu", 'aaá' < 'AAA' COLLATE testcoll_ignore_accents;
 ?column? | ?column? 
----------+----------
 t        | t
(1 row)

CREATE COLLATION testcoll_backwards (provider = icu, locale = '@colBackwards=yes');
SELECT 'coté' < 'côte' COLLATE "und-x-icu", 'coté' > 'côte' COLLATE testcoll_backwards;
 ?column? | ?column? 
----------+----------
 t        | t
(1 row)

CREATE COLLATION testcoll_lower_first (provider = icu, locale = '@colCaseFirst=lower');
CREATE COLLATION testcoll_upper_first (provider = icu, locale = '@colCaseFirst=upper');
SELECT 'aaa' < 'AAA' COLLATE testcoll_lower_first, 'aaa' > 'AAA' COLLATE testcoll_upper_first;
 ?column? | ?column? 
----------+----------
 t        | t
(1 row)

CREATE COLLATION testcoll_shifted (provider = icu, locale = '@colAlternate=shifted');
SELECT 'de-luge' < 'deanza' COLLATE "und-x-icu", 'de-luge' > 'deanza' COLLATE testcoll_shifted;
 ?column? | ?column? 
----------+----------
 t        | t
(1 row)

CREATE COLLATION testcoll_numeric (provider = icu, locale = '@colNumeric=yes');
SELECT 'A-21' > 'A-123' COLLATE "und-x-icu", 'A-21' < 'A-123' COLLATE testcoll_numeric;
 ?column? | ?column? 
----------+----------
 t        | t
(1 row)

CREATE COLLATION testcoll_error1 (provider = icu, locale = '@colNumeric=lower');
ERROR:  could not open collator for locale "@colNumeric=lower": U_ILLEGAL_ARGUMENT_ERROR
-- test that attributes not handled by icu_set_collation_attributes()
-- (handled by ucol_open() directly) also work
CREATE COLLATION testcoll_de_phonebook (provider = icu, locale = 'de@collation=phonebook');
SELECT 'Goldmann' < 'Götz' COLLATE "de-x-icu", 'Goldmann' > 'Götz' COLLATE testcoll_de_phonebook;
 ?column? | ?column? 
----------+----------
 t        | t
(1 row)

-- nondeterministic collations
CREATE COLLATION ctest_det (provider = icu, locale = '', deterministic = true);
CREATE COLLATION ctest_nondet (provider = icu, locale = '', deterministic = false);
CREATE TABLE test6 (a int, b text);
-- same string in different normal forms
INSERT INTO test6 VALUES (1, U&'\00E4bc');
INSERT INTO test6 VALUES (2, U&'\0061\0308bc');
SELECT * FROM test6;
 a |  b  
---+-----
 1 | äbc
 2 | äbc
(2 rows)

SELECT * FROM test6 WHERE b = 'äbc' COLLATE ctest_det;
 a |  b  
---+-----
 1 | äbc
(1 row)

SELECT * FROM test6 WHERE b = 'äbc' COLLATE ctest_nondet;
 a |  b  
---+-----
 1 | äbc
 2 | äbc
(2 rows)

-- same with arrays
CREATE TABLE test6a (a int, b text[]);
INSERT INTO test6a VALUES (1, ARRAY[U&'\00E4bc']);
INSERT INTO test6a VALUES (2, ARRAY[U&'\0061\0308bc']);
SELECT * FROM test6a;
 a |   b   
---+-------
 1 | {äbc}
 2 | {äbc}
(2 rows)

SELECT * FROM test6a WHERE b = ARRAY['äbc'] COLLATE ctest_det;
 a |   b   
---+-------
 1 | {äbc}
(1 row)

SELECT * FROM test6a WHERE b = ARRAY['äbc'] COLLATE ctest_nondet;
 a |   b   
---+-------
 1 | {äbc}
 2 | {äbc}
(2 rows)

CREATE COLLATION case_sensitive (provider = icu, locale = '');
CREATE COLLATION case_insensitive (provider = icu, locale = '@colStrength=secondary', deterministic = false);
SELECT 'abc' <= 'ABC' COLLATE case_sensitive, 'abc' >= 'ABC' COLLATE case_sensitive;
 ?column? | ?column? 
----------+----------
 t        | f
(1 row)

SELECT 'abc' <= 'ABC' COLLATE case_insensitive, 'abc' >= 'ABC' COLLATE case_insensitive;
 ?column? | ?column? 
----------+----------
 t        | t
(1 row)

CREATE TABLE test1cs (x text COLLATE case_sensitive);
CREATE TABLE test2cs (x text COLLATE case_sensitive);
CREATE TABLE test3cs (x text COLLATE case_sensitive);
INSERT INTO test1cs VALUES ('abc'), ('def'), ('ghi');
INSERT INTO test2cs VALUES ('ABC'), ('ghi');
INSERT INTO test3cs VALUES ('abc'), ('ABC'), ('def'), ('ghi');
SELECT x FROM test3cs WHERE x = 'abc';
  x  
-----
 abc
(1 row)

SELECT x FROM test3cs WHERE x <> 'abc';
  x  
-----
 ABC
 def
 ghi
(3 rows)

SELECT x FROM test3cs WHERE x LIKE 'a%';
  x  
-----
 abc
(1 row)

SELECT x FROM test3cs WHERE x ILIKE 'a%';
  x  
-----
 abc
 ABC
(2 rows)

SELECT x FROM test3cs WHERE x SIMILAR TO 'a%';
  x  
-----
 abc
(1 row)

SELECT x FROM test3cs WHERE x ~ 'a';
  x  
-----
 abc
(1 row)

SELECT x FROM test1cs UNION SELECT x FROM test2cs ORDER BY x;
  x  
-----
 abc
 ABC
 def
 ghi
(4 rows)

SELECT x FROM test2cs UNION SELECT x FROM test1cs ORDER BY x;
  x  
-----
 abc
 ABC
 def
 ghi
(4 rows)

SELECT x FROM test1cs INTERSECT SELECT x FROM test2cs;
  x  
-----
 ghi
(1 row)

SELECT x FROM test2cs INTERSECT SELECT x FROM test1cs;
  x  
-----
 ghi
(1 row)

SELECT x FROM test1cs EXCEPT SELECT x FROM test2cs;
  x  
-----
 abc
 def
(2 rows)

SELECT x FROM test2cs EXCEPT SELECT x FROM test1cs;
  x  
-----
 ABC
(1 row)

SELECT DISTINCT x FROM test3cs ORDER BY x;
  x  
-----
 abc
 ABC
 def
 ghi
(4 rows)

SELECT count(DISTINCT x) FROM test3cs;
 count 
-------
     4
(1 row)

SELECT x, count(*) FROM test3cs GROUP BY x ORDER BY x;
  x  | count 
-----+-------
 abc |     1
 ABC |     1
 def |     1
 ghi |     1
(4 rows)

SELECT x, row_number() OVER (ORDER BY x), rank() OVER (ORDER BY x) FROM test3cs ORDER BY x;
  x  | row_number | rank 
-----+------------+------
 abc |          1 |    1
 ABC |          2 |    2
 def |          3 |    3
 ghi |          4 |    4
(4 rows)

CREATE UNIQUE INDEX ON test1cs (x);  -- ok
INSERT INTO test1cs VALUES ('ABC');  -- ok
CREATE UNIQUE INDEX ON test3cs (x);  -- ok
SELECT string_to_array('ABC,DEF,GHI' COLLATE case_sensitive, ',', 'abc');
 string_to_array 
-----------------
 {ABC,DEF,GHI}
(1 row)

SELECT string_to_array('ABCDEFGHI' COLLATE case_sensitive, NULL, 'b');
   string_to_array   
---------------------
 {A,B,C,D,E,F,G,H,I}
(1 row)

CREATE TABLE test1ci (x text COLLATE case_insensitive);
CREATE TABLE test2ci (x text COLLATE case_insensitive);
CREATE TABLE test3ci (x text COLLATE case_insensitive);
CREATE INDEX ON test3ci (x text_pattern_ops);  -- error
ERROR:  nondeterministic collations are not supported for operator class "text_pattern_ops"
INSERT INTO test1ci VALUES ('abc'), ('def'), ('ghi');
INSERT INTO test2ci VALUES ('ABC'), ('ghi');
INSERT INTO test3ci VALUES ('abc'), ('ABC'), ('def'), ('ghi');
SELECT x FROM test3ci WHERE x = 'abc';
  x  
-----
 abc
 ABC
(2 rows)

SELECT x FROM test3ci WHERE x <> 'abc';
  x  
-----
 def
 ghi
(2 rows)

SELECT x FROM test3ci WHERE x LIKE 'a%';
ERROR:  nondeterministic collations are not supported for LIKE
SELECT x FROM test3ci WHERE x ILIKE 'a%';
ERROR:  nondeterministic collations are not supported for ILIKE
SELECT x FROM test3ci WHERE x SIMILAR TO 'a%';
ERROR:  nondeterministic collations are not supported for regular expressions
SELECT x FROM test3ci WHERE x ~ 'a';
ERROR:  nondeterministic collations are not supported for regular expressions
SELECT x FROM test1ci UNION SELECT x FROM test2ci ORDER BY x;
  x  
-----
 abc
 def
 ghi
(3 rows)

SELECT x FROM test2ci UNION SELECT x FROM test1ci ORDER BY x;
  x  
-----
 ABC
 def
 ghi
(3 rows)

SELECT x FROM test1ci INTERSECT SELECT x FROM test2ci ORDER BY x;
  x  
-----
 abc
 ghi
(2 rows)

SELECT x FROM test2ci INTERSECT SELECT x FROM test1ci ORDER BY x;
  x  
-----
 ABC
 ghi
(2 rows)

SELECT x FROM test1ci EXCEPT SELECT x FROM test2ci;
  x  
-----
 def
(1 row)

SELECT x FROM test2ci EXCEPT SELECT x FROM test1ci;
 x 
---
(0 rows)

SELECT DISTINCT x FROM test3ci ORDER BY x;
  x  
-----
 abc
 def
 ghi
(3 rows)

SELECT count(DISTINCT x) FROM test3ci;
 count 
-------
     3
(1 row)

SELECT x, count(*) FROM test3ci GROUP BY x ORDER BY x;
  x  | count 
-----+-------
 abc |     2
 def |     1
 ghi |     1
(3 rows)

SELECT x, row_number() OVER (ORDER BY x), rank() OVER (ORDER BY x) FROM test3ci ORDER BY x;
  x  | row_number | rank 
-----+------------+------
 abc |          1 |    1
 ABC |          2 |    1
 def |          3 |    3
 ghi |          4 |    4
(4 rows)

CREATE UNIQUE INDEX ON test1ci (x);  -- ok
INSERT INTO test1ci VALUES ('ABC');  -- error
ERROR:  duplicate key value violates unique constraint "test1ci_x_idx"
DETAIL:  Key (x)=(ABC) already exists.
CREATE UNIQUE INDEX ON test3ci (x);  -- error
ERROR:  could not create unique index "test3ci_x_idx"
DETAIL:  Key (x)=(abc) is duplicated.
SELECT string_to_array('ABC,DEF,GHI' COLLATE case_insensitive, ',', 'abc');
ERROR:  nondeterministic collations are not supported for substring searches
SELECT string_to_array('ABCDEFGHI' COLLATE case_insensitive, NULL, 'b');
    string_to_array     
------------------------
 {A,NULL,C,D,E,F,G,H,I}
(1 row)

-- bpchar
CREATE TABLE test1bpci (x char(3) COLLATE case_insensitive);
CREATE TABLE test2bpci (x char(3) COLLATE case_insensitive);
CREATE TABLE test3bpci (x char(3) COLLATE case_insensitive);
CREATE INDEX ON test3bpci (x bpchar_pattern_ops);  -- error
ERROR:  nondeterministic collations are not supported for operator class "bpchar_pattern_ops"
INSERT INTO test1bpci VALUES ('abc'), ('def'), ('ghi');
INSERT INTO test2bpci VALUES ('ABC'), ('ghi');
INSERT INTO test3bpci VALUES ('abc'), ('ABC'), ('def'), ('ghi');
SELECT x FROM test3bpci WHERE x = 'abc';
  x  
-----
 abc
 ABC
(2 rows)

SELECT x FROM test3bpci WHERE x <> 'abc';
  x  
-----
 def
 ghi
(2 rows)

SELECT x FROM test3bpci WHERE x LIKE 'a%';
ERROR:  nondeterministic collations are not supported for LIKE
SELECT x FROM test3bpci WHERE x ILIKE 'a%';
ERROR:  nondeterministic collations are not supported for ILIKE
SELECT x FROM test3bpci WHERE x SIMILAR TO 'a%';
ERROR:  nondeterministic collations are not supported for regular expressions
SELECT x FROM test3bpci WHERE x ~ 'a';
ERROR:  nondeterministic collations are not supported for regular expressions
SELECT x FROM test1bpci UNION SELECT x FROM test2bpci ORDER BY x;
  x  
-----
 abc
 def
 ghi
(3 rows)

SELECT x FROM test2bpci UNION SELECT x FROM test1bpci ORDER BY x;
  x  
-----
 ABC
 def
 ghi
(3 rows)

SELECT x FROM test1bpci INTERSECT SELECT x FROM test2bpci ORDER BY x;
  x  
-----
 abc
 ghi
(2 rows)

SELECT x FROM test2bpci INTERSECT SELECT x FROM test1bpci ORDER BY x;
  x  
-----
 ABC
 ghi
(2 rows)

SELECT x FROM test1bpci EXCEPT SELECT x FROM test2bpci;
  x  
-----
 def
(1 row)

SELECT x FROM test2bpci EXCEPT SELECT x FROM test1bpci;
 x 
---
(0 rows)

SELECT DISTINCT x FROM test3bpci ORDER BY x;
  x  
-----
 abc
 def
 ghi
(3 rows)

SELECT count(DISTINCT x) FROM test3bpci;
 count 
-------
     3
(1 row)

SELECT x, count(*) FROM test3bpci GROUP BY x ORDER BY x;
  x  | count 
-----+-------
 abc |     2
 def |     1
 ghi |     1
(3 rows)

SELECT x, row_number() OVER (ORDER BY x), rank() OVER (ORDER BY x) FROM test3bpci ORDER BY x;
  x  | row_number | rank 
-----+------------+------
 abc |          1 |    1
 ABC |          2 |    1
 def |          3 |    3
 ghi |          4 |    4
(4 rows)

CREATE UNIQUE INDEX ON test1bpci (x);  -- ok
INSERT INTO test1bpci VALUES ('ABC');  -- error
ERROR:  duplicate key value violates unique constraint "test1bpci_x_idx"
DETAIL:  Key (x)=(ABC) already exists.
CREATE UNIQUE INDEX ON test3bpci (x);  -- error
ERROR:  could not create unique index "test3bpci_x_idx"
DETAIL:  Key (x)=(abc) is duplicated.
SELECT string_to_array('ABC,DEF,GHI'::char(11) COLLATE case_insensitive, ',', 'abc');
ERROR:  nondeterministic collations are not supported for substring searches
SELECT string_to_array('ABCDEFGHI'::char(9) COLLATE case_insensitive, NULL, 'b');
    string_to_array     
------------------------
 {A,NULL,C,D,E,F,G,H,I}
(1 row)

-- This tests the issue described in match_pattern_prefix().  In the
-- absence of that check, the case_insensitive tests below would
-- return no rows where they should logically return one.
CREATE TABLE test4c (x text COLLATE "C");
INSERT INTO test4c VALUES ('abc');
CREATE INDEX ON test4c (x);
SET enable_seqscan = off;
SELECT x FROM test4c WHERE x LIKE 'ABC' COLLATE case_sensitive;  -- ok, no rows
 x 
---
(0 rows)

SELECT x FROM test4c WHERE x LIKE 'ABC%' COLLATE case_sensitive;  -- ok, no rows
 x 
---
(0 rows)

SELECT x FROM test4c WHERE x LIKE 'ABC' COLLATE case_insensitive;  -- error
ERROR:  nondeterministic collations are not supported for LIKE
SELECT x FROM test4c WHERE x LIKE 'ABC%' COLLATE case_insensitive;  -- error
ERROR:  nondeterministic collations are not supported for LIKE
RESET enable_seqscan;
-- Unicode special case: different variants of Greek lower case sigma.
-- A naive implementation like citext that just does lower(x) =
-- lower(y) will do the wrong thing here, because lower('Σ') is 'σ'
-- but upper('ς') is 'Σ'.
SELECT 'ὀδυσσεύς' = 'ὈΔΥΣΣΕΎΣ' COLLATE case_sensitive;
 ?column? 
----------
 f
(1 row)

SELECT 'ὀδυσσεύς' = 'ὈΔΥΣΣΕΎΣ' COLLATE case_insensitive;
 ?column? 
----------
 t
(1 row)

-- name vs. text comparison operators
SELECT relname FROM pg_class WHERE relname = 'PG_CLASS'::text COLLATE case_insensitive;
 relname  
----------
 pg_class
(1 row)

SELECT relname FROM pg_class WHERE 'PG_CLASS'::text = relname COLLATE case_insensitive;
 relname  
----------
 pg_class
(1 row)

SELECT typname FROM pg_type WHERE typname LIKE 'int_' AND typname <> 'INT2'::text
  COLLATE case_insensitive ORDER BY typname;
 typname 
---------
 int4
 int8
(2 rows)

SELECT typname FROM pg_type WHERE typname LIKE 'int_' AND 'INT2'::text <> typname
  COLLATE case_insensitive ORDER BY typname;
 typname 
---------
 int4
 int8
(2 rows)

-- test case adapted from subselect.sql
CREATE TEMP TABLE outer_text (f1 text COLLATE case_insensitive, f2 text);
INSERT INTO outer_text VALUES ('a', 'a');
INSERT INTO outer_text VALUES ('b', 'a');
INSERT INTO outer_text VALUES ('A', NULL);
INSERT INTO outer_text VALUES ('B', NULL);
CREATE TEMP TABLE inner_text (c1 text COLLATE case_insensitive, c2 text);
INSERT INTO inner_text VALUES ('a', NULL);
SELECT * FROM outer_text WHERE (f1, f2) NOT IN (SELECT * FROM inner_text);
 f1 | f2 
----+----
 b  | a
 B  | 
(2 rows)

-- accents
CREATE COLLATION ignore_accents (provider = icu, locale = '@colStrength=primary;colCaseLevel=yes', deterministic = false);
CREATE TABLE test4 (a int, b text);
INSERT INTO test4 VALUES (1, 'cote'), (2, 'côte'), (3, 'coté'), (4, 'côté');
SELECT * FROM test4 WHERE b = 'cote';
 a |  b   
---+------
 1 | cote
(1 row)

SELECT * FROM test4 WHERE b = 'cote' COLLATE ignore_accents;
 a |  b   
---+------
 1 | cote
 2 | côte
 3 | coté
 4 | côté
(4 rows)

SELECT * FROM test4 WHERE b = 'Cote' COLLATE ignore_accents;  -- still case-sensitive
 a | b 
---+---
(0 rows)

SELECT * FROM test4 WHERE b = 'Cote' COLLATE case_insensitive;
 a |  b   
---+------
 1 | cote
(1 row)

-- foreign keys (should use collation of primary key)
-- PK is case-sensitive, FK is case-insensitive
CREATE TABLE test10pk (x text COLLATE case_sensitive PRIMARY KEY);
INSERT INTO test10pk VALUES ('abc'), ('def'), ('ghi');
CREATE TABLE test10fk (x text COLLATE case_insensitive REFERENCES test10pk (x) ON UPDATE CASCADE ON DELETE CASCADE);
INSERT INTO test10fk VALUES ('abc');  -- ok
INSERT INTO test10fk VALUES ('ABC');  -- error
ERROR:  insert or update on table "test10fk" violates foreign key constraint "test10fk_x_fkey"
DETAIL:  Key (x)=(ABC) is not present in table "test10pk".
INSERT INTO test10fk VALUES ('xyz');  -- error
ERROR:  insert or update on table "test10fk" violates foreign key constraint "test10fk_x_fkey"
DETAIL:  Key (x)=(xyz) is not present in table "test10pk".
SELECT * FROM test10pk;
  x  
-----
 abc
 def
 ghi
(3 rows)

SELECT * FROM test10fk;
  x  
-----
 abc
(1 row)

-- restrict update even though the values are "equal" in the FK table
UPDATE test10fk SET x = 'ABC' WHERE x = 'abc';  -- error
ERROR:  insert or update on table "test10fk" violates foreign key constraint "test10fk_x_fkey"
DETAIL:  Key (x)=(ABC) is not present in table "test10pk".
SELECT * FROM test10fk;
  x  
-----
 abc
(1 row)

DELETE FROM test10pk WHERE x = 'abc';
SELECT * FROM test10pk;
  x  
-----
 def
 ghi
(2 rows)

SELECT * FROM test10fk;
 x 
---
(0 rows)

-- PK is case-insensitive, FK is case-sensitive
CREATE TABLE test11pk (x text COLLATE case_insensitive PRIMARY KEY);
INSERT INTO test11pk VALUES ('abc'), ('def'), ('ghi');
CREATE TABLE test11fk (x text COLLATE case_sensitive REFERENCES test11pk (x) ON UPDATE CASCADE ON DELETE CASCADE);
INSERT INTO test11fk VALUES ('abc');  -- ok
INSERT INTO test11fk VALUES ('ABC');  -- ok
INSERT INTO test11fk VALUES ('xyz');  -- error
ERROR:  insert or update on table "test11fk" violates foreign key constraint "test11fk_x_fkey"
DETAIL:  Key (x)=(xyz) is not present in table "test11pk".
SELECT * FROM test11pk;
  x  
-----
 abc
 def
 ghi
(3 rows)

SELECT * FROM test11fk;
  x  
-----
 abc
 ABC
(2 rows)

-- cascade update even though the values are "equal" in the PK table
UPDATE test11pk SET x = 'ABC' WHERE x = 'abc';
SELECT * FROM test11fk;
  x  
-----
 ABC
 ABC
(2 rows)

DELETE FROM test11pk WHERE x = 'abc';
SELECT * FROM test11pk;
  x  
-----
 def
 ghi
(2 rows)

SELECT * FROM test11fk;
 x 
---
(0 rows)

-- partitioning
CREATE TABLE test20 (a int, b text COLLATE case_insensitive) PARTITION BY LIST (b);
CREATE TABLE test20_1 PARTITION OF test20 FOR VALUES IN ('abc');
INSERT INTO test20 VALUES (1, 'abc');
INSERT INTO test20 VALUES (2, 'ABC');
SELECT * FROM test20_1;
 a |  b  
---+-----
 1 | abc
 2 | ABC
(2 rows)

CREATE TABLE test21 (a int, b text COLLATE case_insensitive) PARTITION BY RANGE (b);
CREATE TABLE test21_1 PARTITION OF test21 FOR VALUES FROM ('ABC') TO ('DEF');
INSERT INTO test21 VALUES (1, 'abc');
INSERT INTO test21 VALUES (2, 'ABC');
SELECT * FROM test21_1;
 a |  b  
---+-----
 1 | abc
 2 | ABC
(2 rows)

CREATE TABLE test22 (a int, b text COLLATE case_sensitive) PARTITION BY HASH (b);
CREATE TABLE test22_0 PARTITION OF test22 FOR VALUES WITH (MODULUS 2, REMAINDER 0);
CREATE TABLE test22_1 PARTITION OF test22 FOR VALUES WITH (MODULUS 2, REMAINDER 1);
INSERT INTO test22 VALUES (1, 'def');
INSERT INTO test22 VALUES (2, 'DEF');
-- they end up in different partitions
SELECT (SELECT count(*) FROM test22_0) = (SELECT count(*) FROM test22_1);
 ?column? 
----------
 t
(1 row)

-- same with arrays
CREATE TABLE test22a (a int, b text[] COLLATE case_sensitive) PARTITION BY HASH (b);
CREATE TABLE test22a_0 PARTITION OF test22a FOR VALUES WITH (MODULUS 2, REMAINDER 0);
CREATE TABLE test22a_1 PARTITION OF test22a FOR VALUES WITH (MODULUS 2, REMAINDER 1);
INSERT INTO test22a VALUES (1, ARRAY['def']);
INSERT INTO test22a VALUES (2, ARRAY['DEF']);
-- they end up in different partitions
SELECT (SELECT count(*) FROM test22a_0) = (SELECT count(*) FROM test22a_1);
 ?column? 
----------
 t
(1 row)

CREATE TABLE test23 (a int, b text COLLATE case_insensitive) PARTITION BY HASH (b);
CREATE TABLE test23_0 PARTITION OF test23 FOR VALUES WITH (MODULUS 2, REMAINDER 0);
CREATE TABLE test23_1 PARTITION OF test23 FOR VALUES WITH (MODULUS 2, REMAINDER 1);
INSERT INTO test23 VALUES (1, 'def');
INSERT INTO test23 VALUES (2, 'DEF');
-- they end up in the same partition (but it's platform-dependent which one)
SELECT (SELECT count(*) FROM test23_0) <> (SELECT count(*) FROM test23_1);
 ?column? 
----------
 t
(1 row)

-- same with arrays
CREATE TABLE test23a (a int, b text[] COLLATE case_insensitive) PARTITION BY HASH (b);
CREATE TABLE test23a_0 PARTITION OF test23a FOR VALUES WITH (MODULUS 2, REMAINDER 0);
CREATE TABLE test23a_1 PARTITION OF test23a FOR VALUES WITH (MODULUS 2, REMAINDER 1);
INSERT INTO test23a VALUES (1, ARRAY['def']);
INSERT INTO test23a VALUES (2, ARRAY['DEF']);
-- they end up in the same partition (but it's platform-dependent which one)
SELECT (SELECT count(*) FROM test23a_0) <> (SELECT count(*) FROM test23a_1);
 ?column? 
----------
 t
(1 row)

CREATE TABLE test30 (a int, b char(3) COLLATE case_insensitive) PARTITION BY LIST (b);
CREATE TABLE test30_1 PARTITION OF test30 FOR VALUES IN ('abc');
INSERT INTO test30 VALUES (1, 'abc');
INSERT INTO test30 VALUES (2, 'ABC');
SELECT * FROM test30_1;
 a |  b  
---+-----
 1 | abc
 2 | ABC
(2 rows)

CREATE TABLE test31 (a int, b char(3) COLLATE case_insensitive) PARTITION BY RANGE (b);
CREATE TABLE test31_1 PARTITION OF test31 FOR VALUES FROM ('ABC') TO ('DEF');
INSERT INTO test31 VALUES (1, 'abc');
INSERT INTO test31 VALUES (2, 'ABC');
SELECT * FROM test31_1;
 a |  b  
---+-----
 1 | abc
 2 | ABC
(2 rows)

CREATE TABLE test32 (a int, b char(3) COLLATE case_sensitive) PARTITION BY HASH (b);
CREATE TABLE test32_0 PARTITION OF test32 FOR VALUES WITH (MODULUS 2, REMAINDER 0);
CREATE TABLE test32_1 PARTITION OF test32 FOR VALUES WITH (MODULUS 2, REMAINDER 1);
INSERT INTO test32 VALUES (1, 'def');
INSERT INTO test32 VALUES (2, 'DEF');
-- they end up in different partitions
SELECT (SELECT count(*) FROM test32_0) = (SELECT count(*) FROM test32_1);
 ?column? 
----------
 t
(1 row)

CREATE TABLE test33 (a int, b char(3) COLLATE case_insensitive) PARTITION BY HASH (b);
CREATE TABLE test33_0 PARTITION OF test33 FOR VALUES WITH (MODULUS 2, REMAINDER 0);
CREATE TABLE test33_1 PARTITION OF test33 FOR VALUES WITH (MODULUS 2, REMAINDER 1);
INSERT INTO test33 VALUES (1, 'def');
INSERT INTO test33 VALUES (2, 'DEF');
-- they end up in the same partition (but it's platform-dependent which one)
SELECT (SELECT count(*) FROM test33_0) <> (SELECT count(*) FROM test33_1);
 ?column? 
----------
 t
(1 row)

-- cleanup
RESET search_path;
SET client_min_messages TO warning;
DROP SCHEMA collate_tests CASCADE;
RESET client_min_messages;
-- leave a collation for pg_upgrade test
CREATE COLLATION coll_icu_upgrade FROM "und-x-icu";