Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expression: implement vectorized evaluation for builtinLocate2ArgsSig #12509

Merged
merged 13 commits into from
Oct 10, 2019
45 changes: 43 additions & 2 deletions expression/builtin_string_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -732,11 +732,52 @@ func (b *builtinLengthSig) vecEvalInt(input *chunk.Chunk, result *chunk.Column)
}

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

// vecEvalInt evals LOCATE(substr,str), non case-sensitive.
// See https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_locate
func (b *builtinLocate2ArgsSig) vecEvalInt(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
}
buf1, err := b.bufAllocator.get(types.ETString, n)
if err != nil {
return err
}
defer b.bufAllocator.put(buf1)
if err := b.args[1].VecEvalString(b.ctx, input, buf1); err != nil {
return err
}

result.ResizeInt64(n, false)
result.MergeNulls(buf, buf1)
i64s := result.Int64s()
for i := 0; i < n; i++ {
if result.IsNull(i) {
continue
}
subStr := buf.GetString(i)
str := buf1.GetString(i)

if int64(len([]rune(subStr))) == 0 {
qw4990 marked this conversation as resolved.
Show resolved Hide resolved
i64s[i] = 1
continue
}
slice := string([]rune(strings.ToLower(str)))
ret, idx := 0, strings.Index(slice, strings.ToLower(subStr))
qw4990 marked this conversation as resolved.
Show resolved Hide resolved
if idx != -1 {
ret = utf8.RuneCountInString(slice[:idx]) + 1
}
i64s[i] = int64(ret)
}
return nil
}

func (b *builtinBitLengthSig) vectorized() bool {
Expand Down
10 changes: 8 additions & 2 deletions expression/builtin_string_vec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@ var vecBuiltinStringCases = map[string][]vecExprBenchCase{
ast.Convert: {},
ast.Substring: {},
ast.SubstringIndex: {},
ast.Locate: {},
ast.Hex: {},
ast.Locate: {
{
retEvalType: types.ETInt,
childrenTypes: []types.EvalType{types.ETString, types.ETString},
geners: []dataGenerator{&randLenStrGener{0, 10}, &randLenStrGener{0, 20}},
qw4990 marked this conversation as resolved.
Show resolved Hide resolved
},
},
ast.Hex: {},
ast.Unhex: {
{retEvalType: types.ETString, childrenTypes: []types.EvalType{types.ETString}, geners: []dataGenerator{&randHexStrGener{10, 100}}},
},
Expand Down