expression: fix json_key not compatible with MySQL (#14556)

This commit is contained in:
Weizhen Wang
2020-01-20 21:30:03 +08:00
committed by pingcap-github-bot
parent e266a805ba
commit 664adfe477
4 changed files with 18 additions and 17 deletions

View File

@ -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
}
@ -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 {

View File

@ -579,6 +579,7 @@ func (s *testEvaluatorSuite) TestJSONKeys(c *C) {
expected interface{}
success bool
}{
// Tests nil arguments
{[]interface{}{nil}, nil, true},
{[]interface{}{nil, "$.c"}, nil, true},
@ -586,12 +587,12 @@ func (s *testEvaluatorSuite) TestJSONKeys(c *C) {
{[]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},

View File

@ -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())
}
@ -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
@ -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()

View File

@ -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'),