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 builtinCastIntAsDurationSig #12042

Merged
merged 11 commits into from
Sep 17, 2019
6 changes: 6 additions & 0 deletions expression/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,12 @@ func (g *randLenStrGener) gen() interface{} {
return string(buf)
}

type randDurInt struct{}

func (g *randDurInt) gen() interface{} {
return int64(rand.Intn(types.TimeMaxHour)*10000 + rand.Intn(60)*100 + rand.Intn(60))
}

type vecExprBenchCase struct {
retEvalType types.EvalType
childrenTypes []types.EvalType
Expand Down
40 changes: 40 additions & 0 deletions expression/builtin_cast_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,49 @@ package expression

import (
"github.com/pingcap/parser/mysql"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
)

func (b *builtinCastIntAsDurationSig) vecEvalDuration(input *chunk.Chunk, result *chunk.Column) error {
n := input.NumRows()
buf, err := b.bufAllocator.get(types.ETInt, n)
if err != nil {
return err
}
defer b.bufAllocator.put(buf)
if err := b.args[0].VecEvalInt(b.ctx, input, buf); err != nil {
return err
}

result.ResizeGoDuration(n, false)
result.MergeNulls(buf)
i64s := buf.Int64s()
ds := result.GoDurations()
for i := 0; i < n; i++ {
if result.IsNull(i) {
continue
}
dur, err := types.NumberToDuration(i64s[i], int8(b.tp.Decimal))
if err != nil {
if types.ErrOverflow.Equal(err) {
err = b.ctx.GetSessionVars().StmtCtx.HandleOverflow(err, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should the warning message be refined like this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In its row-based implementation, this error is not refined.

}
if err != nil {
return err
}
result.SetNull(i, true)
continue
}
ds[i] = dur.Duration
}
return nil
}

func (b *builtinCastIntAsDurationSig) vectorized() bool {
return true
}

func (b *builtinCastIntAsIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.Column) error {
if err := b.args[0].VecEvalInt(b.ctx, input, result); err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions expression/builtin_cast_vec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
var vecBuiltinCastCases = map[string][]vecExprBenchCase{
ast.Cast: {
{types.ETInt, []types.EvalType{types.ETInt}, nil},
{types.ETDuration, []types.EvalType{types.ETInt}, []dataGenerator{new(randDurInt)}},
},
}

Expand Down