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: implement vectorized evaluation for builtinJSONObjectSig #12663

Merged
merged 17 commits into from
Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from 10 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
71 changes: 69 additions & 2 deletions expression/builtin_json_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package expression

import (
"github.com/pingcap/errors"
"github.com/pingcap/parser/ast"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/types/json"
"github.com/pingcap/tidb/util/chunk"
Expand Down Expand Up @@ -154,11 +155,77 @@ func (b *builtinJSONSetSig) vecEvalJSON(input *chunk.Chunk, result *chunk.Column
}

func (b *builtinJSONObjectSig) vectorized() bool {
return false
return true
}

func (b *builtinJSONObjectSig) vecEvalJSON(input *chunk.Chunk, result *chunk.Column) error {
return errors.Errorf("not implemented")
nr := input.NumRows()
if len(b.args)&1 == 1 {
err := ErrIncorrectParameterCount.GenWithStackByArgs(ast.JSONObject)
return err
}

jsons := make([]map[string]interface{}, nr)
for i := 0; i < nr; i++ {
jsons[i] = make(map[string]interface{}, len(b.args)>>1)
}

argBuffers := make([]*chunk.Column, len(b.args))
var err error
for i := 0; i < len(b.args); i++ {
if i&1 == 0 {
if argBuffers[i], err = b.bufAllocator.get(types.ETString, nr); err != nil {
return err
}
defer func(buf *chunk.Column) {
b.bufAllocator.put(buf)
}(argBuffers[i])

if err = b.args[i].VecEvalString(b.ctx, input, argBuffers[i]); err != nil {
js00070 marked this conversation as resolved.
Show resolved Hide resolved
return err
}
} else {
if argBuffers[i], err = b.bufAllocator.get(types.ETJson, nr); err != nil {
return err
}
defer func(buf *chunk.Column) {
b.bufAllocator.put(buf)
}(argBuffers[i])

if err = b.args[i].VecEvalJSON(b.ctx, input, argBuffers[i]); err != nil {
return err
}
}
}

result.ReserveJSON(nr)
for i := 0; i < len(b.args); i++ {
if i&1 == 1 {
keyCol := argBuffers[i-1]
valueCol := argBuffers[i]

var key string
var value json.BinaryJSON
for j := 0; j < nr; j++ {
if keyCol.IsNull(j) {
err := errors.New("JSON documents may not contain NULL member names")
return err
}
key = keyCol.GetString(j)
if valueCol.IsNull(j) {
value = json.CreateBinary(nil)
} else {
value = valueCol.GetJSON(j)
}
jsons[j][key] = value
}
}
}

for i := 0; i < nr; i++ {
result.AppendJSON(json.CreateBinary(jsons[i]))
}
return nil
}

func (b *builtinJSONArrayInsertSig) vectorized() bool {
Expand Down
50 changes: 41 additions & 9 deletions expression/builtin_json_vec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,47 @@ var vecBuiltinJSONCases = map[string][]vecExprBenchCase{
ast.JSONArray: {},
ast.JSONArrayInsert: {},
ast.JSONContains: {},
ast.JSONObject: {},
ast.JSONSet: {},
ast.JSONSearch: {},
ast.JSONReplace: {},
ast.JSONDepth: {{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETJson}}},
ast.JSONUnquote: {},
ast.JSONRemove: {},
ast.JSONMerge: {},
ast.JSONInsert: {},
ast.JSONObject: {
{
retEvalType: types.ETJson,
childrenTypes: []types.EvalType{
types.ETString, types.ETJson,
types.ETString, types.ETJson,
types.ETString, types.ETJson,
types.ETString, types.ETJson,
types.ETString, types.ETJson,
types.ETString, types.ETJson,
types.ETString, types.ETJson,
types.ETString, types.ETJson,
types.ETString, types.ETJson,
types.ETString, types.ETJson,
types.ETString, types.ETJson,
types.ETString, types.ETJson,
},
geners: []dataGenerator{
&randLenStrGener{10, 20}, nil,
&randLenStrGener{10, 20}, nil,
&randLenStrGener{10, 20}, nil,
&randLenStrGener{10, 20}, nil,
&randLenStrGener{10, 20}, nil,
&randLenStrGener{10, 20}, nil,
&randLenStrGener{10, 20}, nil,
&randLenStrGener{10, 20}, nil,
&randLenStrGener{10, 20}, nil,
&randLenStrGener{10, 20}, nil,
&randLenStrGener{10, 20}, nil,
&randLenStrGener{10, 20}, nil,
},
},
},
ast.JSONSet: {},
ast.JSONSearch: {},
ast.JSONReplace: {},
ast.JSONDepth: {{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETJson}}},
ast.JSONUnquote: {},
ast.JSONRemove: {},
ast.JSONMerge: {},
ast.JSONInsert: {},
ast.JSONQuote: {
{retEvalType: types.ETString, childrenTypes: []types.EvalType{types.ETJson}},
},
Expand Down