From aafe5a2bf19a7dcc856e88e47d0c7f49ad510510 Mon Sep 17 00:00:00 2001 From: Meng Xin Date: Tue, 28 Jun 2022 18:26:39 +0800 Subject: [PATCH] cherry pick #35320 to release-6.1 Signed-off-by: ti-srebot --- types/json/binary_functions.go | 4 ++++ types/json/binary_test.go | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/types/json/binary_functions.go b/types/json/binary_functions.go index 88770ae78c89f..926ba52b8cca0 100644 --- a/types/json/binary_functions.go +++ b/types/json/binary_functions.go @@ -221,6 +221,10 @@ func (bj BinaryJSON) Extract(pathExprList []PathExpression) (ret BinaryJSON, fou // even if len(pathExprList) equals to 1. found = true ret = buf[0] + // Fix https://github.com/pingcap/tidb/issues/30352 + if pathExprList[0].ContainsAnyAsterisk() { + ret = buildBinaryArray(buf) + } } else { found = true ret = buildBinaryArray(buf) diff --git a/types/json/binary_test.go b/types/json/binary_test.go index e276d81def524..4142c9cdf150f 100644 --- a/types/json/binary_test.go +++ b/types/json/binary_test.go @@ -62,12 +62,12 @@ func TestBinaryJSONExtract(t *testing.T) { {bj1, []string{"$.*[0]"}, mustParseBinaryFromString(t, `["world", 1, true, "d"]`), true, nil}, {bj1, []string{`$.a[*]."aa"`}, mustParseBinaryFromString(t, `["bb", "cc"]`), true, nil}, {bj1, []string{`$."\"hello\""`}, mustParseBinaryFromString(t, `"world"`), true, nil}, - {bj1, []string{`$**[1]`}, mustParseBinaryFromString(t, `"2"`), true, nil}, + {bj1, []string{`$**[1]`}, mustParseBinaryFromString(t, `["2"]`), true, nil}, {bj3, []string{`$.properties.$type`}, mustParseBinaryFromString(t, `"TiDB"`), true, nil}, {bj4, []string{`$.properties.$type$type`}, mustParseBinaryFromString(t, `{"$a$a" : "TiDB"}`), true, nil}, {bj4, []string{`$.properties.$type$type.$a$a`}, mustParseBinaryFromString(t, `"TiDB"`), true, nil}, {bj5, []string{`$.properties.$type.$a.$b`}, mustParseBinaryFromString(t, `"TiDB"`), true, nil}, - {bj5, []string{`$.properties.$type.$a.*[0]`}, mustParseBinaryFromString(t, `"TiDB"`), true, nil}, + {bj5, []string{`$.properties.$type.$a.*[0]`}, mustParseBinaryFromString(t, `["TiDB"]`), true, nil}, // test extract with multi path expressions. {bj1, []string{"$.a", "$[5]"}, mustParseBinaryFromString(t, `[[1, "2", {"aa": "bb"}, 4.0, {"aa": "cc"}]]`), true, nil},