Skip to content

Commit

Permalink
expression: fix json_key not compatible with MySQL (#14556)
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkingrei authored and sre-bot committed Jan 20, 2020
1 parent e266a80 commit 664adfe
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
5 changes: 1 addition & 4 deletions expression/builtin_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,7 @@ func (b *builtinJSONKeysSig) evalJSON(row chunk.Row) (res json.BinaryJSON, isNul
return res, isNull, err
}
if res.TypeCode != json.TypeCodeObject {
return res, true, json.ErrInvalidJSONData
return res, true, nil
}
return res.GetKeys(), false, nil
}
Expand All @@ -1288,9 +1288,6 @@ func (b *builtinJSONKeys2ArgsSig) evalJSON(row chunk.Row) (res json.BinaryJSON,
if isNull || err != nil {
return res, isNull, err
}
if res.TypeCode != json.TypeCodeObject {
return res, true, json.ErrInvalidJSONData
}

path, isNull, err := b.args[1].EvalString(b.ctx, row)
if isNull || err != nil {
Expand Down
13 changes: 7 additions & 6 deletions expression/builtin_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,19 +579,20 @@ func (s *testEvaluatorSuite) TestJSONKeys(c *C) {
expected interface{}
success bool
}{

// Tests nil arguments
{[]interface{}{nil}, nil, true},
{[]interface{}{nil, "$.c"}, nil, true},
{[]interface{}{`{"a": 1}`, nil}, nil, true},
{[]interface{}{nil, nil}, nil, true},

// Tests with other type
{[]interface{}{`1`}, nil, false},
{[]interface{}{`"str"`}, nil, false},
{[]interface{}{`true`}, nil, false},
{[]interface{}{`null`}, nil, false},
{[]interface{}{`[1, 2]`}, nil, false},
{[]interface{}{`["1", "2"]`}, nil, false},
{[]interface{}{`1`}, nil, true},
{[]interface{}{`"str"`}, nil, true},
{[]interface{}{`true`}, nil, true},
{[]interface{}{`null`}, nil, true},
{[]interface{}{`[1, 2]`}, nil, true},
{[]interface{}{`["1", "2"]`}, nil, true},

// Tests without path expression
{[]interface{}{`{}`}, `[]`, true},
Expand Down
13 changes: 8 additions & 5 deletions expression/builtin_json_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ func (b *builtinJSONKeysSig) vecEvalJSON(input *chunk.Chunk, result *chunk.Colum

j = buf.GetJSON(i)
if j.TypeCode != json.TypeCodeObject {
return json.ErrInvalidJSONData
result.AppendNull()
continue
}
result.AppendJSON(j.GetKeys())
}
Expand Down Expand Up @@ -522,10 +523,6 @@ func (b *builtinJSONKeys2ArgsSig) vecEvalJSON(input *chunk.Chunk, result *chunk.
continue
}

jsonItem := jsonBuf.GetJSON(i)
if jsonItem.TypeCode != json.TypeCodeObject {
return json.ErrInvalidJSONData
}
pathExpr, err := json.ParseJSONPathExpr(pathBuf.GetString(i))
if err != nil {
return err
Expand All @@ -534,6 +531,12 @@ func (b *builtinJSONKeys2ArgsSig) vecEvalJSON(input *chunk.Chunk, result *chunk.
return json.ErrInvalidJSONPathWildcard
}

jsonItem := jsonBuf.GetJSON(i)
if jsonItem.TypeCode != json.TypeCodeObject {
result.AppendNull()
continue
}

res, exists := jsonItem.Extract([]json.PathExpression{pathExpr})
if !exists || res.TypeCode != json.TypeCodeObject {
result.AppendNull()
Expand Down
4 changes: 2 additions & 2 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4058,13 +4058,13 @@ func (s *testIntegrationSuite) TestFuncJSON(c *C) {
r.Check(testkit.Rows("1 0 1 0"))

r = tk.MustQuery(`select
json_keys('[]'),
json_keys('{}'),
json_keys('{"a": 1, "b": 2}'),
json_keys('{"a": {"c": 3}, "b": 2}'),
json_keys('{"a": {"c": 3}, "b": 2}', "$.a")
`)
r.Check(testkit.Rows(`[] ["a", "b"] ["a", "b"] ["c"]`))
r.Check(testkit.Rows(`<nil> [] ["a", "b"] ["a", "b"] ["c"]`))

r = tk.MustQuery(`select
json_length('1'),
Expand Down

0 comments on commit 664adfe

Please sign in to comment.