Skip to content

Commit

Permalink
expression: implement vectorized evaluation for `builtinCastStringAsI…
Browse files Browse the repository at this point in the history
…ntSig` (pingcap#13617)
  • Loading branch information
AerysNan authored and XiaTianliang committed Dec 21, 2019
1 parent a37618b commit d730e2f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
55 changes: 53 additions & 2 deletions expression/builtin_cast_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
package expression

import (
"math"
"strconv"
"strings"

"github.com/pingcap/errors"
"github.com/pingcap/parser/mysql"
Expand Down Expand Up @@ -810,11 +812,60 @@ func (b *builtinCastRealAsDecimalSig) vecEvalDecimal(input *chunk.Chunk, result
}

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

func (b *builtinCastStringAsIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.Column) error {
return errors.Errorf("not implemented")
n := input.NumRows()
if b.args[0].GetType().Hybrid() || IsBinaryLiteral(b.args[0]) {
return b.args[0].VecEvalInt(b.ctx, input, result)
}
result.ResizeInt64(n, false)
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
}
result.MergeNulls(buf)
sc := b.ctx.GetSessionVars().StmtCtx
i64s := result.Int64s()
isUnsigned := mysql.HasUnsignedFlag(b.tp.Flag)
unionUnsigned := isUnsigned && b.inUnion
for i := 0; i < n; i++ {
if result.IsNull(i) {
continue
}
var (
res int64
ures uint64
)
val := strings.TrimSpace(buf.GetString(i))
isNegative := len(val) > 1 && val[0] == '-'
if !isNegative {
ures, err = types.StrToUint(sc, val)
if !isUnsigned && err == nil && ures > uint64(math.MaxInt64) {
sc.AppendWarning(types.ErrCastAsSignedOverflow)
}
res = int64(ures)
} else if unionUnsigned {
res = 0
} else {
res, err = types.StrToInt(sc, val)
if err == nil && isUnsigned {
// If overflow, don't append this warnings
sc.AppendWarning(types.ErrCastNegIntAsUnsigned)
}
}
res, err = b.handleOverflow(res, val, err, isNegative)
if err != nil {
return err
}
i64s[i] = res
}
return nil
}

func (b *builtinCastStringAsDurationSig) vectorized() bool {
Expand Down
11 changes: 11 additions & 0 deletions expression/builtin_cast_vec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package expression

import (
"math"
"math/rand"
"testing"
"time"
Expand All @@ -34,6 +35,16 @@ var vecBuiltinCastCases = map[string][]vecExprBenchCase{
{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETJson}},
{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETDatetime}},
{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETDuration}},
{
retEvalType: types.ETInt,
childrenTypes: []types.EvalType{types.ETString},
geners: []dataGenerator{&numStrGener{rangeInt64Gener{math.MinInt64 + 1, 0}}},
},
{
retEvalType: types.ETInt,
childrenTypes: []types.EvalType{types.ETString},
geners: []dataGenerator{&numStrGener{rangeInt64Gener{0, math.MaxInt64}}},
},
{retEvalType: types.ETReal, childrenTypes: []types.EvalType{types.ETInt}},
{retEvalType: types.ETDuration, childrenTypes: []types.EvalType{types.ETInt}, geners: []dataGenerator{new(randDurInt)}},
{retEvalType: types.ETReal, childrenTypes: []types.EvalType{types.ETReal}},
Expand Down

0 comments on commit d730e2f

Please sign in to comment.