Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expression:add support for JSON_MERGE_PRESERVE #8931

Merged
merged 6 commits into from
Jan 11, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
expression:add support for JSON_MERGE_PRESERVE
  • Loading branch information
shinytang6 committed Jan 8, 2019
commit d2521c5246e5cacbd7596e0cf93de133fdd9de29
39 changes: 38 additions & 1 deletion expression/builtin_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,9 @@ func (b *builtinJSONMergeSig) evalJSON(row chunk.Row) (res json.BinaryJSON, isNu
values = append(values, value)
}
res = json.MergeBinary(values)
// function "JSON_MERGE" is deprecated since MySQL 5.7.22. Synonym for function "JSON_MERGE_PRESERVE".
// See https://dev.mysql.com/doc/refman/5.7/en/json-modification-functions.html#function_json-merge
b.ctx.GetSessionVars().StmtCtx.AppendWarning(errDeprecatedSyntaxNoReplacement.GenWithStackByArgs("JSON_MERGE"))
return res, false, nil
}

Expand Down Expand Up @@ -719,8 +722,42 @@ type jsonMergePreserveFunctionClass struct {
baseFunctionClass
}

type builtinJSONMergePreserveSig struct {
baseBuiltinFunc
}

func (b *builtinJSONMergePreserveSig) Clone() builtinFunc {
newSig := &builtinJSONMergePreserveSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}

func (c *jsonMergePreserveFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (builtinFunc, error) {
return nil, errFunctionNotExists.GenWithStackByArgs("FUNCTION", "JSON_MERGE_PRESERVE")
if err := c.verifyArgs(args); err != nil {
return nil, err
}
argTps := make([]types.EvalType, 0, len(args))
for range args {
argTps = append(argTps, types.ETJson)
}
bf := newBaseBuiltinFuncWithTp(ctx, args, types.ETJson, argTps...)
sig := &builtinJSONMergePreserveSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_JsonMergePreserveSig)
return sig, nil
}

func (b *builtinJSONMergePreserveSig) evalJSON(row chunk.Row) (res json.BinaryJSON, isNull bool, err error) {
values := make([]json.BinaryJSON, 0, len(b.args))
for _, arg := range b.args {
var value json.BinaryJSON
value, isNull, err = arg.EvalJSON(b.ctx, row)
if isNull || err != nil {
return res, isNull, err
}
values = append(values, value)
}
res = json.MergeBinary(values)
return res, false, nil
}

type jsonPrettyFunctionClass struct {
Expand Down
29 changes: 29 additions & 0 deletions expression/builtin_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,35 @@ func (s *testEvaluatorSuite) TestJSONMerge(c *C) {
}
}

func (s *testEvaluatorSuite) TestJSONMergePreserve(c *C) {
defer testleak.AfterTest(c)()
fc := funcs[ast.JSONMergePreserve]
tbl := []struct {
Input []interface{}
Expected interface{}
}{
{[]interface{}{nil, nil}, nil},
{[]interface{}{`{}`, `[]`}, `[{}]`},
{[]interface{}{`{}`, `[]`, `3`, `"4"`}, `[{}, 3, "4"]`},
}
for _, t := range tbl {
args := types.MakeDatums(t.Input...)
f, err := fc.getFunction(s.ctx, s.datumsToConstants(args))
c.Assert(err, IsNil)
d, err := evalBuiltinFunc(f, chunk.Row{})
c.Assert(err, IsNil)

switch x := t.Expected.(type) {
case string:
j1, err := json.ParseBinaryFromString(x)
c.Assert(err, IsNil)
j2 := d.GetMysqlJSON()
cmp := json.CompareBinary(j1, j2)
c.Assert(cmp, Equals, 0, Commentf("got %v expect %v", j1.String(), j2.String()))
}
}
}

func (s *testEvaluatorSuite) TestJSONArray(c *C) {
defer testleak.AfterTest(c)()
fc := funcs[ast.JSONArray]
Expand Down