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 builtinLocate3ArgsSig #12510

Merged
merged 12 commits into from
Oct 14, 2019
59 changes: 57 additions & 2 deletions expression/builtin_string_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,66 @@ func (b *builtinConcatSig) vecEvalString(input *chunk.Chunk, result *chunk.Colum
}

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

// vecEvalInt evals LOCATE(substr,str,pos), non case-sensitive.
// See https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_locate
func (b *builtinLocate3ArgsSig) 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
}
// store positions in result
if err := b.args[2].VecEvalInt(b.ctx, input, result); err != nil {
return err
}

result.MergeNulls(buf, buf1)
i64s := result.Int64s()
for i := 0; i < n; i++ {
if result.IsNull(i) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider this case, b.args[2] is null but the result is not null.

mysql> SELECT LOCATE('bar', 'foobarbar', NULL);
+----------------------------------+
| LOCATE('bar', 'foobarbar', NULL) |
+----------------------------------+
|                                0 |
+----------------------------------+
1 row in set (0.00 sec)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My test result is correct

mysql> SELECT LOCATE('bar', 'foobarbar', NULL);
+----------------------------------+
| LOCATE('bar', 'foobarbar', NULL) |
+----------------------------------+
|                             NULL |
+----------------------------------+
1 row in set (0.00 sec)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can args[0] and args[1] be null?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, any args is null will result in null

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider this case, b.args[2] is null but the result is not null.

mysql> SELECT LOCATE('bar', 'foobarbar', NULL);
+----------------------------------+
| LOCATE('bar', 'foobarbar', NULL) |
+----------------------------------+
|                                0 |
+----------------------------------+
1 row in set (0.00 sec)

In MySQL 8.x, this SQL returns NULL.

continue
}
subStr := buf.GetString(i)
str := buf1.GetString(i)
pos := i64s[i]

// Transfer the argument which starts from 1 to real index which starts from 0.
pos--
strLen := int64(len([]rune(str)))
subStrLen := int64(len([]rune(subStr)))
if pos < 0 || pos > strLen-subStrLen {
i64s[i] = 0
continue
} else if subStrLen == 0 {
i64s[i] = pos + 1
continue
}
slice := string([]rune(str)[pos:])
subStr = strings.ToLower(subStr)
slice = strings.ToLower(slice)
idx := strings.Index(slice, subStr)
if idx != -1 {
i64s[i] = pos + int64(utf8.RuneCountInString(slice[:idx])) + 1
continue
}
i64s[i] = 0
}
return nil
}

func (b *builtinHexStrArgSig) vectorized() bool {
Expand Down
10 changes: 10 additions & 0 deletions expression/builtin_string_vec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ var vecBuiltinStringCases = map[string][]vecExprBenchCase{
childrenTypes: []types.EvalType{types.ETString, types.ETString},
geners: []dataGenerator{&randLenStrGener{1, 2}, &randLenStrGener{0, 20}},
},
{
retEvalType: types.ETInt,
childrenTypes: []types.EvalType{types.ETString, types.ETString, types.ETInt},
geners: []dataGenerator{&randLenStrGener{0, 10}, &randLenStrGener{0, 20}, &rangeInt64Gener{-10, 20}},
},
{
retEvalType: types.ETInt,
childrenTypes: []types.EvalType{types.ETString, types.ETString, types.ETInt},
geners: []dataGenerator{&randLenStrGener{1, 2}, &randLenStrGener{0, 10}, &rangeInt64Gener{0, 8}},
},
},
ast.Hex: {},
ast.Unhex: {
Expand Down