Skip to content

Commit

Permalink
expression: implement vectorized evaluation for `builtinSubstring2Arg…
Browse files Browse the repository at this point in the history
…sSig` (pingcap#12949)
  • Loading branch information
shihongzhi authored and qw4990 committed Oct 31, 2019
1 parent 0fae1e1 commit 64a4797
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
49 changes: 47 additions & 2 deletions expression/builtin_string_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -747,11 +747,56 @@ func (b *builtinSubstringBinary2ArgsSig) vecEvalString(input *chunk.Chunk, resul
}

func (b *builtinSubstring2ArgsSig) vectorized() bool {
return false
return true
}

// evalString evals SUBSTR(str,pos), SUBSTR(str FROM pos), SUBSTR() is a synonym for SUBSTRING().
// See https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_substr
func (b *builtinSubstring2ArgsSig) 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
}

buf2, err := b.bufAllocator.get(types.ETInt, n)
if err != nil {
return err
}
defer b.bufAllocator.put(buf2)
if err := b.args[1].VecEvalInt(b.ctx, input, buf2); err != nil {
return err
}

result.ReserveString(n)
nums := buf2.Int64s()
for i := 0; i < n; i++ {
if buf.IsNull(i) || buf2.IsNull(i) {
result.AppendNull()
continue
}

str := buf.GetString(i)
pos := nums[i]

runes := []rune(str)
length := int64(len(runes))
if pos < 0 {
pos += length
} else {
pos--
}
if pos > length || pos < 0 {
pos = length
}
result.AppendString(string(runes[pos:]))
}

return nil
}

func (b *builtinTrim2ArgsSig) vectorized() bool {
Expand Down
5 changes: 5 additions & 0 deletions expression/builtin_string_vec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ var vecBuiltinStringCases = map[string][]vecExprBenchCase{
ast.ConcatWS: {},
ast.Convert: {},
ast.Substring: {
{
retEvalType: types.ETString,
childrenTypes: []types.EvalType{types.ETString, types.ETInt},
geners: []dataGenerator{&randLenStrGener{0, 20}, &rangeInt64Gener{-25, 25}},
},
{
retEvalType: types.ETString,
childrenTypes: []types.EvalType{types.ETString, types.ETInt, types.ETInt},
Expand Down

0 comments on commit 64a4797

Please sign in to comment.