Skip to content

Commit

Permalink
expression: implement vectorized evaluation for builtinLpadBinarySig (
Browse files Browse the repository at this point in the history
  • Loading branch information
mmyj authored and qw4990 committed Nov 18, 2019
1 parent aa8f6df commit 0cc3dad
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
65 changes: 63 additions & 2 deletions expression/builtin_string_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,11 +739,72 @@ func (b *builtinASCIISig) vecEvalInt(input *chunk.Chunk, result *chunk.Column) e
}

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

// vecEvalString evals LPAD(str,len,padstr).
// See https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_lpad
func (b *builtinLpadBinarySig) vecEvalString(input *chunk.Chunk, result *chunk.Column) error {
return errors.Errorf("not implemented")
n := input.NumRows()
strBuf, err := b.bufAllocator.get(types.ETString, n)
if err != nil {
return err
}
defer b.bufAllocator.put(strBuf)
if err := b.args[0].VecEvalString(b.ctx, input, strBuf); err != nil {
return err
}
lenBuf, err := b.bufAllocator.get(types.ETInt, n)
if err != nil {
return err
}
defer b.bufAllocator.put(lenBuf)
if err := b.args[1].VecEvalInt(b.ctx, input, lenBuf); err != nil {
return err
}
padBuf, err := b.bufAllocator.get(types.ETString, n)
if err != nil {
return err
}
defer b.bufAllocator.put(padBuf)
if err := b.args[2].VecEvalString(b.ctx, input, padBuf); err != nil {
return err
}

result.ReserveString(n)
i64s := lenBuf.Int64s()
lenBuf.MergeNulls(strBuf)
for i := 0; i < n; i++ {
if lenBuf.IsNull(i) {
result.AppendNull()
continue
}
targetLength := int(i64s[i])
if uint64(targetLength) > b.maxAllowedPacket {
b.ctx.GetSessionVars().StmtCtx.AppendWarning(errWarnAllowedPacketOverflowed.GenWithStackByArgs("lpad", b.maxAllowedPacket))
result.AppendNull()
continue
}

if padBuf.IsNull(i) {
result.AppendNull()
continue
}
str := strBuf.GetString(i)
strLength := len(str)
padStr := padBuf.GetString(i)
padLength := len(padStr)
if targetLength < 0 || targetLength > b.tp.Flen || (strLength < targetLength && padLength == 0) {
result.AppendNull()
continue
}
if tailLen := targetLength - strLength; tailLen > 0 {
repeatCount := tailLen/padLength + 1
str = strings.Repeat(padStr, repeatCount)[:tailLen] + str
}
result.AppendString(str[:targetLength])
}
return nil
}

func (b *builtinLpadSig) 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 @@ -135,6 +135,18 @@ var vecBuiltinStringCases = map[string][]vecExprBenchCase{
childrenTypes: []types.EvalType{types.ETString, types.ETInt, types.ETString},
geners: []dataGenerator{&defaultGener{0.2, types.ETString}, &defaultGener{0.2, types.ETInt}, &defaultGener{0.2, types.ETString}},
},
{
retEvalType: types.ETString,
childrenTypes: []types.EvalType{types.ETString, types.ETInt, types.ETString},
childrenFieldTypes: []*types.FieldType{{Tp: mysql.TypeString, Flag: mysql.BinaryFlag, Collate: charset.CollationBin}},
geners: []dataGenerator{&randLenStrGener{0, 20}, &rangeInt64Gener{168435456, 368435456}, &randLenStrGener{0, 10}},
},
{
retEvalType: types.ETString,
childrenTypes: []types.EvalType{types.ETString, types.ETInt, types.ETString},
childrenFieldTypes: []*types.FieldType{{Tp: mysql.TypeString, Flag: mysql.BinaryFlag, Collate: charset.CollationBin}},
geners: []dataGenerator{&defaultGener{0.2, types.ETString}, &defaultGener{0.2, types.ETInt}, &defaultGener{0.2, types.ETString}},
},
},
ast.Rpad: {
{
Expand Down

0 comments on commit 0cc3dad

Please sign in to comment.