Skip to content

Commit

Permalink
expression: implement vectorized evaluation for builtinSpaceSig (pi…
Browse files Browse the repository at this point in the history
  • Loading branch information
b41sh authored and qw4990 committed Sep 24, 2019
1 parent c2dd5ee commit a2ca3a3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
43 changes: 43 additions & 0 deletions expression/builtin_string_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"strings"
"unicode/utf8"

"github.com/pingcap/parser/mysql"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
)
Expand Down Expand Up @@ -255,3 +256,45 @@ func (b *builtinRightSig) vecEvalString(input *chunk.Chunk, result *chunk.Column
func (b *builtinRightSig) vectorized() bool {
return true
}

// vecEvalString evals a builtinSpaceSig.
// See https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_space
func (b *builtinSpaceSig) vecEvalString(input *chunk.Chunk, result *chunk.Column) error {
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.ReserveString(n)
nums := buf.Int64s()
for i := 0; i < n; i++ {
if buf.IsNull(i) {
result.AppendNull()
continue
}
num := nums[i]
if num < 0 {
num = 0
}
if uint64(num) > b.maxAllowedPacket {
b.ctx.GetSessionVars().StmtCtx.AppendWarning(errWarnAllowedPacketOverflowed.GenWithStackByArgs("space", b.maxAllowedPacket))
result.AppendNull()
continue
}
if num > mysql.MaxBlobWidth {
result.AppendNull()
continue
}
result.AppendString(strings.Repeat(" ", int(num)))
}
return nil
}

func (b *builtinSpaceSig) vectorized() bool {
return true
}
4 changes: 4 additions & 0 deletions expression/builtin_string_vec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ var vecBuiltinStringCases = map[string][]vecExprBenchCase{
ast.Left: {
{retEvalType: types.ETString, childrenTypes: []types.EvalType{types.ETString, types.ETInt}},
},
ast.Space: {
{retEvalType: types.ETString, childrenTypes: []types.EvalType{types.ETInt}, geners: []dataGenerator{&rangeInt64Gener{-10, 2000}}},
{retEvalType: types.ETString, childrenTypes: []types.EvalType{types.ETInt}, geners: []dataGenerator{&rangeInt64Gener{5, 10}}},
},
}

func (s *testEvaluatorSuite) TestVectorizedBuiltinStringEvalOneVec(c *C) {
Expand Down

0 comments on commit a2ca3a3

Please sign in to comment.