Commit 95228dc8 authored by Alexey Botchkov's avatar Alexey Botchkov

MDEV-11570 JSON_MERGE returns incorrect result.

        JSON merging fixed.
parent 83dbb2d4
......@@ -315,6 +315,9 @@ Warning 4038 Syntax error in JSON text in argument 1 to function 'json_merge' at
select json_merge('{"a":"b"}','{"c":"d"}');
json_merge('{"a":"b"}','{"c":"d"}')
{"a":"b", "c":"d"}
SELECT JSON_MERGE('[1, 2]', '{"id": 47}');
JSON_MERGE('[1, 2]', '{"id": 47}')
[1, 2, {"id": 47}]
select json_type('{"k1":123, "k2":345}');
json_type('{"k1":123, "k2":345}')
OBJECT
......
......@@ -123,6 +123,7 @@ 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}');
select json_type('{"k1":123, "k2":345}');
select json_type('[123, "k2", 345]');
......
......@@ -1522,12 +1522,10 @@ String *Item_func_json_merge::val_str(String *str)
goto error_return;
str->length(0);
if ((je1.value_type == JSON_VALUE_ARRAY &&
je2.value_type == JSON_VALUE_ARRAY) ||
(je1.value_type == JSON_VALUE_OBJECT &&
je2.value_type == JSON_VALUE_OBJECT))
if (je1.value_type == JSON_VALUE_OBJECT &&
je2.value_type == JSON_VALUE_OBJECT)
{
/* Merge the adjancent arrays or objects. */
/* Wrap as a single objects. */
if (json_skip_level(&je1))
goto error_return;
if (str->append(js1->ptr(),
......@@ -1539,11 +1537,35 @@ String *Item_func_json_merge::val_str(String *str)
}
else
{
/* Wrap as an array. */
if (str->append("[", 1) ||
str->append(js1->ptr(), js1->length()) ||
str->append(", ", 2) ||
str->append(js2->ptr(), js2->length()) ||
const char *end1, *beg2;
/* Merge as a single array. */
if (je1.value_type == JSON_VALUE_ARRAY)
{
if (json_skip_level(&je1))
goto error_return;
end1= (const char *) (je1.s.c_str - je1.sav_c_len);
}
else
{
if (str->append("[", 1))
goto error_return;
end1= js1->end();
}
if (str->append(js1->ptr(), end1 - js1->ptr()),
str->append(", ", 2))
goto error_return;
if (je2.value_type == JSON_VALUE_ARRAY)
beg2= (const char *) je2.s.c_str;
else
beg2= js2->ptr();
if (str->append(beg2, js2->end() - beg2))
goto error_return;
if (je2.value_type != JSON_VALUE_ARRAY &&
str->append("]", 1))
goto error_return;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment