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
|
select json_valid('[1, 2]');
select json_valid('"string"}');
select json_valid('{"key1":1, "key2":[2,3]}');
select json_valid('[false, true, null]');
select json_valid(repeat('[', 1000));
select json_valid(repeat('{"a":', 1000));
select json_value('{"key1":123}', '$.key2');
select json_value('{"key1":123}', '$.key1');
select json_value('{"key1":[1,2,3]}', '$.key1');
select json_value('{"key1": [1,2,3], "key1":123}', '$.key1');
select JSON_VALUE('{ "x": [0,1], "y": "[0,1]", "z": "Mon\\\"t\\\"y" }','$.z') as exp;
select json_query('{"key1":{"a":1, "b":[1,2]}}', '$.key2');
select json_query('{"key1":{"a":1, "b":[1,2]}}', '$.key1');
select json_query('{"key1": 1}', '$.key1');
select json_query('{"key1":123, "key1": [1,2,3]}', '$.key1');
select json_query('{"key1":123, "key1": [1,2,3]}', concat('$', repeat('.k', 1000))) as exp;
select json_array();
select json_array(1);
#enable after fix MDEV-28649
--disable_view_protocol
select json_array(1, "text", false, null);
select json_array_append('["a", "b"]', '$', FALSE);
--enable_view_protocol
select json_array_append('{"k1":1, "k2":["a", "b"]}', '$.k2', 2);
select json_array_append('["a", ["b", "c"], "d"]', '$[0]', 2);
select json_array_insert('["a", {"b": [1, 2]}, [3, 4]]', '$[1]', 'x');
select json_array_insert('["a", {"b": [1, 2]}, [3, 4]]', '$[2]', 'x');
select json_array_insert('["a", {"b": [1, 2]}, [3, 4]]', '$[3]', 'x');
select json_array_insert('["a", {"b": [1, 2]}, [3, 4]]', '$[4]', 'x');
select json_array_insert('["a", {"b": [1, 2]}, [3, 4]]', '$[1].b[0]', 'x') as exp;
select json_array_insert('true', '$', 1);
select json_array_insert('["a", {"b": [1, 2]}, [3, 4]]', '$[2][1]', 'y') as exp;
select json_contains('{"k1":123, "k2":345}', '123', '$.k1');
select json_contains('"you"', '"you"');
select json_contains('"youth"', '"you"');
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_contains('[1]', '[1]', '$', '$[0]');
select json_contains('', '', '$');
select json_contains('null', 'null', '$');
select json_contains('"10"', '"10"', '$');
select json_contains('"10"', '10', '$');
select json_contains('10.1', '10', '$');
select json_contains('10.0', '10', '$');
select json_contains('[1]', '1');
select json_contains('[2, 1]', '1');
select json_contains('[2, [2, 3], 1]', '1');
select json_contains('[4, [2, 3], 1]', '2');
select json_contains('[2, 1]', '[1, 2]');
select json_contains('[2, 1]', '[1, 0, 2]');
select json_contains('[2, 0, 3, 1]', '[1, 2]');
select json_contains('{"b":[1,2], "a":1}', '{"a":1, "b":2}');
select json_contains('{"a":1}', '{}');
select json_contains('[1, {"a":1}]', '{}');
select json_contains('[1, {"a":1}]', '{"a":1}');
select json_contains('[{"abc":"def", "def":"abc"}]', '["foo","bar"]');
select json_contains('[{"abc":"def", "def":"abc"}, "bar"]', '["bar", {}]') as exp;
select json_contains('[{"a":"b"},{"c":"d"}]','{"c":"d"}');
select json_contains_path('{"key1":1, "key2":[2,3]}', "oNE", "$.key2[1]") as exp;
select json_contains_path('{"key1":1, "key2":[2,3]}', "oNE", "$.key2[10]") as exp;
select json_contains_path('{"key1":1, "key2":[2,3]}', "oNE", "$.ma") as exp;
select json_contains_path('{"key1":1, "key2":[2,3]}', "one", "$.key1") as exp;
select json_contains_path('{"key1":1, "key2":[2,3]}', "one", "$.key1", "$.ma") as exp;
select json_contains_path('{"key1":1, "key2":[2,3]}', "aLl", "$.key1", "$.ma") as exp;
select json_contains_path('{"key1":1, "key2":[2,3]}', "aLl", "$.key1", "$.key2") as exp;
select json_contains_path('{ "a": true }', NULL, '$.a' ) as exp;
select json_contains_path('{ "a": true }', 'all', NULL ) as exp;
select json_contains_path('{"a":{"b":"c"}}', 'one', '$.a.*') as exp;
select json_extract('{"key1":"asd", "key2":[2,3]}', "$.key1") as exp;
select json_extract('{"key1":"asd", "key2":[2,3]}', "$.keyX", "$.keyY") as exp;
select json_extract('{"key1":"asd", "key2":[2,3]}', "$.key1", "$.key2") as exp;
select json_extract('{"key1":5, "key2":[2,3]}', "$.key1", "$.key2") as exp;
select json_extract('{"key0":true, "key1":"qwe"}', "$.key1") as exp;
select json_extract(json_object('foo', 'foobar'),'$') as exp;
select json_extract('[10, 20, [30, 40]]', '$[2][*]') as exp;
select json_extract('[10, 20, [{"a":3}, 30, 40]]', '$[2][*]') as exp;
select json_extract('1', '$') as exp;
select json_extract('[10, 20, [30, 40], 1, 10]', '$[1]') as exp;
select json_extract('[10, 20, [30, 40], 1, 10]', '$[1]', '$[25]') as exp;
select json_extract( '[{"a": [3, 4]}, {"b": 2}]', '$[0].a', '$[1].a') as exp;
#enable after MDEV-32454 fix
--disable_view_protocol
select json_insert('{"a":1, "b":{"c":1}, "d":[1, 2]}', '$.b.k1', 'word') as exp;
select json_insert('{"a":1, "b":{"c":1}, "d":[1, 2]}', '$.d[3]', 3) as exp;
select json_insert('{"a":1, "b":{"c":1}, "d":[1, 2]}', '$.a[2]', 2) as exp;
select json_insert('{"a":1, "b":{"c":1}, "d":[1, 2]}', '$.b.c', 'word') as exp;
select json_set('{ "a": 1, "b": [2, 3]}', '$.a', 10, '$.c', '[true, false]') as exp;
--enable_view_protocol
select json_replace('{ "a": 1, "b": [2, 3]}', '$.a', 10, '$.c', '[true, false]') as exp;
select json_replace('{ "a": 1, "b": [2, 3]}', '$.a', 10, '$.b', '[true, false]') as exp;
set @j = '["a", ["b", "c"], "d"]';
select json_remove(@j, '$[0]');
select json_remove(@j, '$[1]');
select json_remove(@j, '$[2]');
set @j = '{"a": 1, "b": [2, 3]}';
select json_remove(@j, '$.b');
select json_remove(@j, '$.a');
select json_object();
select json_object("ki", 1, "mi", "ya");
create table t1 as select json_object('id', 87, 'name', 'carrot') as f;
show create table t1;
select * from t1;
drop table t1;
select json_exists('{"key1":"xxxx", "key2":[1, 2, 3]}', "$.key2") as ex;
select json_exists('{"key1":"xxxx", "key2":[1, 2, 3]}', "$.key2[1]") as ex;
select json_exists('{"key1":"xxxx", "key2":[1, 2, 3]}', "$.key2[10]") as ex;
select json_quote('"string"');
create table t1 as select json_quote('foo');
select * from t1;
show create table t1;
drop table t1;
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select json_merge('string');
select json_merge('string', 123);
select json_merge('"string"', 123);
select json_merge('[1, 2]', '[true, false]');
select json_merge('{"1": 2}', '{"true": false}');
select json_merge('{"1": 2}', '{"true": false}', '{"3": 4}');
select json_merge(NULL,json_object('foo', 1));
select json_merge('a','b');
select json_merge('{"a":"b"}','{"c":"d"}');
SELECT JSON_MERGE('[1, 2]', '{"id": 47}');
#enable after MDEV-32454 fix
--disable_view_protocol
select json_type('{"k1":123, "k2":345}');
select json_type('[123, "k2", 345]');
select json_type("true");
select json_type('123');
select json_type('123.12');
--enable_view_protocol
select json_keys('{"a":{"c":1, "d":2}, "b":2}');
select json_keys('{"a":{"c":1, "d":2}, "b":2}', "$.a");
select json_keys('{"a":{"c":1, "d":2}, "b":2}', "$.b");
select json_keys('foo');
#
# mdev-12789 JSON_KEYS returns duplicate keys twice
#
select json_keys('{"a":{"c":1, "d":2}, "b":2, "c":1, "a":3, "b":1, "c":2}') as ex;
select json_keys('{"c1": "value 1", "c1": "value 2"}') as ex;
SET @j = '["abc", [{"k": "10"}, "def"], {"x":"abc"}, {"y":"bcd"}]';
select json_search(@j, 'one', 'abc') as ex;
select json_search(@j, 'all', 'abc') as ex;
select json_search(@j, 'all', 'abc', NULL, '$[2]') as ex;
select json_search(@j, 'all', 'abc', NULL, '$') as ex;
select json_search(@j, 'all', '10', NULL, '$') as ex;
select json_search(@j, 'all', '10', NULL, '$[*]') as ex;
select json_search(@j, 'all', '10', NULL, '$[*][0].k') as ex;
select json_search(@j, 'all', '10', NULL, '$**.k') as ex;
create table t1( json_col text );
insert into t1 values
('{ "a": "foobar" }'),
('{ "a": "foobar", "b": "focus", "c": [ "arm", "foot", "shoulder" ] }');
select json_search( json_col, 'all', 'foot' ) as ex from t1;
drop table t1;
#enable after MDEV-32454 fix
--disable_view_protocol
select json_unquote('"abc"');
select json_unquote('abc');
--enable_view_protocol
#
# MDEV-13703 Illegal mix of collations for operation 'json_object' on using JSON_UNQUOTE as an argument.
#
create table t1 (c VARCHAR(8)) DEFAULT CHARSET=latin1;
insert into t1 values ('abc'),('def');
select json_object('foo', json_unquote(json_object('bar', c)),'qux', c) as fld from t1;
drop table t1;
#enable after MDEV-32454 fix
--disable_view_protocol
select json_object("a", json_object("b", "abcd"));
select json_object("a", '{"b": "abcd"}');
select json_object("a", json_compact('{"b": "abcd"}'));
--enable_view_protocol
select json_compact(NULL);
select json_depth(json_compact(NULL));
select json_depth('[[], {}]');
select json_depth('[[[1,2,3],"s"], {}, []]');
select json_depth('[10, {"a": 20}]');
select json_length('');
select json_length('{}');
select json_length('[1, 2, {"a": 3}]');
select json_length('{"a": 1, "b": {"c": 30}}', '$.b');
select json_length('{"a": 1, "b": {"c": 30}}');
select json_length('{}{');
create table json (j INT);
show create table json;
drop table json;
select json_length( '[ 1, [ 2, 3, 4 ], 5 ]', '$[2]' ) as ex;
select json_length( '[ 1, [ 2, 3, 4 ], 5 ]', '$[2][0]' ) as ex;
select json_length( '[ 1, [ 2, 3, 4 ], 5 ]', '$[2][0][0]' ) as ex;
select json_length( '[ 1, [ 2, 3, 4 ], 5 ]', '$[2][0][0][0]' ) as ex;
select json_length( '[ 1, [ 2, 3, 4 ], {"a":5, "b":6} ]', '$[2]' ) as ex;
select json_length( '[ 1, [ 2, 3, 4 ], {"a":5, "b":6} ]', '$[2][0]' ) as ex;
select json_length( '[ 1, [ 2, 3, 4 ], {"a":5, "b":6} ]', '$[2][0][0]' ) as ex;
select json_length( '[ 1, [ 2, 3, 4 ], {"a":5, "b":6} ]', '$[2][0][0][0]' ) as ex;
select json_length( '{"a":{"b":{"d":1}}, "a":{"c":{"d":1, "j":2}}}', '$.a[0][0][0].c' ) as ex;
select json_set('1', '$[0]', 100);
select json_set('1', '$[0][0]', 100);
select json_set('1', '$[1]', 100);
select json_set('{"a":12}', '$[0]', 100);
select json_set('{"a":12}', '$[0].a', 100);
select json_set('{"a":12}', '$[0][0].a', 100);
select json_set('{"a":12}', '$[0][1].a', 100);
select json_value('{"\\"key1":123}', '$."\\"key1"') as ex;
select json_value('{"\\"key1\\"":123}', '$."\\"key1\\""') as ex;
select json_value('{"key 1":123}', '$."key 1"') as ex;
select json_contains_path('{"a":[{"c":[1,{"a":[0,1,2]},3]}], "b":[1,2,3]}', 'one', "$**.a[2]") as ex;
select json_contains_path('{"a":[{"c":[1,{"a":[0,1,2]},3]}], "b":[1,2,3]}', 'one', "$**.a[3]") as ex;
select json_extract( '[1]', '$[0][0]' );
select json_extract( '[1]', '$[1][0]' );
select json_extract( '[1]', '$**[0]' );
select json_extract( '[1]', '$**[0][0]' );
select json_insert('1', '$[0]', 4);
select json_replace('1', '$[0]', 4);
select json_set('1', '$[0]', 4);
select json_set('1', '$[1]', 4);
select json_replace('1', '$[1]', 4);
SELECT json_insert('[]', '$[0][0]', 100);
SELECT json_insert('1', '$[0][0]', 100);
SELECT json_replace('1', '$[0][0]', 100);
SELECT json_replace('[]', '$[0][0]', 100);
SELECT json_set('[]', '$[0][0]', 100);
SELECT json_set('[]', '$[0][0][0]', 100);
#
# MDEV-11857 json_search() shows "Out of memory" with empty key.
#
SELECT JSON_search( '{"": "a"}', "one", 'a');
#
# MDEV-11858 json_merge() concatenates instead of merging.
#
select json_merge('{"a":"b"}', '{"a":"c"}') as ex ;
select json_merge('{"a":{"x":"b"}}', '{"a":"c"}') as ex ;
select json_merge('{"a":{"u":12, "x":"b"}}', '{"a":{"x":"c"}}') as ex ;
select json_merge('{"a":{"u":12, "x":"b", "r":1}}', '{"a":{"x":"c", "r":2}}') as ex ;
select json_compact('{"a":1, "b":[1,2,3], "c":{"aa":"v1", "bb": "v2"}}') as ex;
#enable after MDEV-32454 fix
--disable_view_protocol
select json_loose('{"a":1, "b":[1,2,3], "c":{"aa":"v1", "bb": "v2"}}') as ex;
select json_detailed('{"a":1, "b":[1,2,3], "c":{"aa":"v1", "bb": "v2"}}') as ex;
--enable_view_protocol
#
# MDEV-11856 json_search doesn't search for values with double quotes character (")
#
SELECT JSON_search( '{"x": "\\""}', "one", '"') as ex;
SELECT JSON_search( '{"x": "\\""}', "one", '\\"') as ex;
#
# MDEV-11833 JSON functions don't seem to respect max_allowed_packet.
#
set @save_max_allowed_packet=@@max_allowed_packet;
set @save_net_buffer_length=@@net_buffer_length;
set @@global.net_buffer_length=1024;
set @@global.max_allowed_packet=2048;
--connect (newconn, localhost, root,,)
show variables like 'net_buffer_length';
show variables like 'max_allowed_packet';
select json_array(repeat('a',1024),repeat('a',1024)) as ex;
select json_object("a", repeat('a',1024),"b", repeat('a',1024)) as ex;
--connection default
set @@global.max_allowed_packet = @save_max_allowed_packet;
set @@global.net_buffer_length = @save_net_buffer_length;
--disconnect newconn
#
# MDEV-12262 Assertion `!null_value' failed in virtual bool Item::send on JSON_REMOVE.
#
create table t1(j longtext, p longtext);
insert into t1 values
('{"a":1,"b":2,"c":3}','$.a'),
('{"a":1,"b":2,"c":3}','$.b'),
('{"a":1,"b":2,"c":3}','$.c');
select j, p, json_remove(j, p) from t1;
drop table t1;
#
# MDEV-12364 Server crashes in __memcpy_sse2_unaligned / String::copy on JSON_SEARCH with variables.
#
SET @str = 'bar', @path = '$';
SELECT JSON_SEARCH('{"foo":"bar"}', 'all' , @str, '%', @path);
#
# MDEV-12351 Assertion `cur_step->type & JSON_PATH_KEY' failed in json_find_path.
#
SELECT JSON_VALUE('[{"foo": 1},"bar"]', '$[*][0]');
#
# MDEV-12363 Assertion `0' failed in Type_handler_string_result::make_sort_key(uchar*, Item*, const SORT_FIELD_ATTR*, Sort_param*)
#
CREATE TABLE t1 (f INT NOT NULL);
INSERT INTO t1 VALUES (0);
SELECT JSON_KEYS(f) FROM t1 ORDER BY 1;
DROP TABLE t1;
#
# MDEV-12324 Wrong result (phantom array value) on JSON_EXTRACT.
#
SELECT JSON_EXTRACT( '{"foo":"bar"}', '$[*].*' );
SELECT JSON_EXTRACT( '{"foo":"bar"}', '$[*]' );
#
# MDEV-12604 Comparison of JSON_EXTRACT result differs with Mysql.
#
select JSON_EXTRACT('{"name":"value"}', '$.name') = 'value' as ex;
select JSON_EXTRACT('{\"asdf\":true}', "$.\"asdf\"") = true as ex;
select JSON_EXTRACT('{\"asdf\":true}', "$.\"asdf\"") = false as ex;
select JSON_EXTRACT('{\"asdf\":true}', "$.\"asdf\"") = 1 as ex;
select JSON_EXTRACT('{\"input1\":\"\\u00f6\"}', '$.\"input1\"') as ex;
#
# MDEV-129892 JSON_EXTRACT returns data for invalid JSON
#
select JSON_EXTRACT('{"foo": "bar" foobar foo invalid ', '$.foo') as ex;
#
# MDEV-13138 JSON_OBJECT returns null with strings containing backticks.
#
SELECT JSON_OBJECT('foo', '`') as ex;
SELECT JSON_OBJECT("foo", "bar`bar") as ex;
#
# MDEV-13324 JSON_SET returns NULL instead of object.
#
#enable after MDEV-32454 fix
--disable_view_protocol
SELECT JSON_SET('{}', '$.age', 87);
--enable_view_protocol
#
# MDEV-13104 Json functions.
#
SELECT JSON_MERGE('[]', '{"c":"d"}');
#
# MDEV-12774 JSON_EXTRACT fails with some escaped unicode as key.
#
SET @str = "{\"\\u00e4\\u00f6\":\"yes\"}";
SET @path = "$.\"\\u00e4\\u00f6\"";
select @str, @path, JSON_EXTRACT(@str, @path);
SET @str = "{\"\\u00e4\":\"yes\"}";
SET @path = "$.\"\\u00e4\"";
select @str, @path, JSON_EXTRACT(@str, @path);
#
# MDEV-12877 Wrong result from JSON native function.
#
select json_array(5,json_query('[1,2]','$'));
#
# MDEV-13633 JSON_ARRAY() - bad output with some UTF8 characters.
#
SELECT JSON_ARRAY('1. ě 2. š 3. č 4. ř 5. ž 6. ý 7. á 8. í 9. é 10. ů 11. ú') AS json_data;
SELECT JSON_OBJECT("user","Jožko Mrkvičká") as json_data;
#
# MDEV-12312 JSON_CONTAINS_PATH does not detect invalid path and returns TRUE.
#
select json_contains_path('{"foo":"bar"}', 'one', '$[]');
#
# MDEV-13971 crash in skip_num_constant.
#
select JSON_VALID(0x36f0c8dccd83c5eac156da);
#
# MDEV-13970 crash in Item_func_json_extract::read_json.
#
create table t1(a double not null);
insert into t1 values (2),(1);
select 1 from t1 where json_extract(a,'$','$[81]');
drop table t1;
#
# MDEV-15561 json_extract returns NULL with numbers in scientific notation.
#
select json_extract('{"test":8.437e-5}','$.test');
#
# MDEV-15905 select json_value('{"b":true}','$.b')=1 --> false with
# "Truncated incorrect DOUBLE value: 'true'"
#
select json_value('{"b":true}','$.b')=1;
#
# MDEV-16209 JSON_EXTRACT in query crashes server.
#
CREATE TABLE t1 (c VARCHAR(8));
INSERT INTO t1 VALUES ('foo'),('bar');
SELECT * FROM t1 WHERE c IN (JSON_EXTRACT('{"a":"b"}', '$.*'));
DROP TABLE t1;
--echo #
--echo # MDEV-16814 CREATE TABLE SELECT JSON_QUOTE(multibyte_charset_expr) makes a field of a wrong length
--echo #
CREATE TABLE t1 AS SELECT
JSON_QUOTE(_latin1'foo') AS c1,
JSON_QUOTE(_utf8'foo') AS c2;
SHOW CREATE TABLE t1;
DROP TABLE t1;
--echo #
--echo # MDEV-16054 simple json functions flatline cpu on garbage input.
--echo #
select json_array(1,user(),compress(5.140264e+307));
--echo #
--echo # MDEV-16869 String functions don't respect character set of JSON_VALUE.
--echo #
create table t1(json_col TEXT) DEFAULT CHARSET=latin1;
insert into t1 values (_latin1 X'7B226B657931223A2253EC227D');
select JSON_VALUE(json_col, '$.key1')= _latin1 X'53EC' from t1;
select REPLACE(JSON_VALUE(json_col, '$.key1'), 'null', '') = _latin1 X'53EC' as exp from t1;
drop table t1;
--echo #
--echo # MDEV-16750 JSON_SET mishandles unicode every second pair of arguments.
--echo #
#enable after MDEV-32454 fix
--disable_view_protocol
SELECT JSON_SET('{}', '$.a', _utf8 0xC3B6) as exp;
SELECT JSON_SET('{}', '$.a', _utf8 0xC3B6, '$.b', _utf8 0xC3B6) as exp;
SELECT JSON_SET('{}', '$.a', _utf8 X'C3B6', '$.x', 1, '$.b', _utf8 X'C3B6') as exp;
--enable_view_protocol
--echo #
--echo # MDEV-17121 JSON_ARRAY_APPEND
--echo #
select json_array_append('[ ]', '$', 'aue');
--echo #
--echo # MDEV-17018 JSON_SEARCH and User-Defined Variables.
--echo #
SET @`json` := '["A", [{"B": "1"}], {"C": "AB"}, {"D": "BC"}]', @`value` := 'AB';
SELECT JSON_SEARCH(@`json`, 'one', @`value`);
SET @`json` := NULL, @`value` := NULL;
--echo #
--echo # MDEV-17001 JSON_MERGE returns nullwhen merging empty array.
--echo #
SELECT JSON_MERGE('[1]', '[]');
--echo #
--echo # MDEV-16174 Assertion `0' failed in Type_handler_string_result::
--echo # make_sort_key(uchar*, Item*, const SORT_FIELD_ATTR*, Sort_param*)
--echo #
SET sql_mode='';
CREATE TABLE t1 (fld varchar(16) NOT NULL);
CREATE TABLE t2 SELECT JSON_ARRAY_INSERT(fld, '$.[0]', '0') FROM t1;
SHOW CREATE TABLE t2;
DROP TABLE t1, t2;
SET sql_mode=default;
--echo #
--echo # MDEV-17454 JSON_VALID( '{"a":1]' ) evaluates to 1
--echo #
select JSON_VALID( '{"a":1]' );
--echo #
--echo # MDEV-18886 JSON_ARRAY() does not recognise JSON argument.
--echo #
SELECT JSON_ARRAY(_UTF8 'str', JSON_OBJECT(_LATIN1 'plugin', _LATIN1'unix_socket')) as exp;
SELECT CHARSET(JSON_ARRAY()) as exp;
SELECT CHARSET(JSON_OBJECT()) as exp;
--echo #
--echo # MDEV-13992 Implement JSON_MERGE_PATCH
--echo #
CREATE TABLE merge_t(
id INT PRIMARY KEY AUTO_INCREMENT,
target VARCHAR(100), patch VARCHAR(100)
);
INSERT INTO merge_t(target, patch) VALUES
('{"a":"b"}', '{"a":"c"}'),
('{"a":"b"}', '{"b":"c"}'),
('{"a":"b"}', '{"a":null}'),
('{"a":"b", "b":"c"}', '{"a":null}'),
('{"a":["b"]}', '{"a":"c"}'),
('{"a":"c"}', '{"a":["b"]}'),
('{"a": {"b":"c"}}', '{"a": {"b":"d", "c":null}}'),
('{"a":[{"b":"c"}]}', '{"a": [1]}'),
('["a","b"]', '["c","d"]'),
('{"a":"b"}', '["c"]'),
('{"a":"foo"}', 'null'),
('{"a":"foo"}', '"bar"'),
('{"e":null}', '{"a":1}'),
('[1,2]', '{"a":"b", "c":null}'),
('{}', '{"a":{"bb":{"ccc":null}}}'),
(NULL, '{}'),
('{}', NULL);
SELECT id, target, patch,
JSON_MERGE_PATCH(target, patch) AS merged,
JSON_EXTRACT(JSON_MERGE_PATCH(target, patch), '$.a') AS a
FROM merge_t ORDER BY id;
DROP TABLE merge_t;
SELECT JSON_MERGE_PATCH('{"a":"b"}', NULL, '{"c":"d"}') as exp;
SELECT JSON_MERGE_PATCH(NULL, '[1,2,3]') as exp;
SELECT JSON_MERGE_PATCH(NULL, 'a') as exp;
SELECT JSON_MERGE_PATCH('{"a":"b"}', NULL, '[1,2,3]', '{"c":null,"d":"e"}') as exp;
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
SELECT JSON_MERGE_PATCH() as exp;
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
SELECT JSON_MERGE_PATCH('{}') as exp;
SELECT JSON_MERGE_PATCH('{', '[1,2,3]') as exp;
SELECT JSON_MERGE_PATCH('{"a":"b"}', '[1,') as exp;
--echo #
--echo # MDEV-22976 CAST(JSON_EXTRACT() AS DECIMAL) does not handle boolean values
--echo #
SELECT
CAST(JSON_EXTRACT('{"x":true}', '$.x') AS DOUBLE) AS cf,
CAST(JSON_EXTRACT('{"x":true}', '$.x') AS DECIMAL) AS cd;
SELECT
CAST(JSON_EXTRACT('{"x":false}', '$.x') AS DOUBLE) AS cf,
CAST(JSON_EXTRACT('{"x":false}', '$.x') AS DECIMAL) AS cd;
--echo #
--echo # MDEV-24585 Assertion `je->s.cs == nice_js->charset()' failed in json_nice.
--echo #
SELECT JSON_REPLACE( JSON_DETAILED('["x"]'), '$.a', 'xx' );
--echo #
--echo # MDEV-18284 JSON casting using JSON_COMPACT doesn't always work
--echo # with values from subqueries
--echo #
CREATE TABLE json_test(a JSON, b JSON);
INSERT INTO json_test VALUES ("[1,2,3]", '{"a":"foo"}');
SELECT * FROM json_test;
SELECT json_object("a", json_compact(a), "b", b)
FROM (SELECT * FROM json_test) AS json_test_values;
SELECT json_object("a", json_compact(a), "b", json_compact(b))
FROM (SELECT * FROM json_test) AS json_test_values;
DROP TABLE json_test;
--echo #
--echo # End of 10.2 tests
--echo #
--echo #
--echo # MDEV-12854 Synchronize CREATE..SELECT data type and result set metadata data type for INT functions
--echo #
--enable_metadata
--disable_ps_protocol
--disable_view_protocol
SELECT
JSON_VALID('{"id": 1, "name": "Monty"}') AS json_valid,
JSON_EXISTS('{"key1":"xxxx", "key2":[1, 2, 3]}', "$.key2") AS json_exists,
JSON_CONTAINS('{"A": 0, "B": {"C": 1}, "D": 2}', '2', '$.A') AS ison_contains,
JSON_CONTAINS_PATH('{"A": 1, "B": [2], "C": [3, 4]}', 'one', '$.A', '$.D') AS json_contains_path;
SELECT
JSON_LENGTH('{"a": 1, "b": {"c": 30}}') AS json_length,
JSON_DEPTH('[10, {"a": 20}]') AS json_depnth;
--enable_view_protocol
--enable_ps_protocol
--disable_metadata
--echo #
--echo # MDEV-19670 json escaped unicode parse error
--echo #
SELECT json_valid('{"value":"\\ud83d\\ude0a"}');
SELECT json_valid('{"test": "\\ud83d\\ude0b"}');
--echo #
--echo # MDEV-19670 json escaped unicode parse error
--echo #
SELECT JSON_VALID('{"admin\\"": null}'), '{"admin\\"": null}'
UNION
SELECT JSON_VALID('{"\\"admin": null}'), '{"\\"admin": null}'
UNION
SELECT JSON_VALID('{"\\"": null}'), '{"\\"": null}';
--echo #
--echo # MDEV-29188: Crash in JSON_EXTRACT
--echo #
CREATE TABLE t1 (j JSON);
INSERT INTO t1 VALUES
('{"ID": "4", "Name": "Betty", "Age": 19}'),
('[10, 20, [30, 40]]');
SELECT * FROM t1 WHERE JSON_EXTRACT(j, '$.Age')=19;
drop table t1;
--echo #
--echo # MDEV-27151: JSON_VALUE() does not parse NULL properties properly
--echo #
--echo #
--echo # It is correct for JSON_EXTRACT() to give null instead of "NULL" because
--echo # it returns the json literal that is put inside json.
--echo # Hence it should return null as in 'null' string and not SQL NULL.
--echo # JSON_VALUE() returns the "VALUE" so it is correct for it to return SQl NULL
--echo #
SELECT NULL;
SELECT JSON_VALUE('{"nulltest": null}', '$.nulltest');
SELECT 1 + NULL;
SELECT 1 + JSON_VALUE('{"nulltest": null}', '$.nulltest');
SELECT NULL;
SELECT JSON_EXTRACT('{"a":null, "b":10, "c":"null"}', '$.a');
--echo #
--echo # Start of 10.4 tests
--echo #
--echo #
--echo # MDEV-16351 JSON_OBJECT() treats hybrid functions with boolean arguments as numbers
--echo #
--vertical_results
#enable after fix MDEV-28649
--disable_view_protocol
SELECT
JSON_OBJECT("cond", true) AS j1,
JSON_OBJECT("cond", COALESCE(true, false)) j2,
JSON_OBJECT("cond", COALESCE(COALESCE(true, false))) j3;
--enable_view_protocol
--horizontal_results
CREATE TABLE t1 (a INT);
INSERT INTO t1 VALUES (1),(2),(3);
SELECT JSON_OBJECT('x',(SELECT MAX(a)=4 FROM t1));
SELECT JSON_OBJECT('x',(SELECT MAX(a)=3 FROM t1));
SELECT JSON_OBJECT('x',(SELECT MAX(a)=2 FROM t1));
SELECT JSON_OBJECT('x',MAX(a=4)) FROM t1;
SELECT JSON_OBJECT('x',MAX(a=3)) FROM t1;
SELECT JSON_OBJECT('x',MAX(a=2)) FROM t1;
SELECT JSON_OBJECT('x',(SELECT MAX(a=4) FROM t1));
SELECT JSON_OBJECT('x',(SELECT MAX(a=3) FROM t1));
SELECT JSON_OBJECT('x',(SELECT MAX(a=2) FROM t1));
SELECT * FROM t1 WHERE CASE WHEN JSON_OBJECT('x', (SELECT MAX(a)=4 FROM t1))='{"x": true}' THEN a END;
SELECT * FROM t1 WHERE CASE WHEN JSON_OBJECT('x', (SELECT MAX(a)=4 FROM t1))='{"x": false}' THEN a END;
SELECT * FROM t1 WHERE CASE WHEN JSON_OBJECT('x', (SELECT MAX(a)=3 FROM t1))='{"x": true}' THEN a END;
SELECT * FROM t1 WHERE CASE WHEN JSON_OBJECT('x', (SELECT MAX(a)=3 FROM t1))='{"x": false}' THEN a END;
SELECT * FROM t1 WHERE CASE WHEN JSON_OBJECT('x', (SELECT MAX(a)=2 FROM t1))='{"x": true}' THEN a END;
SELECT * FROM t1 WHERE CASE WHEN JSON_OBJECT('x', (SELECT MAX(a)=2 FROM t1))='{"x": false}' THEN a END;
SELECT * FROM t1 WHERE CASE WHEN JSON_OBJECT('x', (SELECT MAX(a=4) FROM t1))='{"x": true}' THEN a END;
SELECT * FROM t1 WHERE CASE WHEN JSON_OBJECT('x', (SELECT MAX(a=4) FROM t1))='{"x": false}' THEN a END;
SELECT * FROM t1 WHERE CASE WHEN JSON_OBJECT('x', (SELECT MAX(a=3) FROM t1))='{"x": true}' THEN a END;
SELECT * FROM t1 WHERE CASE WHEN JSON_OBJECT('x', (SELECT MAX(a=3) FROM t1))='{"x": false}' THEN a END;
SELECT * FROM t1 WHERE CASE WHEN JSON_OBJECT('x', (SELECT MAX(a=2) FROM t1))='{"x": true}' THEN a END;
SELECT * FROM t1 WHERE CASE WHEN JSON_OBJECT('x', (SELECT MAX(a=2) FROM t1))='{"x": false}' THEN a END;
DROP TABLE t1;
-- echo #
-- echo # MDEV-16620 JSON_ARRAYAGG
-- echo #
-- echo #
-- echo # Integer aggregation
-- echo #
CREATE TABLE t1 (a INT, b INT);
INSERT INTO t1 VALUES (1, 1),(2, 1), (1, 1),(2, 1), (3, 2),(2, 2),(2, 2),(2, 2);
SELECT JSON_VALID(JSON_ARRAYAGG(a)) FROM t1;
SELECT JSON_ARRAYAGG(a), JSON_ARRAYAGG(b) FROM t1;
SELECT JSON_ARRAYAGG(a), JSON_ARRAYAGG(b) FROM t1 GROUP BY b;
DROP TABLE t1;
-- echo #
-- echo # Real aggregation
-- echo #
CREATE TABLE t1 (a FLOAT, b DOUBLE, c DECIMAL(10, 2));
INSERT INTO t1 VALUES (1.0, 2.0, 3.0),(1.0, 3.0, 9.0),(1.0, 4.0, 16.0),(1.0, 5.0, 25.0);
SELECT JSON_VALID(JSON_ARRAYAGG(a)) FROM t1;
SELECT JSON_ARRAYAGG(a), JSON_ARRAYAGG(b), JSON_ARRAYAGG(c) FROM t1;
DROP TABLE t1;
-- echo #
-- echo # Boolean aggregation
-- echo #
CREATE TABLE t1 (a BOOLEAN, b BOOLEAN);
INSERT INTO t1 VALUES (TRUE, TRUE), (TRUE, FALSE), (FALSE, TRUE), (FALSE, FALSE);
SELECT JSON_VALID(JSON_ARRAYAGG(a)) FROM t1;
SELECT JSON_ARRAYAGG(a), JSON_ARRAYAGG(b) FROM t1;
SELECT JSON_ARRAYAGG(a), JSON_ARRAYAGG(b) FROM t1 GROUP BY b;
#enable after MDEV-32454 fix
--disable_view_protocol
SELECT JSON_ARRAYAGG(TRUE), JSON_ARRAYAGG(FALSE) FROM t1;
--enable_view_protocol
DROP TABLE t1;
-- echo #
-- echo # Aggregation of strings with quoted
-- echo #
CREATE TABLE t1 (a VARCHAR(80));
INSERT INTO t1 VALUES
('"double_quoted_value"'), ("'single_quoted_value'"),
('"double_quoted_value"'), ("'single_quoted_value'");
SELECT JSON_VALID(JSON_ARRAYAGG(a)) FROM t1;
SELECT JSON_ARRAYAGG(a) FROM t1;
DROP TABLE t1;
-- echo #
-- echo # Strings and NULLs
-- echo #
CREATE TABLE t1 (a INT, b VARCHAR(80));
INSERT INTO t1 VALUES
(1, "Hello"),(1, "World"), (2, "This"),(2, "Will"), (2, "Work"),(2, "!"), (3, NULL),
(1, "Hello"),(1, "World"), (2, "This"),(2, "Will"), (2, "Work"),(2, "!"), (3, NULL);
SELECT JSON_VALID(JSON_ARRAYAGG(b)) FROM t1;
SELECT JSON_ARRAYAGG(a), JSON_ARRAYAGG(b) FROM t1;
SELECT JSON_ARRAYAGG(a), JSON_ARRAYAGG(b) FROM t1 GROUP BY a;
-- echo #
-- echo # DISTINCT and LIMIT
-- echo #
SELECT JSON_ARRAYAGG(b LIMIT 1) FROM t1;
SELECT JSON_ARRAYAGG(b LIMIT 2) FROM t1;
SELECT JSON_ARRAYAGG(b LIMIT 1) FROM t1 GROUP BY b;
SELECT JSON_ARRAYAGG(b LIMIT 2) FROM t1 GROUP BY a;
SELECT JSON_ARRAYAGG(DISTINCT a) FROM t1;
SELECT JSON_ARRAYAGG(DISTINCT b) FROM t1;
SELECT JSON_ARRAYAGG(DISTINCT a LIMIT 2) FROM t1;
SELECT JSON_ARRAYAGG(DISTINCT b LIMIT 2) FROM t1;
-- echo #
-- echo # JSON aggregation
-- echo #
SELECT JSON_VALID(JSON_ARRAYAGG(JSON_ARRAY(a, b))) FROM t1;
SELECT JSON_ARRAYAGG(JSON_ARRAY(a, b)) FROM t1;
SELECT JSON_ARRAYAGG(JSON_ARRAY(a, b)) FROM t1 GROUP BY a;
SELECT JSON_VALID(JSON_ARRAYAGG(JSON_OBJECT('a', a, 'b', b))) FROM t1;
SELECT JSON_ARRAYAGG(JSON_OBJECT('a', a, 'b', b)) FROM t1;
SELECT JSON_ARRAYAGG(JSON_OBJECT('a', a, 'b', b)) FROM t1 GROUP BY a;
-- echo #
-- echo # Error checks
-- echo #
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
SELECT JSON_ARRAYAGG(a, b) FROM t1;
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
SELECT JSON_ARRAYAGG(JSON_ARRAYAGG(a, b)) FROM t1;
--error ER_INVALID_GROUP_FUNC_USE
SELECT JSON_ARRAYAGG(JSON_ARRAYAGG(a)) FROM t1;
-- echo #
-- echo # MDEV-16620 JSON_OBJECTAGG
-- echo #
SELECT JSON_OBJECTAGG(a, b) FROM t1;
--error ER_PARSE_ERROR
SELECT JSON_OBJECTAGG(a) FROM t1;
DROP TABLE t1;
--echo #
--echo # MDEV-19160 JSON_DETAILED output unnecessarily verbose
--echo #
create table t200 (a text);
insert into t200 values
('{
"steps": [
{
"join_optimization": {
"select_id": 1,
"steps": [
{
"rows_estimation": [
{
"table": "t1",
"range_analysis": {
"table_scan": {
"rows": 1000,
"cost": 2e308
},
"potential_range_indexes": [
{
"index": "a_b",
"usable": true,
"key_parts": ["a", "b"]
}
],
"best_covering_index_scan": {
"index": "a_b",
"cost": 52.195,
"chosen": true
},
"setup_range_conditions": [],
"group_index_range": {
"chosen": false,
"cause": "no group by or distinct"
},
"analyzing_range_alternatives": {
"range_scan_alternatives": [
{
"index": "a_b",
"ranges": ["2 <= a <= 2 AND 4 <= b <= 4", "123"],
"rowid_ordered": true,
"using_mrr": false,
"index_only": true,
"rows": 1,
"cost": 1.1752,
"chosen": true
}
],
"analyzing_roworder_intersect": {
"cause": "too few roworder scans"
},
"analyzing_index_merge_union": [],
"test_one_line_array":["123"]
},
"chosen_range_access_summary": {
"range_access_plan": {
"type": "range_scan",
"index": "a_b",
"rows": 1,
"ranges": ["2 <= a <= 2 AND 4 <= b <= 4"]
},
"rows_for_plan": 1,
"cost_for_plan": 1.1752,
"chosen": true
}
}
},
{
"selectivity_for_indexes": [
{
"index_name": "a_b",
"selectivity_from_index": 0.001
}
],
"selectivity_for_columns": [],
"cond_selectivity": 0.001
}
]
}
]
}
},
{
"join_execution": {
"select_id": 1,
"steps": []
}
}
]
}');
select JSON_DETAILED(JSON_EXTRACT(a, '$**.analyzing_range_alternatives')) as exp from t200;
select JSON_PRETTY(JSON_EXTRACT(a, '$**.analyzing_range_alternatives')) as exp from t200;
select JSON_LOOSE(JSON_EXTRACT(a, '$**.analyzing_range_alternatives')) as exp from t200;
drop table t200;
--echo #
--echo # MDEV-24538: JSON_LENGTH does not return error upon wrong number of parameters
--echo #
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
SELECT JSON_LENGTH('{"a":"b"}','$','$', 'foo');
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
SELECT JSON_LENGTH();
--echo # MDEV-23187: Assorted assertion failures in json_find_path with certain collations
SET @old_collation_connection= @@COLLATION_CONNECTION;
SET COLLATION_CONNECTION= ucs2_unicode_ci;
SELECT JSON_VALUE('["foo"]', '$**[0]') AS f;
SET @@COLLATION_CONNECTION= @old_collation_connection;
--echo #
--echo # End of 10.4 tests
--echo #
-- echo #
-- echo # MDEV-16620 JSON_ARRAYAGG
-- echo #
CREATE TABLE t1 (a INT);
SELECT JSON_ARRAYAGG(a) FROM t1;
DROP TABLE t1;
-- echo #
-- echo # MDEV-21915 Server crashes in copy_fields,Item_func_group_concat::add
-- echo while using json_arrayagg() as a window function
-- echo #
--error ER_NOT_SUPPORTED_YET
select json_arrayagg(a) over () from (select 1 a) t;
--error ER_NOT_SUPPORTED_YET
select json_objectagg(a, b) over () from (select 1 a, 2 b) t;
SELECT JSON_ARRAYAGG(NULL) FROM (SELECT 1 AS t) AS A;
SELECT JSON_ARRAYAGG("null") FROM (SELECT 1 AS t) AS A;
create view v as (select json_arrayagg(json_object("type", "permPeriod", "id", "asd")) as JSON_DATA);
select * from v;
drop view v;
select json_arrayagg(a order by a asc) from (select 1 a union select 2 a) t;
select json_object('x', json_arrayagg(json_object('a', 1)));
--echo #
--echo # MDEV-22011: DISTINCT with JSON_ARRAYAGG gives wrong results
--echo #
CREATE TABLE t1(a INT, b INT);
INSERT INTO t1 VALUES (1,1), (2,2), (3,3);
INSERT INTO t1 VALUES (1,1), (2,2), (3,3);
SELECT JSON_ARRAYAGG(a) FROM t1;
SELECT JSON_ARRAYAGG(DISTINCT a) FROM t1;
INSERT INTO t1 VALUES (NULL,NULL), (NULL,NULL);
SELECT JSON_ARRAYAGG(a) FROM t1;
SELECT JSON_ARRAYAGG(DISTINCT a) FROM t1;
DROP TABLE t1;
CREATE TABLE t1(a VARCHAR(10), b INT);
INSERT INTO t1 VALUES (1,1), (2,2), (3,3);
INSERT INTO t1 VALUES (1,1), (2,2), (3,3);
SELECT JSON_ARRAYAGG(a) FROM t1;
SELECT JSON_ARRAYAGG(DISTINCT a) FROM t1;
INSERT INTO t1 VALUES (NULL,NULL), (NULL,NULL);
SELECT JSON_ARRAYAGG(a) FROM t1;
SELECT JSON_ARRAYAGG(DISTINCT a) FROM t1;
DROP TABLE t1;
--echo #
--echo # MDEV-22840: JSON_ARRAYAGG gives wrong results with NULL values and ORDER by clause
--echo #
CREATE TABLE t1(a VARCHAR(255));
INSERT INTO t1 VALUES ('red'),('blue');
SELECT JSON_ARRAYAGG(a) FROM t1;
SELECT JSON_ARRAYAGG(a ORDER BY a DESC) FROM t1;
SELECT JSON_ARRAYAGG(a ORDER BY a ASC) FROM t1;
INSERT INTO t1 VALUES (NULL);
SELECT JSON_ARRAYAGG(a) FROM t1;
SELECT JSON_ARRAYAGG(a ORDER BY a DESC) FROM t1;
SELECT JSON_ARRAYAGG(a ORDER BY a ASC) FROM t1;
DROP TABLE t1;
set group_concat_max_len=64;
create table t1 (a varchar(254));
insert into t1 values (concat('x64-', repeat('a', 60)));
insert into t1 values (concat('x64-', repeat('b', 60)));
insert into t1 values (concat('x64-', repeat('c', 60)));
#enable after MDEV-32454 fix
--disable_view_protocol
--disable_ps2_protocol
select json_arrayagg(a) from t1;
--enable_ps2_protocol
--enable_view_protocol
drop table t1;
SET group_concat_max_len= default;
create table t1 (col1 json);
insert into t1 values('{"color":"red", "size":1}' );
insert into t1 values('{"color":"blue", "size":2}' );
select JSON_ARRAYAGG(col1) from t1;
drop table t1;
--echo #
--echo # MDEV-23029: JSON_OBJECTAGG returns NULL when used together with GROUP BY
--echo #
CREATE TABLE t1 (e INT, a VARCHAR(255), v VARCHAR(255));
INSERT INTO t1 VALUES (0, 'a1', '1') , (0, 'a2', '2') , (1, 'b1', '3');
EXPLAIN SELECT B.e, JSON_OBJECTAGG(B.a, B.v) FROM t1 A, t1 B GROUP BY B.e, B.a;
SELECT B.e, JSON_OBJECTAGG(B.a, B.v) FROM t1 A, t1 B GROUP BY B.e, B.a;
CREATE VIEW v AS SELECT JSON_OBJECTAGG(a, e) FROM t1;
SELECT * FROM v;
DROP VIEW v;
DROP TABLE t1;
--echo #
--echo # MDEV-23004 When using GROUP BY with JSON_ARRAYAGG with joint table, the square brackets are not included.
--echo #
CREATE TABLE t1(id int primary key, name varchar(50));
CREATE TABLE t2(id int, owner_id int);
INSERT INTO t1 VALUES (1, "name1"), (2, "name2"), (3, "name3");
INSERT INTO t2 VALUES (1, 1), (2, 1), (3, 2), (4, 3);
SELECT t1.id, JSON_ARRAYAGG(JSON_OBJECT('id',t2.id) ORDER BY t2.id) as materials
from t1 LEFT JOIN t2 on t1.id = t2.owner_id
GROUP BY t1.id ORDER BY id;
DROP TABLE t1;
DROP TABLE t2;
--echo #
--echo # MDEV-27018 IF and COALESCE lose "json" property
--echo #
SELECT json_object('a', if(1, json_object('b', 'c'), json_object('e', 'f'))) as exp;
SELECT json_object('a', coalesce(json_object('b', 'c'))) as exp;
--echo #
--echo # MDEV-26392: Crash with json_get_path_next and 10.5.12
--echo #
CREATE TABLE arrNestTest (
id VARCHAR(80) AS (JSON_COMPACT(JSON_EXTRACT(doc, "$._id"))) UNIQUE KEY,
doc JSON,
CONSTRAINT id_not_null CHECK(id IS NOT NULL));
INSERT INTO test.arrNestTest (doc) VALUES ('{ "_id" : { "$oid" : "611c0a463b150154132f6636" }, "a" : [ { "a" : [ { "a" : [ { "a" : [ { "a" : [ { "a" : [ { "a" : [ { "a" : [ { "a" : [ { "a" : [ { "a" : [ { "a" : [ { "a" : [ { "a" : [ { "a" : [ { "a" : 1.0 } ] } ] } ] } ] } ] } ] } ] } ] } ] } ] } ] } ] } ] } ] } ] }');
SELECT * FROM arrNestTest;
DROP TABLE arrNestTest;
--echo #
--echo # MDEV-30412 JSON_OBJECTAGG doesn't escape double quote in key
--echo #
SELECT JSON_OBJECTAGG('"', 1);
SELECT JSON_OBJECTAGG('\"', 1);
SELECT JSON_OBJECTAGG('\\', 1);
--echo #
--echo # End of 10.5 tests
--echo #
--echo #
--echo # MDEV-26054 Server crashes in Item_func_json_arrayagg::get_str_from_field
--echo #
CREATE TABLE t (a VARCHAR(8));
CREATE VIEW v AS SELECT * FROM t;
INSERT INTO t VALUES ('foo'),('bar');
SELECT JSON_ARRAYAGG(a) AS f FROM v;
DROP VIEW v;
DROP TABLE t;
--echo #
--echo # MDEV-29264 JSON functions overflow error based ON LONGTEXT field
--echo #
CREATE TABLE t(l1 LONGTEXT, l2 LONGTEXT, l3 LONGTEXT, l4 LONGTEXT);
INSERT INTO t VALUES('k1', 'v1', 'k2', 'v2');
SELECT JSON_ARRAY(l1, l2, l3, l4), JSON_OBJECT(l1, l2, l3, l4) from t;
SELECT JSON_ARRAY_APPEND(JSON_ARRAY(l1, l2, l3, l4), '$[0]', 'k3'), JSON_ARRAY_INSERT(JSON_ARRAY(l1, l2, l3, l4), '$[0]', 'k3') from t;
SELECT JSON_INSERT(JSON_OBJECT(l1, l2, l3, l4), '$.k3', 'v3'),JSON_SET(JSON_OBJECT(l1, l2, l3, l4), '$.k2', 'new v2'),JSON_REPLACE(JSON_OBJECT(l1, l2, l3, l4), '$.k2', 'new v2') from t;
DROP TABLE t;
--echo #
--echo # End of 10.6 tests
--echo #
--echo #
--echo # MDEV-31147 json_normalize does not work correctly with MSAN build
--echo #
CREATE TABLE t1 (val JSON);
ALTER TABLE t1 ADD COLUMN normalized_json JSON AS (JSON_NORMALIZE(val));
INSERT INTO t1 (val) VALUES ('15');
SELECT * FROM t1;
DROP TABLE t1;
--echo #
--echo # End of 10.8 tests
--echo #
--echo #
--echo # MDEV-27677: Implement JSON_OVERLAPS()
--echo #
--echo # Testing scalar json datatypes
--echo # Comparing scalar json datatypes with itself
SELECT JSON_OVERLAPS('true', 'true');
SELECT JSON_OVERLAPS('false', 'false');
SELECT JSON_OVERLAPS('1', '1');
SELECT JSON_OVERLAPS('"string1"', '"string1"');
SELECT JSON_OVERLAPS('null', 'null');
--echo # Comparing scalar json datatypes with other scalar datatype
SELECT JSON_OVERLAPS('true', 'false');
SELECT JSON_OVERLAPS('1', '"1"');
SELECT JSON_OVERLAPS('1', '0');
SELECT JSON_OVERLAPS('null', '0');
SELECT JSON_OVERLAPS('"string1"', '"string2"');
SELECT JSON_OVERLAPS('true','["abc", 1, 2, true, false]');
SELECT JSON_OVERLAPS('true','["abc", 1, 2, [true]]');
SELECT JSON_OVERLAPS('true','{"A":true}');
--echo # Testing non-scalar json data types
--echo # Comparing object with object (non-nested)
SELECT JSON_OVERLAPS('{"A":[1, 2, 3]}','{}');
SELECT JSON_OVERLAPS('{"A": 1}',
'{"A": 1}');
SELECT JSON_OVERLAPS('{"A": 1}',
'{"B": 1}');
SELECT JSON_OVERLAPS('{
"A": 1,
"B": "string1"
}',
'{
"A": 2,
"B": "string1"
}') as exp;
SELECT JSON_OVERLAPS('{
"A": 1,
"B": "string1"
}',
'{
"A": 2,
"B": "string2"
}') as exp;
--echo # Comparing nested object with other nested object
SELECT JSON_OVERLAPS('{
"A": 1,
"B": {"C":2}
}',
'{
"A": 2,
"B": {"C":1}
}') as exp;
SELECT JSON_OVERLAPS('{
"A": 1,
"B": {"C":2}
}',
'{
"A": 2,
"B": {"C":2}
}') as exp;
SELECT JSON_OVERLAPS('{
"A": {
"B": true
}
}',
'{
"A": {
"B": true,
"C": false
}
}') as exp;
SELECT JSON_OVERLAPS('{"A":1, "B":{"D":4, "E":5}}',
'{"C":3, "B":{"E":5, "D":4}}') as exp;
SELECT JSON_OVERLAPS('{"A":1, "B":{"D":4, "E":[5, 6, 7]}}',
'{"C":3, "B":{"E":5, "D":4}}') as exp;
SELECT JSON_OVERLAPS('{"A":1, "B":{"D":4, "E":[5, 6, 7]}}',
'{"C":3, "B":{"E":[5, 6, 7], "D":4}}') as exp;
SELECT JSON_OVERLAPS('{"A":1, "B":{"D":4, "E":[5, 6, 7]}}',
'{"C":3, "B":{"E":[7, 6 ,5], "D":4}}') as exp;
SELECT JSON_OVERLAPS('{"A":1, "B":{"D":4, "E":[5, 6, 7]}}',
'{"C":3, "F":{"E":[5, 6, 7], "D":4}}') as exp;
--echo # Comparing array with array (non-nested)
SELECT JSON_OVERLAPS('[1, 2, true, false, null]',
'[3, 4, 1]') as exp;
SELECT JSON_OVERLAPS('[1, 2, true, false, null]',
'[3, 4, 5]');
SELECT JSON_OVERLAPS('[1,2,3]','[]') as exp;
--echo # Comparing nested arrays
SELECT JSON_OVERLAPS('[1, 2, true, false, null]',
'[3, 4, [1]]') as exp;
SELECT JSON_OVERLAPS('[1, 2, [true, false], null]',
'[[1], [true, false]]') as exp;
SELECT JSON_OVERLAPS('[1, 2, 3, [4, 5, 6]]','[7, 8, 9, [6, 5, 4]]') as exp;
--echo # Comparing one non-scalar json datatypes with another non-scalar
--echo # json datatype
--echo # Comparing array with object
SELECT JSON_OVERLAPS('[1, 2, true, false, null]',
'{"A": 1}') as exp;
SELECT JSON_OVERLAPS('[1, 2, true, false, null, {"A":2}]',
'{"A": 1}') as exp;
SELECT JSON_OVERLAPS('[1, {"A": 2}, {"A": 1}]',
'{"A": 1}') as exp;
SELECT JSON_OVERLAPS('[1, 2, true, false, {"A": 1, "B": 2}]',
'{"A": 1, "B": 2}') as exp;
SELECT JSON_OVERLAPS('[1, 2, true, false, {"A": 1, "B": 2}]',
'{"A": 1, "B": 3}') as exp;
-- echo # Comparing nested array with object
SELECT JSON_OVERLAPS('[1, 2, true, false, [{"A": 1, "B": 2}]]',
'{"A": 1, "B": 2}') as exp;
SELECT JSON_OVERLAPS('[1, 2, true, false, [{"A": 1, "B": 2}]]',
'{"A": 1, "B": 3}') as exp;
SELECT JSON_OVERLAPS('[1, 2, true, false, [{"A": 1, "B": 2}]]',
'{"A": 1}') as exp;
--echo # Comparing array with nested object
SELECT JSON_OVERLAPS('[1, 2, true, false, {"A": 1, "B": {"C": 12}}]',
'{"A": 1, "B": {"C": 12}}') as exp;
SELECT JSON_OVERLAPS('[1, 2, true, false, [{"A": 1, "B": {"C": 12}}]]',
'{"A": 1, "B": {"C": 12}}') as exp;
--echo # Comparing nested array with nested objects
SELECT JSON_OVERLAPS('[1, 2, true, false, [{"A": 1, "B": {"C": 12}}]]',
'{"A": 1, "B":{"C": 12}}') as exp;
SELECT JSON_OVERLAPS('[[1, 2, true, false, {"A": 1, "B": {"C": 12}}]]',
'{"A": 1, "B": {"C": 12}}') as exp;
--echo # Comparing object with array
SELECT JSON_OVERLAPS('{"A": 1, "B": 3}',
'[1, 2, true, false, {"A": 1, "B": 2}]') as exp;
SELECT JSON_OVERLAPS('{"A": 1, "B": 3}',
'[1, 2, true, false, {"A": 1, "B": 3}]') as exp;
SELECT JSON_OVERLAPS('{"A": 1, "B": 3}',
'[1, 2, true, false, {"A": 1, "B": 2}, {"A": 1, "B": 3}]') as exp;
SELECT JSON_OVERLAPS('{"A": 1, "B": [1, 2, 3]}',
'[1, 2, true, false, {"A": 1, "B": 2}, {"A": 1, "B": [1, 2, 3]}]') as exp;
SELECT JSON_OVERLAPS('{"A": 1, "B": [1, 2, {"C": 3, "D": 5}]}',
'[1, 2, true, false, {"A": 1, "B": 2}, {"A":1, "B":[1, 2, {"C": 3, "D": 5}]}]') as exp;
SELECT JSON_OVERLAPS('{"A": 1, "B": [1, 2, {"C": 3, "D": 5}]}',
'[1, 2, true, false, {"A": 1, "B": 2},{"A": 1, "B": [1, 2, {"C": 3, "D": 4}]}]') as exp;
--echo # Comparing object with nested array
SELECT JSON_OVERLAPS('{"A": 1, "B": 3}','[1, 2, true, false, [{"A": 1, "B": 2}, {"A": 1, "B": 3}]]') as exp;
--echo # Checking errors and warnings
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
SELECT JSON_OVERLAPS('[1,2,{"A":B}]', '{"A":B}', '{"C":"string1"}');
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
SELECT JSON_OVERLAPS('[1,2,{"A":B}]');
--echo #
--echo # MDEV-27990: Incorrect behavior of JSON_OVERLAPS() on warning
--echo #
SELECT JSON_OVERLAPS('','');
SELECT JSON_OVERLAPS('true','tr');
--echo #
--echo # MDEV-22224: Support JSON Path negative index
--echo #
SET @json='{
"A": [0,
[1, 2, 3],
[4, 5, 6],
"seven",
0.8,
true,
false,
"eleven",
[12, 13, {"key1":"value1"},[15]],
true],
"B": {"C": 1},
"D": 2
}';
SELECT JSON_ARRAY_APPEND(@json, '$.A[-2][-1]', 5);
SELECT JSON_ARRAY_APPEND(@json, '$.A[last-1][last]', 5);
SET @json='{
"A": [0,
[1, 2, 3],
[4, 5, 6],
"seven",
0.8,
true,
false,
"eleven",
[12, 13, {"key1":"value1"},[15]],
true],
"B": {"C": 1},
"D": 2
}';
SELECT JSON_ARRAY_INSERT(@json, '$.A[-2][-2]', 5);
SELECT JSON_ARRAY_INSERT(@json, '$.A[last-1][last-1]', 5);
SET @json='{
"A": [0,
[1, 2, 3],
[4, 5, 6],
"seven",
0.8,
true,
false,
"eleven",
[12, 13, {"key1":"value1"},[15]],
true],
"B": {"C": 1},
"D": 2
}';
SELECT JSON_CONTAINS(@json, '15', '$.A[-2][-1]');
SELECT JSON_CONTAINS(@json, '15', '$.A[last-1][last]');
SET @json='{
"A": [0,
[1, 2, 3],
[4, 5, 6],
"seven",
0.8,
true,
false,
"eleven",
[12, 13, {"key1":"value1"},[15]],
true],
"B": {"C": 1},
"D": 2
}';
SELECT JSON_CONTAINS_PATH(@json, 'one', '$.A[-2]');
SELECT JSON_CONTAINS_PATH(@json, 'one', '$.A[last-1]');
SET @json='{
"A": [0,
[1, 2, 3],
[4, 5, 6],
"seven",
0.8,
true,
false,
"eleven",
[12, 13, {"key1":"value1"},[15]],
true],
"B": {"C": 1},
"D": 2
}';
SELECT JSON_EXISTS(@json, '$.A[-2][-1]');
SELECT JSON_EXISTS(@json, '$.A[last-1][last]');
SET @json='{
"A": [0,
[1, 2, 3],
[4, 5, 6],
"seven",
0.8,
true,
false,
"eleven",
[12, [13, 14], {"key1":"value1"},[15]],
true],
"B": {"C": 1},
"D": 2
}';
SELECT JSON_EXTRACT(@json, '$.A[-8][1]');
SELECT JSON_EXTRACT(@json, '$.A[last-7][1]');
SET @json= '[{"A": 1, "B": 2, "C": {"D": 3}},{"A": 1, "B": 2, "C": {"D": 3}}]';
SELECT JSON_KEYS(@json, '$[-1].C');
SELECT JSON_KEYS(@json, '$[last].C');
SET @json='{
"A": [0,
[1, 2, 3],
[4, 5, 6],
"seven",
0.8,
true,
false,
"eleven",
[12, [13, 14], {"key1":"value1"},[15]],
true],
"B": {"C": 1},
"D": 2
}';
SELECT JSON_LENGTH(@json, '$.A[-2][-3]');
SELECT JSON_LENGTH(@json, '$.A[last-1][last-2]');
SET @json='{
"A": [0,
[1, 2, 3],
[4, 5, 6],
"seven",
0.8,
true,
false,
"eleven",
[12, [13, 14], {"key1":"value1"},[15]],
true],
"B": {"C": 1},
"D": 2
}';
SELECT JSON_QUERY(@json, '$.A[-8]');
SELECT JSON_QUERY(@json, '$.A[last-7]');
SET @json='{
"A": [0,
[1, 2, 3],
[4, 5, 6],
"seven",
0.8,
true,
false,
"eleven",
[12, [13, 14], {"key1":"value1"},[15]],
true],
"B": {"C": 1},
"D": 2
}';
SELECT JSON_REMOVE(@json, '$.A[-10]');
SELECT JSON_REMOVE(@json, '$.A[last-9]');
SET @json='{
"A": [0,
[1, 2, 3],
[4, 5, 6],
"seven",
0.8,
true,
false,
"eleven",
[12, [13, 14], {"key1":"value1"},[15]],
true],
"B": {"C": 1},
"D": 2
}';
SELECT JSON_REPLACE(@json, '$.A[-1]', 4);
SELECT JSON_REPLACE(@json, '$.A[last]', 4);
SET @json = '["abc", [{"k": "10"}, "def"], {"x":"abc"}, {"y":"bcd"}]';
SELECT JSON_SEARCH(@json, 'all', 'abc', NULL, '$[-2]');
SELECT JSON_SEARCH(@json, 'all', 'abc', NULL, '$[last-1]');
SET @json='{
"A": [0,
[1, 2, 3],
[4, 5, 6],
"seven",
0.8,
true,
false,
"eleven",
[12, [13, 14], {"key1":"value1"},[15]],
true],
"B": {"C": 1},
"D": 2
}';
SELECT JSON_SET(@json, '$.A[-4]', 100);
SELECT JSON_SET(@json, '$.A[last-3]', 100);
SET @json='{
"A": [0,
[1, 2, 3],
[4, 5, 6],
"seven",
0.8,
true,
false,
"eleven",
[12, [13, 14], {"key1":123},[15]],
true],
"B": {"C": 1},
"D": 2
}';
SELECT JSON_VALUE(@json, '$.A[-2][-2].key1');
SELECT JSON_VALUE(@json, '$.A[last-1][last-1].key1');
--echo #
--echo # MDEV-27972: Unexpected behavior with negative zero (-0) in JSON Path
--echo #
SET @json='{ "x": [0,1]}';
SELECT JSON_VALUE(@json,'$.x[last]');
SELECT JSON_VALUE(@json,'$.x[last-0]');
SELECT JSON_VALUE(@json,'$.x[-0]');
SELECT JSON_VALUE(@json,'$.x[0]');
--echo #
--echo # MDEV-27911: Implement range notation for json path
--echo #
SET @json= '[
[1, {"key1": "value1"}, 3],
[false, 5, 6],
[7, 8, [9, {"key2": 2}, 11]],
[15, 1.34, [14], ["string1", [16, {"key1":[1,2,3,[4,5,6]]}, 18]]],
[19, 20],
21, 22
]';
SELECT JSON_EXISTS(@json, '$[3][3][-2 to last]');
SET @json= '[
[1, {"key1": "value1"}, 3],
[false, 5, 6],
[7, 8, [9, {"key2": 2}, 11]],
[15, 1.34, [14], ["string1", [16, {"key1":[1,2,3,[4,5,6]]}, 18]]],
[19, 20],
21, 22
]';
SELECT JSON_SEARCH(@json, 'one', '12', NULL, '$[3][0 to 3]');
SET @json= '[
[1, {"key1": "value1"}, 3],
[false, 5, 6],
[7, 8, [9, {"key2": 2}, 11]],
[12, 1.34, [14], ["string1", [16, {"key1":[1,2,3,[4,5,6]]}, 18]]],
[19, 20]
]';
SELECT JSON_VALUE(@json, '$[0][1 to 2].key1');
SET @json='{
"A": [0,
[1, 2, 3],
[4, 5, 6],
"seven",
0.8,
true,
false,
"eleven",
[12, [13, 14], {"key1":"value1"},[15]],
true],
"B": {"C": 1},
"D": 2
}';
SELECT JSON_QUERY(@json, '$.A[-2][-3 to -1]');
SET @json= '[
[1, {"key1": "value1"}, 3],
[false, 5, 6],
[7, 8, [9, {"key2": 2}, 11]],
[15, 1.34, [14], ["string1", [16, {"key1":[1,2,3,[4,5,6]]}, 18]]],
[19, 20],
21, 22
]';
SELECT JSON_EXTRACT(@json, '$[0 to 3][2]');
SELECT JSON_EXTRACT(@json, '$[3][3][last-1 to last]');
SELECT JSON_EXTRACT(@json, '$[3][3][-2 to -1]');
--echo # Checking errors
SET @json= '[
[1, {"key1": "value1"}, 3],
[false, 5, 6],
[7, 8, [9, {"key2": 2}, 11]],
[15, 1.34, [14], ["string1", [16, {"key1":[1,2,3,[4,5,6]]}, 18]]],
[19, 20],
21, 22
]';
SELECT JSON_CONTAINS_PATH(@json,'one', '$[3][0 to 3]');
SET @json= '[
[1, {"key1": "value1"}, 3],
[false, 5, 6],
[7, 8, [9, {"key2": 2}, 11]],
[15, 1.34, [14], ["string1", [16, {"key1":[1,2,3,[4,5,6]]}, 18]]],
[19, 20],
21, 22
]';
SELECT JSON_CONTAINS(@json, '$[3][0 to 3]');
SET @json='{
"A": [0,
[1, 2, 3],
[4, 5, 6],
"seven",
0.8,
true,
false,
"eleven",
[12, 13, {"key1":"value1"},[15]],
true],
"B": {"C": 1},
"D": 2
}';
SELECT JSON_ARRAY_INSERT(@json, '$.A[0 to last-1]', 5);
SET @json='{
"A": [0,
[1, 2, 3],
[4, 5, 6],
"seven",
0.8,
true,
false,
"eleven",
[12, 13, {"key1":"value1"},[15]],
true],
"B": {"C": 1},
"D": 2
}';
SELECT JSON_ARRAY_APPEND(@json, '$.A[*]', 7);
SET @json= '[
[1, {"key1": "value1"}, 3],
[false, 5, 6],
[7, 8, [9, {"key2": 2}, 11]],
[12, 1.34, [14], ["string1", [16, {"key1":[1,2,3,[4,5,6]]}, 18]]],
[19, 20]
]';
SELECT JSON_SET(@json, '$[0][1 to 2].key1', 1);
SET @json= '[
[1, {"key1": "value1"}, 3],
[false, 5, 6],
[7, 8, [9, {"key2": 2}, 11]],
[15, 1.34, [14], ["string1", [16, {"key1":[1,2,3,[4,5,6]]}, 18]]],
[19, 20],
21, 22
]';
SELECT JSON_REPLACE(@json, '$[1][last-2 to last]', 4);
SET @json= '[
[1, {"key1": "value1"}, 3],
[false, 5, 6],
[7, 8, [9, {"key2": 2}, 11]],
[15, 1.34, [14], ["string1", [16, {"key1":[1,2,3,[4,5,6]]}, 18]]],
[19, 20],
21, 22
]';
SELECT JSON_REMOVE(@json, '$[1][-6 to last-2]');
SET @json='{
"A": [0,
[1, 2, 3],
[4, 5, 6],
"seven",
0.8,
true,
false,
"eleven",
[12, [13, 14], {"key1":"value1"},[15]],
true],
"B": {"C": 1},
"D": 2
}';
SELECT JSON_KEYS(@json, '$.A[8][1 to 3]');
--echo #
--echo # MDEV-28075: JSON_VALUE returns first value from array not from range
--echo #
SET @json1= '[
[{"key1": "value1"}, {"key2": "value2"}],
[{"key3": "value3"}, {"key1": "value4"}],
[{"key1": "value5"}, {"key4": "value6"}, {"key1": "value7"}]
]';
SELECT JSON_VALUE(@json1, '$[2][1 to 2].key1');
SET @json= '[
[1.1, {"key1": "value1"}, 3],
[false, 5, 6],
[7, 8, [9, {"key2": 2}, 11]],
[11, 1.34, [14], ["string1", [16, {"key1":[1,2,3,[4,5,6]]}, 18]]],
[19, 20]
]';
SELECT JSON_VALUE(@json, '$[*][0]');
SELECT JSON_VALUE(@json, '$[2 to 3][0]');
--echo #
--echo # MDEV-28072: JSON_EXTRACT has inconsistent behavior with '0' value in
--echo # json path (when range is used)
--echo #
SET @json= '[ 11, 22 , 33]';
SELECT JSON_EXTRACT(@json, '$[0 to 0]');
SELECT JSON_EXTRACT(@json, '$[0 to -0]');
SELECT JSON_EXTRACT(@json, '$[-0 to 0]');
SELECT JSON_EXTRACT(@json, '$[-0 to -0]');
--echo #
--echo # MDEV-28071: JSON_EXISTS returns always 1 if it is used range notation
--echo # for json path
--echo #
SET @json= '[
[1, {"key1": "value1"}, 3],
[false, 5, 6],
[7, 8, [9, {"key2": 2}, 11]],
[15, 1.34, [14], ["string1", [16, {"key1":[1,2,3,[4,5,6]]}, 18]]],
[19, 20],
21, 22
]';
SELECT JSON_EXISTS(@json, '$[2][2][1 to 2]');
SELECT JSON_EXISTS(@json, '$[2][2][4 to 6]');
SELECT JSON_EXISTS(@json, '$[2][2][1 to 4]');
--echo #
--echo # MDEV-28326: Server crashes in json_path_parts_compare
--echo #
SELECT * FROM JSON_TABLE('{"foo":["bar","qux"]}','$**.*[0]' COLUMNS(col1 CHAR(8) PATH '$[0]')) AS jt;
--echo #
--echo # MDEV-29212: json_overlaps() does not check nested key-value pair correctly
--echo #
SET @json1 = '{"kk":{"k1":"v1","k2":"v2"}}';
SET @json2 = '{"kk":{"k1":"v1","k2":"v2","k3":"v3"}}';
SELECT JSON_OVERLAPS(@json2, @json1);
SELECT JSON_OVERLAPS(@json1, @json2);
--echo #
--echo # MDEV-30304: Json Range only affects first row of the result set
--echo #
CREATE TABLE t1 ( j JSON );
INSERT INTO t1 (j) VALUES ('[{"key1": 1, "key2": 1}, {"key3": 1, "key4": 1}]');
INSERT INTO t1 (j) VALUES ('[{"key1": 2, "key2": 2}, {"key3": 2, "key4": 2}, {"key5": 2, "key6": 2}]');
INSERT INTO t1 (j) VALUES ('[{"key1": 3, "key2": 3}, {"key3": 3, "key4": 3}, {"key5": 3}]');
SELECT JSON_EXTRACT(j, '$[0 to 1]') FROM t1 ;
SELECT JSON_EXTRACT(j, '$[*]') FROM t1 ;
DROP TABLE t1;
--echo #
--echo # MDEV-29381: JSON paths containing dashes are reported as syntax errors in procedures
--echo #
SELECT JSON_EXTRACT('{ "my-key": 1 }', '$."my-key"');
SELECT JSON_EXTRACT('{ "my-key": 1 }', '$.my-key');
--echo #
--echo # MDEV-23187: Assorted assertion failures in json_find_path with certain collations
--echo #
SET @save_collation_connection= @@collation_connection;
SET @json='{ "A": [ [{"k":"v"},[1]],true],"B": {"C": 1} }';
SELECT JSON_VALUE(@json,'$.A[last-1][last-1].key1');
SET @json='{ "A": [ [{"k":"v"},[1]],true],"B": {"C": 1} }';
SET collation_connection='ucs2_bin';
SELECT JSON_VALUE(@json,'$.A[last-1][last-1].key1');
SET @json='{ "A": [ [{"k":"v"},[15]],true],"B": {"C": 1} }';
SET sql_mode=0,character_set_connection=utf32;
SELECT JSON_VALUE(@json,'$.A[last-1][last-1].key1');
SET @json='{ "A": [ [{"k":"v"},[15]],true],"B": {"C": 1} }';
SET sql_mode=0,character_set_connection=utf32;
SELECT JSON_VALUE(@json,'$.A[last-1][last-1].key1');
SET @@collation_connection= @save_collation_connection;
--echo #
--echo # End of 10.9 Test
--echo #
|