Skip to content

Commit

Permalink
expression: implement vectorized evaluation for `builtinLocateBinary3…
Browse files Browse the repository at this point in the history
…ArgsSig` (pingcap#13809)
  • Loading branch information
b41sh authored and qw4990 committed Dec 4, 2019
1 parent 5c4a9ee commit d2f23cd
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
55 changes: 53 additions & 2 deletions expression/builtin_string_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -1175,11 +1175,62 @@ func (b *builtinLocateBinary2ArgsSig) vecEvalInt(input *chunk.Chunk, result *chu
}

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

// vecEvalInt evals LOCATE(substr,str,pos), case-sensitive.
// See https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_locate
func (b *builtinLocateBinary3ArgsSig) vecEvalInt(input *chunk.Chunk, result *chunk.Column) error {
return errors.Errorf("not implemented")
n := input.NumRows()
buf0, err := b.bufAllocator.get(types.ETString, n)
if err != nil {
return err
}
defer b.bufAllocator.put(buf0)
if err := b.args[0].VecEvalString(b.ctx, input, buf0); 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
}
// store positions in result
if err := b.args[2].VecEvalInt(b.ctx, input, result); err != nil {
return err
}

result.MergeNulls(buf0, buf1)
i64s := result.Int64s()
for i := 0; i < n; i++ {
if result.IsNull(i) {
continue
}
pos := i64s[i]
// Transfer the argument which starts from 1 to real index which starts from 0.
pos--
subStr := buf0.GetString(i)
str := buf1.GetString(i)
subStrLen := len(subStr)
if pos < 0 || pos > int64(len(str)-subStrLen) {
i64s[i] = 0
continue
} else if subStrLen == 0 {
i64s[i] = pos + 1
continue
}
slice := str[pos:]
idx := strings.Index(slice, subStr)
if idx != -1 {
i64s[i] = pos + int64(idx) + 1
continue
}
i64s[i] = 0
}
return nil
}

func (b *builtinExportSet4ArgSig) vectorized() bool {
Expand Down
12 changes: 12 additions & 0 deletions expression/builtin_string_vec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,18 @@ var vecBuiltinStringCases = map[string][]vecExprBenchCase{
childrenFieldTypes: []*types.FieldType{{Tp: mysql.TypeString, Flag: mysql.BinaryFlag, Collate: charset.CollationBin}, {Tp: mysql.TypeString, Flag: mysql.BinaryFlag, Collate: charset.CollationBin}},
geners: []dataGenerator{&selectStringGener{[]string{"01", "10", "001", "110", "0001", "1110"}}, &selectStringGener{[]string{"010010001000010", "101101110111101"}}},
},
{
retEvalType: types.ETInt,
childrenTypes: []types.EvalType{types.ETString, types.ETString, types.ETInt},
childrenFieldTypes: []*types.FieldType{{Tp: mysql.TypeString, Flag: mysql.BinaryFlag, Collate: charset.CollationBin}, {Tp: mysql.TypeString, Flag: mysql.BinaryFlag, Collate: charset.CollationBin}, {Tp: mysql.TypeInt24}},
geners: []dataGenerator{&randLenStrGener{0, 10}, &randLenStrGener{0, 20}, &rangeInt64Gener{-10, 20}},
},
{
retEvalType: types.ETInt,
childrenTypes: []types.EvalType{types.ETString, types.ETString, types.ETInt},
childrenFieldTypes: []*types.FieldType{{Tp: mysql.TypeString, Flag: mysql.BinaryFlag, Collate: charset.CollationBin}, {Tp: mysql.TypeString, Flag: mysql.BinaryFlag, Collate: charset.CollationBin}, {Tp: mysql.TypeInt24}},
geners: []dataGenerator{&selectStringGener{[]string{"01", "10", "001", "110", "0001", "1110"}}, &selectStringGener{[]string{"010010001000010", "101101110111101"}}, &rangeInt64Gener{-10, 20}},
},
},
ast.Hex: {
{retEvalType: types.ETString, childrenTypes: []types.EvalType{types.ETString}, geners: []dataGenerator{&randHexStrGener{10, 100}}},
Expand Down

0 comments on commit d2f23cd

Please sign in to comment.