From 1a1736147b90b88119af3128c0149613c3b0e0e7 Mon Sep 17 00:00:00 2001 From: tsthght <781181214@qq.com> Date: Thu, 17 Oct 2019 21:05:56 +0800 Subject: [PATCH 1/3] expression: implement vectorized evaluation for 'builtinCastDecimalAsJSONSig' --- expression/builtin_cast_vec.go | 30 +++++++++++++++++++++++++++-- expression/builtin_cast_vec_test.go | 1 + 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/expression/builtin_cast_vec.go b/expression/builtin_cast_vec.go index 2e8f7a87bbfb2..725e0ef4cddb1 100644 --- a/expression/builtin_cast_vec.go +++ b/expression/builtin_cast_vec.go @@ -1005,11 +1005,37 @@ func (b *builtinCastJSONAsDurationSig) vecEvalDuration(input *chunk.Chunk, resul } func (b *builtinCastDecimalAsJSONSig) vectorized() bool { - return false + return true } func (b *builtinCastDecimalAsJSONSig) vecEvalJSON(input *chunk.Chunk, result *chunk.Column) error { - return errors.Errorf("not implemented") + n := input.NumRows() + buf, err := b.bufAllocator.get(types.ETDecimal, n) + if err != nil { + return err + } + defer b.bufAllocator.put(buf) + if err := b.args[0].VecEvalDecimal(b.ctx, input, buf); err != nil { + return err + } + + result.ReserveJSON(n) + f64s := buf.Decimals() + var f float64 + for i := 0; i < n; i++ { + if buf.IsNull(i) { + result.AppendJSON(json.BinaryJSON{}) + result.SetNull(i, true) + continue + } + // FIXME: `select json_type(cast(1111.11 as json))` should return `DECIMAL`, we return `DOUBLE` now. + f, err = f64s[i].ToFloat64() + if err != nil { + return err + } + result.AppendJSON(json.CreateBinary(f)) + } + return nil } func (b *builtinCastDurationAsJSONSig) vectorized() bool { diff --git a/expression/builtin_cast_vec_test.go b/expression/builtin_cast_vec_test.go index 484c4dccdc604..6496b1634c602 100644 --- a/expression/builtin_cast_vec_test.go +++ b/expression/builtin_cast_vec_test.go @@ -51,6 +51,7 @@ var vecBuiltinCastCases = map[string][]vecExprBenchCase{ {retEvalType: types.ETJson, childrenTypes: []types.EvalType{types.ETInt}}, {retEvalType: types.ETJson, childrenTypes: []types.EvalType{types.ETReal}}, {retEvalType: types.ETJson, childrenTypes: []types.EvalType{types.ETJson}}, + {retEvalType: types.ETJson, childrenTypes: []types.EvalType{types.ETDecimal}}, }, } From 955af4d2d35200c7c031a10388d155e9fb76a366 Mon Sep 17 00:00:00 2001 From: tsthght <781181214@qq.com> Date: Thu, 17 Oct 2019 21:06:29 +0800 Subject: [PATCH 2/3] refine --- expression/builtin_cast_vec.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/expression/builtin_cast_vec.go b/expression/builtin_cast_vec.go index 725e0ef4cddb1..52da185c3fd98 100644 --- a/expression/builtin_cast_vec.go +++ b/expression/builtin_cast_vec.go @@ -1015,7 +1015,7 @@ func (b *builtinCastDecimalAsJSONSig) vecEvalJSON(input *chunk.Chunk, result *ch return err } defer b.bufAllocator.put(buf) - if err := b.args[0].VecEvalDecimal(b.ctx, input, buf); err != nil { + if err = b.args[0].VecEvalDecimal(b.ctx, input, buf); err != nil { return err } From a02ca93be4b115086d5ad7bd89d19d0f0c648124 Mon Sep 17 00:00:00 2001 From: tsthght <781181214@qq.com> Date: Thu, 17 Oct 2019 23:07:28 +0800 Subject: [PATCH 3/3] fix an error --- expression/builtin_cast_vec.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/expression/builtin_cast_vec.go b/expression/builtin_cast_vec.go index 52da185c3fd98..4b8dbcfcfed21 100644 --- a/expression/builtin_cast_vec.go +++ b/expression/builtin_cast_vec.go @@ -1024,8 +1024,7 @@ func (b *builtinCastDecimalAsJSONSig) vecEvalJSON(input *chunk.Chunk, result *ch var f float64 for i := 0; i < n; i++ { if buf.IsNull(i) { - result.AppendJSON(json.BinaryJSON{}) - result.SetNull(i, true) + result.AppendNull() continue } // FIXME: `select json_type(cast(1111.11 as json))` should return `DECIMAL`, we return `DOUBLE` now.