From 8f6e0340feb2372825932e9cd63544ab61d9bbd2 Mon Sep 17 00:00:00 2001 From: Eugene Kalinin Date: Tue, 22 Oct 2019 15:23:29 +0300 Subject: [PATCH] expression: implement vectorized evaluation for builtinCastIntAsTimeSig (#12842) --- expression/builtin_cast_vec.go | 38 +++++++++++++++++++++++++++-- expression/builtin_cast_vec_test.go | 1 + 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/expression/builtin_cast_vec.go b/expression/builtin_cast_vec.go index e6c5fb8758bee..17ee5540be4bd 100644 --- a/expression/builtin_cast_vec.go +++ b/expression/builtin_cast_vec.go @@ -310,11 +310,45 @@ func (b *builtinCastDurationAsIntSig) vecEvalInt(input *chunk.Chunk, result *chu } func (b *builtinCastIntAsTimeSig) vectorized() bool { - return false + return true } func (b *builtinCastIntAsTimeSig) vecEvalTime(input *chunk.Chunk, result *chunk.Column) error { - return errors.Errorf("not implemented") + n := input.NumRows() + buf, err := b.bufAllocator.get(types.ETInt, n) + if err != nil { + return err + } + defer b.bufAllocator.put(buf) + if err := b.args[0].VecEvalInt(b.ctx, input, buf); err != nil { + return err + } + + result.ResizeTime(n, false) + result.MergeNulls(buf) + times := result.Times() + i64s := buf.Int64s() + stmt := b.ctx.GetSessionVars().StmtCtx + fsp := int8(b.tp.Decimal) + for i := 0; i < n; i++ { + if buf.IsNull(i) { + continue + } + tm, err := types.ParseTimeFromNum(stmt, i64s[i], b.tp.Tp, fsp) + if err != nil { + if err = handleInvalidTimeError(b.ctx, err); err != nil { + return err + } + result.SetNull(i, true) + continue + } + times[i] = tm + if b.tp.Tp == mysql.TypeDate { + // Truncate hh:mm:ss part if the type is Date. + times[i].Time = types.FromDate(tm.Time.Year(), tm.Time.Month(), tm.Time.Day(), 0, 0, 0, 0) + } + } + return nil } func (b *builtinCastRealAsJSONSig) vectorized() bool { diff --git a/expression/builtin_cast_vec_test.go b/expression/builtin_cast_vec_test.go index c57abe7cce48f..b5361bfeeb7ed 100644 --- a/expression/builtin_cast_vec_test.go +++ b/expression/builtin_cast_vec_test.go @@ -55,6 +55,7 @@ var vecBuiltinCastCases = map[string][]vecExprBenchCase{ {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}}, + {retEvalType: types.ETDatetime, childrenTypes: []types.EvalType{types.ETInt}}, }, }