1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397
|
########### suite/json/t/json_notable.test #
# Tests json columns functionality that does not need any tables #
# to be defined (no storage engine functionality) #
# #
# This test copies some tests originally in json.test #
######################################################################
# Some extra checks for comparisons between positive and negative zero.
# All should be equal.
SELECT JSON_COMPACT(0.0e0) = -0.0e0;
SELECT JSON_COMPACT(CAST(0 AS DECIMAL)) = CAST(-0.0e0 AS DECIMAL);
SELECT JSON_COMPACT(0.0e0) = CAST(-0.0e0 AS DECIMAL);
SELECT JSON_COMPACT(CAST(0 AS DECIMAL)) = -0.0e0;
SELECT JSON_COMPACT(CAST(0 AS SIGNED)) = -0.0e0;
SELECT JSON_COMPACT(CAST(0 AS SIGNED)) = CAST(-0.0e0 AS DECIMAL);
SELECT JSON_COMPACT(CAST(0 AS UNSIGNED)) = -0.0e0;
SELECT JSON_COMPACT(CAST(0 AS UNSIGNED)) = CAST(-0.0e0 AS DECIMAL);
# Test that CAST string argument isn't treated as ANY_JSON_ATOM
# in that a MySQL string needs to be parsed to JSON here; it is not
# auto-converted to a JSON string as in ANY_JSON_ATOM contexts.
select json_compact('"abc"');
--echo error ER_INVALID_JSON_TEXT_IN_PARAM
select json_compact('abc');
--echo
--echo # String literal - valid JSON
select JSON_VALID('123'); # uint
select JSON_VALID('-123'); # int
select JSON_VALID('5000000000'); # uint64
select JSON_VALID('-5000000000'); # int64
select JSON_VALID('1.23'); # double
select JSON_VALID('"123"');
select JSON_VALID('true');
select JSON_VALID('false');
select JSON_VALID('null');
select JSON_VALID('{"address": "Trondheim"}');
--echo
--echo # String literal - not valid JSON
select JSON_VALID('12 3');
--echo
--echo # String literal not in UTF-8
set names 'ascii';
# auto-convert to utf-8
select JSON_VALID('123');
set names 'utf8';
--echo
--echo # Json expression
select JSON_VALID(json_compact('[123]'));
--echo
--echo # Json expression NULL
select JSON_VALID(json_compact(NULL));
--echo
--echo # Bare NULL
select JSON_VALID( NULL );
--echo
--echo # Function result - string
select JSON_VALID( UPPER('"abc"') );
set names 'latin1';
--echo
--echo # Function result - string
# auto-convert to utf-8
select JSON_VALID( UPPER('"abc"') );
set names 'utf8';
--echo
--echo # Function result - date, not valid as JSON without CAST
select JSON_VALID( CAST('2015-01-15' AS DATE) );
--echo
--echo # The date string doesn't parse as JSON text, so wrong:
select JSON_VALID( CAST(CAST('2015-01-15' AS DATE) as CHAR CHARACTER SET 'utf8') );
--echo # OK, though:
select JSON_VALID( json_compact(CURDATE()) );
--echo
--echo # Function result - NULL
select JSON_VALID( UPPER(NULL) );
select JSON_VALID( UPPER(CAST(NULL as CHAR)) );
# examples from wl7909 spec
# returns 1
SELECT JSON_VALID( '{ "firstName" : "Fred", "lastName" : "Flintstone" }' );
# returns 1
SELECT JSON_VALID( '3' );
# returns NULL as IS JSON would
SELECT JSON_VALID( null );
--echo # ----------------------------------------------------------------------
--echo # Test of JSON_CONTAINS_PATH function.
--echo # ----------------------------------------------------------------------
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_contains_path();
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_contains_path('{ "a": true }' );
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_contains_path('{ "a": true }', 'all' );
--echo error ER_INVALID_JSON_TEXT_IN_PARAM
select json_contains_path('{ "a": tru }', 'all', '$' );
--echo error ER_INVALID_JSON_PATH
select json_contains_path('{ "a": true }', 'all', '$[' );
--echo error ER_INVALID_JSON_PATH
select json_contains_path('{ "a": true }', 'all', '$a.***[3]' );
--echo error ER_JSON_BAD_ONE_OR_ALL_ARG
select json_contains_path('{ "a": true }', 'foo', '$.a' );
--echo error ER_INVALID_JSON_CHARSET
select json_contains_path('{}', 'all', cast('$' as binary));
select json_contains_path(null, 'all', '$.a' );
select json_contains_path('{ "a": true }', null, '$.a' );
select json_contains_path('{ "a": true }', 'all', null );
# degenerate path
select json_contains_path('{ "a": true }', 'all', '$' );
# positive, one path
select json_contains_path('{ "a": true }', 'all', '$.a' );
select json_contains_path('{ "a": true }', 'one', '$.a' );
# negative, one path
select json_contains_path('{ "a": true }', 'all', '$.b' );
select json_contains_path('{ "a": true }', 'one', '$.b' );
# all
select json_contains_path('{ "a": true }', 'all', '$.a', '$.b' );
select json_contains_path('{ "a": true }', 'all', '$.b', '$.a' );
select json_contains_path('{ "a": true }', 'ALL', '$.a', '$.b' );
select json_contains_path('{ "a": true }', 'aLl', '$.a', '$.b' );
# some
select json_contains_path('{ "a": true }', 'one', '$.a', '$.b' );
select json_contains_path('{ "a": true }', 'one', '$.b', '$.a' );
select json_contains_path('{ "a": true }', 'ONE', '$.a', '$.b' );
select json_contains_path('{ "a": true }', 'oNe', '$.a', '$.b' );
# some wildcards
select json_contains_path('{ "a": true, "b": [ 1, 2, { "c": [ 4, 5, { "d": [ 6, 7, 8, 9, 10 ]} ] } ] }', 'all', '$**[4]' );
select json_contains_path('{ "a": true, "b": [ 1, 2, { "c": [ 4, 5, { "d": [ 6, 7, 8, 9, 10 ]} ] } ] }', 'all', '$**[4]', '$**[5]' );
select json_contains_path('{ "a": true, "b": [ 1, 2, { "c": [ 4, 5, { "d": [ 6, 7, 8, 9, 10 ]} ] } ] }', 'all', '$**.c[2]' );
select json_contains_path('{ "a": true, "b": [ 1, 2, { "c": [ 4, 5, { "d": [ 6, 7, 8, 9, 10 ]} ] } ] }', 'all', '$**.c[3]' );
select json_contains_path('{"a":1, "b":2}', 'one', '$.*');
select json_contains_path('[1,2,3]', 'one', '$.*');
select json_contains_path('{}', 'one', '$[*]');
# combine ellipsis and wildcard
SELECT JSON_CONTAINS_PATH('[1, [[{"x": [{"a":{"b":{"c":42}}}]}]]]',
'one', '$**.a.*');
SELECT JSON_CONTAINS_PATH('[1, [[{"x": [{"a":{"b":{"c":42}}}]}]]]',
'all', '$**.a.*');
SELECT JSON_CONTAINS_PATH('[1,2,3]', 'one', '$**[*]');
SELECT JSON_CONTAINS_PATH('[1,2,3]', 'all', '$**[*]');
# 3 paths
select json_contains_path('{ "a": true, "b": [ 1, 2 ] }', 'all', '$**[1]', '$.b[0]', '$.c' );
select json_contains_path('{ "a": true, "b": [ 1, 2 ] }', 'all', '$.c', '$**[1]', '$.b[0]' );
select json_contains_path('{ "a": true, "b": [ 1, 2 ] }', 'all', '$.b[0]', '$.c', '$**[1]' );
select json_contains_path('{ "a": true, "b": [ 1, 2 ] }', 'one', '$**[1]', '$.b[0]', '$.c' );
select json_contains_path('{ "a": true, "b": [ 1, 2 ] }', 'one', '$.c', '$**[1]', '$.b[0]' );
select json_contains_path('{ "a": true, "b": [ 1, 2 ] }', 'one', '$.b[0]', '$.c', '$**[1]' );
# examples from the wl7909 spec
# returns 0 because there is no element at $.a.c
SELECT JSON_CONTAINS_PATH
(
'{ "a" : 123, "b" : [ 123, 456 ] }',
'all',
'$.a.c',
'$.b[1]'
);
# returns 1 because there is an element at $.b[1]
SELECT JSON_CONTAINS_PATH
(
'{ "a" : 123, "b" : [ 123, 456 ] }',
'one',
'$.a.c',
'$.b[1]'
);
# returns 0 because there is no element at the given path
SELECT JSON_CONTAINS_PATH
(
'{ "a" : 123, "b" : [ 123, 456 ] }',
'all',
'$.c'
);
# returns 1 because there is an element at $.b[1].c.d
SELECT JSON_CONTAINS_PATH
(
'{ "a" : 123, "b" : [ 123, { "c" : { "d" : true } } ] }',
'all',
'$.b[1].c.d'
);
select json_length( null );
select json_length( '1' );
--echo
--echo # invalid json text
--echo error ER_INVALID_JSON_TEXT_IN_PARAM
select json_length( 'abc' );
select json_length( '"abc"' );
select json_length( 'true' );
select json_length( 'false' );
select json_length( 'null' );
select json_length( '{}' );
select json_length( '{ "a" : 100, "b" : 200 }' );
select json_length( '{ "a" : 100, "b" : [ 300, 400, 500 ] }' );
select json_length( '[]' );
select json_length( '[ null, "foo", true, 1.1 ]' );
select json_length( '[ null, "foo", true, { "a" : "b", "c" : "d" } ]' );
select json_length( '"foo"' );
select json_length( '1.2' );
# bad path expressions
--echo
--echo # invalid json path
--echo error ER_INVALID_JSON_PATH
select json_length( 'true', 'c$' );
--echo
--echo # invalid json path
--echo error ER_INVALID_JSON_PATH
select json_length( '{ "foo" : [ true, false ] }', '$.foo[bar]' );
--echo
--echo # wildcards not allowed in path expressions for this function
--echo error ER_INVALID_JSON_PATH_WILDCARD
select json_length( 'true', '$.*' );
--echo
--echo # wildcards not allowed in path expressions for this function
--echo error ER_INVALID_JSON_PATH_WILDCARD
select json_length( 'true', '$.foo**.bar' );
# json_length() with non-vacuous path expressions
# 1
select json_length( '[ 1, [ 2, 3, 4 ], 5 ]', '$[0]' );
# 3
select json_length( '[ 1, [ 2, 3, 4 ], 5 ]', '$[1]' );
# 1
select json_length( '[ 1, [ 2, 3, 4 ], 5 ]', '$[2]' );
# auto-wrapping: 1
select json_length( '[ 1, [ 2, 3, 4 ], 5 ]', '$[2][0]' ); # auto-wrap scalar
select json_length( '[ 1, [ 2, 3, 4 ], {"a": 1} ]', '$[2][0]' ); # ditto object
# non-existent path: null
select json_length( '[ 1, [ 2, 3, 4 ], 5 ]', '$[2][1]' );
# 3
select json_length( '[ 1, { "a": true, "b": false, "c": null }, 5 ]', '$[1]' );
# examples from the wl7909 spec
# returns 0
SELECT JSON_LENGTH
(
'{}'
);
# returns 1
SELECT JSON_LENGTH
(
'3'
);
# returns 2
SELECT JSON_LENGTH
(
'{ "a" : 123, "b" : [ 123, 456, 789 ] }'
);
# returns 3
SELECT JSON_LENGTH
(
'{ "a" : 123, "b" : [ 123, 456, 789 ] }',
'$.b'
);
# returns null because the path does not exist
SELECT JSON_LENGTH
(
'{ "a" : 123, "b" : [ 123, 456, 789 ] }',
'$.c'
);
--echo # ----------------------------------------------------------------------
--echo # Test of JSON_DEPTH function.
--echo # ----------------------------------------------------------------------
select json_depth(null);
select json_depth(json_compact(null));
#select i, json_depth(j) from t1;
select json_depth(json_compact('[]')),
json_depth(json_compact('{}')),
json_depth(json_compact('null')),
json_depth(json_quote('foo'));
select json_depth(json_compact('[[2], 3, [[[4]]]]'));
select json_depth(json_compact('{"a": {"a1": [3]}, "b": {"b1": {"c": {"d": [5]}}}}'));
# examples from the wl7909 spec
# returns 1
SELECT JSON_DEPTH
(
'{}'
);
# returns 1
SELECT JSON_DEPTH
(
'[]'
);
# returns 1
SELECT JSON_DEPTH( '"abc"' );
# returns 1
SELECT JSON_DEPTH( json_compact( '"abc"') );
--echo error ER_INVALID_TYPE_FOR_JSON
SELECT JSON_DEPTH( 1 );
#Check after fix MDEV-31728
--disable_cursor_protocol
--echo error ER_INVALID_JSON_TEXT_IN_PARAM
SELECT JSON_DEPTH( 'abc' );
--enable_cursor_protocol
# returns 1
SELECT JSON_DEPTH( json_compact( 1) );
# returns 2
SELECT JSON_DEPTH
(
'{ "a" : true, "b" : false, "c" : null }'
);
# returns 2
SELECT JSON_DEPTH
(
'[ "a", true, "b" , false, "c" , null ]'
);
# returns 2
SELECT JSON_DEPTH
(
'{ "a" : true, "b" : {}, "c" : null }'
);
# returns 2
SELECT JSON_DEPTH
(
'[ "a", true, "b" , {}, "c" , null ]'
);
# returns 3
SELECT JSON_DEPTH
(
'{ "a" : true, "b" : { "e" : false }, "c" : null }'
);
# returns 3
SELECT JSON_DEPTH
(
'[ "a", true, "b" , { "e" : false }, "c" , null ]'
);
#Check after fix MDEV-31728
--disable_cursor_protocol
--echo error ER_INVALID_JSON_TEXT_IN_PARAM
SELECT JSON_DEPTH
(
'[ "a", true, "b" , { "e" : false }, "c" , null'
);
--enable_cursor_protocol
--echo # ----------------------------------------------------------------------
--echo # Test of JSON_REMOVE function.
--echo # ----------------------------------------------------------------------
# null args
select json_remove( null, '$[1]' );
select json_remove( null, '$[1]' ) is null;
select json_remove( '[ 1, { "a": true, "b": false, "c": null }, 5 ]', null );
select json_remove( '[ 1, { "a": true, "b": false, "c": null }, 5 ]', null ) is null;
select json_remove( '[ 1, { "a": true, "b": false, "c": null }, 5 ]', '$[1]', null );
select json_remove( '[ 1, { "a": true, "b": false, "c": null }, 5 ]', '$[1]', null ) is null;
# too few args
--echo
--echo # not enough args
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_remove();
--echo
--echo # not enough args
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_remove( '[ 1, { "a": true, "b": false, "c": null }, 5 ]' );
--echo
--echo # not enough args
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_remove( '$[1]' );
# malformed args
--echo
--echo # invalid json text
--echo error ER_INVALID_JSON_TEXT_IN_PARAM
select json_remove( '[ 1, { "a": true, "b": false, "c": null }, 5 ', '$[1]', '$[2]' );
--echo
--echo # invalid json path
--echo error ER_INVALID_JSON_PATH
select json_remove( '[ 1, { "a": true, "b": false, "c": null }, 5 ]', '$[1', '$[2]' );
--echo
--echo # invalid json path
--echo error ER_INVALID_JSON_PATH
select json_remove( '[ 1, { "a": true, "b": false, "c": null }, 5 ]', '$[1]', '$[2' );
--echo
--echo # Vacuous path expression
--echo error ER_JSON_VACUOUS_PATH
select json_remove( '[ 1, 2, 3 ]', '$' );
--echo
--echo # Vacuous path expression
--echo error ER_JSON_VACUOUS_PATH
select json_remove( '[ 1, 2, 3 ]', '$', '$[2]' );
--echo
--echo # Vacuous path expression
--echo error ER_JSON_VACUOUS_PATH
select json_remove( '[ 1, 2, 3 ]', '$[1]', '$' );
# wildcard/ellipsis not allowed in paths
--echo error ER_INVALID_JSON_PATH_WILDCARD
select json_remove( '[ 1, 2, 3 ]', '$[*]' );
--echo error ER_INVALID_JSON_PATH_WILDCARD
select json_remove( '[ 1, 2, 3 ]', '$**[2]' );
--echo error ER_INVALID_JSON_PATH_WILDCARD
select json_remove( '[ 1, 2, 3 ]', '$[2]', '$[*]' );
--echo error ER_INVALID_JSON_PATH_WILDCARD
select json_remove( '[ 1, 2, 3 ]', '$[2]', '$**[2]' );
# simple matches
select json_remove( '[ 1, 2, 3 ]', '$[0]' );
select json_remove( '[ 1, 2, 3 ]', '$[1]' );
select json_remove( '[ 1, 2, 3 ]', '$[2]' );
select json_remove( '[ 1, 2, 3 ]', '$[3]' );
select json_remove( '[ 1, { "a": true, "b": false, "c": null }, 5 ]', '$[1]' );
# one match nested inside another
--echo error ER_INVALID_JSON_PATH_WILDCARD
select json_remove( '[ { "a": { "a": true } } ]', '$**.a' );
# multiple paths
select json_remove( '[ { "a": true }, { "b": false }, { "c": null }, { "a": null } ]', '$[0].a', '$[2].c' );
# ellipsis with matches at different levels
--echo error ER_INVALID_JSON_PATH_WILDCARD
select json_remove( '[ { "a": true }, { "b": [ { "c": { "a": true } } ] }, { "c": null }, { "a": null } ]', '$**.a' );
# nonsense path
select json_remove( '{"id": 123, "name": "systemQA", "array": [1, 2, 3]}', '$[0]' );
# examples from wl7909 spec
# returns the document {"a": "foo", "b": [true]}
SELECT JSON_REMOVE
(
'{"a" : "foo", "b" : [true, {"c" : 123}]}',
'$.b[ 1 ]'
);
# returns {"a": "foo", "b": [true, {}]} due to normalization
SELECT JSON_REMOVE
(
'{ "a" : "foo", "b" : [ true, { "c" : 123, "c" : 456 } ] }',
'$.b[ 1 ].c'
);
# returns {"a": "foo", "b": [true, {}]}
SELECT JSON_REMOVE
(
'{ "a" : "foo", "b" : [ true, { "c" : 123 } ] }',
'$.b[ 1 ].c'
);
# returns the original document because the path doesn't identify an element
SELECT JSON_REMOVE
(
'{ "a" : "foo", "b" : [ true, { "c" : 123, "d" : 456 } ] }',
'$.b[ 1 ].e'
);
--echo # ----------------------------------------------------------------------
--echo # Test of JSON_MERGE function.
--echo # ----------------------------------------------------------------------
--echo
--echo # not enough args
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_merge();
--echo
--echo # not enough args
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_merge( '[ 1, 2, 3 ]' );
# null args result in NULL value
select json_merge( null, null );
select json_merge( null, '[ 1, 2, 3 ]' );
select json_merge( '[ 1, 2, 3 ]', null );
select json_merge( null, '[ 1, 2, 3 ]', '[ 4, 5, 6 ]' );
select json_merge( '[ 1, 2, 3 ]', null, '[ 4, 5, 6 ]' );
select json_merge( '[ 1, 2, 3 ]', '[ 4, 5, 6 ]', null );
--echo error ER_INVALID_JSON_TEXT_IN_PARAM
select json_merge( '[1, 2]', '[3, 4' );
--echo error ER_INVALID_JSON_TEXT_IN_PARAM
select json_merge( '[1, 2', '[3, 4]' );
# good json_merge() expressions
select json_merge( '1', '2' );
select json_merge( '1', '[2, 3]' );
select json_merge( '[1, 2]', '3' );
select json_merge( '1', '{ "a": 2 }' );
select json_merge( '{ "a": 2 }', '1' );
select json_merge( '[1, 2]', '[3, 4]' );
select json_merge( '{ "a": 2 }', '{ "b": 3}' );
select json_merge( '[1, 2]', '{ "a": 2 }' );
select json_merge( '{ "a": 2 }', '[1, 2]' );
select json_merge( '{"a": 1, "b": 2 }', '{"b": 3, "d": 4 }' );
select json_merge( '{"a": 1, "b": 2 }', '{"b": [3, 4], "d": 4 }' );
select json_merge( '{"a": 1, "b": [2, 3] }', '{"b": 4, "d": 4 }' );
select json_merge( '{"a": 1, "b": 2 }', '{"b": {"e": 7, "f": 8}, "d": 4 }' );
select json_merge( '{"b": {"e": 7, "f": 8}, "d": 4 }', '{"a": 1, "b": 2 }' );
select json_merge( '{"a": 1, "b": [2, 9] }', '{"b": [10, 11], "d": 4 }' );
select json_merge( '{"a": 1, "b": [2, 9] }', '{"b": {"e": 7, "f": 8}, "d": 4 }' );
select json_merge( '{"b": {"e": 7, "f": 8}, "d": 4 }', '{"a": 1, "b": [2, 9] }' );
select json_merge( '{"b": {"e": 7, "f": 8}, "d": 4 }', '{ "a": 1, "b": {"e": 20, "g": 21 } }' );
select json_merge( '1', '2', '3' );
select json_merge( '[1, 2 ]', '3', '[4, 5]' );
select json_merge
(
'{ "a": true, "b": { "c": 3, "d": 4 }, "e": [ 1, 2 ] }',
'{ "d": false, "b": { "g": 3, "d": 5 }, "f": [ 1, 2 ] }',
'{ "m": true, "b": { "h": 8, "d": 4 }, "e": [ 3, 4 ] }'
);
# examples from the wl7909 spec
# returns [{"a": "foo", "b": [true, {"c": 123}]}, 5, 6]
SELECT JSON_MERGE
(
'{ "a" : "foo", "b" : [ true, { "c" : 123 } ] }',
'[ 5, 6]'
);
# returns {"a": "foo", "b": [true, {"c": 123}, false, 34]}
SELECT JSON_MERGE
(
'{ "a" : "foo", "b" : [ true, { "c" : 123 } ] }',
'{ "b": [ false, 34 ] }'
);
# returns {"a": "foo", "b": [true, {"c": 123}, "bar"]}
SELECT JSON_MERGE
(
'{ "a" : "foo", "b" : [ true, { "c" : 123 } ] }',
'{ "b": "bar" }'
);
# returns {"a": {"b": 1, "c": 1}}
SELECT JSON_MERGE
(
'{ "a" : { "b" : 1 } }',
'{ "a" : { "c" : 1 } }'
);
--echo # ----------------------------------------------------------------------
--echo # Test of JSON_TYPE function.
--echo # ----------------------------------------------------------------------
# negative test
--echo error ER_INVALID_JSON_TEXT_IN_PARAM
select json_type('abc');
# Enable after fix MDEV-31554
--disable_cursor_protocol
#select i, json_type(j) from t1;
select json_type('{"a": 2}');
select json_type('[1,2]');
select json_type('"scalar string"');
select json_type('true');
select json_type('false');
select json_type('null');
select json_type('1');
select json_type('-0');
select json_type('-0.0');
--echo error ER_INVALID_TYPE_FOR_JSON
select json_type(-1);
--echo error ER_INVALID_TYPE_FOR_JSON
select json_type(CAST(1 AS UNSIGNED));
select json_type('32767');
--echo error ER_INVALID_TYPE_FOR_JSON
select json_type(PI());
select json_type('3.14');
--echo error ER_INVALID_JSON_TEXT_IN_PARAM
select json_type(CAST(CAST('2015-01-15' AS DATE) as CHAR CHARACTER SET 'utf8'));
--echo # ----------------------------------------------------------------------
--echo # Test of json_compact(literal)
--echo # ----------------------------------------------------------------------
select json_type(json_compact(cast('2014-11-25 18:00' as datetime)));
select json_type(json_compact(cast('2014-11-25' as date)));
select json_type(json_compact(cast('18:00:59' as time)));
# select json_type(json_compact(cast('2014-11-25 18:00' as timestamp))); -- cast target type not supported
# select json_type(json_compact(cast('1999' as year))); -- cast target type not supported
select json_type(json_compact(127));
select json_type(json_compact(255));
select json_type(json_compact(32767));
select json_type(json_compact(65535));
select json_type(json_compact(8388607));
select json_type(json_compact(16777215));
select json_type(json_compact(2147483647));
select json_type(json_compact(4294967295));
select json_type(json_compact(9223372036854775807));
select json_type(json_compact(18446744073709551615));
select json_type(json_compact(true));
select json_type(json_compact(b'10101'));
select json_type(json_compact(cast(3.14 as decimal(5,2))));
select json_type(json_compact(3.14));
select json_type(json_compact(3.14E30));
# select json_type(json_compact(cast(3.14 as numeral(5,2)))); -- cast target type not supported
# select json_type(json_compact(cast(3.14 as double))); -- cast target type not supported
# select json_type(json_compact(cast(3.14 as float))); -- cast target type not supported
# select json_type(json_compact(cast(b'10101' as bit(10)))); -- cast target type not supported
# select json_type(json_compact(cast('10101abcde' as blob))); -- cast target type not supported
select json_type(json_compact(cast('10101abcde' as binary)));
# select json_type(json_compact(cast('a' as enum('a','b','c')))); -- cast target type not supported
# select json_type(json_compact(cast('a,c' as set('a','b','c')))); -- cast target type not supported
select json_type(json_compact(ST_GeomFromText('POINT(1 1)')));
select json_type(json_compact(ST_GeomFromText('LINESTRING(0 0,1 1,2 2)')));
select json_type(json_compact(ST_GeomFromText('POLYGON((0 0,10 0,10 10,0 10,0 0),
(5 5,7 5,7 7,5 7, 5 5))')));
select json_type(json_compact(null));
select json_type(json_compact(null)) is null; # check that it is an SQL NULL
select json_type(null) is null; # is an SQL NULL
--enable_cursor_protocol
#
# same, but now show the printable value:
#
select json_compact(cast('2014-11-25 18:00' as datetime));
select json_compact(cast('2014-11-25' as date));
select json_compact(cast('18:00:59' as time));
# select json_compact(cast('2014-11-25 18:00' as timestamp)); -- cast target type not supported
# select json_compact(cast('1999' as year)); -- cast target type not supported
select json_compact(127);
select json_compact(255);
select json_compact(32767);
select json_compact(65535);
select json_compact(8388607);
select json_compact(16777215);
select json_compact(2147483647);
select json_compact(4294967295);
select json_compact(9223372036854775807);
select json_compact(18446744073709551615);
select json_compact(true);
select json_compact(b'10101');
select json_compact(cast(3.14 as decimal(5,2)));
select json_compact(3.14);
select json_compact(3.14e0);
# select json_compact(cast(3.14 as numeral(5,2))); -- cast target type not supported
# select json_compact(cast(3.14 as double)); -- cast target type not supported
# select json_compact(cast(3.14 as float)); -- cast target type not supported
# select json_compact(cast(b'10101' as bit(10)); -- cast target type not supported
# select json_compact(cast('10101abcde' as blob)); -- cast target type not supported
select json_compact(cast('10101abcde' as binary));
# select json_compact(cast('a' as enum('a','b','c')); -- cast target type not supported
# select json_compact(cast('a,c' as set('a','b','c')); -- cast target type not supported
select json_compact(ST_GeomFromText('POINT(1 1)'));
select json_compact(ST_GeomFromText('LINESTRING(0 0,1 1,2 2)'));
select json_compact(ST_GeomFromText('POLYGON((0 0,10 0,10 10,0 10,0 0),
(5 5,7 5,7 7,5 7, 5 5))'));
select json_compact(null);
select json_compact(null) is null; # check that it is an SQL NULL
--echo # ----------------------------------------------------------------------
--echo # Test of JSON_KEYS function.
--echo # ----------------------------------------------------------------------
# should all give NULL:
select json_keys(NULL);
select json_keys(NULL, '$.b');
select json_keys(NULL, NULL);
select json_keys('{"a": 1, "b": {"e": "foo", "b": 3}}', '$.a');
select json_keys('{"a": 1, "b": {"e": "foo", "b": 3}}', NULL);
# non NULL results
select json_keys('{"a": 1, "b": {"e": "foo", "b": 3}}');
select json_keys('{"a": 1, "b": {"e": "foo", "b": 3}}', '$.b');
--echo error ER_INVALID_JSON_PATH_WILDCARD
select json_keys('{"a": 1, "b": {"e": "foo", "b": 3}}', '$.*.b');
# Examples from the specification
--echo # returns [ "a", "b" ]
SELECT JSON_KEYS('{ "a" : "foo", "b" : [ true, { "c" : "123" } ] }');
--echo # returns []
SELECT JSON_KEYS('{ "a" : "foo", "b" : [ true, { "c" : {} } ] }',
'$.b[1].c');
--echo # returns NULL
SELECT JSON_KEYS('{ "a" : "foo", "b" : [ true, { "c" : {} } ] }',
'$.a.b[2]');
--echo error ER_INVALID_JSON_PATH
SELECT JSON_KEYS('{"a":1}', '1010');
--echo error ER_INVALID_JSON_PATH
SELECT JSON_KEYS('{"a":1}', '1010') IS NULL;
# examples from the wl7909 spec
# returns [ "a", "b" ]
SELECT JSON_KEYS
(
'{ "a" : "foo", "b" : [ true, { "c" : "123" } ] }'
);
# returns []
SELECT JSON_KEYS
(
'{ "a" : "foo", "b" : [ true, { "c" : {} } ] }',
'$.b[1].c'
);
# returns NULL
SELECT JSON_KEYS
(
'{ "a" : "foo", "b" : [ true, { "c" : {} } ] }',
'$.a.b[2]'
);
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
SELECT JSON_KEYS();
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
SELECT JSON_KEYS('{}', '$', '$');
--echo # ----------------------------------------------------------------------
--echo # CAST(<json> AS CHAR). See also 'json_conversions.test' for other
--echo # conversion tests.
--echo # ----------------------------------------------------------------------
select cast(json_keys('{"a": 1}') as char);
select cast(json_compact(1) as char);
select cast(json_keys(NULL) as char);
#select cast(j as char) from keys1;
--echo # ----------------------------------------------------------------------
--echo # Path matching with double-quotes
--echo # ----------------------------------------------------------------------
# matches
select json_extract( '{ "one potato" : 1 }', '$."one potato"' );
# matches
select json_extract( '{ "a.b" : 1 }', '$."a.b"' );
# doesn't match
select json_extract( '{ "\\"a\\"": 1}', '$."a"' );
# matches
select json_extract( '{ "\\"a\\"": 1}', '$."\\"a\\""' );
# matches
select json_extract( '{ "a": 1}', '$."a"' );
# matches
select json_extract( '{ "a": 1}', '$.a' );
# examples from functional spec section on Path Syntax
# [3, 2]
select json_extract( '{ "a": [ [ 3, 2 ], [ { "c" : "d" }, 1 ] ], "b": { "c" : 6 }, "one potato": 7, "b.c" : 8 }', '$.a[0]' );
# 2
select json_extract( '{ "a": [ [ 3, 2 ], [ { "c" : "d" }, 1 ] ], "b": { "c" : 6 }, "one potato": 7, "b.c" : 8 }', '$.a[0][1]' );
# [ { "c": "d" }, 1 ]
select json_extract( '{ "a": [ [ 3, 2 ], [ { "c" : "d" }, 1 ] ], "b": { "c" : 6 }, "one potato": 7, "b.c" : 8 }', '$.a[1]' );
# { "c": "d" }
select json_extract( '{ "a": [ [ 3, 2 ], [ { "c" : "d" }, 1 ] ], "b": { "c" : 6 }, "one potato": 7, "b.c" : 8 }', '$.a[1][0]' );
# "d"
select json_extract( '{ "a": [ [ 3, 2 ], [ { "c" : "d" }, 1 ] ], "b": { "c" : 6 }, "one potato": 7, "b.c" : 8 }', '$.a[1][0].c' );
# 7
select json_extract( '{ "a": [ [ 3, 2 ], [ { "c" : "d" }, 1 ] ], "b": { "c" : 6 }, "one potato": 7, "b.c" : 8 }', '$."one potato"' );
# 6
select json_extract( '{ "a": [ [ 3, 2 ], [ { "c" : "d" }, 1 ] ], "b": { "c" : 6 }, "one potato": 7, "b.c" : 8 }', '$.b.c' );
# 8
select json_extract( '{ "a": [ [ 3, 2 ], [ { "c" : "d" }, 1 ] ], "b": { "c" : 6 }, "one potato": 7, "b.c" : 8 }', '$."b.c"' );
--echo # ----------------------------------------------------------------------
--echo # Test of JSON_EXTRACT function.
--echo # ----------------------------------------------------------------------
# errors
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_extract(NULL);
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_extract('$.b');
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_extract('{"a": 1, "b": {"e": "foo", "b": 3}}');
# Confused argument order
--echo error ER_INVALID_JSON_TEXT_IN_PARAM
select json_extract('$.a', '{"a": 1, "b": {"e": "foo", "b": 3}}');
# NULLs
select json_extract(NULL, '$.b');
select json_extract(NULL, NULL);
# non-NULLs
select json_extract('{"a": 1, "b": {"e": "foo", "b": 3}}', '$.a');
select json_extract('{"a": 1, "b": {"e": "foo", "b": 3}}', '$.*');
select json_extract('{"a": 1, "b": {"e": "foo", "b": 3}}', '$.a', '$.b.e');
select json_extract('{"a": 1, "b": [1,2,3]}', '$.b[2]');
# one path is NULL
select json_extract('{"a": 1, "b": {"e": "foo", "b": 3}}', '$.a', NULL);
# Examples from the specification
--echo # returns a JSON value containing just the string "123"
SELECT JSON_EXTRACT('{ "a" : "foo", "b" : [ true, { "c" : "123" } ] }',
'$.b[ 1 ].c');
--echo # returns a JSON value containing just the number 123
SELECT JSON_EXTRACT('{ "a" : "foo", "b" : [ true, { "c" : 123 } ] }',
'$.b[ 1 ].c');
--echo # raises an error because the document is not valid
--echo error ER_INVALID_JSON_TEXT_IN_PARAM
SELECT JSON_EXTRACT('{ "a" : [ }',
'$.b[ 1 ].c');
--echo # raises an error because the path is invalid
--echo error ER_INVALID_JSON_PATH
SELECT JSON_EXTRACT('{ "a" : "foo", "b" : [ true, { "c" : 123 } ] }',
'$.b[ 1 ].');
--echo # returns a JSON value containing the number 123 (because of
--echo # auto-wrapping the scalar)
SELECT JSON_EXTRACT('{ "a" : "foo", "b" : [ true, { "c" : 123 } ] }',
'$.b[ 1 ].c[ 0 ]');
--echo # returns a JSON value containing the object because of auto-wrapping
SELECT JSON_EXTRACT('{ "a" : "foo", "b" : [ true, { "c" : {"not array": 4} } ] }',
'$.b[ 1 ].c[ 0 ]');
--echo # returns null because the path, although valid, does not identify a value
SELECT JSON_EXTRACT('{ "a" : "foo", "b" : [ true, { "c" : 123 } ] }',
'$.b[ 1 ].c[ 1 ]');
--echo # returns a JSON value containing the number 123 (due to normalization)
SELECT JSON_EXTRACT('{ "a" : "foo", "b" : [ true, { "c" : 123, "c" : 456 } ] }',
'$.b[ 1 ].c');
--echo # returns a JSON array [ "foo", true ]
SELECT JSON_EXTRACT('{ "a" : "foo", "b" : [ true, { "c" : 123, "c" : 456 } ] }',
'$.a', '$.b[0]');
--echo # returns a JSON array [ true ]
SELECT JSON_EXTRACT('{ "a" : "foo", "b" : [ true, { "c" : 123, "c" : 456 } ] }',
'$.d', '$.b[0]');
# some examples verifying ellipsis behavior
# should have same result
select json_extract( '[1]', '$[0][0]' );
select json_extract( '[1]', '$**[0]' );
# should have same result
select json_extract( '{ "a": 1 }', '$.a[0]' );
# Enable after fix MDEV-31554
--disable_cursor_protocol
select json_extract( '{ "a": 1 }', '$**[0]' );
--enable_cursor_protocol
# should have same result
select json_extract( '{ "a": 1 }', '$[0].a' );
select json_extract( '{ "a": 1 }', '$**.a' );
# should have same result
select json_extract( '{ "a": 1 }', '$[0].a[0]' );
# Enable after fix MDEV-31554
--disable_cursor_protocol
select json_extract( '{ "a": 1 }', '$**[0]' );
--enable_cursor_protocol
# should have the same result
select json_extract( '{ "a": 1 }', '$[0].a' );
select json_extract( '{ "a": 1 }', '$**.a' );
select json_extract( '{ "a": 1 }', '$[0][0].a' );
select json_extract( '{ "a": 1 }', '$[0][0][0].a' );
# should have the same result
SELECT JSON_EXTRACT('[1, [[{"x": [{"a":{"b":{"c":42}}}]}]]]', '$**.a.*');
SELECT JSON_EXTRACT('[1, [[{"x": [{"a":{"b":{"c":42}}}]}]]]',
'$[1][0][0].x[0].a.*');
# examples from the wl7909 spec
# returns a JSON value containing just the string "123"
SELECT JSON_EXTRACT
(
'{ "a" : "foo", "b" : [ true, { "c" : "123" } ] }',
'$.b[ 1 ].c'
);
# returns a JSON value containing just the number 123
SELECT JSON_EXTRACT
(
'{ "a" : "foo", "b" : [ true, { "c" : 123 } ] }',
'$.b[ 1 ].c'
);
--echo error ER_INVALID_JSON_TEXT_IN_PARAM
SELECT JSON_EXTRACT
(
'{ "a" : [ }',
'$.b[ 1 ].c'
);
--echo error ER_INVALID_JSON_PATH
SELECT JSON_EXTRACT
(
'{ "a" : "foo", "b" : [ true, { "c" : 123 } ] }',
'$.b[ 1 ].'
);
# returns a JSON value containing the number 123 (because of auto-wrapping)
SELECT JSON_EXTRACT
(
'{ "a" : "foo", "b" : [ true, { "c" : 123 } ] }',
'$.b[ 1 ].c[ 0 ]'
);
# returns null because the path, although valid, does not identify a value
SELECT JSON_EXTRACT
(
'{ "a" : "foo", "b" : [ true, { "c" : 123 } ] }',
'$.b[ 1 ].c[ 1 ]'
);
# returns a JSON value containing the number 123 (due to normalization)
SELECT JSON_EXTRACT
(
'{ "a" : "foo", "b" : [ true, { "c" : 123, "c" : 456 } ] }',
'$.b[ 1 ].c'
);
# returns a JSON array ["foo", true]
SELECT JSON_EXTRACT
(
'{ "a" : "foo", "b" : [ true, { "c" : 123, "c" : 456 } ] }',
'$.a', '$.b[0]'
);
# returns a the 'true' literal
SELECT JSON_EXTRACT
(
'{ "a" : "foo", "b" : [ true, { "c" : 123, "c" : 456 } ] }',
'$.d', '$.b[0]'
);
# should return NULL
select json_extract( '[ { "a": 1 }, { "a": 2 } ]', '$[*].b' ) jdoc;
# should return NULL
select json_extract( '[ { "a": 1 }, { "a": 2 } ]', '$[0].b' ) jdoc;
# should return 1
select json_extract( '[ { "a": 1 }, { "a": 2 } ]', '$[0].a' ) jdoc;
# should return [1, 2]
select json_extract( '[ { "a": 1 }, { "a": 2 } ]', '$[*].a' ) jdoc;
# should return [1]
select json_extract( '[ { "a": 1 }, { "b": 2 } ]', '$[*].a' ) jdoc;
# should return [3, 4]
select json_extract( '[ { "a": [3,4] }, { "b": 2 } ]', '$[0].a' ) jdoc;
# should return [[3, 4]]
select json_extract( '[ { "a": [3,4] }, { "b": 2 } ]', '$[*].a' ) jdoc;
# should return [[3, 4]]
select json_extract( '[ { "a": [3,4] }, { "b": 2 } ]', '$[0].a', '$[1].a' ) jdoc;
--echo # ----------------------------------------------------------------------
--echo # Test of JSON_ARRAY_APPEND function.
--echo # ----------------------------------------------------------------------
# NULLs
select json_array_append(NULL, '$.b', json_compact(1));
select json_array_append('[1,2,3]', NULL, json_compact(1));
# Enable after fix MDEV-31554
--disable_cursor_protocol
select json_array_append('[1,2,3]', '$', NULL);
--enable_cursor_protocol
# wrong # args
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_array_append(NULL);
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_array_append(NULL, NULL);
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_array_append(NULL, NULL, NULL, NULL);
# auto-wrap
SELECT JSON_ARRAY_APPEND(json_compact('1'), '$', 3);
SELECT JSON_ARRAY_APPEND(json_compact('{"a": 3}'), '$', 3);
--echo error ER_INVALID_JSON_PATH_WILDCARD
select json_array_append(json_compact('{"a": {"b": [3]}}'), '$**[0]', 6);
# Examples from the specification
--echo # Auto-wrapping, since because the paths identify scalars.
--echo # should return {"a": "foo", "b": ["bar", 4], "c": ["wibble", "grape"]}
# Enable after fix MDEV-31554
--disable_cursor_protocol
SELECT JSON_ARRAY_APPEND('{"a": "foo", "b": "bar", "c": "wibble"}',
'$.b', json_compact(4),
'$.c', json_compact('"grape"'));
--enable_cursor_protocol
--echo # should return {"a": "foo", "b": [1, 2, 3, 4],
--echo # "c": ["apple", "pear", "grape"]}
SELECT JSON_ARRAY_APPEND('{"a" : "foo","b": [1, 2, 3], "c": ["apple", "pear"]}',
'$.b', json_compact(4),
'$.c', json_compact('"grape"'));
# without CAST: cf. not required for ANY_JSON_ATOM arguments in specification
SELECT JSON_ARRAY_APPEND('{"a" : "foo","b": [1, 2, 3], "c": ["apple", "pear"]}',
'$.b', 4,
'$.c', 'grape');
# wild cards, multiple pairs
--echo error ER_INVALID_JSON_PATH_WILDCARD
select json_array_append( '[[], [], []]', '$[*]', 3, '$[*]', 4);
--echo error ER_INVALID_JSON_PATH_WILDCARD
select json_array_append( '[[], "not array", []]', '$[*]', 3, '$[*]', 4);
# examples from wl7909 spec
# should return {"a": "foo", "b": ["bar", 4], "c": ["wibble", "grape"]} due to autowrapping
SELECT JSON_ARRAY_APPEND
(
'{ "a" : "foo", "b" : "bar", "c" : "wibble" }',
'$.b', 4,
'$.c', "grape"
);
# should return {"a": "foo", "b": [1, 2, 3, 4], "c": ["apple", "pear", "grape"]}
SELECT JSON_ARRAY_APPEND
(
'{ "a" : "foo", "b" : [ 1, 2, 3 ], "c" : [ "apple", "pear" ] }',
'$.b', 4,
'$.c', "grape"
);
--echo # ----------------------------------------------------------------------
--echo # Bug#21373874 ASSERTION `PARENT' FAILED
--echo # ----------------------------------------------------------------------
#Enable after fix MDEV-31554
--disable_cursor_protocol
select json_array_append('{"a":1}', '$[0]', 100);
--enable_cursor_protocol
select json_array_append('3', '$[0]', 100);
select json_array_append('3', '$[0][0][0][0]', 100);
--echo # ----------------------------------------------------------------------
--echo # Test of JSON_INSERT function.
--echo # ----------------------------------------------------------------------
# NULLs
select json_insert(NULL, '$.b', json_compact(1));
select json_insert('[1,2,3]', NULL, json_compact(1));
#Enable after fix MDEV-31554
--disable_cursor_protocol
select json_insert('[1,2,3]', '$[3]', NULL);
--enable_cursor_protocol
# wrong # args
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_insert(NULL);
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_insert(NULL, NULL);
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_insert(NULL, NULL, NULL, NULL);
# positive test cases
select json_insert('[1,2,3]', '$[2]', 4);
select json_insert('[1,2,3]', '$[3]', 4);
select json_insert('[1,2,3]', '$[10]', 4);
select json_insert('{"c":4}', '$.c', 4);
# Enable after fix MDEV-31554
--disable_cursor_protocol
select json_insert('{"c":4}', '$.a', 4);
--enable_cursor_protocol
select json_insert('1', '$', 4);
select json_insert('1', '$[0]', 4);
select json_insert('1', '$[1]', 4);
select json_insert('1', '$[10]', '4', '$[11]', 5);
select json_insert('[1,2,3]', '$[2][0]', 4);
select json_insert('[1,2,3]', '$[2][2]', 4);
select json_insert('{"a": 3}', '$.a[0]', 4);
select json_insert('{"a": 3}', '$.a[1]', 4, '$.a[2]', '5');
# wild card & auto-wrap (scalars)
--echo error ER_INVALID_JSON_PATH_WILDCARD
select json_insert(json_compact('{"a": [1], "b": 2}'), '$.*[1]', 6);
--echo error ER_INVALID_JSON_PATH_WILDCARD
select json_insert(json_compact('{"a": 1, "b": 2}'), '$.*[1]', 6);
--echo error ER_INVALID_JSON_PATH_WILDCARD
select json_insert(json_compact('{"a": {"b": 3}}'), '$.a.*[1]', 6);
--echo error ER_INVALID_JSON_PATH_WILDCARD
select json_insert(json_compact('{"a": {"b": [3]}}'), '$.a.*[1]', 6);
--echo error ER_INVALID_JSON_PATH_WILDCARD
select json_insert(json_compact('{"a": {"b": 3}}'), '$**[1]', 6);
--echo error ER_INVALID_JSON_PATH_WILDCARD
select json_insert(json_compact('{"a": {"b": [3]}}'), '$**[1]', 6);
--echo error ER_INVALID_JSON_PATH_WILDCARD
select json_insert(json_compact('[1]'), '$[*][1]', 6);
--echo error ER_INVALID_JSON_PATH_WILDCARD
select json_insert(json_compact('[1]'), '$**[1]', 6);
--echo error ER_INVALID_JSON_PATH_WILDCARD
select json_insert(json_compact('[1, [2], 3]'), '$[*][1]', 6);
--echo error ER_INVALID_JSON_PATH_WILDCARD
select json_insert(json_compact('[1, [2], 3]'), '$**[1]', 6);
--echo error ER_INVALID_JSON_PATH_WILDCARD
select json_insert(json_compact('[[1]]'), '$[*][1]', 6);
--echo error ER_INVALID_JSON_PATH_WILDCARD
select json_insert(json_compact('[[1]]'), '$**[1]', 6);
# auto-wrap object
select json_insert(json_compact('{"a": 3}'), '$[1]', 6);
# Examples from the specification
# returns the original document because the path does exist
SELECT JSON_INSERT('{ "a" : "foo", "b" : [ 1, 2, 3 ] }', '$.a', true);
# inserts a number, returns '{ "a" : "foo", "b" : [ 1, 2, 3 ], "c" : 123 }
SELECT JSON_INSERT('{ "a" : "foo", "b" : [ 1, 2, 3 ] }', '$.c', 123);
# inserts a string, returns '{ "a" : "foo", "b" : [ 1, 2, 3 ], "c" : "123" }
SELECT JSON_INSERT('{ "a" : "foo", "b" : [ 1, 2, 3 ] }', '$.c', '123');
# returns '{ "a" : [ "foo", true ], "b" : [ 1, 2, 3 ] }'
SELECT JSON_INSERT('{ "a" : "foo", "b" : [ 1, 2, 3 ] }', '$.a[1]', true);
# should return { "a" : "foo", "b": true }
SELECT JSON_INSERT('{ "a" : "foo"}', '$.b', true, '$.b', false);
# examples from the wl7909 spec
# returns the original document because the path does exist
SELECT JSON_INSERT
(
'{ "a" : "foo", "b" : [ 1, 2, 3 ] }',
'$.a',
true
);
# inserts a number, returns '{"a": "foo", "b": [1, 2, 3], "c": 123}
SELECT JSON_INSERT
(
'{ "a" : "foo", "b" : [ 1, 2, 3 ] }',
'$.c',
123
);
# inserts a string, returns '{"a": "foo", "b": [1, 2, 3], "c": "123"}
SELECT JSON_INSERT
(
'{ "a" : "foo", "b" : [ 1, 2, 3 ] }',
'$.c',
'123'
);
# returns '{"a": ["foo", true], "b": [1, 2, 3]}'
SELECT JSON_INSERT
(
'{ "a" : "foo", "b" : [ 1, 2, 3 ] }',
'$.a[1]',
true
);
# returns {"a": "foo", "b": true}
SELECT JSON_INSERT
(
'{ "a" : "foo"}',
'$.b', true,
'$.b', false
);
--echo # ----------------------------------------------------------------------
--echo # Test of JSON_ARRAY_INSERT function.
--echo # ----------------------------------------------------------------------
# NULLs
select json_array_insert(NULL, '$.b[1]', 1);
select json_array_insert('[1,2,3]', NULL, 1);
#Enable after fix MDEV-31554
--disable_cursor_protocol
select json_array_insert('[1,2,3]', '$[3]', NULL);
--enable_cursor_protocol
# wrong # args
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_array_insert(NULL);
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_array_insert(NULL, NULL);
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_array_insert(NULL, NULL, NULL, NULL);
# path does not indicate a cell position
--echo error ER_INVALID_JSON_PATH_ARRAY_CELL
select json_array_insert('true', '$', 1);
--echo error ER_INVALID_JSON_PATH_ARRAY_CELL
select json_array_insert('true', '$.a', 1);
--echo error ER_INVALID_JSON_PATH_ARRAY_CELL
select json_array_insert('true', '$.a[1].b', 1);
#Enable after fix MDEV-31554
--disable_cursor_protocol
# nop if there is no array at the path's parent
select json_array_insert( 'true', '$[0]', false );
select json_array_insert( 'true', '$[1]', false );
select json_array_insert( '{ "a": true }', '$.a[0]', false );
select json_array_insert( '{ "a": true }', '$.a[1]', false );
# positive tests
select json_array_insert( '[]', '$[0]', false );
select json_array_insert( '[]', '$[1]', false );
select json_array_insert( '[true]', '$[0]', false );
select json_array_insert( '[true]', '$[1]', false );
select json_array_insert( '[true]', '$[2]', false );
select json_array_insert( '{ "a": [] }', '$.a[0]', false );
select json_array_insert( '{ "a": [] }', '$.a[1]', false );
select json_array_insert( '{ "a": [true] }', '$.a[0]', false );
select json_array_insert( '{ "a": [true] }', '$.a[1]', false );
select json_array_insert( '{ "a": [true] }', '$.a[2]', false );
# insert into the middle of an array
select json_array_insert( '[1, 2, 3, 4]', '$[0]', false );
select json_array_insert( '[1, 2, 3, 4]', '$[1]', false );
select json_array_insert( '[1, 2, 3, 4]', '$[2]', false );
select json_array_insert( '[1, 2, 3, 4]', '$[3]', false );
select json_array_insert( '[1, 2, 3, 4]', '$[4]', false );
select json_array_insert( '[1, 2, 3, 4]', '$[5]', false );
--enable_cursor_protocol
select json_array_insert( '{ "a": [1, 2, 3, 4] }', '$.a[0]', false );
select json_array_insert( '{ "a": [1, 2, 3, 4] }', '$.a[1]', false );
select json_array_insert( '{ "a": [1, 2, 3, 4] }', '$.a[2]', false );
select json_array_insert( '{ "a": [1, 2, 3, 4] }', '$.a[3]', false );
select json_array_insert( '{ "a": [1, 2, 3, 4] }', '$.a[4]', false );
select json_array_insert( '{ "a": [1, 2, 3, 4] }', '$.a[5]', false );
# nop
select json_array_insert( '{ "a": [1, 2, 3, 4] }', '$.b[0]', false );
select json_array_insert( '{ "a": [1, 2, 3, 4] }', '$.b[1]', false );
# no auto-wrapping
select json_array_insert( '"a"', '$[0]', true );
select json_array_insert( '[ "a" ]', '$[0][0]', true );
select json_array_insert( '"a"', '$[1]', true );
--echo error ER_INVALID_JSON_PATH_WILDCARD
select json_insert('[]', '$.a.*[1]', 6);
--echo error ER_INVALID_JSON_PATH_WILDCARD
select json_insert('[]', '$**[1]', 6);
--echo error ER_INVALID_JSON_PATH_WILDCARD
select json_insert('[]', '$[*][1]', 6);
# multiple paths,
#Enable after fix MDEV-31554
--disable_cursor_protocol
select json_array_insert( '[ 1, 2, 3 ]', '$[1]', true, '$[1]', false );
--enable_cursor_protocol
select json_array_insert( '[ 1, 2, 3 ]', '$[1]',
json_compact( '[ "a", "b", "c", "d" ]'), '$[1][2]', false );
# test an error while evaluating the document expression
--echo error ER_INVALID_JSON_TEXT_IN_PARAM
SELECT JSON_ARRAY_INSERT(JSON_EXTRACT('[1', '$'), '$[0]', 1);
# error in reading new value
--echo error ER_INVALID_JSON_TEXT_IN_PARAM
select json_array_insert( '[ 1, 2, 3 ]', '$[1]', json_extract( '[', '$' ) );
--echo # ----------------------------------------------------------------------
--echo # Test of JSON_SET function.
--echo # ----------------------------------------------------------------------
# NULLs
select json_set(NULL, '$.b', json_compact(1));
select json_set('[1,2,3]', NULL, json_compact(1));
select json_set('[1,2,3]', '$[3]', NULL);
# wrong # args
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_set(NULL);
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_set(NULL, NULL);
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_set(NULL, NULL, NULL, NULL);
# Enable after fix MDEV-31554
--disable_cursor_protocol
# Detect errors in nested function calls.
--echo error ER_INVALID_JSON_TEXT_IN_PARAM
SELECT JSON_SET('{}', '$.name', JSON_EXTRACT('', '$'));
--enable_cursor_protocol
# positive test cases
select json_set('[1,2,3]', '$[2]', 4);
select json_set('[1,2,3]', '$[3]', 4);
select json_set('[1,2,3]', '$[10]', 4);
select json_set('{"c":4}', '$.c', 5);
# Enable after fix MDEV-31554
--disable_cursor_protocol
select json_set('{"c":4}', '$.a', 5);
--enable_cursor_protocol
select json_set('1', '$', 4);
select json_set('1', '$[0]', 4);
select json_set('1', '$[1]', 4);
select json_set('1', '$[10]', '4', '$[11]', 5);
select json_set('[1,2,3]', '$[2][0]', 4);
select json_set('[1,2,3]', '$[2][2]', 4);
select json_set('{"a": 3}', '$.a[0]', 4);
select json_set('{"a": 3}', '$.a[1]', 4, '$.a[2]', '5');
# auto-wrap plus ellipsis with nested hits should give: {"a": [{"b": [3, 6]}, 6]}
--echo error ER_INVALID_JSON_PATH_WILDCARD
select json_set(json_compact('{"a": {"b": [3]}}'), '$**[1]', 6);
# Examples from the specification: Include when missing functions are
# available.
# returns { "a" : {}, "b" : [ 1, 2, 3 ] }
SELECT JSON_SET('{ "a" : "foo", "b" : [ 1, 2, 3 ] }',
'$.a',
JSON_OBJECT());
# # returns { "a" : "foo", "b" : [ 1, 2, 3 ], "c" : [ true, false ] }
# SELECT JSON_SET('{ "a" : "foo", "b" : [ 1, 2, 3 ] }',
# '$.c',
# JSON_ARRAY( true, false ));
# # returns { "a" : "foo", "b" : [ 1, 2, 3 ], "c" : [ true, false ] }
# SELECT JSON_SET('{ "a" : "foo", "b" : [ 1, 2, 3 ] }',
# '$.c',
# JSON_ARRAY( json_compact( 'true'), json_compact( 'false') ));
# # returns [ 1, null, null, 2 ]
# SELECT JSON_SET('1', '$[3]', 2);
# should return { "a": { "b": false, "c": true } }
SELECT JSON_SET('{ "a" : "foo"}', '$.a',
JSON_OBJECT( 'b', false ), '$.a.c', true);
# returns { "a" : {}, "b" : [ 1, 2, 3 ] }
select json_set('{ "a" : "foo", "b" : [ 1, 2, 3 ] }',
'$.a',
json_compact('{}'));
# Enable after fix MDEV-31554
--disable_cursor_protocol
# returns { "a" : "foo", "b" : [ 1, 2, 3 ], "c" : [ true, false ] }
select json_set('{ "a" : "foo", "b" : [ 1, 2, 3 ] }',
'$.c',
json_compact('[true, false]'));
--enable_cursor_protocol
# returns [ 1, null, null, 2 ]
select json_set('1', '$[3]', 2);
# should return { "a": { "b": false, "c": true } }
select json_set('{ "a" : "foo"}', '$.a',
json_compact('{"b": false}'), '$.a.c', true);
# examples from wl7909 spec
# returns {"a": {}, "b": [1, 2, 3]}
SELECT JSON_SET
(
'{ "a" : "foo", "b" : [ 1, 2, 3 ] }',
'$.a',
JSON_OBJECT()
);
# Enable after fix MDEV-31554
--disable_cursor_protocol
# returns {"a": "foo", "b": [1, 2, 3], "c": [true, false]}
SELECT JSON_SET
(
'{ "a" : "foo", "b" : [ 1, 2, 3 ] }',
'$.c',
JSON_ARRAY( true, false )
);
# returns {"a": "foo", "b": [1, 2, 3], "c": [true, false]}
SELECT JSON_SET
(
'{ "a" : "foo", "b" : [ 1, 2, 3 ] }',
'$.c',
JSON_ARRAY( json_compact( 'true'), json_compact( 'false') )
);
--enable_cursor_protocol
# returns [1, 2]
SELECT JSON_SET
(
'1',
'$[3]',
2
);
# returns {"a": {"b": false, "c": true}}
SELECT JSON_SET
(
'{ "a" : "foo"}',
'$.a', JSON_OBJECT( 'b', false ),
'$.a.c', true
);
--echo # ----------------------------------------------------------------------
--echo # Test of JSON_REPLACE function.
--echo # ----------------------------------------------------------------------
# NULLs
select json_replace(NULL, '$.b', json_compact(1));
select json_replace('[1,2,3]', NULL, json_compact(1));
select json_replace('[1,2,3]', '$[2]', NULL);
# wrong # args
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_replace(NULL);
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_replace(NULL, NULL);
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_replace(NULL, NULL, NULL, NULL);
# positive test cases
select json_replace('[1,2,3]', '$[2]', 4);
select json_replace('[1,2,3]', '$[3]', 4);
select json_replace('[1,2,3]', '$[10]', 4);
select json_replace('{"c":4}', '$.c', 5);
select json_replace('{"c":4}', '$.a', 5);
select json_replace('1', '$', 4);
select json_replace('1', '$[0]', 4);
select json_replace('1', '$[1]', 4);
select json_replace('1', '$[10]', '4', '$[11]', 5);
select json_replace('[1,2,3]', '$[2][0]', 4);
select json_replace('[1,2,3]', '$[2][2]', 4);
select json_replace('{"a": 3}', '$.a[0]', 4);
select json_replace('{"a": 3}', '$.a[1]', 4, '$.a[2]', '5');
# Examples from the specification
# returns the original document because the path doesn't exist
SELECT JSON_REPLACE('{ "a" : "foo", "b" : [ 1, 2, 3 ] }',
'$.c',
true);
# returns '{ "a" : true, "b" : [ 1, 2, 3 ] }'
SELECT JSON_REPLACE('{ "a" : "foo", "b" : [ 1, 2, 3 ] }',
'$.a[0]',
true);
# returns the original document because the path doesn't exist
SELECT JSON_REPLACE('{ "a" : "foo", "b" : [ 1, 2, 3 ] }',
'$.b[5]',
true);
--echo # ----------------------------------------------------------------------
--echo # Test of JSON_ARRAY function.
--echo # ----------------------------------------------------------------------
# NULLs
select json_array(NULL, '$.b', json_compact(1));
select json_array('[1,2,3]', NULL, json_compact(1));
select json_array('[1,2,3]', '$[3]', NULL);
# Enable after fix MDEV-31554
--disable_cursor_protocol
# positive test cases
select json_array();
select json_array(3.14);
select json_array('[1,2,3]');
select json_array(json_compact('[1,2,3]'));
select json_array(1,2,3);
select json_array(b'0', b'1', b'10');
# returns the empty array: []
SELECT JSON_ARRAY();
--enable_cursor_protocol
--echo # ----------------------------------------------------------------------
--echo # Test of JSON_OBJECT function.
--echo # ----------------------------------------------------------------------
# odd number of args
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_object( 'a' );
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_object( 'a', 1, 'b' );
# null arg
--echo error ER_JSON_DOCUMENT_NULL_KEY
select json_object( null, 1 );
# Enable after fix MDEV-31554
--disable_cursor_protocol
# positive tests
select json_object();
select json_object( 'a', null );
--enable_cursor_protocol
select json_object( 'a', 1 );
select json_object( 'a', 1, 'b', 'foo' );
select json_object( 'a', 1, 'b', 'foo', 'c', json_compact( '{ "d": "wibble" }') );
select json_object( 'a', true, 'b', false, 'c', json_compact( 'null') );
select json_valid( json_object( '"a"', 1 ) );
# long key
select json_object( REPEAT('a', 64 * 1024), 1 );
# non-string keyNames are cast to CHAR
select json_object(json_array(), json_array());
select json_object( cast(json_array() as char), json_array());
select json_object( 1, json_array());
select json_object( cast(1 as char), json_array());
# Enable after fix MDEV-31554
--disable_cursor_protocol
# returns the empty object: {}
SELECT JSON_OBJECT();
--enable_cursor_protocol
--echo # ----------------------------------------------------------------------
--echo # Test of JSON_SEARCH function.
--echo # ----------------------------------------------------------------------
# wrong number of args
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_search();
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_search( '{ "a": true }' );
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_search( '{ "a": true }', 'one' );
# null args
select json_search( null, 'one', 'foo' );
select json_search( '{ "a": "foo" }', null, 'foo' );
# FIXME. what should happen here?
#select json_search( '{ "a": "foo" }', 'one', null );
select json_search( '{ "a": "foo" }', 'one', 'foo', null, null );
select json_search( '{ "a": "foo" }', 'one', 'foo', null, '$.a', null );
# bad values for the oneOrAll arg
--echo error ER_JSON_BAD_ONE_OR_ALL_ARG
select json_search( '{ "a": "foo" }', 'twof', 'foo' );
--echo error ER_JSON_BAD_ONE_OR_ALL_ARG
select json_search( '{ "a": "foo" }', 'two', 'foo' );
# bad escape arg
--error ER_WRONG_ARGUMENTS
select json_search( '{ "a": "foo" }', 'one', 'foo', 'ab' );
# bad path args
--echo error ER_INVALID_JSON_PATH
select json_search( '{ "a": "foo" }', 'one', 'foo', null, '$a' );
--echo error ER_INVALID_JSON_PATH
select json_search( '{ "a": "foo" }', 'all', 'foo', null, '$.a', '$b' );
--error ER_BAD_FIELD_ERROR
select json_search(a, b, c);
# simple tests for search without path arguments
select json_search( '{ "a": "foobar" }', 'one', 'foo%' );
select json_search( '{ "a": "foobar", "b": "focus", "c": [ "arm", "foot", "shoulder" ] }', 'one', 'foo%' );
select json_search( '{ "a": "foobar", "b": "focus", "c": [ "arm", "foot", "shoulder" ] }', 'all', 'foo%' );
select json_search( '{ "a": "foobar", "b": "focus", "c": [ "arm", "foot", "shoulder" ] }', 'all', 'f__us' );
select json_search( '{ "a": [ "foolish", "folly", "foolhardy" ], "b" : "fool" }', 'all', 'foo%', null, '$.a' );
select json_search( '{ "a": [ "foolish", "folly", "foolhardy" ], "b" : "fool" }', 'all', 'foo%', null, '$.a', '$.b' );
select json_search( '{ "a": [ "foolish", "folly", "foolhardy" ], "b" : "fool" }', 'one', 'foo%', null, '$.a', '$.b' );
select json_search( '{ "a": [ "foolish", "folly", "foolhardy" ], "b" : "fool" }', 'ALL', 'foo%', null, '$.a' );
select json_search( '{ "a": [ "foolish", "folly", "foolhardy" ], "b" : "fool" }', 'aLl', 'foo%', null, '$.a', '$.b' );
select json_search( '{ "a": [ "foolish", "folly", "foolhardy" ], "b" : "fool" }', 'ONE', 'foo%', null, '$.a', '$.b' );
# wildcards in the path expression
select json_search
(
'[ { "a": { "b": { "c": "fool" } } }, { "b": { "c": "shoulder" } }, { "c": { "c": "food"} } ]',
'all',
'foo%',
null,
'$**.c'
);
select json_search
(
'[ { "a": { "b": { "c": "showtime" } } }, { "b": { "c": "shoulder" } }, { "c": { "c": "shoe"} } ]',
'all',
'sho%',
null,
'$**.c'
);
select json_search
(
'[ { "a": { "b": { "c": "showtime" } } }, { "b": { "c": "shoulder" } }, { "c": { "c": "shoe"} } ]',
'all',
'sho%e',
null,
'$**.c'
);
select json_search
(
'[ { "a": { "b": { "c": "showtime" } } }, { "b": { "c": "shoulder" } }, { "c": { "c": "shoe"} } ]',
'all',
'sho%',
null,
'$[*].c'
);
select json_search
(
'[ { "a": { "b": { "c": "showtime" } } }, [ { "b": { "c": "shout" } }, { "c": { "c": "shoe"} } ] ]',
'all',
'sho%',
null,
'$[1]**.c'
);
# escape character
select json_search( '[ "footbar", "foo%bar" ]', 'all', 'foo%bar' );
select json_search( '[ "footbar", "foo%bar" ]', 'all', 'foo\%bar' );
select json_search( '[ "footbar", "foo%bar" ]', 'all', 'foo|%bar', '|' );
select json_search( '[ "footbar", "foo%bar" ]', 'all', 'foo|%bar', '|', '$[0]' );
select json_search( '[ "footbar", "foo%bar" ]', 'all', 'foo|%bar', '|', '$[0]', '$[1]' );
select json_search( '[ "footbar", "foo%bar" ]', 'all', 'foo|%bar', '|', '$[0]', '$[1]', '$[2]' );
select json_search( '[ "footbar", "foo%bar" ]', 'all', 'foo\%bar', null );
select json_search( '[ "footbar", "foo%bar" ]', 'all', 'foo\%bar', null, '$[0]' );
select json_search( '[ "footbar", "foo%bar" ]', 'all', 'foo\%bar', null, '$[1]' );
select json_search( '[ "footbar", "foo%bar" ]', 'all', 'foo|%bar', '|', '$[0]' );
select json_search( '[ "footbar", "foo%bar" ]', 'all', 'foo|%bar', '|', '$[1]' );
# search is case-sensitive
select json_search( '[ "abc", "ABC" ]', 'all', 'aBc' );
select json_search( '[ "abc", "ABC" ]', 'all', 'abc' );
select json_search( '[ "abc", "ABC" ]', 'all', 'ABC' );
# only matches strings, not numerics
select json_search( '[ 10, "10", 1.0, "1.0" ]', 'all', '1%' );
# examples from the wl7909 spec
# returns null because numeric values don't match string values
SELECT JSON_SEARCH
(
'{ "a" : 123, "b" : [ 123, 456 ] }',
'one',
'123'
);
# returns "$.b[2]"
SELECT JSON_SEARCH
(
'{ "a" : "123", "b" : [ 123, "789", "123", "456", "123" ] }',
'one',
'123',
null,
'$.b'
);
# could return either "$.a" or "$.b.key"
SELECT JSON_SEARCH
(
'{ "a" : "123", "b" : { "key" : "123" } }',
'one',
'123'
);
# returns "$.b.key"
SELECT JSON_SEARCH
(
'{ "a" : "1243", "b" : { "key" : "1234" } }',
'one',
'123%'
);
# returns "$.b.c"
SELECT JSON_SEARCH
(
'{ "a" : "1243", "b" : { "key" : "1234", "c": "directorysub%directoryabc" } }',
'one',
'dir%torysub@%dir%',
'@'
);
# returns null because the path doesn't exist
SELECT JSON_SEARCH
(
'{ "a" : "1243", "b" : { "key" : "1234" } }',
'one',
'123%',
null,
'$.c'
);
# returns $."one potato"
SELECT JSON_UNQUOTE
(
JSON_SEARCH
(
'{ "onepotato": "foot", "one potato": "food" , "one \\"potato": "fool" }',
'all',
'food'
)
);
select json_type(case (null is null) when 1 then
json_compact('null') else
json_compact('[1,2,3]') end);
# Enable after fix MDEV-31554
--disable_cursor_protocol
select json_type(case (null is not null) when 1 then
json_compact('null') else
json_compact('[1,2,3]') end);
--enable_cursor_protocol
select json_type( if(null is null,
json_compact('null'),
json_compact('[1,2,3]')) );
# Enable after fix MDEV-31554
--disable_cursor_protocol
select json_type( if(null is not null,
json_compact('null'),
json_compact('[1,2,3]')));
--enable_cursor_protocol
select cast(json_extract(json_compact(concat('[', json_compact('["A",2]'), ']')),
'$[0][1]') as char) = 2;
--echo # ----------------------------------------------------------------------
--echo # Test of aggregate function MAX, MIN.
--echo # ----------------------------------------------------------------------
select max(json_compact('[1,2,3]'));
--echo # ----------------------------------------------------------------------
--echo # Test of JSON_QUOTE, JSON_UNQUOTE
--echo # ----------------------------------------------------------------------
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_quote();
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_quote('abc', 'def');
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_quote(NULL, 'def');
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_quote('abc', NULL);
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_unquote();
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_unquote('"abc"', '"def"');
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_unquote(NULL, 'def');
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_unquote('"abc"', NULL);
select json_quote(NULL);
select json_unquote(NULL);
select json_quote('abc');
select json_quote(convert('abc' using ascii));
select json_quote(convert('abc' using latin1));
select json_quote(convert('abc' using utf8));
select json_quote(convert('abc' using utf8mb4));
select json_unquote('abc'); # should do nothing
select json_unquote('"abc"');
# Enable after fix MDEV-31554
--disable_cursor_protocol
select json_unquote(convert('"abc"' using ascii));
select json_unquote(convert('"abc"' using latin1));
--enable_cursor_protocol
select json_unquote(convert('"abc"' using utf8));
select json_unquote(convert('"abc"' using utf8mb4));
select json_quote('"');
select json_unquote('"'); # should do nothing
--echo error ER_INCORRECT_TYPE
select json_quote(123); # integer not allowed
# Enable after fix MDEV-31554
--disable_cursor_protocol
--echo error ER_INCORRECT_TYPE
select json_unquote(123); # integer not allowed
--enable_cursor_protocol
select json_unquote('""'); # empty string
select char_length(json_unquote('""')); # verify empty string
select json_unquote('"" '); # unchanged: no final "
select json_unquote(json_compact(json_quote('abc'))); # round trip
# No change in this JSON string: it is an object
select json_compact('{"abc": "foo"}');
select json_unquote(json_compact('{"abc": "foo"}'));
# This is a JSON string, so it is actually unquoted
select json_extract(json_compact('{"abc": "foo"}'), '$.abc');
select json_unquote(json_extract(json_compact('{"abc": "foo"}'), '$.abc'));
# Bug fix: thse should be the same
select json_unquote('["a", "b", "c"]');
select json_unquote(json_compact('["a", "b", "c"]'));
select charset(json_unquote('"abc"'));
select json_quote(convert(X'e68891' using utf8)); # chinese "I" (wo3)
select json_quote(convert(X'e68891' using utf8mb4)); # chinese "I" (wo3)
select json_compact(json_quote(convert(X'e68891' using utf8)));
select json_unquote(convert(X'e68891' using utf8)); # chinese "I" (wo3)
select json_quote(json_quote(json_quote('abc'))); # deep quote
select json_unquote(json_unquote(json_unquote( # long round trip of it
json_quote(json_quote(json_quote('abc'))))));
# DATE/TIME will lose their quotes, too:
select json_compact(cast('2015-01-15 23:24:25' as datetime));
# Enable after fix MDEV-31554
--disable_cursor_protocol
select json_unquote(json_compact(cast('2015-01-15 23:24:25' as datetime)));
--enable_cursor_protocol
# as well as opaque values:
select json_compact(st_geomfromtext('point(1 1)'));
# Enable after fix MDEV-31554
--disable_cursor_protocol
select json_unquote(json_compact(st_geomfromtext('point(1 1)')));
--enable_cursor_protocol
# examples from the wl7909 spec
# returns the SQL string literal abc
SELECT JSON_UNQUOTE( '"abc"' );
# Enable after fix MDEV-31554
--disable_cursor_protocol
# returns the SQL string literal "abc
SELECT JSON_UNQUOTE( '"abc' );
--echo error ER_INCORRECT_TYPE
SELECT JSON_UNQUOTE( 123 );
--enable_cursor_protocol
# returns the SQL string literal abc
SELECT JSON_UNQUOTE
( CAST( json_compact( '"abc"') AS CHAR ) );
# returns 1
SELECT JSON_UNQUOTE
(
CAST(
JSON_EXTRACT( '{ "userName" : "fred" }', '$.userName' )
AS CHAR
)
) = 'fred';
# returns 0
SELECT
CAST(
JSON_EXTRACT( '{ "userName" : "fred" }', '$.userName' )
AS CHAR
) = 'fred';
# returns "abc"
SELECT JSON_QUOTE( 'abc' );
--echo error ER_INCORRECT_TYPE
SELECT JSON_QUOTE( 123 );
# returns the JSON document consisting of the string scalar "123"
SELECT json_compact( JSON_QUOTE( '123' ));
--echo # ----------------------------------------------------------------------
--echo # Test of JSON_CONTAINS
--echo # ----------------------------------------------------------------------
--echo # should give NULL
select json_contains(NULL, NULL);
select json_contains(json_compact('{"a": 1, "b": 2}'), NULL);
select json_contains(NULL, json_compact('null'));
select json_contains(json_compact('[1]'), json_compact('[1]'), NULL);
--echo # should give 0:
select json_contains(json_compact(3.14), json_compact(3));
--echo # should give 0: not at top level
select json_contains(json_compact('{"a": {"b": 7}}'), json_compact('{"b": 7}'));
--echo # but path argument will fix it:
select json_contains(json_compact('{"a": {"b": 7}}'), json_compact('{"b": 7}'), '$.a');
--echo # but arrays "introspect"
select json_contains(json_compact('[1,[2.0, 3.0]]'), json_compact('[2.0]'));
select json_contains(json_compact('[1, 2, [3, [4, 5]], 6, 7]'), json_compact('5'));
--echo # should give 0: just a key
select json_contains(json_compact('{"a": 1, "b": 2}'), json_compact('"a"'));
--echo # should give 0: one candidate element doesn't match
select json_contains(json_compact('[1]'), json_compact('[1,2]'));
--echo # should all give 1
select json_contains(json_compact('null'), json_compact('null'));
--echo # simple object subset
select json_contains(json_compact('{"a": 1, "b": 2}'), json_compact( '{"a": 1}'));
--echo # simple vector subset
select json_contains(json_compact('[1, 2, 3]'), json_compact('[1, 3]'));
--echo # auto-wrap, should give 1
select json_contains(json_compact('[1, 2, 3]'), json_compact(3));
--echo # ok even with nested cast off elements
select json_contains(json_compact('{"person": {"id": 1, "country": "norway"}}'),
json_compact('{"person": {"country": "norway"}}'));
--echo # vector reordering and duplicates is ok
select json_contains(json_compact('[1,3,5]'), json_compact('[5,3,1,5]'));
--echo # ok even with more elts in candidate than in doc
select json_contains(json_compact('[{"b": 4, "a":7}]'), json_compact('[{"a":7},{"b":4}]'));
select json_contains(json_compact('[{"b": 4, "a":7}, 5]'), json_compact('[5, {"a":7, "b":4}]'));
--echo # ok even with mixed number types that compare equal
select json_contains(json_compact('[{"b": 4, "a":7}, 5.0]'), json_compact('[5, {"a":7.0E0, "b":4}]'));
# Bug discovered by Rick: used to give 1 (true).
select json_contains( '{"customer": "cust3"}', '{"customer": "cust1"}' );
SELECT JSON_CONTAINS('[null,1,[2,3],true,false]', '[null,1,[3],false]');
SELECT JSON_CONTAINS('[null,1,[2,3],true,false]', '[null,1,[4],false]');
SELECT JSON_CONTAINS('[true,false]', '[[true]]');
SELECT JSON_CONTAINS('[1,2]', '[[1]]');
SELECT JSON_CONTAINS('[1,2]', '1', '$.abc');
SELECT JSON_CONTAINS('{}', '{}');
SELECT JSON_CONTAINS('{}', '{"a":1}');
SELECT JSON_CONTAINS('{"a":1}', '{"a":1,"b":2}');
# examples from the wl7909 spec
# returns 1
SELECT JSON_CONTAINS
(
json_compact('[1, 4, 6]'),
json_compact('[1, 6]')
);
# returns 1; even with nested cast off elements
SELECT JSON_CONTAINS
(
json_compact('{"person": {"id": 1, "country": "norway"}}'),
json_compact('{"person": {"country": "norway"}}')
);
# returns 1; reordering and duplicates are ok
SELECT JSON_CONTAINS
(
json_compact('[1,3,5]'),
json_compact('[5,3,1,5]')
);
# return 0; no type conversion is performed
SELECT JSON_CONTAINS
(
json_compact('[3.14]'),
json_compact('[3]')
);
# returns 1, due to auto-wrapping
SELECT JSON_CONTAINS
(
json_compact('[1, 2, 3]'),
json_compact(3)
);
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
SELECT JSON_CONTAINS();
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
SELECT JSON_CONTAINS('[1]');
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
SELECT JSON_CONTAINS('[1]', '[1]', '$', '$[0]');
--echo # ----------------------------------------------------------------------
--echo # Wrong collation from JSON_QUOTE caused problems: Set it in
--echo # Item_func_json_quote::fix_length_and_dec. Bug found by Knut.
--echo # Similar issue for JSON_UNQUOTE and JSON_TYPE.
--echo # ----------------------------------------------------------------------
select json_object("a", ifnull(json_quote('test'), json_compact('null')));
select json_compact(concat('[', json_quote('ab'), ']'));
select json_compact(concat('[', json_unquote('"12"'), ']'));
# Enable after fix MDEV-31554
--disable_cursor_protocol
select json_compact(concat('["', json_type( json_compact(1)), '"]'));
--enable_cursor_protocol
--echo #
--echo # Bug#20912438: ITEM_TYPE_HOLDER::DISPLAY_LENGTH(ITEM*): ASSERTION `0' FAILED
--echo #
(SELECT JSON_KEYS('{ "key80": "2015-04-20 11:53:55"}')) UNION ALL
(SELECT JSON_KEYS('{ "key80": "2015-04-20 11:53:55" }') LIMIT 0);
SELECT json_compact(1) UNION ALL SELECT json_compact(1);
# Exercise NULL handling and error handling in Item_copy_json::copy().
SELECT COUNT(*), json_compact(NULL);
--echo error ER_INVALID_JSON_TEXT_IN_PARAM
SELECT COUNT(*), JSON_EXTRACT('not valid json!', '$');
--echo # ----------------------------------------------------------------------
--echo # Bug report from John E.
--echo # Crash in Item_copy_json::~Item_copy_json
--echo # ----------------------------------------------------------------------
EXPLAIN SELECT COUNT(*), JSON_KEYS('{}');
select json_search( '{ "a": "foo" }', 'one', 'foo', 'a' );
select json_search( '{ "a": "foo" }', 'one', 'foo', null );
select json_search( '{ "a": "foo" }', 'one', 'foo', convert(x'f8' using latin1) );
# bad escape arg
--error ER_WRONG_ARGUMENTS
select json_search( '{ "a": "foo" }', 'one', 'foo', 'ab' );
--echo # ----------------------------------------------------------------------
--echo # Wrong results when Json_path_cache primed is accessed
--echo # during the prepare-phase.
--echo #----------------------------------------------------------------------
select json_remove( '[ 1, { "a": true, "b": false, "c": null }, 5 ]', null ) is null;
prepare stmt1 from 'select json_remove( ''[ 1, { "a": true, "b": false, "c": null }, 5 ]'', null ) is null';
execute stmt1;
--echo error ER_INVALID_JSON_PATH
select json_remove( '[ 1, { "a": true, "b": false, "c": null }, 5 ]', '$.' ) is null;
--echo error ER_INVALID_JSON_PATH
prepare stmt1 from 'select json_remove( ''[ 1, { "a": true, "b": false, "c": null }, 5 ]'', ''$.'' ) is null';
--echo # ----------------------------------------------------------------------
--echo # Bug#20987329 VALUE OF PREPARED STATEMENT PLACEHOLDER FOR PARAMETER
--echo # IN JSON_EXTRACT IS STICKY
--echo #----------------------------------------------------------------------
# should get different results with different parameter values
# json_extract()
# json_contains()
prepare json_stmt1 FROM 'select json_contains( ''{ "keyA": [1, 2, 3], "keyB": [4, 5, 6] }'', ''[2]'', ? )';
set @mypath = '$.keyA';
execute json_stmt1 USING @mypath;
set @mypath = '$.keyB';
execute json_stmt1 USING @mypath;
# json_contains_path()
prepare json_stmt2 FROM 'select json_contains_path( ''{ "keyA": [1, 2, 3] }'', ''all'', ? )';
set @mypath = '$.keyA';
execute json_stmt2 USING @mypath;
set @mypath = '$.keyB';
execute json_stmt2 USING @mypath;
# json_length()
prepare json_stmt3 FROM 'select json_length( ''{ "keyA": [1, 2, 3], "keyB": [1, 2, 3, 4] }'', ? )';
set @mypath = '$.keyA';
execute json_stmt3 USING @mypath;
set @mypath = '$.keyB';
execute json_stmt3 USING @mypath;
# json_keys()
prepare json_stmt4 FROM 'select json_keys( ''[ { "keyA": true }, { "keyB": false } ]'', ? )';
set @mypath = '$[0]';
execute json_stmt4 USING @mypath;
set @mypath = '$[1]';
execute json_stmt4 USING @mypath;
# json_array_append()
prepare json_stmt5 FROM 'select json_array_append( ''{ "keyA": [1, 2], "keyB": [3, 4] }'', ?, 5 )';
set @mypath = '$.keyA';
execute json_stmt5 USING @mypath;
set @mypath = '$.keyB';
execute json_stmt5 USING @mypath;
# json_insert()
prepare json_stmt6 FROM 'select json_insert( ''{ "keyA": [1, 2], "keyB": [3, 4] }'', ?, 5 )';
set @mypath = '$.keyA[2]';
execute json_stmt6 USING @mypath;
set @mypath = '$.keyB[2]';
execute json_stmt6 USING @mypath;
# json_set()
prepare json_stmt7 FROM 'select json_set( ''{ "keyA": [1, 2], "keyB": [3, 4] }'', ?, 5 )';
set @mypath = '$.keyA[2]';
execute json_stmt7 USING @mypath;
set @mypath = '$.keyB[2]';
execute json_stmt7 USING @mypath;
# json_replace()
prepare json_stmt8 FROM 'select json_replace( ''{ "keyA": [1, 2], "keyB": [3, 4] }'', ?, 5 )';
set @mypath = '$.keyA[1]';
execute json_stmt8 USING @mypath;
set @mypath = '$.keyB[1]';
execute json_stmt8 USING @mypath;
# json_search()
prepare json_stmt9 FROM 'select json_search( ''{ "keyA": [ "foot" ], "keyB": [ "food" ] }'', ''all'', ''foo%'', null, ? )';
set @mypath = '$.keyA';
execute json_stmt9 USING @mypath;
set @mypath = '$.keyB';
execute json_stmt9 USING @mypath;
# json_remove()
prepare json_stmt10 FROM 'select json_remove( ''{ "keyA": [ "foot" ], "keyB": [ "food" ] }'', ? )';
set @mypath = '$.keyA';
execute json_stmt10 USING @mypath;
set @mypath = '$.keyB';
execute json_stmt10 USING @mypath;
# similar caching problem for the oneOrAll args
prepare json_stmt11 FROM 'select json_contains_path( ''{ "keyA": true }'', ?, ''$.keyA'', ''$.keyB'' )';
set @mypath = 'one';
execute json_stmt11 USING @mypath;
set @mypath = 'all';
execute json_stmt11 USING @mypath;
prepare json_stmt12 FROM 'select json_search( ''{ "keyA": [ "foot" ], "keyB": [ "food" ] }'', ?, ''foo%'' )';
set @mypath = 'one';
execute json_stmt12 USING @mypath;
set @mypath = 'all';
execute json_stmt12 USING @mypath;
--echo #
--echo # Bug#21128632 JSON_QUOTE(JSON_TYPE(...)) GIVES ERROR 3139 ER_INVALID_JSON_CHARSET
--echo #
select json_quote( json_type( json_object() ) );
select json_quote( json_type( json_compact('{}') ) );
--echo #
--echo # Bug#21148020 OUTPUT FROM JSON_TYPE() IS TRUNCATED
--echo # WHEN EXECUTED IN A VIEW OR JOIN
--echo #
# Enable after fix MDEV-31554
--disable_cursor_protocol
SELECT JSON_TYPE(JSON_OBJECT());
--enable_cursor_protocol
CREATE VIEW v1 AS SELECT JSON_TYPE(JSON_OBJECT());
SELECT * FROM v1;
drop view v1;
# SELECT JSON_TYPE(json_compact(CAST('2015-05-25 11:23:55' AS DATETIME)));
# CREATE VIEW v2 AS SELECT JSON_TYPE(json_compact(CAST('2015-05-25 11:23:55' AS
# DATETIME)));
# SELECT * FROM v2;
# drop view v2;
--echo #
--echo # Bug#21198333 SIG 6 IN ITEM_CACHE_JSON::CACHE_VALUE AT SQL/ITEM.CC:9470
--echo #
--echo error ER_INVALID_JSON_TEXT_IN_PARAM
SELECT MIN(JSON_EXTRACT('not json', '$'));
--echo #
--echo # Bug#21200657 DATA FROM DERIVED TABLE BASED
--echo # ON JSN_QUOTE()/JSN_UNQUOTE() CALL IS TRUNCATED
--echo #
SELECT JSON_QUOTE('This is a string that should not be truncated') AS field1;
SELECT JSON_UNQUOTE(JSON_QUOTE('This is a string that should not be truncated')) AS field1;
SELECT * FROM (SELECT JSON_QUOTE('This is a string that should not be truncated') AS field1) AS DERIVED_TABLE;
SELECT * FROM (SELECT JSON_UNQUOTE("This is a string that should not be truncated") AS field1) AS DERIVED_TABLE;
SELECT * FROM (SELECT JSON_UNQUOTE(JSON_QUOTE('This is a string that should not be truncated')) AS field1) AS DERIVED_TABLE;
--echo #
--echo # Bug#21296173 JSON_OBJECT('KEY', BOOLEAN_LITERAL) USES VALUES 0, 1
--echo # FOR BOOL WHEN USED VIA VIEW
--echo #
# Enable after fix MDEV-31554
--disable_cursor_protocol
SELECT JSON_OBJECT('key1', false, 'key2', true);
SELECT JSON_ARRAY('key1', false, 'key2', true);
--enable_cursor_protocol
CREATE VIEW v1 AS SELECT JSON_OBJECT('key1', false, 'key2', true);
SELECT * FROM v1;
CREATE VIEW v2 AS SELECT JSON_ARRAY('key1', false, 'key2', true);
SELECT * FROM v2;
drop view v1;
drop view v2;
--echo #
--echo # Bug#21293089 JSON_CONTAINS() RETURNS WRONG RESULT WITH EMPTY JSON ARRAY
--echo #
SELECT JSON_CONTAINS('[]', '{"a" : 1}');
SELECT JSON_CONTAINS('[]', '[1, 2, 3, 4, 5]');
SELECT JSON_CONTAINS('[]', '[1, 2, 3, 4, {"a" : 1}]');
SELECT JSON_CONTAINS('[]', '{"a" : [1, 2, 3, 4, 5]}');
SELECT JSON_CONTAINS('[]', '[]');
--echo #
--echo # Bug#21377136 STACK OVERFLOW IN RAPIDJSON::GENERICREADER
--echo #
--echo error ER_JSON_DOCUMENT_TOO_DEEP
SELECT JSON_VALID(REPEAT('[', 100000));
--echo error ER_JSON_DOCUMENT_TOO_DEEP
SELECT JSON_VALID(REPEAT('{"a":', 100000));
--echo error ER_JSON_DOCUMENT_TOO_DEEP
SELECT JSON_VALID(REPEAT('{"a":[', 100000));
--echo error ER_JSON_DOCUMENT_TOO_DEEP
SELECT JSON_VALID(REPEAT('[{"a":', 100000));
--echo #
--echo # Bug#21381806 JSON: ASSERTION FAILED: ARG->NULL_VALUE
--echo #
SELECT JSON_SET(CASE WHEN 1 THEN NULL ELSE NULL END, '{}', '{}');
SELECT JSON_VALID(CASE WHEN 1 THEN NULL ELSE NULL END);
SELECT JSON_ARRAY(CASE WHEN 1 THEN NULL ELSE NULL END);
--echo #
--echo # Bug#21384048 ASSERTION FAILED: N >= 0 && N <= 308
--echo # IN RAPIDJSON::INTERNAL::FASTPATH
--echo #
SELECT JSON_EXTRACT('-1E-36181012216111515851075235238', '$');
SELECT JSON_EXTRACT('1E-36181012216111515851075235238', '$');
SELECT JSON_EXTRACT('1E-325', '$');
SELECT JSON_EXTRACT('1E-324', '$');
SELECT JSON_EXTRACT('1E-323', '$');
SELECT JSON_EXTRACT('1E+308', '$');
--echo error ER_INVALID_JSON_TEXT_IN_PARAM
SELECT JSON_EXTRACT('1E+309', '$');
--echo error ER_INVALID_JSON_TEXT_IN_PARAM
SELECT JSON_EXTRACT('1E+36181012216111515851075235238', '$');
--echo error ER_INVALID_JSON_TEXT_IN_PARAM
SELECT JSON_EXTRACT('-1E+36181012216111515851075235238', '$');
--echo #
--echo # Bug#21383284: ASSERTION IN SELECT_LEX::SETUP_CONDS
--echo #
--error ER_WRONG_ARGUMENTS
SELECT 1 FROM dual WHERE JSON_SEARCH('{}', 'one', 'foo', 'too-long-escape');
--echo error ER_INVALID_JSON_TEXT_IN_PARAM
SELECT 1 FROM dual WHERE JSON_SEARCH('{}', 'one', 'foo', JSON_EXTRACT('', '$'));
--echo #
--echo # Bug#21442624 INCORRECT RESULT FROM JSON_SET WITH AUTO-WRAPPING
--echo #
SELECT JSON_SET('1', '$', 100);
SELECT JSON_SET('1', '$[0]', 100);
SELECT JSON_SET('1', '$[0][0]', 100);
SELECT JSON_SET('1', '$[0][0][0]', 100);
SELECT JSON_SET('[]', '$', 100);
SELECT JSON_SET('[]', '$[0]', 100);
SELECT JSON_SET('[]', '$[0][0]', 100);
SELECT JSON_SET('[]', '$[0][0][0]', 100);
SELECT JSON_SET('[1]', '$', 100);
SELECT JSON_SET('[1]', '$[0]', 100);
SELECT JSON_SET('[1]', '$[0][0]', 100);
SELECT JSON_SET('[1]', '$[0][0][0]', 100);
SELECT JSON_SET('[[1]]', '$', 100);
SELECT JSON_SET('[[1]]', '$[0]', 100);
SELECT JSON_SET('[[1]]', '$[0][0]', 100);
SELECT JSON_SET('[[1]]', '$[0][0][0]', 100);
SELECT JSON_SET('[[[1]]]', '$', 100);
SELECT JSON_SET('[[[1]]]', '$[0]', 100);
SELECT JSON_SET('[[[1]]]', '$[0][0]', 100);
SELECT JSON_SET('[[[1]]]', '$[0][0][0]', 100);
SELECT JSON_REPLACE('1', '$', 100);
SELECT JSON_REPLACE('1', '$[0]', 100);
SELECT JSON_REPLACE('1', '$[0][0]', 100);
SELECT JSON_REPLACE('1', '$[0][0][0]', 100);
SELECT JSON_REPLACE('[]', '$', 100);
SELECT JSON_REPLACE('[]', '$[0]', 100);
SELECT JSON_REPLACE('[]', '$[0][0]', 100);
SELECT JSON_REPLACE('[]', '$[0][0][0]', 100);
SELECT JSON_REPLACE('[1]', '$', 100);
SELECT JSON_REPLACE('[1]', '$[0]', 100);
SELECT JSON_REPLACE('[1]', '$[0][0]', 100);
SELECT JSON_REPLACE('[1]', '$[0][0][0]', 100);
SELECT JSON_REPLACE('[[1]]', '$', 100);
SELECT JSON_REPLACE('[[1]]', '$[0]', 100);
SELECT JSON_REPLACE('[[1]]', '$[0][0]', 100);
SELECT JSON_REPLACE('[[1]]', '$[0][0][0]', 100);
SELECT JSON_REPLACE('[[[1]]]', '$', 100);
SELECT JSON_REPLACE('[[[1]]]', '$[0]', 100);
SELECT JSON_REPLACE('[[[1]]]', '$[0][0]', 100);
SELECT JSON_REPLACE('[[[1]]]', '$[0][0][0]', 100);
--echo #
--echo # Bug#21828321: JSON FUNCS CALL DBUG_ABORT OR EXIT() ON WINDOWS!
--echo #
# LEAST and GREATEST treat JSON arguments as strings for now. They used to hit
# an assertion if used in a JSON context and all arguments were JSON values, or
# a mix of NULLs and JSON values.
# Enable after fix MDEV-31554
--disable_cursor_protocol
SELECT JSON_ARRAY(LEAST(NULL, NULL), GREATEST(NULL, NULL), LEAST(j1, NULL),
GREATEST(NULL, j2), LEAST(j1, j2), GREATEST(j1, j2)) AS j
FROM (SELECT json_compact('1') AS j1, json_compact('2') AS j2) t;
--enable_cursor_protocol
--echo #
--echo # Start of 11.4 tests
--echo #
--echo #
--echo # MDEV-25829 Change default collation to utf8mb4_1400_ai_ci
--echo #
SELECT
JSON_UNQUOTE("String"),
COERCIBILITY(JSON_UNQUOTE("String")),
COLLATION(JSON_UNQUOTE("String"));
--echo #
--echo # End of 11.4 tests
--echo #
|