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 'builtinTimeFormatSig' #12478

Merged
merged 7 commits into from
Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
39 changes: 39 additions & 0 deletions expression/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/types/json"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/math"
"github.com/pingcap/tidb/util/mock"
)

Expand Down Expand Up @@ -251,6 +252,44 @@ func (g *defaultGener) gen() interface{} {
return nil
}

type rangeDurationGener struct {
nullRation float64
}

func (g *rangeDurationGener) gen() interface{} {
if rand.Float64() < g.nullRation {
return nil
}
tm := (math.Abs(rand.Int63n(12))*3600 + math.Abs(rand.Int63n(60))*60 + math.Abs(rand.Int63n(60))) * 1000
tu := (tm + math.Abs(rand.Int63n(1000))) * 1000
return types.Duration{
Duration: time.Duration(tu * 1000)}
}

type timeFormatGener struct {
nullRation float64
}

func (g *timeFormatGener) gen() interface{} {
if rand.Float64() < g.nullRation {
return nil
}
switch rand.Uint32() % 4 {
case 0:
return "%H %i %S"
case 1:
return "%l %i %s"
case 2:
return "%p %i %s"
case 3:
return "%I %i %S %f"
case 4:
return "%T"
default:
return nil
}
}

// rangeRealGener is used to generate float64 items in [begin, end].
type rangeRealGener struct {
begin float64
Expand Down
35 changes: 33 additions & 2 deletions expression/builtin_time_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,42 @@ func (b *builtinWeekDaySig) vecEvalInt(input *chunk.Chunk, result *chunk.Column)
}

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

func (b *builtinTimeFormatSig) vecEvalString(input *chunk.Chunk, result *chunk.Column) error {
return errors.Errorf("not implemented")
n := input.NumRows()
buf, err := b.bufAllocator.get(types.ETDuration, n)
if err != nil {
return err
}
defer b.bufAllocator.put(buf)
if err := b.args[0].VecEvalDuration(b.ctx, input, buf); err != nil {
return err
}

buf1, err1 := b.bufAllocator.get(types.ETString, n)
if err1 != nil {
return err1
}
defer b.bufAllocator.put(buf1)
if err := b.args[1].VecEvalString(b.ctx, input, buf1); err != nil {
return err
}

result.ReserveString(n)
for i := 0; i < n; i++ {
if buf.IsNull(i) || buf1.IsNull(i) {
result.AppendNull()
continue
}
res, err := b.formatTime(b.ctx, buf.GetDuration(i, 0), buf1.GetString(i))
if err != nil {
return err
}
result.AppendString(res)
}
return nil
}

func (b *builtinUTCTimeWithArgSig) vectorized() bool {
Expand Down
42 changes: 22 additions & 20 deletions expression/builtin_time_vec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,28 @@ import (
)

var vecBuiltinTimeCases = map[string][]vecExprBenchCase{
ast.DateLiteral: {},
ast.DateDiff: {},
ast.TimeDiff: {},
ast.DateFormat: {},
ast.Hour: {},
ast.Minute: {},
ast.Second: {},
ast.MicroSecond: {},
ast.Now: {},
ast.DayOfWeek: {},
ast.DayOfYear: {},
ast.Day: {},
ast.CurrentTime: {},
ast.CurrentDate: {},
ast.MakeDate: {},
ast.MakeTime: {},
ast.PeriodAdd: {},
ast.PeriodDiff: {},
ast.Quarter: {},
ast.TimeFormat: {},
ast.DateLiteral: {},
ast.DateDiff: {},
ast.TimeDiff: {},
ast.DateFormat: {},
ast.Hour: {},
ast.Minute: {},
ast.Second: {},
ast.MicroSecond: {},
ast.Now: {},
ast.DayOfWeek: {},
ast.DayOfYear: {},
ast.Day: {},
ast.CurrentTime: {},
ast.CurrentDate: {},
ast.MakeDate: {},
ast.MakeTime: {},
ast.PeriodAdd: {},
ast.PeriodDiff: {},
ast.Quarter: {},
ast.TimeFormat: {
{retEvalType: types.ETString, childrenTypes: []types.EvalType{types.ETDuration, types.ETString}, geners: []dataGenerator{&rangeDurationGener{0.5}, &timeFormatGener{0.5}}},
},
ast.TimeToSec: {},
ast.TimestampAdd: {},
ast.TimestampDiff: {},
Expand Down