Skip to content
/ server Public
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions mysql-test/main/func_json.result
Original file line number Diff line number Diff line change
Expand Up @@ -1768,6 +1768,26 @@ null<=>json_extract('1',json_object(null,'{ }',null,null),'{}')
Warnings:
Warning 4042 Syntax error in JSON path in argument 2 to function 'json_extract' at position 1
# End of 10.6 tests
SELECT json_extract(t.j, '$')
FROM (SELECT json_extract('["a",1]', '$') AS j) AS t;
json_extract(t.j, '$')
["a", 1]
SELECT json_extract(t.j, '$')
FROM (SELECT json_extract('{"key":"value"}', '$') AS j) AS t;
json_extract(t.j, '$')
{"key": "value"}
SELECT json_extract(t.j, '$[0]', '$[1]')
FROM (SELECT json_extract('[{"a":1},{"b":2},{"c":3}]', '$') AS j) AS t;
json_extract(t.j, '$[0]', '$[1]')
[{"a": 1}, {"b": 2}]
SELECT json_extract(t.j, '$')
FROM (SELECT json_extract('{"name":"Ä Ö Ü ß","city":"München"}', '$') AS j) AS t;
json_extract(t.j, '$')
{"name": "Ä Ö Ü ß", "city": "München"}
SELECT json_extract(t.j, '$.name')
FROM (SELECT json_extract('{"name":"日本語テスト","val":[1,2]}', '$') AS j) AS t;
json_extract(t.j, '$.name')
"日本語テスト"
#
# MDEV-35614 JSON_UNQUOTE doesn't work with emojis
#
Expand Down
17 changes: 17 additions & 0 deletions mysql-test/main/func_json.test
Original file line number Diff line number Diff line change
Expand Up @@ -1235,6 +1235,23 @@ select null<=>json_extract('1',json_object(null,'{ }',null,null),'{}');

--echo # End of 10.6 tests

SELECT json_extract(t.j, '$')
FROM (SELECT json_extract('["a",1]', '$') AS j) AS t;

SELECT json_extract(t.j, '$')
FROM (SELECT json_extract('{"key":"value"}', '$') AS j) AS t;

# Multi-path extraction through derived table
SELECT json_extract(t.j, '$[0]', '$[1]')
FROM (SELECT json_extract('[{"a":1},{"b":2},{"c":3}]', '$') AS j) AS t;

# UTF-8 content through derived table
SELECT json_extract(t.j, '$')
FROM (SELECT json_extract('{"name":"Ä Ö Ü ß","city":"München"}', '$') AS j) AS t;

SELECT json_extract(t.j, '$.name')
FROM (SELECT json_extract('{"name":"日本語テスト","val":[1,2]}', '$') AS j) AS t;

--echo #
--echo # MDEV-35614 JSON_UNQUOTE doesn't work with emojis
--echo #
Expand Down
12 changes: 11 additions & 1 deletion sql/item_jsonfunc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1036,8 +1036,18 @@ bool Item_json_str_multipath::fix_fields(THD *thd, Item **ref)
bool Item_func_json_extract::fix_length_and_dec(THD *thd)
{
collation.set(args[0]->collation);
max_length= args[0]->max_length * (arg_count - 1);

/* *2 accounts for LOOSE json_nice() formatting (spaces after : and ,). */
ulonglong char_length=
(ulonglong) args[0]->max_char_length() * (arg_count - 1) * 2;

if (arg_count > 2)
{
/* Multiple paths: result is wrapped as [val1, val2, ...]. */
char_length+= 2 + (arg_count - 2) * 2;
}

fix_char_length_ulonglong(char_length);
mark_constant_paths(paths, args+1, arg_count-1);
set_maybe_null();
return FALSE;
Expand Down