summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexey Botchkov <holyfoot@askmonty.org>2018-09-13 13:42:09 +0400
committerAlexey Botchkov <holyfoot@askmonty.org>2018-09-13 13:42:09 +0400
commitf54485eadbab7fa1c6a388408cf908daf488284c (patch)
tree37c64375a46092978c126c2208a064f54a6971c2
parent2b46dca5d7fdade3f64b993dc30686693210022a (diff)
downloadmariadb-git-f54485eadbab7fa1c6a388408cf908daf488284c.tar.gz
MDEV-17001 JSON_MERGE returns nullwhen merging empty array.
Don't add the comma if nothing appended to the array.
-rw-r--r--mysql-test/r/func_json.result6
-rw-r--r--mysql-test/t/func_json.test5
-rw-r--r--sql/item_jsonfunc.cc29
-rw-r--r--strings/json_lib.c4
4 files changed, 24 insertions, 20 deletions
diff --git a/mysql-test/r/func_json.result b/mysql-test/r/func_json.result
index 6ba4ed753ea..c828b2676b4 100644
--- a/mysql-test/r/func_json.result
+++ b/mysql-test/r/func_json.result
@@ -804,3 +804,9 @@ SELECT JSON_SEARCH(@`json`, 'one', @`value`);
JSON_SEARCH(@`json`, 'one', @`value`)
"$[2].C"
SET @`json` := NULL, @`value` := NULL;
+#
+# MDEV-17001 JSON_MERGE returns nullwhen merging empty array.
+#
+SELECT JSON_MERGE('[1]', '[]');
+JSON_MERGE('[1]', '[]')
+[1]
diff --git a/mysql-test/t/func_json.test b/mysql-test/t/func_json.test
index 0790ea2ffba..1dc16419cfd 100644
--- a/mysql-test/t/func_json.test
+++ b/mysql-test/t/func_json.test
@@ -463,3 +463,8 @@ 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]', '[]');
diff --git a/sql/item_jsonfunc.cc b/sql/item_jsonfunc.cc
index 4cb79749903..60726050aca 100644
--- a/sql/item_jsonfunc.cc
+++ b/sql/item_jsonfunc.cc
@@ -2020,23 +2020,14 @@ continue_j2:
else
{
const uchar *end1, *beg1, *end2, *beg2;
- int empty_array= 0;
+ int n_items1=1, n_items2= 1;
beg1= je1->value_begin;
/* Merge as a single array. */
if (je1->value_type == JSON_VALUE_ARRAY)
{
- int cur_level= je1->stack_p;
- empty_array= 1;
- while (json_scan_next(je1) == 0)
- {
- if (je1->stack_p < cur_level)
- break;
- empty_array= 0;
- }
-
- if (je1->s.error)
+ if (json_skip_level_and_count(je1, &n_items1))
return 1;
end1= je1->s.c_str - je1->sav_c_len;
@@ -2055,8 +2046,7 @@ continue_j2:
end1= je1->value_end;
}
- if (str->append((const char*) beg1, end1 - beg1) ||
- (!empty_array && str->append(", ", 2)))
+ if (str->append((const char*) beg1, end1 - beg1))
return 3;
if (json_value_scalar(je2))
@@ -2067,15 +2057,22 @@ continue_j2:
else
{
if (je2->value_type == JSON_VALUE_OBJECT)
+ {
beg2= je2->value_begin;
+ if (json_skip_level(je2))
+ return 2;
+ }
else
+ {
beg2= je2->s.c_str;
- if (json_skip_level(je2))
- return 2;
+ if (json_skip_level_and_count(je2, &n_items2))
+ return 2;
+ }
end2= je2->s.c_str;
}
- if (str->append((const char*) beg2, end2 - beg2))
+ if ((n_items1 && n_items2 && str->append(", ", 2)) ||
+ str->append((const char*) beg2, end2 - beg2))
return 3;
if (je2->value_type != JSON_VALUE_ARRAY &&
diff --git a/strings/json_lib.c b/strings/json_lib.c
index 1c0ff4b5345..4f12cbb82b5 100644
--- a/strings/json_lib.c
+++ b/strings/json_lib.c
@@ -1197,10 +1197,6 @@ int json_skip_to_level(json_engine_t *j, int level)
}
-#define json_skip_level(json_engine) \
- json_skip_to_level((json_engine), (json_engine)->stack_p)
-
-
/*
works as json_skip_level() but also counts items on the current
level skipped.