Skip to content

Commit 6a2765b

Browse files
YangKeaozeminzhou
authored andcommitted
json: fix the issue that two arrays are merged but not append in JSON_ARRAY_APPEND. (pingcap#59688)
close pingcap#59465
1 parent 528a463 commit 6a2765b

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

pkg/expression/builtin_json.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,10 @@ func (b *builtinJSONArrayAppendSig) appendJSONArray(res types.BinaryJSON, p stri
12081208
}
12091209
}
12101210

1211+
// wrap the new value `v` into an array explicitly, in case that the `v` is an array itself.
1212+
// For example, `JSON_ARRAY_APPEND('[1]', '$', JSON_ARRAY(2, 3))` should return `[1, [2, 3]]`
1213+
v = types.CreateBinaryJSON([]any{v})
1214+
12111215
obj = types.MergeBinaryJSON([]types.BinaryJSON{obj, v})
12121216
res, err = res.Modify([]types.JSONPathExpression{pathExpr}, []types.BinaryJSON{obj}, types.JSONModifySet)
12131217
return res, false, err

tests/integrationtest/r/expression/json.result

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,3 +861,6 @@ json_extract('{"a":"b"}', '$[last]')
861861
select json_set('{"a":"b"}', '$[last]', 1);
862862
json_set('{"a":"b"}', '$[last]', 1)
863863
1
864+
SELECT JSON_ARRAY_APPEND('[1]', '$', JSON_ARRAY(2, 3));
865+
JSON_ARRAY_APPEND('[1]', '$', JSON_ARRAY(2, 3))
866+
[1, [2, 3]]

tests/integrationtest/t/expression/json.test

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,4 +564,7 @@ select json_extract("{\"\\f\":\"\"}", "$");
564564
# TestJSONExtractObjectFromLast
565565
select json_extract('{"a":"b"}', '$[0]');
566566
select json_extract('{"a":"b"}', '$[last]');
567-
select json_set('{"a":"b"}', '$[last]', 1);
567+
select json_set('{"a":"b"}', '$[last]', 1);
568+
569+
# TestIssue59465
570+
SELECT JSON_ARRAY_APPEND('[1]', '$', JSON_ARRAY(2, 3));

0 commit comments

Comments
 (0)