From a5ff9b41b8d828056c55f0f5410e673c652594a6 Mon Sep 17 00:00:00 2001 From: polyrabbit Date: Thu, 26 Sep 2019 16:59:07 +0800 Subject: [PATCH] Make builtinCastTimeAsRealSig support vectorized evaluation --- expression/builtin_cast_vec.go | 34 +++++++++++++++++++++++++++-- expression/builtin_cast_vec_test.go | 1 + 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/expression/builtin_cast_vec.go b/expression/builtin_cast_vec.go index 2fa60f5ea5df0..762b021642f41 100644 --- a/expression/builtin_cast_vec.go +++ b/expression/builtin_cast_vec.go @@ -258,11 +258,41 @@ func (b *builtinCastRealAsIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.C } func (b *builtinCastTimeAsRealSig) vectorized() bool { - return false + return true } func (b *builtinCastTimeAsRealSig) vecEvalReal(input *chunk.Chunk, result *chunk.Column) error { - return errors.Errorf("not implemented") + n := input.NumRows() + buf, err := b.bufAllocator.get(types.ETDatetime, n) + if err != nil { + return err + } + defer b.bufAllocator.put(buf) + if err := b.args[0].VecEvalTime(b.ctx, input, buf); err != nil { + return err + } + result.ResizeFloat64(n, false) + result.MergeNulls(buf) + times := buf.Times() + f64s := result.Float64s() + for i := 0; i < n; i++ { + if result.IsNull(i) { + continue + } + f64, err := times[i].ToNumber().ToFloat64() + if err != nil { + if types.ErrOverflow.Equal(err) { + err = b.ctx.GetSessionVars().StmtCtx.HandleOverflow(err, err) + } + if err != nil { + return err + } + result.SetNull(i, true) + continue + } + f64s[i] = f64 + } + return nil } func (b *builtinCastStringAsJSONSig) vectorized() bool { diff --git a/expression/builtin_cast_vec_test.go b/expression/builtin_cast_vec_test.go index bc7f517808432..82005ce27a158 100644 --- a/expression/builtin_cast_vec_test.go +++ b/expression/builtin_cast_vec_test.go @@ -28,6 +28,7 @@ var vecBuiltinCastCases = map[string][]vecExprBenchCase{ {retEvalType: types.ETDuration, childrenTypes: []types.EvalType{types.ETInt}, geners: []dataGenerator{new(randDurInt)}}, {retEvalType: types.ETReal, childrenTypes: []types.EvalType{types.ETReal}}, {retEvalType: types.ETReal, childrenTypes: []types.EvalType{types.ETDecimal}}, + {retEvalType: types.ETReal, childrenTypes: []types.EvalType{types.ETDatetime}}, }, }