From 3a074d4bf8f1adfbb4683e5d7d7faef4f024aa40 Mon Sep 17 00:00:00 2001 From: Eugene Kalinin Date: Fri, 1 Nov 2019 11:01:06 +0300 Subject: [PATCH] expression: implement vectorized evaluation for builtinCastStringAsTimeSig (#12947) --- expression/builtin_cast_vec.go | 37 +++++++++++++++++++++++++++-- expression/builtin_cast_vec_test.go | 6 +++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/expression/builtin_cast_vec.go b/expression/builtin_cast_vec.go index 8e17ac386c..72b9f68076 100644 --- a/expression/builtin_cast_vec.go +++ b/expression/builtin_cast_vec.go @@ -1092,11 +1092,44 @@ func (b *builtinCastStringAsDecimalSig) vecEvalDecimal(input *chunk.Chunk, resul } func (b *builtinCastStringAsTimeSig) vectorized() bool { - return false + return true } func (b *builtinCastStringAsTimeSig) vecEvalTime(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.ResizeTime(n, false) + result.MergeNulls(buf) + times := result.Times() + stmtCtx := b.ctx.GetSessionVars().StmtCtx + fsp := int8(b.tp.Decimal) + for i := 0; i < n; i++ { + if result.IsNull(i) { + continue + } + tm, err := types.ParseTime(stmtCtx, buf.GetString(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 *builtinCastDecimalAsIntSig) vectorized() bool { diff --git a/expression/builtin_cast_vec_test.go b/expression/builtin_cast_vec_test.go index 34060d9ff8..f3fe97440c 100644 --- a/expression/builtin_cast_vec_test.go +++ b/expression/builtin_cast_vec_test.go @@ -59,6 +59,12 @@ var vecBuiltinCastCases = map[string][]vecExprBenchCase{ {retEvalType: types.ETDatetime, childrenTypes: []types.EvalType{types.ETReal}}, {retEvalType: types.ETDatetime, childrenTypes: []types.EvalType{types.ETDecimal}}, {retEvalType: types.ETDatetime, childrenTypes: []types.EvalType{types.ETInt}}, + {retEvalType: types.ETDatetime, childrenTypes: []types.EvalType{types.ETString}, + geners: []dataGenerator{ + &dataTimeStrGener{}, + &timeStrGener{}, + &dataStrGener{}, + }}, }, }