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 builtinCastStringAsIntSig #13617

Merged
merged 6 commits into from
Nov 26, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 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,57 @@ 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()
result.ResizeInt64(n, false)
if b.args[0].GetType().Hybrid() || IsBinaryLiteral(b.args[0]) {
return b.args[0].VecEvalInt(b.ctx, input, result)
}
AerysNan marked this conversation as resolved.
Show resolved Hide resolved
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()
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 err == nil && !mysql.HasUnsignedFlag(b.tp.Flag) && ures > uint64(math.MaxInt64) {
AerysNan marked this conversation as resolved.
Show resolved Hide resolved
sc.AppendWarning(types.ErrCastAsSignedOverflow)
}
res = int64(ures)
} else if b.inUnion && mysql.HasUnsignedFlag(b.tp.Flag) {
AerysNan marked this conversation as resolved.
Show resolved Hide resolved
res = 0
} else {
res, err = types.StrToInt(sc, val)
if err == nil && mysql.HasUnsignedFlag(b.tp.Flag) {
sc.AppendWarning(types.ErrCastNegIntAsUnsigned)
AerysNan marked this conversation as resolved.
Show resolved Hide resolved
}
}
res, err = b.handleOverflow(res, val, err, isNegative)
i64s[i] = res
AerysNan marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
}
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 @@ -33,6 +34,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}}},
},
AerysNan marked this conversation as resolved.
Show resolved Hide resolved
{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