Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions go/vt/vtgate/evalengine/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,8 @@ func (c *compiler) compileParseJSON(fn string, doct ctype, offset int) (ctype, e
case sqltypes.TypeJSON:
case sqltypes.VarChar, sqltypes.VarBinary:
c.asm.Parse_j(offset)
case sqltypes.Null:
return ctype{Type: sqltypes.Null, Flag: flagNull | flagNullable, Col: collationNull}, nil
default:
return ctype{}, errJSONType(fn)
}
Expand Down
9 changes: 9 additions & 0 deletions go/vt/vtgate/evalengine/compiler_asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2319,6 +2319,15 @@ func (asm *assembler) Fn_JSON_KEYS(jp *json.Path) {
return 1
}, "FN JSON_KEYS (SP-1)")
} else {
if jp.ContainsWildcards() {
asm.emit(func(env *ExpressionEnv) int {
env.vm.err = errInvalidPathForTransform
return 1
}, "FN JSON_KEYS (SP-1), %q", jp.String())

return
}

asm.emit(func(env *ExpressionEnv) int {
doc := env.vm.stack[env.vm.sp-1]
if doc == nil {
Expand Down
36 changes: 18 additions & 18 deletions go/vt/vtgate/evalengine/fn_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,9 @@ func (call *builtinJSONExtract) compile(c *compiler) (ctype, error) {
nullable := doct.nullable()
skip := c.compileNullCheck1(doct)

// TODO: `*compiler.compileParseJSON` should handle `sqltypes.Null`` properly but
// we'll handle it here until all call sites are fixed.
var jt ctype
if doct.Type != sqltypes.Null {
jt, err = c.compileParseJSON("JSON_EXTRACT", doct, 1)
if err != nil {
return ctype{}, err
}
} else {
jt = ctype{Type: sqltypes.Null, Flag: flagNull | flagNullable, Col: collationNull}
jt, err := c.compileParseJSON("JSON_EXTRACT", doct, 1)
if err != nil {
return ctype{}, err
}

staticPaths := make([]staticPath, 0, len(call.Arguments[1:]))
Expand Down Expand Up @@ -411,6 +404,13 @@ func (call *builtinJSONContainsPath) compile(c *compiler) (ctype, error) {
return ctype{}, c.unsupported(call)
}

skip := c.compileNullCheck1(doct)

_, err = c.compileParseJSON("JSON_CONTAINS_PATH", doct, 1)
if err != nil {
return ctype{}, err
}

match, err := c.jsonExtractOneOrAll("JSON_CONTAINS_PATH", call.Arguments[1])
if err != nil {
return ctype{}, err
Expand All @@ -426,12 +426,10 @@ func (call *builtinJSONContainsPath) compile(c *compiler) (ctype, error) {
paths = append(paths, jp)
}

_, err = c.compileParseJSON("JSON_CONTAINS_PATH", doct, 1)
if err != nil {
return ctype{}, err
}

c.asm.Fn_JSON_CONTAINS_PATH(match, paths)

c.asm.jumpDestination(skip)

return ctype{Type: sqltypes.Int64, Col: collationNumeric, Flag: flagIsBoolean | flagNullable}, nil
}

Expand Down Expand Up @@ -522,6 +520,8 @@ func (call *builtinJSONKeys) compile(c *compiler) (ctype, error) {
return ctype{}, err
}

skip := c.compileNullCheck1(doc)

_, err = c.compileParseJSON("JSON_KEYS", doc, 1)
if err != nil {
return ctype{}, err
Expand All @@ -533,11 +533,11 @@ func (call *builtinJSONKeys) compile(c *compiler) (ctype, error) {
if err != nil {
return ctype{}, err
}
if jp.ContainsWildcards() {
return ctype{}, errInvalidPathForTransform
}
}

c.asm.Fn_JSON_KEYS(jp)

c.asm.jumpDestination(skip)

return ctype{Type: sqltypes.TypeJSON, Flag: flagNullable, Col: collationJSON}, nil
}
21 changes: 13 additions & 8 deletions go/vt/vtgate/evalengine/testcases/cases.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var Cases = []TestCase{
{Run: FnJSONKeys},
{Run: FnJSONExtract},
{Run: FnJSONContainsPath},
{Run: FnJSONUnquote},
{Run: JSONArray},
{Run: JSONObject},
{Run: CharsetConversionOperators},
Expand Down Expand Up @@ -180,21 +181,21 @@ var Cases = []TestCase{

func FnJSONKeys(yield Query) {
for _, obj := range inputJSONObjects {
yield(fmt.Sprintf("JSON_KEYS('%s')", obj), nil, false)
yield(fmt.Sprintf("JSON_KEYS(%s)", obj), nil, false)

for _, path1 := range inputJSONPaths {
yield(fmt.Sprintf("JSON_KEYS('%s', '%s')", obj, path1), nil, false)
yield(fmt.Sprintf("JSON_KEYS(%s, '%s')", obj, path1), nil, false)
}
}
}

func FnJSONExtract(yield Query) {
for _, obj := range inputJSONObjects {
for _, path1 := range inputJSONPaths {
yield(fmt.Sprintf("JSON_EXTRACT('%s', '%s')", obj, path1), nil, false)
yield(fmt.Sprintf("JSON_EXTRACT(%s, '%s')", obj, path1), nil, false)

for _, path2 := range inputJSONPaths {
yield(fmt.Sprintf("JSON_EXTRACT('%s', '%s', '%s')", obj, path1, path2), nil, false)
yield(fmt.Sprintf("JSON_EXTRACT(%s, '%s', '%s')", obj, path1, path2), nil, false)
}
}
}
Expand Down Expand Up @@ -235,17 +236,21 @@ func FnJSONExtract(yield Query) {
func FnJSONContainsPath(yield Query) {
for _, obj := range inputJSONObjects {
for _, path1 := range inputJSONPaths {
yield(fmt.Sprintf("JSON_CONTAINS_PATH('%s', 'one', '%s')", obj, path1), nil, false)
yield(fmt.Sprintf("JSON_CONTAINS_PATH('%s', 'all', '%s')", obj, path1), nil, false)
yield(fmt.Sprintf("JSON_CONTAINS_PATH(%s, 'one', '%s')", obj, path1), nil, false)
yield(fmt.Sprintf("JSON_CONTAINS_PATH(%s, 'all', '%s')", obj, path1), nil, false)

for _, path2 := range inputJSONPaths {
yield(fmt.Sprintf("JSON_CONTAINS_PATH('%s', 'one', '%s', '%s')", obj, path1, path2), nil, false)
yield(fmt.Sprintf("JSON_CONTAINS_PATH('%s', 'all', '%s', '%s')", obj, path1, path2), nil, false)
yield(fmt.Sprintf("JSON_CONTAINS_PATH(%s, 'one', '%s', '%s')", obj, path1, path2), nil, false)
yield(fmt.Sprintf("JSON_CONTAINS_PATH(%s, 'all', '%s', '%s')", obj, path1, path2), nil, false)
}
}
}
}

func FnJSONUnquote(yield Query) {
yield("JSON_UNQUOTE(NULL)", nil, false)
}

func JSONArray(yield Query) {
for _, a := range inputJSONPrimitives {
yield(fmt.Sprintf("JSON_ARRAY(%s)", a), nil, false)
Expand Down
15 changes: 8 additions & 7 deletions go/vt/vtgate/evalengine/testcases/inputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ import (
)

var inputJSONObjects = []string{
`[ { "a": 1 }, { "a": 2 } ]`,
`{ "a" : "foo", "b" : [ true, { "c" : 123, "c" : 456 } ] }`,
`{ "a" : "foo", "b" : [ true, { "c" : "123" } ] }`,
`{ "a" : "foo", "b" : [ true, { "c" : 123 } ] }`,
`{"a": 1, "b": 2, "c": {"d": 4}}`,
`["a", {"b": [true, false]}, [10, 20]]`,
`[10, 20, [30, 40]]`,
`'[ { "a": 1 }, { "a": 2 } ]'`,
`'{ "a" : "foo", "b" : [ true, { "c" : 123, "c" : 456 } ] }'`,
`'{ "a" : "foo", "b" : [ true, { "c" : "123" } ] }'`,
`'{ "a" : "foo", "b" : [ true, { "c" : 123 } ] }'`,
`'{"a": 1, "b": 2, "c": {"d": 4}}'`,
`'["a", {"b": [true, false]}, [10, 20]]'`,
`'[10, 20, [30, 40]]'`,
`NULL`,
}

var inputJSONPaths = []string{
Expand Down
Loading