Skip to content

Commit ebe5ebb

Browse files
author
Alexey Botchkov
committed
MDEV-9143 JSON_xxx functions.
The rest of mysql/json functions implemented. CAST AS JSON implemented.
1 parent 1122c1f commit ebe5ebb

File tree

15 files changed

+1619
-59
lines changed

15 files changed

+1619
-59
lines changed

include/json_lib.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,13 @@ int json_scan_next(json_engine_t *j);
226226
int json_read_keyname_chr(json_engine_t *j);
227227

228228

229+
/*
230+
Check if the name of the current JSON key matches
231+
the step of the path.
232+
*/
233+
int json_key_matches(json_engine_t *je, json_string_t *k);
234+
235+
229236
/*
230237
json_read_value() function parses the JSON value syntax,
231238
so that we can handle the value of a key or an array item.

mysql-test/r/dyncol.result

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,36 +1731,36 @@ column_json(column_create(1, "val", 2, column_create(3, "val2")))
17311731
# Time encoding
17321732
#
17331733
select hex(column_create("t", "800:46:06.23434" AS time)) as hex,
1734-
column_json(column_create("t", "800:46:06.23434" AS time)) as json;
1735-
hex json
1734+
column_json(column_create("t", "800:46:06.23434" AS time)) as js;
1735+
hex js
17361736
04010001000000070074649363B82003 {"t":"800:46:06.234340"}
17371737
select hex(column_create(1, "800:46:06.23434" AS time)) as hex,
1738-
column_json(column_create(1, "800:46:06.23434" AS time)) as json;
1739-
hex json
1738+
column_json(column_create(1, "800:46:06.23434" AS time)) as js;
1739+
hex js
17401740
000100010007649363B82003 {"1":"800:46:06.234340"}
17411741
select hex(column_create("t", "800:46:06" AS time)) as hex,
1742-
column_json(column_create("t", "800:46:06" AS time)) as json;
1743-
hex json
1742+
column_json(column_create("t", "800:46:06" AS time)) as js;
1743+
hex js
17441744
04010001000000070074860B32 {"t":"800:46:06"}
17451745
select hex(column_create(1, "800:46:06" AS time)) as hex,
1746-
column_json(column_create(1, "800:46:06" AS time)) as json;
1747-
hex json
1746+
column_json(column_create(1, "800:46:06" AS time)) as js;
1747+
hex js
17481748
000100010007000060B82003 {"1":"800:46:06"}
17491749
select hex(column_create("t", "2012-12-21 10:46:06.23434" AS datetime)) as hex,
1750-
column_json(column_create("t", "2012-12-21 10:46:06.23434" AS datetime)) as json;
1751-
hex json
1750+
column_json(column_create("t", "2012-12-21 10:46:06.23434" AS datetime)) as js;
1751+
hex js
17521752
0401000100000005007495B90F649363B80A00 {"t":"2012-12-21 10:46:06.234340"}
17531753
select hex(column_create(1, "2012-12-21 10:46:06.23434" AS datetime)) as hex,
1754-
column_json(column_create(1, "2012-12-21 10:46:06.23434" AS datetime)) as json;
1755-
hex json
1754+
column_json(column_create(1, "2012-12-21 10:46:06.23434" AS datetime)) as js;
1755+
hex js
17561756
00010001000595B90F649363B80A00 {"1":"2012-12-21 10:46:06.234340"}
17571757
select hex(column_create("t", "2012-12-21 10:46:06" AS datetime)) as hex,
1758-
column_json(column_create("t", "2012-12-21 10:46:06" AS datetime)) as json;
1759-
hex json
1758+
column_json(column_create("t", "2012-12-21 10:46:06" AS datetime)) as js;
1759+
hex js
17601760
0401000100000005007495B90F86AB00 {"t":"2012-12-21 10:46:06"}
17611761
select hex(column_create(1, "2012-12-21 10:46:06" AS datetime)) as hex,
1762-
column_json(column_create(1, "2012-12-21 10:46:06" AS datetime)) as json;
1763-
hex json
1762+
column_json(column_create(1, "2012-12-21 10:46:06" AS datetime)) as js;
1763+
hex js
17641764
00010001000595B90F000060B80A00 {"1":"2012-12-21 10:46:06"}
17651765
#
17661766
# MDEV-4849: Out of memory error and valgrind warnings on COLUMN_ADD

mysql-test/r/func_json.result

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ json_array_append('["a", "b"]', '$', FALSE)
4646
select json_array_append('{"k1":1, "k2":["a", "b"]}', '$.k2', 2);
4747
json_array_append('{"k1":1, "k2":["a", "b"]}', '$.k2', 2)
4848
{"k1":1, "k2":["a", "b", 2]}
49+
SELECT JSON_ARRAY_INSERT('["a", {"b": [1, 2]}, [3, 4]]', '$[1]', 'x');
50+
JSON_ARRAY_INSERT('["a", {"b": [1, 2]}, [3, 4]]', '$[1]', 'x')
51+
["a", "x", {"b": [1, 2]}, [3, 4]]
52+
SELECT JSON_ARRAY_INSERT('["a", {"b": [1, 2]}, [3, 4]]', '$[2]', 'x');
53+
JSON_ARRAY_INSERT('["a", {"b": [1, 2]}, [3, 4]]', '$[2]', 'x')
54+
["a", {"b": [1, 2]}, "x", [3, 4]]
55+
SELECT JSON_ARRAY_INSERT('["a", {"b": [1, 2]}, [3, 4]]', '$[3]', 'x');
56+
JSON_ARRAY_INSERT('["a", {"b": [1, 2]}, [3, 4]]', '$[3]', 'x')
57+
["a", {"b": [1, 2]}, [3, 4], "x"]
58+
SELECT JSON_ARRAY_INSERT('["a", {"b": [1, 2]}, [3, 4]]', '$[4]', 'x');
59+
JSON_ARRAY_INSERT('["a", {"b": [1, 2]}, [3, 4]]', '$[4]', 'x')
60+
["a", {"b": [1, 2]}, [3, 4], "x"]
4961
select json_contains('{"k1":123, "k2":345}', '123', '$.k1');
5062
json_contains('{"k1":123, "k2":345}', '123', '$.k1')
5163
1
@@ -91,6 +103,44 @@ json_extract('{"key1":5, "key2":[2,3]}', "$.key1", "$.key2")
91103
select json_extract('{"key0":true, "key1":"qwe"}', "$.key1");
92104
json_extract('{"key0":true, "key1":"qwe"}', "$.key1")
93105
qwe
106+
select json_insert('{"a":1, "b":{"c":1}, "d":[1, 2]}', '$.b.k1', 'word');
107+
json_insert('{"a":1, "b":{"c":1}, "d":[1, 2]}', '$.b.k1', 'word')
108+
{"a":1, "b":{"c":1, "k1":"word"}, "d":[1, 2]}
109+
select json_insert('{"a":1, "b":{"c":1}, "d":[1, 2]}', '$.d[3]', 3);
110+
json_insert('{"a":1, "b":{"c":1}, "d":[1, 2]}', '$.d[3]', 3)
111+
{"a":1, "b":{"c":1}, "d":[1, 2, 3]}
112+
select json_insert('{"a":1, "b":{"c":1}, "d":[1, 2]}', '$.a[2]', 2);
113+
json_insert('{"a":1, "b":{"c":1}, "d":[1, 2]}', '$.a[2]', 2)
114+
{"a":[1, 2], "b":{"c":1}, "d":[1, 2]}
115+
select json_insert('{"a":1, "b":{"c":1}, "d":[1, 2]}', '$.b.c', 'word');
116+
json_insert('{"a":1, "b":{"c":1}, "d":[1, 2]}', '$.b.c', 'word')
117+
{"a":1, "b":{"c":1}, "d":[1, 2]}
118+
select json_set('{ "a": 1, "b": [2, 3]}', '$.a', 10, '$.c', '[true, false]');
119+
json_set('{ "a": 1, "b": [2, 3]}', '$.a', 10, '$.c', '[true, false]')
120+
{ "a": 10, "b": [2, 3], "c":"[true, false]"}
121+
select json_replace('{ "a": 1, "b": [2, 3]}', '$.a', 10, '$.c', '[true, false]');
122+
json_replace('{ "a": 1, "b": [2, 3]}', '$.a', 10, '$.c', '[true, false]')
123+
{ "a": 10, "b": [2, 3]}
124+
select json_replace('{ "a": 1, "b": [2, 3]}', '$.a', 10, '$.b', '[true, false]');
125+
json_replace('{ "a": 1, "b": [2, 3]}', '$.a', 10, '$.b', '[true, false]')
126+
{ "a": 10, "b": "[true, false]"}
127+
set @j = '["a", ["b", "c"], "d"]';
128+
select json_remove(@j, '$[0]');
129+
json_remove(@j, '$[0]')
130+
[ ["b", "c"], "d"]
131+
select json_remove(@j, '$[1]');
132+
json_remove(@j, '$[1]')
133+
["a" "d"]
134+
select json_remove(@j, '$[2]');
135+
json_remove(@j, '$[2]')
136+
["a", ["b", "c"]]
137+
set @j = '{"a": 1, "b": [2, 3]}';
138+
select json_remove(@j, '$.b');
139+
json_remove(@j, '$.b')
140+
{"a": 1}
141+
select json_remove(@j, '$.a');
142+
json_remove(@j, '$.a')
143+
{"b": [2, 3]}
94144
select json_object("ki", 1, "mi", "ya");
95145
json_object("ki", 1, "mi", "ya")
96146
{"ki": 1, "mi": "ya"}
@@ -121,3 +171,46 @@ BOOLEAN
121171
select json_type('123');
122172
json_type('123')
123173
NUMBER
174+
select json_keys('{"a":{"c":1, "d":2}, "b":2}');
175+
json_keys('{"a":{"c":1, "d":2}, "b":2}')
176+
["a", "b"]
177+
select json_keys('{"a":{"c":1, "d":2}, "b":2}', "$.a");
178+
json_keys('{"a":{"c":1, "d":2}, "b":2}', "$.a")
179+
["c", "d"]
180+
select json_keys('{"a":{"c":1, "d":2}, "b":2}', "$.b");
181+
json_keys('{"a":{"c":1, "d":2}, "b":2}', "$.b")
182+
NULL
183+
SET @j = '["abc", [{"k": "10"}, "def"], {"x":"abc"}, {"y":"bcd"}]';
184+
select json_search(@j, 'one', 'abc');
185+
json_search(@j, 'one', 'abc')
186+
"$[0]"
187+
select json_search(@j, 'all', 'abc');
188+
json_search(@j, 'all', 'abc')
189+
["$[0]", "$[2].x"]
190+
select json_search(@j, 'all', 'abc', NULL, '$[2]');
191+
json_search(@j, 'all', 'abc', NULL, '$[2]')
192+
"$[2].x"
193+
select json_search(@j, 'all', 'abc', NULL, '$');
194+
json_search(@j, 'all', 'abc', NULL, '$')
195+
["$[0]", "$[2].x"]
196+
select json_search(@j, 'all', '10', NULL, '$');
197+
json_search(@j, 'all', '10', NULL, '$')
198+
"$[1][0].k"
199+
select json_search(@j, 'all', '10', NULL, '$[*]');
200+
json_search(@j, 'all', '10', NULL, '$[*]')
201+
"$[1][0].k"
202+
select json_search(@j, 'all', '10', NULL, '$[*][0].k');
203+
json_search(@j, 'all', '10', NULL, '$[*][0].k')
204+
"$[1][0].k"
205+
select json_unquote('"abc"');
206+
json_unquote('"abc"')
207+
abc
208+
select json_object("a", json_object("b", "abcd"));
209+
json_object("a", json_object("b", "abcd"))
210+
{"a": {"b": "abcd"}}
211+
select json_object("a", '{"b": "abcd"}');
212+
json_object("a", '{"b": "abcd"}')
213+
{"a": "{\"b\": \"abcd\"}"}
214+
select json_object("a", cast('{"b": "abcd"}' as json));
215+
json_object("a", cast('{"b": "abcd"}' as json))
216+
{"a": {"b": "abcd"}}

mysql-test/t/dyncol.test

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -805,24 +805,24 @@ select column_json(column_create(1, "val", 2, column_create(3, "val2")));
805805
--echo # Time encoding
806806
--echo #
807807
select hex(column_create("t", "800:46:06.23434" AS time)) as hex,
808-
column_json(column_create("t", "800:46:06.23434" AS time)) as json;
808+
column_json(column_create("t", "800:46:06.23434" AS time)) as js;
809809
select hex(column_create(1, "800:46:06.23434" AS time)) as hex,
810-
column_json(column_create(1, "800:46:06.23434" AS time)) as json;
810+
column_json(column_create(1, "800:46:06.23434" AS time)) as js;
811811

812812
select hex(column_create("t", "800:46:06" AS time)) as hex,
813-
column_json(column_create("t", "800:46:06" AS time)) as json;
813+
column_json(column_create("t", "800:46:06" AS time)) as js;
814814
select hex(column_create(1, "800:46:06" AS time)) as hex,
815-
column_json(column_create(1, "800:46:06" AS time)) as json;
815+
column_json(column_create(1, "800:46:06" AS time)) as js;
816816

817817
select hex(column_create("t", "2012-12-21 10:46:06.23434" AS datetime)) as hex,
818-
column_json(column_create("t", "2012-12-21 10:46:06.23434" AS datetime)) as json;
818+
column_json(column_create("t", "2012-12-21 10:46:06.23434" AS datetime)) as js;
819819
select hex(column_create(1, "2012-12-21 10:46:06.23434" AS datetime)) as hex,
820-
column_json(column_create(1, "2012-12-21 10:46:06.23434" AS datetime)) as json;
820+
column_json(column_create(1, "2012-12-21 10:46:06.23434" AS datetime)) as js;
821821

822822
select hex(column_create("t", "2012-12-21 10:46:06" AS datetime)) as hex,
823-
column_json(column_create("t", "2012-12-21 10:46:06" AS datetime)) as json;
823+
column_json(column_create("t", "2012-12-21 10:46:06" AS datetime)) as js;
824824
select hex(column_create(1, "2012-12-21 10:46:06" AS datetime)) as hex,
825-
column_json(column_create(1, "2012-12-21 10:46:06" AS datetime)) as json;
825+
column_json(column_create(1, "2012-12-21 10:46:06" AS datetime)) as js;
826826

827827
--echo #
828828
--echo # MDEV-4849: Out of memory error and valgrind warnings on COLUMN_ADD

mysql-test/t/func_json.test

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ select json_array(1, "text", false, null);
1919
select json_array_append('["a", "b"]', '$', FALSE);
2020
select json_array_append('{"k1":1, "k2":["a", "b"]}', '$.k2', 2);
2121

22+
SELECT JSON_ARRAY_INSERT('["a", {"b": [1, 2]}, [3, 4]]', '$[1]', 'x');
23+
SELECT JSON_ARRAY_INSERT('["a", {"b": [1, 2]}, [3, 4]]', '$[2]', 'x');
24+
SELECT JSON_ARRAY_INSERT('["a", {"b": [1, 2]}, [3, 4]]', '$[3]', 'x');
25+
SELECT JSON_ARRAY_INSERT('["a", {"b": [1, 2]}, [3, 4]]', '$[4]', 'x');
26+
2227
select json_contains('{"k1":123, "k2":345}', '123', '$.k1');
2328
select json_contains('"you"', '"you"');
2429
select json_contains('"youth"', '"you"');
@@ -37,6 +42,24 @@ select json_extract('{"key1":"asd", "key2":[2,3]}', "$.key1", "$.key2");
3742
select json_extract('{"key1":5, "key2":[2,3]}', "$.key1", "$.key2");
3843
select json_extract('{"key0":true, "key1":"qwe"}', "$.key1");
3944

45+
select json_insert('{"a":1, "b":{"c":1}, "d":[1, 2]}', '$.b.k1', 'word');
46+
select json_insert('{"a":1, "b":{"c":1}, "d":[1, 2]}', '$.d[3]', 3);
47+
select json_insert('{"a":1, "b":{"c":1}, "d":[1, 2]}', '$.a[2]', 2);
48+
select json_insert('{"a":1, "b":{"c":1}, "d":[1, 2]}', '$.b.c', 'word');
49+
50+
select json_set('{ "a": 1, "b": [2, 3]}', '$.a', 10, '$.c', '[true, false]');
51+
52+
select json_replace('{ "a": 1, "b": [2, 3]}', '$.a', 10, '$.c', '[true, false]');
53+
select json_replace('{ "a": 1, "b": [2, 3]}', '$.a', 10, '$.b', '[true, false]');
54+
55+
set @j = '["a", ["b", "c"], "d"]';
56+
select json_remove(@j, '$[0]');
57+
select json_remove(@j, '$[1]');
58+
select json_remove(@j, '$[2]');
59+
set @j = '{"a": 1, "b": [2, 3]}';
60+
select json_remove(@j, '$.b');
61+
select json_remove(@j, '$.a');
62+
4063
select json_object("ki", 1, "mi", "ya");
4164

4265
select json_exists('{"key1":"xxxx", "key2":[1, 2, 3]}', "$.key2");
@@ -52,3 +75,23 @@ select json_type('[123, "k2", 345]');
5275
select json_type("true");
5376
select json_type('123');
5477

78+
select json_keys('{"a":{"c":1, "d":2}, "b":2}');
79+
select json_keys('{"a":{"c":1, "d":2}, "b":2}', "$.a");
80+
select json_keys('{"a":{"c":1, "d":2}, "b":2}', "$.b");
81+
82+
SET @j = '["abc", [{"k": "10"}, "def"], {"x":"abc"}, {"y":"bcd"}]';
83+
select json_search(@j, 'one', 'abc');
84+
select json_search(@j, 'all', 'abc');
85+
select json_search(@j, 'all', 'abc', NULL, '$[2]');
86+
select json_search(@j, 'all', 'abc', NULL, '$');
87+
select json_search(@j, 'all', '10', NULL, '$');
88+
select json_search(@j, 'all', '10', NULL, '$[*]');
89+
select json_search(@j, 'all', '10', NULL, '$[*][0].k');
90+
91+
92+
select json_unquote('"abc"');
93+
94+
select json_object("a", json_object("b", "abcd"));
95+
select json_object("a", '{"b": "abcd"}');
96+
select json_object("a", cast('{"b": "abcd"}' as json));
97+

sql/item.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,6 +1457,7 @@ class Item: public Value_source,
14571457
virtual void set_result_field(Field *field) {}
14581458
virtual bool is_result_field() { return 0; }
14591459
virtual bool is_bool_type() { return false; }
1460+
virtual bool is_json_type() { return false; }
14601461
/* This is to handle printing of default values */
14611462
virtual bool need_parentheses_in_default() { return false; }
14621463
virtual void save_in_result_field(bool no_conversions) {}
@@ -5763,4 +5764,15 @@ class Item_iterator_row: public Item_iterator
57635764
void close() {}
57645765
};
57655766

