diff --git a/expression/builtin_cast_vec.go b/expression/builtin_cast_vec.go index d0e8e12bb8e00..3cad0cfd1d393 100644 --- a/expression/builtin_cast_vec.go +++ b/expression/builtin_cast_vec.go @@ -19,6 +19,7 @@ import ( "github.com/pingcap/errors" "github.com/pingcap/parser/mysql" "github.com/pingcap/tidb/types" + "github.com/pingcap/tidb/types/json" "github.com/pingcap/tidb/util/chunk" ) @@ -293,11 +294,30 @@ func (b *builtinCastIntAsTimeSig) vecEvalTime(input *chunk.Chunk, result *chunk. } func (b *builtinCastRealAsJSONSig) vectorized() bool { - return false + return true } func (b *builtinCastRealAsJSONSig) vecEvalJSON(input *chunk.Chunk, result *chunk.Column) error { - return errors.Errorf("not implemented") + n := input.NumRows() + buf, err := b.bufAllocator.get(types.ETReal, n) + if err != nil { + return err + } + defer b.bufAllocator.put(buf) + if err := b.args[0].VecEvalReal(b.ctx, input, buf); err != nil { + return err + } + f64s := buf.Float64s() + result.ReserveJSON(n) + for i := 0; i < n; i++ { + // FIXME: `select json_type(cast(1111.11 as json))` should return `DECIMAL`, we return `DOUBLE` now.``` + if buf.IsNull(i) { + result.AppendNull() + } else { + result.AppendJSON(json.CreateBinary(f64s[i])) + } + } + return nil } func (b *builtinCastJSONAsRealSig) vectorized() bool { diff --git a/expression/builtin_cast_vec_test.go b/expression/builtin_cast_vec_test.go index 5035422f11dbd..c60dc3d4ad589 100644 --- a/expression/builtin_cast_vec_test.go +++ b/expression/builtin_cast_vec_test.go @@ -45,6 +45,8 @@ var vecBuiltinCastCases = map[string][]vecExprBenchCase{ {retEvalType: types.ETString, childrenTypes: []types.EvalType{types.ETReal}}, {retEvalType: types.ETString, childrenTypes: []types.EvalType{types.ETJson}}, {retEvalType: types.ETString, childrenTypes: []types.EvalType{types.ETDecimal}}, + + {retEvalType: types.ETJson, childrenTypes: []types.EvalType{types.ETReal}}, }, }