Skip to content

Commit 0cd7318

Browse files
author
Alexey Botchkov
committed
MDEV-13104 Json functions.
An extra ',' added to the JSON_MERGE result making it invalid.
1 parent 467acc2 commit 0cd7318

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

mysql-test/r/func_json.result

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,3 +683,6 @@ JSON_OBJECT("foo", "bar`bar")
683683
SELECT JSON_SET('{}', '$.age', 87);
684684
JSON_SET('{}', '$.age', 87)
685685
{"age": 87}
686+
SELECT JSON_MERGE('[]', '{"c":"d"}');
687+
JSON_MERGE('[]', '{"c":"d"}')
688+
[{"c": "d"}]

mysql-test/t/func_json.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,8 @@ SELECT JSON_OBJECT("foo", "bar`bar");
333333
#
334334
SELECT JSON_SET('{}', '$.age', 87);
335335

336+
#
337+
# MDEV-13104 Json functions.
338+
#
339+
SELECT JSON_MERGE('[]', '{"c":"d"}');
340+

sql/item_jsonfunc.cc

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1971,14 +1971,25 @@ static int do_merge(String *str, json_engine_t *je1, json_engine_t *je2)
19711971
else
19721972
{
19731973
const uchar *end1, *beg1, *end2, *beg2;
1974+
int empty_array= 0;
19741975

19751976
beg1= je1->value_begin;
19761977

19771978
/* Merge as a single array. */
19781979
if (je1->value_type == JSON_VALUE_ARRAY)
19791980
{
1980-
if (json_skip_level(je1))
1981+
int cur_level= je1->stack_p;
1982+
empty_array= 1;
1983+
while (json_scan_next(je1) == 0)
1984+
{
1985+
if (je1->stack_p < cur_level)
1986+
break;
1987+
empty_array= 0;
1988+
}
1989+
1990+
if (je1->s.error)
19811991
return 1;
1992+
19821993
end1= je1->s.c_str - je1->sav_c_len;
19831994
}
19841995
else
@@ -1995,8 +2006,8 @@ static int do_merge(String *str, json_engine_t *je1, json_engine_t *je2)
19952006
end1= je1->value_end;
19962007
}
19972008

1998-
if (str->append((const char*) beg1, end1 - beg1),
1999-
str->append(", ", 2))
2009+
if (str->append((const char*) beg1, end1 - beg1) ||
2010+
(!empty_array && str->append(", ", 2)))
20002011
return 3;
20012012

20022013
if (json_value_scalar(je2))

0 commit comments

Comments
 (0)