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
|
ij> --
-- Licensed to the Apache Software Foundation (ASF) under one or more
-- contributor license agreements. See the NOTICE file distributed with
-- this work for additional information regarding copyright ownership.
-- The ASF licenses this file to You under the Apache License, Version 2.0
-- (the "License"); you may not use this file except in compliance with
-- the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- tests for cast expressions
-- refer to casting.java for a complete analysis on casting
--==================================
--
-- simple test cases
--
--==================================
-- shrink/grow bit and char
-- no exceptions should be raised.
-- expect a warning when shrinking non-space
-- shrink
values (cast ('hell' as char(2)));
1
--
he
WARNING 01004: Data truncation
ij> values (cast ('hell' as varchar(2)));
1
--
he
WARNING 01004: Data truncation
ij> -- shrink, whitespace only
values (cast ('he ' as char(2)));
1
--
he
ij> -- expand, check lengths
values (cast ('hell' as char(20)));
1
--------------------
hell
ij> values (cast ('hell' as varchar(20)));
1
--------------------
hell
ij> values length(cast ('hell' as char(20)));
1
-----------
20
ij> values length(cast ('hell' as varchar(20)));
1
-----------
4
ij> ----------------
--char->bit data
----------------
-- shrink
values (cast (X'1111' as char(1) for bit data));
1
--
11
WARNING 01004: Data truncation
ij> -- shrink, zero only
values (cast (X'1100' as char(1) for bit data));
1
--
11
WARNING 01004: Data truncation
ij> -- expand
values (cast (X'1111' as char(2) for bit data));
1
----
1111
ij> -- w/o format
-- DB2 UDB PASS
-- DB2 CS FAIL
values (cast ('1234' as char(2) for bit data));
ERROR 42846: Cannot convert types 'CHAR' to 'CHAR () FOR BIT DATA'.
ij> -- extra tests for shrinking parts of bits
values cast (X'11111111' as char(1) for bit data);
1
--
11
WARNING 01004: Data truncation
ij> values cast (X'01111111' as char(1) for bit data);
1
--
01
WARNING 01004: Data truncation
ij> values cast (X'11111111' as char(1) for bit data);
1
--
11
WARNING 01004: Data truncation
ij> values cast (X'01111111' as char(1) for bit data);
1
--
01
WARNING 01004: Data truncation
ij> values cast (X'00111111' as char(1) for bit data);
1
--
00
WARNING 01004: Data truncation
ij> values cast (X'11111111' as char(1) for bit data);
1
--
11
WARNING 01004: Data truncation
ij> values cast (X'01111111' as char(1) for bit data);
1
--
01
WARNING 01004: Data truncation
ij> values cast (X'00111111' as char(1) for bit data);
1
--
00
WARNING 01004: Data truncation
ij> values cast (X'00011111' as char(1) for bit data);
1
--
00
WARNING 01004: Data truncation
ij> values cast (X'11111111' as char(1) for bit data);
1
--
11
WARNING 01004: Data truncation
ij> values cast (X'01111111' as char(1) for bit data);
1
--
01
WARNING 01004: Data truncation
ij> values cast (X'00111111' as char(1) for bit data);
1
--
00
WARNING 01004: Data truncation
ij> values cast (X'00011111' as char(1) for bit data);
1
--
00
WARNING 01004: Data truncation
ij> values cast (X'00001111' as char(1) for bit data);
1
--
00
WARNING 01004: Data truncation
ij> values cast (X'11111111' as char(1) for bit data);
1
--
11
WARNING 01004: Data truncation
ij> values cast (X'01111111' as char(1) for bit data);
1
--
01
WARNING 01004: Data truncation
ij> values cast (X'00111111' as char(1) for bit data);
1
--
00
WARNING 01004: Data truncation
ij> values cast (X'00011111' as char(1) for bit data);
1
--
00
WARNING 01004: Data truncation
ij> values cast (X'00001111' as char(1) for bit data);
1
--
00
WARNING 01004: Data truncation
ij> values cast (X'00000111' as char(1) for bit data);
1
--
00
WARNING 01004: Data truncation
ij> values cast (X'11111111' as char(1) for bit data);
1
--
11
WARNING 01004: Data truncation
ij> values cast (X'01111111' as char(1) for bit data);
1
--
01
WARNING 01004: Data truncation
ij> values cast (X'00111111' as char(1) for bit data);
1
--
00
WARNING 01004: Data truncation
ij> values cast (X'00011111' as char(1) for bit data);
1
--
00
WARNING 01004: Data truncation
ij> values cast (X'00001111' as char(1) for bit data);
1
--
00
WARNING 01004: Data truncation
ij> values cast (X'00000111' as char(1) for bit data);
1
--
00
WARNING 01004: Data truncation
ij> values cast (X'00000011' as char(1) for bit data);
1
--
00
WARNING 01004: Data truncation
ij> values cast (X'11111111' as char(1) for bit data);
1
--
11
WARNING 01004: Data truncation
ij> values cast (X'01111111' as char(1) for bit data);
1
--
01
WARNING 01004: Data truncation
ij> values cast (X'00111111' as char(1) for bit data);
1
--
00
WARNING 01004: Data truncation
ij> values cast (X'00011111' as char(1) for bit data);
1
--
00
WARNING 01004: Data truncation
ij> values cast (X'00001111' as char(1) for bit data);
1
--
00
WARNING 01004: Data truncation
ij> values cast (X'00000111' as char(1) for bit data);
1
--
00
WARNING 01004: Data truncation
ij> values cast (X'00000011' as char(1) for bit data);
1
--
00
WARNING 01004: Data truncation
ij> values cast (X'00000001' as char(1) for bit data);
1
--
00
WARNING 01004: Data truncation
ij> values cast (X'0011111111111111' as char(1) for bit data);
1
--
00
WARNING 01004: Data truncation
ij> values cast (X'1111111100111111' as char(2) for bit data);
1
----
1111
WARNING 01004: Data truncation
ij> ---------
--numbers
---------
values (cast (1.1 as int));
1
-----------
1
ij> values (cast (1.1 as smallint));
1
------
1
ij> values (cast (1.1 as bigint));
1
--------------------
1
ij> values (cast (1.1 as double precision));
1
------------------------
1.1
ij> values (cast (1.1 as numeric(2,1)));
1
----
1.1
ij> values (cast (1.1 as decimal(2,1)));
1
----
1.1
ij> values (cast (1.1 as numeric(2,0)));
1
---
1
ij> values (cast (1.1 as decimal(2,0)));
1
---
1
ij> values (cast (1.1 as float));
1
------------------------
1.1
ij> values (cast (1.1 as real));
1
---------------
1.1
ij> values (cast (1.9 as int));
1
-----------
1
ij> values (cast (1.9 as smallint));
1
------
1
ij> values (cast (1.9 as bigint));
1
--------------------
1
ij> values (cast (1.9 as double precision));
1
------------------------
1.9
ij> values (cast (1.9 as numeric(2,1)));
1
----
1.9
ij> values (cast (1.9 as decimal(2,1)));
1
----
1.9
ij> values (cast (1.9 as numeric(2,0)));
1
---
1
ij> values (cast (1.9 as decimal(2,0)));
1
---
1
ij> values (cast (1.9 as float));
1
------------------------
1.9
ij> values (cast (1.9 as real));
1
---------------
1.9
ij> -- bug 4352,4358 loss of precision on casts
-- 9223372036854775807 is Long::MAX_VALUE
values (
9223372036854775807,
cast (9223372036854775807 as DECIMAL(24,1)),
cast (
cast (9223372036854775807 as DECIMAL(24,1)) as BIGINT)
);
1 |2 |3
--------------------------------------------------------------------
9223372036854775807 |9223372036854775807.0 |9223372036854775807
ij> values (
cast ('9223372036854775807' as DECIMAL(24,1)),
cast (cast ('9223372036854775807' as DECIMAL(24,1)) as BIGINT)
);
1 |2
-----------------------------------------------
9223372036854775807.0 |9223372036854775807
ij> values (
cast ('9223372036854775806' as DECIMAL(24,1)),
cast (cast ('9223372036854775806' as DECIMAL(24,1)) as BIGINT)
);
1 |2
-----------------------------------------------
9223372036854775806.0 |9223372036854775806
ij> -- only this should fail
values (
cast ('9223372036854775808' as DECIMAL(24,1)),
cast (cast ('9223372036854775808' as DECIMAL(24,1)) as BIGINT)
);
1 |2
-----------------------------------------------
ERROR 22003: The resulting value is outside the range for the data type BIGINT.
ij> values (
cast ('9223372036854775807.9' as DECIMAL(24,1)),
cast (cast ('9223372036854775807.9' as DECIMAL(24,1)) as BIGINT)
);
1 |2
-----------------------------------------------
9223372036854775807.9 |9223372036854775807
ij> -- -9223372036854775808 is Long::MIN_VALUE
values (
cast ('-9223372036854775808' as DECIMAL(24,1)),
cast (cast ('-9223372036854775808' as DECIMAL(24,1)) as BIGINT)
);
1 |2
-----------------------------------------------
-9223372036854775808.0 |-9223372036854775808
ij> values (
cast ('-9223372036854775807' as DECIMAL(24,1)),
cast (cast ('-9223372036854775807' as DECIMAL(24,1)) as BIGINT)
);
1 |2
-----------------------------------------------
-9223372036854775807.0 |-9223372036854775807
ij> -- only this should fail
values (
cast ('-9223372036854775809' as DECIMAL(24,1)),
cast (cast ('-9223372036854775809' as DECIMAL(24,1)) as BIGINT)
);
1 |2
-----------------------------------------------
ERROR 22003: The resulting value is outside the range for the data type BIGINT.
ij> values (
cast ('-9223372036854775808.9' as DECIMAL(24,1)),
cast (cast ('-9223372036854775808.9' as DECIMAL(24,1)) as BIGINT)
);
1 |2
-----------------------------------------------
-9223372036854775808.9 |-9223372036854775808
ij> values (
cast ('32767' as DECIMAL(24,1)),
cast (cast ('32767' as DECIMAL(24,1)) as SMALLINT)
);
1 |2
---------------------------------
32767.0 |32767
ij> values (
cast ('32766' as DECIMAL(24,1)),
cast (cast ('32766' as DECIMAL(24,1)) as SMALLINT)
);
1 |2
---------------------------------
32766.0 |32766
ij> values (
cast ('32768' as DECIMAL(24,1)),
cast (cast ('32768' as DECIMAL(24,1)) as SMALLINT)
);
1 |2
---------------------------------
ERROR 22003: The resulting value is outside the range for the data type SMALLINT.
ij> -- only this should fail
values (
cast ('32767.9' as DECIMAL(24,1)),
cast (cast ('32767.9' as DECIMAL(24,1)) as SMALLINT)
);
1 |2
---------------------------------
32767.9 |32767
ij> values (
cast ('-32768' as DECIMAL(24,1)),
cast (cast ('-32768' as DECIMAL(24,1)) as SMALLINT)
);
1 |2
---------------------------------
-32768.0 |-32768
ij> values (
cast ('-32767' as DECIMAL(24,1)),
cast (cast ('-32767' as DECIMAL(24,1)) as SMALLINT)
);
1 |2
---------------------------------
-32767.0 |-32767
ij> -- only this should fail
values (
cast ('-32769' as DECIMAL(24,1)),
cast (cast ('-32769' as DECIMAL(24,1)) as SMALLINT)
);
1 |2
---------------------------------
ERROR 22003: The resulting value is outside the range for the data type SMALLINT.
ij> values (
cast ('-32768.9' as DECIMAL(24,1)),
cast (cast ('-32768.9' as DECIMAL(24,1)) as SMALLINT)
);
1 |2
---------------------------------
-32768.9 |-32768
ij> values (
cast ('2147483647' as DECIMAL(24,1)),
cast (cast ('2147483647' as DECIMAL(24,1)) as INTEGER)
);
1 |2
--------------------------------------
2147483647.0 |2147483647
ij> values (
cast ('2147483646' as DECIMAL(24,1)),
cast (cast ('2147483646' as DECIMAL(24,1)) as INTEGER)
);
1 |2
--------------------------------------
2147483646.0 |2147483646
ij> -- only this should fail
values (
cast ('2147483648' as DECIMAL(24,1)),
cast (cast ('2147483648' as DECIMAL(24,1)) as INTEGER)
);
1 |2
--------------------------------------
ERROR 22003: The resulting value is outside the range for the data type INTEGER.
ij> values (
cast ('2147483647.9' as DECIMAL(24,1)),
cast (cast ('2147483647.9' as DECIMAL(24,1)) as INTEGER)
);
1 |2
--------------------------------------
2147483647.9 |2147483647
ij> values (
cast ('-2147483647' as DECIMAL(24,1)),
cast (cast ('-2147483647' as DECIMAL(24,1)) as INTEGER)
);
1 |2
--------------------------------------
-2147483647.0 |-2147483647
ij> values (
cast ('-2147483646' as DECIMAL(24,1)),
cast (cast ('-2147483646' as DECIMAL(24,1)) as INTEGER)
);
1 |2
--------------------------------------
-2147483646.0 |-2147483646
ij> -- only this should fail
values (
cast ('-2147483649' as DECIMAL(24,1)),
cast (cast ('-2147483649' as DECIMAL(24,1)) as INTEGER)
);
1 |2
--------------------------------------
ERROR 22003: The resulting value is outside the range for the data type INTEGER.
ij> values (
cast ('-2147483648.9' as DECIMAL(24,1)),
cast (cast ('-2147483648.9' as DECIMAL(24,1)) as INTEGER)
);
1 |2
--------------------------------------
-2147483648.9 |-2147483648
ij> --numbers to char
values (cast (1.1 as char(10)));
1
----------
1.1
ij> values (cast (1.1 as varchar(10)));
ERROR 42846: Cannot convert types 'DECIMAL' to 'VARCHAR'.
ij> values (cast (1e1 as varchar(10)));
ERROR 42846: Cannot convert types 'DOUBLE' to 'VARCHAR'.
ij> values (cast (1e1 as char(10)));
ERROR 42846: Cannot convert types 'DOUBLE' to 'CHAR'.
ij> values (cast (1 as char(10)));
1
----------
1
ij> values (cast (1 as varchar(10)));
ERROR 42846: Cannot convert types 'INTEGER' to 'VARCHAR'.
ij> values (cast (1e200 as char(10)));
ERROR 42846: Cannot convert types 'DOUBLE' to 'CHAR'.
ij> values (cast (1e200 as varchar(10)));
ERROR 42846: Cannot convert types 'DOUBLE' to 'VARCHAR'.
ij> values (cast (1 as long varchar));
ERROR 42846: Cannot convert types 'INTEGER' to 'LONG VARCHAR'.
ij> values (cast (1.1 as long varchar));
ERROR 42846: Cannot convert types 'DECIMAL' to 'LONG VARCHAR'.
ij> values (cast (1e1 as long varchar));
ERROR 42846: Cannot convert types 'DOUBLE' to 'LONG VARCHAR'.
ij> --char to numbers
values (cast ('123' as smallint));
1
------
123
ij> values (cast ('123' as int));
1
-----------
123
ij> values (cast ('123' as bigint));
1
--------------------
123
ij> values (cast ('123' as double precision));
ERROR 42846: Cannot convert types 'CHAR' to 'DOUBLE'.
ij> values (cast ('123' as float));
ERROR 42846: Cannot convert types 'CHAR' to 'DOUBLE'.
ij> values (cast ('123' as real));
ERROR 42846: Cannot convert types 'CHAR' to 'REAL'.
ij> values (cast ('123' as numeric(3,0)));
1
----
123
ij> values (cast ('123' as decimal(3,0)));
1
----
123
ij> -- char (with decimal) to numbers (truncates where needed Track #3756)
-- bug 5568
values (cast ('123.45' as smallint));
1
------
123
ij> values (cast ('123.45' as int));
1
-----------
123
ij> values (cast ('123.45' as bigint));
1
--------------------
123
ij> values (cast ('123.45' as double precision));
ERROR 42846: Cannot convert types 'CHAR' to 'DOUBLE'.
ij> values (cast ('123.45' as float));
ERROR 42846: Cannot convert types 'CHAR' to 'DOUBLE'.
ij> values (cast ('123.45' as real));
ERROR 42846: Cannot convert types 'CHAR' to 'REAL'.
ij> values (cast ('123.45' as numeric(5,1)));
1
-------
123.4
ij> values (cast ('123.45' as decimal(5,1)));
1
-------
123.4
ij> values (cast ('123.99' as smallint));
1
------
123
ij> values (cast ('123.99' as int));
1
-----------
123
ij> values (cast ('123.99' as bigint));
1
--------------------
123
ij> values (cast ('123.99' as double precision));
ERROR 42846: Cannot convert types 'CHAR' to 'DOUBLE'.
ij> values (cast ('123.99' as float));
ERROR 42846: Cannot convert types 'CHAR' to 'DOUBLE'.
ij> values (cast ('123.99' as real));
ERROR 42846: Cannot convert types 'CHAR' to 'REAL'.
ij> values (cast ('123.99' as numeric(5,1)));
1
-------
123.9
ij> values (cast ('123.99' as decimal(5,1)));
1
-------
123.9
ij> --bad
values (cast (1 as char(2) for bit data));
ERROR 42846: Cannot convert types 'INTEGER' to 'CHAR () FOR BIT DATA'.
ij> values (cast (1 as date));
ERROR 42846: Cannot convert types 'INTEGER' to 'DATE'.
ij> values (cast (1 as time));
ERROR 42846: Cannot convert types 'INTEGER' to 'TIME'.
ij> values (cast (1 as timestamp));
ERROR 42846: Cannot convert types 'INTEGER' to 'TIMESTAMP'.
ij> -------------------
--char -> date/time
-------------------
values (cast ('TIME''11:11:11''' as time));
ERROR 22007: The syntax of the string representation of a date/time value is incorrect.
ij> values (cast ('11:11:11' as time));
1
--------
11:11:11
ij> values (cast ('DATE''1999-09-09''' as date));
ERROR 22007: The syntax of the string representation of a date/time value is incorrect.
ij> values (cast ('1999-09-09' as date));
1
----------
1999-09-09
ij> values (cast ('TIMESTAMP''1999-09-09 11:11:11''' as timestamp));
ERROR 22007: The syntax of the string representation of a date/time value is incorrect.
ij> values (cast ('1999-09-09 11:11:11' as timestamp));
1
-----------------------------
1999-09-09 11:11:11.0
ij> ------------------
--date/time ->other
------------------
values (cast (TIME('11:11:11') as char(20)));
1
--------------------
11:11:11
ij> values (cast (DATE('1999-09-09') as char(20)));
1
--------------------
1999-09-09
ij> values (cast (TIMESTAMP('1999-09-09 11:11:11' )as char(40)));
1
----------------------------------------
1999-09-09 11:11:11.0
ij> values (cast (TIME('11:11:11') as varchar(20)));
1
--------------------
11:11:11
ij> values (cast (DATE('1999-09-09') as varchar(20)));
1
--------------------
1999-09-09
ij> values (cast (TIMESTAMP('1999-09-09 11:11:11' )as varchar(40)));
1
----------------------------------------
1999-09-09 11:11:11.0
ij> values (cast (TIME('11:11:11') as long varchar));
ERROR 42846: Cannot convert types 'TIME' to 'LONG VARCHAR'.
ij> values (cast (DATE('1999-09-09') as long varchar));
ERROR 42846: Cannot convert types 'DATE' to 'LONG VARCHAR'.
ij> values (cast (TIMESTAMP('1999-09-09 11:11:11' )as long varchar));
ERROR 42846: Cannot convert types 'TIMESTAMP' to 'LONG VARCHAR'.
ij> -- truncation errors
values (cast (TIME('11:11:11') as char(2)));
ERROR 22001: A truncation error was encountered trying to shrink CHAR '11:11:11' to length 2.
ij> values (cast (DATE('1999-09-09') as char(2)));
ERROR 22001: A truncation error was encountered trying to shrink CHAR '1999-09-09' to length 2.
ij> values (cast (TIMESTAMP('1999-09-09 11:11:11' )as char(2)));
ERROR 22001: A truncation error was encountered trying to shrink CHAR '1999-09-09 11:11:11.0' to length 2.
ij> -- to date/time
values (cast (TIME('11:11:11') as time));
1
--------
11:11:11
ij> values (cast (TIME('11:11:11') as date));
ERROR 42846: Cannot convert types 'TIME' to 'DATE'.
ij> -- this piece of convoluted logic is to ensure that we
-- get the current date for a conversion of time to timestamp
values substr(cast (cast (TIME('11:11:11') as timestamp) as char(50)), 1, 10) = cast (current_date as char(10));
1
-----
true
ij> -- now make sure we got the time right
values substr(cast (cast (TIME('11:11:11') as timestamp) as char(30)), 12);
1
------------------------------
11:11:11.0
ij> values (cast (DATE('1999-09-09') as date));
1
----------
1999-09-09
ij> values (cast (DATE('1999-09-09') as time));
ERROR 42846: Cannot convert types 'DATE' to 'TIME'.
ij> values (cast (DATE('1999-09-09') as timestamp));
1
-----------------------------
1999-09-09 00:00:00.0
ij> values (cast (TIMESTAMP('1999-09-09 11:11:11' )as date));
1
----------
1999-09-09
ij> values (cast (TIMESTAMP('1999-09-09 11:11:11' )as time));
1
--------
11:11:11
ij> values (cast (TIMESTAMP('1999-09-09 11:11:11' )as timestamp));
1
-----------------------------
1999-09-09 11:11:11.0
ij> --bad
values (cast (TIMESTAMP('1999-09-09 11:11:11' )as int));
ERROR 42846: Cannot convert types 'TIMESTAMP' to 'INTEGER'.
ij> values (cast (DATE('1999-09-09') as int));
ERROR 42846: Cannot convert types 'DATE' to 'INTEGER'.
ij> values (cast (TIME('11:11:11') as int));
ERROR 42846: Cannot convert types 'TIME' to 'INTEGER'.
ij> values (cast (TIMESTAMP('1999-09-09 11:11:11' )as smallint));
ERROR 42846: Cannot convert types 'TIMESTAMP' to 'SMALLINT'.
ij> values (cast (DATE('1999-09-09') as smallint));
ERROR 42846: Cannot convert types 'DATE' to 'SMALLINT'.
ij> values (cast (TIME('11:11:11') as smallint));
ERROR 42846: Cannot convert types 'TIME' to 'SMALLINT'.
ij> values (cast (TIMESTAMP('1999-09-09 11:11:11' )as bigint));
ERROR 42846: Cannot convert types 'TIMESTAMP' to 'BIGINT'.
ij> values (cast (DATE('1999-09-09') as bigint));
ERROR 42846: Cannot convert types 'DATE' to 'BIGINT'.
ij> values (cast (TIME('11:11:11') as bigint));
ERROR 42846: Cannot convert types 'TIME' to 'BIGINT'.
ij> values (cast (TIMESTAMP('1999-09-09 11:11:11' )as numeric));
ERROR 42846: Cannot convert types 'TIMESTAMP' to 'NUMERIC'.
ij> values (cast (DATE('1999-09-09') as numeric));
ERROR 42846: Cannot convert types 'DATE' to 'NUMERIC'.
ij> values (cast (TIME('11:11:11') as numeric));
ERROR 42846: Cannot convert types 'TIME' to 'NUMERIC'.
ij> values (cast (TIMESTAMP('1999-09-09 11:11:11' )as decimal));
ERROR 42846: Cannot convert types 'TIMESTAMP' to 'DECIMAL'.
ij> values (cast (DATE('1999-09-09') as decimal));
ERROR 42846: Cannot convert types 'DATE' to 'DECIMAL'.
ij> values (cast (TIME('11:11:11') as decimal));
ERROR 42846: Cannot convert types 'TIME' to 'DECIMAL'.
ij> values (cast (TIMESTAMP('1999-09-09 11:11:11' ) as char(13) for bit data));
ERROR 42846: Cannot convert types 'TIMESTAMP' to 'CHAR () FOR BIT DATA'.
ij> values (cast (DATE('1999-09-09') as char(13) for bit data));
ERROR 42846: Cannot convert types 'DATE' to 'CHAR () FOR BIT DATA'.
ij> values (cast (TIME('11:11:11') as char(13) for bit data));
ERROR 42846: Cannot convert types 'TIME' to 'CHAR () FOR BIT DATA'.
ij> ------------
--bit ->char
------------
values (cast (X'00680065006c006c006f' as char(10)));
ERROR 42846: Cannot convert types 'CHAR () FOR BIT DATA' to 'CHAR'.
ij> --small bit
values (cast (X'11' as char(10)));
ERROR 42846: Cannot convert types 'CHAR () FOR BIT DATA' to 'CHAR'.
ij> values (cast (X'11' as varchar(10)));
ERROR 42846: Cannot convert types 'CHAR () FOR BIT DATA' to 'VARCHAR'.
ij> values (cast (X'11' as long varchar));
ERROR 42846: Cannot convert types 'CHAR () FOR BIT DATA' to 'LONG VARCHAR'.
ij> --values (cast (X'00' as char(10)));
--odd length won't work anymore
values (cast (X'123' as char(20)));
ERROR 42606: An invalid hexadecimal constant starting with 'X'123'' has been detected.
ij> --truncate, (should be warning in future)
values (cast ('1234' as char(1) for bit data));
ERROR 42846: Cannot convert types 'CHAR' to 'CHAR () FOR BIT DATA'.
ij> --truncate, ok
values (cast ('1200' as char(1) for bit data));
ERROR 42846: Cannot convert types 'CHAR' to 'CHAR () FOR BIT DATA'.
ij> ------------------------------------------------
-- Casting
-----------------------------------------------
create table tab1 (
i integer,
s integer,
b integer,
l bigint,
c char(10),
v varchar(10),
d double precision,
r real,
dt date,
t time,
ts timestamp,
dc decimal);
0 rows inserted/updated/deleted
ij> insert into tab1 values(1,
cast(1 as smallint),
cast(1 as int),
cast(1 as bigint),
'char',
'varchar',
cast(1.1 as double precision),
cast(1.1 as real),
DATE('1990-10-10'),
TIME('11:11:11'),
TIMESTAMP('1990-11-11 11:11:11'),
1.1);
1 row inserted/updated/deleted
ij> insert into tab1 values (null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null);
1 row inserted/updated/deleted
ij> -- tab1 type -> its tab1 type
select cast(i as integer) from tab1;
1
-----------
1
NULL
ij> select cast(s as smallint) from tab1;
1
------
1
NULL
ij> select cast(l as bigint) from tab1;
1
--------------------
1
NULL
ij> select cast(c as char(10)) from tab1;
1
----------
char
NULL
ij> select cast(v as char varying(10)) from tab1;
1
----------
varchar
NULL
ij> select cast(d as double precision) from tab1;
1
------------------------
1.1
NULL
ij> select cast(r as float) from tab1;
1
------------------------
1.100000023841858
NULL
ij> select cast(dt as date) from tab1;
1
----------
1990-10-10
NULL
ij> select cast(t as time) from tab1;
1
--------
11:11:11
NULL
ij> select cast(ts as timestamp) from tab1;
1
-----------------------------
1990-11-11 11:11:11.0
NULL
ij> select cast(dc as dec) from tab1;
1
------
1
NULL
ij> -- try a few others where we try all conversions
select cast(i as integer) from tab1;
1
-----------
1
NULL
ij> select cast(i as smallint) from tab1;
1
------
1
NULL
ij> select cast(i as bigint) from tab1;
1
--------------------
1
NULL
ij> select cast(i as char(10)) from tab1;
1
----------
1
NULL
ij> select cast(i as char varying(10)) from tab1;
ERROR 42846: Cannot convert types 'INTEGER' to 'VARCHAR'.
ij> select cast(i as double precision) from tab1;
1
------------------------
1.0
NULL
ij> select cast(i as float) from tab1;
1
------------------------
1.0
NULL
ij> select cast(i as date) from tab1;
ERROR 42846: Cannot convert types 'INTEGER' to 'DATE'.
ij> select cast(i as time) from tab1;
ERROR 42846: Cannot convert types 'INTEGER' to 'TIME'.
ij> select cast(i as timestamp) from tab1;
ERROR 42846: Cannot convert types 'INTEGER' to 'TIMESTAMP'.
ij> select cast(i as dec) from tab1;
1
------
1
NULL
ij> -- try a few others
select cast(c as integer) from tab1;
1
-----------
ERROR 22018: Invalid character string format for type INTEGER.
ij> select cast(c as smallint) from tab1;
1
------
ERROR 22018: Invalid character string format for type SMALLINT.
ij> select cast(c as bigint) from tab1;
1
--------------------
ERROR 22018: Invalid character string format for type BIGINT.
ij> select cast(c as char(10)) from tab1;
1
----------
char
NULL
ij> select cast(c as char varying(10)) from tab1;
1
----------
char
NULL
ij> select cast(c as double precision) from tab1;
ERROR 42846: Cannot convert types 'CHAR' to 'DOUBLE'.
ij> select cast(c as float) from tab1;
ERROR 42846: Cannot convert types 'CHAR' to 'DOUBLE'.
ij> select cast(c as date) from tab1;
1
----------
ERROR 22007: The syntax of the string representation of a date/time value is incorrect.
ij> select cast(c as time) from tab1;
1
--------
ERROR 22007: The syntax of the string representation of a date/time value is incorrect.
ij> select cast(c as timestamp) from tab1;
1
-----------------------------
ERROR 22007: The syntax of the string representation of a date/time value is incorrect.
ij> select cast(c as dec) from tab1;
1
------
ERROR 22018: Invalid character string format for type DECIMAL.
ij> select cast(t as integer) from tab1;
ERROR 42846: Cannot convert types 'TIME' to 'INTEGER'.
ij> select cast(t as smallint) from tab1;
ERROR 42846: Cannot convert types 'TIME' to 'SMALLINT'.
ij> select cast(t as bigint) from tab1;
ERROR 42846: Cannot convert types 'TIME' to 'BIGINT'.
ij> select cast(t as char(10)) from tab1;
1
----------
11:11:11
NULL
ij> select cast(t as char varying(10)) from tab1;
1
----------
11:11:11
NULL
ij> select cast(t as double precision) from tab1;
ERROR 42846: Cannot convert types 'TIME' to 'DOUBLE'.
ij> select cast(t as float) from tab1;
ERROR 42846: Cannot convert types 'TIME' to 'DOUBLE'.
ij> select cast(t as date) from tab1;
ERROR 42846: Cannot convert types 'TIME' to 'DATE'.
ij> select cast(t as time) from tab1;
1
--------
11:11:11
NULL
ij> select cast(t as dec) from tab1;
ERROR 42846: Cannot convert types 'TIME' to 'DECIMAL'.
ij> drop table tab1;
0 rows inserted/updated/deleted
ij> ---------------------------------------------------------------
-- Other Tests
---------------------------------------------------------------
autocommit off;
ij> -- create tables
create table t1 (bt char(1) for bit data, btv varchar(1) for bit data,
c char(30), d double precision, i int, r real,
s smallint, dc decimal(18), num numeric(18),
dt date, t time, ts timestamp, v varchar(30),
lvc long varchar);
0 rows inserted/updated/deleted
ij> create table strings(c30 char(30));
0 rows inserted/updated/deleted
ij> -- we need a 1 row table with date/time columns because of problems
-- with single quotes in using 'values DATE('')'
create table temporal_values (dt date, t time, ts timestamp);
0 rows inserted/updated/deleted
ij> insert into temporal_values values(DATE('9876-5-4'), TIME('1:02:34'),
TIMESTAMP('9876-5-4 1:02:34'));
1 row inserted/updated/deleted
ij> -- negative
-- pass wrong type for parameter
prepare a1 as 'values cast(? as smallint)';
ij> execute a1 using 'values 1';
1
------
1
ij> -- uninitialized parameter
values cast(? as int);
ERROR 07000: At least one parameter to the current statement is uninitialized.
ij> -- positive
-- test casting null to all builtin types
insert into t1 (bt) values cast(null as char(1) for bit data);
1 row inserted/updated/deleted
ij> insert into t1 (btv) values cast(null as varchar(1) for bit data);
1 row inserted/updated/deleted
ij> insert into t1 (c) values cast(null as char(30));
1 row inserted/updated/deleted
ij> insert into t1 (d) values cast(null as double precision);
1 row inserted/updated/deleted
ij> insert into t1 (i) values cast(null as int);
1 row inserted/updated/deleted
ij> insert into t1 (r) values cast(null as real);
1 row inserted/updated/deleted
ij> insert into t1 (s) values cast(null as smallint);
1 row inserted/updated/deleted
ij> insert into t1 (dc) values cast(null as decimal);
1 row inserted/updated/deleted
ij> insert into t1 (num) values cast(null as numeric);
1 row inserted/updated/deleted
ij> insert into t1 (dt) values cast(null as date);
1 row inserted/updated/deleted
ij> insert into t1 (t) values cast(null as time);
1 row inserted/updated/deleted
ij> insert into t1 (ts) values cast(null as timestamp);
1 row inserted/updated/deleted
ij> insert into t1 (v) values cast(null as varchar(30));
1 row inserted/updated/deleted
ij> insert into t1 (lvc) values cast(null as long varchar);
1 row inserted/updated/deleted
ij> -- expect 10 rows of nulls
select * from t1;
BT |BTV |C |D |I |R |S |DC |NUM |DT |T |TS |V |LVC
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
NULL|NULL|NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL
NULL|NULL|NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL
NULL|NULL|NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL
NULL|NULL|NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL
NULL|NULL|NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL
NULL|NULL|NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL
NULL|NULL|NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL
NULL|NULL|NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL
NULL|NULL|NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL
NULL|NULL|NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL
NULL|NULL|NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL
NULL|NULL|NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL
NULL|NULL|NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL
NULL|NULL|NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL
ij> -- make sure casting works correctly on nulls
select cast (bt as char(1) for bit data) from t1;
1
----
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
ij> select cast (btv as varchar(1) for bit data) from t1;
1
----
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
ij> select cast (c as char(30)) from t1;
1
------------------------------
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
ij> select cast (d as double precision) from t1;
1
------------------------
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
ij> select cast (r as real) from t1;
1
---------------
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
ij> select cast (s as smallint) from t1;
1
------
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
ij> select cast (num as numeric) from t1;
1
------
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
ij> select cast (dc as decimal) from t1;
1
------
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
ij> select cast (dt as date) from t1;
1
----------
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
ij> select cast (t as time) from t1;
1
--------
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
ij> select cast (ts as timestamp) from t1;
1
-----------------------------
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
ij> select cast (v as varchar(30)) from t1;
1
------------------------------
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
ij> select cast (lvc as long varchar) from t1;
1
--------------------------------------------------------------------------------------------------------------------------------
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
ij> -- clean up t1
delete from t1;
14 rows inserted/updated/deleted
ij> -- test casting ? to all builtin types
prepare q1 as 'insert into t1 (bt) values cast(? as char(1) for bit data)';
ij> prepare q2 as 'insert into t1 (btv) values cast(? as varchar(1) for bit data)';
ij> prepare q4 as 'insert into t1 (c) values cast(? as char(30))';
ij> prepare q5 as 'insert into t1 (d) values cast(? as double precision)';
ij> prepare q6 as 'insert into t1 (i) values cast(? as int)';
ij> prepare q7 as 'insert into t1 (r) values cast(? as real)';
ij> prepare q8 as 'insert into t1 (s) values cast(? as smallint)';
ij> prepare q10 as 'insert into t1 (num) values cast(? as numeric(18))';
ij> prepare q11 as 'insert into t1 (dc) values cast(? as decimal(18))';
ij> prepare q12 as 'insert into t1 (dt) values cast(? as date)';
ij> prepare q13 as 'insert into t1 (t) values cast(? as time)';
ij> prepare q14 as 'insert into t1 (ts) values cast(? as timestamp)';
ij> prepare q15 as 'insert into t1 (v) values cast(? as varchar(30))';
ij> prepare q16 as 'insert into t1 (lvc) values cast(? as long varchar)';
ij> execute q1 using 'values X''aa''';
1 row inserted/updated/deleted
ij> execute q2 using 'values X''aa''';
1 row inserted/updated/deleted
ij> execute q4 using 'values char(123456)';
1 row inserted/updated/deleted
ij> execute q5 using 'values 123456.78e0';
1 row inserted/updated/deleted
ij> execute q6 using 'values 4321';
1 row inserted/updated/deleted
ij> -- bug 5421 - support db2 udb compatible built-in functions
execute q7 using 'values REAL(4321.01234)';
ERROR 42X80: VALUES clause must contain at least one element. Empty elements are not allowed.
ij> execute q8 using 'values SMALLINT(12321)';
1 row inserted/updated/deleted
ij> execute q10 using 'values 123456.78';
1 row inserted/updated/deleted
ij> execute q11 using 'values 123456.78';
1 row inserted/updated/deleted
ij> execute q12 using 'select dt from temporal_values';
1 row inserted/updated/deleted
ij> execute q13 using 'select t from temporal_values';
1 row inserted/updated/deleted
ij> execute q14 using 'select ts from temporal_values';
1 row inserted/updated/deleted
ij> execute q15 using 'values char(654321)';
1 row inserted/updated/deleted
ij> execute q16 using 'values char(987654)';
1 row inserted/updated/deleted
ij> select * from t1;
BT |BTV |C |D |I |R |S |DC |NUM |DT |T |TS |V |LVC
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
aa |NULL|NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL
NULL|aa |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL
NULL|NULL|123456 |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL
NULL|NULL|NULL |123456.78 |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL
NULL|NULL|NULL |NULL |4321 |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL
NULL|NULL|NULL |NULL |NULL |NULL |12321 |NULL |NULL |NULL |NULL |NULL |NULL |NULL
NULL|NULL|NULL |NULL |NULL |NULL |NULL |NULL |123456 |NULL |NULL |NULL |NULL |NULL
NULL|NULL|NULL |NULL |NULL |NULL |NULL |123456 |NULL |NULL |NULL |NULL |NULL |NULL
NULL|NULL|NULL |NULL |NULL |NULL |NULL |NULL |NULL |9876-05-04|NULL |NULL |NULL |NULL
NULL|NULL|NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |01:02:34|NULL |NULL |NULL
NULL|NULL|NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |9876-05-04 01:02:34.0 |NULL |NULL
NULL|NULL|NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |654321 |NULL
NULL|NULL|NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |NULL |987654
ij> -- clean up t1
delete from t1;
13 rows inserted/updated/deleted
ij> -- more ? tests
-- Truncation exception expected in non-parameter cases
-- RESOLVE, no truncation expected in parameter cases
-- where parameter value is not a string. This is
-- currently an "extension".
create table x(c1 char(1));
0 rows inserted/updated/deleted
ij> prepare param1 as 'insert into x values cast(? as char(1))';
ij> insert into x values cast('12' as char(1));
1 row inserted/updated/deleted
WARNING 01004: Data truncation
ij> execute param1 using 'values ''34''';
1 row inserted/updated/deleted
WARNING 01004: Data truncation
ij> select * from x;
C1
----
1
3
ij> delete from x;
2 rows inserted/updated/deleted
ij> insert into x values cast(12 as char(1));
ERROR 22001: A truncation error was encountered trying to shrink CHAR '12' to length 1.
ij> execute param1 using 'values 34';
1 row inserted/updated/deleted
WARNING 01004: Data truncation
ij> select * from x;
C1
----
3
ij> delete from x;
1 row inserted/updated/deleted
ij> insert into x values cast(time('12:12:12') as char(1));
ERROR 22001: A truncation error was encountered trying to shrink CHAR '12:12:12' to length 1.
ij> execute param1 using 'values time(''21:12:12'')';
1 row inserted/updated/deleted
WARNING 01004: Data truncation
ij> select * from x;
C1
----
2
ij> delete from x;
1 row inserted/updated/deleted
ij> drop table x;
0 rows inserted/updated/deleted
ij> -- method resolution tests
-- clean up the prepared statements
remove a1;
ij> remove q1;
ij> remove q2;
ij> remove q4;
ij> remove q5;
ij> remove q6;
ij> remove q7;
ij> remove q8;
ij> remove q10;
ij> remove q11;
ij> remove q12;
ij> remove q13;
ij> remove q14;
ij> remove q15;
ij> -- reset autocomiit
commit;
ij> autocommit on;
ij> -- bind time casting tests
-- negative
values cast('asdf' as smallint);
ERROR 22018: Invalid character string format for type SMALLINT.
ij> values cast('asdf' as int);
ERROR 22018: Invalid character string format for type INTEGER.
ij> values cast('asdf' as bigint);
ERROR 22018: Invalid character string format for type BIGINT.
ij> values cast('asdf' as real);
ERROR 42846: Cannot convert types 'CHAR' to 'REAL'.
ij> values cast('asdf' as double precision);
ERROR 42846: Cannot convert types 'CHAR' to 'DOUBLE'.
ij> values cast('asdf' as decimal(5,4));
1
-------
ERROR 22018: Invalid character string format for type DECIMAL.
ij> values cast('asdf' as date);
ERROR 22007: The syntax of the string representation of a date/time value is incorrect.
ij> values cast('asdf' as time);
ERROR 22007: The syntax of the string representation of a date/time value is incorrect.
ij> values cast('asdf' as timestamp);
ERROR 22007: The syntax of the string representation of a date/time value is incorrect.
ij> values cast('2999999999' as int);
ERROR 22003: The resulting value is outside the range for the data type INTEGER.
ij> values cast(2999999999 as int);
ERROR 22003: The resulting value is outside the range for the data type INTEGER.
ij> values cast('99999' as smallint);
ERROR 22003: The resulting value is outside the range for the data type SHORT.
ij> values cast(99999 as smallint);
ERROR 22003: The resulting value is outside the range for the data type SMALLINT.
ij> values cast(cast(99 as int) as char);
ERROR 22001: A truncation error was encountered trying to shrink CHAR '99' to length 1.
ij> values cast(cast(-9 as int) as char);
ERROR 22001: A truncation error was encountered trying to shrink CHAR '-9' to length 1.
ij> values cast(cast(99 as smallint) as char);
ERROR 22001: A truncation error was encountered trying to shrink CHAR '99' to length 1.
ij> values cast(cast(99 as bigint) as char);
ERROR 22001: A truncation error was encountered trying to shrink CHAR '99' to length 1.
ij> values cast(cast(9.9 as real) as char);
ERROR 42846: Cannot convert types 'REAL' to 'CHAR'.
ij> values cast(cast(9.9 as double precision) as char);
ERROR 42846: Cannot convert types 'DOUBLE' to 'CHAR'.
ij> -- positive
values cast(1 as int);
1
-----------
1
ij> values cast(1 as smallint);
1
------
1
ij> values cast(1 as bigint);
1
--------------------
1
ij> values cast(1 as char);
1
-
1
ij> values cast('true' as char(4));
1
----
true
ij> -- drop the tables
drop table t1;
0 rows inserted/updated/deleted
ij> drop table temporal_values;
0 rows inserted/updated/deleted
ij> drop table strings;
0 rows inserted/updated/deleted
ij> -- ISO time/timestamp formats
values (cast ('08.08.08' as TIME));
1
--------
08:08:08
ij> values (cast ('2001-01-01-08.08.08.123456' as TIMESTAMP));
1
-----------------------------
2001-01-01 08:08:08.123456
ij> -- char, varchar
values (char('abcde', 5));
1
-----
abcde
ij> values (char('abcde', 6));
1
------
abcde
ij> values (char('abcde', 4));
1
----
abcd
WARNING 01004: Data truncation
ij> values (varchar('', 20));
1
--------------------
ij> create table t1 (c5 date, c6 time, c7 timestamp, c8 char(5), c9 varchar(5));
0 rows inserted/updated/deleted
ij> insert into t1 values ('2003-09-10', '16:44:02', '2003-09-08 12:20:30.123456', 'abc', 'abcde');
1 row inserted/updated/deleted
ij> insert into t1 values ('2005-09-10', '18.44.02', '2004-09-08-12.20.30.123456', 'cba', 'c');
1 row inserted/updated/deleted
ij> select char(c5), char(c6), char(c7), char(c8), char(c9) from t1;
1 |2 |3 |4 |5
-------------------------------------------------------------
2003-09-10|16:44:02|2003-09-08 12:20:30.123456 |abc |abcde
2005-09-10|18:44:02|2004-09-08 12:20:30.123456 |cba |c
ij> select varchar(c5), varchar(c6), varchar(c7), varchar(c8), varchar(c9) from t1;
1 |2 |3 |4 |5
-------------------------------------------------------------
2003-09-10|16:44:02|2003-09-08 12:20:30.123456 |abc |abcde
2005-09-10|18:44:02|2004-09-08 12:20:30.123456 |cba |c
ij> select char(c8, 10), varchar(c9, 9) from t1;
1 |2
--------------------
abc |abcde
cba |c
ij> select { fn concat(c8, char(c8)) } from t1;
1
----------
abc abc
cba cba
ij> select { fn concat(c8, varchar(c9)) } from t1;
1
----------
abc abcde
cba c
ij> select { fn concat(varchar(c9, 20), char(c8, 8)) } from t1;
1
----------------------------
abcdeabc
ccba
ij> select { fn concat(char(c9, 20), varchar(c8, 8)) } from t1;
1
----------------------------
abcde abc
c cba
ij> -- clean up
drop table t1;
0 rows inserted/updated/deleted
ij> -- bug 5421 - support db2 udb compatible built-in functions
values CHAR(INT(67890));
1
-----------
67890
ij> values CHAR(INTEGER(12345));
1
-----------
12345
ij> values CHAR(DEC(67.21,4,2));
ERROR 42X01: Syntax error: Encountered "DEC" at line 1, column 13.
Issue the 'help' command for general information on IJ command syntax.
Any unrecognized commands are treated as potential SQL commands and executed directly.
Consult your DBMS server reference documentation for details of the SQL syntax supported by your server.
ij> values CHAR(DECIMAL(67.10,4,2));
ERROR 42X01: Syntax error: Encountered "DECIMAL" at line 1, column 13.
Issue the 'help' command for general information on IJ command syntax.
Any unrecognized commands are treated as potential SQL commands and executed directly.
Consult your DBMS server reference documentation for details of the SQL syntax supported by your server.
ij> values CHAR(DOUBLE(5.55));
1
-----------------------------------------------------
5.55
ij> values CHAR(DOUBLE_PRECISION(5.555));
ERROR 42Y03: 'DOUBLE_PRECISION' is not recognized as a function or procedure.
ij> values CHAR(BIGINT(1));
1
--------------------
1
ij> values CHAR(BIGINT(-1));
1
--------------------
-1
ij> values LENGTH(CAST('hello' AS CHAR(25)));
1
-----------
25
ij> values LENGTH(CAST('hello' AS VARCHAR(25)));
1
-----------
5
ij> values LENGTH(CAST('hello' AS LONG VARCHAR));
1
-----------
5
ij> values CAST (X'03' as CHAR(5) for bit data);
1
----------
0320202020
ij> values CAST (X'04' as VARCHAR(5) for bit data);
1
----------
04
ij> values CAST (X'05' as LONG VARCHAR for bit data);
1
--------------------------------------------------------------------------------------------------------------------------------
05
ij> -- clean up
drop table t1;
ERROR 42Y55: 'DROP TABLE' cannot be performed on 'T1' because it does not exist.
ij> -- test some casting from a java type to standard SQL types.
-- should all fail at runtime
select cast (aliasinfo as BOOLEAN) from sys.sysaliases;
1
-----
ERROR XCL12: An attempt was made to put a data value of type 'org.apache.derby.catalog.types.RoutineAliasInfo' into a data value of type 'BOOLEAN'.
ij> select cast (aliasinfo as SMALLINT) from sys.sysaliases;
1
------
ERROR XCL12: An attempt was made to put a data value of type 'org.apache.derby.catalog.types.RoutineAliasInfo' into a data value of type 'SMALLINT'.
ij> select cast (aliasinfo as INTEGER) from sys.sysaliases;
1
-----------
ERROR XCL12: An attempt was made to put a data value of type 'org.apache.derby.catalog.types.RoutineAliasInfo' into a data value of type 'INTEGER'.
ij> select cast (aliasinfo as BIGINT) from sys.sysaliases;
1
--------------------
ERROR XCL12: An attempt was made to put a data value of type 'org.apache.derby.catalog.types.RoutineAliasInfo' into a data value of type 'BIGINT'.
ij> select cast (aliasinfo as REAL) from sys.sysaliases;
1
---------------
ERROR XCL12: An attempt was made to put a data value of type 'org.apache.derby.catalog.types.RoutineAliasInfo' into a data value of type 'REAL'.
ij> select cast (aliasinfo as DOUBLE) from sys.sysaliases;
1
------------------------
ERROR XCL12: An attempt was made to put a data value of type 'org.apache.derby.catalog.types.RoutineAliasInfo' into a data value of type 'DOUBLE'.
ij> select cast (aliasinfo as DECIMAL(5,4)) from sys.sysaliases;
1
-------
ERROR XCL12: An attempt was made to put a data value of type 'org.apache.derby.catalog.types.RoutineAliasInfo' into a data value of type 'DECIMAL'.
ij> select cast (aliasinfo as CHAR(30) FOR BIT DATA) from sys.sysaliases;
1
------------------------------------------------------------
ERROR XCL12: An attempt was made to put a data value of type 'org.apache.derby.catalog.types.RoutineAliasInfo' into a data value of type 'CHAR () FOR BIT DATA'.
ij> select cast (aliasinfo as VARCHAR(30) FOR BIT DATA) from sys.sysaliases;
1
------------------------------------------------------------
ERROR XCL12: An attempt was made to put a data value of type 'org.apache.derby.catalog.types.RoutineAliasInfo' into a data value of type 'VARCHAR () FOR BIT DATA'.
ij> select cast (aliasinfo as LONG VARCHAR FOR BIT DATA) from sys.sysaliases;
1
--------------------------------------------------------------------------------------------------------------------------------
ERROR XCL12: An attempt was made to put a data value of type 'org.apache.derby.catalog.types.RoutineAliasInfo' into a data value of type 'LONG VARCHAR FOR BIT DATA'.
ij> select cast (aliasinfo as BLOB) from sys.sysaliases;
1
--------------------------------------------------------------------------------------------------------------------------------
ERROR XCL12: An attempt was made to put a data value of type 'org.apache.derby.catalog.types.RoutineAliasInfo' into a data value of type 'BLOB'.
ij> select cast (aliasinfo as CLOB) from sys.sysaliases where CAST(alias AS VARCHAR(128)) = 'INSTALL_JAR';
1
--------------------------------------------------------------------------------------------------------------------------------
ERROR XCL12: An attempt was made to put a data value of type 'org.apache.derby.catalog.types.RoutineAliasInfo' into a data value of type 'CLOB'.
ij> -- Java casts to character types excluding CLOB are supported using Object.toString
select cast (aliasinfo as CHAR(240)) from sys.sysaliases where CAST(alias AS VARCHAR(128)) = 'INSTALL_JAR';
1
--------------------------------------------------------------------------------------------------------------------------------
INSTALL_JAR(IN "URL" VARCHAR(256),IN "JAR" VARCHAR(128),IN "DEPLOY" INTEGER) LANGUAGE JAVA PARAMETER STYLE JAVA MODIFIES SQL DA&
ij> select cast (aliasinfo as VARCHAR(240)) from sys.sysaliases where CAST(alias AS VARCHAR(128)) = 'INSTALL_JAR';
1
--------------------------------------------------------------------------------------------------------------------------------
INSTALL_JAR(IN "URL" VARCHAR(256),IN "JAR" VARCHAR(128),IN "DEPLOY" INTEGER) LANGUAGE JAVA PARAMETER STYLE JAVA MODIFIES SQL DA&
ij> select cast (aliasinfo as LONG VARCHAR) from sys.sysaliases where CAST(alias AS VARCHAR(128)) = 'INSTALL_JAR';
1
--------------------------------------------------------------------------------------------------------------------------------
INSTALL_JAR(IN "URL" VARCHAR(256),IN "JAR" VARCHAR(128),IN "DEPLOY" INTEGER) LANGUAGE JAVA PARAMETER STYLE JAVA MODIFIES SQL DA&
ij>
|