5767+
5768+
/*
5769+
It's used in ::fix_fields() methods of LIKE and JSON_SEARCH
5770+
functions to handle the ESCAPE parameter.
5771+
This parameter is quite non-standard so the specific function.
5772+
*/
5773+
bool fix_escape_item(THD *thd, Item *escape_item, String *tmp_str,
5774+
bool escape_used_in_parsing, CHARSET_INFO *cmp_cs,
5775+
int *escape);
5776+
5777+
57665778
#endif /* SQL_ITEM_INCLUDED */

sql/item_cmpfunc.cc

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5124,13 +5124,10 @@ bool Item_func_like::with_sargable_pattern() const
51245124
}
51255125

51265126

5127-
bool Item_func_like::fix_fields(THD *thd, Item **ref)
5127+
bool fix_escape_item(THD *thd, Item *escape_item, String *tmp_str,
5128+
bool escape_used_in_parsing, CHARSET_INFO *cmp_cs,
5129+
int *escape)
51285130
{
5129-
DBUG_ASSERT(fixed == 0);
5130-
if (Item_bool_func2::fix_fields(thd, ref) ||
5131-
escape_item->fix_fields(thd, &escape_item))
5132-
return TRUE;
5133-
51345131
if (!escape_item->const_during_execution())
51355132
{
51365133
my_error(ER_WRONG_ARGUMENTS,MYF(0),"ESCAPE");
@@ -5140,7 +5137,7 @@ bool Item_func_like::fix_fields(THD *thd, Item **ref)
51405137
if (escape_item->const_item())
51415138
{
51425139
/* If we are on execution stage */
5143-
String *escape_str= escape_item->val_str(&cmp_value1);
5140+
String *escape_str= escape_item->val_str(tmp_str);
51445141
if (escape_str)
51455142
{
51465143
const char *escape_str_ptr= escape_str->ptr();
@@ -5153,15 +5150,15 @@ bool Item_func_like::fix_fields(THD *thd, Item **ref)
51535150
return TRUE;
51545151
}
51555152

5156-
if (use_mb(cmp_collation.collation))
5153+
if (use_mb(cmp_cs))
51575154
{
51585155
CHARSET_INFO *cs= escape_str->charset();
51595156
my_wc_t wc;
51605157
int rc= cs->cset->mb_wc(cs, &wc,
51615158
(const uchar*) escape_str_ptr,
51625159
(const uchar*) escape_str_ptr +
51635160
escape_str->length());
5164-
escape= (int) (rc > 0 ? wc : '\\');
5161+
*escape= (int) (rc > 0 ? wc : '\\');
51655162
}
51665163
else
51675164
{
@@ -5170,25 +5167,40 @@ bool Item_func_like::fix_fields(THD *thd, Item **ref)
51705167
code instead of Unicode code as "escape" argument.
51715168
Convert to "cs" if charset of escape differs.
51725169
*/
5173-
CHARSET_INFO *cs= cmp_collation.collation;
51745170
uint32 unused;
51755171
if (escape_str->needs_conversion(escape_str->length(),
5176-
escape_str->charset(), cs, &unused))
5172+
escape_str->charset(),cmp_cs,&unused))
51775173
{
51785174
char ch;
51795175
uint errors;
5180-
uint32 cnvlen= copy_and_convert(&ch, 1, cs, escape_str_ptr,
5176+
uint32 cnvlen= copy_and_convert(&ch, 1, cmp_cs, escape_str_ptr,
51815177
escape_str->length(),
51825178
escape_str->charset(), &errors);
5183-
escape= cnvlen ? ch : '\\';
5179+
*escape= cnvlen ? ch : '\\';
51845180
}
51855181
else
5186-
escape= escape_str_ptr ? *escape_str_ptr : '\\';
5182+
*escape= escape_str_ptr ? *escape_str_ptr : '\\';
51875183
}
51885184
}
51895185
else
5190-
escape= '\\';
5186+
*escape= '\\';
5187+
}
5188+
5189+
return FALSE;
5190+
}
51915191

5192+
5193+
bool Item_func_like::fix_fields(THD *thd, Item **ref)
5194+
{
5195+
DBUG_ASSERT(fixed == 0);
5196+
if (Item_bool_func2::fix_fields(thd, ref) ||
5197+
escape_item->fix_fields(thd, &escape_item) ||
5198+
fix_escape_item(thd, escape_item, &cmp_value1, escape_used_in_parsing,
5199+
cmp_collation.collation, &escape))
5200+
return TRUE;
5201+
5202+
if (escape_item->const_item())
5203+
{
51925204
/*
51935205
We could also do boyer-more for non-const items, but as we would have to
51945206
recompute the tables for each row it's not worth it.

0 commit comments

Comments
 (0)