From b22ff44cb6b9d8e6e011714686edb302748927ca Mon Sep 17 00:00:00 2001 From: tsthght <781181214@qq.com> Date: Wed, 9 Oct 2019 20:35:44 +0800 Subject: [PATCH] expression: implement vectorized evaluation for 'builtinCastStringAsStringSig' (#12538) --- expression/builtin_cast_vec.go | 37 +++++++++++++++++++++++++++-- expression/builtin_cast_vec_test.go | 1 + 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/expression/builtin_cast_vec.go b/expression/builtin_cast_vec.go index 5c9483fe37dab..889576e7653fb 100644 --- a/expression/builtin_cast_vec.go +++ b/expression/builtin_cast_vec.go @@ -792,11 +792,44 @@ func (b *builtinCastDecimalAsDurationSig) vecEvalDuration(input *chunk.Chunk, re } func (b *builtinCastStringAsStringSig) vectorized() bool { - return false + return true } func (b *builtinCastStringAsStringSig) vecEvalString(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 + } + + var res string + var isNull bool + sc := b.ctx.GetSessionVars().StmtCtx + result.ReserveString(n) + for i := 0; i < n; i++ { + if buf.IsNull(i) { + result.AppendNull() + continue + } + res, err = types.ProduceStrWithSpecifiedTp(buf.GetString(i), b.tp, sc, false) + if err != nil { + return err + } + res, isNull, err = padZeroForBinaryType(res, b.tp, b.ctx) + if err != nil { + return err + } + if isNull { + result.AppendNull() + continue + } + result.AppendString(res) + } + return nil } func (b *builtinCastJSONAsDurationSig) vectorized() bool { diff --git a/expression/builtin_cast_vec_test.go b/expression/builtin_cast_vec_test.go index 1c216583e4cc2..35df967686d4d 100644 --- a/expression/builtin_cast_vec_test.go +++ b/expression/builtin_cast_vec_test.go @@ -37,6 +37,7 @@ var vecBuiltinCastCases = map[string][]vecExprBenchCase{ }, {retEvalType: types.ETString, childrenTypes: []types.EvalType{types.ETInt}}, {retEvalType: types.ETDecimal, childrenTypes: []types.EvalType{types.ETDatetime}}, + {retEvalType: types.ETString, childrenTypes: []types.EvalType{types.ETString}}, {retEvalType: types.ETString, childrenTypes: []types.EvalType{types.ETDuration}}, {retEvalType: types.ETString, childrenTypes: []types.EvalType{types.ETDatetime}}, {retEvalType: types.ETString, childrenTypes: []types.EvalType{types.ETTimestamp}},