expression: implement vectorized evaluation for 'builtinCastStringAsJSONSig' (#12802)

This commit is contained in:
tsthght
2019-10-18 17:30:49 +08:00
committed by pingcap-github-bot
parent 79e3e7521a
commit 286047be6b
3 changed files with 47 additions and 2 deletions

View File

@ -252,6 +252,16 @@ func (g *defaultGener) gen() interface{} {
return nil
}
type jsonStringGener struct{}
func (g *jsonStringGener) gen() interface{} {
j := new(json.BinaryJSON)
if err := j.UnmarshalJSON([]byte(fmt.Sprintf(`{"key":%v}`, rand.Int()))); err != nil {
panic(err)
}
return j.String()
}
type rangeDurationGener struct {
nullRation float64
}

View File

@ -513,11 +513,45 @@ func (b *builtinCastTimeAsRealSig) vecEvalReal(input *chunk.Chunk, result *chunk
}
func (b *builtinCastStringAsJSONSig) vectorized() bool {
return false
return true
}
func (b *builtinCastStringAsJSONSig) vecEvalJSON(input *chunk.Chunk, result *chunk.Column) error {
return errors.Errorf("not implemented")
n := input.NumRows()
buf, err := b.bufAllocator.get(types.ETString, n)
if err != nil {
return err
}
defer b.bufAllocator.put(buf)
if err = b.args[0].VecEvalString(b.ctx, input, buf); err != nil {
return err
}
result.ReserveJSON(n)
hasParse := mysql.HasParseToJSONFlag(b.tp.Flag)
if hasParse {
var res json.BinaryJSON
for i := 0; i < n; i++ {
if buf.IsNull(i) {
result.AppendNull()
continue
}
res, err = json.ParseBinaryFromString(buf.GetString(i))
if err != nil {
return err
}
result.AppendJSON(res)
}
} else {
for i := 0; i < n; i++ {
if buf.IsNull(i) {
result.AppendNull()
continue
}
result.AppendJSON(json.CreateBinary(buf.GetString(i)))
}
}
return nil
}
func (b *builtinCastRealAsDecimalSig) vectorized() bool {

View File

@ -52,6 +52,7 @@ var vecBuiltinCastCases = map[string][]vecExprBenchCase{
{retEvalType: types.ETJson, childrenTypes: []types.EvalType{types.ETReal}},
{retEvalType: types.ETJson, childrenTypes: []types.EvalType{types.ETDatetime}},
{retEvalType: types.ETJson, childrenTypes: []types.EvalType{types.ETJson}},
{retEvalType: types.ETJson, childrenTypes: []types.EvalType{types.ETString}, geners: []dataGenerator{&jsonStringGener{}}},
{retEvalType: types.ETJson, childrenTypes: []types.EvalType{types.ETDecimal}},
},
}